Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 11 | |
| 12 | #include "llvm/Constants.h" |
Evan Cheng | 833501d | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/ValueTracking.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 15 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | 1b42ac1 | 2008-09-22 22:21:38 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/Passes.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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" |
Edwin Török | 675d562 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | using namespace llvm::dwarf; |
| 29 | |
| 30 | // Handle the Pass registration stuff necessary to use TargetData's. |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 31 | static RegisterPass<MachineModuleInfo> |
| 32 | X("machinemoduleinfo", "Module Information"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 33 | char MachineModuleInfo::ID = 0; |
| 34 | |
Chris Lattner | 72ba672 | 2009-09-15 22:44:26 +0000 | [diff] [blame] | 35 | // Out of line virtual method. |
| 36 | MachineModuleInfoImpl::~MachineModuleInfoImpl() {} |
| 37 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 39 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | MachineModuleInfo::MachineModuleInfo() |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 41 | : ImmutablePass(&ID) |
Chris Lattner | 16fcdf9 | 2009-09-16 05:26:00 +0000 | [diff] [blame] | 42 | , ObjFileMMI(0) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 43 | , CallsEHReturn(0) |
| 44 | , CallsUnwindInit(0) |
Chris Lattner | 72ba672 | 2009-09-15 22:44:26 +0000 | [diff] [blame] | 45 | , DbgInfoAvailable(false) { |
Eric Christopher | c87d5ae | 2009-08-26 21:30:49 +0000 | [diff] [blame] | 46 | // Always emit some info, by default "no personality" info. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 47 | Personalities.push_back(NULL); |
| 48 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 72ba672 | 2009-09-15 22:44:26 +0000 | [diff] [blame] | 50 | MachineModuleInfo::~MachineModuleInfo() { |
Chris Lattner | 16fcdf9 | 2009-09-16 05:26:00 +0000 | [diff] [blame] | 51 | delete ObjFileMMI; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 66 | /// EndFunction - Discard function meta information. |
| 67 | /// |
| 68 | void MachineModuleInfo::EndFunction() { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | // Clean up frame info. |
| 70 | FrameMoves.clear(); |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 71 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 72 | // Clean up exception info. |
| 73 | LandingPads.clear(); |
| 74 | TypeInfos.clear(); |
| 75 | FilterIds.clear(); |
| 76 | FilterEnds.clear(); |
| 77 | CallsEHReturn = 0; |
| 78 | CallsUnwindInit = 0; |
Devang Patel | f57e106 | 2009-10-08 20:41:17 +0000 | [diff] [blame] | 79 | VariableDbgInfo.clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | /// AnalyzeModule - Scan the module for global debug information. |
| 83 | /// |
| 84 | void MachineModuleInfo::AnalyzeModule(Module &M) { |
Chris Lattner | 1e0e0d1 | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 85 | // Insert functions in the llvm.used array (but not llvm.compiler.used) into |
| 86 | // UsedFunctions. |
Dale Johannesen | 3dadeb3 | 2008-01-16 19:59:28 +0000 | [diff] [blame] | 87 | GlobalVariable *GV = M.getGlobalVariable("llvm.used"); |
| 88 | if (!GV || !GV->hasInitializer()) return; |
| 89 | |
| 90 | // Should be an array of 'i8*'. |
| 91 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 92 | if (InitList == 0) return; |
| 93 | |
Chris Lattner | 2616560 | 2009-07-20 06:05:50 +0000 | [diff] [blame] | 94 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 95 | if (Function *F = |
| 96 | dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts())) |
| 97 | UsedFunctions.insert(F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 100 | //===-EH-------------------------------------------------------------------===// |
| 101 | |
| 102 | /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the |
| 103 | /// specified MachineBasicBlock. |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 104 | LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo |
| 105 | (MachineBasicBlock *LandingPad) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | unsigned N = LandingPads.size(); |
| 107 | for (unsigned i = 0; i < N; ++i) { |
| 108 | LandingPadInfo &LP = LandingPads[i]; |
| 109 | if (LP.LandingPadBlock == LandingPad) |
| 110 | return LP; |
| 111 | } |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 112 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | LandingPads.push_back(LandingPadInfo(LandingPad)); |
| 114 | return LandingPads[N]; |
| 115 | } |
| 116 | |
| 117 | /// addInvoke - Provide the begin and end labels of an invoke style call and |
| 118 | /// associate it with a try landing pad block. |
| 119 | void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad, |
| 120 | unsigned BeginLabel, unsigned EndLabel) { |
| 121 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 122 | LP.BeginLabels.push_back(BeginLabel); |
| 123 | LP.EndLabels.push_back(EndLabel); |
| 124 | } |
| 125 | |
| 126 | /// addLandingPad - Provide the label of a try LandingPad block. |
| 127 | /// |
| 128 | unsigned MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) { |
| 129 | unsigned LandingPadLabel = NextLabelID(); |
| 130 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 131 | LP.LandingPadLabel = LandingPadLabel; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 132 | return LandingPadLabel; |
| 133 | } |
| 134 | |
| 135 | /// addPersonality - Provide the personality function for the exception |
| 136 | /// information. |
| 137 | void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad, |
| 138 | Function *Personality) { |
| 139 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 140 | LP.Personality = Personality; |
| 141 | |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 142 | for (unsigned i = 0; i < Personalities.size(); ++i) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 143 | if (Personalities[i] == Personality) |
| 144 | return; |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 145 | |
Eric Christopher | c87d5ae | 2009-08-26 21:30:49 +0000 | [diff] [blame] | 146 | // If this is the first personality we're adding go |
| 147 | // ahead and add it at the beginning. |
| 148 | if (Personalities[0] == NULL) |
| 149 | Personalities[0] = Personality; |
| 150 | else |
| 151 | Personalities.push_back(Personality); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. |
| 155 | /// |
| 156 | void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad, |
| 157 | std::vector<GlobalVariable *> &TyInfo) { |
| 158 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 159 | for (unsigned N = TyInfo.size(); N; --N) |
| 160 | LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); |
| 161 | } |
| 162 | |
| 163 | /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. |
| 164 | /// |
| 165 | void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad, |
| 166 | std::vector<GlobalVariable *> &TyInfo) { |
| 167 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Bill Wendling | 993b3dd | 2008-07-07 21:41:57 +0000 | [diff] [blame] | 168 | std::vector<unsigned> IdsInFilter(TyInfo.size()); |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 169 | for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 170 | IdsInFilter[I] = getTypeIDFor(TyInfo[I]); |
| 171 | LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); |
| 172 | } |
| 173 | |
Duncan Sands | 923fdb1 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 174 | /// addCleanup - Add a cleanup action for a landing pad. |
| 175 | /// |
| 176 | void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) { |
| 177 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 178 | LP.TypeIds.push_back(0); |
| 179 | } |
| 180 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 181 | /// TidyLandingPads - Remap landing pad labels and remove any deleted landing |
| 182 | /// pads. |
| 183 | void MachineModuleInfo::TidyLandingPads() { |
| 184 | for (unsigned i = 0; i != LandingPads.size(); ) { |
| 185 | LandingPadInfo &LandingPad = LandingPads[i]; |
| 186 | LandingPad.LandingPadLabel = MappedLabel(LandingPad.LandingPadLabel); |
| 187 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 188 | // Special case: we *should* emit LPs with null LP MBB. This indicates |
Duncan Sands | 4ff179f | 2007-12-19 07:36:31 +0000 | [diff] [blame] | 189 | // "nounwind" case. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 190 | if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { |
| 191 | LandingPads.erase(LandingPads.begin() + i); |
| 192 | continue; |
| 193 | } |
Duncan Sands | 241a0c9 | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 194 | |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 195 | for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 196 | unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]); |
| 197 | unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]); |
Duncan Sands | 241a0c9 | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 198 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 199 | if (!BeginLabel || !EndLabel) { |
| 200 | LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); |
| 201 | LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); |
| 202 | continue; |
| 203 | } |
| 204 | |
| 205 | LandingPad.BeginLabels[j] = BeginLabel; |
| 206 | LandingPad.EndLabels[j] = EndLabel; |
| 207 | ++j; |
| 208 | } |
Duncan Sands | 241a0c9 | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 209 | |
| 210 | // Remove landing pads with no try-ranges. |
Dan Gohman | 301f405 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 211 | if (LandingPads[i].BeginLabels.empty()) { |
Duncan Sands | 241a0c9 | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 212 | LandingPads.erase(LandingPads.begin() + i); |
| 213 | continue; |
| 214 | } |
| 215 | |
| 216 | // If there is no landing pad, ensure that the list of typeids is empty. |
| 217 | // If the only typeid is a cleanup, this is the same as having no typeids. |
| 218 | if (!LandingPad.LandingPadBlock || |
| 219 | (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) |
| 220 | LandingPad.TypeIds.clear(); |
| 221 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 222 | ++i; |
| 223 | } |
| 224 | } |
| 225 | |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 226 | /// getTypeIDFor - Return the type id for the specified typeinfo. This is |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 227 | /// function wide. |
| 228 | unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) { |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 229 | for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) |
| 230 | if (TypeInfos[i] == TI) return i + 1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 231 | |
| 232 | TypeInfos.push_back(TI); |
| 233 | return TypeInfos.size(); |
| 234 | } |
| 235 | |
| 236 | /// getFilterIDFor - Return the filter id for the specified typeinfos. This is |
| 237 | /// function wide. |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 238 | int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 239 | // If the new filter coincides with the tail of an existing filter, then |
| 240 | // re-use the existing filter. Folding filters more than this requires |
| 241 | // re-ordering filters and/or their elements - probably not worth it. |
| 242 | for (std::vector<unsigned>::iterator I = FilterEnds.begin(), |
| 243 | E = FilterEnds.end(); I != E; ++I) { |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 244 | unsigned i = *I, j = TyIds.size(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 245 | |
| 246 | while (i && j) |
| 247 | if (FilterIds[--i] != TyIds[--j]) |
| 248 | goto try_next; |
| 249 | |
| 250 | if (!j) |
| 251 | // The new filter coincides with range [i, end) of the existing filter. |
| 252 | return -(1 + i); |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 253 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 254 | try_next:; |
| 255 | } |
| 256 | |
| 257 | // Add the new filter. |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 258 | int FilterID = -(1 + FilterIds.size()); |
| 259 | FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); |
| 260 | for (unsigned I = 0, N = TyIds.size(); I != N; ++I) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 261 | FilterIds.push_back(TyIds[I]); |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 262 | FilterEnds.push_back(FilterIds.size()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 263 | FilterIds.push_back(0); // terminator |
| 264 | return FilterID; |
| 265 | } |
| 266 | |
| 267 | /// getPersonality - Return the personality function for the current function. |
| 268 | Function *MachineModuleInfo::getPersonality() const { |
| 269 | // FIXME: Until PR1414 will be fixed, we're using 1 personality function per |
| 270 | // function |
| 271 | return !LandingPads.empty() ? LandingPads[0].Personality : NULL; |
| 272 | } |
| 273 | |
| 274 | /// getPersonalityIndex - Return unique index for current personality |
Eric Christopher | e92cc8d | 2009-08-26 21:44:57 +0000 | [diff] [blame] | 275 | /// function. NULL/first personality function should always get zero index. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 276 | unsigned MachineModuleInfo::getPersonalityIndex() const { |
| 277 | const Function* Personality = NULL; |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 278 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 279 | // Scan landing pads. If there is at least one non-NULL personality - use it. |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 280 | for (unsigned i = 0; i != LandingPads.size(); ++i) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 281 | if (LandingPads[i].Personality) { |
| 282 | Personality = LandingPads[i].Personality; |
| 283 | break; |
| 284 | } |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 285 | |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 286 | for (unsigned i = 0; i < Personalities.size(); ++i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 287 | if (Personalities[i] == Personality) |
| 288 | return i; |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 289 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 290 | |
Eric Christopher | e92cc8d | 2009-08-26 21:44:57 +0000 | [diff] [blame] | 291 | // This will happen if the current personality function is |
| 292 | // in the zero index. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 293 | return 0; |
| 294 | } |
| 295 | |