LLVM  16.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/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"
18 using 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())
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)
45 }
46 
48  switch (Pers) {
49  case EHPersonality::GNU_Ada: return "__gnat_eh_personality";
50  case EHPersonality::GNU_CXX: return "__gxx_personality_v0";
51  case EHPersonality::GNU_CXX_SjLj: return "__gxx_personality_sj0";
52  case EHPersonality::GNU_C: return "__gcc_personality_v0";
53  case EHPersonality::GNU_C_SjLj: return "__gcc_personality_sj0";
54  case EHPersonality::GNU_ObjC: return "__objc_personality_v0";
55  case EHPersonality::MSVC_X86SEH: return "_except_handler3";
57  return "__C_specific_handler";
58  case EHPersonality::MSVC_CXX: return "__CxxFrameHandler3";
59  case EHPersonality::CoreCLR: return "ProcessCLRException";
60  case EHPersonality::Rust: return "rust_eh_personality";
61  case EHPersonality::Wasm_CXX: return "__gxx_wasm_personality_v0";
63  return "__xlcxx_personality_v1";
64  case EHPersonality::Unknown: llvm_unreachable("Unknown EHPersonality!");
65  }
66 
67  llvm_unreachable("Invalid EHPersonality!");
68 }
69 
71  if (T.isPS5())
73  else
74  return EHPersonality::GNU_C;
75 }
76 
78  EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
79  // We can't simplify any invokes to nounwind functions if the personality
80  // function wants to catch asynch exceptions. The nounwind attribute only
81  // implies that the function does not throw synchronous exceptions.
82  return !isAsynchronousEHPersonality(Personality);
83 }
84 
87  BasicBlock *EntryBlock = &F.getEntryBlock();
89 
90  // Build up the color map, which maps each block to its set of 'colors'.
91  // For any block B the "colors" of B are the set of funclets F (possibly
92  // including a root "funclet" representing the main function) such that
93  // F will need to directly contain B or a copy of B (where the term "directly
94  // contain" is used to distinguish from being "transitively contained" in
95  // a nested funclet).
96  //
97  // Note: Despite not being a funclet in the truest sense, a catchswitch is
98  // considered to belong to its own funclet for the purposes of coloring.
99 
100  DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
101  << F.getName() << "\n");
102 
103  Worklist.push_back({EntryBlock, EntryBlock});
104 
105  while (!Worklist.empty()) {
106  BasicBlock *Visiting;
107  BasicBlock *Color;
108  std::tie(Visiting, Color) = Worklist.pop_back_val();
109  DEBUG_WITH_TYPE("winehprepare-coloring",
110  dbgs() << "Visiting " << Visiting->getName() << ", "
111  << Color->getName() << "\n");
112  Instruction *VisitingHead = Visiting->getFirstNonPHI();
113  if (VisitingHead->isEHPad()) {
114  // Mark this funclet head as a member of itself.
115  Color = Visiting;
116  }
117  // Note that this is a member of the given color.
118  ColorVector &Colors = BlockColors[Visiting];
119  if (!is_contained(Colors, Color))
120  Colors.push_back(Color);
121  else
122  continue;
123 
124  DEBUG_WITH_TYPE("winehprepare-coloring",
125  dbgs() << " Assigned color \'" << Color->getName()
126  << "\' to block \'" << Visiting->getName()
127  << "\'.\n");
128 
129  BasicBlock *SuccColor = Color;
130  Instruction *Terminator = Visiting->getTerminator();
131  if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
132  Value *ParentPad = CatchRet->getCatchSwitchParentPad();
133  if (isa<ConstantTokenNone>(ParentPad))
134  SuccColor = EntryBlock;
135  else
136  SuccColor = cast<Instruction>(ParentPad)->getParent();
137  }
138 
139  for (BasicBlock *Succ : successors(Visiting))
140  Worklist.push_back({Succ, SuccColor});
141  }
142  return BlockColors;
143 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:70
llvm::EHPersonality::MSVC_CXX
@ MSVC_CXX
llvm::isAsynchronousEHPersonality
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
Definition: EHPersonalities.h:49
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:104
llvm::EHPersonality::GNU_Ada
@ GNU_Ada
T
llvm::Function
Definition: Function.h:60
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1199
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
EHPersonalities.h
llvm::successors
auto successors(MachineBasicBlock *BB)
Definition: MachineSSAContext.h:29
llvm::EHPersonality::GNU_ObjC
@ GNU_ObjC
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
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:22
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:42
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:209
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:85
llvm::canSimplifyInvokeNoUnwind
bool canSimplifyInvokeNoUnwind(const Function *F)
Definition: EHPersonalities.cpp:77
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:1868
llvm::getEHPersonalityName
StringRef getEHPersonalityName(EHPersonality Pers)
Definition: EHPersonalities.cpp:47
llvm::EHPersonality::CoreCLR
@ CoreCLR
Triple.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
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:143
llvm::EHPersonality::XL_CXX
@ XL_CXX
llvm::getDefaultEHPersonality
EHPersonality getDefaultEHPersonality(const Triple &T)
Definition: EHPersonalities.cpp:70
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
llvm::EHPersonality
EHPersonality
Definition: EHPersonalities.h:21
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:685
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:669
StringSwitch.h
Instructions.h
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition: SmallVector.h:677
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.h:119
llvm::StringSwitch::Default
R Default(T Value)
Definition: StringSwitch.h:183
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:45
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:157
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Debug.h