blob: b12ae9884e3d623a62e2754c208ca9b35ca29c5a [file] [log] [blame]
David Majnemer6f4583c2015-12-02 23:09:05 +00001//===- 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"
David Majnemer8a1c45d2015-12-12 05:38:55 +000012#include "llvm/IR/CFG.h"
13#include "llvm/IR/Constants.h"
David Majnemer6f4583c2015-12-02 23:09:05 +000014#include "llvm/IR/Function.h"
David Majnemer8a1c45d2015-12-12 05:38:55 +000015#include "llvm/IR/Instructions.h"
16#include "llvm/Support/Debug.h"
David Majnemerf28c52f2015-12-12 05:53:20 +000017#include "llvm/Support/raw_ostream.h"
David Majnemer6f4583c2015-12-02 23:09:05 +000018using 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.
22EHPersonality 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)
Reid Klecknerfc7ba562017-05-31 22:35:52 +000030 .Case("__gxx_personality_seh0",EHPersonality::GNU_CXX)
Saleem Abdulrasoold2f705d2016-05-31 01:48:07 +000031 .Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj)
David Majnemer6f4583c2015-12-02 23:09:05 +000032 .Case("__gcc_personality_v0", EHPersonality::GNU_C)
Reid Klecknerfc7ba562017-05-31 22:35:52 +000033 .Case("__gcc_personality_seh0",EHPersonality::GNU_C)
Saleem Abdulrasoold2f705d2016-05-31 01:48:07 +000034 .Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj)
David Majnemer6f4583c2015-12-02 23:09:05 +000035 .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_Win64SEH)
39 .Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX)
40 .Case("ProcessCLRException", EHPersonality::CoreCLR)
Bjorn Steinbrink59fdec62016-03-15 20:35:45 +000041 .Case("rust_eh_personality", EHPersonality::Rust)
David Majnemer6f4583c2015-12-02 23:09:05 +000042 .Default(EHPersonality::Unknown);
43}
44
Kuba Breckaddfdba32016-11-14 21:41:13 +000045StringRef llvm::getEHPersonalityName(EHPersonality Pers) {
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::Unknown: llvm_unreachable("Unknown EHPersonality!");
59 }
60
61 llvm_unreachable("Invalid EHPersonality!");
62}
63
64EHPersonality llvm::getDefaultEHPersonality(const Triple &T) {
65 return EHPersonality::GNU_C;
66}
67
David Majnemer6f4583c2015-12-02 23:09:05 +000068bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
69 EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
70 // We can't simplify any invokes to nounwind functions if the personality
71 // function wants to catch asynch exceptions. The nounwind attribute only
72 // implies that the function does not throw synchronous exceptions.
73 return !isAsynchronousEHPersonality(Personality);
74}
David Majnemer8a1c45d2015-12-12 05:38:55 +000075
76DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) {
77 SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
78 BasicBlock *EntryBlock = &F.getEntryBlock();
79 DenseMap<BasicBlock *, ColorVector> BlockColors;
80
81 // Build up the color map, which maps each block to its set of 'colors'.
82 // For any block B the "colors" of B are the set of funclets F (possibly
83 // including a root "funclet" representing the main function) such that
84 // F will need to directly contain B or a copy of B (where the term "directly
85 // contain" is used to distinguish from being "transitively contained" in
86 // a nested funclet).
87 //
David Majnemerbbfc7212015-12-14 18:34:23 +000088 // Note: Despite not being a funclet in the truest sense, a catchswitch is
89 // considered to belong to its own funclet for the purposes of coloring.
David Majnemer8a1c45d2015-12-12 05:38:55 +000090
91 DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
92 << F.getName() << "\n");
93
94 Worklist.push_back({EntryBlock, EntryBlock});
95
96 while (!Worklist.empty()) {
97 BasicBlock *Visiting;
98 BasicBlock *Color;
99 std::tie(Visiting, Color) = Worklist.pop_back_val();
100 DEBUG_WITH_TYPE("winehprepare-coloring",
101 dbgs() << "Visiting " << Visiting->getName() << ", "
102 << Color->getName() << "\n");
103 Instruction *VisitingHead = Visiting->getFirstNonPHI();
104 if (VisitingHead->isEHPad()) {
105 // Mark this funclet head as a member of itself.
106 Color = Visiting;
107 }
108 // Note that this is a member of the given color.
109 ColorVector &Colors = BlockColors[Visiting];
David Majnemer0d955d02016-08-11 22:21:41 +0000110 if (!is_contained(Colors, Color))
David Majnemer8a1c45d2015-12-12 05:38:55 +0000111 Colors.push_back(Color);
112 else
113 continue;
114
115 DEBUG_WITH_TYPE("winehprepare-coloring",
116 dbgs() << " Assigned color \'" << Color->getName()
117 << "\' to block \'" << Visiting->getName()
118 << "\'.\n");
119
120 BasicBlock *SuccColor = Color;
121 TerminatorInst *Terminator = Visiting->getTerminator();
122 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
Joseph Tremoulet44b3f962016-01-15 21:16:19 +0000123 Value *ParentPad = CatchRet->getCatchSwitchParentPad();
David Majnemer8a1c45d2015-12-12 05:38:55 +0000124 if (isa<ConstantTokenNone>(ParentPad))
125 SuccColor = EntryBlock;
126 else
127 SuccColor = cast<Instruction>(ParentPad)->getParent();
128 }
129
130 for (BasicBlock *Succ : successors(Visiting))
131 Worklist.push_back({Succ, SuccColor});
132 }
133 return BlockColors;
134}