Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/MachineModuleInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/CodeGen/MachineModuleInfo.h"
10
#include "llvm/ADT/ArrayRef.h"
11
#include "llvm/ADT/DenseMap.h"
12
#include "llvm/ADT/PostOrderIterator.h"
13
#include "llvm/ADT/StringRef.h"
14
#include "llvm/ADT/TinyPtrVector.h"
15
#include "llvm/CodeGen/MachineFunction.h"
16
#include "llvm/CodeGen/Passes.h"
17
#include "llvm/IR/BasicBlock.h"
18
#include "llvm/IR/DerivedTypes.h"
19
#include "llvm/IR/Instructions.h"
20
#include "llvm/IR/Module.h"
21
#include "llvm/IR/Value.h"
22
#include "llvm/IR/ValueHandle.h"
23
#include "llvm/MC/MCContext.h"
24
#include "llvm/MC/MCSymbol.h"
25
#include "llvm/Pass.h"
26
#include "llvm/Support/Casting.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include "llvm/Target/TargetLoweringObjectFile.h"
29
#include "llvm/Target/TargetMachine.h"
30
#include <algorithm>
31
#include <cassert>
32
#include <memory>
33
#include <utility>
34
#include <vector>
35
36
using namespace llvm;
37
using namespace llvm::dwarf;
38
39
// Handle the Pass registration stuff necessary to use DataLayout's.
40
INITIALIZE_PASS(MachineModuleInfo, "machinemoduleinfo",
41
                "Machine Module Information", false, false)
