1 //===-- WinEHPrepare - Prepare exception handling for code generation ---===// 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 pass lowers LLVM IR exception handling into something closer to what the 10 // backend wants for functions using a personality function from a runtime 11 // provided by MSVC. Functions with other personality functions are left alone 12 // and may be prepared by other passes. In particular, all supported MSVC 13 // personality functions require cleanup code to be outlined, and the C++ 14 // personality requires catch handler code to be outlined. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #include "llvm/ADT/DenseMap.h" 19 #include "llvm/ADT/MapVector.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/CodeGen/MachineBasicBlock.h" 22 #include "llvm/CodeGen/Passes.h" 23 #include "llvm/CodeGen/WinEHFuncInfo.h" 24 #include "llvm/IR/Constants.h" 25 #include "llvm/IR/EHPersonalities.h" 26 #include "llvm/IR/Instructions.h" 27 #include "llvm/IR/Verifier.h" 28 #include "llvm/InitializePasses.h" 29 #include "llvm/Pass.h" 30 #include "llvm/Support/CommandLine.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/raw_ostream.h" 33 #include "llvm/TargetParser/Triple.h" 34 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 35 #include "llvm/Transforms/Utils/Cloning.h" 36 #include "llvm/Transforms/Utils/Local.h" 37 #include "llvm/Transforms/Utils/SSAUpdater.h" 38 39 using namespace llvm; 40 41 #define DEBUG_TYPE "winehprepare" 42 43 static cl::opt<bool> DisableDemotion( 44 "disable-demotion", cl::Hidden, 45 cl::desc( 46 "Clone multicolor basic blocks but do not demote cross scopes"), 47 cl::init(false)); 48 49 static cl::opt<bool> DisableCleanups( 50 "disable-cleanups", cl::Hidden, 51 cl::desc("Do not remove implausible terminators or other similar cleanups"), 52 cl::init(false)); 53 54 static cl::opt<bool> DemoteCatchSwitchPHIOnlyOpt( 55 "demote-catchswitch-only", cl::Hidden, 56 cl::desc("Demote catchswitch BBs only (for wasm EH)"), cl::init(false)); 57 58 namespace { 59 60 class WinEHPrepare : public FunctionPass { 61 public: 62 static char ID; // Pass identification, replacement for typeid. 63 WinEHPrepare(bool DemoteCatchSwitchPHIOnly = false) 64 : FunctionPass(ID), DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly) {} 65 66 bool runOnFunction(Function &Fn) override; 67 68 bool doFinalization(Module &M) override; 69 70 void getAnalysisUsage(AnalysisUsage &AU) const override; 71 72 StringRef getPassName() const override { 73 return "Windows exception handling preparation"; 74 } 75 76 private: 77 void insertPHIStores(PHINode *OriginalPHI, AllocaInst *SpillSlot); 78 void 79 insertPHIStore(BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot, 80 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist); 81 AllocaInst *insertPHILoads(PHINode *PN, Function &F); 82 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot, 83 DenseMap<BasicBlock *, Value *> &Loads, Function &F); 84 bool prepareExplicitEH(Function &F); 85 void colorFunclets(Function &F); 86 87 void demotePHIsOnFunclets(Function &F, bool DemoteCatchSwitchPHIOnly); 88 void cloneCommonBlocks(Function &F); 89 void removeImplausibleInstructions(Function &F); 90 void cleanupPreparedFunclets(Function &F); 91 void verifyPreparedFunclets(Function &F); 92 93 bool DemoteCatchSwitchPHIOnly; 94 95 // All fields are reset by runOnFunction. 96 EHPersonality Personality = EHPersonality::Unknown; 97 98 const DataLayout *DL = nullptr; 99 DenseMap<BasicBlock *, ColorVector> BlockColors; 100 MapVector<BasicBlock *, std::vector<BasicBlock *>> FuncletBlocks; 101 }; 102 103 } // end anonymous namespace 104 105 char WinEHPrepare::ID = 0; 106 INITIALIZE_PASS(WinEHPrepare, DEBUG_TYPE, "Prepare Windows exceptions", 107 false, false) 108 109 FunctionPass *llvm::createWinEHPass(bool DemoteCatchSwitchPHIOnly) { 110 return new WinEHPrepare(DemoteCatchSwitchPHIOnly); 111 } 112 113 bool WinEHPrepare::runOnFunction(Function &Fn) { 114 if (!Fn.hasPersonalityFn()) 115 return false; 116 117 // Classify the personality to see what kind of preparation we need. 118 Personality = classifyEHPersonality(Fn.getPersonalityFn()); 119 120 // Do nothing if this is not a scope-based personality. 121 if (!isScopedEHPersonality(Personality)) 122 return false; 123 124 DL = &Fn.getParent()->getDataLayout(); 125 return prepareExplicitEH(Fn); 126 } 127 128 bool WinEHPrepare::doFinalization(Module &M) { return false; } 129 130 void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {} 131 132 static int addUnwindMapEntry(WinEHFuncInfo &FuncInfo, int ToState, 133 const BasicBlock *BB) { 134 CxxUnwindMapEntry UME; 135 UME.ToState = ToState; 136 UME.Cleanup = BB; 137 FuncInfo.CxxUnwindMap.push_back(UME); 138 return FuncInfo.getLastStateNumber(); 139 } 140 141 static void addTryBlockMapEntry(WinEHFuncInfo &FuncInfo, int TryLow, 142 int TryHigh, int CatchHigh, 143 ArrayRef<const CatchPadInst *> Handlers) { 144 WinEHTryBlockMapEntry TBME; 145 TBME.TryLow = TryLow; 146 TBME.TryHigh = TryHigh; 147 TBME.CatchHigh = CatchHigh; 148 assert(TBME.TryLow <= TBME.TryHigh); 149 for (const CatchPadInst *CPI : Handlers) { 150 WinEHHandlerType HT; 151 Constant *TypeInfo = cast<Constant>(CPI->getArgOperand(0)); 152 if (TypeInfo->isNullValue()) 153 HT.TypeDescriptor = nullptr; 154 else 155 HT.TypeDescriptor = cast<GlobalVariable>(TypeInfo->stripPointerCasts()); 156 HT.Adjectives = cast<ConstantInt>(CPI->getArgOperand(1))->getZExtValue(); 157 HT.Handler = CPI->getParent(); 158 if (auto *AI = 159 dyn_cast<AllocaInst>(CPI->getArgOperand(2)->stripPointerCasts())) 160 HT.CatchObj.Alloca = AI; 161 else 162 HT.CatchObj.Alloca = nullptr; 163 TBME.HandlerArray.push_back(HT); 164 } 165 FuncInfo.TryBlockMap.push_back(TBME); 166 } 167 168 static BasicBlock *getCleanupRetUnwindDest(const CleanupPadInst *CleanupPad) { 169 for (const User *U : CleanupPad->users()) 170 if (const auto *CRI = dyn_cast<CleanupReturnInst>(U)) 171 return CRI->getUnwindDest(); 172 return nullptr; 173 } 174 175 static void calculateStateNumbersForInvokes(const Function *Fn, 176 WinEHFuncInfo &FuncInfo) { 177 auto *F = const_cast<Function *>(Fn); 178 DenseMap<BasicBlock *, ColorVector> BlockColors = colorEHFunclets(*F); 179 for (BasicBlock &BB : *F) { 180 auto *II = dyn_cast<InvokeInst>(BB.getTerminator()); 181 if (!II) 182 continue; 183 184 auto &BBColors = BlockColors[&BB]; 185 assert(BBColors.size() == 1 && "multi-color BB not removed by preparation"); 186 BasicBlock *FuncletEntryBB = BBColors.front(); 187 188 BasicBlock *FuncletUnwindDest; 189 auto *FuncletPad = 190 dyn_cast<FuncletPadInst>(FuncletEntryBB->getFirstNonPHI()); 191 assert(FuncletPad || FuncletEntryBB == &Fn->getEntryBlock()); 192 if (!FuncletPad) 193 FuncletUnwindDest = nullptr; 194 else if (auto *CatchPad = dyn_cast<CatchPadInst>(FuncletPad)) 195 FuncletUnwindDest = CatchPad->getCatchSwitch()->getUnwindDest(); 196 else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(FuncletPad)) 197 FuncletUnwindDest = getCleanupRetUnwindDest(CleanupPad); 198 else 199 llvm_unreachable("unexpected funclet pad!"); 200 201 BasicBlock *InvokeUnwindDest = II->getUnwindDest(); 202 int BaseState = -1; 203 if (FuncletUnwindDest == InvokeUnwindDest) { 204 auto BaseStateI = FuncInfo.FuncletBaseStateMap.find(FuncletPad); 205 if (BaseStateI != FuncInfo.FuncletBaseStateMap.end()) 206 BaseState = BaseStateI->second; 207 } 208 209 if (BaseState != -1) { 210 FuncInfo.InvokeStateMap[II] = BaseState; 211 } else { 212 Instruction *PadInst = InvokeUnwindDest->getFirstNonPHI(); 213 assert(FuncInfo.EHPadStateMap.count(PadInst) && "EH Pad has no state!"); 214 FuncInfo.InvokeStateMap[II] = FuncInfo.EHPadStateMap[PadInst]; 215 } 216 } 217 } 218 219 // See comments below for calculateSEHStateForAsynchEH(). 220 // State - incoming State of normal paths 221 struct WorkItem { 222 const BasicBlock *Block; 223 int State; 224 WorkItem(const BasicBlock *BB, int St) { 225 Block = BB; 226 State = St; 227 } 228 }; 229 void llvm::calculateCXXStateForAsynchEH(const BasicBlock *BB, int State, 230 WinEHFuncInfo &EHInfo) { 231 SmallVector<struct WorkItem *, 8> WorkList; 232 struct WorkItem *WI = new WorkItem(BB, State); 233 WorkList.push_back(WI); 234 235 while (!WorkList.empty()) { 236 WI = WorkList.pop_back_val(); 237 const BasicBlock *BB = WI->Block; 238 int State = WI->State; 239 delete WI; 240 if (EHInfo.BlockToStateMap.count(BB) && EHInfo.BlockToStateMap[BB] <= State) 241 continue; // skip blocks already visited by lower State 242 243 const llvm::Instruction *I = BB->getFirstNonPHI(); 244 const llvm::Instruction *TI = BB->getTerminator(); 245 if (I->isEHPad()) 246 State = EHInfo.EHPadStateMap[I]; 247 EHInfo.BlockToStateMap[BB] = State; // Record state, also flag visiting 248 249 if ((isa<CleanupReturnInst>(TI) || isa<CatchReturnInst>(TI)) && State > 0) { 250 // Retrive the new State 251 State = EHInfo.CxxUnwindMap[State].ToState; // Retrive next State 252 } else if (isa<InvokeInst>(TI)) { 253 auto *Call = cast<CallBase>(TI); 254 const Function *Fn = Call->getCalledFunction(); 255 if (Fn && Fn->isIntrinsic() && 256 (Fn->getIntrinsicID() == Intrinsic::seh_scope_begin || 257 Fn->getIntrinsicID() == Intrinsic::seh_try_begin)) 258 // Retrive the new State from seh_scope_begin 259 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)]; 260 else if (Fn && Fn->isIntrinsic() && 261 (Fn->getIntrinsicID() == Intrinsic::seh_scope_end || 262 Fn->getIntrinsicID() == Intrinsic::seh_try_end)) { 263 // In case of conditional ctor, let's retrieve State from Invoke 264 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)]; 265 // end of current state, retrive new state from UnwindMap 266 State = EHInfo.CxxUnwindMap[State].ToState; 267 } 268 } 269 // Continue push successors into worklist 270 for (auto *SuccBB : successors(BB)) { 271 WI = new WorkItem(SuccBB, State); 272 WorkList.push_back(WI); 273 } 274 } 275 } 276 277 // The central theory of this routine is based on the following: 278 // A _try scope is always a SEME (Single Entry Multiple Exits) region 279 // as jumping into a _try is not allowed 280 // The single entry must start with a seh_try_begin() invoke with a 281 // correct State number that is the initial state of the SEME. 282 // Through control-flow, state number is propagated into all blocks. 283 // Side exits marked by seh_try_end() will unwind to parent state via 284 // existing SEHUnwindMap[]. 285 // Side exits can ONLY jump into parent scopes (lower state number). 286 // Thus, when a block succeeds various states from its predecessors, 287 // the lowest State trumphs others. 288 // If some exits flow to unreachable, propagation on those paths terminate, 289 // not affecting remaining blocks. 290 void llvm::calculateSEHStateForAsynchEH(const BasicBlock *BB, int State, 291 WinEHFuncInfo &EHInfo) { 292 SmallVector<struct WorkItem *, 8> WorkList; 293 struct WorkItem *WI = new WorkItem(BB, State); 294 WorkList.push_back(WI); 295 296 while (!WorkList.empty()) { 297 WI = WorkList.pop_back_val(); 298 const BasicBlock *BB = WI->Block; 299 int State = WI->State; 300 delete WI; 301 if (EHInfo.BlockToStateMap.count(BB) && EHInfo.BlockToStateMap[BB] <= State) 302 continue; // skip blocks already visited by lower State 303 304 const llvm::Instruction *I = BB->getFirstNonPHI(); 305 const llvm::Instruction *TI = BB->getTerminator(); 306 if (I->isEHPad()) 307 State = EHInfo.EHPadStateMap[I]; 308 EHInfo.BlockToStateMap[BB] = State; // Record state 309 310 if (isa<CatchPadInst>(I) && isa<CatchReturnInst>(TI)) { 311 const Constant *FilterOrNull = cast<Constant>( 312 cast<CatchPadInst>(I)->getArgOperand(0)->stripPointerCasts()); 313 const Function *Filter = dyn_cast<Function>(FilterOrNull); 314 if (!Filter || !Filter->getName().startswith("__IsLocalUnwind")) 315 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State 316 } else if ((isa<CleanupReturnInst>(TI) || isa<CatchReturnInst>(TI)) && 317 State > 0) { 318 // Retrive the new State. 319 State = EHInfo.SEHUnwindMap[State].ToState; // Retrive next State 320 } else if (isa<InvokeInst>(TI)) { 321 auto *Call = cast<CallBase>(TI); 322 const Function *Fn = Call->getCalledFunction(); 323 if (Fn && Fn->isIntrinsic() && 324 Fn->getIntrinsicID() == Intrinsic::seh_try_begin) 325 // Retrive the new State from seh_try_begin 326 State = EHInfo.InvokeStateMap[cast<InvokeInst>(TI)]; 327 else if (Fn && Fn->isIntrinsic() && 328 Fn->getIntrinsicID() == Intrinsic::seh_try_end) 329 // end of current state, retrive new state from UnwindMap 330 State = EHInfo.SEHUnwindMap[State].ToState; 331 } 332 // Continue push successors into worklist 333 for (auto *SuccBB : successors(BB)) { 334 WI = new WorkItem(SuccBB, State); 335 WorkList.push_back(WI); 336 } 337 } 338 } 339 340 // Given BB which ends in an unwind edge, return the EHPad that this BB belongs 341 // to. If the unwind edge came from an invoke, return null. 342 static const BasicBlock *getEHPadFromPredecessor(const BasicBlock *BB, 343 Value *ParentPad) { 344 const Instruction *TI = BB->getTerminator(); 345 if (isa<InvokeInst>(TI)) 346 return nullptr; 347 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(TI)) { 348 if (CatchSwitch->getParentPad() != ParentPad) 349 return nullptr; 350 return BB; 351 } 352 assert(!TI->isEHPad() && "unexpected EHPad!"); 353 auto *CleanupPad = cast<CleanupReturnInst>(TI)->getCleanupPad(); 354 if (CleanupPad->getParentPad() != ParentPad) 355 return nullptr; 356 return CleanupPad->getParent(); 357 } 358 359 // Starting from a EHPad, Backward walk through control-flow graph 360 // to produce two primary outputs: 361 // FuncInfo.EHPadStateMap[] and FuncInfo.CxxUnwindMap[] 362 static void calculateCXXStateNumbers(WinEHFuncInfo &FuncInfo, 363 const Instruction *FirstNonPHI, 364 int ParentState) { 365 const BasicBlock *BB = FirstNonPHI->getParent(); 366 assert(BB->isEHPad() && "not a funclet!"); 367 368 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) { 369 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && 370 "shouldn't revist catch funclets!"); 371 372 SmallVector<const CatchPadInst *, 2> Handlers; 373 for (const BasicBlock *CatchPadBB : CatchSwitch->handlers()) { 374 auto *CatchPad = cast<CatchPadInst>(CatchPadBB->getFirstNonPHI()); 375 Handlers.push_back(CatchPad); 376 } 377 int TryLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr); 378 FuncInfo.EHPadStateMap[CatchSwitch] = TryLow; 379 for (const BasicBlock *PredBlock : predecessors(BB)) 380 if ((PredBlock = getEHPadFromPredecessor(PredBlock, 381 CatchSwitch->getParentPad()))) 382 calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), 383 TryLow); 384 int CatchLow = addUnwindMapEntry(FuncInfo, ParentState, nullptr); 385 386 // catchpads are separate funclets in C++ EH due to the way rethrow works. 387 int TryHigh = CatchLow - 1; 388 389 // MSVC FrameHandler3/4 on x64&Arm64 expect Catch Handlers in $tryMap$ 390 // stored in pre-order (outer first, inner next), not post-order 391 // Add to map here. Fix the CatchHigh after children are processed 392 const Module *Mod = BB->getParent()->getParent(); 393 bool IsPreOrder = Triple(Mod->getTargetTriple()).isArch64Bit(); 394 if (IsPreOrder) 395 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchLow, Handlers); 396 unsigned TBMEIdx = FuncInfo.TryBlockMap.size() - 1; 397 398 for (const auto *CatchPad : Handlers) { 399 FuncInfo.FuncletBaseStateMap[CatchPad] = CatchLow; 400 FuncInfo.EHPadStateMap[CatchPad] = CatchLow; 401 for (const User *U : CatchPad->users()) { 402 const auto *UserI = cast<Instruction>(U); 403 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) { 404 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest(); 405 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest()) 406 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow); 407 } 408 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) { 409 BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad); 410 // If a nested cleanup pad reports a null unwind destination and the 411 // enclosing catch pad doesn't it must be post-dominated by an 412 // unreachable instruction. 413 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest()) 414 calculateCXXStateNumbers(FuncInfo, UserI, CatchLow); 415 } 416 } 417 } 418 int CatchHigh = FuncInfo.getLastStateNumber(); 419 // Now child Catches are processed, update CatchHigh 420 if (IsPreOrder) 421 FuncInfo.TryBlockMap[TBMEIdx].CatchHigh = CatchHigh; 422 else // PostOrder 423 addTryBlockMapEntry(FuncInfo, TryLow, TryHigh, CatchHigh, Handlers); 424 425 LLVM_DEBUG(dbgs() << "TryLow[" << BB->getName() << "]: " << TryLow << '\n'); 426 LLVM_DEBUG(dbgs() << "TryHigh[" << BB->getName() << "]: " << TryHigh 427 << '\n'); 428 LLVM_DEBUG(dbgs() << "CatchHigh[" << BB->getName() << "]: " << CatchHigh 429 << '\n'); 430 } else { 431 auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI); 432 433 // It's possible for a cleanup to be visited twice: it might have multiple 434 // cleanupret instructions. 435 if (FuncInfo.EHPadStateMap.count(CleanupPad)) 436 return; 437 438 int CleanupState = addUnwindMapEntry(FuncInfo, ParentState, BB); 439 FuncInfo.EHPadStateMap[CleanupPad] = CleanupState; 440 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB " 441 << BB->getName() << '\n'); 442 for (const BasicBlock *PredBlock : predecessors(BB)) { 443 if ((PredBlock = getEHPadFromPredecessor(PredBlock, 444 CleanupPad->getParentPad()))) { 445 calculateCXXStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), 446 CleanupState); 447 } 448 } 449 for (const User *U : CleanupPad->users()) { 450 const auto *UserI = cast<Instruction>(U); 451 if (UserI->isEHPad()) 452 report_fatal_error("Cleanup funclets for the MSVC++ personality cannot " 453 "contain exceptional actions"); 454 } 455 } 456 } 457 458 static int addSEHExcept(WinEHFuncInfo &FuncInfo, int ParentState, 459 const Function *Filter, const BasicBlock *Handler) { 460 SEHUnwindMapEntry Entry; 461 Entry.ToState = ParentState; 462 Entry.IsFinally = false; 463 Entry.Filter = Filter; 464 Entry.Handler = Handler; 465 FuncInfo.SEHUnwindMap.push_back(Entry); 466 return FuncInfo.SEHUnwindMap.size() - 1; 467 } 468 469 static int addSEHFinally(WinEHFuncInfo &FuncInfo, int ParentState, 470 const BasicBlock *Handler) { 471 SEHUnwindMapEntry Entry; 472 Entry.ToState = ParentState; 473 Entry.IsFinally = true; 474 Entry.Filter = nullptr; 475 Entry.Handler = Handler; 476 FuncInfo.SEHUnwindMap.push_back(Entry); 477 return FuncInfo.SEHUnwindMap.size() - 1; 478 } 479 480 // Starting from a EHPad, Backward walk through control-flow graph 481 // to produce two primary outputs: 482 // FuncInfo.EHPadStateMap[] and FuncInfo.SEHUnwindMap[] 483 static void calculateSEHStateNumbers(WinEHFuncInfo &FuncInfo, 484 const Instruction *FirstNonPHI, 485 int ParentState) { 486 const BasicBlock *BB = FirstNonPHI->getParent(); 487 assert(BB->isEHPad() && "no a funclet!"); 488 489 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(FirstNonPHI)) { 490 assert(FuncInfo.EHPadStateMap.count(CatchSwitch) == 0 && 491 "shouldn't revist catch funclets!"); 492 493 // Extract the filter function and the __except basic block and create a 494 // state for them. 495 assert(CatchSwitch->getNumHandlers() == 1 && 496 "SEH doesn't have multiple handlers per __try"); 497 const auto *CatchPad = 498 cast<CatchPadInst>((*CatchSwitch->handler_begin())->getFirstNonPHI()); 499 const BasicBlock *CatchPadBB = CatchPad->getParent(); 500 const Constant *FilterOrNull = 501 cast<Constant>(CatchPad->getArgOperand(0)->stripPointerCasts()); 502 const Function *Filter = dyn_cast<Function>(FilterOrNull); 503 assert((Filter || FilterOrNull->isNullValue()) && 504 "unexpected filter value"); 505 int TryState = addSEHExcept(FuncInfo, ParentState, Filter, CatchPadBB); 506 507 // Everything in the __try block uses TryState as its parent state. 508 FuncInfo.EHPadStateMap[CatchSwitch] = TryState; 509 FuncInfo.EHPadStateMap[CatchPad] = TryState; 510 LLVM_DEBUG(dbgs() << "Assigning state #" << TryState << " to BB " 511 << CatchPadBB->getName() << '\n'); 512 for (const BasicBlock *PredBlock : predecessors(BB)) 513 if ((PredBlock = getEHPadFromPredecessor(PredBlock, 514 CatchSwitch->getParentPad()))) 515 calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), 516 TryState); 517 518 // Everything in the __except block unwinds to ParentState, just like code 519 // outside the __try. 520 for (const User *U : CatchPad->users()) { 521 const auto *UserI = cast<Instruction>(U); 522 if (auto *InnerCatchSwitch = dyn_cast<CatchSwitchInst>(UserI)) { 523 BasicBlock *UnwindDest = InnerCatchSwitch->getUnwindDest(); 524 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest()) 525 calculateSEHStateNumbers(FuncInfo, UserI, ParentState); 526 } 527 if (auto *InnerCleanupPad = dyn_cast<CleanupPadInst>(UserI)) { 528 BasicBlock *UnwindDest = getCleanupRetUnwindDest(InnerCleanupPad); 529 // If a nested cleanup pad reports a null unwind destination and the 530 // enclosing catch pad doesn't it must be post-dominated by an 531 // unreachable instruction. 532 if (!UnwindDest || UnwindDest == CatchSwitch->getUnwindDest()) 533 calculateSEHStateNumbers(FuncInfo, UserI, ParentState); 534 } 535 } 536 } else { 537 auto *CleanupPad = cast<CleanupPadInst>(FirstNonPHI); 538 539 // It's possible for a cleanup to be visited twice: it might have multiple 540 // cleanupret instructions. 541 if (FuncInfo.EHPadStateMap.count(CleanupPad)) 542 return; 543 544 int CleanupState = addSEHFinally(FuncInfo, ParentState, BB); 545 FuncInfo.EHPadStateMap[CleanupPad] = CleanupState; 546 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState << " to BB " 547 << BB->getName() << '\n'); 548 for (const BasicBlock *PredBlock : predecessors(BB)) 549 if ((PredBlock = 550 getEHPadFromPredecessor(PredBlock, CleanupPad->getParentPad()))) 551 calculateSEHStateNumbers(FuncInfo, PredBlock->getFirstNonPHI(), 552 CleanupState); 553 for (const User *U : CleanupPad->users()) { 554 const auto *UserI = cast<Instruction>(U); 555 if (UserI->isEHPad()) 556 report_fatal_error("Cleanup funclets for the SEH personality cannot " 557 "contain exceptional actions"); 558 } 559 } 560 } 561 562 static bool isTopLevelPadForMSVC(const Instruction *EHPad) { 563 if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(EHPad)) 564 return isa<ConstantTokenNone>(CatchSwitch->getParentPad()) && 565 CatchSwitch->unwindsToCaller(); 566 if (auto *CleanupPad = dyn_cast<CleanupPadInst>(EHPad)) 567 return isa<ConstantTokenNone>(CleanupPad->getParentPad()) && 568 getCleanupRetUnwindDest(CleanupPad) == nullptr; 569 if (isa<CatchPadInst>(EHPad)) 570 return false; 571 llvm_unreachable("unexpected EHPad!"); 572 } 573 574 void llvm::calculateSEHStateNumbers(const Function *Fn, 575 WinEHFuncInfo &FuncInfo) { 576 // Don't compute state numbers twice. 577 if (!FuncInfo.SEHUnwindMap.empty()) 578 return; 579 580 for (const BasicBlock &BB : *Fn) { 581 if (!BB.isEHPad()) 582 continue; 583 const Instruction *FirstNonPHI = BB.getFirstNonPHI(); 584 if (!isTopLevelPadForMSVC(FirstNonPHI)) 585 continue; 586 ::calculateSEHStateNumbers(FuncInfo, FirstNonPHI, -1); 587 } 588 589 calculateStateNumbersForInvokes(Fn, FuncInfo); 590 591 bool IsEHa = Fn->getParent()->getModuleFlag("eh-asynch"); 592 if (IsEHa) { 593 const BasicBlock *EntryBB = &(Fn->getEntryBlock()); 594 calculateSEHStateForAsynchEH(EntryBB, -1, FuncInfo); 595 } 596 } 597 598 void llvm::calculateWinCXXEHStateNumbers(const Function *Fn, 599 WinEHFuncInfo &FuncInfo) { 600 // Return if it's already been done. 601 if (!FuncInfo.EHPadStateMap.empty()) 602 return; 603 604 for (const BasicBlock &BB : *Fn) { 605 if (!BB.isEHPad()) 606 continue; 607 const Instruction *FirstNonPHI = BB.getFirstNonPHI(); 608 if (!isTopLevelPadForMSVC(FirstNonPHI)) 609 continue; 610 calculateCXXStateNumbers(FuncInfo, FirstNonPHI, -1); 611 } 612 613 calculateStateNumbersForInvokes(Fn, FuncInfo); 614 615 bool IsEHa = Fn->getParent()->getModuleFlag("eh-asynch"); 616 if (IsEHa) { 617 const BasicBlock *EntryBB = &(Fn->getEntryBlock()); 618 calculateCXXStateForAsynchEH(EntryBB, -1, FuncInfo); 619 } 620 } 621 622 static int addClrEHHandler(WinEHFuncInfo &FuncInfo, int HandlerParentState, 623 int TryParentState, ClrHandlerType HandlerType, 624 uint32_t TypeToken, const BasicBlock *Handler) { 625 ClrEHUnwindMapEntry Entry; 626 Entry.HandlerParentState = HandlerParentState; 627 Entry.TryParentState = TryParentState; 628 Entry.Handler = Handler; 629 Entry.HandlerType = HandlerType; 630 Entry.TypeToken = TypeToken; 631 FuncInfo.ClrEHUnwindMap.push_back(Entry); 632 return FuncInfo.ClrEHUnwindMap.size() - 1; 633 } 634 635 void llvm::calculateClrEHStateNumbers(const Function *Fn, 636 WinEHFuncInfo &FuncInfo) { 637 // Return if it's already been done. 638 if (!FuncInfo.EHPadStateMap.empty()) 639 return; 640 641 // This numbering assigns one state number to each catchpad and cleanuppad. 642 // It also computes two tree-like relations over states: 643 // 1) Each state has a "HandlerParentState", which is the state of the next 644 // outer handler enclosing this state's handler (same as nearest ancestor 645 // per the ParentPad linkage on EH pads, but skipping over catchswitches). 646 // 2) Each state has a "TryParentState", which: 647 // a) for a catchpad that's not the last handler on its catchswitch, is 648 // the state of the next catchpad on that catchswitch 649 // b) for all other pads, is the state of the pad whose try region is the 650 // next outer try region enclosing this state's try region. The "try 651 // regions are not present as such in the IR, but will be inferred 652 // based on the placement of invokes and pads which reach each other 653 // by exceptional exits 654 // Catchswitches do not get their own states, but each gets mapped to the 655 // state of its first catchpad. 656 657 // Step one: walk down from outermost to innermost funclets, assigning each 658 // catchpad and cleanuppad a state number. Add an entry to the 659 // ClrEHUnwindMap for each state, recording its HandlerParentState and 660 // handler attributes. Record the TryParentState as well for each catchpad 661 // that's not the last on its catchswitch, but initialize all other entries' 662 // TryParentStates to a sentinel -1 value that the next pass will update. 663 664 // Seed a worklist with pads that have no parent. 665 SmallVector<std::pair<const Instruction *, int>, 8> Worklist; 666 for (const BasicBlock &BB : *Fn) { 667 const Instruction *FirstNonPHI = BB.getFirstNonPHI(); 668 const Value *ParentPad; 669 if (const auto *CPI = dyn_cast<CleanupPadInst>(FirstNonPHI)) 670 ParentPad = CPI->getParentPad(); 671 else if (const auto *CSI = dyn_cast<CatchSwitchInst>(FirstNonPHI)) 672 ParentPad = CSI->getParentPad(); 673 else 674 continue; 675 if (isa<ConstantTokenNone>(ParentPad)) 676 Worklist.emplace_back(FirstNonPHI, -1); 677 } 678 679 // Use the worklist to visit all pads, from outer to inner. Record 680 // HandlerParentState for all pads. Record TryParentState only for catchpads 681 // that aren't the last on their catchswitch (setting all other entries' 682 // TryParentStates to an initial value of -1). This loop is also responsible 683 // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and 684 // catchswitches. 685 while (!Worklist.empty()) { 686 const Instruction *Pad; 687 int HandlerParentState; 688 std::tie(Pad, HandlerParentState) = Worklist.pop_back_val(); 689 690 if (const auto *Cleanup = dyn_cast<CleanupPadInst>(Pad)) { 691 // Create the entry for this cleanup with the appropriate handler 692 // properties. Finally and fault handlers are distinguished by arity. 693 ClrHandlerType HandlerType = 694 (Cleanup->arg_size() ? ClrHandlerType::Fault 695 : ClrHandlerType::Finally); 696 int CleanupState = addClrEHHandler(FuncInfo, HandlerParentState, -1, 697 HandlerType, 0, Pad->getParent()); 698 // Queue any child EH pads on the worklist. 699 for (const User *U : Cleanup->users()) 700 if (const auto *I = dyn_cast<Instruction>(U)) 701 if (I->isEHPad()) 702 Worklist.emplace_back(I, CleanupState); 703 // Remember this pad's state. 704 FuncInfo.EHPadStateMap[Cleanup] = CleanupState; 705 } else { 706 // Walk the handlers of this catchswitch in reverse order since all but 707 // the last need to set the following one as its TryParentState. 708 const auto *CatchSwitch = cast<CatchSwitchInst>(Pad); 709 int CatchState = -1, FollowerState = -1; 710 SmallVector<const BasicBlock *, 4> CatchBlocks(CatchSwitch->handlers()); 711 for (const BasicBlock *CatchBlock : llvm::reverse(CatchBlocks)) { 712 // Create the entry for this catch with the appropriate handler 713 // properties. 714 const auto *Catch = cast<CatchPadInst>(CatchBlock->getFirstNonPHI()); 715 uint32_t TypeToken = static_cast<uint32_t>( 716 cast<ConstantInt>(Catch->getArgOperand(0))->getZExtValue()); 717 CatchState = 718 addClrEHHandler(FuncInfo, HandlerParentState, FollowerState, 719 ClrHandlerType::Catch, TypeToken, CatchBlock); 720 // Queue any child EH pads on the worklist. 721 for (const User *U : Catch->users()) 722 if (const auto *I = dyn_cast<Instruction>(U)) 723 if (I->isEHPad()) 724 Worklist.emplace_back(I, CatchState); 725 // Remember this catch's state. 726 FuncInfo.EHPadStateMap[Catch] = CatchState; 727 FollowerState = CatchState; 728 } 729 // Associate the catchswitch with the state of its first catch. 730 assert(CatchSwitch->getNumHandlers()); 731 FuncInfo.EHPadStateMap[CatchSwitch] = CatchState; 732 } 733 } 734 735 // Step two: record the TryParentState of each state. For cleanuppads that 736 // don't have cleanuprets, we may need to infer this from their child pads, 737 // so visit pads in descendant-most to ancestor-most order. 738 for (ClrEHUnwindMapEntry &Entry : llvm::reverse(FuncInfo.ClrEHUnwindMap)) { 739 const Instruction *Pad = 740 cast<const BasicBlock *>(Entry.Handler)->getFirstNonPHI(); 741 // For most pads, the TryParentState is the state associated with the 742 // unwind dest of exceptional exits from it. 743 const BasicBlock *UnwindDest; 744 if (const auto *Catch = dyn_cast<CatchPadInst>(Pad)) { 745 // If a catch is not the last in its catchswitch, its TryParentState is 746 // the state associated with the next catch in the switch, even though 747 // that's not the unwind dest of exceptions escaping the catch. Those 748 // cases were already assigned a TryParentState in the first pass, so 749 // skip them. 750 if (Entry.TryParentState != -1) 751 continue; 752 // Otherwise, get the unwind dest from the catchswitch. 753 UnwindDest = Catch->getCatchSwitch()->getUnwindDest(); 754 } else { 755 const auto *Cleanup = cast<CleanupPadInst>(Pad); 756 UnwindDest = nullptr; 757 for (const User *U : Cleanup->users()) { 758 if (auto *CleanupRet = dyn_cast<CleanupReturnInst>(U)) { 759 // Common and unambiguous case -- cleanupret indicates cleanup's 760 // unwind dest. 761 UnwindDest = CleanupRet->getUnwindDest(); 762 break; 763 } 764 765 // Get an unwind dest for the user 766 const BasicBlock *UserUnwindDest = nullptr; 767 if (auto *Invoke = dyn_cast<InvokeInst>(U)) { 768 UserUnwindDest = Invoke->getUnwindDest(); 769 } else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(U)) { 770 UserUnwindDest = CatchSwitch->getUnwindDest(); 771 } else if (auto *ChildCleanup = dyn_cast<CleanupPadInst>(U)) { 772 int UserState = FuncInfo.EHPadStateMap[ChildCleanup]; 773 int UserUnwindState = 774 FuncInfo.ClrEHUnwindMap[UserState].TryParentState; 775 if (UserUnwindState != -1) 776 UserUnwindDest = cast<const BasicBlock *>( 777 FuncInfo.ClrEHUnwindMap[UserUnwindState].Handler); 778 } 779 780 // Not having an unwind dest for this user might indicate that it 781 // doesn't unwind, so can't be taken as proof that the cleanup itself 782 // may unwind to caller (see e.g. SimplifyUnreachable and 783 // RemoveUnwindEdge). 784 if (!UserUnwindDest) 785 continue; 786 787 // Now we have an unwind dest for the user, but we need to see if it 788 // unwinds all the way out of the cleanup or if it stays within it. 789 const Instruction *UserUnwindPad = UserUnwindDest->getFirstNonPHI(); 790 const Value *UserUnwindParent; 791 if (auto *CSI = dyn_cast<CatchSwitchInst>(UserUnwindPad)) 792 UserUnwindParent = CSI->getParentPad(); 793 else 794 UserUnwindParent = 795 cast<CleanupPadInst>(UserUnwindPad)->getParentPad(); 796 797 // The unwind stays within the cleanup iff it targets a child of the 798 // cleanup. 799 if (UserUnwindParent == Cleanup) 800 continue; 801 802 // This unwind exits the cleanup, so its dest is the cleanup's dest. 803 UnwindDest = UserUnwindDest; 804 break; 805 } 806 } 807 808 // Record the state of the unwind dest as the TryParentState. 809 int UnwindDestState; 810 811 // If UnwindDest is null at this point, either the pad in question can 812 // be exited by unwind to caller, or it cannot be exited by unwind. In 813 // either case, reporting such cases as unwinding to caller is correct. 814 // This can lead to EH tables that "look strange" -- if this pad's is in 815 // a parent funclet which has other children that do unwind to an enclosing 816 // pad, the try region for this pad will be missing the "duplicate" EH 817 // clause entries that you'd expect to see covering the whole parent. That 818 // should be benign, since the unwind never actually happens. If it were 819 // an issue, we could add a subsequent pass that pushes unwind dests down 820 // from parents that have them to children that appear to unwind to caller. 821 if (!UnwindDest) { 822 UnwindDestState = -1; 823 } else { 824 UnwindDestState = FuncInfo.EHPadStateMap[UnwindDest->getFirstNonPHI()]; 825 } 826 827 Entry.TryParentState = UnwindDestState; 828 } 829 830 // Step three: transfer information from pads to invokes. 831 calculateStateNumbersForInvokes(Fn, FuncInfo); 832 } 833 834 void WinEHPrepare::colorFunclets(Function &F) { 835 BlockColors = colorEHFunclets(F); 836 837 // Invert the map from BB to colors to color to BBs. 838 for (BasicBlock &BB : F) { 839 ColorVector &Colors = BlockColors[&BB]; 840 for (BasicBlock *Color : Colors) 841 FuncletBlocks[Color].push_back(&BB); 842 } 843 } 844 845 void WinEHPrepare::demotePHIsOnFunclets(Function &F, 846 bool DemoteCatchSwitchPHIOnly) { 847 // Strip PHI nodes off of EH pads. 848 SmallVector<PHINode *, 16> PHINodes; 849 for (BasicBlock &BB : make_early_inc_range(F)) { 850 if (!BB.isEHPad()) 851 continue; 852 if (DemoteCatchSwitchPHIOnly && !isa<CatchSwitchInst>(BB.getFirstNonPHI())) 853 continue; 854 855 for (Instruction &I : make_early_inc_range(BB)) { 856 auto *PN = dyn_cast<PHINode>(&I); 857 // Stop at the first non-PHI. 858 if (!PN) 859 break; 860 861 AllocaInst *SpillSlot = insertPHILoads(PN, F); 862 if (SpillSlot) 863 insertPHIStores(PN, SpillSlot); 864 865 PHINodes.push_back(PN); 866 } 867 } 868 869 for (auto *PN : PHINodes) { 870 // There may be lingering uses on other EH PHIs being removed 871 PN->replaceAllUsesWith(PoisonValue::get(PN->getType())); 872 PN->eraseFromParent(); 873 } 874 } 875 876 void WinEHPrepare::cloneCommonBlocks(Function &F) { 877 // We need to clone all blocks which belong to multiple funclets. Values are 878 // remapped throughout the funclet to propagate both the new instructions 879 // *and* the new basic blocks themselves. 880 for (auto &Funclets : FuncletBlocks) { 881 BasicBlock *FuncletPadBB = Funclets.first; 882 std::vector<BasicBlock *> &BlocksInFunclet = Funclets.second; 883 Value *FuncletToken; 884 if (FuncletPadBB == &F.getEntryBlock()) 885 FuncletToken = ConstantTokenNone::get(F.getContext()); 886 else 887 FuncletToken = FuncletPadBB->getFirstNonPHI(); 888 889 std::vector<std::pair<BasicBlock *, BasicBlock *>> Orig2Clone; 890 ValueToValueMapTy VMap; 891 for (BasicBlock *BB : BlocksInFunclet) { 892 ColorVector &ColorsForBB = BlockColors[BB]; 893 // We don't need to do anything if the block is monochromatic. 894 size_t NumColorsForBB = ColorsForBB.size(); 895 if (NumColorsForBB == 1) 896 continue; 897 898 DEBUG_WITH_TYPE("winehprepare-coloring", 899 dbgs() << " Cloning block \'" << BB->getName() 900 << "\' for funclet \'" << FuncletPadBB->getName() 901 << "\'.\n"); 902 903 // Create a new basic block and copy instructions into it! 904 BasicBlock *CBB = 905 CloneBasicBlock(BB, VMap, Twine(".for.", FuncletPadBB->getName())); 906 // Insert the clone immediately after the original to ensure determinism 907 // and to keep the same relative ordering of any funclet's blocks. 908 CBB->insertInto(&F, BB->getNextNode()); 909 910 // Add basic block mapping. 911 VMap[BB] = CBB; 912 913 // Record delta operations that we need to perform to our color mappings. 914 Orig2Clone.emplace_back(BB, CBB); 915 } 916 917 // If nothing was cloned, we're done cloning in this funclet. 918 if (Orig2Clone.empty()) 919 continue; 920 921 // Update our color mappings to reflect that one block has lost a color and 922 // another has gained a color. 923 for (auto &BBMapping : Orig2Clone) { 924 BasicBlock *OldBlock = BBMapping.first; 925 BasicBlock *NewBlock = BBMapping.second; 926 927 BlocksInFunclet.push_back(NewBlock); 928 ColorVector &NewColors = BlockColors[NewBlock]; 929 assert(NewColors.empty() && "A new block should only have one color!"); 930 NewColors.push_back(FuncletPadBB); 931 932 DEBUG_WITH_TYPE("winehprepare-coloring", 933 dbgs() << " Assigned color \'" << FuncletPadBB->getName() 934 << "\' to block \'" << NewBlock->getName() 935 << "\'.\n"); 936 937 llvm::erase_value(BlocksInFunclet, OldBlock); 938 ColorVector &OldColors = BlockColors[OldBlock]; 939 llvm::erase_value(OldColors, FuncletPadBB); 940 941 DEBUG_WITH_TYPE("winehprepare-coloring", 942 dbgs() << " Removed color \'" << FuncletPadBB->getName() 943 << "\' from block \'" << OldBlock->getName() 944 << "\'.\n"); 945 } 946 947 // Loop over all of the instructions in this funclet, fixing up operand 948 // references as we go. This uses VMap to do all the hard work. 949 for (BasicBlock *BB : BlocksInFunclet) 950 // Loop over all instructions, fixing each one as we find it... 951 for (Instruction &I : *BB) 952 RemapInstruction(&I, VMap, 953 RF_IgnoreMissingLocals | RF_NoModuleLevelChanges); 954 955 // Catchrets targeting cloned blocks need to be updated separately from 956 // the loop above because they are not in the current funclet. 957 SmallVector<CatchReturnInst *, 2> FixupCatchrets; 958 for (auto &BBMapping : Orig2Clone) { 959 BasicBlock *OldBlock = BBMapping.first; 960 BasicBlock *NewBlock = BBMapping.second; 961 962 FixupCatchrets.clear(); 963 for (BasicBlock *Pred : predecessors(OldBlock)) 964 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Pred->getTerminator())) 965 if (CatchRet->getCatchSwitchParentPad() == FuncletToken) 966 FixupCatchrets.push_back(CatchRet); 967 968 for (CatchReturnInst *CatchRet : FixupCatchrets) 969 CatchRet->setSuccessor(NewBlock); 970 } 971 972 auto UpdatePHIOnClonedBlock = [&](PHINode *PN, bool IsForOldBlock) { 973 unsigned NumPreds = PN->getNumIncomingValues(); 974 for (unsigned PredIdx = 0, PredEnd = NumPreds; PredIdx != PredEnd; 975 ++PredIdx) { 976 BasicBlock *IncomingBlock = PN->getIncomingBlock(PredIdx); 977 bool EdgeTargetsFunclet; 978 if (auto *CRI = 979 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) { 980 EdgeTargetsFunclet = (CRI->getCatchSwitchParentPad() == FuncletToken); 981 } else { 982 ColorVector &IncomingColors = BlockColors[IncomingBlock]; 983 assert(!IncomingColors.empty() && "Block not colored!"); 984 assert((IncomingColors.size() == 1 || 985 !llvm::is_contained(IncomingColors, FuncletPadBB)) && 986 "Cloning should leave this funclet's blocks monochromatic"); 987 EdgeTargetsFunclet = (IncomingColors.front() == FuncletPadBB); 988 } 989 if (IsForOldBlock != EdgeTargetsFunclet) 990 continue; 991 PN->removeIncomingValue(IncomingBlock, /*DeletePHIIfEmpty=*/false); 992 // Revisit the next entry. 993 --PredIdx; 994 --PredEnd; 995 } 996 }; 997 998 for (auto &BBMapping : Orig2Clone) { 999 BasicBlock *OldBlock = BBMapping.first; 1000 BasicBlock *NewBlock = BBMapping.second; 1001 for (PHINode &OldPN : OldBlock->phis()) { 1002 UpdatePHIOnClonedBlock(&OldPN, /*IsForOldBlock=*/true); 1003 } 1004 for (PHINode &NewPN : NewBlock->phis()) { 1005 UpdatePHIOnClonedBlock(&NewPN, /*IsForOldBlock=*/false); 1006 } 1007 } 1008 1009 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to 1010 // the PHI nodes for NewBB now. 1011 for (auto &BBMapping : Orig2Clone) { 1012 BasicBlock *OldBlock = BBMapping.first; 1013 BasicBlock *NewBlock = BBMapping.second; 1014 for (BasicBlock *SuccBB : successors(NewBlock)) { 1015 for (PHINode &SuccPN : SuccBB->phis()) { 1016 // Ok, we have a PHI node. Figure out what the incoming value was for 1017 // the OldBlock. 1018 int OldBlockIdx = SuccPN.getBasicBlockIndex(OldBlock); 1019 if (OldBlockIdx == -1) 1020 break; 1021 Value *IV = SuccPN.getIncomingValue(OldBlockIdx); 1022 1023 // Remap the value if necessary. 1024 if (auto *Inst = dyn_cast<Instruction>(IV)) { 1025 ValueToValueMapTy::iterator I = VMap.find(Inst); 1026 if (I != VMap.end()) 1027 IV = I->second; 1028 } 1029 1030 SuccPN.addIncoming(IV, NewBlock); 1031 } 1032 } 1033 } 1034 1035 for (ValueToValueMapTy::value_type VT : VMap) { 1036 // If there were values defined in BB that are used outside the funclet, 1037 // then we now have to update all uses of the value to use either the 1038 // original value, the cloned value, or some PHI derived value. This can 1039 // require arbitrary PHI insertion, of which we are prepared to do, clean 1040 // these up now. 1041 SmallVector<Use *, 16> UsesToRename; 1042 1043 auto *OldI = dyn_cast<Instruction>(const_cast<Value *>(VT.first)); 1044 if (!OldI) 1045 continue; 1046 auto *NewI = cast<Instruction>(VT.second); 1047 // Scan all uses of this instruction to see if it is used outside of its 1048 // funclet, and if so, record them in UsesToRename. 1049 for (Use &U : OldI->uses()) { 1050 Instruction *UserI = cast<Instruction>(U.getUser()); 1051 BasicBlock *UserBB = UserI->getParent(); 1052 ColorVector &ColorsForUserBB = BlockColors[UserBB]; 1053 assert(!ColorsForUserBB.empty()); 1054 if (ColorsForUserBB.size() > 1 || 1055 *ColorsForUserBB.begin() != FuncletPadBB) 1056 UsesToRename.push_back(&U); 1057 } 1058 1059 // If there are no uses outside the block, we're done with this 1060 // instruction. 1061 if (UsesToRename.empty()) 1062 continue; 1063 1064 // We found a use of OldI outside of the funclet. Rename all uses of OldI 1065 // that are outside its funclet to be uses of the appropriate PHI node 1066 // etc. 1067 SSAUpdater SSAUpdate; 1068 SSAUpdate.Initialize(OldI->getType(), OldI->getName()); 1069 SSAUpdate.AddAvailableValue(OldI->getParent(), OldI); 1070 SSAUpdate.AddAvailableValue(NewI->getParent(), NewI); 1071 1072 while (!UsesToRename.empty()) 1073 SSAUpdate.RewriteUseAfterInsertions(*UsesToRename.pop_back_val()); 1074 } 1075 } 1076 } 1077 1078 void WinEHPrepare::removeImplausibleInstructions(Function &F) { 1079 // Remove implausible terminators and replace them with UnreachableInst. 1080 for (auto &Funclet : FuncletBlocks) { 1081 BasicBlock *FuncletPadBB = Funclet.first; 1082 std::vector<BasicBlock *> &BlocksInFunclet = Funclet.second; 1083 Instruction *FirstNonPHI = FuncletPadBB->getFirstNonPHI(); 1084 auto *FuncletPad = dyn_cast<FuncletPadInst>(FirstNonPHI); 1085 auto *CatchPad = dyn_cast_or_null<CatchPadInst>(FuncletPad); 1086 auto *CleanupPad = dyn_cast_or_null<CleanupPadInst>(FuncletPad); 1087 1088 for (BasicBlock *BB : BlocksInFunclet) { 1089 for (Instruction &I : *BB) { 1090 auto *CB = dyn_cast<CallBase>(&I); 1091 if (!CB) 1092 continue; 1093 1094 Value *FuncletBundleOperand = nullptr; 1095 if (auto BU = CB->getOperandBundle(LLVMContext::OB_funclet)) 1096 FuncletBundleOperand = BU->Inputs.front(); 1097 1098 if (FuncletBundleOperand == FuncletPad) 1099 continue; 1100 1101 // Skip call sites which are nounwind intrinsics or inline asm. 1102 auto *CalledFn = 1103 dyn_cast<Function>(CB->getCalledOperand()->stripPointerCasts()); 1104 if (CalledFn && ((CalledFn->isIntrinsic() && CB->doesNotThrow()) || 1105 CB->isInlineAsm())) 1106 continue; 1107 1108 // This call site was not part of this funclet, remove it. 1109 if (isa<InvokeInst>(CB)) { 1110 // Remove the unwind edge if it was an invoke. 1111 removeUnwindEdge(BB); 1112 // Get a pointer to the new call. 1113 BasicBlock::iterator CallI = 1114 std::prev(BB->getTerminator()->getIterator()); 1115 auto *CI = cast<CallInst>(&*CallI); 1116 changeToUnreachable(CI); 1117 } else { 1118 changeToUnreachable(&I); 1119 } 1120 1121 // There are no more instructions in the block (except for unreachable), 1122 // we are done. 1123 break; 1124 } 1125 1126 Instruction *TI = BB->getTerminator(); 1127 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst. 1128 bool IsUnreachableRet = isa<ReturnInst>(TI) && FuncletPad; 1129 // The token consumed by a CatchReturnInst must match the funclet token. 1130 bool IsUnreachableCatchret = false; 1131 if (auto *CRI = dyn_cast<CatchReturnInst>(TI)) 1132 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad; 1133 // The token consumed by a CleanupReturnInst must match the funclet token. 1134 bool IsUnreachableCleanupret = false; 1135 if (auto *CRI = dyn_cast<CleanupReturnInst>(TI)) 1136 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad; 1137 if (IsUnreachableRet || IsUnreachableCatchret || 1138 IsUnreachableCleanupret) { 1139 changeToUnreachable(TI); 1140 } else if (isa<InvokeInst>(TI)) { 1141 if (Personality == EHPersonality::MSVC_CXX && CleanupPad) { 1142 // Invokes within a cleanuppad for the MSVC++ personality never 1143 // transfer control to their unwind edge: the personality will 1144 // terminate the program. 1145 removeUnwindEdge(BB); 1146 } 1147 } 1148 } 1149 } 1150 } 1151 1152 void WinEHPrepare::cleanupPreparedFunclets(Function &F) { 1153 // Clean-up some of the mess we made by removing useles PHI nodes, trivial 1154 // branches, etc. 1155 for (BasicBlock &BB : llvm::make_early_inc_range(F)) { 1156 SimplifyInstructionsInBlock(&BB); 1157 ConstantFoldTerminator(&BB, /*DeleteDeadConditions=*/true); 1158 MergeBlockIntoPredecessor(&BB); 1159 } 1160 1161 // We might have some unreachable blocks after cleaning up some impossible 1162 // control flow. 1163 removeUnreachableBlocks(F); 1164 } 1165 1166 #ifndef NDEBUG 1167 void WinEHPrepare::verifyPreparedFunclets(Function &F) { 1168 for (BasicBlock &BB : F) { 1169 size_t NumColors = BlockColors[&BB].size(); 1170 assert(NumColors == 1 && "Expected monochromatic BB!"); 1171 if (NumColors == 0) 1172 report_fatal_error("Uncolored BB!"); 1173 if (NumColors > 1) 1174 report_fatal_error("Multicolor BB!"); 1175 assert((DisableDemotion || !(BB.isEHPad() && isa<PHINode>(BB.begin()))) && 1176 "EH Pad still has a PHI!"); 1177 } 1178 } 1179 #endif 1180 1181 bool WinEHPrepare::prepareExplicitEH(Function &F) { 1182 // Remove unreachable blocks. It is not valuable to assign them a color and 1183 // their existence can trick us into thinking values are alive when they are 1184 // not. 1185 removeUnreachableBlocks(F); 1186 1187 // Determine which blocks are reachable from which funclet entries. 1188 colorFunclets(F); 1189 1190 cloneCommonBlocks(F); 1191 1192 if (!DisableDemotion) 1193 demotePHIsOnFunclets(F, DemoteCatchSwitchPHIOnly || 1194 DemoteCatchSwitchPHIOnlyOpt); 1195 1196 if (!DisableCleanups) { 1197 assert(!verifyFunction(F, &dbgs())); 1198 removeImplausibleInstructions(F); 1199 1200 assert(!verifyFunction(F, &dbgs())); 1201 cleanupPreparedFunclets(F); 1202 } 1203 1204 LLVM_DEBUG(verifyPreparedFunclets(F)); 1205 // Recolor the CFG to verify that all is well. 1206 LLVM_DEBUG(colorFunclets(F)); 1207 LLVM_DEBUG(verifyPreparedFunclets(F)); 1208 1209 BlockColors.clear(); 1210 FuncletBlocks.clear(); 1211 1212 return true; 1213 } 1214 1215 // TODO: Share loads when one use dominates another, or when a catchpad exit 1216 // dominates uses (needs dominators). 1217 AllocaInst *WinEHPrepare::insertPHILoads(PHINode *PN, Function &F) { 1218 BasicBlock *PHIBlock = PN->getParent(); 1219 AllocaInst *SpillSlot = nullptr; 1220 Instruction *EHPad = PHIBlock->getFirstNonPHI(); 1221 1222 if (!EHPad->isTerminator()) { 1223 // If the EHPad isn't a terminator, then we can insert a load in this block 1224 // that will dominate all uses. 1225 SpillSlot = new AllocaInst(PN->getType(), DL->getAllocaAddrSpace(), nullptr, 1226 Twine(PN->getName(), ".wineh.spillslot"), 1227 &F.getEntryBlock().front()); 1228 Value *V = new LoadInst(PN->getType(), SpillSlot, 1229 Twine(PN->getName(), ".wineh.reload"), 1230 &*PHIBlock->getFirstInsertionPt()); 1231 PN->replaceAllUsesWith(V); 1232 return SpillSlot; 1233 } 1234 1235 // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert 1236 // loads of the slot before every use. 1237 DenseMap<BasicBlock *, Value *> Loads; 1238 for (Use &U : llvm::make_early_inc_range(PN->uses())) { 1239 auto *UsingInst = cast<Instruction>(U.getUser()); 1240 if (isa<PHINode>(UsingInst) && UsingInst->getParent()->isEHPad()) { 1241 // Use is on an EH pad phi. Leave it alone; we'll insert loads and 1242 // stores for it separately. 1243 continue; 1244 } 1245 replaceUseWithLoad(PN, U, SpillSlot, Loads, F); 1246 } 1247 return SpillSlot; 1248 } 1249 1250 // TODO: improve store placement. Inserting at def is probably good, but need 1251 // to be careful not to introduce interfering stores (needs liveness analysis). 1252 // TODO: identify related phi nodes that can share spill slots, and share them 1253 // (also needs liveness). 1254 void WinEHPrepare::insertPHIStores(PHINode *OriginalPHI, 1255 AllocaInst *SpillSlot) { 1256 // Use a worklist of (Block, Value) pairs -- the given Value needs to be 1257 // stored to the spill slot by the end of the given Block. 1258 SmallVector<std::pair<BasicBlock *, Value *>, 4> Worklist; 1259 1260 Worklist.push_back({OriginalPHI->getParent(), OriginalPHI}); 1261 1262 while (!Worklist.empty()) { 1263 BasicBlock *EHBlock; 1264 Value *InVal; 1265 std::tie(EHBlock, InVal) = Worklist.pop_back_val(); 1266 1267 PHINode *PN = dyn_cast<PHINode>(InVal); 1268 if (PN && PN->getParent() == EHBlock) { 1269 // The value is defined by another PHI we need to remove, with no room to 1270 // insert a store after the PHI, so each predecessor needs to store its 1271 // incoming value. 1272 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) { 1273 Value *PredVal = PN->getIncomingValue(i); 1274 1275 // Undef can safely be skipped. 1276 if (isa<UndefValue>(PredVal)) 1277 continue; 1278 1279 insertPHIStore(PN->getIncomingBlock(i), PredVal, SpillSlot, Worklist); 1280 } 1281 } else { 1282 // We need to store InVal, which dominates EHBlock, but can't put a store 1283 // in EHBlock, so need to put stores in each predecessor. 1284 for (BasicBlock *PredBlock : predecessors(EHBlock)) { 1285 insertPHIStore(PredBlock, InVal, SpillSlot, Worklist); 1286 } 1287 } 1288 } 1289 } 1290 1291 void WinEHPrepare::insertPHIStore( 1292 BasicBlock *PredBlock, Value *PredVal, AllocaInst *SpillSlot, 1293 SmallVectorImpl<std::pair<BasicBlock *, Value *>> &Worklist) { 1294 1295 if (PredBlock->isEHPad() && PredBlock->getFirstNonPHI()->isTerminator()) { 1296 // Pred is unsplittable, so we need to queue it on the worklist. 1297 Worklist.push_back({PredBlock, PredVal}); 1298 return; 1299 } 1300 1301 // Otherwise, insert the store at the end of the basic block. 1302 new StoreInst(PredVal, SpillSlot, PredBlock->getTerminator()); 1303 } 1304 1305 void WinEHPrepare::replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot, 1306 DenseMap<BasicBlock *, Value *> &Loads, 1307 Function &F) { 1308 // Lazilly create the spill slot. 1309 if (!SpillSlot) 1310 SpillSlot = new AllocaInst(V->getType(), DL->getAllocaAddrSpace(), nullptr, 1311 Twine(V->getName(), ".wineh.spillslot"), 1312 &F.getEntryBlock().front()); 1313 1314 auto *UsingInst = cast<Instruction>(U.getUser()); 1315 if (auto *UsingPHI = dyn_cast<PHINode>(UsingInst)) { 1316 // If this is a PHI node, we can't insert a load of the value before 1317 // the use. Instead insert the load in the predecessor block 1318 // corresponding to the incoming value. 1319 // 1320 // Note that if there are multiple edges from a basic block to this 1321 // PHI node that we cannot have multiple loads. The problem is that 1322 // the resulting PHI node will have multiple values (from each load) 1323 // coming in from the same block, which is illegal SSA form. 1324 // For this reason, we keep track of and reuse loads we insert. 1325 BasicBlock *IncomingBlock = UsingPHI->getIncomingBlock(U); 1326 if (auto *CatchRet = 1327 dyn_cast<CatchReturnInst>(IncomingBlock->getTerminator())) { 1328 // Putting a load above a catchret and use on the phi would still leave 1329 // a cross-funclet def/use. We need to split the edge, change the 1330 // catchret to target the new block, and put the load there. 1331 BasicBlock *PHIBlock = UsingInst->getParent(); 1332 BasicBlock *NewBlock = SplitEdge(IncomingBlock, PHIBlock); 1333 // SplitEdge gives us: 1334 // IncomingBlock: 1335 // ... 1336 // br label %NewBlock 1337 // NewBlock: 1338 // catchret label %PHIBlock 1339 // But we need: 1340 // IncomingBlock: 1341 // ... 1342 // catchret label %NewBlock 1343 // NewBlock: 1344 // br label %PHIBlock 1345 // So move the terminators to each others' blocks and swap their 1346 // successors. 1347 BranchInst *Goto = cast<BranchInst>(IncomingBlock->getTerminator()); 1348 Goto->removeFromParent(); 1349 CatchRet->removeFromParent(); 1350 CatchRet->insertInto(IncomingBlock, IncomingBlock->end()); 1351 Goto->insertInto(NewBlock, NewBlock->end()); 1352 Goto->setSuccessor(0, PHIBlock); 1353 CatchRet->setSuccessor(NewBlock); 1354 // Update the color mapping for the newly split edge. 1355 // Grab a reference to the ColorVector to be inserted before getting the 1356 // reference to the vector we are copying because inserting the new 1357 // element in BlockColors might cause the map to be reallocated. 1358 ColorVector &ColorsForNewBlock = BlockColors[NewBlock]; 1359 ColorVector &ColorsForPHIBlock = BlockColors[PHIBlock]; 1360 ColorsForNewBlock = ColorsForPHIBlock; 1361 for (BasicBlock *FuncletPad : ColorsForPHIBlock) 1362 FuncletBlocks[FuncletPad].push_back(NewBlock); 1363 // Treat the new block as incoming for load insertion. 1364 IncomingBlock = NewBlock; 1365 } 1366 Value *&Load = Loads[IncomingBlock]; 1367 // Insert the load into the predecessor block 1368 if (!Load) 1369 Load = new LoadInst(V->getType(), SpillSlot, 1370 Twine(V->getName(), ".wineh.reload"), 1371 /*isVolatile=*/false, IncomingBlock->getTerminator()); 1372 1373 U.set(Load); 1374 } else { 1375 // Reload right before the old use. 1376 auto *Load = new LoadInst(V->getType(), SpillSlot, 1377 Twine(V->getName(), ".wineh.reload"), 1378 /*isVolatile=*/false, UsingInst); 1379 U.set(Load); 1380 } 1381 } 1382 1383 void WinEHFuncInfo::addIPToStateRange(const InvokeInst *II, 1384 MCSymbol *InvokeBegin, 1385 MCSymbol *InvokeEnd) { 1386 assert(InvokeStateMap.count(II) && 1387 "should get invoke with precomputed state"); 1388 LabelToStateMap[InvokeBegin] = std::make_pair(InvokeStateMap[II], InvokeEnd); 1389 } 1390 1391 void WinEHFuncInfo::addIPToStateRange(int State, MCSymbol* InvokeBegin, 1392 MCSymbol* InvokeEnd) { 1393 LabelToStateMap[InvokeBegin] = std::make_pair(State, InvokeEnd); 1394 } 1395 1396 WinEHFuncInfo::WinEHFuncInfo() = default; 1397