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