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/Module.h"
29 #include "llvm/IR/Verifier.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/TargetParser/Triple.h"
36 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
37 #include "llvm/Transforms/Utils/Cloning.h"
38 #include "llvm/Transforms/Utils/Local.h"
39 #include "llvm/Transforms/Utils/SSAUpdater.h"
43 #define DEBUG_TYPE "win-eh-prepare"
45 static cl::opt
<bool> DisableDemotion(
46 "disable-demotion", cl::Hidden
,
48 "Clone multicolor basic blocks but do not demote cross scopes"),
51 static cl::opt
<bool> DisableCleanups(
52 "disable-cleanups", cl::Hidden
,
53 cl::desc("Do not remove implausible terminators or other similar cleanups"),
56 // TODO: Remove this option when we fully migrate to new pass manager
57 static cl::opt
<bool> DemoteCatchSwitchPHIOnlyOpt(
58 "demote-catchswitch-only", cl::Hidden
,
59 cl::desc("Demote catchswitch BBs only (for wasm EH)"), cl::init(false));
63 class WinEHPrepareImpl
{
65 WinEHPrepareImpl(bool DemoteCatchSwitchPHIOnly
)
66 : DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly
) {}
68 bool runOnFunction(Function
&Fn
);
71 void insertPHIStores(PHINode
*OriginalPHI
, AllocaInst
*SpillSlot
);
73 insertPHIStore(BasicBlock
*PredBlock
, Value
*PredVal
, AllocaInst
*SpillSlot
,
74 SmallVectorImpl
<std::pair
<BasicBlock
*, Value
*>> &Worklist
);
75 AllocaInst
*insertPHILoads(PHINode
*PN
, Function
&F
);
76 void replaceUseWithLoad(Value
*V
, Use
&U
, AllocaInst
*&SpillSlot
,
77 DenseMap
<BasicBlock
*, Value
*> &Loads
, Function
&F
);
78 bool prepareExplicitEH(Function
&F
);
79 void colorFunclets(Function
&F
);
81 void demotePHIsOnFunclets(Function
&F
, bool DemoteCatchSwitchPHIOnly
);
82 void cloneCommonBlocks(Function
&F
);
83 void removeImplausibleInstructions(Function
&F
);
84 void cleanupPreparedFunclets(Function
&F
);
85 void verifyPreparedFunclets(Function
&F
);
87 bool DemoteCatchSwitchPHIOnly
;
89 // All fields are reset by runOnFunction.
90 EHPersonality Personality
= EHPersonality::Unknown
;
92 const DataLayout
*DL
= nullptr;
93 DenseMap
<BasicBlock
*, ColorVector
> BlockColors
;
94 MapVector
<BasicBlock
*, std::vector
<BasicBlock
*>> FuncletBlocks
;
97 class WinEHPrepare
: public FunctionPass
{
98 bool DemoteCatchSwitchPHIOnly
;
101 static char ID
; // Pass identification, replacement for typeid.
103 WinEHPrepare(bool DemoteCatchSwitchPHIOnly
= false)
104 : FunctionPass(ID
), DemoteCatchSwitchPHIOnly(DemoteCatchSwitchPHIOnly
) {}
106 StringRef
getPassName() const override
{
107 return "Windows exception handling preparation";
110 bool runOnFunction(Function
&Fn
) override
{
111 return WinEHPrepareImpl(DemoteCatchSwitchPHIOnly
).runOnFunction(Fn
);
115 } // end anonymous namespace
117 PreservedAnalyses
WinEHPreparePass::run(Function
&F
,
118 FunctionAnalysisManager
&) {
119 bool Changed
= WinEHPrepareImpl(DemoteCatchSwitchPHIOnly
).runOnFunction(F
);
120 return Changed
? PreservedAnalyses::none() : PreservedAnalyses::all();
123 char WinEHPrepare::ID
= 0;
124 INITIALIZE_PASS(WinEHPrepare
, DEBUG_TYPE
, "Prepare Windows exceptions", false,
127 FunctionPass
*llvm::createWinEHPass(bool DemoteCatchSwitchPHIOnly
) {
128 return new WinEHPrepare(DemoteCatchSwitchPHIOnly
);
131 bool WinEHPrepareImpl::runOnFunction(Function
&Fn
) {
132 if (!Fn
.hasPersonalityFn())
135 // Classify the personality to see what kind of preparation we need.
136 Personality
= classifyEHPersonality(Fn
.getPersonalityFn());
138 // Do nothing if this is not a scope-based personality.
139 if (!isScopedEHPersonality(Personality
))
142 DL
= &Fn
.getDataLayout();
143 return prepareExplicitEH(Fn
);
146 static int addUnwindMapEntry(WinEHFuncInfo
&FuncInfo
, int ToState
,
147 const BasicBlock
*BB
) {
148 CxxUnwindMapEntry UME
;
149 UME
.ToState
= ToState
;
151 FuncInfo
.CxxUnwindMap
.push_back(UME
);
152 return FuncInfo
.getLastStateNumber();
155 static void addTryBlockMapEntry(WinEHFuncInfo
&FuncInfo
, int TryLow
,
156 int TryHigh
, int CatchHigh
,
157 ArrayRef
<const CatchPadInst
*> Handlers
) {
158 WinEHTryBlockMapEntry TBME
;
159 TBME
.TryLow
= TryLow
;
160 TBME
.TryHigh
= TryHigh
;
161 TBME
.CatchHigh
= CatchHigh
;
162 assert(TBME
.TryLow
<= TBME
.TryHigh
);
163 for (const CatchPadInst
*CPI
: Handlers
) {
165 Constant
*TypeInfo
= cast
<Constant
>(CPI
->getArgOperand(0));
166 if (TypeInfo
->isNullValue())
167 HT
.TypeDescriptor
= nullptr;
169 HT
.TypeDescriptor
= cast
<GlobalVariable
>(TypeInfo
->stripPointerCasts());
170 HT
.Adjectives
= cast
<ConstantInt
>(CPI
->getArgOperand(1))->getZExtValue();
171 HT
.Handler
= CPI
->getParent();
173 dyn_cast
<AllocaInst
>(CPI
->getArgOperand(2)->stripPointerCasts()))
174 HT
.CatchObj
.Alloca
= AI
;
176 HT
.CatchObj
.Alloca
= nullptr;
177 TBME
.HandlerArray
.push_back(HT
);
179 FuncInfo
.TryBlockMap
.push_back(TBME
);
182 static BasicBlock
*getCleanupRetUnwindDest(const CleanupPadInst
*CleanupPad
) {
183 for (const User
*U
: CleanupPad
->users())
184 if (const auto *CRI
= dyn_cast
<CleanupReturnInst
>(U
))
185 return CRI
->getUnwindDest();
189 static void calculateStateNumbersForInvokes(const Function
*Fn
,
190 WinEHFuncInfo
&FuncInfo
) {
191 auto *F
= const_cast<Function
*>(Fn
);
192 DenseMap
<BasicBlock
*, ColorVector
> BlockColors
= colorEHFunclets(*F
);
193 for (BasicBlock
&BB
: *F
) {
194 auto *II
= dyn_cast
<InvokeInst
>(BB
.getTerminator());
198 auto &BBColors
= BlockColors
[&BB
];
199 assert(BBColors
.size() == 1 && "multi-color BB not removed by preparation");
200 BasicBlock
*FuncletEntryBB
= BBColors
.front();
202 BasicBlock
*FuncletUnwindDest
;
204 dyn_cast
<FuncletPadInst
>(FuncletEntryBB
->getFirstNonPHI());
205 assert(FuncletPad
|| FuncletEntryBB
== &Fn
->getEntryBlock());
207 FuncletUnwindDest
= nullptr;
208 else if (auto *CatchPad
= dyn_cast
<CatchPadInst
>(FuncletPad
))
209 FuncletUnwindDest
= CatchPad
->getCatchSwitch()->getUnwindDest();
210 else if (auto *CleanupPad
= dyn_cast
<CleanupPadInst
>(FuncletPad
))
211 FuncletUnwindDest
= getCleanupRetUnwindDest(CleanupPad
);
213 llvm_unreachable("unexpected funclet pad!");
215 BasicBlock
*InvokeUnwindDest
= II
->getUnwindDest();
217 if (FuncletUnwindDest
== InvokeUnwindDest
) {
218 auto BaseStateI
= FuncInfo
.FuncletBaseStateMap
.find(FuncletPad
);
219 if (BaseStateI
!= FuncInfo
.FuncletBaseStateMap
.end())
220 BaseState
= BaseStateI
->second
;
223 if (BaseState
!= -1) {
224 FuncInfo
.InvokeStateMap
[II
] = BaseState
;
226 Instruction
*PadInst
= InvokeUnwindDest
->getFirstNonPHI();
227 assert(FuncInfo
.EHPadStateMap
.count(PadInst
) && "EH Pad has no state!");
228 FuncInfo
.InvokeStateMap
[II
] = FuncInfo
.EHPadStateMap
[PadInst
];
233 // See comments below for calculateSEHStateForAsynchEH().
234 // State - incoming State of normal paths
236 const BasicBlock
*Block
;
238 WorkItem(const BasicBlock
*BB
, int St
) {
243 void llvm::calculateCXXStateForAsynchEH(const BasicBlock
*BB
, int State
,
244 WinEHFuncInfo
&EHInfo
) {
245 SmallVector
<struct WorkItem
*, 8> WorkList
;
246 struct WorkItem
*WI
= new WorkItem(BB
, State
);
247 WorkList
.push_back(WI
);
249 while (!WorkList
.empty()) {
250 WI
= WorkList
.pop_back_val();
251 const BasicBlock
*BB
= WI
->Block
;
252 int State
= WI
->State
;
254 if (EHInfo
.BlockToStateMap
.count(BB
) && EHInfo
.BlockToStateMap
[BB
] <= State
)
255 continue; // skip blocks already visited by lower State
257 const llvm::Instruction
*I
= BB
->getFirstNonPHI();
258 const llvm::Instruction
*TI
= BB
->getTerminator();
260 State
= EHInfo
.EHPadStateMap
[I
];
261 EHInfo
.BlockToStateMap
[BB
] = State
; // Record state, also flag visiting
263 if ((isa
<CleanupReturnInst
>(TI
) || isa
<CatchReturnInst
>(TI
)) && State
> 0) {
264 // Retrive the new State
265 State
= EHInfo
.CxxUnwindMap
[State
].ToState
; // Retrive next State
266 } else if (isa
<InvokeInst
>(TI
)) {
267 auto *Call
= cast
<CallBase
>(TI
);
268 const Function
*Fn
= Call
->getCalledFunction();
269 if (Fn
&& Fn
->isIntrinsic() &&
270 (Fn
->getIntrinsicID() == Intrinsic::seh_scope_begin
||
271 Fn
->getIntrinsicID() == Intrinsic::seh_try_begin
))
272 // Retrive the new State from seh_scope_begin
273 State
= EHInfo
.InvokeStateMap
[cast
<InvokeInst
>(TI
)];
274 else if (Fn
&& Fn
->isIntrinsic() &&
275 (Fn
->getIntrinsicID() == Intrinsic::seh_scope_end
||
276 Fn
->getIntrinsicID() == Intrinsic::seh_try_end
)) {
277 // In case of conditional ctor, let's retrieve State from Invoke
278 State
= EHInfo
.InvokeStateMap
[cast
<InvokeInst
>(TI
)];
279 // end of current state, retrive new state from UnwindMap
280 State
= EHInfo
.CxxUnwindMap
[State
].ToState
;
283 // Continue push successors into worklist
284 for (auto *SuccBB
: successors(BB
)) {
285 WI
= new WorkItem(SuccBB
, State
);
286 WorkList
.push_back(WI
);
291 // The central theory of this routine is based on the following:
292 // A _try scope is always a SEME (Single Entry Multiple Exits) region
293 // as jumping into a _try is not allowed
294 // The single entry must start with a seh_try_begin() invoke with a
295 // correct State number that is the initial state of the SEME.
296 // Through control-flow, state number is propagated into all blocks.
297 // Side exits marked by seh_try_end() will unwind to parent state via
298 // existing SEHUnwindMap[].
299 // Side exits can ONLY jump into parent scopes (lower state number).
300 // Thus, when a block succeeds various states from its predecessors,
301 // the lowest State trumphs others.
302 // If some exits flow to unreachable, propagation on those paths terminate,
303 // not affecting remaining blocks.
304 void llvm::calculateSEHStateForAsynchEH(const BasicBlock
*BB
, int State
,
305 WinEHFuncInfo
&EHInfo
) {
306 SmallVector
<struct WorkItem
*, 8> WorkList
;
307 struct WorkItem
*WI
= new WorkItem(BB
, State
);
308 WorkList
.push_back(WI
);
310 while (!WorkList
.empty()) {
311 WI
= WorkList
.pop_back_val();
312 const BasicBlock
*BB
= WI
->Block
;
313 int State
= WI
->State
;
315 if (EHInfo
.BlockToStateMap
.count(BB
) && EHInfo
.BlockToStateMap
[BB
] <= State
)
316 continue; // skip blocks already visited by lower State
318 const llvm::Instruction
*I
= BB
->getFirstNonPHI();
319 const llvm::Instruction
*TI
= BB
->getTerminator();
321 State
= EHInfo
.EHPadStateMap
[I
];
322 EHInfo
.BlockToStateMap
[BB
] = State
; // Record state
324 if (isa
<CatchPadInst
>(I
) && isa
<CatchReturnInst
>(TI
)) {
325 const Constant
*FilterOrNull
= cast
<Constant
>(
326 cast
<CatchPadInst
>(I
)->getArgOperand(0)->stripPointerCasts());
327 const Function
*Filter
= dyn_cast
<Function
>(FilterOrNull
);
328 if (!Filter
|| !Filter
->getName().starts_with("__IsLocalUnwind"))
329 State
= EHInfo
.SEHUnwindMap
[State
].ToState
; // Retrive next State
330 } else if ((isa
<CleanupReturnInst
>(TI
) || isa
<CatchReturnInst
>(TI
)) &&
332 // Retrive the new State.
333 State
= EHInfo
.SEHUnwindMap
[State
].ToState
; // Retrive next State
334 } else if (isa
<InvokeInst
>(TI
)) {
335 auto *Call
= cast
<CallBase
>(TI
);
336 const Function
*Fn
= Call
->getCalledFunction();
337 if (Fn
&& Fn
->isIntrinsic() &&
338 Fn
->getIntrinsicID() == Intrinsic::seh_try_begin
)
339 // Retrive the new State from seh_try_begin
340 State
= EHInfo
.InvokeStateMap
[cast
<InvokeInst
>(TI
)];
341 else if (Fn
&& Fn
->isIntrinsic() &&
342 Fn
->getIntrinsicID() == Intrinsic::seh_try_end
)
343 // end of current state, retrive new state from UnwindMap
344 State
= EHInfo
.SEHUnwindMap
[State
].ToState
;
346 // Continue push successors into worklist
347 for (auto *SuccBB
: successors(BB
)) {
348 WI
= new WorkItem(SuccBB
, State
);
349 WorkList
.push_back(WI
);
354 // Given BB which ends in an unwind edge, return the EHPad that this BB belongs
355 // to. If the unwind edge came from an invoke, return null.
356 static const BasicBlock
*getEHPadFromPredecessor(const BasicBlock
*BB
,
358 const Instruction
*TI
= BB
->getTerminator();
359 if (isa
<InvokeInst
>(TI
))
361 if (auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(TI
)) {
362 if (CatchSwitch
->getParentPad() != ParentPad
)
366 assert(!TI
->isEHPad() && "unexpected EHPad!");
367 auto *CleanupPad
= cast
<CleanupReturnInst
>(TI
)->getCleanupPad();
368 if (CleanupPad
->getParentPad() != ParentPad
)
370 return CleanupPad
->getParent();
373 // Starting from a EHPad, Backward walk through control-flow graph
374 // to produce two primary outputs:
375 // FuncInfo.EHPadStateMap[] and FuncInfo.CxxUnwindMap[]
376 static void calculateCXXStateNumbers(WinEHFuncInfo
&FuncInfo
,
377 const Instruction
*FirstNonPHI
,
379 const BasicBlock
*BB
= FirstNonPHI
->getParent();
380 assert(BB
->isEHPad() && "not a funclet!");
382 if (auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(FirstNonPHI
)) {
383 assert(FuncInfo
.EHPadStateMap
.count(CatchSwitch
) == 0 &&
384 "shouldn't revist catch funclets!");
386 SmallVector
<const CatchPadInst
*, 2> Handlers
;
387 for (const BasicBlock
*CatchPadBB
: CatchSwitch
->handlers()) {
388 auto *CatchPad
= cast
<CatchPadInst
>(CatchPadBB
->getFirstNonPHI());
389 Handlers
.push_back(CatchPad
);
391 int TryLow
= addUnwindMapEntry(FuncInfo
, ParentState
, nullptr);
392 FuncInfo
.EHPadStateMap
[CatchSwitch
] = TryLow
;
393 for (const BasicBlock
*PredBlock
: predecessors(BB
))
394 if ((PredBlock
= getEHPadFromPredecessor(PredBlock
,
395 CatchSwitch
->getParentPad())))
396 calculateCXXStateNumbers(FuncInfo
, PredBlock
->getFirstNonPHI(),
398 int CatchLow
= addUnwindMapEntry(FuncInfo
, ParentState
, nullptr);
400 // catchpads are separate funclets in C++ EH due to the way rethrow works.
401 int TryHigh
= CatchLow
- 1;
403 // MSVC FrameHandler3/4 on x64&Arm64 expect Catch Handlers in $tryMap$
404 // stored in pre-order (outer first, inner next), not post-order
405 // Add to map here. Fix the CatchHigh after children are processed
406 const Module
*Mod
= BB
->getParent()->getParent();
407 bool IsPreOrder
= Triple(Mod
->getTargetTriple()).isArch64Bit();
409 addTryBlockMapEntry(FuncInfo
, TryLow
, TryHigh
, CatchLow
, Handlers
);
410 unsigned TBMEIdx
= FuncInfo
.TryBlockMap
.size() - 1;
412 for (const auto *CatchPad
: Handlers
) {
413 FuncInfo
.FuncletBaseStateMap
[CatchPad
] = CatchLow
;
414 FuncInfo
.EHPadStateMap
[CatchPad
] = CatchLow
;
415 for (const User
*U
: CatchPad
->users()) {
416 const auto *UserI
= cast
<Instruction
>(U
);
417 if (auto *InnerCatchSwitch
= dyn_cast
<CatchSwitchInst
>(UserI
)) {
418 BasicBlock
*UnwindDest
= InnerCatchSwitch
->getUnwindDest();
419 if (!UnwindDest
|| UnwindDest
== CatchSwitch
->getUnwindDest())
420 calculateCXXStateNumbers(FuncInfo
, UserI
, CatchLow
);
422 if (auto *InnerCleanupPad
= dyn_cast
<CleanupPadInst
>(UserI
)) {
423 BasicBlock
*UnwindDest
= getCleanupRetUnwindDest(InnerCleanupPad
);
424 // If a nested cleanup pad reports a null unwind destination and the
425 // enclosing catch pad doesn't it must be post-dominated by an
426 // unreachable instruction.
427 if (!UnwindDest
|| UnwindDest
== CatchSwitch
->getUnwindDest())
428 calculateCXXStateNumbers(FuncInfo
, UserI
, CatchLow
);
432 int CatchHigh
= FuncInfo
.getLastStateNumber();
433 // Now child Catches are processed, update CatchHigh
435 FuncInfo
.TryBlockMap
[TBMEIdx
].CatchHigh
= CatchHigh
;
437 addTryBlockMapEntry(FuncInfo
, TryLow
, TryHigh
, CatchHigh
, Handlers
);
439 LLVM_DEBUG(dbgs() << "TryLow[" << BB
->getName() << "]: " << TryLow
<< '\n');
440 LLVM_DEBUG(dbgs() << "TryHigh[" << BB
->getName() << "]: " << TryHigh
442 LLVM_DEBUG(dbgs() << "CatchHigh[" << BB
->getName() << "]: " << CatchHigh
445 auto *CleanupPad
= cast
<CleanupPadInst
>(FirstNonPHI
);
447 // It's possible for a cleanup to be visited twice: it might have multiple
448 // cleanupret instructions.
449 if (FuncInfo
.EHPadStateMap
.count(CleanupPad
))
452 int CleanupState
= addUnwindMapEntry(FuncInfo
, ParentState
, BB
);
453 FuncInfo
.EHPadStateMap
[CleanupPad
] = CleanupState
;
454 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState
<< " to BB "
455 << BB
->getName() << '\n');
456 for (const BasicBlock
*PredBlock
: predecessors(BB
)) {
457 if ((PredBlock
= getEHPadFromPredecessor(PredBlock
,
458 CleanupPad
->getParentPad()))) {
459 calculateCXXStateNumbers(FuncInfo
, PredBlock
->getFirstNonPHI(),
463 for (const User
*U
: CleanupPad
->users()) {
464 const auto *UserI
= cast
<Instruction
>(U
);
465 if (UserI
->isEHPad())
466 report_fatal_error("Cleanup funclets for the MSVC++ personality cannot "
467 "contain exceptional actions");
472 static int addSEHExcept(WinEHFuncInfo
&FuncInfo
, int ParentState
,
473 const Function
*Filter
, const BasicBlock
*Handler
) {
474 SEHUnwindMapEntry Entry
;
475 Entry
.ToState
= ParentState
;
476 Entry
.IsFinally
= false;
477 Entry
.Filter
= Filter
;
478 Entry
.Handler
= Handler
;
479 FuncInfo
.SEHUnwindMap
.push_back(Entry
);
480 return FuncInfo
.SEHUnwindMap
.size() - 1;
483 static int addSEHFinally(WinEHFuncInfo
&FuncInfo
, int ParentState
,
484 const BasicBlock
*Handler
) {
485 SEHUnwindMapEntry Entry
;
486 Entry
.ToState
= ParentState
;
487 Entry
.IsFinally
= true;
488 Entry
.Filter
= nullptr;
489 Entry
.Handler
= Handler
;
490 FuncInfo
.SEHUnwindMap
.push_back(Entry
);
491 return FuncInfo
.SEHUnwindMap
.size() - 1;
494 // Starting from a EHPad, Backward walk through control-flow graph
495 // to produce two primary outputs:
496 // FuncInfo.EHPadStateMap[] and FuncInfo.SEHUnwindMap[]
497 static void calculateSEHStateNumbers(WinEHFuncInfo
&FuncInfo
,
498 const Instruction
*FirstNonPHI
,
500 const BasicBlock
*BB
= FirstNonPHI
->getParent();
501 assert(BB
->isEHPad() && "no a funclet!");
503 if (auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(FirstNonPHI
)) {
504 assert(FuncInfo
.EHPadStateMap
.count(CatchSwitch
) == 0 &&
505 "shouldn't revist catch funclets!");
507 // Extract the filter function and the __except basic block and create a
509 assert(CatchSwitch
->getNumHandlers() == 1 &&
510 "SEH doesn't have multiple handlers per __try");
511 const auto *CatchPad
=
512 cast
<CatchPadInst
>((*CatchSwitch
->handler_begin())->getFirstNonPHI());
513 const BasicBlock
*CatchPadBB
= CatchPad
->getParent();
514 const Constant
*FilterOrNull
=
515 cast
<Constant
>(CatchPad
->getArgOperand(0)->stripPointerCasts());
516 const Function
*Filter
= dyn_cast
<Function
>(FilterOrNull
);
517 assert((Filter
|| FilterOrNull
->isNullValue()) &&
518 "unexpected filter value");
519 int TryState
= addSEHExcept(FuncInfo
, ParentState
, Filter
, CatchPadBB
);
521 // Everything in the __try block uses TryState as its parent state.
522 FuncInfo
.EHPadStateMap
[CatchSwitch
] = TryState
;
523 FuncInfo
.EHPadStateMap
[CatchPad
] = TryState
;
524 LLVM_DEBUG(dbgs() << "Assigning state #" << TryState
<< " to BB "
525 << CatchPadBB
->getName() << '\n');
526 for (const BasicBlock
*PredBlock
: predecessors(BB
))
527 if ((PredBlock
= getEHPadFromPredecessor(PredBlock
,
528 CatchSwitch
->getParentPad())))
529 calculateSEHStateNumbers(FuncInfo
, PredBlock
->getFirstNonPHI(),
532 // Everything in the __except block unwinds to ParentState, just like code
533 // outside the __try.
534 for (const User
*U
: CatchPad
->users()) {
535 const auto *UserI
= cast
<Instruction
>(U
);
536 if (auto *InnerCatchSwitch
= dyn_cast
<CatchSwitchInst
>(UserI
)) {
537 BasicBlock
*UnwindDest
= InnerCatchSwitch
->getUnwindDest();
538 if (!UnwindDest
|| UnwindDest
== CatchSwitch
->getUnwindDest())
539 calculateSEHStateNumbers(FuncInfo
, UserI
, ParentState
);
541 if (auto *InnerCleanupPad
= dyn_cast
<CleanupPadInst
>(UserI
)) {
542 BasicBlock
*UnwindDest
= getCleanupRetUnwindDest(InnerCleanupPad
);
543 // If a nested cleanup pad reports a null unwind destination and the
544 // enclosing catch pad doesn't it must be post-dominated by an
545 // unreachable instruction.
546 if (!UnwindDest
|| UnwindDest
== CatchSwitch
->getUnwindDest())
547 calculateSEHStateNumbers(FuncInfo
, UserI
, ParentState
);
551 auto *CleanupPad
= cast
<CleanupPadInst
>(FirstNonPHI
);
553 // It's possible for a cleanup to be visited twice: it might have multiple
554 // cleanupret instructions.
555 if (FuncInfo
.EHPadStateMap
.count(CleanupPad
))
558 int CleanupState
= addSEHFinally(FuncInfo
, ParentState
, BB
);
559 FuncInfo
.EHPadStateMap
[CleanupPad
] = CleanupState
;
560 LLVM_DEBUG(dbgs() << "Assigning state #" << CleanupState
<< " to BB "
561 << BB
->getName() << '\n');
562 for (const BasicBlock
*PredBlock
: predecessors(BB
))
564 getEHPadFromPredecessor(PredBlock
, CleanupPad
->getParentPad())))
565 calculateSEHStateNumbers(FuncInfo
, PredBlock
->getFirstNonPHI(),
567 for (const User
*U
: CleanupPad
->users()) {
568 const auto *UserI
= cast
<Instruction
>(U
);
569 if (UserI
->isEHPad())
570 report_fatal_error("Cleanup funclets for the SEH personality cannot "
571 "contain exceptional actions");
576 static bool isTopLevelPadForMSVC(const Instruction
*EHPad
) {
577 if (auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(EHPad
))
578 return isa
<ConstantTokenNone
>(CatchSwitch
->getParentPad()) &&
579 CatchSwitch
->unwindsToCaller();
580 if (auto *CleanupPad
= dyn_cast
<CleanupPadInst
>(EHPad
))
581 return isa
<ConstantTokenNone
>(CleanupPad
->getParentPad()) &&
582 getCleanupRetUnwindDest(CleanupPad
) == nullptr;
583 if (isa
<CatchPadInst
>(EHPad
))
585 llvm_unreachable("unexpected EHPad!");
588 void llvm::calculateSEHStateNumbers(const Function
*Fn
,
589 WinEHFuncInfo
&FuncInfo
) {
590 // Don't compute state numbers twice.
591 if (!FuncInfo
.SEHUnwindMap
.empty())
594 for (const BasicBlock
&BB
: *Fn
) {
597 const Instruction
*FirstNonPHI
= BB
.getFirstNonPHI();
598 if (!isTopLevelPadForMSVC(FirstNonPHI
))
600 ::calculateSEHStateNumbers(FuncInfo
, FirstNonPHI
, -1);
603 calculateStateNumbersForInvokes(Fn
, FuncInfo
);
605 bool IsEHa
= Fn
->getParent()->getModuleFlag("eh-asynch");
607 const BasicBlock
*EntryBB
= &(Fn
->getEntryBlock());
608 calculateSEHStateForAsynchEH(EntryBB
, -1, FuncInfo
);
612 void llvm::calculateWinCXXEHStateNumbers(const Function
*Fn
,
613 WinEHFuncInfo
&FuncInfo
) {
614 // Return if it's already been done.
615 if (!FuncInfo
.EHPadStateMap
.empty())
618 for (const BasicBlock
&BB
: *Fn
) {
621 const Instruction
*FirstNonPHI
= BB
.getFirstNonPHI();
622 if (!isTopLevelPadForMSVC(FirstNonPHI
))
624 calculateCXXStateNumbers(FuncInfo
, FirstNonPHI
, -1);
627 calculateStateNumbersForInvokes(Fn
, FuncInfo
);
629 bool IsEHa
= Fn
->getParent()->getModuleFlag("eh-asynch");
631 const BasicBlock
*EntryBB
= &(Fn
->getEntryBlock());
632 calculateCXXStateForAsynchEH(EntryBB
, -1, FuncInfo
);
636 static int addClrEHHandler(WinEHFuncInfo
&FuncInfo
, int HandlerParentState
,
637 int TryParentState
, ClrHandlerType HandlerType
,
638 uint32_t TypeToken
, const BasicBlock
*Handler
) {
639 ClrEHUnwindMapEntry Entry
;
640 Entry
.HandlerParentState
= HandlerParentState
;
641 Entry
.TryParentState
= TryParentState
;
642 Entry
.Handler
= Handler
;
643 Entry
.HandlerType
= HandlerType
;
644 Entry
.TypeToken
= TypeToken
;
645 FuncInfo
.ClrEHUnwindMap
.push_back(Entry
);
646 return FuncInfo
.ClrEHUnwindMap
.size() - 1;
649 void llvm::calculateClrEHStateNumbers(const Function
*Fn
,
650 WinEHFuncInfo
&FuncInfo
) {
651 // Return if it's already been done.
652 if (!FuncInfo
.EHPadStateMap
.empty())
655 // This numbering assigns one state number to each catchpad and cleanuppad.
656 // It also computes two tree-like relations over states:
657 // 1) Each state has a "HandlerParentState", which is the state of the next
658 // outer handler enclosing this state's handler (same as nearest ancestor
659 // per the ParentPad linkage on EH pads, but skipping over catchswitches).
660 // 2) Each state has a "TryParentState", which:
661 // a) for a catchpad that's not the last handler on its catchswitch, is
662 // the state of the next catchpad on that catchswitch
663 // b) for all other pads, is the state of the pad whose try region is the
664 // next outer try region enclosing this state's try region. The "try
665 // regions are not present as such in the IR, but will be inferred
666 // based on the placement of invokes and pads which reach each other
667 // by exceptional exits
668 // Catchswitches do not get their own states, but each gets mapped to the
669 // state of its first catchpad.
671 // Step one: walk down from outermost to innermost funclets, assigning each
672 // catchpad and cleanuppad a state number. Add an entry to the
673 // ClrEHUnwindMap for each state, recording its HandlerParentState and
674 // handler attributes. Record the TryParentState as well for each catchpad
675 // that's not the last on its catchswitch, but initialize all other entries'
676 // TryParentStates to a sentinel -1 value that the next pass will update.
678 // Seed a worklist with pads that have no parent.
679 SmallVector
<std::pair
<const Instruction
*, int>, 8> Worklist
;
680 for (const BasicBlock
&BB
: *Fn
) {
681 const Instruction
*FirstNonPHI
= BB
.getFirstNonPHI();
682 const Value
*ParentPad
;
683 if (const auto *CPI
= dyn_cast
<CleanupPadInst
>(FirstNonPHI
))
684 ParentPad
= CPI
->getParentPad();
685 else if (const auto *CSI
= dyn_cast
<CatchSwitchInst
>(FirstNonPHI
))
686 ParentPad
= CSI
->getParentPad();
689 if (isa
<ConstantTokenNone
>(ParentPad
))
690 Worklist
.emplace_back(FirstNonPHI
, -1);
693 // Use the worklist to visit all pads, from outer to inner. Record
694 // HandlerParentState for all pads. Record TryParentState only for catchpads
695 // that aren't the last on their catchswitch (setting all other entries'
696 // TryParentStates to an initial value of -1). This loop is also responsible
697 // for setting the EHPadStateMap entry for all catchpads, cleanuppads, and
699 while (!Worklist
.empty()) {
700 const Instruction
*Pad
;
701 int HandlerParentState
;
702 std::tie(Pad
, HandlerParentState
) = Worklist
.pop_back_val();
704 if (const auto *Cleanup
= dyn_cast
<CleanupPadInst
>(Pad
)) {
705 // Create the entry for this cleanup with the appropriate handler
706 // properties. Finally and fault handlers are distinguished by arity.
707 ClrHandlerType HandlerType
=
708 (Cleanup
->arg_size() ? ClrHandlerType::Fault
709 : ClrHandlerType::Finally
);
710 int CleanupState
= addClrEHHandler(FuncInfo
, HandlerParentState
, -1,
711 HandlerType
, 0, Pad
->getParent());
712 // Queue any child EH pads on the worklist.
713 for (const User
*U
: Cleanup
->users())
714 if (const auto *I
= dyn_cast
<Instruction
>(U
))
716 Worklist
.emplace_back(I
, CleanupState
);
717 // Remember this pad's state.
718 FuncInfo
.EHPadStateMap
[Cleanup
] = CleanupState
;
720 // Walk the handlers of this catchswitch in reverse order since all but
721 // the last need to set the following one as its TryParentState.
722 const auto *CatchSwitch
= cast
<CatchSwitchInst
>(Pad
);
723 int CatchState
= -1, FollowerState
= -1;
724 SmallVector
<const BasicBlock
*, 4> CatchBlocks(CatchSwitch
->handlers());
725 for (const BasicBlock
*CatchBlock
: llvm::reverse(CatchBlocks
)) {
726 // Create the entry for this catch with the appropriate handler
728 const auto *Catch
= cast
<CatchPadInst
>(CatchBlock
->getFirstNonPHI());
729 uint32_t TypeToken
= static_cast<uint32_t>(
730 cast
<ConstantInt
>(Catch
->getArgOperand(0))->getZExtValue());
732 addClrEHHandler(FuncInfo
, HandlerParentState
, FollowerState
,
733 ClrHandlerType::Catch
, TypeToken
, CatchBlock
);
734 // Queue any child EH pads on the worklist.
735 for (const User
*U
: Catch
->users())
736 if (const auto *I
= dyn_cast
<Instruction
>(U
))
738 Worklist
.emplace_back(I
, CatchState
);
739 // Remember this catch's state.
740 FuncInfo
.EHPadStateMap
[Catch
] = CatchState
;
741 FollowerState
= CatchState
;
743 // Associate the catchswitch with the state of its first catch.
744 assert(CatchSwitch
->getNumHandlers());
745 FuncInfo
.EHPadStateMap
[CatchSwitch
] = CatchState
;
749 // Step two: record the TryParentState of each state. For cleanuppads that
750 // don't have cleanuprets, we may need to infer this from their child pads,
751 // so visit pads in descendant-most to ancestor-most order.
752 for (ClrEHUnwindMapEntry
&Entry
: llvm::reverse(FuncInfo
.ClrEHUnwindMap
)) {
753 const Instruction
*Pad
=
754 cast
<const BasicBlock
*>(Entry
.Handler
)->getFirstNonPHI();
755 // For most pads, the TryParentState is the state associated with the
756 // unwind dest of exceptional exits from it.
757 const BasicBlock
*UnwindDest
;
758 if (const auto *Catch
= dyn_cast
<CatchPadInst
>(Pad
)) {
759 // If a catch is not the last in its catchswitch, its TryParentState is
760 // the state associated with the next catch in the switch, even though
761 // that's not the unwind dest of exceptions escaping the catch. Those
762 // cases were already assigned a TryParentState in the first pass, so
764 if (Entry
.TryParentState
!= -1)
766 // Otherwise, get the unwind dest from the catchswitch.
767 UnwindDest
= Catch
->getCatchSwitch()->getUnwindDest();
769 const auto *Cleanup
= cast
<CleanupPadInst
>(Pad
);
770 UnwindDest
= nullptr;
771 for (const User
*U
: Cleanup
->users()) {
772 if (auto *CleanupRet
= dyn_cast
<CleanupReturnInst
>(U
)) {
773 // Common and unambiguous case -- cleanupret indicates cleanup's
775 UnwindDest
= CleanupRet
->getUnwindDest();
779 // Get an unwind dest for the user
780 const BasicBlock
*UserUnwindDest
= nullptr;
781 if (auto *Invoke
= dyn_cast
<InvokeInst
>(U
)) {
782 UserUnwindDest
= Invoke
->getUnwindDest();
783 } else if (auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(U
)) {
784 UserUnwindDest
= CatchSwitch
->getUnwindDest();
785 } else if (auto *ChildCleanup
= dyn_cast
<CleanupPadInst
>(U
)) {
786 int UserState
= FuncInfo
.EHPadStateMap
[ChildCleanup
];
787 int UserUnwindState
=
788 FuncInfo
.ClrEHUnwindMap
[UserState
].TryParentState
;
789 if (UserUnwindState
!= -1)
790 UserUnwindDest
= cast
<const BasicBlock
*>(
791 FuncInfo
.ClrEHUnwindMap
[UserUnwindState
].Handler
);
794 // Not having an unwind dest for this user might indicate that it
795 // doesn't unwind, so can't be taken as proof that the cleanup itself
796 // may unwind to caller (see e.g. SimplifyUnreachable and
797 // RemoveUnwindEdge).
801 // Now we have an unwind dest for the user, but we need to see if it
802 // unwinds all the way out of the cleanup or if it stays within it.
803 const Instruction
*UserUnwindPad
= UserUnwindDest
->getFirstNonPHI();
804 const Value
*UserUnwindParent
;
805 if (auto *CSI
= dyn_cast
<CatchSwitchInst
>(UserUnwindPad
))
806 UserUnwindParent
= CSI
->getParentPad();
809 cast
<CleanupPadInst
>(UserUnwindPad
)->getParentPad();
811 // The unwind stays within the cleanup iff it targets a child of the
813 if (UserUnwindParent
== Cleanup
)
816 // This unwind exits the cleanup, so its dest is the cleanup's dest.
817 UnwindDest
= UserUnwindDest
;
822 // Record the state of the unwind dest as the TryParentState.
825 // If UnwindDest is null at this point, either the pad in question can
826 // be exited by unwind to caller, or it cannot be exited by unwind. In
827 // either case, reporting such cases as unwinding to caller is correct.
828 // This can lead to EH tables that "look strange" -- if this pad's is in
829 // a parent funclet which has other children that do unwind to an enclosing
830 // pad, the try region for this pad will be missing the "duplicate" EH
831 // clause entries that you'd expect to see covering the whole parent. That
832 // should be benign, since the unwind never actually happens. If it were
833 // an issue, we could add a subsequent pass that pushes unwind dests down
834 // from parents that have them to children that appear to unwind to caller.
836 UnwindDestState
= -1;
838 UnwindDestState
= FuncInfo
.EHPadStateMap
[UnwindDest
->getFirstNonPHI()];
841 Entry
.TryParentState
= UnwindDestState
;
844 // Step three: transfer information from pads to invokes.
845 calculateStateNumbersForInvokes(Fn
, FuncInfo
);
848 void WinEHPrepareImpl::colorFunclets(Function
&F
) {
849 BlockColors
= colorEHFunclets(F
);
851 // Invert the map from BB to colors to color to BBs.
852 for (BasicBlock
&BB
: F
) {
853 ColorVector
&Colors
= BlockColors
[&BB
];
854 for (BasicBlock
*Color
: Colors
)
855 FuncletBlocks
[Color
].push_back(&BB
);
859 void WinEHPrepareImpl::demotePHIsOnFunclets(Function
&F
,
860 bool DemoteCatchSwitchPHIOnly
) {
861 // Strip PHI nodes off of EH pads.
862 SmallVector
<PHINode
*, 16> PHINodes
;
863 for (BasicBlock
&BB
: make_early_inc_range(F
)) {
866 if (DemoteCatchSwitchPHIOnly
&& !isa
<CatchSwitchInst
>(BB
.getFirstNonPHI()))
869 for (Instruction
&I
: make_early_inc_range(BB
)) {
870 auto *PN
= dyn_cast
<PHINode
>(&I
);
871 // Stop at the first non-PHI.
875 AllocaInst
*SpillSlot
= insertPHILoads(PN
, F
);
877 insertPHIStores(PN
, SpillSlot
);
879 PHINodes
.push_back(PN
);
883 for (auto *PN
: PHINodes
) {
884 // There may be lingering uses on other EH PHIs being removed
885 PN
->replaceAllUsesWith(PoisonValue::get(PN
->getType()));
886 PN
->eraseFromParent();
890 void WinEHPrepareImpl::cloneCommonBlocks(Function
&F
) {
891 // We need to clone all blocks which belong to multiple funclets. Values are
892 // remapped throughout the funclet to propagate both the new instructions
893 // *and* the new basic blocks themselves.
894 for (auto &Funclets
: FuncletBlocks
) {
895 BasicBlock
*FuncletPadBB
= Funclets
.first
;
896 std::vector
<BasicBlock
*> &BlocksInFunclet
= Funclets
.second
;
898 if (FuncletPadBB
== &F
.getEntryBlock())
899 FuncletToken
= ConstantTokenNone::get(F
.getContext());
901 FuncletToken
= FuncletPadBB
->getFirstNonPHI();
903 std::vector
<std::pair
<BasicBlock
*, BasicBlock
*>> Orig2Clone
;
904 ValueToValueMapTy VMap
;
905 for (BasicBlock
*BB
: BlocksInFunclet
) {
906 ColorVector
&ColorsForBB
= BlockColors
[BB
];
907 // We don't need to do anything if the block is monochromatic.
908 size_t NumColorsForBB
= ColorsForBB
.size();
909 if (NumColorsForBB
== 1)
912 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
913 dbgs() << " Cloning block \'" << BB
->getName()
914 << "\' for funclet \'" << FuncletPadBB
->getName()
917 // Create a new basic block and copy instructions into it!
919 CloneBasicBlock(BB
, VMap
, Twine(".for.", FuncletPadBB
->getName()));
920 // Insert the clone immediately after the original to ensure determinism
921 // and to keep the same relative ordering of any funclet's blocks.
922 CBB
->insertInto(&F
, BB
->getNextNode());
924 // Add basic block mapping.
927 // Record delta operations that we need to perform to our color mappings.
928 Orig2Clone
.emplace_back(BB
, CBB
);
931 // If nothing was cloned, we're done cloning in this funclet.
932 if (Orig2Clone
.empty())
935 // Update our color mappings to reflect that one block has lost a color and
936 // another has gained a color.
937 for (auto &BBMapping
: Orig2Clone
) {
938 BasicBlock
*OldBlock
= BBMapping
.first
;
939 BasicBlock
*NewBlock
= BBMapping
.second
;
941 BlocksInFunclet
.push_back(NewBlock
);
942 ColorVector
&NewColors
= BlockColors
[NewBlock
];
943 assert(NewColors
.empty() && "A new block should only have one color!");
944 NewColors
.push_back(FuncletPadBB
);
946 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
947 dbgs() << " Assigned color \'" << FuncletPadBB
->getName()
948 << "\' to block \'" << NewBlock
->getName()
951 llvm::erase(BlocksInFunclet
, OldBlock
);
952 ColorVector
&OldColors
= BlockColors
[OldBlock
];
953 llvm::erase(OldColors
, FuncletPadBB
);
955 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
956 dbgs() << " Removed color \'" << FuncletPadBB
->getName()
957 << "\' from block \'" << OldBlock
->getName()
961 // Loop over all of the instructions in this funclet, fixing up operand
962 // references as we go. This uses VMap to do all the hard work.
963 for (BasicBlock
*BB
: BlocksInFunclet
)
964 // Loop over all instructions, fixing each one as we find it...
965 for (Instruction
&I
: *BB
)
966 RemapInstruction(&I
, VMap
,
967 RF_IgnoreMissingLocals
| RF_NoModuleLevelChanges
);
969 // Catchrets targeting cloned blocks need to be updated separately from
970 // the loop above because they are not in the current funclet.
971 SmallVector
<CatchReturnInst
*, 2> FixupCatchrets
;
972 for (auto &BBMapping
: Orig2Clone
) {
973 BasicBlock
*OldBlock
= BBMapping
.first
;
974 BasicBlock
*NewBlock
= BBMapping
.second
;
976 FixupCatchrets
.clear();
977 for (BasicBlock
*Pred
: predecessors(OldBlock
))
978 if (auto *CatchRet
= dyn_cast
<CatchReturnInst
>(Pred
->getTerminator()))
979 if (CatchRet
->getCatchSwitchParentPad() == FuncletToken
)
980 FixupCatchrets
.push_back(CatchRet
);
982 for (CatchReturnInst
*CatchRet
: FixupCatchrets
)
983 CatchRet
->setSuccessor(NewBlock
);
986 auto UpdatePHIOnClonedBlock
= [&](PHINode
*PN
, bool IsForOldBlock
) {
987 unsigned NumPreds
= PN
->getNumIncomingValues();
988 for (unsigned PredIdx
= 0, PredEnd
= NumPreds
; PredIdx
!= PredEnd
;
990 BasicBlock
*IncomingBlock
= PN
->getIncomingBlock(PredIdx
);
991 bool EdgeTargetsFunclet
;
993 dyn_cast
<CatchReturnInst
>(IncomingBlock
->getTerminator())) {
994 EdgeTargetsFunclet
= (CRI
->getCatchSwitchParentPad() == FuncletToken
);
996 ColorVector
&IncomingColors
= BlockColors
[IncomingBlock
];
997 assert(!IncomingColors
.empty() && "Block not colored!");
998 assert((IncomingColors
.size() == 1 ||
999 !llvm::is_contained(IncomingColors
, FuncletPadBB
)) &&
1000 "Cloning should leave this funclet's blocks monochromatic");
1001 EdgeTargetsFunclet
= (IncomingColors
.front() == FuncletPadBB
);
1003 if (IsForOldBlock
!= EdgeTargetsFunclet
)
1005 PN
->removeIncomingValue(IncomingBlock
, /*DeletePHIIfEmpty=*/false);
1006 // Revisit the next entry.
1012 for (auto &BBMapping
: Orig2Clone
) {
1013 BasicBlock
*OldBlock
= BBMapping
.first
;
1014 BasicBlock
*NewBlock
= BBMapping
.second
;
1015 for (PHINode
&OldPN
: OldBlock
->phis()) {
1016 UpdatePHIOnClonedBlock(&OldPN
, /*IsForOldBlock=*/true);
1018 for (PHINode
&NewPN
: NewBlock
->phis()) {
1019 UpdatePHIOnClonedBlock(&NewPN
, /*IsForOldBlock=*/false);
1023 // Check to see if SuccBB has PHI nodes. If so, we need to add entries to
1024 // the PHI nodes for NewBB now.
1025 for (auto &BBMapping
: Orig2Clone
) {
1026 BasicBlock
*OldBlock
= BBMapping
.first
;
1027 BasicBlock
*NewBlock
= BBMapping
.second
;
1028 for (BasicBlock
*SuccBB
: successors(NewBlock
)) {
1029 for (PHINode
&SuccPN
: SuccBB
->phis()) {
1030 // Ok, we have a PHI node. Figure out what the incoming value was for
1032 int OldBlockIdx
= SuccPN
.getBasicBlockIndex(OldBlock
);
1033 if (OldBlockIdx
== -1)
1035 Value
*IV
= SuccPN
.getIncomingValue(OldBlockIdx
);
1037 // Remap the value if necessary.
1038 if (auto *Inst
= dyn_cast
<Instruction
>(IV
)) {
1039 ValueToValueMapTy::iterator I
= VMap
.find(Inst
);
1040 if (I
!= VMap
.end())
1044 SuccPN
.addIncoming(IV
, NewBlock
);
1049 for (ValueToValueMapTy::value_type VT
: VMap
) {
1050 // If there were values defined in BB that are used outside the funclet,
1051 // then we now have to update all uses of the value to use either the
1052 // original value, the cloned value, or some PHI derived value. This can
1053 // require arbitrary PHI insertion, of which we are prepared to do, clean
1055 SmallVector
<Use
*, 16> UsesToRename
;
1057 auto *OldI
= dyn_cast
<Instruction
>(const_cast<Value
*>(VT
.first
));
1060 auto *NewI
= cast
<Instruction
>(VT
.second
);
1061 // Scan all uses of this instruction to see if it is used outside of its
1062 // funclet, and if so, record them in UsesToRename.
1063 for (Use
&U
: OldI
->uses()) {
1064 Instruction
*UserI
= cast
<Instruction
>(U
.getUser());
1065 BasicBlock
*UserBB
= UserI
->getParent();
1066 ColorVector
&ColorsForUserBB
= BlockColors
[UserBB
];
1067 assert(!ColorsForUserBB
.empty());
1068 if (ColorsForUserBB
.size() > 1 ||
1069 *ColorsForUserBB
.begin() != FuncletPadBB
)
1070 UsesToRename
.push_back(&U
);
1073 // If there are no uses outside the block, we're done with this
1075 if (UsesToRename
.empty())
1078 // We found a use of OldI outside of the funclet. Rename all uses of OldI
1079 // that are outside its funclet to be uses of the appropriate PHI node
1081 SSAUpdater SSAUpdate
;
1082 SSAUpdate
.Initialize(OldI
->getType(), OldI
->getName());
1083 SSAUpdate
.AddAvailableValue(OldI
->getParent(), OldI
);
1084 SSAUpdate
.AddAvailableValue(NewI
->getParent(), NewI
);
1086 while (!UsesToRename
.empty())
1087 SSAUpdate
.RewriteUseAfterInsertions(*UsesToRename
.pop_back_val());
1092 void WinEHPrepareImpl::removeImplausibleInstructions(Function
&F
) {
1093 // Remove implausible terminators and replace them with UnreachableInst.
1094 for (auto &Funclet
: FuncletBlocks
) {
1095 BasicBlock
*FuncletPadBB
= Funclet
.first
;
1096 std::vector
<BasicBlock
*> &BlocksInFunclet
= Funclet
.second
;
1097 Instruction
*FirstNonPHI
= FuncletPadBB
->getFirstNonPHI();
1098 auto *FuncletPad
= dyn_cast
<FuncletPadInst
>(FirstNonPHI
);
1099 auto *CatchPad
= dyn_cast_or_null
<CatchPadInst
>(FuncletPad
);
1100 auto *CleanupPad
= dyn_cast_or_null
<CleanupPadInst
>(FuncletPad
);
1102 for (BasicBlock
*BB
: BlocksInFunclet
) {
1103 for (Instruction
&I
: *BB
) {
1104 auto *CB
= dyn_cast
<CallBase
>(&I
);
1108 Value
*FuncletBundleOperand
= nullptr;
1109 if (auto BU
= CB
->getOperandBundle(LLVMContext::OB_funclet
))
1110 FuncletBundleOperand
= BU
->Inputs
.front();
1112 if (FuncletBundleOperand
== FuncletPad
)
1115 // Skip call sites which are nounwind intrinsics or inline asm.
1117 dyn_cast
<Function
>(CB
->getCalledOperand()->stripPointerCasts());
1118 if (CalledFn
&& ((CalledFn
->isIntrinsic() && CB
->doesNotThrow()) ||
1122 // This call site was not part of this funclet, remove it.
1123 if (isa
<InvokeInst
>(CB
)) {
1124 // Remove the unwind edge if it was an invoke.
1125 removeUnwindEdge(BB
);
1126 // Get a pointer to the new call.
1127 BasicBlock::iterator CallI
=
1128 std::prev(BB
->getTerminator()->getIterator());
1129 auto *CI
= cast
<CallInst
>(&*CallI
);
1130 changeToUnreachable(CI
);
1132 changeToUnreachable(&I
);
1135 // There are no more instructions in the block (except for unreachable),
1140 Instruction
*TI
= BB
->getTerminator();
1141 // CatchPadInst and CleanupPadInst can't transfer control to a ReturnInst.
1142 bool IsUnreachableRet
= isa
<ReturnInst
>(TI
) && FuncletPad
;
1143 // The token consumed by a CatchReturnInst must match the funclet token.
1144 bool IsUnreachableCatchret
= false;
1145 if (auto *CRI
= dyn_cast
<CatchReturnInst
>(TI
))
1146 IsUnreachableCatchret
= CRI
->getCatchPad() != CatchPad
;
1147 // The token consumed by a CleanupReturnInst must match the funclet token.
1148 bool IsUnreachableCleanupret
= false;
1149 if (auto *CRI
= dyn_cast
<CleanupReturnInst
>(TI
))
1150 IsUnreachableCleanupret
= CRI
->getCleanupPad() != CleanupPad
;
1151 if (IsUnreachableRet
|| IsUnreachableCatchret
||
1152 IsUnreachableCleanupret
) {
1153 changeToUnreachable(TI
);
1154 } else if (isa
<InvokeInst
>(TI
)) {
1155 if (Personality
== EHPersonality::MSVC_CXX
&& CleanupPad
) {
1156 // Invokes within a cleanuppad for the MSVC++ personality never
1157 // transfer control to their unwind edge: the personality will
1158 // terminate the program.
1159 removeUnwindEdge(BB
);
1166 void WinEHPrepareImpl::cleanupPreparedFunclets(Function
&F
) {
1167 // Clean-up some of the mess we made by removing useles PHI nodes, trivial
1169 for (BasicBlock
&BB
: llvm::make_early_inc_range(F
)) {
1170 SimplifyInstructionsInBlock(&BB
);
1171 ConstantFoldTerminator(&BB
, /*DeleteDeadConditions=*/true);
1172 MergeBlockIntoPredecessor(&BB
);
1175 // We might have some unreachable blocks after cleaning up some impossible
1177 removeUnreachableBlocks(F
);
1181 void WinEHPrepareImpl::verifyPreparedFunclets(Function
&F
) {
1182 for (BasicBlock
&BB
: F
) {
1183 size_t NumColors
= BlockColors
[&BB
].size();
1184 assert(NumColors
== 1 && "Expected monochromatic BB!");
1186 report_fatal_error("Uncolored BB!");
1188 report_fatal_error("Multicolor BB!");
1189 assert((DisableDemotion
|| !(BB
.isEHPad() && isa
<PHINode
>(BB
.begin()))) &&
1190 "EH Pad still has a PHI!");
1195 bool WinEHPrepareImpl::prepareExplicitEH(Function
&F
) {
1196 // Remove unreachable blocks. It is not valuable to assign them a color and
1197 // their existence can trick us into thinking values are alive when they are
1199 removeUnreachableBlocks(F
);
1201 // Determine which blocks are reachable from which funclet entries.
1204 cloneCommonBlocks(F
);
1206 if (!DisableDemotion
)
1207 demotePHIsOnFunclets(F
, DemoteCatchSwitchPHIOnly
||
1208 DemoteCatchSwitchPHIOnlyOpt
);
1210 if (!DisableCleanups
) {
1211 assert(!verifyFunction(F
, &dbgs()));
1212 removeImplausibleInstructions(F
);
1214 assert(!verifyFunction(F
, &dbgs()));
1215 cleanupPreparedFunclets(F
);
1218 LLVM_DEBUG(verifyPreparedFunclets(F
));
1219 // Recolor the CFG to verify that all is well.
1220 LLVM_DEBUG(colorFunclets(F
));
1221 LLVM_DEBUG(verifyPreparedFunclets(F
));
1226 // TODO: Share loads when one use dominates another, or when a catchpad exit
1227 // dominates uses (needs dominators).
1228 AllocaInst
*WinEHPrepareImpl::insertPHILoads(PHINode
*PN
, Function
&F
) {
1229 BasicBlock
*PHIBlock
= PN
->getParent();
1230 AllocaInst
*SpillSlot
= nullptr;
1231 Instruction
*EHPad
= PHIBlock
->getFirstNonPHI();
1233 if (!EHPad
->isTerminator()) {
1234 // If the EHPad isn't a terminator, then we can insert a load in this block
1235 // that will dominate all uses.
1236 SpillSlot
= new AllocaInst(PN
->getType(), DL
->getAllocaAddrSpace(), nullptr,
1237 Twine(PN
->getName(), ".wineh.spillslot"),
1238 F
.getEntryBlock().begin());
1239 Value
*V
= new LoadInst(PN
->getType(), SpillSlot
,
1240 Twine(PN
->getName(), ".wineh.reload"),
1241 PHIBlock
->getFirstInsertionPt());
1242 PN
->replaceAllUsesWith(V
);
1246 // Otherwise, we have a PHI on a terminator EHPad, and we give up and insert
1247 // loads of the slot before every use.
1248 DenseMap
<BasicBlock
*, Value
*> Loads
;
1249 for (Use
&U
: llvm::make_early_inc_range(PN
->uses())) {
1250 auto *UsingInst
= cast
<Instruction
>(U
.getUser());
1251 if (isa
<PHINode
>(UsingInst
) && UsingInst
->getParent()->isEHPad()) {
1252 // Use is on an EH pad phi. Leave it alone; we'll insert loads and
1253 // stores for it separately.
1256 replaceUseWithLoad(PN
, U
, SpillSlot
, Loads
, F
);
1261 // TODO: improve store placement. Inserting at def is probably good, but need
1262 // to be careful not to introduce interfering stores (needs liveness analysis).
1263 // TODO: identify related phi nodes that can share spill slots, and share them
1264 // (also needs liveness).
1265 void WinEHPrepareImpl::insertPHIStores(PHINode
*OriginalPHI
,
1266 AllocaInst
*SpillSlot
) {
1267 // Use a worklist of (Block, Value) pairs -- the given Value needs to be
1268 // stored to the spill slot by the end of the given Block.
1269 SmallVector
<std::pair
<BasicBlock
*, Value
*>, 4> Worklist
;
1271 Worklist
.push_back({OriginalPHI
->getParent(), OriginalPHI
});
1273 while (!Worklist
.empty()) {
1274 BasicBlock
*EHBlock
;
1276 std::tie(EHBlock
, InVal
) = Worklist
.pop_back_val();
1278 PHINode
*PN
= dyn_cast
<PHINode
>(InVal
);
1279 if (PN
&& PN
->getParent() == EHBlock
) {
1280 // The value is defined by another PHI we need to remove, with no room to
1281 // insert a store after the PHI, so each predecessor needs to store its
1283 for (unsigned i
= 0, e
= PN
->getNumIncomingValues(); i
< e
; ++i
) {
1284 Value
*PredVal
= PN
->getIncomingValue(i
);
1286 // Undef can safely be skipped.
1287 if (isa
<UndefValue
>(PredVal
))
1290 insertPHIStore(PN
->getIncomingBlock(i
), PredVal
, SpillSlot
, Worklist
);
1293 // We need to store InVal, which dominates EHBlock, but can't put a store
1294 // in EHBlock, so need to put stores in each predecessor.
1295 for (BasicBlock
*PredBlock
: predecessors(EHBlock
)) {
1296 insertPHIStore(PredBlock
, InVal
, SpillSlot
, Worklist
);
1302 void WinEHPrepareImpl::insertPHIStore(
1303 BasicBlock
*PredBlock
, Value
*PredVal
, AllocaInst
*SpillSlot
,
1304 SmallVectorImpl
<std::pair
<BasicBlock
*, Value
*>> &Worklist
) {
1306 if (PredBlock
->isEHPad() && PredBlock
->getFirstNonPHI()->isTerminator()) {
1307 // Pred is unsplittable, so we need to queue it on the worklist.
1308 Worklist
.push_back({PredBlock
, PredVal
});
1312 // Otherwise, insert the store at the end of the basic block.
1313 new StoreInst(PredVal
, SpillSlot
, PredBlock
->getTerminator()->getIterator());
1316 void WinEHPrepareImpl::replaceUseWithLoad(
1317 Value
*V
, Use
&U
, AllocaInst
*&SpillSlot
,
1318 DenseMap
<BasicBlock
*, Value
*> &Loads
, Function
&F
) {
1319 // Lazilly create the spill slot.
1321 SpillSlot
= new AllocaInst(V
->getType(), DL
->getAllocaAddrSpace(), nullptr,
1322 Twine(V
->getName(), ".wineh.spillslot"),
1323 F
.getEntryBlock().begin());
1325 auto *UsingInst
= cast
<Instruction
>(U
.getUser());
1326 if (auto *UsingPHI
= dyn_cast
<PHINode
>(UsingInst
)) {
1327 // If this is a PHI node, we can't insert a load of the value before
1328 // the use. Instead insert the load in the predecessor block
1329 // corresponding to the incoming value.
1331 // Note that if there are multiple edges from a basic block to this
1332 // PHI node that we cannot have multiple loads. The problem is that
1333 // the resulting PHI node will have multiple values (from each load)
1334 // coming in from the same block, which is illegal SSA form.
1335 // For this reason, we keep track of and reuse loads we insert.
1336 BasicBlock
*IncomingBlock
= UsingPHI
->getIncomingBlock(U
);
1337 if (auto *CatchRet
=
1338 dyn_cast
<CatchReturnInst
>(IncomingBlock
->getTerminator())) {
1339 // Putting a load above a catchret and use on the phi would still leave
1340 // a cross-funclet def/use. We need to split the edge, change the
1341 // catchret to target the new block, and put the load there.
1342 BasicBlock
*PHIBlock
= UsingInst
->getParent();
1343 BasicBlock
*NewBlock
= SplitEdge(IncomingBlock
, PHIBlock
);
1344 // SplitEdge gives us:
1347 // br label %NewBlock
1349 // catchret label %PHIBlock
1353 // catchret label %NewBlock
1355 // br label %PHIBlock
1356 // So move the terminators to each others' blocks and swap their
1358 BranchInst
*Goto
= cast
<BranchInst
>(IncomingBlock
->getTerminator());
1359 Goto
->removeFromParent();
1360 CatchRet
->removeFromParent();
1361 CatchRet
->insertInto(IncomingBlock
, IncomingBlock
->end());
1362 Goto
->insertInto(NewBlock
, NewBlock
->end());
1363 Goto
->setSuccessor(0, PHIBlock
);
1364 CatchRet
->setSuccessor(NewBlock
);
1365 // Update the color mapping for the newly split edge.
1366 // Grab a reference to the ColorVector to be inserted before getting the
1367 // reference to the vector we are copying because inserting the new
1368 // element in BlockColors might cause the map to be reallocated.
1369 ColorVector
&ColorsForNewBlock
= BlockColors
[NewBlock
];
1370 ColorVector
&ColorsForPHIBlock
= BlockColors
[PHIBlock
];
1371 ColorsForNewBlock
= ColorsForPHIBlock
;
1372 for (BasicBlock
*FuncletPad
: ColorsForPHIBlock
)
1373 FuncletBlocks
[FuncletPad
].push_back(NewBlock
);
1374 // Treat the new block as incoming for load insertion.
1375 IncomingBlock
= NewBlock
;
1377 Value
*&Load
= Loads
[IncomingBlock
];
1378 // Insert the load into the predecessor block
1380 Load
= new LoadInst(
1381 V
->getType(), SpillSlot
, Twine(V
->getName(), ".wineh.reload"),
1382 /*isVolatile=*/false, IncomingBlock
->getTerminator()->getIterator());
1386 // Reload right before the old use.
1387 auto *Load
= new LoadInst(V
->getType(), SpillSlot
,
1388 Twine(V
->getName(), ".wineh.reload"),
1389 /*isVolatile=*/false, UsingInst
->getIterator());
1394 void WinEHFuncInfo::addIPToStateRange(const InvokeInst
*II
,
1395 MCSymbol
*InvokeBegin
,
1396 MCSymbol
*InvokeEnd
) {
1397 assert(InvokeStateMap
.count(II
) &&
1398 "should get invoke with precomputed state");
1399 LabelToStateMap
[InvokeBegin
] = std::make_pair(InvokeStateMap
[II
], InvokeEnd
);
1402 void WinEHFuncInfo::addIPToStateRange(int State
, MCSymbol
* InvokeBegin
,
1403 MCSymbol
* InvokeEnd
) {
1404 LabelToStateMap
[InvokeBegin
] = std::make_pair(State
, InvokeEnd
);
1407 WinEHFuncInfo::WinEHFuncInfo() = default;