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