1 //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/CodeGen/MachineModuleInfo.h" 11 12 #include "llvm/Constants.h" 13 #include "llvm/Analysis/ValueTracking.h" 14 #include "llvm/CodeGen/MachineFunctionPass.h" 15 #include "llvm/CodeGen/MachineFunction.h" 16 #include "llvm/CodeGen/Passes.h" 17 #include "llvm/Target/TargetInstrInfo.h" 18 #include "llvm/Target/TargetMachine.h" 19 #include "llvm/Target/TargetOptions.h" 20 #include "llvm/DerivedTypes.h" 21 #include "llvm/GlobalVariable.h" 22 #include "llvm/Intrinsics.h" 23 #include "llvm/Instructions.h" 24 #include "llvm/Module.h" 25 #include "llvm/Support/Dwarf.h" 26 #include "llvm/Support/ErrorHandling.h" 27 using namespace llvm; 28 using namespace llvm::dwarf; 29 30 // Handle the Pass registration stuff necessary to use TargetData's. 31 static RegisterPass<MachineModuleInfo> 32 X("machinemoduleinfo", "Module Information"); 33 char MachineModuleInfo::ID = 0; 34 35 // Out of line virtual method. 36 MachineModuleInfoImpl::~MachineModuleInfoImpl() {} 37 38 //===----------------------------------------------------------------------===// 39 40 MachineModuleInfo::MachineModuleInfo() 41 : ImmutablePass(&ID) 42 , ObjFileMMI(0) 43 , CallsEHReturn(0) 44 , CallsUnwindInit(0) 45 , DbgInfoAvailable(false) { 46 // Always emit some info, by default "no personality" info. 47 Personalities.push_back(NULL); 48 } 49 50 MachineModuleInfo::~MachineModuleInfo() { 51 delete ObjFileMMI; 52 } 53 54 /// doInitialization - Initialize the state for a new module. 55 /// 56 bool MachineModuleInfo::doInitialization() { 57 return false; 58 } 59 60 /// doFinalization - Tear down the state after completion of a module. 61 /// 62 bool MachineModuleInfo::doFinalization() { 63 return false; 64 } 65 66 /// EndFunction - Discard function meta information. 67 /// 68 void MachineModuleInfo::EndFunction() { 69 // Clean up frame info. 70 FrameMoves.clear(); 71 72 // Clean up exception info. 73 LandingPads.clear(); 74 TypeInfos.clear(); 75 FilterIds.clear(); 76 FilterEnds.clear(); 77 CallsEHReturn = 0; 78 CallsUnwindInit = 0; 79 } 80 81 /// AnalyzeModule - Scan the module for global debug information. 82 /// 83 void MachineModuleInfo::AnalyzeModule(Module &M) { 84 // Insert functions in the llvm.used array (but not llvm.compiler.used) into 85 // UsedFunctions. 86 GlobalVariable *GV = M.getGlobalVariable("llvm.used"); 87 if (!GV || !GV->hasInitializer()) return; 88 89 // Should be an array of 'i8*'. 90 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 91 if (InitList == 0) return; 92 93 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 94 if (Function *F = 95 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts())) 96 UsedFunctions.insert(F); 97 } 98 99 //===-EH-------------------------------------------------------------------===// 100 101 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the 102 /// specified MachineBasicBlock. 103 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo 104 (MachineBasicBlock *LandingPad) { 105 unsigned N = LandingPads.size(); 106 for (unsigned i = 0; i < N; ++i) { 107 LandingPadInfo &LP = LandingPads[i]; 108 if (LP.LandingPadBlock == LandingPad) 109 return LP; 110 } 111 112 LandingPads.push_back(LandingPadInfo(LandingPad)); 113 return LandingPads[N]; 114 } 115 116 /// addInvoke - Provide the begin and end labels of an invoke style call and 117 /// associate it with a try landing pad block. 118 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad, 119 unsigned BeginLabel, unsigned EndLabel) { 120 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 121 LP.BeginLabels.push_back(BeginLabel); 122 LP.EndLabels.push_back(EndLabel); 123 } 124 125 /// addLandingPad - Provide the label of a try LandingPad block. 126 /// 127 unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) { 128 unsigned LandingPadLabel = NextLabelID(); 129 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 130 LP.LandingPadLabel = LandingPadLabel; 131 return LandingPadLabel; 132 } 133 134 /// addPersonality - Provide the personality function for the exception 135 /// information. 136 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad, 137 Function *Personality) { 138 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 139 LP.Personality = Personality; 140 141 for (unsigned i = 0; i < Personalities.size(); ++i) 142 if (Personalities[i] == Personality) 143 return; 144 145 // If this is the first personality we're adding go 146 // ahead and add it at the beginning. 147 if (Personalities[0] == NULL) 148 Personalities[0] = Personality; 149 else 150 Personalities.push_back(Personality); 151 } 152 153 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. 154 /// 155 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad, 156 std::vector<GlobalVariable *> &TyInfo) { 157 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 158 for (unsigned N = TyInfo.size(); N; --N) 159 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 160 } 161 162 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. 163 /// 164 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad, 165 std::vector<GlobalVariable *> &TyInfo) { 166 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 167 std::vector<unsigned> IdsInFilter(TyInfo.size()); 168 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 169 IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 170 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 171 } 172 173 /// addCleanup - Add a cleanup action for a landing pad. 174 /// 175 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) { 176 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 177 LP.TypeIds.push_back(0); 178 } 179 180 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing 181 /// pads. 182 void MachineModuleInfo::TidyLandingPads() { 183 for (unsigned i = 0; i != LandingPads.size(); ) { 184 LandingPadInfo &LandingPad = LandingPads[i]; 185 LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel); 186 187 // Special case: we *should* emit LPs with null LP MBB. This indicates 188 // "nounwind" case. 189 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 190 LandingPads.erase(LandingPads.begin() + i); 191 continue; 192 } 193 194 for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) { 195 unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]); 196 unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]); 197 198 if (!BeginLabel || !EndLabel) { 199 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 200 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 201 continue; 202 } 203 204 LandingPad.BeginLabels[j] = BeginLabel; 205 LandingPad.EndLabels[j] = EndLabel; 206 ++j; 207 } 208 209 // Remove landing pads with no try-ranges. 210 if (LandingPads[i].BeginLabels.empty()) { 211 LandingPads.erase(LandingPads.begin() + i); 212 continue; 213 } 214 215 // If there is no landing pad, ensure that the list of typeids is empty. 216 // If the only typeid is a cleanup, this is the same as having no typeids. 217 if (!LandingPad.LandingPadBlock || 218 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 219 LandingPad.TypeIds.clear(); 220 221 ++i; 222 } 223 } 224 225 /// getTypeIDFor - Return the type id for the specified typeinfo. This is 226 /// function wide. 227 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) { 228 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 229 if (TypeInfos[i] == TI) return i + 1; 230 231 TypeInfos.push_back(TI); 232 return TypeInfos.size(); 233 } 234 235 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is 236 /// function wide. 237 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) { 238 // If the new filter coincides with the tail of an existing filter, then 239 // re-use the existing filter. Folding filters more than this requires 240 // re-ordering filters and/or their elements - probably not worth it. 241 for (std::vector<unsigned>::iterator I = FilterEnds.begin(), 242 E = FilterEnds.end(); I != E; ++I) { 243 unsigned i = *I, j = TyIds.size(); 244 245 while (i && j) 246 if (FilterIds[--i] != TyIds[--j]) 247 goto try_next; 248 249 if (!j) 250 // The new filter coincides with range [i, end) of the existing filter. 251 return -(1 + i); 252 253 try_next:; 254 } 255 256 // Add the new filter. 257 int FilterID = -(1 + FilterIds.size()); 258 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 259 for (unsigned I = 0, N = TyIds.size(); I != N; ++I) 260 FilterIds.push_back(TyIds[I]); 261 FilterEnds.push_back(FilterIds.size()); 262 FilterIds.push_back(0); // terminator 263 return FilterID; 264 } 265 266 /// getPersonality - Return the personality function for the current function. 267 Function *MachineModuleInfo::getPersonality() const { 268 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per 269 // function 270 return !LandingPads.empty() ? LandingPads[0].Personality : NULL; 271 } 272 273 /// getPersonalityIndex - Return unique index for current personality 274 /// function. NULL/first personality function should always get zero index. 275 unsigned MachineModuleInfo::getPersonalityIndex() const { 276 const Function* Personality = NULL; 277 278 // Scan landing pads. If there is at least one non-NULL personality - use it. 279 for (unsigned i = 0; i != LandingPads.size(); ++i) 280 if (LandingPads[i].Personality) { 281 Personality = LandingPads[i].Personality; 282 break; 283 } 284 285 for (unsigned i = 0; i < Personalities.size(); ++i) { 286 if (Personalities[i] == Personality) 287 return i; 288 } 289 290 // This will happen if the current personality function is 291 // in the zero index. 292 return 0; 293 } 294 295 //===----------------------------------------------------------------------===// 296 /// DebugLabelFolding pass - This pass prunes out redundant labels. This allows 297 /// a info consumer to determine if the range of two labels is empty, by seeing 298 /// if the labels map to the same reduced label. 299 300 namespace llvm { 301 302 struct DebugLabelFolder : public MachineFunctionPass { 303 static char ID; 304 DebugLabelFolder() : MachineFunctionPass(&ID) {} 305 306 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 307 AU.setPreservesCFG(); 308 AU.addPreservedID(MachineLoopInfoID); 309 AU.addPreservedID(MachineDominatorsID); 310 MachineFunctionPass::getAnalysisUsage(AU); 311 } 312 313 virtual bool runOnMachineFunction(MachineFunction &MF); 314 virtual const char *getPassName() const { return "Label Folder"; } 315 }; 316 317 char DebugLabelFolder::ID = 0; 318 319 bool DebugLabelFolder::runOnMachineFunction(MachineFunction &MF) { 320 // Get machine module info. 321 MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>(); 322 if (!MMI) return false; 323 324 // Track if change is made. 325 bool MadeChange = false; 326 // No prior label to begin. 327 unsigned PriorLabel = 0; 328 329 // Iterate through basic blocks. 330 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); 331 BB != E; ++BB) { 332 // Iterate through instructions. 333 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 334 // Is it a label. 335 if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){ 336 // The label ID # is always operand #0, an immediate. 337 unsigned NextLabel = I->getOperand(0).getImm(); 338 339 // If there was an immediate prior label. 340 if (PriorLabel) { 341 // Remap the current label to prior label. 342 MMI->RemapLabel(NextLabel, PriorLabel); 343 // Delete the current label. 344 I = BB->erase(I); 345 // Indicate a change has been made. 346 MadeChange = true; 347 continue; 348 } else { 349 // Start a new round. 350 PriorLabel = NextLabel; 351 } 352 } else { 353 // No consecutive labels. 354 PriorLabel = 0; 355 } 356 357 ++I; 358 } 359 } 360 361 return MadeChange; 362 } 363 364 FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); } 365 366 } 367