Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/CodeGen/DwarfEHPrepare.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- DwarfEHPrepare - 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 mulches exception handling code into a form adapted to code
11
// generation. Required if using dwarf exception handling.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvm/ADT/BitVector.h"
16
#include "llvm/ADT/SmallVector.h"
17
#include "llvm/ADT/Statistic.h"
18
#include "llvm/Analysis/CFG.h"
19
#include "llvm/Analysis/EHPersonalities.h"
20
#include "llvm/Analysis/TargetTransformInfo.h"
21
#include "llvm/CodeGen/RuntimeLibcalls.h"
22
#include "llvm/CodeGen/TargetPassConfig.h"
23
#include "llvm/IR/BasicBlock.h"
24
#include "llvm/IR/Constants.h"
25
#include "llvm/IR/DerivedTypes.h"
26
#include "llvm/IR/Dominators.h"
27
#include "llvm/IR/Function.h"
28
#include "llvm/IR/Instructions.h"
29
#include "llvm/IR/Module.h"
30
#include "llvm/IR/Type.h"
31
#include "llvm/Pass.h"
32
#include "llvm/Support/Casting.h"
33
#include "llvm/Target/TargetLowering.h"
34
#include "llvm/Target/TargetMachine.h"
35
#include "llvm/Target/TargetSubtargetInfo.h"
36
#include "llvm/Transforms/Utils/Local.h"
37
#include <cstddef>
38
39
using namespace llvm;
40
41
#define DEBUG_TYPE "dwarfehprepare"
42
43
STATISTIC(NumResumesLowered, "Number of resume calls lowered");
44
45
namespace {
46
47
  class DwarfEHPrepare : public FunctionPass {
48
    // RewindFunction - _Unwind_Resume or the target equivalent.
49
    Constant *RewindFunction = nullptr;
50
51
    DominatorTree *DT = nullptr;
52
    const TargetLowering *TLI = nullptr;
53
54
    bool InsertUnwindResumeCalls(Function &Fn);
55
    Value *GetExceptionObject(ResumeInst *RI);
56
    size_t
57
    pruneUnreachableResumes(Function &Fn,
58
                            SmallVectorImpl<ResumeInst *> &Resumes,
59
                            SmallVectorImpl<LandingPadInst *> &CleanupLPads);
60
61
  public:
62
    static char ID; // Pass identification, replacement for typeid.
63
64
31.4k
    DwarfEHPrepare() : FunctionPass(ID) {}
65
66
    bool runOnFunction(Function &Fn) override;
67
68
31.3k
    bool doFinalization(Module &M) override {
69
31.3k
      RewindFunction = nullptr;
70
31.3k
      return false;
71
31.3k
    }
72
73
    void getAnalysisUsage(AnalysisUsage &AU) const override;
74
75
17
    StringRef getPassName() const override {
76
17
      return "Exception handling preparation";
77
17
    }
78
  };
79
80
} // end anonymous namespace
81
82
char DwarfEHPrepare::ID = 0;
83
84
42.0k
INITIALIZE_PASS_BEGIN42.0k
(DwarfEHPrepare, DEBUG_TYPE,
85
42.0k
                      "Prepare DWARF exceptions", false, false)
86
42.0k
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
87
42.0k
INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
88
42.0k
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
89
42.0k
INITIALIZE_PASS_END(DwarfEHPrepare, DEBUG_TYPE,
90
                    "Prepare DWARF exceptions", false, false)
