blob: 48b3585e0cf355d2546f89abf2d0846d41641d09 [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"
Benjamin Kramereb63e4d2015-01-29 13:26:50 +000020#include "llvm/IR/IRBuilder.h"
Reid Kleckner1185fce2015-01-29 00:41:44 +000021#include "llvm/IR/Instructions.h"
Reid Kleckner1185fce2015-01-29 00:41:44 +000022#include "llvm/Pass.h"
Reid Kleckner1185fce2015-01-29 00:41:44 +000023using namespace llvm;
24
25#define DEBUG_TYPE "winehprepare"
26
27namespace {
28class WinEHPrepare : public FunctionPass {
Reid Kleckner1185fce2015-01-29 00:41:44 +000029 FunctionPass *DwarfPrepare;
30
31public:
32 static char ID; // Pass identification, replacement for typeid.
33 WinEHPrepare(const TargetMachine *TM = nullptr)
Benjamin Kramereb63e4d2015-01-29 13:26:50 +000034 : FunctionPass(ID), DwarfPrepare(createDwarfEHPass(TM)) {}
Reid Kleckner1185fce2015-01-29 00:41:44 +000035
36 bool runOnFunction(Function &Fn) override;
37
38 bool doFinalization(Module &M) override;
39
40 void getAnalysisUsage(AnalysisUsage &AU) const override;
41
42 const char *getPassName() const override {
43 return "Windows exception handling preparation";
44 }
45};
46} // end anonymous namespace
47
48char WinEHPrepare::ID = 0;
49INITIALIZE_TM_PASS(WinEHPrepare, "winehprepare",
50 "Prepare Windows exceptions", false, false)
51
52FunctionPass *llvm::createWinEHPass(const TargetMachine *TM) {
53 return new WinEHPrepare(TM);
54}
55
56static bool isMSVCPersonality(EHPersonality Pers) {
57 return Pers == EHPersonality::MSVC_Win64SEH ||
58 Pers == EHPersonality::MSVC_CXX;
59}
60
61bool WinEHPrepare::runOnFunction(Function &Fn) {
62 SmallVector<LandingPadInst *, 4> LPads;
63 SmallVector<ResumeInst *, 4> Resumes;
64 for (BasicBlock &BB : Fn) {
65 if (auto *LP = BB.getLandingPadInst())
66 LPads.push_back(LP);
67 if (auto *Resume = dyn_cast<ResumeInst>(BB.getTerminator()))
68 Resumes.push_back(Resume);
69 }
70
71 // No need to prepare functions that lack landing pads.
72 if (LPads.empty())
73 return false;
74
75 // Classify the personality to see what kind of preparation we need.
76 EHPersonality Pers = ClassifyEHPersonality(LPads.back()->getPersonalityFn());
77
78 // Delegate through to the DWARF pass if this is unrecognized.
79 if (!isMSVCPersonality(Pers))
80 return DwarfPrepare->runOnFunction(Fn);
81
82 // FIXME: Cleanups are unimplemented. Replace them with calls to @llvm.trap.
83 if (Resumes.empty())
84 return false;
85
Reid Kleckner1185fce2015-01-29 00:41:44 +000086 for (ResumeInst *Resume : Resumes) {
87 IRBuilder<>(Resume).CreateUnreachable();
88 Resume->eraseFromParent();
89 }
90
91 return true;
92}
93
94bool WinEHPrepare::doFinalization(Module &M) {
95 return DwarfPrepare->doFinalization(M);
96}
97
98void WinEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
99 DwarfPrepare->getAnalysisUsage(AU);
100}