LLVM API Documentation
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/Streams.h" 00027 using namespace llvm; 00028 using namespace llvm::dwarf; 00029 00030 // Handle the Pass registration stuff necessary to use TargetData's. 00031 static RegisterPass<MachineModuleInfo> 00032 X("machinemoduleinfo", "Module Information"); 00033 char MachineModuleInfo::ID = 0; 00034 00035 //===----------------------------------------------------------------------===// 00036 00037 MachineModuleInfo::MachineModuleInfo() 00038 : ImmutablePass(&ID) 00039 , Lines() 00040 , LabelIDList() 00041 , FrameMoves() 00042 , LandingPads() 00043 , Personalities() 00044 , CallsEHReturn(0) 00045 , CallsUnwindInit(0) 00046 , DbgInfoAvailable(false) 00047 { 00048 // Always emit "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 into UsedFunctions. 00092 GlobalVariable *GV = M.getGlobalVariable("llvm.used"); 00093 if (!GV || !GV->hasInitializer()) return; 00094 00095 // Should be an array of 'i8*'. 00096 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 00097 if (InitList == 0) return; 00098 00099 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 00100 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i))) 00101 if (CE->getOpcode() == Instruction::BitCast) 00102 if (Function *F = dyn_cast<Function>(CE->getOperand(0))) 00103 UsedFunctions.insert(F); 00104 } 00105 } 00106 00107 //===-EH-------------------------------------------------------------------===// 00108 00109 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the 00110 /// specified MachineBasicBlock. 00111 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo 00112 (MachineBasicBlock *LandingPad) { 00113 unsigned N = LandingPads.size(); 00114 for (unsigned i = 0; i < N; ++i) { 00115 LandingPadInfo &LP = LandingPads[i]; 00116 if (LP.LandingPadBlock == LandingPad) 00117 return LP; 00118 } 00119 00120 LandingPads.push_back(LandingPadInfo(LandingPad)); 00121 return LandingPads[N]; 00122 } 00123 00124 /// addInvoke - Provide the begin and end labels of an invoke style call and 00125 /// associate it with a try landing pad block. 00126 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad, 00127 unsigned BeginLabel, unsigned EndLabel) { 00128 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 00129 LP.BeginLabels.push_back(BeginLabel); 00130 LP.EndLabels.push_back(EndLabel); 00131 } 00132 00133 /// addLandingPad - Provide the label of a try LandingPad block. 00134 /// 00135 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) { 00136 unsigned LandingPadLabel = NextLabelID(); 00137 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 00138 LP.LandingPadLabel = LandingPadLabel; 00139 return LandingPadLabel; 00140 } 00141 00142 /// addPersonality - Provide the personality function for the exception 00143 /// information. 00144 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad, 00145 Function *Personality) { 00146 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 00147 LP.Personality = Personality; 00148 00149 for (unsigned i = 0; i < Personalities.size(); ++i) 00150 if (Personalities[i] == Personality) 00151 return; 00152 00153 Personalities.push_back(Personality); 00154 } 00155 00156 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. 00157 /// 00158 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad, 00159 std::vector<GlobalVariable *> &TyInfo) { 00160 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 00161 for (unsigned N = TyInfo.size(); N; --N) 00162 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 00163 } 00164 00165 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. 00166 /// 00167 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad, 00168 std::vector<GlobalVariable *> &TyInfo) { 00169 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 00170 std::vector<unsigned> IdsInFilter(TyInfo.size()); 00171 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 00172 IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 00173 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 00174 } 00175 00176 /// addCleanup - Add a cleanup action for a landing pad. 00177 /// 00178 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) { 00179 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 00180 LP.TypeIds.push_back(0); 00181 } 00182 00183 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing 00184 /// pads. 00185 void MachineModuleInfo::TidyLandingPads() { 00186 for (unsigned i = 0; i != LandingPads.size(); ) { 00187 LandingPadInfo &LandingPad = LandingPads[i]; 00188 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel); 00189 00190 // Special case: we *should* emit LPs with null LP MBB. This indicates 00191 // "nounwind" case. 00192 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 00193 LandingPads.erase(LandingPads.begin() + i); 00194 continue; 00195 } 00196 00197 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) { 00198 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]); 00199 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]); 00200 00201 if (!BeginLabel || !EndLabel) { 00202 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 00203 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 00204 continue; 00205 } 00206 00207 LandingPad.BeginLabels[j] = BeginLabel; 00208 LandingPad.EndLabels[j] = EndLabel; 00209 ++j; 00210 } 00211 00212 // Remove landing pads with no try-ranges. 00213 if (LandingPads[i].BeginLabels.empty()) { 00214 LandingPads.erase(LandingPads.begin() + i); 00215 continue; 00216 } 00217 00218 // If there is no landing pad, ensure that the list of typeids is empty. 00219 // If the only typeid is a cleanup, this is the same as having no typeids. 00220 if (!LandingPad.LandingPadBlock || 00221 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 00222 LandingPad.TypeIds.clear(); 00223 00224 ++i; 00225 } 00226 } 00227 00228 /// getTypeIDFor - Return the type id for the specified typeinfo. This is 00229 /// function wide. 00230 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) { 00231 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 00232 if (TypeInfos[i] == TI) return i + 1; 00233 00234 TypeInfos.push_back(TI); 00235 return TypeInfos.size(); 00236 } 00237 00238 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is 00239 /// function wide. 00240 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) { 00241 // If the new filter coincides with the tail of an existing filter, then 00242 // re-use the existing filter. Folding filters more than this requires 00243 // re-ordering filters and/or their elements - probably not worth it. 00244 for (std::vector<unsigned>::iterator I = FilterEnds.begin(), 00245 E = FilterEnds.end(); I != E; ++I) { 00246 unsigned i = *I, j = TyIds.size(); 00247 00248 while (i && j) 00249 if (FilterIds[--i] != TyIds[--j]) 00250 goto try_next; 00251 00252 if (!j) 00253 // The new filter coincides with range [i, end) of the existing filter. 00254 return -(1 + i); 00255 00256 try_next:; 00257 } 00258 00259 // Add the new filter. 00260 int FilterID = -(1 + FilterIds.size()); 00261 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 00262 for (unsigned I = 0, N = TyIds.size(); I != N; ++I) 00263 FilterIds.push_back(TyIds[I]); 00264 FilterEnds.push_back(FilterIds.size()); 00265 FilterIds.push_back(0); // terminator 00266 return FilterID; 00267 } 00268 00269 /// getPersonality - Return the personality function for the current function. 00270 Function *MachineModuleInfo::getPersonality() const { 00271 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per 00272 // function 00273 return !LandingPads.empty() ? LandingPads[0].Personality : NULL; 00274 } 00275 00276 /// getPersonalityIndex - Return unique index for current personality 00277 /// function. NULL personality function should always get zero index. 00278 unsigned MachineModuleInfo::getPersonalityIndex() const { 00279 const Function* Personality = NULL; 00280 00281 // Scan landing pads. If there is at least one non-NULL personality - use it. 00282 for (unsigned i = 0; i != LandingPads.size(); ++i) 00283 if (LandingPads[i].Personality) { 00284 Personality = LandingPads[i].Personality; 00285 break; 00286 } 00287 00288 for (unsigned i = 0; i < Personalities.size(); ++i) { 00289 if (Personalities[i] == Personality) 00290 return i; 00291 } 00292 00293 // This should never happen 00294 assert(0 && "Personality function should be set!"); 00295 return 0; 00296 } 00297 00298 //===----------------------------------------------------------------------===// 00299 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows 00300 /// a info consumer to determine if the range of two labels is empty, by seeing 00301 /// if the labels map to the same reduced label. 00302 00303 namespace llvm { 00304 00305 struct DebugLabelFolder : public MachineFunctionPass { 00306 static char ID; 00307 DebugLabelFolder() : MachineFunctionPass(&ID) {} 00308 00309 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00310 AU.addPreservedID(MachineLoopInfoID); 00311 AU.addPreservedID(MachineDominatorsID); 00312 MachineFunctionPass::getAnalysisUsage(AU); 00313 } 00314 00315 virtual bool runOnMachineFunction(MachineFunction &MF); 00316 virtual const char *getPassName() const { return "Label Folder"; } 00317 }; 00318 00319 char DebugLabelFolder::ID = 0; 00320 00321 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) { 00322 // Get machine module info. 00323 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>(); 00324 if (!MMI) return false; 00325 00326 // Track if change is made. 00327 bool MadeChange = false; 00328 // No prior label to begin. 00329 unsigned PriorLabel = 0; 00330 00331 // Iterate through basic blocks. 00332 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); 00333 BB != E; ++BB) { 00334 // Iterate through instructions. 00335 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 00336 // Is it a label. 00337 if (I->isDebugLabel()) { 00338 // The label ID # is always operand #0, an immediate. 00339 unsigned NextLabel = I->getOperand(0).getImm(); 00340 00341 // If there was an immediate prior label. 00342 if (PriorLabel) { 00343 // Remap the current label to prior label. 00344 MMI->RemapLabel(NextLabel, PriorLabel); 00345 // Delete the current label. 00346 I = BB->erase(I); 00347 // Indicate a change has been made. 00348 MadeChange = true; 00349 continue; 00350 } else { 00351 // Start a new round. 00352 PriorLabel = NextLabel; 00353 } 00354 } else { 00355 // No consecutive labels. 00356 PriorLabel = 0; 00357 } 00358 00359 ++I; 00360 } 00361 } 00362 00363 return MadeChange; 00364 } 00365 00366 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); } 00367 00368 } 00369