blob: 4868b66139b67607d36030204bd6f42d75b0eab8 [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)
30 .Case("__gcc_personality_v0", EHPersonality::GNU_C)
31 .Case("__objc_personality_v0", EHPersonality::GNU_ObjC)
32 .Case("_except_handler3", EHPersonality::MSVC_X86SEH)
33 .Case("_except_handler4", EHPersonality::MSVC_X86SEH)
34 .Case("__C_specific_handler", EHPersonality::MSVC_Win64SEH)
35 .Case("__CxxFrameHandler3", EHPersonality::MSVC_CXX)
36 .Case("ProcessCLRException", EHPersonality::CoreCLR)
37 .Default(EHPersonality::Unknown);
38}
39
40bool llvm::canSimplifyInvokeNoUnwind(const Function *F) {
41 EHPersonality Personality = classifyEHPersonality(F->getPersonalityFn());
42 // We can't simplify any invokes to nounwind functions if the personality
43 // function wants to catch asynch exceptions. The nounwind attribute only
44 // implies that the function does not throw synchronous exceptions.
45 return !isAsynchronousEHPersonality(Personality);
46}
David Majnemer8a1c45d2015-12-12 05:38:55 +000047
48DenseMap<BasicBlock *, ColorVector> llvm::colorEHFunclets(Function &F) {
49 SmallVector<std::pair<BasicBlock *, BasicBlock *>, 16> Worklist;
50 BasicBlock *EntryBlock = &F.getEntryBlock();
51 DenseMap<BasicBlock *, ColorVector> BlockColors;
52
53 // Build up the color map, which maps each block to its set of 'colors'.
54 // For any block B the "colors" of B are the set of funclets F (possibly
55 // including a root "funclet" representing the main function) such that
56 // F will need to directly contain B or a copy of B (where the term "directly
57 // contain" is used to distinguish from being "transitively contained" in
58 // a nested funclet).
59 //
David Majnemerbbfc7212015-12-14 18:34:23 +000060 // Note: Despite not being a funclet in the truest sense, a catchswitch is
61 // considered to belong to its own funclet for the purposes of coloring.
David Majnemer8a1c45d2015-12-12 05:38:55 +000062
63 DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
64 << F.getName() << "\n");
65
66 Worklist.push_back({EntryBlock, EntryBlock});
67
68 while (!Worklist.empty()) {
69 BasicBlock *Visiting;
70 BasicBlock *Color;
71 std::tie(Visiting, Color) = Worklist.pop_back_val();
72 DEBUG_WITH_TYPE("winehprepare-coloring",
73 dbgs() << "Visiting " << Visiting->getName() << ", "
74 << Color->getName() << "\n");
75 Instruction *VisitingHead = Visiting->getFirstNonPHI();
76 if (VisitingHead->isEHPad()) {
77 // Mark this funclet head as a member of itself.
78 Color = Visiting;
79 }
80 // Note that this is a member of the given color.
81 ColorVector &Colors = BlockColors[Visiting];
82 if (std::find(Colors.begin(), Colors.end(), Color) == Colors.end())
83 Colors.push_back(Color);
84 else
85 continue;
86
87 DEBUG_WITH_TYPE("winehprepare-coloring",
88 dbgs() << " Assigned color \'" << Color->getName()
89 << "\' to block \'" << Visiting->getName()
90 << "\'.\n");
91
92 BasicBlock *SuccColor = Color;
93 TerminatorInst *Terminator = Visiting->getTerminator();
94 if (auto *CatchRet = dyn_cast<CatchReturnInst>(Terminator)) {
Joseph Tremoulet44b3f962016-01-15 21:16:19 +000095 Value *ParentPad = CatchRet->getCatchSwitchParentPad();
David Majnemer8a1c45d2015-12-12 05:38:55 +000096 if (isa<ConstantTokenNone>(ParentPad))
97 SuccColor = EntryBlock;
98 else
99 SuccColor = cast<Instruction>(ParentPad)->getParent();
100 }
101
102 for (BasicBlock *Succ : successors(Visiting))
103 Worklist.push_back({Succ, SuccColor});
104 }
105 return BlockColors;
106}