Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/MachineFunction.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MachineFunction.cpp ------------------------------------------------===//
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
// Collect native machine code information for a function.  This allows
10
// target-specific information about the generated code to be stored with each
11
// function.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "llvm/CodeGen/MachineFunction.h"
16
#include "llvm/ADT/BitVector.h"
17
#include "llvm/ADT/DenseMap.h"
18
#include "llvm/ADT/DenseSet.h"
19
#include "llvm/ADT/STLExtras.h"
20
#include "llvm/ADT/SmallString.h"
21
#include "llvm/ADT/SmallVector.h"
22
#include "llvm/ADT/StringRef.h"
23
#include "llvm/ADT/Twine.h"
24
#include "llvm/Analysis/ConstantFolding.h"
25
#include "llvm/Analysis/EHPersonalities.h"
26
#include "llvm/CodeGen/MachineBasicBlock.h"
27
#include "llvm/CodeGen/MachineConstantPool.h"
28
#include "llvm/CodeGen/MachineFrameInfo.h"
29
#include "llvm/CodeGen/MachineInstr.h"
30
#include "llvm/CodeGen/MachineJumpTableInfo.h"
31
#include "llvm/CodeGen/MachineMemOperand.h"
32
#include "llvm/CodeGen/MachineModuleInfo.h"
33
#include "llvm/CodeGen/MachineRegisterInfo.h"
34
#include "llvm/CodeGen/PseudoSourceValue.h"
35
#include "llvm/CodeGen/TargetFrameLowering.h"
36
#include "llvm/CodeGen/TargetLowering.h"
37
#include "llvm/CodeGen/TargetRegisterInfo.h"
38
#include "llvm/CodeGen/TargetSubtargetInfo.h"
39
#include "llvm/CodeGen/WasmEHFuncInfo.h"
40
#include "llvm/CodeGen/WinEHFuncInfo.h"
41
#include "llvm/Config/llvm-config.h"
42
#include "llvm/IR/Attributes.h"
43
#include "llvm/IR/BasicBlock.h"
44
#include "llvm/IR/Constant.h"
45
#include "llvm/IR/DataLayout.h"
46
#include "llvm/IR/DebugInfoMetadata.h"
47
#include "llvm/IR/DerivedTypes.h"
48
#include "llvm/IR/Function.h"
49
#include "llvm/IR/GlobalValue.h"
50
#include "llvm/IR/Instruction.h"
51
#include "llvm/IR/Instructions.h"
52
#include "llvm/IR/Metadata.h"
53
#include "llvm/IR/Module.h"
54
#include "llvm/IR/ModuleSlotTracker.h"
55
#include "llvm/IR/Value.h"
56
#include "llvm/MC/MCContext.h"
57
#include "llvm/MC/MCSymbol.h"
58
#include "llvm/MC/SectionKind.h"
59
#include "llvm/Support/Casting.h"
60
#include "llvm/Support/CommandLine.h"
61
#include "llvm/Support/Compiler.h"
62
#include "llvm/Support/DOTGraphTraits.h"
63
#include "llvm/Support/Debug.h"
64
#include "llvm/Support/ErrorHandling.h"
65
#include "llvm/Support/GraphWriter.h"
66
#include "llvm/Support/raw_ostream.h"
67
#include "llvm/Target/TargetMachine.h"
68
#include <algorithm>
69
#include <cassert>
70
#include <cstddef>
71
#include <cstdint>
72
#include <iterator>
73
#include <string>
74
#include <utility>
75
#include <vector>
76
77
using namespace llvm;
78
79
#define DEBUG_TYPE "codegen"
80
81
static cl::opt<unsigned>
82
AlignAllFunctions("align-all-functions",
83
                  cl::desc("Force the alignment of all functions."),
84
                  cl::init(0), cl::Hidden);
85
86
7.27k
static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
87
7.27k
  using P = MachineFunctionProperties::Property;
88
7.27k
89
7.27k
  switch(Prop) {
90
7.27k
  
case P::FailedISel: return "FailedISel"0
;
91
7.27k
  
case P::IsSSA: return "IsSSA"1.24k
;
92
7.27k
  
case P::Legalized: return "Legalized"27
;
93
7.27k
  
case P::NoPHIs: return "NoPHIs"1.80k
;
94
7.27k
  
case P::NoVRegs: return "NoVRegs"1.33k
;
95
7.27k
  
case P::RegBankSelected: return "RegBankSelected"2
;
96
7.27k
  
case P::Selected: return "Selected"1
;
97
7.27k
  
case P::TracksLiveness: return "TracksLiveness"2.86k
;
98
0
  }
99
0
  llvm_unreachable("Invalid machine function property");
100
0
}
101
102
// Pin the vtable to this file.
103
0
void MachineFunction::Delegate::anchor() {}
104
105
2.95k
void MachineFunctionProperties::print(raw_ostream &OS) const {
106
2.95k
  const char *Separator = "";
107
26.5k
  for (BitVector::size_type I = 0; I < Properties.size(); 
++I23.6k
) {
108
23.6k
    if (!Properties[I])
109
16.3k
      continue;
110
7.27k
    OS << Separator << getPropertyName(static_cast<Property>(I));
111
7.27k
    Separator = ", ";
112
7.27k
  }
113
2.95k
}
114
115
//===----------------------------------------------------------------------===//
116
// MachineFunction implementation
117
//===----------------------------------------------------------------------===//
118
119
// Out-of-line virtual method.
120
505k
MachineFunctionInfo::~MachineFunctionInfo() = default;
121
122
3.54M
void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
123
3.54M
  MBB->getParent()->DeleteMachineBasicBlock(MBB);
124
3.54M
}
125
126
static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
127
527k
                                           const Function &F) {
128
527k
  if (F.hasFnAttribute(Attribute::StackAlignment))
129
33
    return F.getFnStackAlignment();
130
527k
  return STI->getFrameLowering()->getStackAlignment();
131
527k
}
132
133
MachineFunction::MachineFunction(const Function &F,
134
                                 const LLVMTargetMachine &Target,
135
                                 const TargetSubtargetInfo &STI,
136
                                 unsigned FunctionNum, MachineModuleInfo &mmi)
