1//===- EHPersonalities.cpp - Compute EH-related information ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#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"
19using namespace llvm;
20
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.
23EHPersonality llvm::classifyEHPersonality(const Value *Pers) {
24 const GlobalValue *F =
25 Pers ? dyn_cast<GlobalValue>(Val: Pers->stripPointerCasts()) : nullptr;
26 if (!F || !F->getValueType() || !F->getValueType()->isFunctionTy())
27 return EHPersonality::Unknown;
28 return StringSwitch<EHPersonality>(F->getName())
29 .Case(S: "__gnat_eh_personality", Value: EHPersonality::GNU_Ada)
30 .Case(S: "__gxx_personality_v0", Value: EHPersonality::GNU_CXX)
31 .Case(S: "__gxx_personality_seh0", Value: EHPersonality::GNU_CXX)
32 .Case(S: "__gxx_personality_sj0", Value: EHPersonality::GNU_CXX_SjLj)
33 .Case(S: "__gcc_personality_v0", Value: EHPersonality::GNU_C)
34 .Case(S: "__gcc_personality_seh0", Value: EHPersonality::GNU_C)
35 .Case(S: "__gcc_personality_sj0", Value: EHPersonality::GNU_C_SjLj)
36 .Case(S: "__objc_personality_v0", Value: EHPersonality::GNU_ObjC)
37 .Case(S: "_except_handler3", Value: EHPersonality::MSVC_X86SEH)
38 .Case(S: "_except_handler4", Value: EHPersonality::MSVC_X86SEH)
39 .Case(S: "__C_specific_handler", Value: EHPersonality::MSVC_TableSEH)
40 .Case(S: "__CxxFrameHandler3", Value: EHPersonality::MSVC_CXX)
41 .Case(S: "ProcessCLRException", Value: EHPersonality::CoreCLR)
42 .Case(S: "rust_eh_personality", Value: EHPersonality::Rust)
43 .Case(S: "__gxx_wasm_personality_v0", Value: EHPersonality::Wasm_CXX)
44 .Case(S: "__xlcxx_personality_v1", Value: EHPersonality::XL_CXX)
45 .Case(S: "__zos_cxx_personality_v2", Value: EHPersonality::ZOS_CXX)
46 .Default(Value: EHPersonality::Unknown);
47}
48
49StringRef llvm::getEHPersonalityName(EHPersonality Pers) {
50 switch (Pers) {
51 case EHPersonality::GNU_Ada:
52 return "__gnat_eh_personality";
53 case EHPersonality::GNU_CXX:
54 return "__gxx_personality_v0";
55 case EHPersonality::GNU_CXX_SjLj:
56 return "__gxx_personality_sj0";
57 case EHPersonality::GNU_C:
58 return "__gcc_personality_v0";
59 case EHPersonality::GNU_C_SjLj:
60 return "__gcc_personality_sj0";
61 case EHPersonality::GNU_ObjC:
62 return "__objc_personality_v0";
63 case EHPersonality::MSVC_X86SEH:
64 return "_except_handler3";
65 case EHPersonality::MSVC_TableSEH:
66 return "__C_specific_handler";
67 case EHPersonality::MSVC_CXX:
68 return "__CxxFrameHandler3";
69 case EHPersonality::CoreCLR:
70 return "ProcessCLRException";
71 case EHPersonality::Rust:
72 return "rust_eh_personality";
73 case EHPersonality::Wasm_CXX:
74 return "__gxx_wasm_personality_v0";
75 case EHPersonality::XL_CXX:
76 return "__xlcxx_personality_v1";
77 case EHPersonality::ZOS_CXX:
78 return "__zos_cxx_personality_v2";
79 case EHPersonality::Unknown:
80 llvm_unreachable("Unknown EHPersonality!");
81 }
82
83 llvm_unreachable("Invalid EHPersonality!");
84}
85
86EHPersonality llvm::getDefaultEHPersonality(const Triple &T) {
87 if (T.isPS5())
88 return EHPersonality::GNU_CXX;
89 else
90 return EHPersonality::GNU_C;
91}
92
93bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
94 EHPersonality Personality = classifyEHPersonality(Pers: F->getPersonalityFn());
95 // We can't simplify any invokes to nounwind functions if the personality
96 // function wants to catch asynch exceptions. The nounwind attribute only
97 // implies that the function does not throw synchronous exceptions.
98
99 // Cannot simplify CXX Personality under AsynchEH
100 const llvm::Module *M = (const llvm::Module *)F->getParent();
101 bool EHa = M->getModuleFlag(Key: "eh-asynch");
102 return !EHa && !isAsynchronousEHPersonality(Pers: Personality);
103}
104
105DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) {
106 SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
107 BasicBlock *EntryBlock = &F.getEntryBlock();
108 DenseMap<BasicBlock *, ColorVector> BlockColors;
109
110 // Build up the color map, which maps each block to its set of 'colors'.
111 // For any block B the "colors" of B are the set of funclets F (possibly
112 // including a root "funclet" representing the main function) such that
113 // F will need to directly contain B or a copy of B (where the term "directly
114 // contain" is used to distinguish from being "transitively contained" in
115 // a nested funclet).
116 //
117 // Note: Despite not being a funclet in the truest sense, a catchswitch is
118 // considered to belong to its own funclet for the purposes of coloring.
119
120 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
121 dbgs() << "\nColoring funclets for " << F.getName() << "\n");
122
123 Worklist.push_back(Elt: {EntryBlock, EntryBlock});
124
125 while (!Worklist.empty()) {
126 BasicBlock *Visiting;
127 BasicBlock *Color;
128 std::tie(args&: Visiting, args&: Color) = Worklist.pop_back_val();
129 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
130 dbgs() << "Visiting " << Visiting->getName() << ", "
131 << Color->getName() << "\n");
132 Instruction *VisitingHead = Visiting->getFirstNonPHI();
133 if (VisitingHead->isEHPad()) {
134 // Mark this funclet head as a member of itself.
135 Color = Visiting;
136 }
137 // Note that this is a member of the given color.
138 ColorVector &Colors = BlockColors[Visiting];
139 if (!is_contained(Range&: Colors, Element: Color))
140 Colors.push_back(NewVal: Color);
141 else
142 continue;
143
144 DEBUG_WITH_TYPE("win-eh-prepare-coloring",
145 dbgs() << " Assigned color \'" << Color->getName()
146 << "\' to block \'" << Visiting->getName()
147 << "\'.\n");
148
149 BasicBlock *SuccColor = Color;
150 Instruction *Terminator = Visiting->getTerminator();
151 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Val: Terminator)) {
152 Value *ParentPad = CatchRet->getCatchSwitchParentPad();
153 if (isa<ConstantTokenNone>(Val: ParentPad))
154 SuccColor = EntryBlock;
155 else
156 SuccColor = cast<Instruction>(Val: ParentPad)->getParent();
157 }
158
159 for (BasicBlock *Succ : successors(BB: Visiting))
160 Worklist.push_back(Elt: {Succ, SuccColor});
161 }
162 return BlockColors;
163}
164