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