137
510k
    : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) {
138
510k
  FunctionNumber = FunctionNum;
139
510k
  init();
140
510k
}
141
142
66.5M
void MachineFunction::handleInsertion(MachineInstr &MI) {
143
66.5M
  if (TheDelegate)
144
23.9M
    TheDelegate->MF_HandleInsertion(MI);
145
66.5M
}
146
147
41.6M
void MachineFunction::handleRemoval(MachineInstr &MI) {
148
41.6M
  if (TheDelegate)
149
2.37M
    TheDelegate->MF_HandleRemoval(MI);
150
41.6M
}
151
152
527k
void MachineFunction::init() {
153
527k
  // Assume the function starts in SSA form with correct liveness.
154
527k
  Properties.set(MachineFunctionProperties::Property::IsSSA);
155
527k
  Properties.set(MachineFunctionProperties::Property::TracksLiveness);
156
527k
  if (STI->getRegisterInfo())
157
527k
    RegInfo = new (Allocator) MachineRegisterInfo(this);
158
0
  else
159
0
    RegInfo = nullptr;
160
527k
161
527k
  MFInfo = nullptr;
162
527k
  // We can realign the stack if the target supports it and the user hasn't
163
527k
  // explicitly asked us not to.
164
527k
  bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
165
527k
                      
!F.hasFnAttribute("no-realign-stack")519k
;
166
527k
  FrameInfo = new (Allocator) MachineFrameInfo(
167
527k
      getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP,
168
527k
      /*ForcedRealign=*/CanRealignSP &&
169
527k
          
F.hasFnAttribute(Attribute::StackAlignment)518k
);
170
527k
171
527k
  if (F.hasFnAttribute(Attribute::StackAlignment))
172
33
    FrameInfo->ensureMaxAlignment(F.getFnStackAlignment());
173
527k
174
527k
  ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
175
527k
  Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
176
527k
177
527k
  // FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
178
527k
  // FIXME: Use Function::hasOptSize().
179
527k
  if (!F.hasFnAttribute(Attribute::OptimizeForSize))
180
524k
    Alignment = std::max(Alignment,
181
524k
                         STI->getTargetLowering()->getPrefFunctionAlignment());
182
527k
183
527k
  if (AlignAllFunctions)
184
0
    Alignment = AlignAllFunctions;
185
527k
186
527k
  JumpTableInfo = nullptr;
187
527k
188
527k
  if (isFuncletEHPersonality(classifyEHPersonality(
189
527k
          F.hasPersonalityFn() ? 
F.getPersonalityFn()5.96k
:
nullptr521k
))) {
190
107
    WinEHInfo = new (Allocator) WinEHFuncInfo();
191
107
  }
192
527k
193
527k
  if (isScopedEHPersonality(classifyEHPersonality(
194
527k
          F.hasPersonalityFn() ? 
F.getPersonalityFn()5.96k
:
nullptr521k
))) {
195
151
    WasmEHInfo = new (Allocator) WasmEHFuncInfo();
196
151
  }
197
527k
198
527k
  assert(Target.isCompatibleDataLayout(getDataLayout()) &&
199
527k
         "Can't create a MachineFunction using a Module with a "
200
527k
         "Target-incompatible DataLayout attached\n");
201
527k
202
527k
  PSVManager =
203
527k
    llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget().
204
527k
                                                  getInstrInfo()));
205
527k
}
206
207
510k
MachineFunction::~MachineFunction() {
208
510k
  clear();
209
510k
}
210
211
526k
void MachineFunction::clear() {
212
526k
  Properties.reset();
213
526k
  // Don't call destructors on MachineInstr and MachineOperand. All of their
214
526k
  // memory comes from the BumpPtrAllocator which is about to be purged.
215
526k
  //
216
526k
  // Do call MachineBasicBlock destructors, it contains std::vectors.
217
3.76M
  for (iterator I = begin(), E = end(); I != E; 
I = BasicBlocks.erase(I)3.23M
)
218
3.23M
    I->Insts.clearAndLeakNodesUnsafely();
219
526k
  MBBNumbering.clear();
220
526k
221
526k
  InstructionRecycler.clear(Allocator);
222
526k
  OperandRecycler.clear(Allocator);
223
526k
  BasicBlockRecycler.clear(Allocator);
224
526k
  CodeViewAnnotations.clear();
225
526k
  VariableDbgInfos.clear();
226
526k
  if (
RegInfo526k
) {
227
526k
    RegInfo->~MachineRegisterInfo();
228
526k
    Allocator.Deallocate(RegInfo);
229
526k
  }
230
526k
  if (MFInfo) {
231
505k
    MFInfo->~MachineFunctionInfo();
232
505k
    Allocator.Deallocate(MFInfo);
233
505k
  }
234
526k
235
526k
  FrameInfo->~MachineFrameInfo();
236
526k
  Allocator.Deallocate(FrameInfo);
237
526k
238
526k
  ConstantPool->~MachineConstantPool();
239
526k
  Allocator.Deallocate(ConstantPool);
240
526k
241
526k
  if (JumpTableInfo) {
242
4.31k
    JumpTableInfo->~MachineJumpTableInfo();
243
4.31k
    Allocator.Deallocate(JumpTableInfo);
244
4.31k
  }
245
526k
246
526k
  if (WinEHInfo) {
247
107
    WinEHInfo->~WinEHFuncInfo();
248
107
    Allocator.Deallocate(WinEHInfo);
249
107
  }
250
526k
251
526k
  if (WasmEHInfo) {
252
151
    WasmEHInfo->~WasmEHFuncInfo();
253
151
    Allocator.Deallocate(WasmEHInfo);
254
151
  }
255
526k
}
256
257
90.3M
const DataLayout &MachineFunction::getDataLayout() const {
258
90.3M
  return F.getParent()->getDataLayout();
259
90.3M
}
260
261
/// Get the JumpTableInfo for this function.
262
/// If it does not already exist, allocate one.
263
MachineJumpTableInfo *MachineFunction::
264
4.58k
getOrCreateJumpTableInfo(unsigned EntryKind) {
265
4.58k
  if (JumpTableInfo) 
return JumpTableInfo274
;
266
4.31k
267
4.31k
  JumpTableInfo = new (Allocator)
268
4.31k
    MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
269
4.31k
  return JumpTableInfo;
270
4.31k
}
271
272
/// Should we be emitting segmented stack stuff for the function
273
648k
bool MachineFunction::shouldSplitStack() const {
274
648k
  return getFunction().hasFnAttribute("split-stack");
275
648k
}
276
277
LLVM_NODISCARD unsigned
278
501k
MachineFunction::addFrameInst(const MCCFIInstruction &Inst) {
279
501k
  FrameInstructions.push_back(Inst);
280
501k
  return FrameInstructions.size() - 1;
281
501k
}
282
283
/// This discards all of the MachineBasicBlock numbers and recomputes them.
284
/// This guarantees that the MBB numbers are sequential, dense, and match the
285
/// ordering of the blocks within the function.  If a specific MachineBasicBlock
286
/// is specified, only that block and those after it are renumbered.
287
1.60M
void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
288
1.60M
  if (empty()) 
{ MBBNumbering.clear(); return; }0
289
1.60M
  MachineFunction::iterator MBBI, E = end();
