1*9880d681SAndroid Build Coastguard Worker //===- EHPersonalities.cpp - Compute EH-related information ---------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/EHPersonalities.h"
11*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringSwitch.h"
12*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CFG.h"
13*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
14*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
15*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
18*9880d681SAndroid Build Coastguard Worker using namespace llvm;
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker /// See if the given exception handling personality function is one that we
21*9880d681SAndroid Build Coastguard Worker /// understand. If so, return a description of it; otherwise return Unknown.
classifyEHPersonality(const Value * Pers)22*9880d681SAndroid Build Coastguard Worker EHPersonality llvm::classifyEHPersonality(const Value *Pers) {
23*9880d681SAndroid Build Coastguard Worker const Function *F =
24*9880d681SAndroid Build Coastguard Worker Pers ? dyn_cast<Function>(Pers->stripPointerCasts()) : nullptr;
25*9880d681SAndroid Build Coastguard Worker if (!F)
26*9880d681SAndroid Build Coastguard Worker return EHPersonality::Unknown;
27*9880d681SAndroid Build Coastguard Worker return StringSwitch<EHPersonality>(F->getName())
28*9880d681SAndroid Build Coastguard Worker .Case("__gnat_eh_personality", EHPersonality::GNU_Ada)
29*9880d681SAndroid Build Coastguard Worker .Case("__gxx_personality_v0", EHPersonality::GNU_CXX)
30*9880d681SAndroid Build Coastguard Worker .Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj)
31*9880d681SAndroid Build Coastguard Worker .Case("__gcc_personality_v0", EHPersonality::GNU_C)
32*9880d681SAndroid Build Coastguard Worker .Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj)
33*9880d681SAndroid Build Coastguard Worker .Case("__objc_personality_v0", EHPersonality::GNU_ObjC)
34*9880d681SAndroid Build Coastguard Worker .Case("_except_handler3", EHPersonality::MSVC_X86SEH)
35*9880d681SAndroid Build Coastguard Worker .Case("_except_handler4", EHPersonality::MSVC_X86SEH)
36*9880d681SAndroid Build Coastguard Worker .Case("__C_specific_handler", EHPersonality::MSVC_Win64SEH)
37*9880d681SAndroid Build Coastguard Worker .Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX)
38*9880d681SAndroid Build Coastguard Worker .Case("ProcessCLRException", EHPersonality::CoreCLR)
39*9880d681SAndroid Build Coastguard Worker .Case("rust_eh_personality", EHPersonality::Rust)
40*9880d681SAndroid Build Coastguard Worker .Default(EHPersonality::Unknown);
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker
canSimplifyInvokeNoUnwind(const Function * F)43*9880d681SAndroid Build Coastguard Worker bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
44*9880d681SAndroid Build Coastguard Worker EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
45*9880d681SAndroid Build Coastguard Worker // We can't simplify any invokes to nounwind functions if the personality
46*9880d681SAndroid Build Coastguard Worker // function wants to catch asynch exceptions. The nounwind attribute only
47*9880d681SAndroid Build Coastguard Worker // implies that the function does not throw synchronous exceptions.
48*9880d681SAndroid Build Coastguard Worker return !isAsynchronousEHPersonality(Personality);
49*9880d681SAndroid Build Coastguard Worker }
50*9880d681SAndroid Build Coastguard Worker
colorEHFunclets(Function & F)51*9880d681SAndroid Build Coastguard Worker DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) {
52*9880d681SAndroid Build Coastguard Worker SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
53*9880d681SAndroid Build Coastguard Worker BasicBlock *EntryBlock = &F.getEntryBlock();
54*9880d681SAndroid Build Coastguard Worker DenseMap<BasicBlock *, ColorVector> BlockColors;
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker // Build up the color map, which maps each block to its set of 'colors'.
57*9880d681SAndroid Build Coastguard Worker // For any block B the "colors" of B are the set of funclets F (possibly
58*9880d681SAndroid Build Coastguard Worker // including a root "funclet" representing the main function) such that
59*9880d681SAndroid Build Coastguard Worker // F will need to directly contain B or a copy of B (where the term "directly
60*9880d681SAndroid Build Coastguard Worker // contain" is used to distinguish from being "transitively contained" in
61*9880d681SAndroid Build Coastguard Worker // a nested funclet).
62*9880d681SAndroid Build Coastguard Worker //
63*9880d681SAndroid Build Coastguard Worker // Note: Despite not being a funclet in the truest sense, a catchswitch is
64*9880d681SAndroid Build Coastguard Worker // considered to belong to its own funclet for the purposes of coloring.
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
67*9880d681SAndroid Build Coastguard Worker << F.getName() << "\n");
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker Worklist.push_back({EntryBlock, EntryBlock});
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker while (!Worklist.empty()) {
72*9880d681SAndroid Build Coastguard Worker BasicBlock *Visiting;
73*9880d681SAndroid Build Coastguard Worker BasicBlock *Color;
74*9880d681SAndroid Build Coastguard Worker std::tie(Visiting, Color) = Worklist.pop_back_val();
75*9880d681SAndroid Build Coastguard Worker DEBUG_WITH_TYPE("winehprepare-coloring",
76*9880d681SAndroid Build Coastguard Worker dbgs() << "Visiting " << Visiting->getName() << ", "
77*9880d681SAndroid Build Coastguard Worker << Color->getName() << "\n");
78*9880d681SAndroid Build Coastguard Worker Instruction *VisitingHead = Visiting->getFirstNonPHI();
79*9880d681SAndroid Build Coastguard Worker if (VisitingHead->isEHPad()) {
80*9880d681SAndroid Build Coastguard Worker // Mark this funclet head as a member of itself.
81*9880d681SAndroid Build Coastguard Worker Color = Visiting;
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker // Note that this is a member of the given color.
84*9880d681SAndroid Build Coastguard Worker ColorVector &Colors = BlockColors[Visiting];
85*9880d681SAndroid Build Coastguard Worker if (std::find(Colors.begin(), Colors.end(), Color) == Colors.end())
86*9880d681SAndroid Build Coastguard Worker Colors.push_back(Color);
87*9880d681SAndroid Build Coastguard Worker else
88*9880d681SAndroid Build Coastguard Worker continue;
89*9880d681SAndroid Build Coastguard Worker
90*9880d681SAndroid Build Coastguard Worker DEBUG_WITH_TYPE("winehprepare-coloring",
91*9880d681SAndroid Build Coastguard Worker dbgs() << " Assigned color \'" << Color->getName()
92*9880d681SAndroid Build Coastguard Worker << "\' to block \'" << Visiting->getName()
93*9880d681SAndroid Build Coastguard Worker << "\'.\n");
94*9880d681SAndroid Build Coastguard Worker
95*9880d681SAndroid Build Coastguard Worker BasicBlock *SuccColor = Color;
96*9880d681SAndroid Build Coastguard Worker TerminatorInst *Terminator = Visiting->getTerminator();
97*9880d681SAndroid Build Coastguard Worker if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
98*9880d681SAndroid Build Coastguard Worker Value *ParentPad = CatchRet->getCatchSwitchParentPad();
99*9880d681SAndroid Build Coastguard Worker if (isa<ConstantTokenNone>(ParentPad))
100*9880d681SAndroid Build Coastguard Worker SuccColor = EntryBlock;
101*9880d681SAndroid Build Coastguard Worker else
102*9880d681SAndroid Build Coastguard Worker SuccColor = cast<Instruction>(ParentPad)->getParent();
103*9880d681SAndroid Build Coastguard Worker }
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker for (BasicBlock *Succ : successors(Visiting))
106*9880d681SAndroid Build Coastguard Worker Worklist.push_back({Succ, SuccColor});
107*9880d681SAndroid Build Coastguard Worker }
108*9880d681SAndroid Build Coastguard Worker return BlockColors;
109*9880d681SAndroid Build Coastguard Worker }
110