LCOV - code coverage report
Current view: top level - lib/CodeGen - MachineFunction.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 380 423 89.8 %
Date: 2018-10-20 13:21:21 Functions: 56 62 90.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- MachineFunction.cpp ------------------------------------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // Collect native machine code information for a function.  This allows
      11             : // target-specific information about the generated code to be stored with each
      12             : // function.
      13             : //
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #include "llvm/CodeGen/MachineFunction.h"
      17             : #include "llvm/ADT/BitVector.h"
      18             : #include "llvm/ADT/DenseMap.h"
      19             : #include "llvm/ADT/DenseSet.h"
      20             : #include "llvm/ADT/STLExtras.h"
      21             : #include "llvm/ADT/SmallString.h"
      22             : #include "llvm/ADT/SmallVector.h"
      23             : #include "llvm/ADT/StringRef.h"
      24             : #include "llvm/ADT/Twine.h"
      25             : #include "llvm/Analysis/ConstantFolding.h"
      26             : #include "llvm/Analysis/EHPersonalities.h"
      27             : #include "llvm/CodeGen/MachineBasicBlock.h"
      28             : #include "llvm/CodeGen/MachineConstantPool.h"
      29             : #include "llvm/CodeGen/MachineFrameInfo.h"
      30             : #include "llvm/CodeGen/MachineInstr.h"
      31             : #include "llvm/CodeGen/MachineJumpTableInfo.h"
      32             : #include "llvm/CodeGen/MachineMemOperand.h"
      33             : #include "llvm/CodeGen/MachineModuleInfo.h"
      34             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      35             : #include "llvm/CodeGen/PseudoSourceValue.h"
      36             : #include "llvm/CodeGen/TargetFrameLowering.h"
      37             : #include "llvm/CodeGen/TargetLowering.h"
      38             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      39             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      40             : #include "llvm/CodeGen/WasmEHFuncInfo.h"
      41             : #include "llvm/CodeGen/WinEHFuncInfo.h"
      42             : #include "llvm/Config/llvm-config.h"
      43             : #include "llvm/IR/Attributes.h"
      44             : #include "llvm/IR/BasicBlock.h"
      45             : #include "llvm/IR/Constant.h"
      46             : #include "llvm/IR/DataLayout.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        4551 : static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
      87             :   using P = MachineFunctionProperties::Property;
      88             : 
      89        4551 :   switch(Prop) {
      90             :   case P::FailedISel: return "FailedISel";
      91         831 :   case P::IsSSA: return "IsSSA";
      92           3 :   case P::Legalized: return "Legalized";
      93        1107 :   case P::NoPHIs: return "NoPHIs";
      94         776 :   case P::NoVRegs: return "NoVRegs";
      95           2 :   case P::RegBankSelected: return "RegBankSelected";
      96           1 :   case P::Selected: return "Selected";
      97        1831 :   case P::TracksLiveness: return "TracksLiveness";
      98             :   }
      99           0 :   llvm_unreachable("Invalid machine function property");
     100             : }
     101             : 
     102             : // Pin the vtable to this file.
     103           0 : void MachineFunction::Delegate::anchor() {}
     104             : 
     105        1889 : void MachineFunctionProperties::print(raw_ostream &OS) const {
     106             :   const char *Separator = "";
     107       17001 :   for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
     108       15112 :     if (!Properties[I])
     109             :       continue;
     110        4551 :     OS << Separator << getPropertyName(static_cast<Property>(I));
     111             :     Separator = ", ";
     112             :   }
     113        1889 : }
     114             : 
     115             : //===----------------------------------------------------------------------===//
     116             : // MachineFunction implementation
     117             : //===----------------------------------------------------------------------===//
     118             : 
     119             : // Out-of-line virtual method.
     120             : MachineFunctionInfo::~MachineFunctionInfo() = default;
     121             : 
     122     3306606 : void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
     123     3306606 :   MBB->getParent()->DeleteMachineBasicBlock(MBB);
     124     3306607 : }
     125             : 
     126      411212 : static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
     127             :                                            const Function &F) {
     128      411212 :   if (F.hasFnAttribute(Attribute::StackAlignment))
     129          31 :     return F.getFnStackAlignment();
     130      411181 :   return STI->getFrameLowering()->getStackAlignment();
     131             : }
     132             : 
     133      411087 : MachineFunction::MachineFunction(const Function &F, const TargetMachine &Target,
     134             :                                  const TargetSubtargetInfo &STI,
     135      411087 :                                  unsigned FunctionNum, MachineModuleInfo &mmi)
     136     1233261 :     : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) {
     137      411087 :   FunctionNumber = FunctionNum;
     138      411087 :   init();
     139      411087 : }
     140             : 
     141    54115094 : void MachineFunction::handleInsertion(const MachineInstr &MI) {
     142    54115094 :   if (TheDelegate)
     143           0 :     TheDelegate->MF_HandleInsertion(MI);
     144    54115094 : }
     145             : 
     146    19629835 : void MachineFunction::handleRemoval(const MachineInstr &MI) {
     147    19629835 :   if (TheDelegate)
     148           0 :     TheDelegate->MF_HandleRemoval(MI);
     149    19629835 : }
     150             : 
     151      411212 : void MachineFunction::init() {
     152             :   // Assume the function starts in SSA form with correct liveness.
     153             :   Properties.set(MachineFunctionProperties::Property::IsSSA);
     154             :   Properties.set(MachineFunctionProperties::Property::TracksLiveness);
     155      411212 :   if (STI->getRegisterInfo())
     156      411209 :     RegInfo = new (Allocator) MachineRegisterInfo(this);
     157             :   else
     158           3 :     RegInfo = nullptr;
     159             : 
     160      411212 :   MFInfo = nullptr;
     161             :   // We can realign the stack if the target supports it and the user hasn't
     162             :   // explicitly asked us not to.
     163      411212 :   bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
     164      404087 :                       !F.hasFnAttribute("no-realign-stack");
     165      411212 :   FrameInfo = new (Allocator) MachineFrameInfo(
     166      411213 :       getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP,
     167      815159 :       /*ForceRealign=*/CanRealignSP &&
     168      403947 :           F.hasFnAttribute(Attribute::StackAlignment));
     169             : 
     170      411212 :   if (F.hasFnAttribute(Attribute::StackAlignment))
     171          31 :     FrameInfo->ensureMaxAlignment(F.getFnStackAlignment());
     172             : 
     173      411212 :   ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
     174      411212 :   Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
     175             : 
     176             :   // FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
     177             :   // FIXME: Use Function::optForSize().
     178      411212 :   if (!F.hasFnAttribute(Attribute::OptimizeForSize))
     179      811824 :     Alignment = std::max(Alignment,
     180      738216 :                          STI->getTargetLowering()->getPrefFunctionAlignment());
     181             : 
     182      411212 :   if (AlignAllFunctions)
     183           0 :     Alignment = AlignAllFunctions;
     184             : 
     185      411212 :   JumpTableInfo = nullptr;
     186             : 
     187      470996 :   if (isFuncletEHPersonality(classifyEHPersonality(
     188      411212 :           F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
     189          91 :     WinEHInfo = new (Allocator) WinEHFuncInfo();
     190             :   }
     191             : 
     192      470996 :   if (isScopedEHPersonality(classifyEHPersonality(
     193      411212 :           F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
     194         212 :     WasmEHInfo = new (Allocator) WasmEHFuncInfo();
     195             :   }
     196             : 
     197             :   assert(Target.isCompatibleDataLayout(getDataLayout()) &&
     198             :          "Can't create a MachineFunction using a Module with a "
     199             :          "Target-incompatible DataLayout attached\n");
     200             : 
     201             :   PSVManager =
     202      411212 :     llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget().
     203      822424 :                                                   getInstrInfo()));
     204      411212 : }
     205             : 
     206     1232820 : MachineFunction::~MachineFunction() {
     207      410940 :   clear();
     208      410940 : }
     209             : 
     210      411065 : void MachineFunction::clear() {
     211             :   Properties.reset();
     212             :   // Don't call destructors on MachineInstr and MachineOperand. All of their
     213             :   // memory comes from the BumpPtrAllocator which is about to be purged.
     214             :   //
     215             :   // Do call MachineBasicBlock destructors, it contains std::vectors.
     216     3699659 :   for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
     217             :     I->Insts.clearAndLeakNodesUnsafely();
     218             :   MBBNumbering.clear();
     219             : 
     220             :   InstructionRecycler.clear(Allocator);
     221             :   OperandRecycler.clear(Allocator);
     222             :   BasicBlockRecycler.clear(Allocator);
     223             :   CodeViewAnnotations.clear();
     224             :   VariableDbgInfos.clear();
     225      411065 :   if (RegInfo) {
     226      411062 :     RegInfo->~MachineRegisterInfo();
     227             :     Allocator.Deallocate(RegInfo);
     228             :   }
     229      411065 :   if (MFInfo) {
     230      407457 :     MFInfo->~MachineFunctionInfo();
     231             :     Allocator.Deallocate(MFInfo);
     232             :   }
     233             : 
     234      411065 :   FrameInfo->~MachineFrameInfo();
     235             :   Allocator.Deallocate(FrameInfo);
     236             : 
     237      411065 :   ConstantPool->~MachineConstantPool();
     238             :   Allocator.Deallocate(ConstantPool);
     239             : 
     240      411065 :   if (JumpTableInfo) {
     241             :     JumpTableInfo->~MachineJumpTableInfo();
     242             :     Allocator.Deallocate(JumpTableInfo);
     243             :   }
     244             : 
     245      411065 :   if (WinEHInfo) {
     246          91 :     WinEHInfo->~WinEHFuncInfo();
     247             :     Allocator.Deallocate(WinEHInfo);
     248             :   }
     249             : 
     250      411065 :   if (WasmEHInfo) {
     251             :     WasmEHInfo->~WasmEHFuncInfo();
     252             :     Allocator.Deallocate(WasmEHInfo);
     253             :   }
     254      411065 : }
     255             : 
     256   193724903 : const DataLayout &MachineFunction::getDataLayout() const {
     257   193724903 :   return F.getParent()->getDataLayout();
     258             : }
     259             : 
     260             : /// Get the JumpTableInfo for this function.
     261             : /// If it does not already exist, allocate one.
     262        3245 : MachineJumpTableInfo *MachineFunction::
     263             : getOrCreateJumpTableInfo(unsigned EntryKind) {
     264        3245 :   if (JumpTableInfo) return JumpTableInfo;
     265             : 
     266        2196 :   JumpTableInfo = new (Allocator)
     267             :     MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
     268        2196 :   return JumpTableInfo;
     269             : }
     270             : 
     271             : /// Should we be emitting segmented stack stuff for the function
     272      569207 : bool MachineFunction::shouldSplitStack() const {
     273      569207 :   return getFunction().hasFnAttribute("split-stack");
     274             : }
     275             : 
     276             : /// This discards all of the MachineBasicBlock numbers and recomputes them.
     277             : /// This guarantees that the MBB numbers are sequential, dense, and match the
     278             : /// ordering of the blocks within the function.  If a specific MachineBasicBlock
     279             : /// is specified, only that block and those after it are renumbered.
     280      594135 : void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
     281      594135 :   if (empty()) { MBBNumbering.clear(); return; }
     282             :   MachineFunction::iterator MBBI, E = end();
     283      594135 :   if (MBB == nullptr)
     284             :     MBBI = begin();
     285             :   else
     286         749 :     MBBI = MBB->getIterator();
     287             : 
     288             :   // Figure out the block number this should have.
     289             :   unsigned BlockNo = 0;
     290      594135 :   if (MBBI != begin())
     291         739 :     BlockNo = std::prev(MBBI)->getNumber() + 1;
     292             : 
     293     2282960 :   for (; MBBI != E; ++MBBI, ++BlockNo) {
     294     1688825 :     if (MBBI->getNumber() != (int)BlockNo) {
     295             :       // Remove use of the old number.
     296      774656 :       if (MBBI->getNumber() != -1) {
     297             :         assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
     298             :                "MBB number mismatch!");
     299      689840 :         MBBNumbering[MBBI->getNumber()] = nullptr;
     300             :       }
     301             : 
     302             :       // If BlockNo is already taken, set that block's number to -1.
     303     1549312 :       if (MBBNumbering[BlockNo])
     304             :         MBBNumbering[BlockNo]->setNumber(-1);
     305             : 
     306      774656 :       MBBNumbering[BlockNo] = &*MBBI;
     307             :       MBBI->setNumber(BlockNo);
     308             :     }
     309             :   }
     310             : 
     311             :   // Okay, all the blocks are renumbered.  If we have compactified the block
     312             :   // numbering, shrink MBBNumbering now.
     313             :   assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
     314      594135 :   MBBNumbering.resize(BlockNo);
     315             : }
     316             : 
     317             : /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
     318    52472967 : MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
     319             :                                                   const DebugLoc &DL,
     320             :                                                   bool NoImp) {
     321    52472967 :   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
     322    52472967 :     MachineInstr(*this, MCID, DL, NoImp);
     323             : }
     324             : 
     325             : /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
     326             : /// identical in all ways except the instruction has no parent, prev, or next.
     327             : MachineInstr *
     328      109227 : MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
     329      109227 :   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
     330      109227 :              MachineInstr(*this, *Orig);
     331             : }
     332             : 
     333        9356 : MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
     334             :     MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
     335             :   MachineInstr *FirstClone = nullptr;
     336        9356 :   MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
     337             :   while (true) {
     338        9360 :     MachineInstr *Cloned = CloneMachineInstr(&*I);
     339             :     MBB.insert(InsertBefore, Cloned);
     340        9360 :     if (FirstClone == nullptr) {
     341             :       FirstClone = Cloned;
     342             :     } else {
     343           4 :       Cloned->bundleWithPred();
     344             :     }
     345             : 
     346        9360 :     if (!I->isBundledWithSucc())
     347             :       break;
     348             :     ++I;
     349             :   }
     350        9356 :   return *FirstClone;
     351             : }
     352             : 
     353             : /// Delete the given MachineInstr.
     354             : ///
     355             : /// This function also serves as the MachineInstr destructor - the real
     356             : /// ~MachineInstr() destructor must be empty.
     357             : void
     358    18096173 : MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
     359             :   // Strip it for parts. The operand array and the MI object itself are
     360             :   // independently recyclable.
     361    18096173 :   if (MI->Operands)
     362             :     deallocateOperandArray(MI->CapOperands, MI->Operands);
     363             :   // Don't call ~MachineInstr() which must be trivial anyway because
     364             :   // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
     365             :   // destructors.
     366             :   InstructionRecycler.Deallocate(Allocator, MI);
     367    18096172 : }
     368             : 
     369             : /// Allocate a new MachineBasicBlock. Use this instead of
     370             : /// `new MachineBasicBlock'.
     371             : MachineBasicBlock *
     372     3307967 : MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
     373     3307967 :   return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
     374     3307967 :              MachineBasicBlock(*this, bb);
     375             : }
     376             : 
     377             : /// Delete the given MachineBasicBlock.
     378             : void
     379     3307794 : MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
     380             :   assert(MBB->getParent() == this && "MBB parent mismatch!");
     381     3307794 :   MBB->~MachineBasicBlock();
     382             :   BasicBlockRecycler.Deallocate(Allocator, MBB);
     383     3307794 : }
     384             : 
     385    19432954 : MachineMemOperand *MachineFunction::getMachineMemOperand(
     386             :     MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
     387             :     unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
     388             :     SyncScope::ID SSID, AtomicOrdering Ordering,
     389             :     AtomicOrdering FailureOrdering) {
     390    19432954 :   return new (Allocator)
     391             :       MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
     392    19432954 :                         SSID, Ordering, FailureOrdering);
     393             : }
     394             : 
     395             : MachineMemOperand *
     396        2603 : MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
     397             :                                       int64_t Offset, uint64_t Size) {
     398        2504 :   if (MMO->getValue())
     399        2504 :     return new (Allocator)
     400        5008 :                MachineMemOperand(MachinePointerInfo(MMO->getValue(),
     401        2504 :                                                     MMO->getOffset()+Offset),
     402        2504 :                                  MMO->getFlags(), Size, MMO->getBaseAlignment(),
     403        5008 :                                  AAMDNodes(), nullptr, MMO->getSyncScopeID(),
     404        2504 :                                  MMO->getOrdering(), MMO->getFailureOrdering());
     405          99 :   return new (Allocator)
     406         198 :              MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
     407          99 :                                                   MMO->getOffset()+Offset),
     408          99 :                                MMO->getFlags(), Size, MMO->getBaseAlignment(),
     409         198 :                                AAMDNodes(), nullptr, MMO->getSyncScopeID(),
     410          99 :                                MMO->getOrdering(), MMO->getFailureOrdering());
     411             : }
     412             : 
     413             : MachineMemOperand *
     414       51683 : MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
     415             :                                       const AAMDNodes &AAInfo) {
     416             :   MachinePointerInfo MPI = MMO->getValue() ?
     417             :              MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
     418       51683 :              MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
     419             : 
     420       51683 :   return new (Allocator)
     421       51683 :              MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
     422       51683 :                                MMO->getBaseAlignment(), AAInfo,
     423      103366 :                                MMO->getRanges(), MMO->getSyncScopeID(),
     424       51683 :                                MMO->getOrdering(), MMO->getFailureOrdering());
     425             : }
     426             : 
     427             : MachineInstr::ExtraInfo *
     428      368564 : MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
     429             :                                    MCSymbol *PreInstrSymbol,
     430             :                                    MCSymbol *PostInstrSymbol) {
     431      368564 :   return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
     432      368564 :                                          PostInstrSymbol);
     433             : }
     434             : 
     435         607 : const char *MachineFunction::createExternalSymbolName(StringRef Name) {
     436         607 :   char *Dest = Allocator.Allocate<char>(Name.size() + 1);
     437             :   std::copy(Name.begin(), Name.end(), Dest);
     438         607 :   Dest[Name.size()] = 0;
     439         607 :   return Dest;
     440             : }
     441             : 
     442         261 : uint32_t *MachineFunction::allocateRegMask() {
     443         261 :   unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs();
     444             :   unsigned Size = MachineOperand::getRegMaskSize(NumRegs);
     445         261 :   uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
     446         261 :   memset(Mask, 0, Size * sizeof(Mask[0]));
     447         261 :   return Mask;
     448             : }
     449             : 
     450             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     451             : LLVM_DUMP_METHOD void MachineFunction::dump() const {
     452             :   print(dbgs());
     453             : }
     454             : #endif
     455             : 
     456     1168233 : StringRef MachineFunction::getName() const {
     457     1168233 :   return getFunction().getName();
     458             : }
     459             : 
     460        1889 : void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
     461        1889 :   OS << "# Machine code for function " << getName() << ": ";
     462        1889 :   getProperties().print(OS);
     463             :   OS << '\n';
     464             : 
     465             :   // Print Frame Information
     466        1889 :   FrameInfo->print(*this, OS);
     467             : 
     468             :   // Print JumpTable Information
     469        1889 :   if (JumpTableInfo)
     470         933 :     JumpTableInfo->print(OS);
     471             : 
     472             :   // Print Constant Pool
     473        1889 :   ConstantPool->print(OS);
     474             : 
     475        1889 :   const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
     476             : 
     477        1889 :   if (RegInfo && !RegInfo->livein_empty()) {
     478        1526 :     OS << "Function Live Ins: ";
     479             :     for (MachineRegisterInfo::livein_iterator
     480        4225 :          I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
     481        2699 :       OS << printReg(I->first, TRI);
     482        2699 :       if (I->second)
     483        2714 :         OS << " in " << printReg(I->second, TRI);
     484        2699 :       if (std::next(I) != E)
     485        1173 :         OS << ", ";
     486             :     }
     487             :     OS << '\n';
     488             :   }
     489             : 
     490        3778 :   ModuleSlotTracker MST(getFunction().getParent());
     491        1889 :   MST.incorporateFunction(getFunction());
     492       17516 :   for (const auto &BB : *this) {
     493             :     OS << '\n';
     494             :     // If we print the whole function, print it at its most verbose level.
     495       15627 :     BB.print(OS, MST, Indexes, /*IsStandalone=*/true);
     496             :   }
     497             : 
     498        1889 :   OS << "\n# End machine code for function " << getName() << ".\n\n";
     499        1889 : }
     500             : 
     501             : namespace llvm {
     502             : 
     503             :   template<>
     504             :   struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
     505             :     DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
     506             : 
     507             :     static std::string getGraphName(const MachineFunction *F) {
     508             :       return ("CFG for '" + F->getName() + "' function").str();
     509             :     }
     510             : 
     511             :     std::string getNodeLabel(const MachineBasicBlock *Node,
     512             :                              const MachineFunction *Graph) {
     513             :       std::string OutStr;
     514             :       {
     515             :         raw_string_ostream OSS(OutStr);
     516             : 
     517             :         if (isSimple()) {
     518             :           OSS << printMBBReference(*Node);
     519             :           if (const BasicBlock *BB = Node->getBasicBlock())
     520             :             OSS << ": " << BB->getName();
     521             :         } else
     522             :           Node->print(OSS);
     523             :       }
     524             : 
     525             :       if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
     526             : 
     527             :       // Process string output to make it nicer...
     528             :       for (unsigned i = 0; i != OutStr.length(); ++i)
     529             :         if (OutStr[i] == '\n') {                            // Left justify
     530             :           OutStr[i] = '\\';
     531             :           OutStr.insert(OutStr.begin()+i+1, 'l');
     532             :         }
     533             :       return OutStr;
     534             :     }
     535             :   };
     536             : 
     537             : } // end namespace llvm
     538             : 
     539           0 : void MachineFunction::viewCFG() const
     540             : {
     541             : #ifndef NDEBUG
     542             :   ViewGraph(this, "mf" + getName());
     543             : #else
     544           0 :   errs() << "MachineFunction::viewCFG is only available in debug builds on "
     545           0 :          << "systems with Graphviz or gv!\n";
     546             : #endif // NDEBUG
     547           0 : }
     548             : 
     549           0 : void MachineFunction::viewCFGOnly() const
     550             : {
     551             : #ifndef NDEBUG
     552             :   ViewGraph(this, "mf" + getName(), true);
     553             : #else
     554           0 :   errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
     555           0 :          << "systems with Graphviz or gv!\n";
     556             : #endif // NDEBUG
     557           0 : }
     558             : 
     559             : /// Add the specified physical register as a live-in value and
     560             : /// create a corresponding virtual register for it.
     561      676279 : unsigned MachineFunction::addLiveIn(unsigned PReg,
     562             :                                     const TargetRegisterClass *RC) {
     563      676279 :   MachineRegisterInfo &MRI = getRegInfo();
     564      676279 :   unsigned VReg = MRI.getLiveInVirtReg(PReg);
     565      676279 :   if (VReg) {
     566             :     const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
     567             :     (void)VRegRC;
     568             :     // A physical register can be added several times.
     569             :     // Between two calls, the register class of the related virtual register
     570             :     // may have been constrained to match some operation constraints.
     571             :     // In that case, check that the current register class includes the
     572             :     // physical register and is a sub class of the specified RC.
     573             :     assert((VRegRC == RC || (VRegRC->contains(PReg) &&
     574             :                              RC->hasSubClassEq(VRegRC))) &&
     575             :             "Register class mismatch!");
     576             :     return VReg;
     577             :   }
     578      676233 :   VReg = MRI.createVirtualRegister(RC);
     579             :   MRI.addLiveIn(PReg, VReg);
     580      676232 :   return VReg;
     581             : }
     582             : 
     583             : /// Return the MCSymbol for the specified non-empty jump table.
     584             : /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
     585             : /// normal 'L' label is returned.
     586        8535 : MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
     587             :                                         bool isLinkerPrivate) const {
     588        8535 :   const DataLayout &DL = getDataLayout();
     589             :   assert(JumpTableInfo && "No jump tables");
     590             :   assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
     591             : 
     592           6 :   StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
     593        8535 :                                      : DL.getPrivateGlobalPrefix();
     594             :   SmallString<60> Name;
     595        8535 :   raw_svector_ostream(Name)
     596        8535 :     << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
     597        8535 :   return Ctx.getOrCreateSymbol(Name);
     598             : }
     599             : 
     600             : /// Return a function-local symbol to represent the PIC base.
     601        6278 : MCSymbol *MachineFunction::getPICBaseSymbol() const {
     602        6278 :   const DataLayout &DL = getDataLayout();
     603       18834 :   return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
     604        6278 :                                Twine(getFunctionNumber()) + "$pb");
     605             : }
     606             : 
     607             : /// \name Exception Handling
     608             : /// \{
     609             : 
     610             : LandingPadInfo &
     611     1200898 : MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
     612     2401796 :   unsigned N = LandingPads.size();
     613    63737777 :   for (unsigned i = 0; i < N; ++i) {
     614    63399575 :     LandingPadInfo &LP = LandingPads[i];
     615    63399575 :     if (LP.LandingPadBlock == LandingPad)
     616      862696 :       return LP;
     617             :   }
     618             : 
     619      338202 :   LandingPads.push_back(LandingPadInfo(LandingPad));
     620      676404 :   return LandingPads[N];
     621             : }
     622             : 
     623      496847 : void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
     624             :                                 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
     625      496847 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     626      496847 :   LP.BeginLabels.push_back(BeginLabel);
     627      496847 :   LP.EndLabels.push_back(EndLabel);
     628      496847 : }
     629             : 
     630      338185 : MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
     631      338185 :   MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
     632      338185 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     633      338185 :   LP.LandingPadLabel = LandingPadLabel;
     634             : 
     635      338185 :   const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI();
     636             :   if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) {
     637             :     if (const auto *PF =
     638      338185 :             dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()))
     639      338184 :       getMMI().addPersonality(PF);
     640             : 
     641      338185 :     if (LPI->isCleanup())
     642      254903 :       addCleanup(LandingPad);
     643             : 
     644             :     // FIXME: New EH - Add the clauses in reverse order. This isn't 100%
     645             :     //        correct, but we need to do it this way because of how the DWARF EH
     646             :     //        emitter processes the clauses.
     647      449148 :     for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
     648      110963 :       Value *Val = LPI->getClause(I - 1);
     649      110963 :       if (LPI->isCatch(I - 1)) {
     650      104527 :         addCatchTypeInfo(LandingPad,
     651      104527 :                          dyn_cast<GlobalValue>(Val->stripPointerCasts()));
     652             :       } else {
     653             :         // Add filters in a list.
     654             :         auto *CVal = cast<Constant>(Val);
     655             :         SmallVector<const GlobalValue *, 4> FilterList;
     656        6436 :         for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
     657        6438 :              II != IE; ++II)
     658           2 :           FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
     659             : 
     660        6436 :         addFilterTypeInfo(LandingPad, FilterList);
     661             :       }
     662             :     }
     663             : 
     664             :   } else if (isa<CatchPadInst>(FirstI)) {
     665             :     // TODO
     666             : 
     667             :   } else {
     668             :     assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!");
     669             :   }
     670             : 
     671      338185 :   return LandingPadLabel;
     672             : }
     673             : 
     674      104527 : void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
     675             :                                        ArrayRef<const GlobalValue *> TyInfo) {
     676      104527 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     677      209054 :   for (unsigned N = TyInfo.size(); N; --N)
     678      209054 :     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
     679      104527 : }
     680             : 
     681        6436 : void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
     682             :                                         ArrayRef<const GlobalValue *> TyInfo) {
     683        6436 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     684        6436 :   std::vector<unsigned> IdsInFilter(TyInfo.size());
     685        6438 :   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
     686           6 :     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
     687        6436 :   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
     688        6436 : }
     689             : 
     690       47164 : void MachineFunction::tidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
     691      770668 :   for (unsigned i = 0; i != LandingPads.size(); ) {
     692             :     LandingPadInfo &LandingPad = LandingPads[i];
     693      338170 :     if (LandingPad.LandingPadLabel &&
     694      338170 :         !LandingPad.LandingPadLabel->isDefined() &&
     695           0 :         (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
     696           0 :       LandingPad.LandingPadLabel = nullptr;
     697             : 
     698             :     // Special case: we *should* emit LPs with null LP MBB. This indicates
     699             :     // "nounwind" case.
     700      338170 :     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
     701           0 :       LandingPads.erase(LandingPads.begin() + i);
     702           0 :       continue;
     703             :     }
     704             : 
     705     1173153 :     for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
     706      993626 :       MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
     707      496813 :       MCSymbol *EndLabel = LandingPad.EndLabels[j];
     708           0 :       if ((BeginLabel->isDefined() ||
     709      993626 :            (LPMap && (*LPMap)[BeginLabel] != 0)) &&
     710      496813 :           (EndLabel->isDefined() ||
     711      496813 :            (LPMap && (*LPMap)[EndLabel] != 0))) continue;
     712             : 
     713           0 :       LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
     714           0 :       LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
     715           0 :       --j;
     716           0 :       --e;
     717             :     }
     718             : 
     719             :     // Remove landing pads with no try-ranges.
     720      676340 :     if (LandingPads[i].BeginLabels.empty()) {
     721           2 :       LandingPads.erase(LandingPads.begin() + i);
     722           2 :       continue;
     723             :     }
     724             : 
     725             :     // If there is no landing pad, ensure that the list of typeids is empty.
     726             :     // If the only typeid is a cleanup, this is the same as having no typeids.
     727      338168 :     if (!LandingPad.LandingPadBlock ||
     728      338168 :         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
     729             :       LandingPad.TypeIds.clear();
     730      338168 :     ++i;
     731             :   }
     732       47164 : }
     733             : 
     734      254903 : void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
     735      254903 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     736      254903 :   LP.TypeIds.push_back(0);
     737      254903 : }
     738             : 
     739           0 : void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
     740             :                                          const Function *Filter,
     741             :                                          const BlockAddress *RecoverBA) {
     742           0 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     743             :   SEHHandler Handler;
     744           0 :   Handler.FilterOrFinally = Filter;
     745           0 :   Handler.RecoverBA = RecoverBA;
     746           0 :   LP.SEHHandlers.push_back(Handler);
     747           0 : }
     748             : 
     749           0 : void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
     750             :                                            const Function *Cleanup) {
     751           0 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     752             :   SEHHandler Handler;
     753           0 :   Handler.FilterOrFinally = Cleanup;
     754           0 :   Handler.RecoverBA = nullptr;
     755           0 :   LP.SEHHandlers.push_back(Handler);
     756           0 : }
     757             : 
     758      338181 : void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
     759             :                                             ArrayRef<unsigned> Sites) {
     760      338181 :   LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
     761      338181 : }
     762             : 
     763      115917 : unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
     764      255078 :   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
     765      204838 :     if (TypeInfos[i] == TI) return i + 1;
     766             : 
     767       36742 :   TypeInfos.push_back(TI);
     768       73484 :   return TypeInfos.size();
     769             : }
     770             : 
     771        6436 : int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
     772             :   // If the new filter coincides with the tail of an existing filter, then
     773             :   // re-use the existing filter.  Folding filters more than this requires
     774             :   // re-ordering filters and/or their elements - probably not worth it.
     775             :   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
     776        6436 :        E = FilterEnds.end(); I != E; ++I) {
     777       10986 :     unsigned i = *I, j = TyIds.size();
     778             : 
     779        5493 :     while (i && j)
     780           0 :       if (FilterIds[--i] != TyIds[--j])
     781             :         goto try_next;
     782             : 
     783        5493 :     if (!j)
     784             :       // The new filter coincides with range [i, end) of the existing filter.
     785        5493 :       return -(1 + i);
     786             : 
     787           0 : try_next:;
     788             :   }
     789             : 
     790             :   // Add the new filter.
     791         943 :   int FilterID = -(1 + FilterIds.size());
     792        1886 :   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
     793         943 :   FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
     794        1886 :   FilterEnds.push_back(FilterIds.size());
     795         943 :   FilterIds.push_back(0); // terminator
     796         943 :   return FilterID;
     797             : }
     798             : 
     799             : /// \}
     800             : 
     801             : //===----------------------------------------------------------------------===//
     802             : //  MachineJumpTableInfo implementation
     803             : //===----------------------------------------------------------------------===//
     804             : 
     805             : /// Return the size of each entry in the jump table.
     806       74384 : unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
     807             :   // The size of a jump table entry is 4 bytes unless the entry is just the
     808             :   // address of a block, in which case it is the pointer size.
     809       74384 :   switch (getEntryKind()) {
     810       70995 :   case MachineJumpTableInfo::EK_BlockAddress:
     811       70995 :     return TD.getPointerSize();
     812             :   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
     813             :     return 8;
     814        3384 :   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
     815             :   case MachineJumpTableInfo::EK_LabelDifference32:
     816             :   case MachineJumpTableInfo::EK_Custom32:
     817        3384 :     return 4;
     818           0 :   case MachineJumpTableInfo::EK_Inline:
     819           0 :     return 0;
     820             :   }
     821           0 :   llvm_unreachable("Unknown jump table encoding!");
     822             : }
     823             : 
     824             : /// Return the alignment of each entry in the jump table.
     825        2111 : unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
     826             :   // The alignment of a jump table entry is the alignment of int32 unless the
     827             :   // entry is just the address of a block, in which case it is the pointer
     828             :   // alignment.
     829        2111 :   switch (getEntryKind()) {
     830        1995 :   case MachineJumpTableInfo::EK_BlockAddress:
     831        1995 :     return TD.getPointerABIAlignment(0);
     832           5 :   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
     833           5 :     return TD.getABIIntegerTypeAlignment(64);
     834         111 :   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
     835             :   case MachineJumpTableInfo::EK_LabelDifference32:
     836             :   case MachineJumpTableInfo::EK_Custom32:
     837         111 :     return TD.getABIIntegerTypeAlignment(32);
     838             :   case MachineJumpTableInfo::EK_Inline:
     839             :     return 1;
     840             :   }
     841           0 :   llvm_unreachable("Unknown jump table encoding!");
     842             : }
     843             : 
     844             : /// Create a new jump table entry in the jump table info.
     845        3246 : unsigned MachineJumpTableInfo::createJumpTableIndex(
     846             :                                const std::vector<MachineBasicBlock*> &DestBBs) {
     847             :   assert(!DestBBs.empty() && "Cannot create an empty jump table!");
     848        3246 :   JumpTables.push_back(MachineJumpTableEntry(DestBBs));
     849        6492 :   return JumpTables.size()-1;
     850             : }
     851             : 
     852             : /// If Old is the target of any jump tables, update the jump tables to branch
     853             : /// to New instead.
     854        1730 : bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
     855             :                                                   MachineBasicBlock *New) {
     856             :   assert(Old != New && "Not making a change?");
     857             :   bool MadeChange = false;
     858        6471 :   for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
     859        4741 :     ReplaceMBBInJumpTable(i, Old, New);
     860        1730 :   return MadeChange;
     861             : }
     862             : 
     863             : /// If Old is a target of the jump tables, update the jump table to branch to
     864             : /// New instead.
     865        4759 : bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
     866             :                                                  MachineBasicBlock *Old,
     867             :                                                  MachineBasicBlock *New) {
     868             :   assert(Old != New && "Not making a change?");
     869             :   bool MadeChange = false;
     870        4759 :   MachineJumpTableEntry &JTE = JumpTables[Idx];
     871      191462 :   for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
     872      186703 :     if (JTE.MBBs[j] == Old) {
     873        2064 :       JTE.MBBs[j] = New;
     874             :       MadeChange = true;
     875             :     }
     876        4759 :   return MadeChange;
     877             : }
     878             : 
     879         933 : void MachineJumpTableInfo::print(raw_ostream &OS) const {
     880         933 :   if (JumpTables.empty()) return;
     881             : 
     882         933 :   OS << "Jump Tables:\n";
     883             : 
     884        3089 :   for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
     885        2446 :     OS << printJumpTableEntryReference(i) << ": ";
     886       12895 :     for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
     887       36904 :       OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]);
     888             :   }
     889             : 
     890             :   OS << '\n';
     891             : }
     892             : 
     893             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     894             : LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
     895             : #endif
     896             : 
     897        3681 : Printable llvm::printJumpTableEntryReference(unsigned Idx) {
     898        3681 :   return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; });
     899             : }
     900             : 
     901             : //===----------------------------------------------------------------------===//
     902             : //  MachineConstantPool implementation
     903             : //===----------------------------------------------------------------------===//
     904             : 
     905           0 : void MachineConstantPoolValue::anchor() {}
     906             : 
     907       63392 : Type *MachineConstantPoolEntry::getType() const {
     908      126784 :   if (isMachineConstantPoolEntry())
     909         687 :     return Val.MachineCPVal->getType();
     910       62705 :   return Val.ConstVal->getType();
     911             : }
     912             : 
     913       30370 : bool MachineConstantPoolEntry::needsRelocation() const {
     914       60740 :   if (isMachineConstantPoolEntry())
     915             :     return true;
     916       30353 :   return Val.ConstVal->needsRelocation();
     917             : }
     918             : 
     919             : SectionKind
     920       30370 : MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
     921       30370 :   if (needsRelocation())
     922             :     return SectionKind::getReadOnlyWithRel();
     923       30345 :   switch (DL->getTypeAllocSize(getType())) {
     924             :   case 4:
     925             :     return SectionKind::getMergeableConst4();
     926             :   case 8:
     927             :     return SectionKind::getMergeableConst8();
     928             :   case 16:
     929             :     return SectionKind::getMergeableConst16();
     930             :   case 32:
     931             :     return SectionKind::getMergeableConst32();
     932             :   default:
     933             :     return SectionKind::getReadOnly();
     934             :   }
     935             : }
     936             : 
     937      411065 : MachineConstantPool::~MachineConstantPool() {
     938             :   // A constant may be a member of both Constants and MachineCPVsSharingEntries,
     939             :   // so keep track of which we've deleted to avoid double deletions.
     940             :   DenseSet<MachineConstantPoolValue*> Deleted;
     941      855247 :   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
     942       99351 :     if (Constants[i].isMachineConstantPoolEntry()) {
     943         691 :       Deleted.insert(Constants[i].Val.MachineCPVal);
     944        1382 :       delete Constants[i].Val.MachineCPVal;
     945             :     }
     946             :   for (DenseSet<MachineConstantPoolValue*>::iterator I =
     947             :        MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
     948      411076 :        I != E; ++I) {
     949          11 :     if (Deleted.count(*I) == 0)
     950           7 :       delete *I;
     951             :   }
     952      411065 : }
     953             : 
     954             : /// Test whether the given two constants can be allocated the same constant pool
     955             : /// entry.
     956      135092 : static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
     957             :                                       const DataLayout &DL) {
     958             :   // Handle the trivial case quickly.
     959      135092 :   if (A == B) return true;
     960             : 
     961             :   // If they have the same type but weren't the same constant, quickly
     962             :   // reject them.
     963      123042 :   if (A->getType() == B->getType()) return false;
     964             : 
     965             :   // We can't handle structs or arrays.
     966       43950 :   if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
     967       87899 :       isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
     968             :     return false;
     969             : 
     970             :   // For now, only support constants with the same size.
     971             :   uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
     972       43941 :   if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
     973             :     return false;
     974             : 
     975        4208 :   Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
     976             : 
     977             :   // Try constant folding a bitcast of both instructions to an integer.  If we
     978             :   // get two identical ConstantInt's, then we are good to share them.  We use
     979             :   // the constant folding APIs to do this so that we get the benefit of
     980             :   // DataLayout.
     981        8416 :   if (isa<PointerType>(A->getType()))
     982         472 :     A = ConstantFoldCastOperand(Instruction::PtrToInt,
     983             :                                 const_cast<Constant *>(A), IntTy, DL);
     984        3736 :   else if (A->getType() != IntTy)
     985        3631 :     A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
     986             :                                 IntTy, DL);
     987        8416 :   if (isa<PointerType>(B->getType()))
     988         370 :     B = ConstantFoldCastOperand(Instruction::PtrToInt,
     989             :                                 const_cast<Constant *>(B), IntTy, DL);
     990        3838 :   else if (B->getType() != IntTy)
     991        3642 :     B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
     992             :                                 IntTy, DL);
     993             : 
     994        4208 :   return A == B;
     995             : }
     996             : 
     997             : /// Create a new entry in the constant pool or return an existing one.
     998             : /// User must specify the log2 of the minimum required alignment for the object.
     999       44517 : unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
    1000             :                                                    unsigned Alignment) {
    1001             :   assert(Alignment && "Alignment must be specified!");
    1002       44517 :   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
    1003             : 
    1004             :   // Check to see if we already have this constant.
    1005             :   //
    1006             :   // FIXME, this could be made much more efficient for large constant pools.
    1007      212195 :   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
    1008      540848 :     if (!Constants[i].isMachineConstantPoolEntry() &&
    1009      135092 :         CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
    1010       36273 :       if ((unsigned)Constants[i].getAlignment() < Alignment)
    1011           0 :         Constants[i].Alignment = Alignment;
    1012       12091 :       return i;
    1013             :     }
    1014             : 
    1015       32426 :   Constants.push_back(MachineConstantPoolEntry(C, Alignment));
    1016       64852 :   return Constants.size()-1;
    1017             : }
    1018             : 
    1019         702 : unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
    1020             :                                                    unsigned Alignment) {
    1021             :   assert(Alignment && "Alignment must be specified!");
    1022         702 :   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
    1023             : 
    1024             :   // Check to see if we already have this constant.
    1025             :   //
    1026             :   // FIXME, this could be made much more efficient for large constant pools.
    1027         702 :   int Idx = V->getExistingMachineCPValue(this, Alignment);
    1028         702 :   if (Idx != -1) {
    1029             :     MachineCPVsSharingEntries.insert(V);
    1030          11 :     return (unsigned)Idx;
    1031             :   }
    1032             : 
    1033         691 :   Constants.push_back(MachineConstantPoolEntry(V, Alignment));
    1034        1382 :   return Constants.size()-1;
    1035             : }
    1036             : 
    1037        1889 : void MachineConstantPool::print(raw_ostream &OS) const {
    1038        1889 :   if (Constants.empty()) return;
    1039             : 
    1040           1 :   OS << "Constant Pool:\n";
    1041           4 :   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
    1042           4 :     OS << "  cp#" << i << ": ";
    1043           6 :     if (Constants[i].isMachineConstantPoolEntry())
    1044           2 :       Constants[i].Val.MachineCPVal->print(OS);
    1045             :     else
    1046           0 :       Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
    1047           2 :     OS << ", align=" << Constants[i].getAlignment();
    1048           2 :     OS << "\n";
    1049             :   }
    1050             : }
    1051             : 
    1052             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    1053             : LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
    1054             : #endif

Generated by: LCOV version 1.13