290
1.60M
  if (MBB == nullptr)
291
1.60M
    MBBI = begin();
292
1.00k
  else
293
1.00k
    MBBI = MBB->getIterator();
294
1.60M
295
1.60M
  // Figure out the block number this should have.
296
1.60M
  unsigned BlockNo = 0;
297
1.60M
  if (MBBI != begin())
298
991
    BlockNo = std::prev(MBBI)->getNumber() + 1;
299
1.60M
300
13.5M
  for (; MBBI != E; 
++MBBI, ++BlockNo11.9M
) {
301
11.9M
    if (MBBI->getNumber() != (int)BlockNo) {
302
6.72M
      // Remove use of the old number.
303
6.72M
      if (MBBI->getNumber() != -1) {
304
4.10M
        assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
305
4.10M
               "MBB number mismatch!");
306
4.10M
        MBBNumbering[MBBI->getNumber()] = nullptr;
307
4.10M
      }
308
6.72M
309
6.72M
      // If BlockNo is already taken, set that block's number to -1.
310
6.72M
      if (MBBNumbering[BlockNo])
311
2.61M
        MBBNumbering[BlockNo]->setNumber(-1);
312
6.72M
313
6.72M
      MBBNumbering[BlockNo] = &*MBBI;
314
6.72M
      MBBI->setNumber(BlockNo);
315
6.72M
    }
316
11.9M
  }
317
1.60M
318
1.60M
  // Okay, all the blocks are renumbered.  If we have compactified the block
319
1.60M
  // numbering, shrink MBBNumbering now.
320
1.60M
  assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
321
1.60M
  MBBNumbering.resize(BlockNo);
322
1.60M
}
323
324
/// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
325
MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
326
                                                  const DebugLoc &DL,
327
64.8M
                                                  bool NoImp) {
328
64.8M
  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
329
64.8M
    MachineInstr(*this, MCID, DL, NoImp);
330
64.8M
}
331
332
/// Create a new MachineInstr which is a copy of the 'Orig' instruction,
333
/// identical in all ways except the instruction has no parent, prev, or next.
334
MachineInstr *
335
1.74M
MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
336
1.74M
  return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
337
1.74M
             MachineInstr(*this, *Orig);
338
1.74M
}
339
340
MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
341
685k
    MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
342
685k
  MachineInstr *FirstClone = nullptr;
343
685k
  MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
344
685k
  while (true) {
345
685k
    MachineInstr *Cloned = CloneMachineInstr(&*I);
346
685k
    MBB.insert(InsertBefore, Cloned);
347
685k
    if (FirstClone == nullptr) {
348
685k
      FirstClone = Cloned;
349
685k
    } else {
350
193
      Cloned->bundleWithPred();
351
193
    }
352
685k
353
685k
    if (!I->isBundledWithSucc())
354
685k
      break;
355
193
    ++I;
356
193
  }
357
685k
  return *FirstClone;
358
685k
}
359
360
/// Delete the given MachineInstr.
361
///
362
/// This function also serves as the MachineInstr destructor - the real
363
/// ~MachineInstr() destructor must be empty.
364
void
365
41.6M
MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
366
41.6M
  // Verify that a call site info is at valid state. This assertion should
367
41.6M
  // be triggered during the implementation of support for the
368
41.6M
  // call site info of a new architecture. If the assertion is triggered,
369
41.6M
  // back trace will tell where to insert a call to updateCallSiteInfo().
370
41.6M
  assert((!MI->isCall(MachineInstr::IgnoreBundle) ||
371
41.6M
          CallSitesInfo.find(MI) == CallSitesInfo.end()) &&
372
41.6M
         "Call site info was not updated!");
373
41.6M
  // Strip it for parts. The operand array and the MI object itself are
374
41.6M
  // independently recyclable.
375
41.6M
  if (MI->Operands)
376
41.5M
    deallocateOperandArray(MI->CapOperands, MI->Operands);
377
41.6M
  // Don't call ~MachineInstr() which must be trivial anyway because
378
41.6M
  // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
379
41.6M
  // destructors.
380
41.6M
  InstructionRecycler.Deallocate(Allocator, MI);
381
41.6M
}
382
383
/// Allocate a new MachineBasicBlock. Use this instead of
384
/// `new MachineBasicBlock'.
385
MachineBasicBlock *
386
3.78M
MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
387
3.78M
  return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
388
3.78M
             MachineBasicBlock(*this, bb);
389
3.78M
}
390
391
/// Delete the given MachineBasicBlock.
392
void
393
3.78M
MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
394
3.78M
  assert(MBB->getParent() == this && "MBB parent mismatch!");
395
3.78M
  MBB->~MachineBasicBlock();
396
3.78M
  BasicBlockRecycler.Deallocate(Allocator, MBB);
397
3.78M
}
398
399
MachineMemOperand *MachineFunction::getMachineMemOperand(
400
    MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
401
    unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
402
    SyncScope::ID SSID, AtomicOrdering Ordering,
403
7.78M
    AtomicOrdering FailureOrdering) {
404
7.78M
  return new (Allocator)
405
7.78M
      MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
406
7.78M
                        SSID, Ordering, FailureOrdering);
