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