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