407
7.78M
}
408
409
MachineMemOperand *
410
MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
411
6.99k
                                      int64_t Offset, uint64_t Size) {
412
6.99k
  const MachinePointerInfo &PtrInfo = MMO->getPointerInfo();
413
6.99k
414
6.99k
  // If there is no pointer value, the offset isn't tracked so we need to adjust
415
6.99k
  // the base alignment.
416
6.99k
  unsigned Align = PtrInfo.V.isNull()
417
6.99k
                       ? 
MinAlign(MMO->getBaseAlignment(), Offset)60
418
6.99k
                       : 
MMO->getBaseAlignment()6.93k
;
419
6.99k
420
6.99k
  return new (Allocator)
421
6.99k
      MachineMemOperand(PtrInfo.getWithOffset(Offset), MMO->getFlags(), Size,
422
6.99k
                        Align, AAMDNodes(), nullptr, MMO->getSyncScopeID(),
423
6.99k
                        MMO->getOrdering(), MMO->getFailureOrdering());
424
6.99k
}
425
426
MachineMemOperand *
427
MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
428
275k
                                      const AAMDNodes &AAInfo) {
429
275k
  MachinePointerInfo MPI = MMO->getValue() ?
430
275k
             MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
431
275k
             
MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset())0
;
432
275k
433
275k
  return new (Allocator)
434
275k
             MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
435
275k
                               MMO->getBaseAlignment(), AAInfo,
436
275k
                               MMO->getRanges(), MMO->getSyncScopeID(),
437
275k
                               MMO->getOrdering(), MMO->getFailureOrdering());
438
275k
}
439
440
MachineMemOperand *
441
MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
442
8
                                      MachineMemOperand::Flags Flags) {
443
8
  return new (Allocator) MachineMemOperand(
444
8
      MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlignment(),
445
8
      MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(),
446
8
      MMO->getOrdering(), MMO->getFailureOrdering());
447
8
}
448
449
MachineInstr::ExtraInfo *
450
MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
451
                                   MCSymbol *PreInstrSymbol,
452
2.19M
                                   MCSymbol *PostInstrSymbol) {
453
2.19M
  return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
454
2.19M
                                         PostInstrSymbol);
455
2.19M
}
456
457
928
const char *MachineFunction::createExternalSymbolName(StringRef Name) {
458
928
  char *Dest = Allocator.Allocate<char>(Name.size() + 1);
459
928
  llvm::copy(Name, Dest);
460
928
  Dest[Name.size()] = 0;
461
928
  return Dest;
462
928
}
463
464
264
uint32_t *MachineFunction::allocateRegMask() {
465
264
  unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs();
466
264
  unsigned Size = MachineOperand::getRegMaskSize(NumRegs);
467
264
  uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
468
264
  memset(Mask, 0, Size * sizeof(Mask[0]));
469
264
  return Mask;
470
264
}
471
472
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
473
LLVM_DUMP_METHOD void MachineFunction::dump() const {
474
  print(dbgs());
475
}
476
#endif
477
478
199k
StringRef MachineFunction::getName() const {
479
199k
  return getFunction().getName();
480
199k
}
481
482
2.95k
void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
483
2.95k
  OS << "# Machine code for function " << getName() << ": ";
484
2.95k
  getProperties().print(OS);
485
2.95k
  OS << '\n';
486
2.95k
487
2.95k
  // Print Frame Information
488
2.95k
  FrameInfo->print(*this, OS);
489
2.95k
490
2.95k
  // Print JumpTable Information
491
2.95k
  if (JumpTableInfo)
492
1.95k
    JumpTableInfo->print(OS);
493
2.95k
494
2.95k
  // Print Constant Pool
495
2.95k
  ConstantPool->print(OS);
496
2.95k
497
2.95k
  const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
498
2.95k
499
2.95k
  if (RegInfo && !RegInfo->livein_empty()) {
500
2.53k
    OS << "Function Live Ins: ";
501
2.53k
    for (MachineRegisterInfo::livein_iterator
502
6.48k
         I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; 
++I3.95k
) {
503
3.95k
      OS << printReg(I->first, TRI);
504
3.95k
      if (I->second)
505
1.92k
        OS << " in " << printReg(I->second, TRI);
506
3.95k
      if (std::next(I) != E)
507
1.41k
        OS << ", ";
508
3.95k
    }
509
2.53k
    OS << '\n';
510
2.53k
  }
511
2.95k
512
2.95k
  ModuleSlotTracker MST(getFunction().getParent());
513
2.95k
  MST.incorporateFunction(getFunction());
514
31.7k
  for (const auto &BB : *this) {
515
31.7k
    OS << '\n';
516
31.7k
    // If we print the whole function, print it at its most verbose level.
517
31.7k
    BB.print(OS, MST, Indexes, /*IsStandalone=*/true);
518
31.7k
  }
519
2.95k
520
2.95k
  OS << "\n# End machine code for function " << getName() << ".\n\n";
521
2.95k
}
522
523
namespace llvm {
524
525
  template<>
526
  struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
527
0
    DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
528
529
0
    static std::string getGraphName(const MachineFunction *F) {
530
0
      return ("CFG for '" + F->getName() + "' function").str();
531
0
    }
532
533
    std::string getNodeLabel(const MachineBasicBlock *Node,
534
0
                             const MachineFunction *Graph) {
535
0
      std::string OutStr;
536
0
      {
537
0
        raw_string_ostream OSS(OutStr);
538
0
539
0
        if (isSimple()) {
540
0
          OSS << printMBBReference(*Node);
541
0
          if (const BasicBlock *BB = Node->getBasicBlock())
542
0
            OSS << ": " << BB->getName();
543
0
        } else
544
0
          Node->print(OSS);
545
0
      }
546
0
547
0
      if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
548
0
549
0
      // Process string output to make it nicer...
550
0
      for (unsigned i = 0; i != OutStr.length(); ++i)
551
0
        if (OutStr[i] == '\n') {                            // Left justify
552
0
          OutStr[i] = '\\';
553
0
          OutStr.insert(OutStr.begin()+i+1, 'l');
554
0
        }
555
0
      return OutStr;
556
0
    }
557
  };
