1 //===- EHPersonalities.cpp - Compute EH-related information ---------------===//
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 #include "llvm/IR/EHPersonalities.h"
10 #include "llvm/ADT/StringSwitch.h"
11 #include "llvm/ADT/Triple.h"
12 #include "llvm/IR/CFG.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/Function.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/raw_ostream.h"
20 /// See if the given exception handling personality function is one that we
21 /// understand. If so, return a description of it; otherwise return Unknown.
22 EHPersonality
llvm::classifyEHPersonality(const Value
*Pers
) {
23 const GlobalValue
*F
=
24 Pers
? dyn_cast
<GlobalValue
>(Pers
->stripPointerCasts()) : nullptr;
25 if (!F
|| !F
->getValueType() || !F
->getValueType()->isFunctionTy())
26 return EHPersonality::Unknown
;
27 return StringSwitch
<EHPersonality
>(F
->getName())
28 .Case("__gnat_eh_personality", EHPersonality::GNU_Ada
)
29 .Case("__gxx_personality_v0", EHPersonality::GNU_CXX
)
30 .Case("__gxx_personality_seh0", EHPersonality::GNU_CXX
)
31 .Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj
)
32 .Case("__gcc_personality_v0", EHPersonality::GNU_C
)
33 .Case("__gcc_personality_seh0", EHPersonality::GNU_C
)
34 .Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj
)
35 .Case("__objc_personality_v0", EHPersonality::GNU_ObjC
)
36 .Case("_except_handler3", EHPersonality::MSVC_X86SEH
)
37 .Case("_except_handler4", EHPersonality::MSVC_X86SEH
)
38 .Case("__C_specific_handler", EHPersonality::MSVC_TableSEH
)
39 .Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX
)
40 .Case("ProcessCLRException", EHPersonality::CoreCLR
)
41 .Case("rust_eh_personality", EHPersonality::Rust
)
42 .Case("__gxx_wasm_personality_v0", EHPersonality::Wasm_CXX
)
43 .Case("__xlcxx_personality_v1", EHPersonality::XL_CXX
)
44 .Default(EHPersonality::Unknown
);
47 StringRef
llvm::getEHPersonalityName(EHPersonality Pers
) {
49 case EHPersonality::GNU_Ada
:
50 return "__gnat_eh_personality";
51 case EHPersonality::GNU_CXX
:
52 return "__gxx_personality_v0";
53 case EHPersonality::GNU_CXX_SjLj
:
54 return "__gxx_personality_sj0";
55 case EHPersonality::GNU_C
:
56 return "__gcc_personality_v0";
57 case EHPersonality::GNU_C_SjLj
:
58 return "__gcc_personality_sj0";
59 case EHPersonality::GNU_ObjC
:
60 return "__objc_personality_v0";
61 case EHPersonality::MSVC_X86SEH
:
62 return "_except_handler3";
63 case EHPersonality::MSVC_TableSEH
:
64 return "__C_specific_handler";
65 case EHPersonality::MSVC_CXX
:
66 return "__CxxFrameHandler3";
67 case EHPersonality::CoreCLR
:
68 return "ProcessCLRException";
69 case EHPersonality::Rust
:
70 return "rust_eh_personality";
71 case EHPersonality::Wasm_CXX
:
72 return "__gxx_wasm_personality_v0";
73 case EHPersonality::XL_CXX
:
74 return "__xlcxx_personality_v1";
75 case EHPersonality::Unknown
:
76 llvm_unreachable("Unknown EHPersonality!");
79 llvm_unreachable("Invalid EHPersonality!");
82 EHPersonality
llvm::getDefaultEHPersonality(const Triple
&T
) {
84 return EHPersonality::GNU_CXX
;
86 return EHPersonality::GNU_C
;
89 bool llvm::canSimplifyInvokeNoUnwind(const Function
*F
) {
90 EHPersonality Personality
= classifyEHPersonality(F
->getPersonalityFn());
91 // We can't simplify any invokes to nounwind functions if the personality
92 // function wants to catch asynch exceptions. The nounwind attribute only
93 // implies that the function does not throw synchronous exceptions.
94 return !isAsynchronousEHPersonality(Personality
);
97 DenseMap
<BasicBlock
*, ColorVector
> llvm::colorEHFunclets(Function
&F
) {
98 SmallVector
<std::pair
<BasicBlock
*, BasicBlock
*>, 16> Worklist
;
99 BasicBlock
*EntryBlock
= &F
.getEntryBlock();
100 DenseMap
<BasicBlock
*, ColorVector
> BlockColors
;
102 // Build up the color map, which maps each block to its set of 'colors'.
103 // For any block B the "colors" of B are the set of funclets F (possibly
104 // including a root "funclet" representing the main function) such that
105 // F will need to directly contain B or a copy of B (where the term "directly
106 // contain" is used to distinguish from being "transitively contained" in
107 // a nested funclet).
109 // Note: Despite not being a funclet in the truest sense, a catchswitch is
110 // considered to belong to its own funclet for the purposes of coloring.
112 DEBUG_WITH_TYPE("winehprepare-coloring",
113 dbgs() << "\nColoring funclets for " << F
.getName() << "\n");
115 Worklist
.push_back({EntryBlock
, EntryBlock
});
117 while (!Worklist
.empty()) {
118 BasicBlock
*Visiting
;
120 std::tie(Visiting
, Color
) = Worklist
.pop_back_val();
121 DEBUG_WITH_TYPE("winehprepare-coloring",
122 dbgs() << "Visiting " << Visiting
->getName() << ", "
123 << Color
->getName() << "\n");
124 Instruction
*VisitingHead
= Visiting
->getFirstNonPHI();
125 if (VisitingHead
->isEHPad()) {
126 // Mark this funclet head as a member of itself.
129 // Note that this is a member of the given color.
130 ColorVector
&Colors
= BlockColors
[Visiting
];
131 if (!is_contained(Colors
, Color
))
132 Colors
.push_back(Color
);
136 DEBUG_WITH_TYPE("winehprepare-coloring",
137 dbgs() << " Assigned color \'" << Color
->getName()
138 << "\' to block \'" << Visiting
->getName()
141 BasicBlock
*SuccColor
= Color
;
142 Instruction
*Terminator
= Visiting
->getTerminator();
143 if (auto *CatchRet
= dyn_cast
<CatchReturnInst
>(Terminator
)) {
144 Value
*ParentPad
= CatchRet
->getCatchSwitchParentPad();
145 if (isa
<ConstantTokenNone
>(ParentPad
))
146 SuccColor
= EntryBlock
;
148 SuccColor
= cast
<Instruction
>(ParentPad
)->getParent();
151 for (BasicBlock
*Succ
: successors(Visiting
))
152 Worklist
.push_back({Succ
, SuccColor
});