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