blob: b5f8463b896d026c10a11c5ada3d9694c8d65911 [file] [log] [blame]
Reid Kleckner1185fce2015-01-29 00:41:44 +00001//===-- WinEHPrepare - Prepare exception handling for code generation ---===//
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// This pass lowers LLVM IR exception handling into something closer to what the
11// backend wants. It snifs the personality function to see which kind of
12// preparation is necessary. If the personality function uses the Itanium LSDA,
13// this pass delegates to the DWARF EH preparation pass.
14//
15//===----------------------------------------------------------------------===//
16
17#include "llvm/CodeGen/Passes.h"
18#include "llvm/Analysis/LibCallSemantics.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Intrinsics.h"
22#include "llvm/Pass.h"
23#include "llvm/Target/TargetLowering.h"
24using namespace llvm;
25
26#define DEBUG_TYPE "winehprepare"
27
28namespace {
29class WinEHPrepare : public FunctionPass {
Reid Kleckner1185fce2015-01-29 00:41:44 +000030 FunctionPass *DwarfPrepare;
31
32public:
33 static char ID; // Pass identification, replacement for typeid.
34 WinEHPrepare(const TargetMachine *TM = nullptr)
Chandler Carrutha1a62272015-01-29 02:44:53 +000035 : FunctionPass(ID), DwarfPrepare(createDwarfEHPass(TM)) {
Reid Kleckner1185fce2015-01-29 00:41:44 +000036 initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
37 }
38
39 bool runOnFunction(Function &Fn) override;
40
41 bool doFinalization(Module &M) override;
42
43 void getAnalysisUsage(AnalysisUsage &AU) const override;
44
45 const char *getPassName() const override {
46 return "Windows exception handling preparation";
47 }
48};
49} // end anonymous namespace
50
51char WinEHPrepare::ID = 0;
52INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare",
53 "Prepare Windows exceptions", false, false)
54
55FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
56 return new WinEHPrepare(TM);
57}
58
59static bool isMSVCPersonality(EHPersonality Pers) {
60 return Pers == EHPersonality::MSVC_Win64SEH ||
61 Pers == EHPersonality::MSVC_CXX;
62}
63
64bool WinEHPrepare::runOnFunction(Function &Fn) {
65 SmallVector<LandingPadInst *, 4> LPads;
66 SmallVector<ResumeInst *, 4> Resumes;
67 for (BasicBlock &BB : Fn) {
68 if (auto *LP = BB.getLandingPadInst())
69 LPads.push_back(LP);
70 if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
71 Resumes.push_back(Resume);
72 }
73
74 // No need to prepare functions that lack landing pads.
75 if (LPads.empty())
76 return false;
77
78 // Classify the personality to see what kind of preparation we need.
79 EHPersonality Pers = ClassifyEHPersonality(LPads.back()->getPersonalityFn());
80
81 // Delegate through to the DWARF pass if this is unrecognized.
82 if (!isMSVCPersonality(Pers))
83 return DwarfPrepare->runOnFunction(Fn);
84
85 // FIXME: Cleanups are unimplemented. Replace them with calls to @llvm.trap.
86 if (Resumes.empty())
87 return false;
88
Reid Kleckner1185fce2015-01-29 00:41:44 +000089 for (ResumeInst *Resume : Resumes) {
90 IRBuilder<>(Resume).CreateUnreachable();
91 Resume->eraseFromParent();
92 }
93
94 return true;
95}
96
97bool WinEHPrepare::doFinalization(Module &M) {
98 return DwarfPrepare->doFinalization(M);
99}
100
101void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
102 DwarfPrepare->getAnalysisUsage(AU);
103}