blob: 5f951f5112e9e9853e4bba94863459f065c1d5e2 [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)
Saleem Abdulrasoold2f705d2016-05-31 01:48:07 +000030 .Case("__gxx_personality_sj0", EHPersonality::GNU_CXX_SjLj)
David Majnemer6f4583c2015-12-02 23:09:05 +000031 .Case("__gcc_personality_v0", EHPersonality::GNU_C)
Saleem Abdulrasoold2f705d2016-05-31 01:48:07 +000032 .Case("__gcc_personality_sj0", EHPersonality::GNU_C_SjLj)
David Majnemer6f4583c2015-12-02 23:09:05 +000033 .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)
Bjorn Steinbrink59fdec62016-03-15 20:35:45 +000039 .Case("rust_eh_personality", EHPersonality::Rust)
David Majnemer6f4583c2015-12-02 23:09:05 +000040 .Default(EHPersonality::Unknown);
41}
42
43bool 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}
David Majnemer8a1c45d2015-12-12 05:38:55 +000050
51DenseMap<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 //
David Majnemerbbfc7212015-12-14 18:34:23 +000063 // 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.
David Majnemer8a1c45d2015-12-12 05:38:55 +000065
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)) {
Joseph Tremoulet44b3f962016-01-15 21:16:19 +000098 Value *ParentPad = CatchRet->getCatchSwitchParentPad();
David Majnemer8a1c45d2015-12-12 05:38:55 +000099 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}