558
559
} // end namespace llvm
560
561
void MachineFunction::viewCFG() const
562
0
{
563
#ifndef NDEBUG
564
  ViewGraph(this, "mf" + getName());
565
#else
566
  errs() << "MachineFunction::viewCFG is only available in debug builds on "
567
0
         << "systems with Graphviz or gv!\n";
568
0
#endif // NDEBUG
569
0
}
570
571
void MachineFunction::viewCFGOnly() const
572
0
{
573
#ifndef NDEBUG
574
  ViewGraph(this, "mf" + getName(), true);
575
#else
576
  errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
577
0
         << "systems with Graphviz or gv!\n";
578
0
#endif // NDEBUG
579
0
}
580
581
/// Add the specified physical register as a live-in value and
582
/// create a corresponding virtual register for it.
583
unsigned MachineFunction::addLiveIn(unsigned PReg,
584
510k
                                    const TargetRegisterClass *RC) {
585
510k
  MachineRegisterInfo &MRI = getRegInfo();
586
510k
  unsigned VReg = MRI.getLiveInVirtReg(PReg);
587
510k
  if (VReg) {
588
94
    const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
589
94
    (void)VRegRC;
590
94
    // A physical register can be added several times.
591
94
    // Between two calls, the register class of the related virtual register
592
94
    // may have been constrained to match some operation constraints.
593
94
    // In that case, check that the current register class includes the
594
94
    // physical register and is a sub class of the specified RC.
595
94
    assert((VRegRC == RC || (VRegRC->contains(PReg) &&
596
94
                             RC->hasSubClassEq(VRegRC))) &&
597
94
            "Register class mismatch!");
598
94
    return VReg;
599
94
  }
600
510k
  VReg = MRI.createVirtualRegister(RC);
601
510k
  MRI.addLiveIn(PReg, VReg);
602
510k
  return VReg;
603
510k
}
604
605
/// Return the MCSymbol for the specified non-empty jump table.
606
/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
607
/// normal 'L' label is returned.
608
MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
609
8.78k
                                        bool isLinkerPrivate) const {
610
8.78k
  const DataLayout &DL = getDataLayout();
611
8.78k
  assert(JumpTableInfo && "No jump tables");
612
8.78k
  assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
613
8.78k
614
8.78k
  StringRef Prefix = isLinkerPrivate ? 
DL.getLinkerPrivateGlobalPrefix()9
615
8.78k
                                     : 
DL.getPrivateGlobalPrefix()8.77k
;
616
8.78k
  SmallString<60> Name;
617
8.78k
  raw_svector_ostream(Name)
618
8.78k
    << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
619
8.78k
  return Ctx.getOrCreateSymbol(Name);
620
8.78k
}
621
622
/// Return a function-local symbol to represent the PIC base.
623
15.0k
MCSymbol *MachineFunction::getPICBaseSymbol() const {
624
15.0k
  const DataLayout &DL = getDataLayout();
625
15.0k
  return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
626
15.0k
                               Twine(getFunctionNumber()) + "$pb");
627
15.0k
}
628
629
/// \name Exception Handling
630
/// \{
631
632
LandingPadInfo &
633
66.6k
MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
634
66.6k
  unsigned N = LandingPads.size();
635
1.27M
  for (unsigned i = 0; i < N; 
++i1.21M
) {
636
1.26M
    LandingPadInfo &LP = LandingPads[i];
637
1.26M
    if (LP.LandingPadBlock == LandingPad)
638
48.9k
      return LP;
639
1.26M
  }
640
66.6k
641
66.6k
  LandingPads.push_back(LandingPadInfo(LandingPad));
642
17.6k
  return LandingPads[N];
643
66.6k
}
644
645
void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
646
30.4k
                                MCSymbol *BeginLabel, MCSymbol *EndLabel) {
647
30.4k
  LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
648
30.4k
  LP.BeginLabels.push_back(BeginLabel);
649
30.4k
  LP.EndLabels.push_back(EndLabel);
650
30.4k
}
651
652
17.6k
MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
653
17.6k
  MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
654
17.6k
  LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
655
17.6k
  LP.LandingPadLabel = LandingPadLabel;
656
17.6k
657
17.6k
  const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI();
658
17.6k
  if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) {
659
17.6k
    if (const auto *PF =
660
17.6k
            dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()))
661
17.6k
      getMMI().addPersonality(PF);
662
17.6k
663
17.6k
    if (LPI->isCleanup())
664
12.8k
      addCleanup(LandingPad);
665
17.6k
666
17.6k
    // FIXME: New EH - Add the clauses in reverse order. This isn't 100%
667
17.6k
    //        correct, but we need to do it this way because of how the DWARF EH
668
17.6k
    //        emitter processes the clauses.
669
23.2k
    for (unsigned I = LPI->getNumClauses(); I != 0; 
--I5.67k
) {
670
5.67k
      Value *Val = LPI->getClause(I - 1);
671
5.67k
      if (LPI->isCatch(I - 1)) {
672
5.66k
        addCatchTypeInfo(LandingPad,
673
5.66k
                         dyn_cast<GlobalValue>(Val->stripPointerCasts()));
674
5.66k
      } else {
675
14
        // Add filters in a list.
676
14
        auto *CVal = cast<Constant>(Val);
677
14
        SmallVector<const GlobalValue *, 4> FilterList;
678
14
        for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
679
18
             II != IE; 
++II4
)
680
4
          FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
681
14
682
14
        addFilterTypeInfo(LandingPad, FilterList);
683
14
      }
684
5.67k
    }
685
17.6k
686
17.6k
  } else 
if (const auto *70
CPI70
= dyn_cast<CatchPadInst>(FirstI)) {
687
108
    for (unsigned I = CPI->getNumArgOperands(); I != 0; 
--I58
) {
688
58
      Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts();
689
58
      addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo));
690
58
    }
691
50
692
50
  } else {
693
20
    assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!");
694
20
  }
695
17.6k
696
17.6k
  return LandingPadLabel;
