1 //===- SjLjEHPrepare.cpp - Eliminate Invoke & Unwind instructions ---------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This transformation is designed for use by code generators which use SjLj 10 // based exception handling. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/SetVector.h" 15 #include "llvm/ADT/SmallPtrSet.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/Statistic.h" 18 #include "llvm/CodeGen/Passes.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/DataLayout.h" 21 #include "llvm/IR/DerivedTypes.h" 22 #include "llvm/IR/IRBuilder.h" 23 #include "llvm/IR/Instructions.h" 24 #include "llvm/IR/Intrinsics.h" 25 #include "llvm/IR/Module.h" 26 #include "llvm/InitializePasses.h" 27 #include "llvm/Pass.h" 28 #include "llvm/Support/Debug.h" 29 #include "llvm/Support/raw_ostream.h" 30 #include "llvm/Target/TargetMachine.h" 31 #include "llvm/Transforms/Utils/Local.h" 32 using namespace llvm; 33 34 #define DEBUG_TYPE "sjljehprepare" 35 36 STATISTIC(NumInvokes, "Number of invokes replaced"); 37 STATISTIC(NumSpilled, "Number of registers live across unwind edges"); 38 39 namespace { 40 class SjLjEHPrepare : public FunctionPass { 41 IntegerType *DataTy; 42 Type *doubleUnderDataTy; 43 Type *doubleUnderJBufTy; 44 Type *FunctionContextTy; 45 FunctionCallee RegisterFn; 46 FunctionCallee UnregisterFn; 47 Function *BuiltinSetupDispatchFn; 48 Function *FrameAddrFn; 49 Function *StackAddrFn; 50 Function *StackRestoreFn; 51 Function *LSDAAddrFn; 52 Function *CallSiteFn; 53 Function *FuncCtxFn; 54 AllocaInst *FuncCtx; 55 const TargetMachine *TM; 56 57 public: 58 static char ID; // Pass identification, replacement for typeid 59 explicit SjLjEHPrepare(const TargetMachine *TM = nullptr) 60 : FunctionPass(ID), TM(TM) {} 61 bool doInitialization(Module &M) override; 62 bool runOnFunction(Function &F) override; 63 64 void getAnalysisUsage(AnalysisUsage &AU) const override {} 65 StringRef getPassName() const override { 66 return "SJLJ Exception Handling preparation"; 67 } 68 69 private: 70 bool setupEntryBlockAndCallSites(Function &F); 71 void substituteLPadValues(LandingPadInst *LPI, Value *ExnVal, Value *SelVal); 72 Value *setupFunctionContext(Function &F, ArrayRef<LandingPadInst *> LPads); 73 void lowerIncomingArguments(Function &F); 74 void lowerAcrossUnwindEdges(Function &F, ArrayRef<InvokeInst *> Invokes); 75 void insertCallSiteStore(Instruction *I, int Number); 76 }; 77 } // end anonymous namespace 78 79 char SjLjEHPrepare::ID = 0; 80 INITIALIZE_PASS(SjLjEHPrepare, DEBUG_TYPE, "Prepare SjLj exceptions", 81 false, false) 82 83 // Public Interface To the SjLjEHPrepare pass. 84 FunctionPass *llvm::createSjLjEHPreparePass(const TargetMachine *TM) { 85 return new SjLjEHPrepare(TM); 86 } 87 88 // doInitialization - Set up decalarations and types needed to process 89 // exceptions. 90 bool SjLjEHPrepare::doInitialization(Module &M) { 91 // Build the function context structure. 92 // builtin_setjmp uses a five word jbuf 93 Type *VoidPtrTy = Type::getInt8PtrTy(M.getContext()); 94 unsigned DataBits = 95 TM ? TM->getSjLjDataSize() : TargetMachine::DefaultSjLjDataSize; 96 DataTy = Type::getIntNTy(M.getContext(), DataBits); 97 doubleUnderDataTy = ArrayType::get(DataTy, 4); 98 doubleUnderJBufTy = ArrayType::get(VoidPtrTy, 5); 99 FunctionContextTy = StructType::get(VoidPtrTy, // __prev 100 DataTy, // call_site 101 doubleUnderDataTy, // __data 102 VoidPtrTy, // __personality 103 VoidPtrTy, // __lsda 104 doubleUnderJBufTy // __jbuf 105 ); 106 107 return true; 108 } 109 110 /// insertCallSiteStore - Insert a store of the call-site value to the 111 /// function context 112 void SjLjEHPrepare::insertCallSiteStore(Instruction *I, int Number) { 113 IRBuilder<> Builder(I); 114 115 // Get a reference to the call_site field. 116 Type *Int32Ty = Type::getInt32Ty(I->getContext()); 117 Value *Zero = ConstantInt::get(Int32Ty, 0); 118 Value *One = ConstantInt::get(Int32Ty, 1); 119 Value *Idxs[2] = { Zero, One }; 120 Value *CallSite = 121 Builder.CreateGEP(FunctionContextTy, FuncCtx, Idxs, "call_site"); 122 123 // Insert a store of the call-site number 124 ConstantInt *CallSiteNoC = ConstantInt::get(DataTy, Number); 125 Builder.CreateStore(CallSiteNoC, CallSite, true /*volatile*/); 126 } 127 128 /// MarkBlocksLiveIn - Insert BB and all of its predecessors into LiveBBs until 129 /// we reach blocks we've already seen. 130 static void MarkBlocksLiveIn(BasicBlock *BB, 131 SmallPtrSetImpl<BasicBlock *> &LiveBBs) { 132 if (!LiveBBs.insert(BB).second) 133 return; // already been here. 134 135 df_iterator_default_set<BasicBlock*> Visited; 136 137 for (BasicBlock *B : inverse_depth_first_ext(BB, Visited)) 138 LiveBBs.insert(B); 139 } 140 141 /// substituteLPadValues - Substitute the values returned by the landingpad 142 /// instruction with those returned by the personality function. 143 void SjLjEHPrepare::substituteLPadValues(LandingPadInst *LPI, Value *ExnVal, 144 Value *SelVal) { 145 SmallVector<Value *, 8> UseWorkList(LPI->users()); 146 while (!UseWorkList.empty()) { 147 Value *Val = UseWorkList.pop_back_val(); 148 auto *EVI = dyn_cast<ExtractValueInst>(Val); 149 if (!EVI) 150 continue; 151 if (EVI->getNumIndices() != 1) 152 continue; 153 if (*EVI->idx_begin() == 0) 154 EVI->replaceAllUsesWith(ExnVal); 155 else if (*EVI->idx_begin() == 1) 156 EVI->replaceAllUsesWith(SelVal); 157 if (EVI->use_empty()) 158 EVI->eraseFromParent(); 159 } 160 161 if (LPI->use_empty()) 162 return; 163 164 // There are still some uses of LPI. Construct an aggregate with the exception 165 // values and replace the LPI with that aggregate. 166 Type *LPadType = LPI->getType(); 167 Value *LPadVal = UndefValue::get(LPadType); 168 auto *SelI = cast<Instruction>(SelVal); 169 IRBuilder<> Builder(SelI->getParent(), std::next(SelI->getIterator())); 170 LPadVal = Builder.CreateInsertValue(LPadVal, ExnVal, 0, "lpad.val"); 171 LPadVal = Builder.CreateInsertValue(LPadVal, SelVal, 1, "lpad.val"); 172 173 LPI->replaceAllUsesWith(LPadVal); 174 } 175 176 /// setupFunctionContext - Allocate the function context on the stack and fill 177 /// it with all of the data that we know at this point. 178 Value *SjLjEHPrepare::setupFunctionContext(Function &F, 179 ArrayRef<LandingPadInst *> LPads) { 180 BasicBlock *EntryBB = &F.front(); 181 182 // Create an alloca for the incoming jump buffer ptr and the new jump buffer 183 // that needs to be restored on all exits from the function. This is an alloca 184 // because the value needs to be added to the global context list. 185 auto &DL = F.getParent()->getDataLayout(); 186 const Align Alignment(DL.getPrefTypeAlignment(FunctionContextTy)); 187 FuncCtx = new AllocaInst(FunctionContextTy, DL.getAllocaAddrSpace(), nullptr, 188 Alignment, "fn_context", &EntryBB->front()); 189 190 // Fill in the function context structure. 191 for (LandingPadInst *LPI : LPads) { 192 IRBuilder<> Builder(LPI->getParent(), 193 LPI->getParent()->getFirstInsertionPt()); 194 195 // Reference the __data field. 196 Value *FCData = 197 Builder.CreateConstGEP2_32(FunctionContextTy, FuncCtx, 0, 2, "__data"); 198 199 // The exception values come back in context->__data[0]. 200 Value *ExceptionAddr = Builder.CreateConstGEP2_32(doubleUnderDataTy, FCData, 201 0, 0, "exception_gep"); 202 Value *ExnVal = Builder.CreateLoad(DataTy, ExceptionAddr, true, "exn_val"); 203 ExnVal = Builder.CreateIntToPtr(ExnVal, Builder.getInt8PtrTy()); 204 205 Value *SelectorAddr = Builder.CreateConstGEP2_32(doubleUnderDataTy, FCData, 206 0, 1, "exn_selector_gep"); 207 Value *SelVal = 208 Builder.CreateLoad(DataTy, SelectorAddr, true, "exn_selector_val"); 209 210 // SelVal must be Int32Ty, so trunc it 211 SelVal = Builder.CreateTrunc(SelVal, Type::getInt32Ty(F.getContext())); 212 213 substituteLPadValues(LPI, ExnVal, SelVal); 214 } 215 216 // Personality function 217 IRBuilder<> Builder(EntryBB->getTerminator()); 218 Value *PersonalityFn = F.getPersonalityFn(); 219 Value *PersonalityFieldPtr = Builder.CreateConstGEP2_32( 220 FunctionContextTy, FuncCtx, 0, 3, "pers_fn_gep"); 221 Builder.CreateStore( 222 Builder.CreateBitCast(PersonalityFn, Builder.getInt8PtrTy()), 223 PersonalityFieldPtr, /*isVolatile=*/true); 224 225 // LSDA address 226 Value *LSDA = Builder.CreateCall(LSDAAddrFn, {}, "lsda_addr"); 227 Value *LSDAFieldPtr = 228 Builder.CreateConstGEP2_32(FunctionContextTy, FuncCtx, 0, 4, "lsda_gep"); 229 Builder.CreateStore(LSDA, LSDAFieldPtr, /*isVolatile=*/true); 230 231 return FuncCtx; 232 } 233 234 /// lowerIncomingArguments - To avoid having to handle incoming arguments 235 /// specially, we lower each arg to a copy instruction in the entry block. This 236 /// ensures that the argument value itself cannot be live out of the entry 237 /// block. 238 void SjLjEHPrepare::lowerIncomingArguments(Function &F) { 239 BasicBlock::iterator AfterAllocaInsPt = F.begin()->begin(); 240 while (isa<AllocaInst>(AfterAllocaInsPt) && 241 cast<AllocaInst>(AfterAllocaInsPt)->isStaticAlloca()) 242 ++AfterAllocaInsPt; 243 assert(AfterAllocaInsPt != F.front().end()); 244 245 for (auto &AI : F.args()) { 246 // Swift error really is a register that we model as memory -- instruction 247 // selection will perform mem-to-reg for us and spill/reload appropriately 248 // around calls that clobber it. There is no need to spill this 249 // value to the stack and doing so would not be allowed. 250 if (AI.isSwiftError()) 251 continue; 252 253 Type *Ty = AI.getType(); 254 255 // Use 'select i8 true, %arg, undef' to simulate a 'no-op' instruction. 256 Value *TrueValue = ConstantInt::getTrue(F.getContext()); 257 Value *UndefValue = UndefValue::get(Ty); 258 Instruction *SI = SelectInst::Create( 259 TrueValue, &AI, UndefValue, AI.getName() + ".tmp", &*AfterAllocaInsPt); 260 AI.replaceAllUsesWith(SI); 261 262 // Reset the operand, because it was clobbered by the RAUW above. 263 SI->setOperand(1, &AI); 264 } 265 } 266 267 /// lowerAcrossUnwindEdges - Find all variables which are alive across an unwind 268 /// edge and spill them. 269 void SjLjEHPrepare::lowerAcrossUnwindEdges(Function &F, 270 ArrayRef<InvokeInst *> Invokes) { 271 // Finally, scan the code looking for instructions with bad live ranges. 272 for (BasicBlock &BB : F) { 273 for (Instruction &Inst : BB) { 274 // Ignore obvious cases we don't have to handle. In particular, most 275 // instructions either have no uses or only have a single use inside the 276 // current block. Ignore them quickly. 277 if (Inst.use_empty()) 278 continue; 279 if (Inst.hasOneUse() && 280 cast<Instruction>(Inst.user_back())->getParent() == &BB && 281 !isa<PHINode>(Inst.user_back())) 282 continue; 283 284 // If this is an alloca in the entry block, it's not a real register 285 // value. 286 if (auto *AI = dyn_cast<AllocaInst>(&Inst)) 287 if (AI->isStaticAlloca()) 288 continue; 289 290 // Avoid iterator invalidation by copying users to a temporary vector. 291 SmallVector<Instruction *, 16> Users; 292 for (User *U : Inst.users()) { 293 Instruction *UI = cast<Instruction>(U); 294 if (UI->getParent() != &BB || isa<PHINode>(UI)) 295 Users.push_back(UI); 296 } 297 298 // Find all of the blocks that this value is live in. 299 SmallPtrSet<BasicBlock *, 32> LiveBBs; 300 LiveBBs.insert(&BB); 301 while (!Users.empty()) { 302 Instruction *U = Users.pop_back_val(); 303 304 if (!isa<PHINode>(U)) { 305 MarkBlocksLiveIn(U->getParent(), LiveBBs); 306 } else { 307 // Uses for a PHI node occur in their predecessor block. 308 PHINode *PN = cast<PHINode>(U); 309 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 310 if (PN->getIncomingValue(i) == &Inst) 311 MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs); 312 } 313 } 314 315 // Now that we know all of the blocks that this thing is live in, see if 316 // it includes any of the unwind locations. 317 bool NeedsSpill = false; 318 for (InvokeInst *Invoke : Invokes) { 319 BasicBlock *UnwindBlock = Invoke->getUnwindDest(); 320 if (UnwindBlock != &BB && LiveBBs.count(UnwindBlock)) { 321 LLVM_DEBUG(dbgs() << "SJLJ Spill: " << Inst << " around " 322 << UnwindBlock->getName() << "\n"); 323 NeedsSpill = true; 324 break; 325 } 326 } 327 328 // If we decided we need a spill, do it. 329 // FIXME: Spilling this way is overkill, as it forces all uses of 330 // the value to be reloaded from the stack slot, even those that aren't 331 // in the unwind blocks. We should be more selective. 332 if (NeedsSpill) { 333 DemoteRegToStack(Inst, true); 334 ++NumSpilled; 335 } 336 } 337 } 338 339 // Go through the landing pads and remove any PHIs there. 340 for (InvokeInst *Invoke : Invokes) { 341 BasicBlock *UnwindBlock = Invoke->getUnwindDest(); 342 LandingPadInst *LPI = UnwindBlock->getLandingPadInst(); 343 344 // Place PHIs into a set to avoid invalidating the iterator. 345 SmallPtrSet<PHINode *, 8> PHIsToDemote; 346 for (BasicBlock::iterator PN = UnwindBlock->begin(); isa<PHINode>(PN); ++PN) 347 PHIsToDemote.insert(cast<PHINode>(PN)); 348 if (PHIsToDemote.empty()) 349 continue; 350 351 // Demote the PHIs to the stack. 352 for (PHINode *PN : PHIsToDemote) 353 DemotePHIToStack(PN); 354 355 // Move the landingpad instruction back to the top of the landing pad block. 356 LPI->moveBefore(&UnwindBlock->front()); 357 } 358 } 359 360 /// setupEntryBlockAndCallSites - Setup the entry block by creating and filling 361 /// the function context and marking the call sites with the appropriate 362 /// values. These values are used by the DWARF EH emitter. 363 bool SjLjEHPrepare::setupEntryBlockAndCallSites(Function &F) { 364 SmallVector<ReturnInst *, 16> Returns; 365 SmallVector<InvokeInst *, 16> Invokes; 366 SmallSetVector<LandingPadInst *, 16> LPads; 367 368 // Look through the terminators of the basic blocks to find invokes. 369 for (BasicBlock &BB : F) 370 if (auto *II = dyn_cast<InvokeInst>(BB.getTerminator())) { 371 if (Function *Callee = II->getCalledFunction()) 372 if (Callee->getIntrinsicID() == Intrinsic::donothing) { 373 // Remove the NOP invoke. 374 BranchInst::Create(II->getNormalDest(), II); 375 II->eraseFromParent(); 376 continue; 377 } 378 379 Invokes.push_back(II); 380 LPads.insert(II->getUnwindDest()->getLandingPadInst()); 381 } else if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator())) { 382 Returns.push_back(RI); 383 } 384 385 if (Invokes.empty()) 386 return false; 387 388 NumInvokes += Invokes.size(); 389 390 lowerIncomingArguments(F); 391 lowerAcrossUnwindEdges(F, Invokes); 392 393 Value *FuncCtx = 394 setupFunctionContext(F, makeArrayRef(LPads.begin(), LPads.end())); 395 BasicBlock *EntryBB = &F.front(); 396 IRBuilder<> Builder(EntryBB->getTerminator()); 397 398 // Get a reference to the jump buffer. 399 Value *JBufPtr = 400 Builder.CreateConstGEP2_32(FunctionContextTy, FuncCtx, 0, 5, "jbuf_gep"); 401 402 // Save the frame pointer. 403 Value *FramePtr = Builder.CreateConstGEP2_32(doubleUnderJBufTy, JBufPtr, 0, 0, 404 "jbuf_fp_gep"); 405 406 Value *Val = Builder.CreateCall(FrameAddrFn, Builder.getInt32(0), "fp"); 407 Builder.CreateStore(Val, FramePtr, /*isVolatile=*/true); 408 409 // Save the stack pointer. 410 Value *StackPtr = Builder.CreateConstGEP2_32(doubleUnderJBufTy, JBufPtr, 0, 2, 411 "jbuf_sp_gep"); 412 413 Val = Builder.CreateCall(StackAddrFn, {}, "sp"); 414 Builder.CreateStore(Val, StackPtr, /*isVolatile=*/true); 415 416 // Call the setup_dispatch instrinsic. It fills in the rest of the jmpbuf. 417 Builder.CreateCall(BuiltinSetupDispatchFn, {}); 418 419 // Store a pointer to the function context so that the back-end will know 420 // where to look for it. 421 Value *FuncCtxArg = Builder.CreateBitCast(FuncCtx, Builder.getInt8PtrTy()); 422 Builder.CreateCall(FuncCtxFn, FuncCtxArg); 423 424 // At this point, we are all set up, update the invoke instructions to mark 425 // their call_site values. 426 for (unsigned I = 0, E = Invokes.size(); I != E; ++I) { 427 insertCallSiteStore(Invokes[I], I + 1); 428 429 ConstantInt *CallSiteNum = 430 ConstantInt::get(Type::getInt32Ty(F.getContext()), I + 1); 431 432 // Record the call site value for the back end so it stays associated with 433 // the invoke. 434 CallInst::Create(CallSiteFn, CallSiteNum, "", Invokes[I]); 435 } 436 437 // Mark call instructions that aren't nounwind as no-action (call_site == 438 // -1). Skip the entry block, as prior to then, no function context has been 439 // created for this function and any unexpected exceptions thrown will go 440 // directly to the caller's context, which is what we want anyway, so no need 441 // to do anything here. 442 for (BasicBlock &BB : F) { 443 if (&BB == &F.front()) 444 continue; 445 for (Instruction &I : BB) 446 if (I.mayThrow()) 447 insertCallSiteStore(&I, -1); 448 } 449 450 // Register the function context and make sure it's known to not throw 451 CallInst *Register = 452 CallInst::Create(RegisterFn, FuncCtx, "", EntryBB->getTerminator()); 453 Register->setDoesNotThrow(); 454 455 // Following any allocas not in the entry block, update the saved SP in the 456 // jmpbuf to the new value. 457 for (BasicBlock &BB : F) { 458 if (&BB == &F.front()) 459 continue; 460 for (Instruction &I : BB) { 461 if (auto *CI = dyn_cast<CallInst>(&I)) { 462 if (CI->getCalledFunction() != StackRestoreFn) 463 continue; 464 } else if (!isa<AllocaInst>(&I)) { 465 continue; 466 } 467 Instruction *StackAddr = CallInst::Create(StackAddrFn, "sp"); 468 StackAddr->insertAfter(&I); 469 new StoreInst(StackAddr, StackPtr, true, StackAddr->getNextNode()); 470 } 471 } 472 473 // Finally, for any returns from this function, if this function contains an 474 // invoke, add a call to unregister the function context. 475 for (ReturnInst *Return : Returns) 476 CallInst::Create(UnregisterFn, FuncCtx, "", Return); 477 478 return true; 479 } 480 481 bool SjLjEHPrepare::runOnFunction(Function &F) { 482 Module &M = *F.getParent(); 483 RegisterFn = M.getOrInsertFunction( 484 "_Unwind_SjLj_Register", Type::getVoidTy(M.getContext()), 485 PointerType::getUnqual(FunctionContextTy)); 486 UnregisterFn = M.getOrInsertFunction( 487 "_Unwind_SjLj_Unregister", Type::getVoidTy(M.getContext()), 488 PointerType::getUnqual(FunctionContextTy)); 489 FrameAddrFn = Intrinsic::getDeclaration( 490 &M, Intrinsic::frameaddress, 491 {Type::getInt8PtrTy(M.getContext(), 492 M.getDataLayout().getAllocaAddrSpace())}); 493 StackAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave); 494 StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore); 495 BuiltinSetupDispatchFn = 496 Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_setup_dispatch); 497 LSDAAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_lsda); 498 CallSiteFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_callsite); 499 FuncCtxFn = Intrinsic::getDeclaration(&M, Intrinsic::eh_sjlj_functioncontext); 500 501 bool Res = setupEntryBlockAndCallSites(F); 502 return Res; 503 } 504