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