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/DerivedTypes.h" 14 #include "llvm/GlobalVariable.h" 15 #include "llvm/Intrinsics.h" 16 #include "llvm/Instructions.h" 17 #include "llvm/Module.h" 18 #include "llvm/Analysis/ValueTracking.h" 19 #include "llvm/CodeGen/MachineFunctionPass.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/Passes.h" 22 #include "llvm/Target/TargetInstrInfo.h" 23 #include "llvm/Target/TargetMachine.h" 24 #include "llvm/Target/TargetOptions.h" 25 #include "llvm/MC/MCSymbol.h" 26 #include "llvm/Support/Dwarf.h" 27 #include "llvm/Support/ErrorHandling.h" 28 using namespace llvm; 29 using namespace llvm::dwarf; 30 31 // Handle the Pass registration stuff necessary to use TargetData's. 32 static RegisterPass<MachineModuleInfo> 33 X("machinemoduleinfo", "Machine Module Information"); 34 char MachineModuleInfo::ID = 0; 35 36 // Out of line virtual method. 37 MachineModuleInfoImpl::~MachineModuleInfoImpl() {} 38 39 //===----------------------------------------------------------------------===// 40 41 MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI) 42 : ImmutablePass(&ID), Context(MAI), 43 ObjFileMMI(0), 44 CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0), DbgInfoAvailable(false){ 45 // Always emit some info, by default "no personality" info. 46 Personalities.push_back(NULL); 47 } 48 49 MachineModuleInfo::MachineModuleInfo() 50 : ImmutablePass(&ID), Context(*(MCAsmInfo*)0) { 51 assert(0 && "This MachineModuleInfo constructor should never be called, MMI " 52 "should always be explicitly constructed by LLVMTargetMachine"); 53 abort(); 54 } 55 56 MachineModuleInfo::~MachineModuleInfo() { 57 delete ObjFileMMI; 58 } 59 60 /// doInitialization - Initialize the state for a new module. 61 /// 62 bool MachineModuleInfo::doInitialization() { 63 return false; 64 } 65 66 /// doFinalization - Tear down the state after completion of a module. 67 /// 68 bool MachineModuleInfo::doFinalization() { 69 return false; 70 } 71 72 /// EndFunction - Discard function meta information. 73 /// 74 void MachineModuleInfo::EndFunction() { 75 // Clean up frame info. 76 FrameMoves.clear(); 77 78 // Clean up exception info. 79 LandingPads.clear(); 80 CallSiteMap.clear(); 81 TypeInfos.clear(); 82 FilterIds.clear(); 83 FilterEnds.clear(); 84 CallsEHReturn = 0; 85 CallsUnwindInit = 0; 86 VariableDbgInfo.clear(); 87 } 88 89 /// AnalyzeModule - Scan the module for global debug information. 90 /// 91 void MachineModuleInfo::AnalyzeModule(Module &M) { 92 // Insert functions in the llvm.used array (but not llvm.compiler.used) into 93 // UsedFunctions. 94 GlobalVariable *GV = M.getGlobalVariable("llvm.used"); 95 if (!GV || !GV->hasInitializer()) return; 96 97 // Should be an array of 'i8*'. 98 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 99 if (InitList == 0) return; 100 101 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) 102 if (Function *F = 103 dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts())) 104 UsedFunctions.insert(F); 105 } 106 107 /// getAddrLabelSymbol - Return the symbol to be used for the specified basic 108 /// block when its address is taken. This cannot be its normal LBB label 109 /// because the block may be accessed outside its containing function. 110 MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) { 111 assert(BB->hasAddressTaken() && 112 "Shouldn't get label for block without address taken"); 113 MCSymbol *&Entry = AddrLabelSymbols[const_cast<BasicBlock*>(BB)]; 114 if (Entry) return Entry; 115 return Entry = Context.CreateTempSymbol(); 116 } 117 118 119 //===-EH-------------------------------------------------------------------===// 120 121 /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the 122 /// specified MachineBasicBlock. 123 LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo 124 (MachineBasicBlock *LandingPad) { 125 unsigned N = LandingPads.size(); 126 for (unsigned i = 0; i < N; ++i) { 127 LandingPadInfo &LP = LandingPads[i]; 128 if (LP.LandingPadBlock == LandingPad) 129 return LP; 130 } 131 132 LandingPads.push_back(LandingPadInfo(LandingPad)); 133 return LandingPads[N]; 134 } 135 136 /// addInvoke - Provide the begin and end labels of an invoke style call and 137 /// associate it with a try landing pad block. 138 void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad, 139 MCSymbol *BeginLabel, MCSymbol *EndLabel) { 140 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 141 LP.BeginLabels.push_back(BeginLabel); 142 LP.EndLabels.push_back(EndLabel); 143 } 144 145 /// addLandingPad - Provide the label of a try LandingPad block. 146 /// 147 MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) { 148 MCSymbol *LandingPadLabel = Context.CreateTempSymbol(); 149 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 150 LP.LandingPadLabel = LandingPadLabel; 151 return LandingPadLabel; 152 } 153 154 /// addPersonality - Provide the personality function for the exception 155 /// information. 156 void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad, 157 Function *Personality) { 158 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 159 LP.Personality = Personality; 160 161 for (unsigned i = 0; i < Personalities.size(); ++i) 162 if (Personalities[i] == Personality) 163 return; 164 165 // If this is the first personality we're adding go 166 // ahead and add it at the beginning. 167 if (Personalities[0] == NULL) 168 Personalities[0] = Personality; 169 else 170 Personalities.push_back(Personality); 171 } 172 173 /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. 174 /// 175 void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad, 176 std::vector<GlobalVariable *> &TyInfo) { 177 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 178 for (unsigned N = TyInfo.size(); N; --N) 179 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 180 } 181 182 /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. 183 /// 184 void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad, 185 std::vector<GlobalVariable *> &TyInfo) { 186 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 187 std::vector<unsigned> IdsInFilter(TyInfo.size()); 188 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 189 IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 190 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 191 } 192 193 /// addCleanup - Add a cleanup action for a landing pad. 194 /// 195 void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) { 196 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 197 LP.TypeIds.push_back(0); 198 } 199 200 /// TidyLandingPads - Remap landing pad labels and remove any deleted landing 201 /// pads. 202 void MachineModuleInfo::TidyLandingPads() { 203 for (unsigned i = 0; i != LandingPads.size(); ) { 204 LandingPadInfo &LandingPad = LandingPads[i]; 205 if (LandingPad.LandingPadLabel && !LandingPad.LandingPadLabel->isDefined()) 206 LandingPad.LandingPadLabel = 0; 207 208 // Special case: we *should* emit LPs with null LP MBB. This indicates 209 // "nounwind" case. 210 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 211 LandingPads.erase(LandingPads.begin() + i); 212 continue; 213 } 214 215 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { 216 MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; 217 MCSymbol *EndLabel = LandingPad.EndLabels[j]; 218 if (BeginLabel->isDefined() && EndLabel->isDefined()) continue; 219 220 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 221 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 222 --j, --e; 223 } 224 225 // Remove landing pads with no try-ranges. 226 if (LandingPads[i].BeginLabels.empty()) { 227 LandingPads.erase(LandingPads.begin() + i); 228 continue; 229 } 230 231 // If there is no landing pad, ensure that the list of typeids is empty. 232 // If the only typeid is a cleanup, this is the same as having no typeids. 233 if (!LandingPad.LandingPadBlock || 234 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 235 LandingPad.TypeIds.clear(); 236 ++i; 237 } 238 } 239 240 /// getTypeIDFor - Return the type id for the specified typeinfo. This is 241 /// function wide. 242 unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) { 243 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 244 if (TypeInfos[i] == TI) return i + 1; 245 246 TypeInfos.push_back(TI); 247 return TypeInfos.size(); 248 } 249 250 /// getFilterIDFor - Return the filter id for the specified typeinfos. This is 251 /// function wide. 252 int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) { 253 // If the new filter coincides with the tail of an existing filter, then 254 // re-use the existing filter. Folding filters more than this requires 255 // re-ordering filters and/or their elements - probably not worth it. 256 for (std::vector<unsigned>::iterator I = FilterEnds.begin(), 257 E = FilterEnds.end(); I != E; ++I) { 258 unsigned i = *I, j = TyIds.size(); 259 260 while (i && j) 261 if (FilterIds[--i] != TyIds[--j]) 262 goto try_next; 263 264 if (!j) 265 // The new filter coincides with range [i, end) of the existing filter. 266 return -(1 + i); 267 268 try_next:; 269 } 270 271 // Add the new filter. 272 int FilterID = -(1 + FilterIds.size()); 273 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 274 for (unsigned I = 0, N = TyIds.size(); I != N; ++I) 275 FilterIds.push_back(TyIds[I]); 276 FilterEnds.push_back(FilterIds.size()); 277 FilterIds.push_back(0); // terminator 278 return FilterID; 279 } 280 281 /// getPersonality - Return the personality function for the current function. 282 Function *MachineModuleInfo::getPersonality() const { 283 // FIXME: Until PR1414 will be fixed, we're using 1 personality function per 284 // function 285 return !LandingPads.empty() ? LandingPads[0].Personality : NULL; 286 } 287 288 /// getPersonalityIndex - Return unique index for current personality 289 /// function. NULL/first personality function should always get zero index. 290 unsigned MachineModuleInfo::getPersonalityIndex() const { 291 const Function* Personality = NULL; 292 293 // Scan landing pads. If there is at least one non-NULL personality - use it. 294 for (unsigned i = 0; i != LandingPads.size(); ++i) 295 if (LandingPads[i].Personality) { 296 Personality = LandingPads[i].Personality; 297 break; 298 } 299 300 for (unsigned i = 0; i < Personalities.size(); ++i) { 301 if (Personalities[i] == Personality) 302 return i; 303 } 304 305 // This will happen if the current personality function is 306 // in the zero index. 307 return 0; 308 } 309 310