697
17.6k
}
698
699
void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
700
5.72k
                                       ArrayRef<const GlobalValue *> TyInfo) {
701
5.72k
  LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
702
11.4k
  for (unsigned N = TyInfo.size(); N; 
--N5.72k
)
703
5.72k
    LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
704
5.72k
}
705
706
void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
707
14
                                        ArrayRef<const GlobalValue *> TyInfo) {
708
14
  LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
709
14
  std::vector<unsigned> IdsInFilter(TyInfo.size());
710
18
  for (unsigned I = 0, E = TyInfo.size(); I != E; 
++I4
)
711
4
    IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
712
14
  LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
713
14
}
714
715
void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap,
716
4.09k
                                      bool TidyIfNoBeginLabels) {
717
21.7k
  for (unsigned i = 0; i != LandingPads.size(); ) {
718
17.6k
    LandingPadInfo &LandingPad = LandingPads[i];
719
17.6k
    if (LandingPad.LandingPadLabel &&
720
17.6k
        
!LandingPad.LandingPadLabel->isDefined()17.6k
&&
721
17.6k
        
(1.55k
!LPMap1.55k
||
(*LPMap)[LandingPad.LandingPadLabel] == 00
))
722
1.55k
      LandingPad.LandingPadLabel = nullptr;
723
17.6k
724
17.6k
    // Special case: we *should* emit LPs with null LP MBB. This indicates
725
17.6k
    // "nounwind" case.
726
17.6k
    if (!LandingPad.LandingPadLabel && 
LandingPad.LandingPadBlock1.57k
) {
727
1.57k
      LandingPads.erase(LandingPads.begin() + i);
728
1.57k
      continue;
729
1.57k
    }
730
16.1k
731
16.1k
    if (TidyIfNoBeginLabels) {
732
43.1k
      for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; 
++j27.1k
) {
733
27.1k
        MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
734
27.1k
        MCSymbol *EndLabel = LandingPad.EndLabels[j];
735
27.1k
        if ((BeginLabel->isDefined() || 
(0
LPMap0
&&
(*LPMap)[BeginLabel] != 00
)) &&
736
27.1k
            (EndLabel->isDefined() || 
(0
LPMap0
&&
(*LPMap)[EndLabel] != 00
)))
737
27.1k
          continue;
738
0
739
0
        LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
740
0
        LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
741
0
        --j;
742
0
        --e;
743
0
      }
744
16.0k
745
16.0k
      // Remove landing pads with no try-ranges.
746
16.0k
      if (LandingPads[i].BeginLabels.empty()) {
747
2
        LandingPads.erase(LandingPads.begin() + i);
748
2
        continue;
749
2
      }
750
16.1k
    }
751
16.1k
752
16.1k
    // If there is no landing pad, ensure that the list of typeids is empty.
753
16.1k
    // If the only typeid is a cleanup, this is the same as having no typeids.
754
16.1k
    if (!LandingPad.LandingPadBlock ||
755
16.1k
        (LandingPad.TypeIds.size() == 1 && 
!LandingPad.TypeIds[0]15.3k
))
756
11.4k
      LandingPad.TypeIds.clear();
757
16.1k
    ++i;
758
16.1k
  }
759
4.09k
}
760
761
12.8k
void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
762
12.8k
  LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
763
12.8k
  LP.TypeIds.push_back(0);
764
12.8k
}
765
766
void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
767
                                         const Function *Filter,
768
0
                                         const BlockAddress *RecoverBA) {
769
0
  LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
770
0
  SEHHandler Handler;
771
0
  Handler.FilterOrFinally = Filter;
772
0
  Handler.RecoverBA = RecoverBA;
773
0
  LP.SEHHandlers.push_back(Handler);
774
0
}
775
776
void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
777
0
                                           const Function *Cleanup) {
778
0
  LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
779
0
  SEHHandler Handler;
780
0
  Handler.FilterOrFinally = Cleanup;
781
0
  Handler.RecoverBA = nullptr;
782
0
  LP.SEHHandlers.push_back(Handler);
783
0
}
784
785
void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
786
3.55k
                                            ArrayRef<unsigned> Sites) {
787
3.55k
  LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
788
3.55k
}
789
790
5.96k
unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
791
7.10k
  for (unsigned i = 0, N = TypeInfos.size(); i != N; 
++i1.14k
)
792
5.10k
    if (TypeInfos[i] == TI) 
return i + 13.95k
;
793
5.96k
794
5.96k
  TypeInfos.push_back(TI);
795
2.00k
  return TypeInfos.size();
796
5.96k
}
797
798
14
int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
799
14
  // If the new filter coincides with the tail of an existing filter, then
800
14
  // re-use the existing filter.  Folding filters more than this requires
801
14
  // re-ordering filters and/or their elements - probably not worth it.
802
14
  for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
803
14
       E = FilterEnds.end(); I != E; 
++I0
) {
804
3
    unsigned i = *I, j = TyIds.size();
805
3
806
3
    while (i && 
j0
)
807
0
      if (FilterIds[--i] != TyIds[--j])
808
0
        goto try_next;
809
3
810
3
    if (!j)
811
3
      // The new filter coincides with range [i, end) of the existing filter.
812
3
      return -(1 + i);
813
0
814
0
try_next:;
815
0
  }
816
14
817
14
  // Add the new filter.
818
14
  int FilterID = -(1 + FilterIds.size());
819
11
  FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
820
11
  FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
821
11
  FilterEnds.push_back(FilterIds.size());
822
11
  FilterIds.push_back(0); // terminator
823
11
  return FilterID;
824
14
}
825
826
void MachineFunction::addCodeViewHeapAllocSite(MachineInstr *I,
827
3
                                               const MDNode *MD) {
828
3
  MCSymbol *BeginLabel = Ctx.createTempSymbol("heapallocsite", true);
829
3
  MCSymbol *EndLabel = Ctx.createTempSymbol("heapallocsite", true);
830
3
  I->setPreInstrSymbol(*this, BeginLabel);
831
3
  I->setPostInstrSymbol(*this, EndLabel);
832
3
833
3
  const DIType *DI = dyn_cast<DIType>(MD);
834
3
  CodeViewHeapAllocSites.push_back(std::make_tuple(BeginLabel, EndLabel, DI));
835
3
}
836
837
void MachineFunction::updateCallSiteInfo(const MachineInstr *Old,
838
129k
                                         const MachineInstr *New) {
839
129k
  if (!Target.Options.EnableDebugEntryValues || 
Old == New0
)
840
129k
    return;
841
0
842
0
  assert(Old->isCall() && (!New || New->isCall()) &&
843
0
         "Call site info referes only to call instructions!");
844
0
  CallSiteInfoMap::iterator CSIt = CallSitesInfo.find(Old);
845
0
  if (CSIt == CallSitesInfo.end())
846
0
    return;
847
0
  CallSiteInfo CSInfo = std::move(CSIt->second);
848
0
  CallSitesInfo.erase(CSIt);
849
0
  if (New)
850
0
    CallSitesInfo[New] = CSInfo;
851
0
}
852
853
/// \}
854
855
//===----------------------------------------------------------------------===//
856
//  MachineJumpTableInfo implementation
857
//===----------------------------------------------------------------------===//
858
859
/// Return the size of each entry in the jump table.
860
8.09k
unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
861
8.09k
  // The size of a jump table entry is 4 bytes unless the entry is just the