91
92
31.4k
FunctionPass *llvm::createDwarfEHPass() { return new DwarfEHPrepare(); }
93
94
31.4k
void DwarfEHPrepare::getAnalysisUsage(AnalysisUsage &AU) const {
95
31.4k
  AU.addRequired<TargetPassConfig>();
96
31.4k
  AU.addRequired<TargetTransformInfoWrapperPass>();
97
31.4k
  AU.addRequired<DominatorTreeWrapperPass>();
98
31.4k
}
99
100
/// GetExceptionObject - Return the exception object from the value passed into
101
/// the 'resume' instruction (typically an aggregate). Clean up any dead
102
/// instructions, including the 'resume' instruction.
103
2.90k
Value *DwarfEHPrepare::GetExceptionObject(ResumeInst *RI) {
104
2.90k
  Value *V = RI->getOperand(0);
105
2.90k
  Value *ExnObj = nullptr;
106
2.90k
  InsertValueInst *SelIVI = dyn_cast<InsertValueInst>(V);
107
2.90k
  LoadInst *SelLoad = nullptr;
108
2.90k
  InsertValueInst *ExcIVI = nullptr;
109
2.90k
  bool EraseIVIs = false;
110
2.90k
111
2.90k
  if (
SelIVI2.90k
) {
112
818
    if (
SelIVI->getNumIndices() == 1 && 818
*SelIVI->idx_begin() == 1818
) {
113
818
      ExcIVI = dyn_cast<InsertValueInst>(SelIVI->getOperand(0));
114
818
      if (
ExcIVI && 818
isa<UndefValue>(ExcIVI->getOperand(0))818
&&
115
818
          
ExcIVI->getNumIndices() == 1818
&&
*ExcIVI->idx_begin() == 0818
) {
116
818
        ExnObj = ExcIVI->getOperand(1);
117
818
        SelLoad = dyn_cast<LoadInst>(SelIVI->getOperand(1));
118
818
        EraseIVIs = true;
119
818
      }
120
818
    }
121
818
  }
122
2.90k
123
2.90k
  if (!ExnObj)
124
2.09k
    ExnObj = ExtractValueInst::Create(RI->getOperand(0), 0, "exn.obj", RI);
125
2.90k
126
2.90k
  RI->eraseFromParent();
127
2.90k
128
2.90k
  if (
EraseIVIs2.90k
) {
129
818
    if (SelIVI->use_empty())
130
818
      SelIVI->eraseFromParent();
131
818
    if (ExcIVI->use_empty())
132
818
      ExcIVI->eraseFromParent();
133
818
    if (
SelLoad && 818
SelLoad->use_empty()21
)
134
21
      SelLoad->eraseFromParent();
135
818
  }
136
2.90k
137
2.90k
  return ExnObj;
138
2.90k
}
139
140
/// Replace resumes that are not reachable from a cleanup landing pad with
141
/// unreachable and then simplify those blocks.
142
size_t DwarfEHPrepare::pruneUnreachableResumes(
143
    Function &Fn, SmallVectorImpl<ResumeInst *> &Resumes,
144
2.72k
    SmallVectorImpl<LandingPadInst *> &CleanupLPads) {
145
2.72k
  BitVector ResumeReachable(Resumes.size());
146
2.72k
  size_t ResumeIndex = 0;
147
2.93k
  for (auto *RI : Resumes) {
148
4.20k
    for (auto *LP : CleanupLPads) {
149
4.20k
      if (
isPotentiallyReachable(LP, RI, DT)4.20k
) {
150
2.90k
        ResumeReachable.set(ResumeIndex);
151
2.90k
        break;
152
2.90k
      }
153
2.93k
    }
154
2.93k
    ++ResumeIndex;
155
2.93k
  }
156
2.72k
157
2.72k
  // If everything is reachable, there is no change.
158
2.72k
  if (ResumeReachable.all())
159
2.69k
    return Resumes.size();
160
30
161
30
  const TargetTransformInfo &TTI =
162
30
      getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn);
163
30
  LLVMContext &Ctx = Fn.getContext();
164
30
165
30
  // Otherwise, insert unreachable instructions and call simplifycfg.
166
30
  size_t ResumesLeft = 0;
167
60
  for (size_t I = 0, E = Resumes.size(); 
I < E60
;
++I30
) {
168
30
    ResumeInst *RI = Resumes[I];
169
30
    if (
ResumeReachable[I]30
) {
170
0
      Resumes[ResumesLeft++] = RI;
171
30
    } else {
172
30
      BasicBlock *BB = RI->getParent();
173
30
      new UnreachableInst(Ctx, RI);
174
30
      RI->eraseFromParent();
175
30
      SimplifyCFG(BB, TTI);
176
30
    }
177
30
  }
178
2.72k
  Resumes.resize(ResumesLeft);
179
2.72k
  return ResumesLeft;
