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; |
| 79 | } |
| 80 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | /// AnalyzeModule - Scan the module for global debug information. |
| 82 | /// |
| 83 | void MachineModuleInfo::AnalyzeModule(Module &M) { |
Chris Lattner | 1e0e0d1 | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 84 | // Insert functions in the llvm.used array (but not llvm.compiler.used) into |
| 85 | // UsedFunctions. |
Dale Johannesen | 3dadeb3 | 2008-01-16 19:59:28 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 2616560 | 2009-07-20 06:05:50 +0000 | [diff] [blame] | 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); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 99 | //===-EH-------------------------------------------------------------------===// |
| 100 | |
| 101 | /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the |
| 102 | /// specified MachineBasicBlock. |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 103 | LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo |
| 104 | (MachineBasicBlock *LandingPad) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | } |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 111 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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); |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 130 | LP.LandingPadLabel = LandingPadLabel; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 141 | for (unsigned i = 0; i < Personalities.size(); ++i) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | if (Personalities[i] == Personality) |
| 143 | return; |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 144 | |
Eric Christopher | c87d5ae | 2009-08-26 21:30:49 +0000 | [diff] [blame] | 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); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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); |
Bill Wendling | 993b3dd | 2008-07-07 21:41:57 +0000 | [diff] [blame] | 167 | std::vector<unsigned> IdsInFilter(TyInfo.size()); |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 168 | for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 169 | IdsInFilter[I] = getTypeIDFor(TyInfo[I]); |
| 170 | LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); |
| 171 | } |
| 172 | |
Duncan Sands | 923fdb1 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 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 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 187 | // Special case: we *should* emit LPs with null LP MBB. This indicates |
Duncan Sands | 4ff179f | 2007-12-19 07:36:31 +0000 | [diff] [blame] | 188 | // "nounwind" case. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 189 | if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { |
| 190 | LandingPads.erase(LandingPads.begin() + i); |
| 191 | continue; |
| 192 | } |
Duncan Sands | 241a0c9 | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 193 | |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 194 | for (unsigned j=0; j != LandingPads[i].BeginLabels.size(); ) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 195 | unsigned BeginLabel = MappedLabel(LandingPad.BeginLabels[j]); |
| 196 | unsigned EndLabel = MappedLabel(LandingPad.EndLabels[j]); |
Duncan Sands | 241a0c9 | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 197 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | } |
Duncan Sands | 241a0c9 | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 208 | |
| 209 | // Remove landing pads with no try-ranges. |
Dan Gohman | 301f405 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 210 | if (LandingPads[i].BeginLabels.empty()) { |
Duncan Sands | 241a0c9 | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 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 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 221 | ++i; |
| 222 | } |
| 223 | } |
| 224 | |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 225 | /// getTypeIDFor - Return the type id for the specified typeinfo. This is |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 226 | /// function wide. |
| 227 | unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) { |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 228 | for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) |
| 229 | if (TypeInfos[i] == TI) return i + 1; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 237 | int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) { |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 243 | unsigned i = *I, j = TyIds.size(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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); |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 252 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 253 | try_next:; |
| 254 | } |
| 255 | |
| 256 | // Add the new filter. |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 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) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | FilterIds.push_back(TyIds[I]); |
Bill Wendling | 0e679b8 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 261 | FilterEnds.push_back(FilterIds.size()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 |
Eric Christopher | e92cc8d | 2009-08-26 21:44:57 +0000 | [diff] [blame] | 274 | /// function. NULL/first personality function should always get zero index. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 275 | unsigned MachineModuleInfo::getPersonalityIndex() const { |
| 276 | const Function* Personality = NULL; |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 277 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 278 | // 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] | 279 | for (unsigned i = 0; i != LandingPads.size(); ++i) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 280 | if (LandingPads[i].Personality) { |
| 281 | Personality = LandingPads[i].Personality; |
| 282 | break; |
| 283 | } |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 284 | |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 285 | for (unsigned i = 0; i < Personalities.size(); ++i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 286 | if (Personalities[i] == Personality) |
| 287 | return i; |
Bill Wendling | 4de8de5 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 288 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 289 | |
Eric Christopher | e92cc8d | 2009-08-26 21:44:57 +0000 | [diff] [blame] | 290 | // This will happen if the current personality function is |
| 291 | // in the zero index. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 304 | DebugLabelFolder() : MachineFunctionPass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 305 | |
Evan Cheng | 465a66e | 2008-09-22 20:58:04 +0000 | [diff] [blame] | 306 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | ecb436f | 2009-07-31 23:37:33 +0000 | [diff] [blame] | 307 | AU.setPreservesCFG(); |
Evan Cheng | 1b42ac1 | 2008-09-22 22:21:38 +0000 | [diff] [blame] | 308 | AU.addPreservedID(MachineLoopInfoID); |
| 309 | AU.addPreservedID(MachineDominatorsID); |
Evan Cheng | 465a66e | 2008-09-22 20:58:04 +0000 | [diff] [blame] | 310 | MachineFunctionPass::getAnalysisUsage(AU); |
| 311 | } |
| 312 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Duncan Sands | 4e0d6a7 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 321 | MachineModuleInfo *MMI = getAnalysisIfAvailable<MachineModuleInfo>(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 322 | if (!MMI) return false; |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 323 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 324 | // Track if change is made. |
| 325 | bool MadeChange = false; |
| 326 | // No prior label to begin. |
| 327 | unsigned PriorLabel = 0; |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 328 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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. |
Devang Patel | 9a72741 | 2009-04-10 18:58:59 +0000 | [diff] [blame] | 335 | if (I->isDebugLabel() && !MMI->isDbgLabelUsed(I->getOperand(0).getImm())){ |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 336 | // The label ID # is always operand #0, an immediate. |
| 337 | unsigned NextLabel = I->getOperand(0).getImm(); |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 338 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | } |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 356 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 357 | ++I; |
| 358 | } |
| 359 | } |
Eric Christopher | 8460363 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 360 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 361 | return MadeChange; |
| 362 | } |
| 363 | |
| 364 | FunctionPass *createDebugLabelFoldingPass() { return new DebugLabelFolder(); } |
| 365 | |
| 366 | } |