862
8.09k
  // address of a block, in which case it is the pointer size.
863
8.09k
  switch (getEntryKind()) {
864
8.09k
  case MachineJumpTableInfo::EK_BlockAddress:
865
1.54k
    return TD.getPointerSize();
866
8.09k
  case MachineJumpTableInfo::EK_GPRel64BlockAddress:
867
5
    return 8;
868
8.09k
  case MachineJumpTableInfo::EK_GPRel32BlockAddress:
869
6.54k
  case MachineJumpTableInfo::EK_LabelDifference32:
870
6.54k
  case MachineJumpTableInfo::EK_Custom32:
871
6.54k
    return 4;
872
6.54k
  case MachineJumpTableInfo::EK_Inline:
873
0
    return 0;
874
0
  }
875
0
  llvm_unreachable("Unknown jump table encoding!");
876
0
}
877
878
/// Return the alignment of each entry in the jump table.
879
339
unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
880
339
  // The alignment of a jump table entry is the alignment of int32 unless the
881
339
  // entry is just the address of a block, in which case it is the pointer
882
339
  // alignment.
883
339
  switch (getEntryKind()) {
884
339
  case MachineJumpTableInfo::EK_BlockAddress:
885
94
    return TD.getPointerABIAlignment(0);
886
339
  case MachineJumpTableInfo::EK_GPRel64BlockAddress:
887
5
    return TD.getABIIntegerTypeAlignment(64);
888
339
  case MachineJumpTableInfo::EK_GPRel32BlockAddress:
889
240
  case MachineJumpTableInfo::EK_LabelDifference32:
890
240
  case MachineJumpTableInfo::EK_Custom32:
891
240
    return TD.getABIIntegerTypeAlignment(32);
892
240
  case MachineJumpTableInfo::EK_Inline:
893
0
    return 1;
894
0
  }
895
0
  llvm_unreachable("Unknown jump table encoding!");
896
0
}
897
898
/// Create a new jump table entry in the jump table info.
899
unsigned MachineJumpTableInfo::createJumpTableIndex(
900
4.59k
                               const std::vector<MachineBasicBlock*> &DestBBs) {
901
4.59k
  assert(!DestBBs.empty() && "Cannot create an empty jump table!");
902
4.59k
  JumpTables.push_back(MachineJumpTableEntry(DestBBs));
903
4.59k
  return JumpTables.size()-1;
904
4.59k
}
905
906
/// If Old is the target of any jump tables, update the jump tables to branch
907
/// to New instead.
908
bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
909
28.7k
                                                  MachineBasicBlock *New) {
910
28.7k
  assert(Old != New && "Not making a change?");
911
28.7k
  bool MadeChange = false;
912
63.2k
  for (size_t i = 0, e = JumpTables.size(); i != e; 
++i34.5k
)
913
34.5k
    ReplaceMBBInJumpTable(i, Old, New);
914
28.7k
  return MadeChange;
915
28.7k
}
916
917
/// If Old is a target of the jump tables, update the jump table to branch to
918
/// New instead.
919
bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
920
                                                 MachineBasicBlock *Old,
921
34.5k
                                                 MachineBasicBlock *New) {
922
34.5k
  assert(Old != New && "Not making a change?");
923
34.5k
  bool MadeChange = false;
924
34.5k
  MachineJumpTableEntry &JTE = JumpTables[Idx];
925
1.04M
  for (size_t j = 0, e = JTE.MBBs.size(); j != e; 
++j1.01M
)
926
1.01M
    if (JTE.MBBs[j] == Old) {
927
6.02k
      JTE.MBBs[j] = New;
928
6.02k
      MadeChange = true;
929
6.02k
    }
930
34.5k
  return MadeChange;
931
34.5k
}
932
933
1.95k
void MachineJumpTableInfo::print(raw_ostream &OS) const {
934
1.95k
  if (JumpTables.empty()) 
return0
;
935
1.95k
936
1.95k
  OS << "Jump Tables:\n";
937
1.95k
938
4.67k
  for (unsigned i = 0, e = JumpTables.size(); i != e; 
++i2.72k
) {
939
2.72k
    OS << printJumpTableEntryReference(i) << ':';
940
26.6k
    for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; 
++j23.9k
)
941
23.9k
      OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]);
942
2.72k
    if (i != e)
943
2.72k
      OS << '\n';
944
2.72k
  }
945
1.95k
946
1.95k
  OS << '\n';
947
1.95k
}
948
949
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
950
LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
951
#endif
952
953
10.9k
Printable llvm::printJumpTableEntryReference(unsigned Idx) {
954
10.9k
  return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; });
955
10.9k
}
956
957
//===----------------------------------------------------------------------===//
958
//  MachineConstantPool implementation
959
//===----------------------------------------------------------------------===//
960
961
0
void MachineConstantPoolValue::anchor() {}
962
963
99.1k
Type *MachineConstantPoolEntry::getType() const {
964
99.1k
  if (isMachineConstantPoolEntry())
965
1.42k
    return Val.MachineCPVal->getType();
966
97.7k
  return Val.ConstVal->getType();
967
97.7k
}
968
969
47.3k
bool MachineConstantPoolEntry::needsRelocation() const {
970
47.3k
  if (isMachineConstantPoolEntry())
971
17
    return true;
972
47.2k
  return Val.ConstVal->needsRelocation();
973
47.2k
}
974
975
SectionKind
976
47.3k
MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
977
47.3k
  if (needsRelocation())
978
25
    return SectionKind::getReadOnlyWithRel();
