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 |
| 11 | // generation. Required if using dwarf exception handling. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "dwarfehprepare" |
| 16 | #include "llvm/ADT/Statistic.h" |
| 17 | #include "llvm/Analysis/Dominators.h" |
| 18 | #include "llvm/CodeGen/Passes.h" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/Instructions.h" |
| 21 | #include "llvm/IntrinsicInst.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/Pass.h" |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/IRBuilder.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 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 31 | STATISTIC(NumExceptionValuesMoved, "Number of eh.exception calls moved"); |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 32 | STATISTIC(NumLonelyLandingPads, "Number of landing pads with no selector"); |
| 33 | STATISTIC(NumLonelySelectors, "Number of lonely selectors lowered"); |
| 34 | STATISTIC(NumLandingPadsSplit, "Number of landing pads split"); |
| 35 | STATISTIC(NumSelectorsAdjusted, "Number of selector results adjusted"); |
| 36 | STATISTIC(NumSelectorsSimplified, "Number of selectors truncated"); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 37 | STATISTIC(NumStackTempsIntroduced, "Number of stack temporaries introduced"); |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 38 | STATISTIC(NumUnwindsLowered, "Number of unwind instructions lowered"); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 39 | |
| 40 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 41 | class DwarfEHPrepare : public FunctionPass { |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 42 | const TargetLowering *TLI; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 43 | |
| 44 | // The eh.exception intrinsic. |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 45 | Function *ExceptionIntrinsic; |
| 46 | |
| 47 | // The eh.selector intrinsic. |
| 48 | Function *SelectorIntrinsic; |
| 49 | |
| 50 | // The eh.typeid.for intrinsic. |
| 51 | Function *TypeIdIntrinsic; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 52 | |
| 53 | // _Unwind_Resume or the target equivalent. |
| 54 | Constant *RewindFunction; |
| 55 | |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 56 | // _Unwind_RaiseException. |
| 57 | Constant *UnwindFunction; |
| 58 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 59 | // Dominator info is used when turning stack temporaries into registers. |
| 60 | DominatorTree *DT; |
| 61 | DominanceFrontier *DF; |
| 62 | |
| 63 | // The function we are running on. |
| 64 | Function *F; |
| 65 | |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 66 | // The current context. |
| 67 | LLVMContext *Context; |
| 68 | |
| 69 | // The personality and catch-all value for this function. |
| 70 | Constant *Personality; |
| 71 | Constant *CatchAll; |
| 72 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 73 | // The landing pads for this function. |
| 74 | typedef SmallPtrSet<BasicBlock*, 8> BBSet; |
| 75 | BBSet LandingPads; |
| 76 | |
| 77 | // Stack temporary used to hold eh.exception values. |
| 78 | AllocaInst *ExceptionValueVar; |
| 79 | |
| 80 | bool NormalizeLandingPads(); |
| 81 | bool LowerUnwinds(); |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 82 | bool MoveSelectorCalls(); |
| 83 | bool RectifySelectorCalls(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 84 | bool MoveExceptionValueCalls(); |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 85 | bool AddMissingSelectors(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 86 | bool FinishStackTemporaries(); |
| 87 | bool PromoteStackTemporaries(); |
| 88 | |
| 89 | Instruction *CreateExceptionValueCall(BasicBlock *BB); |
| 90 | Instruction *CreateValueLoad(BasicBlock *BB); |
| 91 | |
| 92 | /// CreateReadOfExceptionValue - Return the result of the eh.exception |
| 93 | /// intrinsic by calling the intrinsic if in a landing pad, or loading |
| 94 | /// it from the exception value variable otherwise. |
| 95 | Instruction *CreateReadOfExceptionValue(BasicBlock *BB) { |
| 96 | return LandingPads.count(BB) ? |
| 97 | CreateExceptionValueCall(BB) : CreateValueLoad(BB); |
| 98 | } |
| 99 | |
| 100 | public: |
| 101 | static char ID; // Pass identification, replacement for typeid. |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 102 | DwarfEHPrepare(const TargetLowering *tli) : |
| 103 | FunctionPass(&ID), TLI(tli), ExceptionIntrinsic(0), |
| 104 | SelectorIntrinsic(0), TypeIdIntrinsic(0), RewindFunction(0), |
| 105 | UnwindFunction(0) {} |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 106 | |
| 107 | virtual bool runOnFunction(Function &Fn); |
| 108 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 109 | const char *getPassName() const { |
| 110 | return "Exception handling preparation"; |
| 111 | } |
| 112 | |
| 113 | }; |
| 114 | } // end anonymous namespace |
| 115 | |
| 116 | char DwarfEHPrepare::ID = 0; |
| 117 | |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 118 | FunctionPass *llvm::createDwarfEHPass(const TargetLowering *tli) { |
| 119 | return new DwarfEHPrepare(tli); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /// NormalizeLandingPads - Normalize and discover landing pads, noting them |
| 123 | /// 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] | 124 | /// that end at it are unwind edges from invoke instructions. If we inlined |
| 125 | /// through an invoke we could have a normal branch from the previous |
| 126 | /// unwind block through to the landing pad for the original invoke. |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 127 | /// Abnormal landing pads are fixed up by redirecting all unwind edges to |
| 128 | /// a new basic block which falls through to the original. |
| 129 | bool DwarfEHPrepare::NormalizeLandingPads() { |
| 130 | bool Changed = false; |
| 131 | |
| 132 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 133 | TerminatorInst *TI = I->getTerminator(); |
| 134 | if (!isa<InvokeInst>(TI)) |
| 135 | continue; |
| 136 | BasicBlock *LPad = TI->getSuccessor(1); |
| 137 | // Skip landing pads that have already been normalized. |
| 138 | if (LandingPads.count(LPad)) |
| 139 | continue; |
| 140 | |
| 141 | // Check that only invoke unwind edges end at the landing pad. |
| 142 | bool OnlyUnwoundTo = true; |
| 143 | for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); |
| 144 | PI != PE; ++PI) { |
| 145 | TerminatorInst *PT = (*PI)->getTerminator(); |
| 146 | if (!isa<InvokeInst>(PT) || LPad == PT->getSuccessor(0)) { |
| 147 | OnlyUnwoundTo = false; |
| 148 | break; |
| 149 | } |
| 150 | } |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 151 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 152 | if (OnlyUnwoundTo) { |
| 153 | // Only unwind edges lead to the landing pad. Remember the landing pad. |
| 154 | LandingPads.insert(LPad); |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | // At least one normal edge ends at the landing pad. Redirect the unwind |
| 159 | // edges to a new basic block which falls through into this one. |
| 160 | |
| 161 | // Create the new basic block. |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 162 | BasicBlock *NewBB = BasicBlock::Create(*Context, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 163 | LPad->getName() + "_unwind_edge"); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 164 | |
| 165 | // Insert it into the function right before the original landing pad. |
| 166 | LPad->getParent()->getBasicBlockList().insert(LPad, NewBB); |
| 167 | |
| 168 | // Redirect unwind edges from the original landing pad to NewBB. |
| 169 | for (pred_iterator PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ) { |
| 170 | TerminatorInst *PT = (*PI++)->getTerminator(); |
| 171 | if (isa<InvokeInst>(PT) && PT->getSuccessor(1) == LPad) |
| 172 | // Unwind to the new block. |
| 173 | PT->setSuccessor(1, NewBB); |
| 174 | } |
| 175 | |
| 176 | // If there are any PHI nodes in LPad, we need to update them so that they |
| 177 | // merge incoming values from NewBB instead. |
| 178 | for (BasicBlock::iterator II = LPad->begin(); isa<PHINode>(II); ++II) { |
| 179 | PHINode *PN = cast<PHINode>(II); |
| 180 | pred_iterator PB = pred_begin(NewBB), PE = pred_end(NewBB); |
| 181 | |
| 182 | // Check to see if all of the values coming in via unwind edges are the |
| 183 | // same. If so, we don't need to create a new PHI node. |
| 184 | Value *InVal = PN->getIncomingValueForBlock(*PB); |
| 185 | for (pred_iterator PI = PB; PI != PE; ++PI) { |
| 186 | if (PI != PB && InVal != PN->getIncomingValueForBlock(*PI)) { |
| 187 | InVal = 0; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (InVal == 0) { |
| 193 | // Different unwind edges have different values. Create a new PHI node |
| 194 | // in NewBB. |
| 195 | PHINode *NewPN = PHINode::Create(PN->getType(), PN->getName()+".unwind", |
| 196 | NewBB); |
| 197 | // Add an entry for each unwind edge, using the value from the old PHI. |
| 198 | for (pred_iterator PI = PB; PI != PE; ++PI) |
| 199 | NewPN->addIncoming(PN->getIncomingValueForBlock(*PI), *PI); |
| 200 | |
| 201 | // Now use this new PHI as the common incoming value for NewBB in PN. |
| 202 | InVal = NewPN; |
| 203 | } |
| 204 | |
| 205 | // Revector exactly one entry in the PHI node to come from NewBB |
| 206 | // and delete all other entries that come from unwind edges. If |
| 207 | // there are both normal and unwind edges from the same predecessor, |
| 208 | // this leaves an entry for the normal edge. |
| 209 | for (pred_iterator PI = PB; PI != PE; ++PI) |
| 210 | PN->removeIncomingValue(*PI); |
| 211 | PN->addIncoming(InVal, NewBB); |
| 212 | } |
| 213 | |
| 214 | // Add a fallthrough from NewBB to the original landing pad. |
| 215 | BranchInst::Create(LPad, NewBB); |
| 216 | |
| 217 | // Now update DominatorTree and DominanceFrontier analysis information. |
| 218 | if (DT) |
| 219 | DT->splitBlock(NewBB); |
| 220 | if (DF) |
| 221 | DF->splitBlock(NewBB); |
| 222 | |
| 223 | // Remember the newly constructed landing pad. The original landing pad |
| 224 | // LPad is no longer a landing pad now that all unwind edges have been |
| 225 | // revectored to NewBB. |
| 226 | LandingPads.insert(NewBB); |
| 227 | ++NumLandingPadsSplit; |
| 228 | Changed = true; |
| 229 | } |
| 230 | |
| 231 | return Changed; |
| 232 | } |
| 233 | |
| 234 | /// LowerUnwinds - Turn unwind instructions into calls to _Unwind_Resume, |
| 235 | /// rethrowing any previously caught exception. This will crash horribly |
| 236 | /// at runtime if there is no such exception: using unwind to throw a new |
| 237 | /// exception is currently not supported. |
| 238 | bool DwarfEHPrepare::LowerUnwinds() { |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 239 | SmallVector<TerminatorInst*, 16> UnwindInsts; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 240 | |
| 241 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) { |
| 242 | TerminatorInst *TI = I->getTerminator(); |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 243 | if (isa<UnwindInst>(TI)) |
| 244 | UnwindInsts.push_back(TI); |
| 245 | } |
| 246 | |
| 247 | if (UnwindInsts.empty()) return false; |
| 248 | |
| 249 | // Find the rewind function if we didn't already. |
| 250 | if (!RewindFunction) { |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 251 | std::vector<const Type*> |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 252 | Params(1, Type::getInt8PtrTy(*Context)); |
| 253 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(*Context), |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 254 | Params, false); |
| 255 | const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME); |
| 256 | RewindFunction = F->getParent()->getOrInsertFunction(RewindName, FTy); |
| 257 | } |
| 258 | |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 259 | for (SmallVectorImpl<TerminatorInst*>::iterator |
| 260 | I = UnwindInsts.begin(), E = UnwindInsts.end(); I != E; ++I) { |
| 261 | TerminatorInst *TI = *I; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 262 | |
| 263 | // Replace the unwind instruction with a call to _Unwind_Resume (or the |
| 264 | // appropriate target equivalent) followed by an UnreachableInst. |
| 265 | |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 266 | // Create the call... |
Eric Christopher | 82f149d | 2009-09-04 01:14:14 +0000 | [diff] [blame] | 267 | CallInst *CI = CallInst::Create(RewindFunction, |
Daniel Dunbar | a279bc3 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 268 | CreateReadOfExceptionValue(TI->getParent()), |
Bill Wendling | 4348871 | 2009-09-14 20:52:37 +0000 | [diff] [blame] | 269 | "", TI); |
Eric Christopher | 82f149d | 2009-09-04 01:14:14 +0000 | [diff] [blame] | 270 | CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME)); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 271 | // ...followed by an UnreachableInst. |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 272 | new UnreachableInst(*Context, TI); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 273 | |
| 274 | // Nuke the unwind instruction. |
| 275 | TI->eraseFromParent(); |
| 276 | ++NumUnwindsLowered; |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 277 | } |
| 278 | |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | /// MoveSelectorCalls - Make sure that every call to eh.selector occurs in its |
| 283 | /// own landing pad, the landing pad corresponding to the exception object. |
| 284 | bool DwarfEHPrepare::MoveSelectorCalls() { |
| 285 | // If the eh.selector intrinsic is not declared in the module then there is |
| 286 | // nothing to do. Speed up compilation by checking for this common case. |
| 287 | if (!F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_selector))) |
| 288 | return false; |
| 289 | |
| 290 | // TODO: There is a lot of room for optimization here. |
| 291 | |
| 292 | bool Changed = false; |
| 293 | BasicBlock *UnrBB = 0; |
| 294 | |
| 295 | for (Function::iterator BB = F->begin(); BB != F->end(); ++BB) { |
| 296 | // If this basic block is not a landing pad then synthesize a landing pad |
| 297 | // for every selector in it. |
| 298 | bool SynthesizeLandingPad = !LandingPads.count(BB); |
| 299 | |
| 300 | for (BasicBlock::iterator II = BB->begin(), IE = BB->end(); II != IE; ++II) { |
| 301 | EHSelectorInst *SI = dyn_cast<EHSelectorInst>(II); |
| 302 | // Only interested in eh.selector calls. |
| 303 | if (!SI) |
| 304 | continue; |
| 305 | |
| 306 | // Note the personality and catch-all for later use. |
| 307 | Personality = cast<Constant>(SI->getOperand(2)); |
| 308 | CatchAll = cast<Constant>(SI->getOperand(SI->getNumOperands() - 1) |
| 309 | ->stripPointerCasts()); |
| 310 | |
| 311 | // The exception object. |
| 312 | Value *Exception = SI->getOperand(1); |
| 313 | |
| 314 | if (!SynthesizeLandingPad) { |
| 315 | // Did the exception come from unwinding to this landing pad or another? |
| 316 | // If it comes from a different landing pad then we need to synthesize a |
| 317 | // new landing pad for the selector. |
| 318 | EHExceptionInst *EI = dyn_cast<EHExceptionInst>(Exception); |
| 319 | SynthesizeLandingPad = !EI || EI->getParent() != BB; |
| 320 | } |
| 321 | |
| 322 | if (!SynthesizeLandingPad) { |
| 323 | // This is the first selector in this landing pad, and it is the landing |
| 324 | // pad corresponding to the exception object. No need to do anything to |
| 325 | // this selector, but any subsequent selectors in this landing pad will |
| 326 | // need their own invoke in order to make them independent of this one. |
| 327 | SynthesizeLandingPad = true; |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | // Rethrow the exception and catch it again, generating a landing pad for |
| 332 | // this selector to live in. |
| 333 | |
| 334 | // Find _Unwind_RaiseException if we didn't already. |
| 335 | if (!UnwindFunction) { |
| 336 | std::vector<const Type*> ArgTys(1, Type::getInt8PtrTy(*Context)); |
| 337 | const FunctionType *FTy = |
| 338 | FunctionType::get(Type::getInt32Ty(*Context), ArgTys, true); |
| 339 | |
| 340 | const char *Name = "_Unwind_RaiseException"; |
| 341 | UnwindFunction = F->getParent()->getOrInsertFunction(Name, FTy); |
| 342 | } |
| 343 | |
| 344 | // Create a basic block containing only an unreachable instruction if we |
| 345 | // didn't already. |
| 346 | if (!UnrBB) { |
| 347 | UnrBB = BasicBlock::Create(*Context, "unreachable", F); |
| 348 | new UnreachableInst(*Context, UnrBB); |
| 349 | } |
| 350 | |
| 351 | // Split the basic block before the selector. |
| 352 | BasicBlock *NewBB = SplitBlock(BB, SI, this); |
| 353 | |
| 354 | // Replace the terminator with an invoke of _Unwind_RaiseException. |
| 355 | BB->getTerminator()->eraseFromParent(); |
| 356 | InvokeInst::Create(UnwindFunction, UnrBB, NewBB, &Exception, |
| 357 | 1 + &Exception, "", BB); |
| 358 | |
| 359 | // The split off basic block is now a landing pad. |
| 360 | LandingPads.insert(NewBB); |
| 361 | |
| 362 | // Replace the exception argument in the selector call with a call to |
| 363 | // eh.exception. This is not really necessary but it makes things more |
| 364 | // regular. |
| 365 | Exception = CreateExceptionValueCall(NewBB); |
| 366 | SI->setOperand(1, Exception); |
| 367 | |
| 368 | ++NumLonelySelectors; |
| 369 | Changed = true; |
| 370 | |
| 371 | // All instructions still in the original basic block have been scanned. |
| 372 | // Move on to the next basic block. |
| 373 | break; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return Changed; |
| 378 | } |
| 379 | |
| 380 | /// RectifySelectorCalls - Remove useless catch-all clauses from the ends of |
| 381 | /// selectors, or correct the selector result for the presence of the catch-all |
| 382 | /// if it is really needed. |
| 383 | bool DwarfEHPrepare::RectifySelectorCalls() { |
| 384 | // If the eh.selector intrinsic is not declared in the module then there is |
| 385 | // nothing to do. Speed up compilation by checking for this common case. |
| 386 | if (!F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_selector))) |
| 387 | return false; |
| 388 | |
| 389 | bool Changed = false; |
| 390 | |
| 391 | for (BBSet::iterator I = LandingPads.begin(), E = LandingPads.end(); I != E; |
| 392 | ++I) |
| 393 | for (BasicBlock::iterator II = (*I)->begin(), IE = (*I)->end(); II != IE; ) |
| 394 | if (EHSelectorInst *SI = dyn_cast<EHSelectorInst>(II++)) { |
| 395 | // Found a call to eh.selector. Check whether it has a catch-all in the |
| 396 | // middle. |
| 397 | unsigned LastIndex = 0; |
| 398 | for (unsigned i = 3, e = SI->getNumOperands() - 1; i < e; ++i) { |
| 399 | Value *V = SI->getOperand(i); |
| 400 | if (V->stripPointerCasts() == CatchAll) { |
| 401 | // A catch-all. The catch-all at the end was not needed. |
| 402 | LastIndex = i; |
| 403 | break; |
| 404 | } else if (ConstantInt *FilterLength = dyn_cast<ConstantInt>(V)) { |
| 405 | // A cleanup or a filter. |
| 406 | unsigned Length = FilterLength->getZExtValue(); |
| 407 | if (Length == 0) |
| 408 | // A cleanup - skip it. |
| 409 | continue; |
| 410 | if (Length == 1) { |
| 411 | // A catch-all filter. Drop everything that follows. |
| 412 | LastIndex = i; |
| 413 | break; |
| 414 | } |
| 415 | // A filter, skip over the typeinfos. |
| 416 | i += Length - 1; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (LastIndex) { |
| 421 | // Drop the pointless catch-all from the end. In fact drop everything |
| 422 | // after LastIndex as an optimization. |
| 423 | SmallVector<Value*, 16> Args; |
| 424 | Args.reserve(LastIndex); |
| 425 | for (unsigned i = 1; i <= LastIndex; ++i) |
| 426 | Args.push_back(SI->getOperand(i)); |
| 427 | CallInst *CI = CallInst::Create(SI->getOperand(0), Args.begin(), |
| 428 | Args.end(), "", SI); |
| 429 | CI->takeName(SI); |
| 430 | SI->replaceAllUsesWith(CI); |
| 431 | SI->eraseFromParent(); |
| 432 | ++NumSelectorsSimplified; |
| 433 | } else if (!isa<ConstantInt>(CatchAll) && // Not a cleanup. |
| 434 | !SI->use_empty()) { |
| 435 | // Correct the selector value to return zero if the catch-all matches. |
| 436 | Constant *Zero = ConstantInt::getNullValue(Type::getInt32Ty(*Context)); |
| 437 | |
| 438 | // Create the new selector value, with placeholders instead of the |
| 439 | // real operands and make everyone use it. The reason for this round |
| 440 | // about approach is that the computation of the new value makes use |
| 441 | // of the old value, so we can't just compute it then do RAUW. |
| 442 | SelectInst *S = SelectInst::Create(ConstantInt::getFalse(*Context), |
| 443 | Zero, Zero, "", II); |
| 444 | SI->replaceAllUsesWith(S); |
| 445 | |
| 446 | // Now calculate the operands of the select. |
| 447 | IRBuilder<> Builder(*I, S); |
| 448 | |
| 449 | // Find the eh.typeid.for intrinsic if we didn't already. |
| 450 | if (!TypeIdIntrinsic) |
| 451 | TypeIdIntrinsic = Intrinsic::getDeclaration(F->getParent(), |
| 452 | Intrinsic::eh_typeid_for); |
| 453 | |
| 454 | // Obtain the id of the catch-all. |
| 455 | Value *CatchAllId = Builder.CreateCall(TypeIdIntrinsic, |
| 456 | ConstantExpr::getBitCast(CatchAll, Type::getInt8PtrTy(*Context))); |
| 457 | |
| 458 | // Compare it with the original selector result. If it matched then |
| 459 | // the selector result is zero, otherwise it is the original selector. |
| 460 | Value *MatchesCatchAll = Builder.CreateICmpEQ(SI, CatchAllId); |
| 461 | S->setOperand(0, MatchesCatchAll); |
| 462 | S->setOperand(2, SI); |
| 463 | ++NumSelectorsAdjusted; |
| 464 | } |
| 465 | |
| 466 | Changed = true; |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | return Changed; |
| 471 | } |
| 472 | |
| 473 | /// Make sure every landing pad has a selector in it. |
| 474 | bool DwarfEHPrepare::AddMissingSelectors() { |
| 475 | if (!Personality) |
| 476 | // We only know how to codegen invokes if there is a personality. |
| 477 | // FIXME: This results in wrong code. |
| 478 | return false; |
| 479 | |
| 480 | bool Changed = false; |
| 481 | |
| 482 | for (BBSet::iterator I = LandingPads.begin(), E = LandingPads.end(); I != E; |
| 483 | ++I) { |
| 484 | bool FoundSelector = false; |
| 485 | |
| 486 | // Check whether the landing pad already contains a call to eh.selector. |
| 487 | for (BasicBlock::iterator II = (*I)->begin(), IE = (*I)->end(); II != IE; |
| 488 | ++II) |
| 489 | if (isa<EHSelectorInst>(II)) { |
| 490 | FoundSelector = true; |
| 491 | break; |
| 492 | } |
| 493 | |
| 494 | if (FoundSelector) |
| 495 | continue; |
| 496 | |
| 497 | // Find the eh.selector intrinsic if we didn't already. |
| 498 | if (!SelectorIntrinsic) |
| 499 | SelectorIntrinsic = Intrinsic::getDeclaration(F->getParent(), |
| 500 | Intrinsic::eh_selector); |
| 501 | |
| 502 | // Get the exception object. |
| 503 | Instruction *Exception = CreateExceptionValueCall(*I); |
| 504 | |
| 505 | Value *Args[3] = { Exception, Personality, CatchAll }; |
| 506 | CallInst *Selector = CallInst::Create(SelectorIntrinsic, Args, Args + 3); |
| 507 | Selector->insertAfter(Exception); |
| 508 | |
| 509 | ++NumLonelyLandingPads; |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 510 | Changed = true; |
| 511 | } |
| 512 | |
| 513 | return Changed; |
| 514 | } |
| 515 | |
| 516 | /// MoveExceptionValueCalls - Ensure that eh.exception is only ever called from |
| 517 | /// landing pads by replacing calls outside of landing pads with loads from a |
| 518 | /// stack temporary. Move eh.exception calls inside landing pads to the start |
| 519 | /// of the landing pad (optional, but may make things simpler for later passes). |
| 520 | bool DwarfEHPrepare::MoveExceptionValueCalls() { |
| 521 | // If the eh.exception intrinsic is not declared in the module then there is |
| 522 | // nothing to do. Speed up compilation by checking for this common case. |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 523 | if (!ExceptionIntrinsic && |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 524 | !F->getParent()->getFunction(Intrinsic::getName(Intrinsic::eh_exception))) |
| 525 | return false; |
| 526 | |
| 527 | bool Changed = false; |
| 528 | |
| 529 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 530 | for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E;) |
| 531 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(II++)) |
| 532 | if (CI->getIntrinsicID() == Intrinsic::eh_exception) { |
| 533 | if (!CI->use_empty()) { |
| 534 | Value *ExceptionValue = CreateReadOfExceptionValue(BB); |
| 535 | if (CI == ExceptionValue) { |
| 536 | // The call was at the start of a landing pad - leave it alone. |
| 537 | assert(LandingPads.count(BB) && |
| 538 | "Created eh.exception call outside landing pad!"); |
| 539 | continue; |
| 540 | } |
| 541 | CI->replaceAllUsesWith(ExceptionValue); |
| 542 | } |
| 543 | CI->eraseFromParent(); |
| 544 | ++NumExceptionValuesMoved; |
| 545 | Changed = true; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | return Changed; |
| 550 | } |
| 551 | |
| 552 | /// FinishStackTemporaries - If we introduced a stack variable to hold the |
| 553 | /// exception value then initialize it in each landing pad. |
| 554 | bool DwarfEHPrepare::FinishStackTemporaries() { |
| 555 | if (!ExceptionValueVar) |
| 556 | // Nothing to do. |
| 557 | return false; |
| 558 | |
| 559 | bool Changed = false; |
| 560 | |
| 561 | // Make sure that there is a store of the exception value at the start of |
| 562 | // each landing pad. |
| 563 | for (BBSet::iterator LI = LandingPads.begin(), LE = LandingPads.end(); |
| 564 | LI != LE; ++LI) { |
| 565 | Instruction *ExceptionValue = CreateReadOfExceptionValue(*LI); |
| 566 | Instruction *Store = new StoreInst(ExceptionValue, ExceptionValueVar); |
| 567 | Store->insertAfter(ExceptionValue); |
| 568 | Changed = true; |
| 569 | } |
| 570 | |
| 571 | return Changed; |
| 572 | } |
| 573 | |
| 574 | /// PromoteStackTemporaries - Turn any stack temporaries we introduced into |
| 575 | /// registers if possible. |
| 576 | bool DwarfEHPrepare::PromoteStackTemporaries() { |
| 577 | if (ExceptionValueVar && DT && DF && isAllocaPromotable(ExceptionValueVar)) { |
| 578 | // Turn the exception temporary into registers and phi nodes if possible. |
| 579 | std::vector<AllocaInst*> Allocas(1, ExceptionValueVar); |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 580 | PromoteMemToReg(Allocas, *DT, *DF, *Context); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 581 | return true; |
| 582 | } |
| 583 | return false; |
| 584 | } |
| 585 | |
| 586 | /// CreateExceptionValueCall - Insert a call to the eh.exception intrinsic at |
| 587 | /// the start of the basic block (unless there already is one, in which case |
| 588 | /// the existing call is returned). |
| 589 | Instruction *DwarfEHPrepare::CreateExceptionValueCall(BasicBlock *BB) { |
| 590 | Instruction *Start = BB->getFirstNonPHI(); |
| 591 | // Is this a call to eh.exception? |
| 592 | if (IntrinsicInst *CI = dyn_cast<IntrinsicInst>(Start)) |
| 593 | if (CI->getIntrinsicID() == Intrinsic::eh_exception) |
| 594 | // Reuse the existing call. |
| 595 | return Start; |
| 596 | |
| 597 | // Find the eh.exception intrinsic if we didn't already. |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 598 | if (!ExceptionIntrinsic) |
| 599 | ExceptionIntrinsic = Intrinsic::getDeclaration(F->getParent(), |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 600 | Intrinsic::eh_exception); |
| 601 | |
| 602 | // Create the call. |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 603 | return CallInst::Create(ExceptionIntrinsic, "eh.value.call", Start); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /// CreateValueLoad - Insert a load of the exception value stack variable |
| 607 | /// (creating it if necessary) at the start of the basic block (unless |
| 608 | /// there already is a load, in which case the existing load is returned). |
| 609 | Instruction *DwarfEHPrepare::CreateValueLoad(BasicBlock *BB) { |
| 610 | Instruction *Start = BB->getFirstNonPHI(); |
| 611 | // Is this a load of the exception temporary? |
| 612 | if (ExceptionValueVar) |
| 613 | if (LoadInst* LI = dyn_cast<LoadInst>(Start)) |
| 614 | if (LI->getPointerOperand() == ExceptionValueVar) |
| 615 | // Reuse the existing load. |
| 616 | return Start; |
| 617 | |
| 618 | // Create the temporary if we didn't already. |
| 619 | if (!ExceptionValueVar) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 620 | ExceptionValueVar = new AllocaInst(PointerType::getUnqual( |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 621 | Type::getInt8Ty(*Context)), "eh.value", F->begin()->begin()); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 622 | ++NumStackTempsIntroduced; |
| 623 | } |
| 624 | |
| 625 | // Load the value. |
| 626 | return new LoadInst(ExceptionValueVar, "eh.value.load", Start); |
| 627 | } |
| 628 | |
| 629 | bool DwarfEHPrepare::runOnFunction(Function &Fn) { |
| 630 | bool Changed = false; |
| 631 | |
| 632 | // Initialize internal state. |
| 633 | DT = getAnalysisIfAvailable<DominatorTree>(); |
| 634 | DF = getAnalysisIfAvailable<DominanceFrontier>(); |
| 635 | ExceptionValueVar = 0; |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 636 | Personality = 0; |
| 637 | CatchAll = 0; |
| 638 | Context = &Fn.getContext(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 639 | F = &Fn; |
| 640 | |
| 641 | // Ensure that only unwind edges end at landing pads (a landing pad is a |
| 642 | // basic block where an invoke unwind edge ends). |
| 643 | Changed |= NormalizeLandingPads(); |
| 644 | |
| 645 | // Turn unwind instructions into libcalls. |
| 646 | Changed |= LowerUnwinds(); |
| 647 | |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 648 | // Make sure that every call to eh.selector occurs in its own landing pad. |
| 649 | Changed |= MoveSelectorCalls(); |
| 650 | |
| 651 | // Remove useless catch-all clauses from the ends of selectors, or correct the |
| 652 | // selector result for the presence of the catch-all if it is really needed. |
| 653 | Changed |= RectifySelectorCalls(); |
| 654 | |
| 655 | // Make sure every landing pad has a selector in it. |
| 656 | Changed |= AddMissingSelectors(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 657 | |
| 658 | // Move eh.exception calls to landing pads. |
| 659 | Changed |= MoveExceptionValueCalls(); |
| 660 | |
| 661 | // Initialize any stack temporaries we introduced. |
| 662 | Changed |= FinishStackTemporaries(); |
| 663 | |
| 664 | // Turn any stack temporaries into registers if possible. |
Bill Wendling | f58898f | 2009-10-29 00:22:16 +0000 | [diff] [blame^] | 665 | //TODO if (!CompileFast) |
| 666 | //TODO Changed |= PromoteStackTemporaries(); |
Duncan Sands | b0f1e17 | 2009-05-22 20:36:31 +0000 | [diff] [blame] | 667 | |
| 668 | LandingPads.clear(); |
| 669 | |
| 670 | return Changed; |
| 671 | } |