LLVM  10.0.0svn
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 Function *F =
23  Pers ? dyn_cast<Function>(Pers->stripPointerCasts()) : nullptr;
24  if (!F)
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_Win64SEH)
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)
43 }
44 
46  switch (Pers) {
47  case EHPersonality::GNU_Ada: return "__gnat_eh_personality";
48  case EHPersonality::GNU_CXX: return "__gxx_personality_v0";
49  case EHPersonality::GNU_CXX_SjLj: return "__gxx_personality_sj0";
50  case EHPersonality::GNU_C: return "__gcc_personality_v0";
51  case EHPersonality::GNU_C_SjLj: return "__gcc_personality_sj0";
52  case EHPersonality::GNU_ObjC: return "__objc_personality_v0";
53  case EHPersonality::MSVC_X86SEH: return "_except_handler3";
54  case EHPersonality::MSVC_Win64SEH: return "__C_specific_handler";
55  case EHPersonality::MSVC_CXX: return "__CxxFrameHandler3";
56  case EHPersonality::CoreCLR: return "ProcessCLRException";
57  case EHPersonality::Rust: return "rust_eh_personality";
58  case EHPersonality::Wasm_CXX: return "__gxx_wasm_personality_v0";
59  case EHPersonality::Unknown: llvm_unreachable("Unknown EHPersonality!");
60  }
61 
62  llvm_unreachable("Invalid EHPersonality!");
63 }
64 
66  return EHPersonality::GNU_C;
67 }
68 
71  // We can't simplify any invokes to nounwind functions if the personality
72  // function wants to catch asynch exceptions. The nounwind attribute only
73  // implies that the function does not throw synchronous exceptions.
74  return !isAsynchronousEHPersonality(Personality);
75 }
76 
79  BasicBlock *EntryBlock = &F.getEntryBlock();
81 
82  // Build up the color map, which maps each block to its set of 'colors'.
83  // For any block B the "colors" of B are the set of funclets F (possibly
84  // including a root "funclet" representing the main function) such that
85  // F will need to directly contain B or a copy of B (where the term "directly
86  // contain" is used to distinguish from being "transitively contained" in
87  // a nested funclet).
88  //
89  // Note: Despite not being a funclet in the truest sense, a catchswitch is
90  // considered to belong to its own funclet for the purposes of coloring.
91 
92  DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
93  << F.getName() << "\n");
94 
95  Worklist.push_back({EntryBlock, EntryBlock});
96 
97  while (!Worklist.empty()) {
98  BasicBlock *Visiting;
100  std::tie(Visiting, Color) = Worklist.pop_back_val();
101  DEBUG_WITH_TYPE("winehprepare-coloring",
102  dbgs() << "Visiting " << Visiting->getName() << ", "
103  << Color->getName() << "\n");
104  Instruction *VisitingHead = Visiting->getFirstNonPHI();
105  if (VisitingHead->isEHPad()) {
106  // Mark this funclet head as a member of itself.
107  Color = Visiting;
108  }
109  // Note that this is a member of the given color.
110  ColorVector &Colors = BlockColors[Visiting];
111  if (!is_contained(Colors, Color))
112  Colors.push_back(Color);
113  else
114  continue;
115 
116  DEBUG_WITH_TYPE("winehprepare-coloring",
117  dbgs() << " Assigned color \'" << Color->getName()
118  << "\' to block \'" << Visiting->getName()
119  << "\'.\n");
120 
121  BasicBlock *SuccColor = Color;
122  Instruction *Terminator = Visiting->getTerminator();
123  if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
124  Value *ParentPad = CatchRet->getCatchSwitchParentPad();
125  if (isa<ConstantTokenNone>(ParentPad))
126  SuccColor = EntryBlock;
127  else
128  SuccColor = cast<Instruction>(ParentPad)->getParent();
129  }
130 
131  for (BasicBlock *Succ : successors(Visiting))
132  Worklist.push_back({Succ, SuccColor});
133  }
134  return BlockColors;
135 }
This class represents lattice values for constants.
Definition: AllocatorList.h:23
F(f)
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:144
TinyPtrVector - This class is specialized for cases where there are normally 0 or 1 element in a vect...
Definition: TinyPtrVector.h:30
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
#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
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
bool canSimplifyInvokeNoUnwind(const Function *F)
const BasicBlock & getEntryBlock() const
Definition: Function.h:664
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:196
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
void push_back(EltTy NewVal)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
EHPersonality getDefaultEHPersonality(const Triple &T)
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:525
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
Color
A "color", which is either even or odd.
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:374
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
StringRef getEHPersonalityName(EHPersonality Pers)
bool isAsynchronousEHPersonality(EHPersonality Pers)
Returns true if this personality function catches asynchronous exceptions.
LLVM Value Representation.
Definition: Value.h:73
Constant * getPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.cpp:1441
succ_range successors(Instruction *I)
Definition: CFG.h:259
static const Function * getParent(const Value *V)
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
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:1224