LLVM 17.0.0git
EHPersonalities.cpp
Go to the documentation of this file.
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
11#include "llvm/ADT/Triple.h"
12#include "llvm/IR/CFG.h"
13#include "llvm/IR/Constants.h"
14#include "llvm/IR/Function.h"
16#include "llvm/Support/Debug.h"
18using namespace llvm;
19
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.
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);
45}
46
48 switch (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!");
77 }
78
79 llvm_unreachable("Invalid EHPersonality!");
80}
81
83 if (T.isPS5())
84 return EHPersonality::GNU_CXX;
85 else
86 return EHPersonality::GNU_C;
87}
88
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);
95}
96
99 BasicBlock *EntryBlock = &F.getEntryBlock();
101
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).
108 //
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.
111
112 DEBUG_WITH_TYPE("winehprepare-coloring",
113 dbgs() << "\nColoring funclets for " << F.getName() << "\n");
114
115 Worklist.push_back({EntryBlock, EntryBlock});
116
117 while (!Worklist.empty()) {
118 BasicBlock *Visiting;
119 BasicBlock *Color;
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.
127 Color = Visiting;
128 }
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);
133 else
134 continue;
135
136 DEBUG_WITH_TYPE("winehprepare-coloring",
137 dbgs() << " Assigned color \'" << Color->getName()
138 << "\' to block \'" << Visiting->getName()
139 << "\'.\n");
140
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;
147 else
148 SuccColor = cast<Instruction>(ParentPad)->getParent();
149 }
150
151 for (BasicBlock *Succ : successors(Visiting))
152 Worklist.push_back({Succ, SuccColor});
153 }
154 return BlockColors;
155}
This header is deprecated in favour of llvm/TargetParser/Triple.h.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition: Debug.h:64
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
#define F(x, y, z)
Definition: MD5.cpp:55
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:208
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:112
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:127
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:685
bool empty() const
Definition: SmallVector.h:94
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:44
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:69
R Default(T Value)
Definition: StringSwitch.h:182
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:29
void push_back(EltTy NewVal)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
LLVM Value Representation.
Definition: Value.h:74
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:685
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
StringRef getEHPersonalityName(EHPersonality Pers)
auto successors(const MachineBasicBlock *BB)
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
bool canSimplifyInvokeNoUnwind(const Function *F)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1869
EHPersonality getDefaultEHPersonality(const Triple &T)