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" |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 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" |
Evan Cheng | 0ff39b3 | 2008-06-30 07:31:25 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/ValueTracking.h" |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
Evan Cheng | 8b56a90 | 2008-09-22 22:21:38 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/Passes.h" |
Jim Laskey | 9d4209f | 2006-11-07 19:33:46 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetInstrInfo.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
Jim Laskey | c1c47c3 | 2007-01-29 23:40:33 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetOptions.h" |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSymbol.h" |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Dwarf.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Jim Laskey | 9c4447a | 2006-03-01 20:39:36 +0000 | [diff] [blame] | 29 | using namespace llvm::dwarf; |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 30 | |
| 31 | // Handle the Pass registration stuff necessary to use TargetData's. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 32 | static RegisterPass<MachineModuleInfo> |
Chris Lattner | 11d53c1 | 2010-03-13 20:55:24 +0000 | [diff] [blame] | 33 | X("machinemoduleinfo", "Machine Module Information"); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 34 | char MachineModuleInfo::ID = 0; |
Jim Laskey | 063e765 | 2006-01-17 17:31:53 +0000 | [diff] [blame] | 35 | |
Chris Lattner | a70e2e3 | 2009-09-15 22:44:26 +0000 | [diff] [blame] | 36 | // Out of line virtual method. |
| 37 | MachineModuleInfoImpl::~MachineModuleInfoImpl() {} |
| 38 | |
Chris Lattner | 0220ba7 | 2010-03-15 19:09:43 +0000 | [diff] [blame] | 39 | namespace llvm { |
| 40 | class MMIAddrLabelMapCallbackPtr : CallbackVH { |
| 41 | MMIAddrLabelMap *Map; |
| 42 | public: |
| 43 | MMIAddrLabelMapCallbackPtr() : Map(0) {} |
| 44 | MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V), Map(0) {} |
| 45 | |
| 46 | void setMap(MMIAddrLabelMap *map) { Map = map; } |
| 47 | |
| 48 | virtual void deleted(); |
| 49 | virtual void allUsesReplacedWith(Value *V2); |
| 50 | }; |
| 51 | |
| 52 | class MMIAddrLabelMap { |
| 53 | MCContext &Context; |
| 54 | struct AddrLabelSymEntry { |
| 55 | MCSymbol *Sym; |
| 56 | unsigned Index; |
| 57 | }; |
| 58 | |
| 59 | DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols; |
| 60 | |
| 61 | std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks; |
| 62 | public: |
| 63 | |
| 64 | MMIAddrLabelMap(MCContext &context) : Context(context) {} |
| 65 | |
| 66 | MCSymbol *getAddrLabelSymbol(BasicBlock *BB); |
| 67 | void UpdateForDeletedBlock(BasicBlock *BB); |
| 68 | void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New); |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | MCSymbol *MMIAddrLabelMap::getAddrLabelSymbol(BasicBlock *BB) { |
| 73 | assert(BB->hasAddressTaken() && |
| 74 | "Shouldn't get label for block without address taken"); |
| 75 | AddrLabelSymEntry &Entry = AddrLabelSymbols[BB]; |
| 76 | |
| 77 | // If we already had an entry for this block, just return it. |
| 78 | if (Entry.Sym) return Entry.Sym; |
| 79 | |
| 80 | // Otherwise, this is a new entry, create a new symbol for it and add an |
| 81 | // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd. |
| 82 | BBCallbacks.push_back(BB); |
| 83 | BBCallbacks.back().setMap(this); |
| 84 | Entry.Index = BBCallbacks.size()-1; |
| 85 | return Entry.Sym = Context.CreateTempSymbol(); |
| 86 | } |
| 87 | |
| 88 | void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) { |
| 89 | // If the block got deleted, there is no need for the symbol. If the symbol |
| 90 | // was already emitted, we can just forget about it, otherwise we need to |
| 91 | // queue it up for later emission when the function is output. |
| 92 | AddrLabelSymEntry Entry = AddrLabelSymbols[BB]; |
| 93 | AddrLabelSymbols.erase(BB); |
| 94 | assert(Entry.Sym && "Didn't have a symbol, why a callback?"); |
| 95 | BBCallbacks[Entry.Index] = 0; // Clear the callback. |
| 96 | |
| 97 | if (Entry.Sym->isDefined()) |
| 98 | return; |
| 99 | |
| 100 | // If the block is not yet defined, we need to emit it at the end of the |
| 101 | // function. |
| 102 | assert(0 && "Case not handled yet!"); |
| 103 | abort(); |
| 104 | } |
| 105 | |
| 106 | void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) { |
| 107 | // Get the entry for the RAUW'd block and remove it from our map. |
| 108 | AddrLabelSymEntry OldEntry = AddrLabelSymbols[Old]; |
| 109 | AddrLabelSymbols.erase(Old); |
| 110 | assert(OldEntry.Sym && "Didn't have a symbol, why a callback?"); |
| 111 | |
| 112 | // If New is not address taken, just move our symbol over to it. |
| 113 | if (!AddrLabelSymbols.count(New)) { |
| 114 | BBCallbacks[OldEntry.Index] = New; // Update the callback. |
| 115 | AddrLabelSymbols[New] = OldEntry; // Set New's entry. |
| 116 | } else { |
| 117 | assert(0 && "Case not handled yet!"); |
| 118 | abort(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | |
| 123 | void MMIAddrLabelMapCallbackPtr::deleted() { |
| 124 | Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr())); |
| 125 | } |
| 126 | |
| 127 | void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) { |
| 128 | Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2)); |
| 129 | } |
| 130 | |
| 131 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
Eric Christopher | cf29697 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 11d53c1 | 2010-03-13 20:55:24 +0000 | [diff] [blame] | 134 | MachineModuleInfo::MachineModuleInfo(const MCAsmInfo &MAI) |
| 135 | : ImmutablePass(&ID), Context(MAI), |
Chris Lattner | 63d7836 | 2010-03-14 08:36:50 +0000 | [diff] [blame] | 136 | ObjFileMMI(0), |
Chris Lattner | 18589de | 2010-03-14 02:24:55 +0000 | [diff] [blame] | 137 | CurCallSite(0), CallsEHReturn(0), CallsUnwindInit(0), DbgInfoAvailable(false){ |
Eric Christopher | d44fff7 | 2009-08-26 21:30:49 +0000 | [diff] [blame] | 138 | // Always emit some info, by default "no personality" info. |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 139 | Personalities.push_back(NULL); |
Chris Lattner | 0220ba7 | 2010-03-15 19:09:43 +0000 | [diff] [blame] | 140 | AddrLabelSymbols = 0; |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 141 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 11d53c1 | 2010-03-13 20:55:24 +0000 | [diff] [blame] | 143 | MachineModuleInfo::MachineModuleInfo() |
| 144 | : ImmutablePass(&ID), Context(*(MCAsmInfo*)0) { |
| 145 | assert(0 && "This MachineModuleInfo constructor should never be called, MMI " |
| 146 | "should always be explicitly constructed by LLVMTargetMachine"); |
| 147 | abort(); |
| 148 | } |
| 149 | |
Chris Lattner | a70e2e3 | 2009-09-15 22:44:26 +0000 | [diff] [blame] | 150 | MachineModuleInfo::~MachineModuleInfo() { |
Chris Lattner | f185455 | 2009-09-16 05:26:00 +0000 | [diff] [blame] | 151 | delete ObjFileMMI; |
Chris Lattner | 0220ba7 | 2010-03-15 19:09:43 +0000 | [diff] [blame] | 152 | |
| 153 | // FIXME: Why isn't doFinalization being called?? |
| 154 | //assert(AddrLabelSymbols == 0 && "doFinalization not called"); |
| 155 | delete AddrLabelSymbols; |
| 156 | AddrLabelSymbols = 0; |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 159 | /// doInitialization - Initialize the state for a new module. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 160 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 161 | bool MachineModuleInfo::doInitialization() { |
Chris Lattner | 0220ba7 | 2010-03-15 19:09:43 +0000 | [diff] [blame] | 162 | assert(AddrLabelSymbols == 0 && "Improperly initialized"); |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 163 | return false; |
Jim Laskey | 6af5681 | 2006-01-04 13:36:38 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 166 | /// doFinalization - Tear down the state after completion of a module. |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 167 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 168 | bool MachineModuleInfo::doFinalization() { |
Chris Lattner | 0220ba7 | 2010-03-15 19:09:43 +0000 | [diff] [blame] | 169 | delete AddrLabelSymbols; |
| 170 | AddrLabelSymbols = 0; |
Jim Laskey | b2efb85 | 2006-01-04 22:28:25 +0000 | [diff] [blame] | 171 | return false; |
| 172 | } |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 173 | |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 174 | /// EndFunction - Discard function meta information. |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 175 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 176 | void MachineModuleInfo::EndFunction() { |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 177 | // Clean up frame info. |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 178 | FrameMoves.clear(); |
Eric Christopher | cf29697 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 179 | |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 180 | // Clean up exception info. |
| 181 | LandingPads.clear(); |
Jim Grosbach | ca752c9 | 2010-01-28 01:45:32 +0000 | [diff] [blame] | 182 | CallSiteMap.clear(); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 183 | TypeInfos.clear(); |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 184 | FilterIds.clear(); |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 185 | FilterEnds.clear(); |
Anton Korobeynikov | 2365f51 | 2007-07-14 14:06:15 +0000 | [diff] [blame] | 186 | CallsEHReturn = 0; |
| 187 | CallsUnwindInit = 0; |
Devang Patel | 85d29e2 | 2009-10-08 20:41:17 +0000 | [diff] [blame] | 188 | VariableDbgInfo.clear(); |
Jim Laskey | 4188699 | 2006-04-07 16:34:46 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 191 | /// AnalyzeModule - Scan the module for global debug information. |
| 192 | /// |
Jim Laskey | 6da1864 | 2007-01-26 21:38:26 +0000 | [diff] [blame] | 193 | void MachineModuleInfo::AnalyzeModule(Module &M) { |
Chris Lattner | 401e10c | 2009-07-20 06:14:25 +0000 | [diff] [blame] | 194 | // Insert functions in the llvm.used array (but not llvm.compiler.used) into |
| 195 | // UsedFunctions. |
Dale Johannesen | 48ae02f | 2008-01-16 19:59:28 +0000 | [diff] [blame] | 196 | GlobalVariable *GV = M.getGlobalVariable("llvm.used"); |
| 197 | if (!GV || !GV->hasInitializer()) return; |
| 198 | |
| 199 | // Should be an array of 'i8*'. |
| 200 | ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); |
| 201 | if (InitList == 0) return; |
| 202 | |
Chris Lattner | 1d5c493 | 2009-07-20 06:05:50 +0000 | [diff] [blame] | 203 | for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) |
| 204 | if (Function *F = |
| 205 | dyn_cast<Function>(InitList->getOperand(i)->stripPointerCasts())) |
| 206 | UsedFunctions.insert(F); |
Jim Laskey | b3e789a | 2006-01-26 20:21:46 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chris Lattner | 0220ba7 | 2010-03-15 19:09:43 +0000 | [diff] [blame] | 209 | //===- Address of Block Management ----------------------------------------===// |
| 210 | |
| 211 | |
Chris Lattner | 3b9d621 | 2010-03-14 17:53:23 +0000 | [diff] [blame] | 212 | /// getAddrLabelSymbol - Return the symbol to be used for the specified basic |
| 213 | /// block when its address is taken. This cannot be its normal LBB label |
| 214 | /// because the block may be accessed outside its containing function. |
| 215 | MCSymbol *MachineModuleInfo::getAddrLabelSymbol(const BasicBlock *BB) { |
Chris Lattner | 0220ba7 | 2010-03-15 19:09:43 +0000 | [diff] [blame] | 216 | // Lazily create AddrLabelSymbols. |
| 217 | if (AddrLabelSymbols == 0) |
| 218 | AddrLabelSymbols = new MMIAddrLabelMap(Context); |
| 219 | return AddrLabelSymbols->getAddrLabelSymbol(const_cast<BasicBlock*>(BB)); |
Chris Lattner | 3b9d621 | 2010-03-14 17:53:23 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | |
Chris Lattner | 0220ba7 | 2010-03-15 19:09:43 +0000 | [diff] [blame] | 223 | //===- EH -----------------------------------------------------------------===// |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 224 | |
| 225 | /// getOrCreateLandingPadInfo - Find or create an LandingPadInfo for the |
| 226 | /// specified MachineBasicBlock. |
Bill Wendling | 10fff60 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 227 | LandingPadInfo &MachineModuleInfo::getOrCreateLandingPadInfo |
| 228 | (MachineBasicBlock *LandingPad) { |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 229 | unsigned N = LandingPads.size(); |
| 230 | for (unsigned i = 0; i < N; ++i) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 231 | LandingPadInfo &LP = LandingPads[i]; |
| 232 | if (LP.LandingPadBlock == LandingPad) |
| 233 | return LP; |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 234 | } |
Eric Christopher | cf29697 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 235 | |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 236 | LandingPads.push_back(LandingPadInfo(LandingPad)); |
| 237 | return LandingPads[N]; |
| 238 | } |
| 239 | |
| 240 | /// addInvoke - Provide the begin and end labels of an invoke style call and |
| 241 | /// associate it with a try landing pad block. |
| 242 | void MachineModuleInfo::addInvoke(MachineBasicBlock *LandingPad, |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 243 | MCSymbol *BeginLabel, MCSymbol *EndLabel) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 244 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Anton Korobeynikov | eeb37e0 | 2007-05-10 22:34:59 +0000 | [diff] [blame] | 245 | LP.BeginLabels.push_back(BeginLabel); |
| 246 | LP.EndLabels.push_back(EndLabel); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /// addLandingPad - Provide the label of a try LandingPad block. |
| 250 | /// |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 251 | MCSymbol *MachineModuleInfo::addLandingPad(MachineBasicBlock *LandingPad) { |
Chris Lattner | 63d7836 | 2010-03-14 08:36:50 +0000 | [diff] [blame] | 252 | MCSymbol *LandingPadLabel = Context.CreateTempSymbol(); |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 253 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Eric Christopher | cf29697 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 254 | LP.LandingPadLabel = LandingPadLabel; |
Chris Lattner | 7561d48 | 2010-03-14 02:33:54 +0000 | [diff] [blame] | 255 | return LandingPadLabel; |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /// addPersonality - Provide the personality function for the exception |
| 259 | /// information. |
| 260 | void MachineModuleInfo::addPersonality(MachineBasicBlock *LandingPad, |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 261 | Function *Personality) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 262 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 263 | LP.Personality = Personality; |
Anton Korobeynikov | 0ff3ca4 | 2007-05-12 22:36:25 +0000 | [diff] [blame] | 264 | |
Bill Wendling | 10fff60 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 265 | for (unsigned i = 0; i < Personalities.size(); ++i) |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 266 | if (Personalities[i] == Personality) |
| 267 | return; |
Eric Christopher | cf29697 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 268 | |
Eric Christopher | d44fff7 | 2009-08-26 21:30:49 +0000 | [diff] [blame] | 269 | // If this is the first personality we're adding go |
| 270 | // ahead and add it at the beginning. |
| 271 | if (Personalities[0] == NULL) |
| 272 | Personalities[0] = Personality; |
| 273 | else |
| 274 | Personalities.push_back(Personality); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /// addCatchTypeInfo - Provide the catch typeinfo for a landing pad. |
| 278 | /// |
| 279 | void MachineModuleInfo::addCatchTypeInfo(MachineBasicBlock *LandingPad, |
| 280 | std::vector<GlobalVariable *> &TyInfo) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 281 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 282 | for (unsigned N = TyInfo.size(); N; --N) |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 283 | LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 284 | } |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 285 | |
| 286 | /// addFilterTypeInfo - Provide the filter typeinfo for a landing pad. |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 287 | /// |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 288 | void MachineModuleInfo::addFilterTypeInfo(MachineBasicBlock *LandingPad, |
| 289 | std::vector<GlobalVariable *> &TyInfo) { |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 290 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
Bill Wendling | 4925567 | 2008-07-07 21:41:57 +0000 | [diff] [blame] | 291 | std::vector<unsigned> IdsInFilter(TyInfo.size()); |
Bill Wendling | 10fff60 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 292 | for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 293 | IdsInFilter[I] = getTypeIDFor(TyInfo[I]); |
| 294 | LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); |
Jim Laskey | 59e8434 | 2007-03-01 20:25:32 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Duncan Sands | 6590b04 | 2007-08-27 15:47:50 +0000 | [diff] [blame] | 297 | /// addCleanup - Add a cleanup action for a landing pad. |
| 298 | /// |
| 299 | void MachineModuleInfo::addCleanup(MachineBasicBlock *LandingPad) { |
| 300 | LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); |
| 301 | LP.TypeIds.push_back(0); |
| 302 | } |
| 303 | |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 304 | /// TidyLandingPads - Remap landing pad labels and remove any deleted landing |
| 305 | /// pads. |
| 306 | void MachineModuleInfo::TidyLandingPads() { |
| 307 | for (unsigned i = 0; i != LandingPads.size(); ) { |
| 308 | LandingPadInfo &LandingPad = LandingPads[i]; |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 309 | if (LandingPad.LandingPadLabel && !LandingPad.LandingPadLabel->isDefined()) |
Chris Lattner | a34ec229 | 2010-03-09 01:51:43 +0000 | [diff] [blame] | 310 | LandingPad.LandingPadLabel = 0; |
Anton Korobeynikov | eeb37e0 | 2007-05-10 22:34:59 +0000 | [diff] [blame] | 311 | |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 312 | // Special case: we *should* emit LPs with null LP MBB. This indicates |
Duncan Sands | 481dc72 | 2007-12-19 07:36:31 +0000 | [diff] [blame] | 313 | // "nounwind" case. |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 314 | if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 315 | LandingPads.erase(LandingPads.begin() + i); |
| 316 | continue; |
| 317 | } |
Duncan Sands | 57810cd | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 318 | |
Chris Lattner | 1611273 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 319 | for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { |
| 320 | MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; |
| 321 | MCSymbol *EndLabel = LandingPad.EndLabels[j]; |
| 322 | if (BeginLabel->isDefined() && EndLabel->isDefined()) continue; |
| 323 | |
| 324 | LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); |
| 325 | LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); |
| 326 | --j, --e; |
Anton Korobeynikov | eeb37e0 | 2007-05-10 22:34:59 +0000 | [diff] [blame] | 327 | } |
Duncan Sands | 57810cd | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 328 | |
| 329 | // Remove landing pads with no try-ranges. |
Dan Gohman | 3035959 | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 330 | if (LandingPads[i].BeginLabels.empty()) { |
Duncan Sands | 57810cd | 2007-09-05 11:27:52 +0000 | [diff] [blame] | 331 | LandingPads.erase(LandingPads.begin() + i); |
| 332 | continue; |
| 333 | } |
| 334 | |
| 335 | // If there is no landing pad, ensure that the list of typeids is empty. |
| 336 | // If the only typeid is a cleanup, this is the same as having no typeids. |
| 337 | if (!LandingPad.LandingPadBlock || |
| 338 | (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) |
| 339 | LandingPad.TypeIds.clear(); |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 340 | ++i; |
| 341 | } |
| 342 | } |
| 343 | |
Eric Christopher | cf29697 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 344 | /// getTypeIDFor - Return the type id for the specified typeinfo. This is |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 345 | /// function wide. |
| 346 | unsigned MachineModuleInfo::getTypeIDFor(GlobalVariable *TI) { |
Bill Wendling | 10fff60 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 347 | for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) |
| 348 | if (TypeInfos[i] == TI) return i + 1; |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 349 | |
| 350 | TypeInfos.push_back(TI); |
| 351 | return TypeInfos.size(); |
| 352 | } |
| 353 | |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 354 | /// getFilterIDFor - Return the filter id for the specified typeinfos. This is |
| 355 | /// function wide. |
Bill Wendling | 914c970 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 356 | int MachineModuleInfo::getFilterIDFor(std::vector<unsigned> &TyIds) { |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 357 | // If the new filter coincides with the tail of an existing filter, then |
| 358 | // re-use the existing filter. Folding filters more than this requires |
| 359 | // re-ordering filters and/or their elements - probably not worth it. |
| 360 | for (std::vector<unsigned>::iterator I = FilterEnds.begin(), |
| 361 | E = FilterEnds.end(); I != E; ++I) { |
Bill Wendling | 914c970 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 362 | unsigned i = *I, j = TyIds.size(); |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 363 | |
| 364 | while (i && j) |
| 365 | if (FilterIds[--i] != TyIds[--j]) |
| 366 | goto try_next; |
| 367 | |
| 368 | if (!j) |
| 369 | // The new filter coincides with range [i, end) of the existing filter. |
| 370 | return -(1 + i); |
Bill Wendling | 914c970 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 371 | |
Duncan Sands | 14da32a | 2007-07-05 15:15:01 +0000 | [diff] [blame] | 372 | try_next:; |
| 373 | } |
| 374 | |
| 375 | // Add the new filter. |
Bill Wendling | 914c970 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 376 | int FilterID = -(1 + FilterIds.size()); |
| 377 | FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); |
| 378 | for (unsigned I = 0, N = TyIds.size(); I != N; ++I) |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 379 | FilterIds.push_back(TyIds[I]); |
Bill Wendling | 914c970 | 2008-06-27 01:27:56 +0000 | [diff] [blame] | 380 | FilterEnds.push_back(FilterIds.size()); |
Duncan Sands | 73ef58a | 2007-06-02 16:53:42 +0000 | [diff] [blame] | 381 | FilterIds.push_back(0); // terminator |
| 382 | return FilterID; |
| 383 | } |
| 384 | |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 385 | /// getPersonality - Return the personality function for the current function. |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 386 | Function *MachineModuleInfo::getPersonality() const { |
Anton Korobeynikov | 0ff3ca4 | 2007-05-12 22:36:25 +0000 | [diff] [blame] | 387 | // 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] | 388 | // function |
| 389 | return !LandingPads.empty() ? LandingPads[0].Personality : NULL; |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 392 | /// getPersonalityIndex - Return unique index for current personality |
Eric Christopher | 5e365e2 | 2009-08-26 21:44:57 +0000 | [diff] [blame] | 393 | /// function. NULL/first personality function should always get zero index. |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 394 | unsigned MachineModuleInfo::getPersonalityIndex() const { |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 395 | const Function* Personality = NULL; |
Eric Christopher | cf29697 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 396 | |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 397 | // 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] | 398 | for (unsigned i = 0; i != LandingPads.size(); ++i) |
Anton Korobeynikov | 070280e | 2007-05-23 11:08:31 +0000 | [diff] [blame] | 399 | if (LandingPads[i].Personality) { |
| 400 | Personality = LandingPads[i].Personality; |
| 401 | break; |
| 402 | } |
Eric Christopher | cf29697 | 2009-08-26 21:27:09 +0000 | [diff] [blame] | 403 | |
Bill Wendling | 10fff60 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 404 | for (unsigned i = 0; i < Personalities.size(); ++i) { |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 405 | if (Personalities[i] == Personality) |
| 406 | return i; |
Bill Wendling | 10fff60 | 2008-07-03 22:53:42 +0000 | [diff] [blame] | 407 | } |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 408 | |
Eric Christopher | 5e365e2 | 2009-08-26 21:44:57 +0000 | [diff] [blame] | 409 | // This will happen if the current personality function is |
| 410 | // in the zero index. |
Anton Korobeynikov | 8c7c173 | 2007-05-13 15:42:26 +0000 | [diff] [blame] | 411 | return 0; |
| 412 | } |
Jim Laskey | 59667fe | 2007-02-21 22:38:31 +0000 | [diff] [blame] | 413 | |