Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 1 | //===-- DwarfEHPrepare - Prepare exception handling for code generation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass mulches exception handling code into a form adapted to code |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 11 | // generation. Required if using dwarf exception handling. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "dwarfehprepare" |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
| 17 | #include "llvm/Instructions.h" |
| 18 | #include "llvm/IntrinsicInst.h" |
| 19 | #include "llvm/Module.h" |
| 20 | #include "llvm/Pass.h" |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
| 22 | #include "llvm/Analysis/Dominators.h" |
| 23 | #include "llvm/CodeGen/Passes.h" |
Jim Grosbach | 8d77cc8 | 2010-01-20 23:03:55 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCAsmInfo.h" |
Gabor Greif | 2bf4b3b | 2010-06-25 11:25:30 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CallSite.h" |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetLowering.h" |
| 27 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 28 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 29 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame] | 32 | STATISTIC(NumLandingPadsSplit, "Number of landing pads split"); |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame] | 33 | STATISTIC(NumUnwindsLowered, "Number of unwind instructions lowered"); |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 34 | STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved"); |
| 35 | STATISTIC(NumStackTempsIntroduced, "Number of stack temporaries introduced"); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 37 | static void PromoteAlloca(AllocaInst *AI); |
| 38 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 39 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 40 | class DwarfEHPrepare : public FunctionPass { |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 41 | const TargetMachine *TM; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 42 | const TargetLowering *TLI; |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 43 | bool CompileFast; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 44 | |
| 45 | // The eh.exception intrinsic. |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 46 | Function *ExceptionValueIntrinsic; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 47 | |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 48 | // The eh.selector intrinsic. |
| 49 | Function *SelectorIntrinsic; |
| 50 | |
| 51 | // _Unwind_Resume_or_Rethrow call. |
| 52 | Constant *URoR; |
| 53 | |
| 54 | // The EH language-specific catch-all type. |
| 55 | GlobalVariable *EHCatchAllValue; |
| 56 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 57 | // _Unwind_Resume or the target equivalent. |
| 58 | Constant *RewindFunction; |
| 59 | |
| 60 | // Dominator info is used when turning stack temporaries into registers. |
| 61 | DominatorTree *DT; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 62 | |
| 63 | // The function we are running on. |
| 64 | Function *F; |
| 65 | |
| 66 | // The landing pads for this function. |
| 67 | typedef SmallPtrSet<BasicBlock*, 8> BBSet; |
| 68 | BBSet LandingPads; |
| 69 | |
| 70 | // Stack temporary used to hold eh.exception values. |
| 71 | AllocaInst *ExceptionValueVar; |
| 72 | |
| 73 | bool NormalizeLandingPads(); |
| 74 | bool LowerUnwinds(); |
| 75 | bool MoveExceptionValueCalls(); |
| 76 | bool FinishStackTemporaries(); |
| 77 | bool PromoteStackTemporaries(); |
| 78 | |
| 79 | Instruction *CreateExceptionValueCall(BasicBlock *BB); |
| 80 | Instruction *CreateValueLoad(BasicBlock *BB); |
| 81 | |
| 82 | /// CreateReadOfExceptionValue - Return the result of the eh.exception |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 83 | /// intrinsic by calling the intrinsic if in a landing pad, or loading it |
| 84 | /// from the exception value variable otherwise. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 85 | Instruction *CreateReadOfExceptionValue(BasicBlock *BB) { |
| 86 | return LandingPads.count(BB) ? |
| 87 | CreateExceptionValueCall(BB) : CreateValueLoad(BB); |
| 88 | } |
| 89 | |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 90 | /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still |
Bill Wendling | 23295cc | 2010-07-26 22:36:52 +0000 | [diff] [blame] | 91 | /// use the "llvm.eh.catch.all.value" call need to convert to using its |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 92 | /// initializer instead. |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 93 | bool CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels); |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 94 | |
Bill Wendling | efbf306 | 2010-06-24 18:49:10 +0000 | [diff] [blame] | 95 | bool HasCatchAllInSelector(IntrinsicInst *); |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 96 | |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 97 | /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups. |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 98 | void FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels, |
| 99 | SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels); |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 100 | |
| 101 | /// FindAllURoRInvokes - Find all URoR invokes in the function. |
| 102 | void FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes); |
| 103 | |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 104 | /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" |
| 105 | /// calls. The "unwind" part of these invokes jump to a landing pad within |
| 106 | /// the current function. This is a candidate to merge the selector |
| 107 | /// associated with the URoR invoke with the one from the URoR's landing |
| 108 | /// pad. |
| 109 | bool HandleURoRInvokes(); |
| 110 | |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 111 | /// FindSelectorAndURoR - Find the eh.selector call and URoR call associated |
| 112 | /// with the eh.exception call. This recursively looks past instructions |
| 113 | /// which don't change the EH pointer value, like casts or PHI nodes. |
| 114 | bool FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke, |
| 115 | SmallPtrSet<IntrinsicInst*, 8> &SelCalls); |
| 116 | |
Chris Lattner | c34c220 | 2010-08-29 19:28:28 +0000 | [diff] [blame] | 117 | /// PromoteStoreInst - Perform Mem2Reg on a StoreInst. |
| 118 | bool PromoteStoreInst(StoreInst *SI) { |
Chris Lattner | c34c220 | 2010-08-29 19:28:28 +0000 | [diff] [blame] | 119 | AllocaInst *AI = dyn_cast<AllocaInst>(SI->getOperand(1)); |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 120 | if (!AI || !isAllocaPromotable(AI)) return false; |
Chris Lattner | c34c220 | 2010-08-29 19:28:28 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 122 | PromoteAlloca(AI); |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 126 | /// PromoteEHPtrStore - Promote the storing of an EH pointer into a |
| 127 | /// register. This should get rid of the store and subsequent loads. |
| 128 | bool PromoteEHPtrStore(IntrinsicInst *II) { |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 129 | if (!CompileFast) return false; |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 130 | |
| 131 | bool Changed = false; |
| 132 | StoreInst *SI; |
| 133 | |
| 134 | while (1) { |
| 135 | SI = 0; |
| 136 | for (Value::use_iterator |
| 137 | I = II->use_begin(), E = II->use_end(); I != E; ++I) { |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 138 | SI = dyn_cast<StoreInst>(*I); |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 139 | if (SI) break; |
| 140 | } |
| 141 | |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 142 | if (SI && !PromoteStoreInst(SI)) |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 143 | break; |
| 144 | |
| 145 | Changed = true; |
| 146 | } |
| 147 | |
Duncan Sands | 80b9b89 | 2010-06-29 14:49:35 +0000 | [diff] [blame] | 148 | return Changed; |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 151 | public: |
| 152 | static char ID; // Pass identification, replacement for typeid. |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 153 | DwarfEHPrepare(const TargetMachine *tm, bool fast) : |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 154 | FunctionPass(ID), TM(tm), TLI(TM->getTargetLowering()), |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 155 | CompileFast(fast), |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 156 | ExceptionValueIntrinsic(0), SelectorIntrinsic(0), |
| 157 | URoR(0), EHCatchAllValue(0), RewindFunction(0) {} |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 158 | |
| 159 | virtual bool runOnFunction(Function &Fn); |
| 160 | |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 161 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 162 | AU.addPreserved<DominatorTree>(); |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 165 | const char *getPassName() const { |
| 166 | return "Exception handling preparation"; |
| 167 | } |
| 168 | |
| 169 | }; |
| 170 | } // end anonymous namespace |
| 171 | |
| 172 | char DwarfEHPrepare::ID = 0; |
| 173 | |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 174 | FunctionPass *llvm::createDwarfEHPass(const TargetMachine *tm, bool fast) { |
| 175 | return new DwarfEHPrepare(tm, fast); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 178 | /// PromoteAlloca - This promotes an alloca to registers when we know that it |
| 179 | /// only has non-volatile loads and stores to it. |
| 180 | static void PromoteAlloca(AllocaInst *AI) { |
| 181 | assert(isAllocaPromotable(AI)); |
| 182 | |
| 183 | // First step: bucket up uses of the pointers by the block they occur in. |
| 184 | // This is important because we have to handle multiple defs/uses in a block |
| 185 | // ourselves: SSAUpdater is purely for cross-block references. |
| 186 | // FIXME: Want a TinyVector<Instruction*> since there is usually 0/1 element. |
| 187 | DenseMap<BasicBlock*, std::vector<Instruction*> > UsesByBlock; |
| 188 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 189 | UI != E; ++UI) { |
| 190 | Instruction *User = cast<Instruction>(*UI); |
| 191 | UsesByBlock[User->getParent()].push_back(User); |
| 192 | } |
| 193 | |
| 194 | SSAUpdater SSA; |
| 195 | |
| 196 | // It wants to know some value of the same type as what we'll be inserting. |
| 197 | Value *SomeValue; |
| 198 | if (isa<LoadInst>(*AI->use_begin())) |
| 199 | SomeValue = *AI->use_begin(); |
| 200 | else |
| 201 | SomeValue = cast<StoreInst>(*AI->use_begin())->getOperand(0); |
| 202 | SSA.Initialize(SomeValue); |
| 203 | |
| 204 | // Okay, now we can iterate over all the blocks in the loop with uses, |
| 205 | // processing them. Keep track of which loads are loading a live-in value. |
| 206 | SmallVector<LoadInst*, 32> LiveInLoads; |
| 207 | |
| 208 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 209 | UI != E; ++UI) { |
| 210 | Instruction *User = cast<Instruction>(*UI); |
| 211 | std::vector<Instruction*> &BlockUses = UsesByBlock[User->getParent()]; |
| 212 | |
| 213 | // If this block has already been processed, ignore this repeat use. |
| 214 | if (BlockUses.empty()) continue; |
| 215 | |
| 216 | // Okay, this is the first use in the block. If this block just has a |
| 217 | // single user in it, we can rewrite it trivially. |
| 218 | if (BlockUses.size() == 1) { |
| 219 | // If it is a store, it is a trivial def of the value in the block. |
| 220 | if (isa<StoreInst>(User)) { |
| 221 | SSA.AddAvailableValue(User->getParent(), |
| 222 | cast<StoreInst>(User)->getOperand(0)); |
| 223 | } else { |
| 224 | // Otherwise it is a load, queue it to rewrite as a live-in load. |
| 225 | LiveInLoads.push_back(cast<LoadInst>(User)); |
| 226 | } |
| 227 | BlockUses.clear(); |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | // Otherwise, check to see if this block is all loads. If so, we can queue |
| 232 | // them all as live in loads. |
| 233 | bool HasStore = false; |
| 234 | for (unsigned i = 0, e = BlockUses.size(); i != e; ++i) { |
| 235 | if (isa<StoreInst>(BlockUses[i])) { |
| 236 | HasStore = true; |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (!HasStore) { |
| 242 | for (unsigned i = 0, e = BlockUses.size(); i != e; ++i) |
| 243 | LiveInLoads.push_back(cast<LoadInst>(BlockUses[i])); |
| 244 | BlockUses.clear(); |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | // Otherwise, we have mixed loads and stores (or just a bunch of stores). |
| 249 | // Since SSAUpdater is purely for cross-block values, we need to determine |
| 250 | // the order of these instructions in the block. If the first use in the |
| 251 | // block is a load, then it uses the live in value. The last store defines |
| 252 | // the live out value. We handle this by doing a linear scan of the block. |
| 253 | BasicBlock *BB = User->getParent(); |
| 254 | Value *StoredValue = 0; |
| 255 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) { |
| 256 | if (LoadInst *L = dyn_cast<LoadInst>(II)) { |
| 257 | // If this is a load to an unrelated pointer, ignore it. |
| 258 | if (L->getOperand(0) != AI) continue; |
| 259 | |
| 260 | // If we haven't seen a store yet, this is a live in use, otherwise |
| 261 | // use the stored value. |
| 262 | if (StoredValue) |
| 263 | L->replaceAllUsesWith(StoredValue); |
| 264 | else |
| 265 | LiveInLoads.push_back(L); |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | if (StoreInst *S = dyn_cast<StoreInst>(II)) { |
| 270 | // If this is a store to an unrelated pointer, ignore it. |
| 271 | if (S->getOperand(1) != AI) continue; |
| 272 | |
| 273 | // Remember that this is the active value in the block. |
| 274 | StoredValue = S->getOperand(0); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | // The last stored value that happened is the live-out for the block. |
| 279 | assert(StoredValue && "Already checked that there is a store in block"); |
| 280 | SSA.AddAvailableValue(BB, StoredValue); |
| 281 | BlockUses.clear(); |
| 282 | } |
| 283 | |
| 284 | // Okay, now we rewrite all loads that use live-in values in the loop, |
| 285 | // inserting PHI nodes as necessary. |
| 286 | for (unsigned i = 0, e = LiveInLoads.size(); i != e; ++i) { |
| 287 | LoadInst *ALoad = LiveInLoads[i]; |
| 288 | ALoad->replaceAllUsesWith(SSA.GetValueInMiddleOfBlock(ALoad->getParent())); |
| 289 | } |
| 290 | |
| 291 | // Now that everything is rewritten, delete the old instructions from the body |
| 292 | // of the loop. They should all be dead now. |
| 293 | for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); |
| 294 | UI != E; ++UI) |
| 295 | cast<Instruction>(*UI)->eraseFromParent(); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | |
Bill Wendling | efbf306 | 2010-06-24 18:49:10 +0000 | [diff] [blame] | 300 | /// HasCatchAllInSelector - Return true if the intrinsic instruction has a |
| 301 | /// catch-all. |
| 302 | bool DwarfEHPrepare::HasCatchAllInSelector(IntrinsicInst *II) { |
| 303 | if (!EHCatchAllValue) return false; |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 304 | |
Gabor Greif | 2bf4b3b | 2010-06-25 11:25:30 +0000 | [diff] [blame] | 305 | unsigned ArgIdx = II->getNumArgOperands() - 1; |
| 306 | GlobalVariable *GV = dyn_cast<GlobalVariable>(II->getArgOperand(ArgIdx)); |
Bill Wendling | efbf306 | 2010-06-24 18:49:10 +0000 | [diff] [blame] | 307 | return GV == EHCatchAllValue; |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 310 | /// FindAllCleanupSelectors - Find all eh.selector calls that are clean-ups. |
| 311 | void DwarfEHPrepare:: |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 312 | FindAllCleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels, |
| 313 | SmallPtrSet<IntrinsicInst*, 32> &CatchAllSels) { |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 314 | for (Value::use_iterator |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 315 | I = SelectorIntrinsic->use_begin(), |
| 316 | E = SelectorIntrinsic->use_end(); I != E; ++I) { |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 317 | IntrinsicInst *II = cast<IntrinsicInst>(*I); |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 318 | |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 319 | if (II->getParent()->getParent() != F) |
| 320 | continue; |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 321 | |
Bill Wendling | efbf306 | 2010-06-24 18:49:10 +0000 | [diff] [blame] | 322 | if (!HasCatchAllInSelector(II)) |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 323 | Sels.insert(II); |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 324 | else |
| 325 | CatchAllSels.insert(II); |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
| 329 | /// FindAllURoRInvokes - Find all URoR invokes in the function. |
| 330 | void DwarfEHPrepare:: |
| 331 | FindAllURoRInvokes(SmallPtrSet<InvokeInst*, 32> &URoRInvokes) { |
| 332 | for (Value::use_iterator |
| 333 | I = URoR->use_begin(), |
| 334 | E = URoR->use_end(); I != E; ++I) { |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 335 | if (InvokeInst *II = dyn_cast<InvokeInst>(*I)) |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 336 | URoRInvokes.insert(II); |
| 337 | } |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 340 | /// CleanupSelectors - Any remaining eh.selector intrinsic calls which still use |
Bill Wendling | 23295cc | 2010-07-26 22:36:52 +0000 | [diff] [blame] | 341 | /// the "llvm.eh.catch.all.value" call need to convert to using its |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 342 | /// initializer instead. |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 343 | bool DwarfEHPrepare::CleanupSelectors(SmallPtrSet<IntrinsicInst*, 32> &Sels) { |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 344 | if (!EHCatchAllValue) return false; |
| 345 | |
| 346 | if (!SelectorIntrinsic) { |
| 347 | SelectorIntrinsic = |
| 348 | Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector); |
| 349 | if (!SelectorIntrinsic) return false; |
| 350 | } |
| 351 | |
Bill Wendling | 43de15f | 2010-03-27 01:22:38 +0000 | [diff] [blame] | 352 | bool Changed = false; |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 353 | for (SmallPtrSet<IntrinsicInst*, 32>::iterator |
| 354 | I = Sels.begin(), E = Sels.end(); I != E; ++I) { |
| 355 | IntrinsicInst *Sel = *I; |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 356 | |
Bill Wendling | 23295cc | 2010-07-26 22:36:52 +0000 | [diff] [blame] | 357 | // Index of the "llvm.eh.catch.all.value" variable. |
Gabor Greif | 9d67768 | 2010-06-29 13:03:46 +0000 | [diff] [blame] | 358 | unsigned OpIdx = Sel->getNumArgOperands() - 1; |
| 359 | GlobalVariable *GV = dyn_cast<GlobalVariable>(Sel->getArgOperand(OpIdx)); |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 360 | if (GV != EHCatchAllValue) continue; |
Gabor Greif | 9d67768 | 2010-06-29 13:03:46 +0000 | [diff] [blame] | 361 | Sel->setArgOperand(OpIdx, EHCatchAllValue->getInitializer()); |
Bill Wendling | 43de15f | 2010-03-27 01:22:38 +0000 | [diff] [blame] | 362 | Changed = true; |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 363 | } |
Bill Wendling | 43de15f | 2010-03-27 01:22:38 +0000 | [diff] [blame] | 364 | |
| 365 | return Changed; |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 368 | /// FindSelectorAndURoR - Find the eh.selector call associated with the |
| 369 | /// eh.exception call. And indicate if there is a URoR "invoke" associated with |
| 370 | /// the eh.exception call. This recursively looks past instructions which don't |
| 371 | /// change the EH pointer value, like casts or PHI nodes. |
| 372 | bool |
| 373 | DwarfEHPrepare::FindSelectorAndURoR(Instruction *Inst, bool &URoRInvoke, |
| 374 | SmallPtrSet<IntrinsicInst*, 8> &SelCalls) { |
| 375 | SmallPtrSet<PHINode*, 32> SeenPHIs; |
| 376 | bool Changed = false; |
| 377 | |
| 378 | restart: |
| 379 | for (Value::use_iterator |
| 380 | I = Inst->use_begin(), E = Inst->use_end(); I != E; ++I) { |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 381 | Instruction *II = dyn_cast<Instruction>(*I); |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 382 | if (!II || II->getParent()->getParent() != F) continue; |
| 383 | |
| 384 | if (IntrinsicInst *Sel = dyn_cast<IntrinsicInst>(II)) { |
| 385 | if (Sel->getIntrinsicID() == Intrinsic::eh_selector) |
| 386 | SelCalls.insert(Sel); |
| 387 | } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(II)) { |
| 388 | if (Invoke->getCalledFunction() == URoR) |
| 389 | URoRInvoke = true; |
| 390 | } else if (CastInst *CI = dyn_cast<CastInst>(II)) { |
| 391 | Changed |= FindSelectorAndURoR(CI, URoRInvoke, SelCalls); |
| 392 | } else if (StoreInst *SI = dyn_cast<StoreInst>(II)) { |
| 393 | if (!PromoteStoreInst(SI)) continue; |
| 394 | Changed = true; |
| 395 | SeenPHIs.clear(); |
| 396 | goto restart; // Uses may have changed, restart loop. |
| 397 | } else if (PHINode *PN = dyn_cast<PHINode>(II)) { |
| 398 | if (SeenPHIs.insert(PN)) |
| 399 | // Don't process a PHI node more than once. |
| 400 | Changed |= FindSelectorAndURoR(PN, URoRInvoke, SelCalls); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | return Changed; |
| 405 | } |
| 406 | |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 407 | /// HandleURoRInvokes - Handle invokes of "_Unwind_Resume_or_Rethrow" calls. The |
| 408 | /// "unwind" part of these invokes jump to a landing pad within the current |
| 409 | /// function. This is a candidate to merge the selector associated with the URoR |
| 410 | /// invoke with the one from the URoR's landing pad. |
| 411 | bool DwarfEHPrepare::HandleURoRInvokes() { |
| 412 | if (!EHCatchAllValue) { |
| 413 | EHCatchAllValue = |
Bill Wendling | 23295cc | 2010-07-26 22:36:52 +0000 | [diff] [blame] | 414 | F->getParent()->getNamedGlobal("llvm.eh.catch.all.value"); |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 415 | if (!EHCatchAllValue) return false; |
| 416 | } |
| 417 | |
Bill Wendling | bfbd853 | 2010-03-27 01:19:12 +0000 | [diff] [blame] | 418 | if (!SelectorIntrinsic) { |
| 419 | SelectorIntrinsic = |
| 420 | Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_selector); |
| 421 | if (!SelectorIntrinsic) return false; |
| 422 | } |
| 423 | |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 424 | SmallPtrSet<IntrinsicInst*, 32> Sels; |
| 425 | SmallPtrSet<IntrinsicInst*, 32> CatchAllSels; |
| 426 | FindAllCleanupSelectors(Sels, CatchAllSels); |
| 427 | |
| 428 | if (!DT) |
| 429 | // We require DominatorTree information. |
| 430 | return CleanupSelectors(CatchAllSels); |
| 431 | |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 432 | if (!URoR) { |
| 433 | URoR = F->getParent()->getFunction("_Unwind_Resume_or_Rethrow"); |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 434 | if (!URoR) return CleanupSelectors(CatchAllSels); |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 437 | SmallPtrSet<InvokeInst*, 32> URoRInvokes; |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 438 | FindAllURoRInvokes(URoRInvokes); |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 439 | |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 440 | SmallPtrSet<IntrinsicInst*, 32> SelsToConvert; |
| 441 | |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 442 | for (SmallPtrSet<IntrinsicInst*, 32>::iterator |
| 443 | SI = Sels.begin(), SE = Sels.end(); SI != SE; ++SI) { |
| 444 | const BasicBlock *SelBB = (*SI)->getParent(); |
| 445 | for (SmallPtrSet<InvokeInst*, 32>::iterator |
| 446 | UI = URoRInvokes.begin(), UE = URoRInvokes.end(); UI != UE; ++UI) { |
| 447 | const BasicBlock *URoRBB = (*UI)->getParent(); |
Dan Gohman | ce0fe5d | 2010-07-26 17:38:15 +0000 | [diff] [blame] | 448 | if (DT->dominates(SelBB, URoRBB)) { |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 449 | SelsToConvert.insert(*SI); |
| 450 | break; |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Bill Wendling | bfde644 | 2010-03-29 23:02:46 +0000 | [diff] [blame] | 455 | bool Changed = false; |
| 456 | |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 457 | if (Sels.size() != SelsToConvert.size()) { |
| 458 | // If we haven't been able to convert all of the clean-up selectors, then |
| 459 | // loop through the slow way to see if they still need to be converted. |
| 460 | if (!ExceptionValueIntrinsic) { |
| 461 | ExceptionValueIntrinsic = |
| 462 | Intrinsic::getDeclaration(F->getParent(), Intrinsic::eh_exception); |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 463 | if (!ExceptionValueIntrinsic) |
| 464 | return CleanupSelectors(CatchAllSels); |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | for (Value::use_iterator |
| 468 | I = ExceptionValueIntrinsic->use_begin(), |
| 469 | E = ExceptionValueIntrinsic->use_end(); I != E; ++I) { |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 470 | IntrinsicInst *EHPtr = dyn_cast<IntrinsicInst>(*I); |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 471 | if (!EHPtr || EHPtr->getParent()->getParent() != F) continue; |
| 472 | |
| 473 | Changed |= PromoteEHPtrStore(EHPtr); |
| 474 | |
| 475 | bool URoRInvoke = false; |
| 476 | SmallPtrSet<IntrinsicInst*, 8> SelCalls; |
| 477 | Changed |= FindSelectorAndURoR(EHPtr, URoRInvoke, SelCalls); |
| 478 | |
| 479 | if (URoRInvoke) { |
| 480 | // This EH pointer is being used by an invoke of an URoR instruction and |
| 481 | // an eh.selector intrinsic call. If the eh.selector is a 'clean-up', we |
| 482 | // need to convert it to a 'catch-all'. |
| 483 | for (SmallPtrSet<IntrinsicInst*, 8>::iterator |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 484 | SI = SelCalls.begin(), SE = SelCalls.end(); SI != SE; ++SI) |
Bill Wendling | efbf306 | 2010-06-24 18:49:10 +0000 | [diff] [blame] | 485 | if (!HasCatchAllInSelector(*SI)) |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 486 | SelsToConvert.insert(*SI); |
Bill Wendling | 201f5f0 | 2010-03-29 23:37:07 +0000 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 491 | if (!SelsToConvert.empty()) { |
| 492 | // Convert all clean-up eh.selectors, which are associated with "invokes" of |
| 493 | // URoR calls, into catch-all eh.selectors. |
| 494 | Changed = true; |
| 495 | |
| 496 | for (SmallPtrSet<IntrinsicInst*, 8>::iterator |
| 497 | SI = SelsToConvert.begin(), SE = SelsToConvert.end(); |
| 498 | SI != SE; ++SI) { |
| 499 | IntrinsicInst *II = *SI; |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 500 | |
| 501 | // Use the exception object pointer and the personality function |
| 502 | // from the original selector. |
Gabor Greif | 2bf4b3b | 2010-06-25 11:25:30 +0000 | [diff] [blame] | 503 | CallSite CS(II); |
| 504 | IntrinsicInst::op_iterator I = CS.arg_begin(); |
Gabor Greif | 2bf4b3b | 2010-06-25 11:25:30 +0000 | [diff] [blame] | 505 | IntrinsicInst::op_iterator E = CS.arg_end(); |
| 506 | IntrinsicInst::op_iterator B = prior(E); |
| 507 | |
| 508 | // Exclude last argument if it is an integer. |
| 509 | if (isa<ConstantInt>(B)) E = B; |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 510 | |
Gabor Greif | 32621ad | 2010-06-28 16:40:52 +0000 | [diff] [blame] | 511 | // Add exception object pointer (front). |
| 512 | // Add personality function (next). |
| 513 | // Add in any filter IDs (rest). |
| 514 | SmallVector<Value*, 8> Args(I, E); |
Bill Wendling | 9436611 | 2010-06-12 02:34:29 +0000 | [diff] [blame] | 515 | |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 516 | Args.push_back(EHCatchAllValue->getInitializer()); // Catch-all indicator. |
| 517 | |
| 518 | CallInst *NewSelector = |
| 519 | CallInst::Create(SelectorIntrinsic, Args.begin(), Args.end(), |
| 520 | "eh.sel.catch.all", II); |
| 521 | |
| 522 | NewSelector->setTailCall(II->isTailCall()); |
| 523 | NewSelector->setAttributes(II->getAttributes()); |
| 524 | NewSelector->setCallingConv(II->getCallingConv()); |
| 525 | |
| 526 | II->replaceAllUsesWith(NewSelector); |
| 527 | II->eraseFromParent(); |
| 528 | } |
| 529 | } |
| 530 | |
Bill Wendling | d9e3b2b | 2010-06-30 22:49:53 +0000 | [diff] [blame] | 531 | Changed |= CleanupSelectors(CatchAllSels); |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 532 | return Changed; |
| 533 | } |
| 534 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 535 | /// NormalizeLandingPads - Normalize and discover landing pads, noting them |
| 536 | /// in the LandingPads set. A landing pad is normal if the only CFG edges |
Eric Christopher | ec26bf7 | 2009-09-15 21:56:46 +0000 | [diff] [blame] | 537 | /// that end at it are unwind edges from invoke instructions. If we inlined |
| 538 | /// through an invoke we could have a normal branch from the previous |
| 539 | /// unwind block through to the landing pad for the original invoke. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 540 | /// Abnormal landing pads are fixed up by redirecting all unwind edges to |
| 541 | /// a new basic block which falls through to the original. |
| 542 | bool DwarfEHPrepare::NormalizeLandingPads() { |
| 543 | bool Changed = false; |
| 544 | |
Dan Gohman | 55e59c1 | 2010-04-19 19:05:59 +0000 | [diff] [blame] | 545 | const MCAsmInfo *MAI = TM->getMCAsmInfo(); |
Jim Grosbach | 8d77cc8 | 2010-01-20 23:03:55 +0000 | [diff] [blame] | 546 | bool usingSjLjEH = MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; |
| 547 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 548 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 549 | TerminatorInst *TI = I->getTerminator(); |
| 550 | if (!isa<InvokeInst>(TI)) |
| 551 | continue; |
| 552 | BasicBlock *LPad = TI->getSuccessor(1); |
| 553 | // Skip landing pads that have already been normalized. |
| 554 | if (LandingPads.count(LPad)) |
| 555 | continue; |
| 556 | |
| 557 | // Check that only invoke unwind edges end at the landing pad. |
| 558 | bool OnlyUnwoundTo = true; |
Jim Grosbach | 8d77cc8 | 2010-01-20 23:03:55 +0000 | [diff] [blame] | 559 | bool SwitchOK = usingSjLjEH; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 560 | for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); |
| 561 | PI != PE; ++PI) { |
| 562 | TerminatorInst *PT = (*PI)->getTerminator(); |
Jim Grosbach | 8d77cc8 | 2010-01-20 23:03:55 +0000 | [diff] [blame] | 563 | // The SjLj dispatch block uses a switch instruction. This is effectively |
| 564 | // an unwind edge, so we can disregard it here. There will only ever |
| 565 | // be one dispatch, however, so if there are multiple switches, one |
| 566 | // of them truly is a normal edge, not an unwind edge. |
| 567 | if (SwitchOK && isa<SwitchInst>(PT)) { |
| 568 | SwitchOK = false; |
| 569 | continue; |
| 570 | } |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 571 | if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) { |
| 572 | OnlyUnwoundTo = false; |
| 573 | break; |
| 574 | } |
| 575 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 576 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 577 | if (OnlyUnwoundTo) { |
| 578 | // Only unwind edges lead to the landing pad. Remember the landing pad. |
| 579 | LandingPads.insert(LPad); |
| 580 | continue; |
| 581 | } |
| 582 | |
| 583 | // At least one normal edge ends at the landing pad. Redirect the unwind |
| 584 | // edges to a new basic block which falls through into this one. |
| 585 | |
| 586 | // Create the new basic block. |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 587 | BasicBlock *NewBB = BasicBlock::Create(F->getContext(), |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 588 | LPad->getName() + "_unwind_edge"); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 589 | |
| 590 | // Insert it into the function right before the original landing pad. |
| 591 | LPad->getParent()->getBasicBlockList().insert(LPad, NewBB); |
| 592 | |
| 593 | // Redirect unwind edges from the original landing pad to NewBB. |
| 594 | for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) { |
| 595 | TerminatorInst *PT = (*PI++)->getTerminator(); |
| 596 | if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad) |
| 597 | // Unwind to the new block. |
| 598 | PT->setSuccessor(1, NewBB); |
| 599 | } |
| 600 | |
| 601 | // If there are any PHI nodes in LPad, we need to update them so that they |
| 602 | // merge incoming values from NewBB instead. |
| 603 | for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) { |
| 604 | PHINode *PN = cast<PHINode>(II); |
| 605 | pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB); |
| 606 | |
| 607 | // Check to see if all of the values coming in via unwind edges are the |
| 608 | // same. If so, we don't need to create a new PHI node. |
| 609 | Value *InVal = PN->getIncomingValueForBlock(*PB); |
| 610 | for (pred_iterator PI = PB; PI != PE; ++PI) { |
| 611 | if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) { |
| 612 | InVal = 0; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | if (InVal == 0) { |
| 618 | // Different unwind edges have different values. Create a new PHI node |
| 619 | // in NewBB. |
| 620 | PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind", |
| 621 | NewBB); |
| 622 | // Add an entry for each unwind edge, using the value from the old PHI. |
| 623 | for (pred_iterator PI = PB; PI != PE; ++PI) |
| 624 | NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI); |
| 625 | |
| 626 | // Now use this new PHI as the common incoming value for NewBB in PN. |
| 627 | InVal = NewPN; |
| 628 | } |
| 629 | |
| 630 | // Revector exactly one entry in the PHI node to come from NewBB |
| 631 | // and delete all other entries that come from unwind edges. If |
| 632 | // there are both normal and unwind edges from the same predecessor, |
| 633 | // this leaves an entry for the normal edge. |
| 634 | for (pred_iterator PI = PB; PI != PE; ++PI) |
| 635 | PN->removeIncomingValue(*PI); |
| 636 | PN->addIncoming(InVal, NewBB); |
| 637 | } |
| 638 | |
| 639 | // Add a fallthrough from NewBB to the original landing pad. |
| 640 | BranchInst::Create(LPad, NewBB); |
| 641 | |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 642 | // Now update DominatorTree analysis information if it is around. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 643 | if (DT) |
| 644 | DT->splitBlock(NewBB); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 645 | |
| 646 | // Remember the newly constructed landing pad. The original landing pad |
| 647 | // LPad is no longer a landing pad now that all unwind edges have been |
| 648 | // revectored to NewBB. |
| 649 | LandingPads.insert(NewBB); |
| 650 | ++NumLandingPadsSplit; |
| 651 | Changed = true; |
| 652 | } |
| 653 | |
| 654 | return Changed; |
| 655 | } |
| 656 | |
| 657 | /// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume, |
| 658 | /// rethrowing any previously caught exception. This will crash horribly |
| 659 | /// at runtime if there is no such exception: using unwind to throw a new |
| 660 | /// exception is currently not supported. |
| 661 | bool DwarfEHPrepare::LowerUnwinds() { |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 662 | SmallVector<TerminatorInst*, 16> UnwindInsts; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 663 | |
| 664 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 665 | TerminatorInst *TI = I->getTerminator(); |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 666 | if (isa<UnwindInst>(TI)) |
| 667 | UnwindInsts.push_back(TI); |
| 668 | } |
| 669 | |
| 670 | if (UnwindInsts.empty()) return false; |
| 671 | |
| 672 | // Find the rewind function if we didn't already. |
| 673 | if (!RewindFunction) { |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 674 | LLVMContext &Ctx = UnwindInsts[0]->getContext(); |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 675 | std::vector<const Type*> |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 676 | Params(1, Type::getInt8PtrTy(Ctx)); |
| 677 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 678 | Params, false); |
| 679 | const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); |
| 680 | RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy); |
| 681 | } |
| 682 | |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 683 | bool Changed = false; |
| 684 | |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 685 | for (SmallVectorImpl<TerminatorInst*>::iterator |
| 686 | I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) { |
| 687 | TerminatorInst *TI = *I; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 688 | |
| 689 | // Replace the unwind instruction with a call to _Unwind_Resume (or the |
| 690 | // appropriate target equivalent) followed by an UnreachableInst. |
| 691 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 692 | // Create the call... |
Eric Christopher | 82f149d | 2009-09-04 01:14:14 +0000 | [diff] [blame] | 693 | CallInst *CI = CallInst::Create(RewindFunction, |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 694 | CreateReadOfExceptionValue(TI->getParent()), |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 695 | "", TI); |
Eric Christopher | 82f149d | 2009-09-04 01:14:14 +0000 | [diff] [blame] | 696 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 697 | // ...followed by an UnreachableInst. |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 698 | new UnreachableInst(TI->getContext(), TI); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 699 | |
| 700 | // Nuke the unwind instruction. |
| 701 | TI->eraseFromParent(); |
| 702 | ++NumUnwindsLowered; |
| 703 | Changed = true; |
| 704 | } |
| 705 | |
| 706 | return Changed; |
| 707 | } |
| 708 | |
| 709 | /// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from |
| 710 | /// landing pads by replacing calls outside of landing pads with loads from a |
| 711 | /// stack temporary. Move eh.exception calls inside landing pads to the start |
| 712 | /// of the landing pad (optional, but may make things simpler for later passes). |
| 713 | bool DwarfEHPrepare::MoveExceptionValueCalls() { |
| 714 | // If the eh.exception intrinsic is not declared in the module then there is |
| 715 | // nothing to do. Speed up compilation by checking for this common case. |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 716 | if (!ExceptionValueIntrinsic && |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 717 | !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception))) |
| 718 | return false; |
| 719 | |
| 720 | bool Changed = false; |
| 721 | |
| 722 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 723 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) |
| 724 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) |
| 725 | if (CI->getIntrinsicID() == Intrinsic::eh_exception) { |
| 726 | if (!CI->use_empty()) { |
| 727 | Value *ExceptionValue = CreateReadOfExceptionValue(BB); |
| 728 | if (CI == ExceptionValue) { |
| 729 | // The call was at the start of a landing pad - leave it alone. |
| 730 | assert(LandingPads.count(BB) && |
| 731 | "Created eh.exception call outside landing pad!"); |
| 732 | continue; |
| 733 | } |
| 734 | CI->replaceAllUsesWith(ExceptionValue); |
| 735 | } |
| 736 | CI->eraseFromParent(); |
| 737 | ++NumExceptionValuesMoved; |
| 738 | Changed = true; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | return Changed; |
| 743 | } |
| 744 | |
| 745 | /// FinishStackTemporaries - If we introduced a stack variable to hold the |
| 746 | /// exception value then initialize it in each landing pad. |
| 747 | bool DwarfEHPrepare::FinishStackTemporaries() { |
| 748 | if (!ExceptionValueVar) |
| 749 | // Nothing to do. |
| 750 | return false; |
| 751 | |
| 752 | bool Changed = false; |
| 753 | |
| 754 | // Make sure that there is a store of the exception value at the start of |
| 755 | // each landing pad. |
| 756 | for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end(); |
| 757 | LI != LE; ++LI) { |
| 758 | Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI); |
| 759 | Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar); |
| 760 | Store->insertAfter(ExceptionValue); |
| 761 | Changed = true; |
| 762 | } |
| 763 | |
| 764 | return Changed; |
| 765 | } |
| 766 | |
| 767 | /// PromoteStackTemporaries - Turn any stack temporaries we introduced into |
| 768 | /// registers if possible. |
| 769 | bool DwarfEHPrepare::PromoteStackTemporaries() { |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 770 | // Turn the exception temporary into registers and phi nodes if possible. |
| 771 | if (ExceptionValueVar && isAllocaPromotable(ExceptionValueVar)) { |
| 772 | PromoteAlloca(ExceptionValueVar); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 773 | return true; |
| 774 | } |
| 775 | return false; |
| 776 | } |
| 777 | |
| 778 | /// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at |
| 779 | /// the start of the basic block (unless there already is one, in which case |
| 780 | /// the existing call is returned). |
| 781 | Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) { |
Dale Johannesen | 7249ef0 | 2010-04-02 21:49:27 +0000 | [diff] [blame] | 782 | Instruction *Start = BB->getFirstNonPHIOrDbg(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 783 | // Is this a call to eh.exception? |
| 784 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start)) |
| 785 | if (CI->getIntrinsicID() == Intrinsic::eh_exception) |
| 786 | // Reuse the existing call. |
| 787 | return Start; |
| 788 | |
| 789 | // Find the eh.exception intrinsic if we didn't already. |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 790 | if (!ExceptionValueIntrinsic) |
| 791 | ExceptionValueIntrinsic = Intrinsic::getDeclaration(F->getParent(), |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 792 | Intrinsic::eh_exception); |
| 793 | |
| 794 | // Create the call. |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 795 | return CallInst::Create(ExceptionValueIntrinsic, "eh.value.call", Start); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /// CreateValueLoad - Insert a load of the exception value stack variable |
| 799 | /// (creating it if necessary) at the start of the basic block (unless |
| 800 | /// there already is a load, in which case the existing load is returned). |
| 801 | Instruction *DwarfEHPrepare::CreateValueLoad(BasicBlock *BB) { |
Dale Johannesen | 7249ef0 | 2010-04-02 21:49:27 +0000 | [diff] [blame] | 802 | Instruction *Start = BB->getFirstNonPHIOrDbg(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 803 | // Is this a load of the exception temporary? |
| 804 | if (ExceptionValueVar) |
| 805 | if (LoadInst* LI = dyn_cast<LoadInst>(Start)) |
| 806 | if (LI->getPointerOperand() == ExceptionValueVar) |
| 807 | // Reuse the existing load. |
| 808 | return Start; |
| 809 | |
| 810 | // Create the temporary if we didn't already. |
| 811 | if (!ExceptionValueVar) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 812 | ExceptionValueVar = new AllocaInst(PointerType::getUnqual( |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 813 | Type::getInt8Ty(BB->getContext())), "eh.value", F->begin()->begin()); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 814 | ++NumStackTempsIntroduced; |
| 815 | } |
| 816 | |
| 817 | // Load the value. |
| 818 | return new LoadInst(ExceptionValueVar, "eh.value.load", Start); |
| 819 | } |
| 820 | |
| 821 | bool DwarfEHPrepare::runOnFunction(Function &Fn) { |
| 822 | bool Changed = false; |
| 823 | |
| 824 | // Initialize internal state. |
| 825 | DT = getAnalysisIfAvailable<DominatorTree>(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 826 | ExceptionValueVar = 0; |
| 827 | F = &Fn; |
| 828 | |
| 829 | // Ensure that only unwind edges end at landing pads (a landing pad is a |
| 830 | // basic block where an invoke unwind edge ends). |
| 831 | Changed |= NormalizeLandingPads(); |
| 832 | |
| 833 | // Turn unwind instructions into libcalls. |
| 834 | Changed |= LowerUnwinds(); |
| 835 | |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 836 | // TODO: Move eh.selector calls to landing pads and combine them. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 837 | |
| 838 | // Move eh.exception calls to landing pads. |
| 839 | Changed |= MoveExceptionValueCalls(); |
| 840 | |
| 841 | // Initialize any stack temporaries we introduced. |
| 842 | Changed |= FinishStackTemporaries(); |
| 843 | |
Chris Lattner | 4455608 | 2010-08-29 19:54:28 +0000 | [diff] [blame^] | 844 | // Turn any stack temporaries into registers. |
Bill Wendling | 8bedf97 | 2009-10-29 00:37:35 +0000 | [diff] [blame] | 845 | if (!CompileFast) |
| 846 | Changed |= PromoteStackTemporaries(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 847 | |
Bill Wendling | 2ad4aca | 2010-03-26 23:41:30 +0000 | [diff] [blame] | 848 | Changed |= HandleURoRInvokes(); |
| 849 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 850 | LandingPads.clear(); |
| 851 | |
| 852 | return Changed; |
| 853 | } |