LLVM  14.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 
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/Support/Debug.h"
17 using namespace llvm;
18 
19 /// See if the given exception handling personality function is one that we
20 /// understand. If so, return a description of it; otherwise return Unknown.
22  const GlobalValue *F =
23  Pers ? dyn_cast<GlobalValue>(Pers->stripPointerCasts()) : nullptr;
24  if (!F || !F->getValueType() || !F->getValueType()->isFunctionTy())
26  return StringSwitch<EHPersonality>(F->getName())
27  .Case("__gnat_eh_personality", EHPersonality::GNU_Ada)
28  .Case("__gxx_personality_v0", EHPersonality::GNU_CXX)
29  .Case("__gxx_personality_seh0", EHPersonality::GNU_CXX)
30  .Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj)
31  .Case("__gcc_personality_v0", EHPersonality::GNU_C)
32  .Case("__gcc_personality_seh0", EHPersonality::GNU_C)
33  .Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj)
34  .Case("__objc_personality_v0", EHPersonality::GNU_ObjC)
35  .Case("_except_handler3", EHPersonality::MSVC_X86SEH)
36  .Case("_except_handler4", EHPersonality::MSVC_X86SEH)
37  .Case("__C_specific_handler", EHPersonality::MSVC_TableSEH)
38  .Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX)
39  .Case("ProcessCLRException", EHPersonality::CoreCLR)
40  .Case("rust_eh_personality", EHPersonality::Rust)
41  .Case("__gxx_wasm_personality_v0", EHPersonality::Wasm_CXX)
42  .Case("__xlcxx_personality_v1", EHPersonality::XL_CXX)
44 }
45 
47  switch (Pers) {
48  case EHPersonality::GNU_Ada: return "__gnat_eh_personality";
49  case EHPersonality::GNU_CXX: return "__gxx_personality_v0";
50  case EHPersonality::GNU_CXX_SjLj: return "__gxx_personality_sj0";
51  case EHPersonality::GNU_C: return "__gcc_personality_v0";
52  case EHPersonality::GNU_C_SjLj: return "__gcc_personality_sj0";
53  case EHPersonality::GNU_ObjC: return "__objc_personality_v0";
54  case EHPersonality::MSVC_X86SEH: return "_except_handler3";
56  return "__C_specific_handler";
57  case EHPersonality::MSVC_CXX: return "__CxxFrameHandler3";
58  case EHPersonality::CoreCLR: return "ProcessCLRException";
59  case EHPersonality::Rust: return "rust_eh_personality";
60  case EHPersonality::Wasm_CXX: return "__gxx_wasm_personality_v0";
62  return "__xlcxx_personality_v1";
63  case EHPersonality::Unknown: llvm_unreachable("Unknown EHPersonality!");
64  }
65 
66  llvm_unreachable("Invalid EHPersonality!");
67 }
68 
70  return EHPersonality::GNU_C;
71 }
72 
74  EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
75  // We can't simplify any invokes to nounwind functions if the personality
76  // function wants to catch asynch exceptions. The nounwind attribute only
77  // implies that the function does not throw synchronous exceptions.
78  return !isAsynchronousEHPersonality(Personality);
79 }
80 
83  BasicBlock *EntryBlock = &F.getEntryBlock();
85 
86  // Build up the color map, which maps each block to its set of 'colors'.
87  // For any block B the "colors" of B are the set of funclets F (possibly
88  // including a root "funclet" representing the main function) such that
89  // F will need to directly contain B or a copy of B (where the term "directly
90  // contain" is used to distinguish from being "transitively contained" in
91  // a nested funclet).
92  //
93  // Note: Despite not being a funclet in the truest sense, a catchswitch is
94  // considered to belong to its own funclet for the purposes of coloring.
95 
96  DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
97  << F.getName() << "\n");
98 
99  Worklist.push_back({EntryBlock, EntryBlock});
100 
101  while (!Worklist.empty()) {
102  BasicBlock *Visiting;
103  BasicBlock *Color;
104  std::tie(Visiting, Color) = Worklist.pop_back_val();
105  DEBUG_WITH_TYPE("winehprepare-coloring",
106  dbgs() << "Visiting " << Visiting->getName() << ", "
107  << Color->getName() << "\n");
108  Instruction *VisitingHead = Visiting->getFirstNonPHI();
109  if (VisitingHead->isEHPad()) {
110  // Mark this funclet head as a member of itself.
111  Color = Visiting;
112  }
113  // Note that this is a member of the given color.
114  ColorVector &Colors = BlockColors[Visiting];
115  if (!is_contained(Colors, Color))
116  Colors.push_back(Color);
117  else
118  continue;
119 
120  DEBUG_WITH_TYPE("winehprepare-coloring",
121  dbgs() << " Assigned color \'" << Color->getName()
122  << "\' to block \'" << Visiting->getName()
123  << "\'.\n");
124 
125  BasicBlock *SuccColor = Color;
126  Instruction *Terminator = Visiting->getTerminator();
127  if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
128  Value *ParentPad = CatchRet->getCatchSwitchParentPad();
129  if (isa<ConstantTokenNone>(ParentPad))
130  SuccColor = EntryBlock;
131  else
132  SuccColor = cast<Instruction>(ParentPad)->getParent();
133  }
134 
135  for (BasicBlock *Succ : successors(Visiting))
136  Worklist.push_back({Succ, SuccColor});
137  }
138  return BlockColors;
139 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
llvm::EHPersonality::MSVC_CXX
@ MSVC_CXX
llvm::isAsynchronousEHPersonality
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
Definition: EHPersonalities.h:50
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::EHPersonality::GNU_C_SjLj
@ GNU_C_SjLj
llvm::BasicBlock::getParent
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:107
llvm::EHPersonality::GNU_Ada
@ GNU_Ada
T
llvm::Function
Definition: Function.h:62
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
EHPersonalities.h
llvm::SmallVectorImpl::pop_back_val
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:635
llvm::successors
succ_range successors(Instruction *I)
Definition: CFG.h:262
llvm::EHPersonality::GNU_ObjC
@ GNU_ObjC
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::classifyEHPersonality
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Definition: EHPersonalities.cpp:21
DEBUG_WITH_TYPE
#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
Constants.h
llvm::TinyPtrVector::push_back
void push_back(EltTy NewVal)
Definition: TinyPtrVector.h:244
llvm::EHPersonality::GNU_C
@ GNU_C
llvm::Instruction
Definition: Instruction.h:45
llvm::BasicBlock::getFirstNonPHI
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:216
llvm::EHPersonality::Wasm_CXX
@ Wasm_CXX
CFG.h
llvm::EHPersonality::GNU_CXX_SjLj
@ GNU_CXX_SjLj
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::colorEHFunclets
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
Definition: EHPersonalities.cpp:81
llvm::canSimplifyInvokeNoUnwind
bool canSimplifyInvokeNoUnwind(const Function *F)
Definition: EHPersonalities.cpp:73
llvm::DenseMap
Definition: DenseMap.h:714
llvm::is_contained
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:1616
llvm::getEHPersonalityName
StringRef getEHPersonalityName(EHPersonality Pers)
Definition: EHPersonalities.cpp:46
llvm::EHPersonality::CoreCLR
@ CoreCLR
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::EHPersonality::MSVC_TableSEH
@ MSVC_TableSEH
llvm::EHPersonality::GNU_CXX
@ GNU_CXX
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:134
llvm::EHPersonality::XL_CXX
@ XL_CXX
llvm::getDefaultEHPersonality
EHPersonality getDefaultEHPersonality(const Triple &T)
Definition: EHPersonalities.cpp:69
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
llvm::EHPersonality
EHPersonality
Definition: EHPersonalities.h:22
llvm::BasicBlock::getTerminator
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.cpp:152
llvm::EHPersonality::MSVC_X86SEH
@ MSVC_X86SEH
llvm::Value::stripPointerCasts
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:687
llvm::EHPersonality::Unknown
@ Unknown
Function.h
llvm::Instruction::isEHPad
bool isEHPad() const
Return true if the instruction is a variety of EH-block.
Definition: Instruction.h:661
StringSwitch.h
Instructions.h
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::EHPersonality::Rust
@ Rust
raw_ostream.h
llvm::TinyPtrVector
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:30
llvm::MCID::Terminator
@ Terminator
Definition: MCInstrDesc.h:155
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
Debug.h