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