42
char MachineModuleInfo::ID = 0;
43
44
// Out of line virtual method.
45
27.8k
MachineModuleInfoImpl::~MachineModuleInfoImpl() = default;
46
47
namespace llvm {
48
49
class MMIAddrLabelMapCallbackPtr final : CallbackVH {
50
  MMIAddrLabelMap *Map = nullptr;
51
52
public:
53
  MMIAddrLabelMapCallbackPtr() = default;
54
579
  MMIAddrLabelMapCallbackPtr(Value *V) : CallbackVH(V) {}
55
56
0
  void setPtr(BasicBlock *BB) {
57
0
    ValueHandleBase::operator=(BB);
58
0
  }
59
60
579
  void setMap(MMIAddrLabelMap *map) { Map = map; }
61
62
  void deleted() override;
63
  void allUsesReplacedWith(Value *V2) override;
64
};
65
66
class MMIAddrLabelMap {
67
  MCContext &Context;
68
  struct AddrLabelSymEntry {
69
    /// The symbols for the label.
70
    TinyPtrVector<MCSymbol *> Symbols;
71
72
    Function *Fn;   // The containing function of the BasicBlock.
73
    unsigned Index; // The index in BBCallbacks for the BasicBlock.
74
  };
75
76
  DenseMap<AssertingVH<BasicBlock>, AddrLabelSymEntry> AddrLabelSymbols;
77
78
  /// Callbacks for the BasicBlock's that we have entries for.  We use this so
79
  /// we get notified if a block is deleted or RAUWd.
80
  std::vector<MMIAddrLabelMapCallbackPtr> BBCallbacks;
81
82
  /// This is a per-function list of symbols whose corresponding BasicBlock got
83
  /// deleted.  These symbols need to be emitted at some point in the file, so
84
  /// AsmPrinter emits them after the function body.
85
  DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>
86
    DeletedAddrLabelsNeedingEmission;
87
88
public:
89
244
  MMIAddrLabelMap(MCContext &context) : Context(context) {}
90
91
243
  ~MMIAddrLabelMap() {
92
243
    assert(DeletedAddrLabelsNeedingEmission.empty() &&
93
243
           "Some labels for deleted blocks never got emitted");
94
243
  }
95
96
  ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(BasicBlock *BB);
97
98
  void takeDeletedSymbolsForFunction(Function *F,
99
                                     std::vector<MCSymbol*> &Result);
100
101
  void UpdateForDeletedBlock(BasicBlock *BB);
102
  void UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New);
103
};
104
105
} // end namespace llvm
106
107
1.43k
ArrayRef<MCSymbol *> MMIAddrLabelMap::getAddrLabelSymbolToEmit(BasicBlock *BB) {
108
1.43k
  assert(BB->hasAddressTaken() &&
109
1.43k
         "Shouldn't get label for block without address taken");
110
1.43k
  AddrLabelSymEntry &Entry = AddrLabelSymbols[BB];
111
1.43k
112
1.43k
  // If we already had an entry for this block, just return it.
113
1.43k
  if (!Entry.Symbols.empty()) {
114
857
    assert(BB->getParent() == Entry.Fn && "Parent changed");
115
857
    return Entry.Symbols;
116
857
  }
117
579
118
579
  // Otherwise, this is a new entry, create a new symbol for it and add an
119
579
  // entry to BBCallbacks so we can be notified if the BB is deleted or RAUWd.
120
579
  BBCallbacks.emplace_back(BB);
121
579
  BBCallbacks.back().setMap(this);
122
579
  Entry.Index = BBCallbacks.size() - 1;
123
579
  Entry.Fn = BB->getParent();
124
579
  Entry.Symbols.push_back(Context.createTempSymbol());
125
579
  return Entry.Symbols;
126
579
}
127
128
/// If we have any deleted symbols for F, return them.
129
void MMIAddrLabelMap::
130
151
takeDeletedSymbolsForFunction(Function *F, std::vector<MCSymbol*> &Result) {
131
151
  DenseMap<AssertingVH<Function>, std::vector<MCSymbol*>>::iterator I =
132
151
    DeletedAddrLabelsNeedingEmission.find(F);
133
151
134
151
  // If there are no entries for the function, just return.
135
151
  if (I == DeletedAddrLabelsNeedingEmission.end()) return;
136
0
137
0
  // Otherwise, take the list.
138
0
  std::swap(Result, I->second);
139
0
  DeletedAddrLabelsNeedingEmission.erase(I);
140
0
}
141
142
0
void MMIAddrLabelMap::UpdateForDeletedBlock(BasicBlock *BB) {
143
0
  // If the block got deleted, there is no need for the symbol.  If the symbol
144
0
  // was already emitted, we can just forget about it, otherwise we need to
145
0
  // queue it up for later emission when the function is output.
146
0
  AddrLabelSymEntry Entry = std::move(AddrLabelSymbols[BB]);
147
0
  AddrLabelSymbols.erase(BB);
148
0
  assert(!Entry.Symbols.empty() && "Didn't have a symbol, why a callback?");
149
0
  BBCallbacks[Entry.Index] = nullptr;  // Clear the callback.
150
0
151
0
  assert((BB->getParent() == nullptr || BB->getParent() == Entry.Fn) &&
152
0
         "Block/parent mismatch");
153
0
154
0
  for (MCSymbol *Sym : Entry.Symbols) {
155
0
    if (Sym->isDefined())
156
0
      return;
157
0
158
0
    // If the block is not yet defined, we need to emit it at the end of the
159
0
    // function.  Add the symbol to the DeletedAddrLabelsNeedingEmission list
160
0
    // for the containing Function.  Since the block is being deleted, its
161
0
    // parent may already be removed, we have to get the function from 'Entry'.
162
0
    DeletedAddrLabelsNeedingEmission[Entry.Fn].push_back(Sym);
163
0
  }
164
0
}
165
166
0
void MMIAddrLabelMap::UpdateForRAUWBlock(BasicBlock *Old, BasicBlock *New) {
167
0
  // Get the entry for the RAUW'd block and remove it from our map.
168
0
  AddrLabelSymEntry OldEntry = std::move(AddrLabelSymbols[Old]);
169
0
  AddrLabelSymbols.erase(Old);
170
0
  assert(!OldEntry.Symbols.empty() && "Didn't have a symbol, why a callback?");
171
0
172
0
  AddrLabelSymEntry &NewEntry = AddrLabelSymbols[New];
173
0
174
0
  // If New is not address taken, just move our symbol over to it.
175
0
  if (NewEntry.Symbols.empty()) {
176
0
    BBCallbacks[OldEntry.Index].setPtr(New);    // Update the callback.
177
0
    NewEntry = std::move(OldEntry);             // Set New's entry.
178
0
    return;
179
0
  }
180
0
181
0
  BBCallbacks[OldEntry.Index] = nullptr;    // Update the callback.
182
0
183
0
  // Otherwise, we need to add the old symbols to the new block's set.
184
0
  NewEntry.Symbols.insert(NewEntry.Symbols.end(), OldEntry.Symbols.begin(),
185
0
                          OldEntry.Symbols.end());
186
0
}
187
188
0
void MMIAddrLabelMapCallbackPtr::deleted() {
189
0
  Map->UpdateForDeletedBlock(cast<BasicBlock>(getValPtr()));
190
0
}
191
192
0
void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
193
0
  Map->UpdateForRAUWBlock(cast<BasicBlock>(getValPtr()), cast<BasicBlock>(V2));
194
0
}
195
196
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
197
  : ImmutablePass(ID), TM(*TM),
198
    Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