180
2.72k
}
181
182
/// InsertUnwindResumeCalls - Convert the ResumeInsts that are still present
183
/// into calls to the appropriate _Unwind_Resume function.
184
576k
bool DwarfEHPrepare::InsertUnwindResumeCalls(Function &Fn) {
185
576k
  SmallVector<ResumeInst*, 16> Resumes;
186
576k
  SmallVector<LandingPadInst*, 16> CleanupLPads;
187
3.70M
  for (BasicBlock &BB : Fn) {
188
3.70M
    if (auto *RI = dyn_cast<ResumeInst>(BB.getTerminator()))
189
2.93k
      Resumes.push_back(RI);
190
3.70M
    if (auto *LP = BB.getLandingPadInst())
191
13.5k
      
if (13.5k
LP->isCleanup()13.5k
)
192
8.65k
        CleanupLPads.push_back(LP);
193
3.70M
  }
194
576k
195
576k
  if (Resumes.empty())
196
574k
    return false;
197
2.72k
198
2.72k
  // Check the personality, don't do anything if it's funclet-based.
199
2.72k
  EHPersonality Pers = classifyEHPersonality(Fn.getPersonalityFn());
200
2.72k
  if (isFuncletEHPersonality(Pers))
201
0
    return false;
202
2.72k
203
2.72k
  LLVMContext &Ctx = Fn.getContext();
204
2.72k
205
2.72k
  size_t ResumesLeft = pruneUnreachableResumes(Fn, Resumes, CleanupLPads);
206
2.72k
  if (ResumesLeft == 0)
207
30
    return true; // We pruned them all.
208
2.69k
209
2.69k
  // Find the rewind function if we didn't already.
210
2.69k
  
if (2.69k
!RewindFunction2.69k
) {
211
416
    FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
212
416
                                          Type::getInt8PtrTy(Ctx), false);
213
416
    const char *RewindName = TLI->getLibcallName(RTLIB::UNWIND_RESUME);
214
416
    RewindFunction = Fn.getParent()->getOrInsertFunction(RewindName, FTy);
215
416
  }
216
2.69k
217
2.69k
  // Create the basic block where the _Unwind_Resume call will live.
218
2.69k
  if (
ResumesLeft == 12.69k
) {
219
2.52k
    // Instead of creating a new BB and PHI node, just append the call to
220
2.52k
    // _Unwind_Resume to the end of the single resume block.
221
2.52k
    ResumeInst *RI = Resumes.front();
222
2.52k
    BasicBlock *UnwindBB = RI->getParent();
223
2.52k
    Value *ExnObj = GetExceptionObject(RI);
224
2.52k
225
2.52k
    // Call the _Unwind_Resume function.
226
2.52k
    CallInst *CI = CallInst::Create(RewindFunction, ExnObj, "", UnwindBB);
227
2.52k
    CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
228
2.52k
229
2.52k
    // We never expect _Unwind_Resume to return.
230
2.52k
    new UnreachableInst(Ctx, UnwindBB);
231
2.52k
    return true;
232
2.52k
  }
233
173
234
173
  BasicBlock *UnwindBB = BasicBlock::Create(Ctx, "unwind_resume", &Fn);
235
173
  PHINode *PN = PHINode::Create(Type::getInt8PtrTy(Ctx), ResumesLeft,
236
173
                                "exn.obj", UnwindBB);
237
173
238
173
  // Extract the exception object from the ResumeInst and add it to the PHI node
239
173
  // that feeds the _Unwind_Resume call.
240
383
  for (ResumeInst *RI : Resumes) {
241
383
    BasicBlock *Parent = RI->getParent();
242
383
    BranchInst::Create(UnwindBB, Parent);
243
383
244
383
    Value *ExnObj = GetExceptionObject(RI);
245
383
    PN->addIncoming(ExnObj, Parent);
246
383
247
383
    ++NumResumesLowered;
248
383
  }
249
576k
250
576k
  // Call the function.
251
576k
  CallInst *CI = CallInst::Create(RewindFunction, PN, "", UnwindBB);
252
576k
  CI->setCallingConv(TLI->getLibcallCallingConv(RTLIB::UNWIND_RESUME));
253
576k
254
576k
  // We never expect _Unwind_Resume to return.
255
576k
  new UnreachableInst(Ctx, UnwindBB);
256
576k
  return true;
257
576k
}
258
259
576k
bool DwarfEHPrepare::runOnFunction(Function &Fn) {
260
576k
  const TargetMachine &TM =
261
576k
      getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
262
576k
  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
263
576k
  TLI = TM.getSubtargetImpl(Fn)->getTargetLowering();
264
576k
  bool Changed = InsertUnwindResumeCalls(Fn);
265
576k
  DT = nullptr;
266
576k
  TLI = nullptr;
267
576k
  return Changed;
268
576k
}