LLVM API Documentation

MachineModuleInfo.cpp

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/CodeGen/MachineModuleInfo.h"
00011 
00012 #include "llvm/Constants.h"
00013 #include "llvm/Analysis/ValueTracking.h"
00014 #include "llvm/CodeGen/MachineFunctionPass.h"
00015 #include "llvm/CodeGen/MachineFunction.h"
00016 #include "llvm/CodeGen/Passes.h"
00017 #include "llvm/Target/TargetInstrInfo.h"
00018 #include "llvm/Target/TargetMachine.h"
00019 #include "llvm/Target/TargetOptions.h"
00020 #include "llvm/DerivedTypes.h"
00021 #include "llvm/GlobalVariable.h"
00022 #include "llvm/Intrinsics.h"
00023 #include "llvm/Instructions.h"
00024 #include "llvm/Module.h"
00025 #include "llvm/Support/Dwarf.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 #include "llvm/Support/Streams.h"
00028 using namespace llvm;
00029 using namespace llvm::dwarf;
00030 
00031 // Handle the Pass registration stuff necessary to use TargetData's.
00032 static RegisterPass<MachineModuleInfo>
00033 X("machinemoduleinfo", "Module Information");
00034 char MachineModuleInfo::ID = 0;
00035 
00036 //===----------------------------------------------------------------------===//
00037   
00038 MachineModuleInfo::MachineModuleInfo()
00039 : ImmutablePass(&ID)
00040 , LabelIDList()
00041 , FrameMoves()
00042 , LandingPads()
00043 , Personalities()
00044 , CallsEHReturn(0)
00045 , CallsUnwindInit(0)
00046 , DbgInfoAvailable(false)
00047 {
00048   // Always emit some info, by default "no personality" info.
00049   Personalities.push_back(NULL);
00050 }
00051 MachineModuleInfo::~MachineModuleInfo() {
00052 
00053 }
00054 
00055 /// doInitialization - Initialize the state for a new module.
00056 ///
00057 bool MachineModuleInfo::doInitialization() {
00058   return false;
00059 }
00060 
00061 /// doFinalization - Tear down the state after completion of a module.
00062 ///
00063 bool MachineModuleInfo::doFinalization() {
00064   return false;
00065 }
00066 
00067 /// BeginFunction - Begin gathering function meta information.
00068 ///
00069 void MachineModuleInfo::BeginFunction(MachineFunction *MF) {
00070   // Coming soon.
00071 }
00072 
00073 /// EndFunction - Discard function meta information.
00074 ///
00075 void MachineModuleInfo::EndFunction() {
00076   // Clean up frame info.
00077   FrameMoves.clear();
00078   
00079   // Clean up exception info.
00080   LandingPads.clear();
00081   TypeInfos.clear();
00082   FilterIds.clear();
00083   FilterEnds.clear();
00084   CallsEHReturn = 0;
00085   CallsUnwindInit = 0;
00086 }
00087 
00088 /// AnalyzeModule - Scan the module for global debug information.
00089 ///
00090 void MachineModuleInfo::AnalyzeModule(Module &M) {
00091   // Insert functions in the llvm.used array (but not llvm.compiler.used) into
00092   // UsedFunctions.
00093   GlobalVariable *GV = M.getGlobalVariable("llvm.used");
00094   if (!GV || !GV->hasInitializer()) return;
00095 
00096   // Should be an array of 'i8*'.
00097   ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
00098   if (InitList == 0) return;
00099 
00100   for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
00101     if (Function *F =
00102           dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts()))
00103       UsedFunctions.insert(F);
00104 }
00105 
00106 //===-EH-------------------------------------------------------------------===//
00107 
00108 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the
00109 /// specified MachineBasicBlock.
00110 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo
00111     (MachineBasicBlock *LandingPad) {
00112   unsigned N = LandingPads.size();
00113   for (unsigned i = 0; i < N; ++i) {
00114     LandingPadInfo &LP = LandingPads[i];
00115     if (LP.LandingPadBlock == LandingPad)
00116       return LP;
00117   }
00118   
00119   LandingPads.push_back(LandingPadInfo(LandingPad));
00120   return LandingPads[N];
00121 }
00122 
00123 /// addInvoke - Provide the begin and end labels of an invoke style call and
00124 /// associate it with a try landing pad block.
00125 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad,
00126                                   unsigned BeginLabel, unsigned EndLabel) {
00127   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00128   LP.BeginLabels.push_back(BeginLabel);
00129   LP.EndLabels.push_back(EndLabel);
00130 }
00131 
00132 /// addLandingPad - Provide the label of a try LandingPad block.
00133 ///
00134 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) {
00135   unsigned LandingPadLabel = NextLabelID();
00136   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00137   LP.LandingPadLabel = LandingPadLabel;  
00138   return LandingPadLabel;
00139 }
00140 
00141 /// addPersonality - Provide the personality function for the exception
00142 /// information.
00143 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad,
00144                                        Function *Personality) {
00145   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00146   LP.Personality = Personality;
00147 
00148   for (unsigned i = 0; i < Personalities.size(); ++i)
00149     if (Personalities[i] == Personality)
00150       return;
00151   
00152   // If this is the first personality we're adding go
00153   // ahead and add it at the beginning.
00154   if (Personalities[0] == NULL)
00155     Personalities[0] = Personality;
00156   else
00157     Personalities.push_back(Personality);
00158 }
00159 
00160 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
00161 ///
00162 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad,
00163                                         std::vector<GlobalVariable *> &TyInfo) {
00164   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00165   for (unsigned N = TyInfo.size(); N; --N)
00166     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
00167 }
00168 
00169 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad.
00170 ///
00171 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad,
00172                                         std::vector<GlobalVariable *> &TyInfo) {
00173   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00174   std::vector<unsigned> IdsInFilter(TyInfo.size());
00175   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
00176     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
00177   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
00178 }
00179 
00180 /// addCleanup - Add a cleanup action for a landing pad.
00181 ///
00182 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) {
00183   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
00184   LP.TypeIds.push_back(0);
00185 }
00186 
00187 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing
00188 /// pads.
00189 void MachineModuleInfo::TidyLandingPads() {
00190   for (unsigned i = 0; i != LandingPads.size(); ) {
00191     LandingPadInfo &LandingPad = LandingPads[i];
00192     LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel);
00193 
00194     // Special case: we *should* emit LPs with null LP MBB. This indicates
00195     // "nounwind" case.
00196     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
00197       LandingPads.erase(LandingPads.begin() + i);
00198       continue;
00199     }
00200 
00201     for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) {
00202       unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]);
00203       unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]);
00204 
00205       if (!BeginLabel || !EndLabel) {
00206         LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
00207         LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
00208         continue;
00209       }
00210 
00211       LandingPad.BeginLabels[j] = BeginLabel;
00212       LandingPad.EndLabels[j] = EndLabel;
00213       ++j;
00214     }
00215 
00216     // Remove landing pads with no try-ranges.
00217     if (LandingPads[i].BeginLabels.empty()) {
00218       LandingPads.erase(LandingPads.begin() + i);
00219       continue;
00220     }
00221 
00222     // If there is no landing pad, ensure that the list of typeids is empty.
00223     // If the only typeid is a cleanup, this is the same as having no typeids.
00224     if (!LandingPad.LandingPadBlock ||
00225         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
00226       LandingPad.TypeIds.clear();
00227 
00228     ++i;
00229   }
00230 }
00231 
00232 /// getTypeIDFor - Return the type id for the specified typeinfo.  This is 
00233 /// function wide.
00234 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) {
00235   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
00236     if (TypeInfos[i] == TI) return i + 1;
00237 
00238   TypeInfos.push_back(TI);
00239   return TypeInfos.size();
00240 }
00241 
00242 /// getFilterIDFor - Return the filter id for the specified typeinfos.  This is
00243 /// function wide.
00244 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) {
00245   // If the new filter coincides with the tail of an existing filter, then
00246   // re-use the existing filter.  Folding filters more than this requires
00247   // re-ordering filters and/or their elements - probably not worth it.
00248   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
00249        E = FilterEnds.end(); I != E; ++I) {
00250     unsigned i = *I, j = TyIds.size();
00251 
00252     while (i && j)
00253       if (FilterIds[--i] != TyIds[--j])
00254         goto try_next;
00255 
00256     if (!j)
00257       // The new filter coincides with range [i, end) of the existing filter.
00258       return -(1 + i);
00259 
00260 try_next:;
00261   }
00262 
00263   // Add the new filter.
00264   int FilterID = -(1 + FilterIds.size());
00265   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
00266   for (unsigned I = 0, N = TyIds.size(); I != N; ++I)
00267     FilterIds.push_back(TyIds[I]);
00268   FilterEnds.push_back(FilterIds.size());
00269   FilterIds.push_back(0); // terminator
00270   return FilterID;
00271 }
00272 
00273 /// getPersonality - Return the personality function for the current function.
00274 Function *MachineModuleInfo::getPersonality() const {
00275   // FIXME: Until PR1414 will be fixed, we're using 1 personality function per
00276   // function
00277   return !LandingPads.empty() ? LandingPads[0].Personality : NULL;
00278 }
00279 
00280 /// getPersonalityIndex - Return unique index for current personality
00281 /// function. NULL/first personality function should always get zero index.
00282 unsigned MachineModuleInfo::getPersonalityIndex() const {
00283   const Function* Personality = NULL;
00284   
00285   // Scan landing pads. If there is at least one non-NULL personality - use it.
00286   for (unsigned i = 0; i != LandingPads.size(); ++i)
00287     if (LandingPads[i].Personality) {
00288       Personality = LandingPads[i].Personality;
00289       break;
00290     }
00291   
00292   for (unsigned i = 0; i < Personalities.size(); ++i) {
00293     if (Personalities[i] == Personality)
00294       return i;
00295   }
00296 
00297   // This will happen if the current personality function is
00298   // in the zero index.
00299   return 0;
00300 }
00301 
00302 //===----------------------------------------------------------------------===//
00303 /// DebugLabelFolding pass - This pass prunes out redundant labels.  This allows
00304 /// a info consumer to determine if the range of two labels is empty, by seeing
00305 /// if the labels map to the same reduced label.
00306 
00307 namespace llvm {
00308 
00309 struct DebugLabelFolder : public MachineFunctionPass {
00310   static char ID;
00311   DebugLabelFolder() : MachineFunctionPass(&ID) {}
00312 
00313   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00314     AU.setPreservesCFG();
00315     AU.addPreservedID(MachineLoopInfoID);
00316     AU.addPreservedID(MachineDominatorsID);
00317     MachineFunctionPass::getAnalysisUsage(AU);
00318   }
00319 
00320   virtual bool runOnMachineFunction(MachineFunction &MF);
00321   virtual const char *getPassName() const { return "Label Folder"; }
00322 };
00323 
00324 char DebugLabelFolder::ID = 0;
00325 
00326 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) {
00327   // Get machine module info.
00328   MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>();
00329   if (!MMI) return false;
00330   
00331   // Track if change is made.
00332   bool MadeChange = false;
00333   // No prior label to begin.
00334   unsigned PriorLabel = 0;
00335   
00336   // Iterate through basic blocks.
00337   for (MachineFunction::iterator BB = MF.begin(), E = MF.end();
00338        BB != E; ++BB) {
00339     // Iterate through instructions.
00340     for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
00341       // Is it a label.
00342       if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){
00343         // The label ID # is always operand #0, an immediate.
00344         unsigned NextLabel = I->getOperand(0).getImm();
00345         
00346         // If there was an immediate prior label.
00347         if (PriorLabel) {
00348           // Remap the current label to prior label.
00349           MMI->RemapLabel(NextLabel, PriorLabel);
00350           // Delete the current label.
00351           I = BB->erase(I);
00352           // Indicate a change has been made.
00353           MadeChange = true;
00354           continue;
00355         } else {
00356           // Start a new round.
00357           PriorLabel = NextLabel;
00358         }
00359        } else {
00360         // No consecutive labels.
00361         PriorLabel = 0;
00362       }
00363       
00364       ++I;
00365     }
00366   }
00367   
00368   return MadeChange;
00369 }
00370 
00371 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); }
00372 
00373 }
00374