199
38.4k
            TM->getObjFileLowering(), nullptr, false) {
200
38.4k
  initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
201
38.4k
}
202
203
38.2k
MachineModuleInfo::~MachineModuleInfo() = default;
204
205
38.1k
bool MachineModuleInfo::doInitialization(Module &M) {
206
38.1k
  ObjFileMMI = nullptr;
207
38.1k
  CurCallSite = 0;
208
38.1k
  UsesMSVCFloatingPoint = UsesMorestackAddr = false;
209
38.1k
  HasSplitStack = HasNosplitStack = false;
210
38.1k
  AddrLabelSymbols = nullptr;
211
38.1k
  TheModule = &M;
212
38.1k
  DbgInfoAvailable = !llvm::empty(M.debug_compile_units());
213
38.1k
  return false;
214
38.1k
}
215
216
37.9k
bool MachineModuleInfo::doFinalization(Module &M) {
217
37.9k
  Personalities.clear();
218
37.9k
219
37.9k
  delete AddrLabelSymbols;
220
37.9k
  AddrLabelSymbols = nullptr;
221
37.9k
222
37.9k
  Context.reset();
223
37.9k
224
37.9k
  delete ObjFileMMI;
225
37.9k
  ObjFileMMI = nullptr;
226
37.9k
227
37.9k
  return false;
228
37.9k
}
229
230
//===- Address of Block Management ----------------------------------------===//
231
232
ArrayRef<MCSymbol *>
233
1.43k
MachineModuleInfo::getAddrLabelSymbolToEmit(const BasicBlock *BB) {
234
1.43k
  // Lazily create AddrLabelSymbols.
235
1.43k
  if (!AddrLabelSymbols)
236
244
    AddrLabelSymbols = new MMIAddrLabelMap(Context);
237
1.43k
 return AddrLabelSymbols->getAddrLabelSymbolToEmit(const_cast<BasicBlock*>(BB));
238
1.43k
}
239
240
void MachineModuleInfo::
241
takeDeletedSymbolsForFunction(const Function *F,
242
498k
                              std::vector<MCSymbol*> &Result) {
243
498k
  // If no blocks have had their addresses taken, we're done.
244
498k
  if (!AddrLabelSymbols) 
return498k
;
245
144
  return AddrLabelSymbols->
246
144
     takeDeletedSymbolsForFunction(const_cast<Function*>(F), Result);
247
144
}
248
249
/// \name Exception Handling
250
/// \{
251
252
17.6k
void MachineModuleInfo::addPersonality(const Function *Personality) {
253
17.6k
  for (unsigned i = 0; i < Personalities.size(); 
++i8
)
254
16.9k
    if (Personalities[i] == Personality)
255
16.9k
      return;
256
17.6k
  Personalities.push_back(Personality);
257
713
}
258
259
/// \}
260
261
MachineFunction *
262
268k
MachineModuleInfo::getMachineFunction(const Function &F) const {
263
268k
  auto I = MachineFunctions.find(&F);
264
268k
  return I != MachineFunctions.end() ? 
I->second.get()257k
:
nullptr11.2k
;
265
268k
}
266
267
MachineFunction &
268
52.2M
MachineModuleInfo::getOrCreateMachineFunction(const Function &F) {
269
52.2M
  // Shortcut for the common case where a sequence of MachineFunctionPasses
270
52.2M
  // all query for the same Function.
271
52.2M
  if (LastRequest == &F)
272
51.4M
    return *LastResult;
273
777k
274
777k
  auto I = MachineFunctions.insert(
275
777k
      std::make_pair(&F, std::unique_ptr<MachineFunction>()));
276
777k
  MachineFunction *MF;
277
777k
  if (I.second) {
278
510k
    // No pre-existing machine function, create a new one.
279
510k
    const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
280
510k
    MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
281
510k
    // Update the set entry.
282
510k
    I.first->second.reset(MF);
283
510k
  } else {
284
266k
    MF = I.first->second.get();
285
266k
  }
286
777k
287
777k
  LastRequest = &F;
288
777k
  LastResult = MF;
289
777k
  return *MF;
290
777k
}
291
292
510k
void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
293
510k
  MachineFunctions.erase(&F);
294
510k
  LastRequest = nullptr;
295
510k
  LastResult = nullptr;
296
510k
}
297
298
namespace {
299
300
/// This pass frees the MachineFunction object associated with a Function.
301
class FreeMachineFunction : public FunctionPass {
302
public:
303
  static char ID;
304
305
38.3k
  FreeMachineFunction() : FunctionPass(ID) {}
306
307
38.3k
  void getAnalysisUsage(AnalysisUsage &AU) const override {
308
38.3k
    AU.addRequired<MachineModuleInfo>();
309
38.3k
    AU.addPreserved<MachineModuleInfo>();
310
38.3k
  }
311
312
510k
  bool runOnFunction(Function &F) override {
313
510k
    MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
314
510k
    MMI.deleteMachineFunctionFor(F);
315
510k
    return true;
316
510k
  }
317
318
510k
  StringRef getPassName() const override {
319
510k
    return "Free MachineFunction";
320
510k
  }
321
};
322
323
} // end anonymous namespace
324
325
char FreeMachineFunction::ID;
326
327
38.3k
FunctionPass *llvm::createFreeMachineFunctionPass() {
328
38.3k
  return new FreeMachineFunction();
329
38.3k
}