979
47.2k
  switch (DL->getTypeAllocSize(getType())) {
980
47.2k
  case 4:
981
4.39k
    return SectionKind::getMergeableConst4();
982
47.2k
  case 8:
983
6.06k
    return SectionKind::getMergeableConst8();
984
47.2k
  case 16:
985
29.4k
    return SectionKind::getMergeableConst16();
986
47.2k
  case 32:
987
5.79k
    return SectionKind::getMergeableConst32();
988
47.2k
  default:
989
1.55k
    return SectionKind::getReadOnly();
990
47.2k
  }
991
47.2k
}
992
993
526k
MachineConstantPool::~MachineConstantPool() {
994
526k
  // A constant may be a member of both Constants and MachineCPVsSharingEntries,
995
526k
  // so keep track of which we've deleted to avoid double deletions.
996
526k
  DenseSet<MachineConstantPoolValue*> Deleted;
997
578k
  for (unsigned i = 0, e = Constants.size(); i != e; 
++i52.0k
)
998
52.0k
    if (Constants[i].isMachineConstantPoolEntry()) {
999
1.42k
      Deleted.insert(Constants[i].Val.MachineCPVal);
1000
1.42k
      delete Constants[i].Val.MachineCPVal;
1001
1.42k
    }
1002
526k
  for (DenseSet<MachineConstantPoolValue*>::iterator I =
1003
526k
       MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
1004
527k
       I != E; 
++I132
) {
1005
132
    if (Deleted.count(*I) == 0)
1006
128
      delete *I;
1007
132
  }
1008
526k
}
1009
1010
/// Test whether the given two constants can be allocated the same constant pool
1011
/// entry.
1012
static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
1013
2.04M
                                      const DataLayout &DL) {
1014
2.04M
  // Handle the trivial case quickly.
1015
2.04M
  if (A == B) 
return true55.3k
;
1016
1.99M
1017
1.99M
  // If they have the same type but weren't the same constant, quickly
1018
1.99M
  // reject them.
1019
1.99M
  if (A->getType() == B->getType()) 
return false429k
;
1020
1.56M
1021
1.56M
  // We can't handle structs or arrays.
1022
1.56M
  if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
1023
1.56M
      
isa<StructType>(B->getType())1.55M
||
isa<ArrayType>(B->getType())1.55M
)
1024
11.1k
    return false;
1025
1.55M
1026
1.55M
  // For now, only support constants with the same size.
1027
1.55M
  uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
1028
1.55M
  if (StoreSize != DL.getTypeStoreSize(B->getType()) || 
StoreSize > 128635k
)
1029
915k
    return false;
1030
635k
1031
635k
  Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
1032
635k
1033
635k
  // Try constant folding a bitcast of both instructions to an integer.  If we
1034
635k
  // get two identical ConstantInt's, then we are good to share them.  We use
1035
635k
  // the constant folding APIs to do this so that we get the benefit of
1036
635k
  // DataLayout.
1037
635k
  if (isa<PointerType>(A->getType()))
1038
525
    A = ConstantFoldCastOperand(Instruction::PtrToInt,
1039
525
                                const_cast<Constant *>(A), IntTy, DL);
1040
634k
  else if (A->getType() != IntTy)
1041
634k
    A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
1042
634k
                                IntTy, DL);
1043
635k
  if (isa<PointerType>(B->getType()))
1044
422
    B = ConstantFoldCastOperand(Instruction::PtrToInt,
1045
422
                                const_cast<Constant *>(B), IntTy, DL);
1046
634k
  else if (B->getType() != IntTy)
1047
634k
    B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
1048
634k
                                IntTy, DL);
1049
635k
1050
635k
  return A == B;
1051
635k
}
1052
1053
/// Create a new entry in the constant pool or return an existing one.
1054
/// User must specify the log2 of the minimum required alignment for the object.
1055
unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
1056
106k
                                                   unsigned Alignment) {
1057
106k
  assert(Alignment && "Alignment must be specified!");
1058
106k
  if (Alignment > PoolAlignment) 
PoolAlignment = Alignment29.1k
;
1059
106k
1060
106k
  // Check to see if we already have this constant.
1061
106k
  //
1062
106k
  // FIXME, this could be made much more efficient for large constant pools.
1063
2.09M
  for (unsigned i = 0, e = Constants.size(); i != e; 
++i1.99M
)
1064
2.04M
    if (!Constants[i].isMachineConstantPoolEntry() &&
1065
2.04M
        
CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)2.04M
) {
1066
55.4k
      if ((unsigned)Constants[i].getAlignment() < Alignment)
1067
0
        Constants[i].Alignment = Alignment;
1068
55.4k
      return i;
1069
55.4k
    }
1070
106k
1071
106k
  Constants.push_back(MachineConstantPoolEntry(C, Alignment));
1072
50.5k
  return Constants.size()-1;
1073
106k
}
1074
1075
unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
1076
1.56k
                                                   unsigned Alignment) {
1077
1.56k
  assert(Alignment && "Alignment must be specified!");
1078
1.56k
  if (Alignment > PoolAlignment) 
PoolAlignment = Alignment801
;
1079
1.56k
1080
1.56k
  // Check to see if we already have this constant.
1081
1.56k
  //
1082
1.56k
  // FIXME, this could be made much more efficient for large constant pools.
1083
1.56k
  int Idx = V->getExistingMachineCPValue(this, Alignment);
1084
1.56k
  if (Idx != -1) {
1085
132
    MachineCPVsSharingEntries.insert(V);
1086
132
    return (unsigned)Idx;
1087
132
  }
1088
1.42k
1089
1.42k
  Constants.push_back(MachineConstantPoolEntry(V, Alignment));
1090
1.42k
  return Constants.size()-1;
1091
1.42k
}
1092
1093
2.95k
void MachineConstantPool::print(raw_ostream &OS) const {
1094
2.95k
  if (Constants.empty()) 
return2.94k
;
1095
1
1096
1
  OS << "Constant Pool:\n";
1097
3
  for (unsigned i = 0, e = Constants.size(); i != e; 
++i2
) {
1098
2
    OS << "  cp#" << i << ": ";
1099
2
    if (Constants[i].isMachineConstantPoolEntry())
1100
2
      Constants[i].Val.MachineCPVal->print(OS);
1101
0
    else
1102
0
      Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1103
2
    OS << ", align=" << Constants[i].getAlignment();
1104
2
    OS << "\n";
1105
2
  }
1106
1
}
1107
1108
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1109
LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
1110
#endif