LCOV - code coverage report
Current view: top level - lib/CodeGen - MachineFunction.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 437 491 89.0 %
Date: 2017-09-14 15:23:50 Functions: 58 64 90.6 %
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/STLExtras.h"
      18             : #include "llvm/ADT/SmallString.h"
      19             : #include "llvm/Analysis/ConstantFolding.h"
      20             : #include "llvm/Analysis/EHPersonalities.h"
      21             : #include "llvm/CodeGen/MachineConstantPool.h"
      22             : #include "llvm/CodeGen/MachineFrameInfo.h"
      23             : #include "llvm/CodeGen/MachineFunctionPass.h"
      24             : #include "llvm/CodeGen/MachineInstr.h"
      25             : #include "llvm/CodeGen/MachineJumpTableInfo.h"
      26             : #include "llvm/CodeGen/MachineModuleInfo.h"
      27             : #include "llvm/CodeGen/MachineRegisterInfo.h"
      28             : #include "llvm/CodeGen/Passes.h"
      29             : #include "llvm/CodeGen/PseudoSourceValue.h"
      30             : #include "llvm/CodeGen/WinEHFuncInfo.h"
      31             : #include "llvm/IR/DataLayout.h"
      32             : #include "llvm/IR/DebugInfo.h"
      33             : #include "llvm/IR/Function.h"
      34             : #include "llvm/IR/Module.h"
      35             : #include "llvm/IR/ModuleSlotTracker.h"
      36             : #include "llvm/MC/MCAsmInfo.h"
      37             : #include "llvm/MC/MCContext.h"
      38             : #include "llvm/Support/Debug.h"
      39             : #include "llvm/Support/GraphWriter.h"
      40             : #include "llvm/Support/raw_ostream.h"
      41             : #include "llvm/Target/TargetFrameLowering.h"
      42             : #include "llvm/Target/TargetLowering.h"
      43             : #include "llvm/Target/TargetMachine.h"
      44             : #include "llvm/Target/TargetSubtargetInfo.h"
      45             : using namespace llvm;
      46             : 
      47             : #define DEBUG_TYPE "codegen"
      48             : 
      49             : static cl::opt<unsigned>
      50       72306 :     AlignAllFunctions("align-all-functions",
      51      216918 :                       cl::desc("Force the alignment of all functions."),
      52      289224 :                       cl::init(0), cl::Hidden);
      53             : 
      54        3447 : static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
      55             :   typedef MachineFunctionProperties::Property P;
      56        3447 :   switch(Prop) {
      57             :   case P::FailedISel: return "FailedISel";
      58         626 :   case P::IsSSA: return "IsSSA";
      59           1 :   case P::Legalized: return "Legalized";
      60         796 :   case P::NoPHIs: return "NoPHIs";
      61         608 :   case P::NoVRegs: return "NoVRegs";
      62           2 :   case P::RegBankSelected: return "RegBankSelected";
      63           1 :   case P::Selected: return "Selected";
      64        1413 :   case P::TracksLiveness: return "TracksLiveness";
      65             :   }
      66           0 :   llvm_unreachable("Invalid machine function property");
      67             : }
      68             : 
      69        1416 : void MachineFunctionProperties::print(raw_ostream &OS) const {
      70        1416 :   const char *Separator = "";
      71       12744 :   for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
      72       22656 :     if (!Properties[I])
      73        7881 :       continue;
      74        3447 :     OS << Separator << getPropertyName(static_cast<Property>(I));
      75        3447 :     Separator = ", ";
      76             :   }
      77        1416 : }
      78             : 
      79             : //===----------------------------------------------------------------------===//
      80             : // MachineFunction implementation
      81             : //===----------------------------------------------------------------------===//
      82             : 
      83             : // Out-of-line virtual method.
      84      142890 : MachineFunctionInfo::~MachineFunctionInfo() {}
      85             : 
      86      304817 : void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
      87      304817 :   MBB->getParent()->DeleteMachineBasicBlock(MBB);
      88      304817 : }
      89             : 
      90      145510 : static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
      91             :                                            const Function *Fn) {
      92      145510 :   if (Fn->hasFnAttribute(Attribute::StackAlignment))
      93          30 :     return Fn->getFnStackAlignment();
      94      145480 :   return STI->getFrameLowering()->getStackAlignment();
      95             : }
      96             : 
      97      145417 : MachineFunction::MachineFunction(const Function *F, const TargetMachine &TM,
      98      145417 :                                  unsigned FunctionNum, MachineModuleInfo &mmi)
      99      290834 :     : Fn(F), Target(TM), STI(TM.getSubtargetImpl(*F)), Ctx(mmi.getContext()),
     100     3053757 :       MMI(mmi) {
     101      145417 :   FunctionNumber = FunctionNum;
     102      145417 :   init();
     103      145417 : }
     104             : 
     105      145510 : void MachineFunction::init() {
     106             :   // Assume the function starts in SSA form with correct liveness.
     107      291020 :   Properties.set(MachineFunctionProperties::Property::IsSSA);
     108      291020 :   Properties.set(MachineFunctionProperties::Property::TracksLiveness);
     109      145510 :   if (STI->getRegisterInfo())
     110      291020 :     RegInfo = new (Allocator) MachineRegisterInfo(this);
     111             :   else
     112           0 :     RegInfo = nullptr;
     113             : 
     114      145510 :   MFInfo = nullptr;
     115             :   // We can realign the stack if the target supports it and the user hasn't
     116             :   // explicitly asked us not to.
     117      284435 :   bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
     118      423360 :                       !Fn->hasFnAttribute("no-realign-stack");
     119      145510 :   FrameInfo = new (Allocator) MachineFrameInfo(
     120      145510 :       getFnStackAlignment(STI, Fn), /*StackRealignable=*/CanRealignSP,
     121      284298 :       /*ForceRealign=*/CanRealignSP &&
     122      714106 :           Fn->hasFnAttribute(Attribute::StackAlignment));
     123             : 
     124      291020 :   if (Fn->hasFnAttribute(Attribute::StackAlignment))
     125          30 :     FrameInfo->ensureMaxAlignment(Fn->getFnStackAlignment());
     126             : 
     127      436530 :   ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
     128      145510 :   Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
     129             : 
     130             :   // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn.
     131             :   // FIXME: Use Function::optForSize().
     132      291020 :   if (!Fn->hasFnAttribute(Attribute::OptimizeForSize))
     133      144189 :     Alignment = std::max(Alignment,
     134      432567 :                          STI->getTargetLowering()->getPrefFunctionAlignment());
     135             : 
     136      145510 :   if (AlignAllFunctions)
     137           0 :     Alignment = AlignAllFunctions;
     138             : 
     139      145510 :   JumpTableInfo = nullptr;
     140             : 
     141      145597 :   if (isFuncletEHPersonality(classifyEHPersonality(
     142      291020 :           Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr))) {
     143         174 :     WinEHInfo = new (Allocator) WinEHFuncInfo();
     144             :   }
     145             : 
     146             :   assert(Target.isCompatibleDataLayout(getDataLayout()) &&
     147             :          "Can't create a MachineFunction using a Module with a "
     148             :          "Target-incompatible DataLayout attached\n");
     149             : 
     150      145510 :   PSVManager = llvm::make_unique<PseudoSourceValueManager>();
     151      145510 : }
     152             : 
     153     2325116 : MachineFunction::~MachineFunction() {
     154      145320 :   clear();
     155      145320 : }
     156             : 
     157      145413 : void MachineFunction::clear() {
     158      290826 :   Properties.reset();
     159             :   // Don't call destructors on MachineInstr and MachineOperand. All of their
     160             :   // memory comes from the BumpPtrAllocator which is about to be purged.
     161             :   //
     162             :   // Do call MachineBasicBlock destructors, it contains std::vectors.
     163      582511 :   for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
     164      583370 :     I->Insts.clearAndLeakNodesUnsafely();
     165             : 
     166      290826 :   InstructionRecycler.clear(Allocator);
     167      290826 :   OperandRecycler.clear(Allocator);
     168      290826 :   BasicBlockRecycler.clear(Allocator);
     169      290826 :   CodeViewAnnotations.clear();
     170      290826 :   VariableDbgInfos.clear();
     171      145413 :   if (RegInfo) {
     172      145413 :     RegInfo->~MachineRegisterInfo();
     173      145413 :     Allocator.Deallocate(RegInfo);
     174             :   }
     175      145412 :   if (MFInfo) {
     176      142889 :     MFInfo->~MachineFunctionInfo();
     177      142889 :     Allocator.Deallocate(MFInfo);
     178             :   }
     179             : 
     180      145413 :   FrameInfo->~MachineFrameInfo();
     181      145413 :   Allocator.Deallocate(FrameInfo);
     182             : 
     183      145413 :   ConstantPool->~MachineConstantPool();
     184      145413 :   Allocator.Deallocate(ConstantPool);
     185             : 
     186      145413 :   if (JumpTableInfo) {
     187         434 :     JumpTableInfo->~MachineJumpTableInfo();
     188         217 :     Allocator.Deallocate(JumpTableInfo);
     189             :   }
     190             : 
     191      145413 :   if (WinEHInfo) {
     192          87 :     WinEHInfo->~WinEHFuncInfo();
     193          87 :     Allocator.Deallocate(WinEHInfo);
     194             :   }
     195      145413 : }
     196             : 
     197    47708763 : const DataLayout &MachineFunction::getDataLayout() const {
     198    47708763 :   return Fn->getParent()->getDataLayout();
     199             : }
     200             : 
     201             : /// Get the JumpTableInfo for this function.
     202             : /// If it does not already exist, allocate one.
     203         230 : MachineJumpTableInfo *MachineFunction::
     204             : getOrCreateJumpTableInfo(unsigned EntryKind) {
     205         230 :   if (JumpTableInfo) return JumpTableInfo;
     206             : 
     207         217 :   JumpTableInfo = new (Allocator)
     208         651 :     MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
     209         217 :   return JumpTableInfo;
     210             : }
     211             : 
     212             : /// Should we be emitting segmented stack stuff for the function
     213      213777 : bool MachineFunction::shouldSplitStack() const {
     214      427554 :   return getFunction()->hasFnAttribute("split-stack");
     215             : }
     216             : 
     217             : /// This discards all of the MachineBasicBlock numbers and recomputes them.
     218             : /// This guarantees that the MBB numbers are sequential, dense, and match the
     219             : /// ordering of the blocks within the function.  If a specific MachineBasicBlock
     220             : /// is specified, only that block and those after it are renumbered.
     221      411634 : void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
     222      411634 :   if (empty()) { MBBNumbering.clear(); return; }
     223      823268 :   MachineFunction::iterator MBBI, E = end();
     224      411634 :   if (MBB == nullptr)
     225      410806 :     MBBI = begin();
     226             :   else
     227        1656 :     MBBI = MBB->getIterator();
     228             : 
     229             :   // Figure out the block number this should have.
     230      411634 :   unsigned BlockNo = 0;
     231      411634 :   if (MBBI != begin())
     232        1636 :     BlockNo = std::prev(MBBI)->getNumber() + 1;
     233             : 
     234     2542046 :   for (; MBBI != E; ++MBBI, ++BlockNo) {
     235     1065206 :     if (MBBI->getNumber() != (int)BlockNo) {
     236             :       // Remove use of the old number.
     237      365885 :       if (MBBI->getNumber() != -1) {
     238             :         assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
     239             :                "MBB number mismatch!");
     240      324380 :         MBBNumbering[MBBI->getNumber()] = nullptr;
     241             :       }
     242             : 
     243             :       // If BlockNo is already taken, set that block's number to -1.
     244      731770 :       if (MBBNumbering[BlockNo])
     245      203695 :         MBBNumbering[BlockNo]->setNumber(-1);
     246             : 
     247      731770 :       MBBNumbering[BlockNo] = &*MBBI;
     248      365885 :       MBBI->setNumber(BlockNo);
     249             :     }
     250             :   }
     251             : 
     252             :   // Okay, all the blocks are renumbered.  If we have compactified the block
     253             :   // numbering, shrink MBBNumbering now.
     254             :   assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
     255      411634 :   MBBNumbering.resize(BlockNo);
     256             : }
     257             : 
     258             : /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
     259     6315503 : MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
     260             :                                                   const DebugLoc &DL,
     261             :                                                   bool NoImp) {
     262     6315503 :   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
     263    18946509 :     MachineInstr(*this, MCID, DL, NoImp);
     264             : }
     265             : 
     266             : /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
     267             : /// identical in all ways except the instruction has no parent, prev, or next.
     268             : MachineInstr *
     269      139910 : MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
     270      139910 :   return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
     271      139910 :              MachineInstr(*this, *Orig);
     272             : }
     273             : 
     274        4702 : MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
     275             :     MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
     276        4702 :   MachineInstr *FirstClone = nullptr;
     277        9404 :   MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
     278             :   for (;;) {
     279        4706 :     MachineInstr *Cloned = CloneMachineInstr(&*I);
     280        4706 :     MBB.insert(InsertBefore, Cloned);
     281        4706 :     if (FirstClone == nullptr) {
     282             :       FirstClone = Cloned;
     283             :     } else {
     284           4 :       Cloned->bundleWithPred();
     285             :     }
     286             : 
     287        9412 :     if (!I->isBundledWithSucc())
     288             :       break;
     289             :     ++I;
     290             :   }
     291        4702 :   return *FirstClone;
     292             : }
     293             : 
     294             : /// Delete the given MachineInstr.
     295             : ///
     296             : /// This function also serves as the MachineInstr destructor - the real
     297             : /// ~MachineInstr() destructor must be empty.
     298             : void
     299     3316462 : MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
     300             :   // Strip it for parts. The operand array and the MI object itself are
     301             :   // independently recyclable.
     302     3316462 :   if (MI->Operands)
     303     3307138 :     deallocateOperandArray(MI->CapOperands, MI->Operands);
     304             :   // Don't call ~MachineInstr() which must be trivial anyway because
     305             :   // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
     306             :   // destructors.
     307     6632924 :   InstructionRecycler.Deallocate(Allocator, MI);
     308     3316462 : }
     309             : 
     310             : /// Allocate a new MachineBasicBlock. Use this instead of
     311             : /// `new MachineBasicBlock'.
     312             : MachineBasicBlock *
     313      305766 : MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
     314      305766 :   return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
     315      305766 :              MachineBasicBlock(*this, bb);
     316             : }
     317             : 
     318             : /// Delete the given MachineBasicBlock.
     319             : void
     320      305659 : MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
     321             :   assert(MBB->getParent() == this && "MBB parent mismatch!");
     322      305659 :   MBB->~MachineBasicBlock();
     323      611318 :   BasicBlockRecycler.Deallocate(Allocator, MBB);
     324      305659 : }
     325             : 
     326     1430041 : MachineMemOperand *MachineFunction::getMachineMemOperand(
     327             :     MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
     328             :     unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
     329             :     SyncScope::ID SSID, AtomicOrdering Ordering,
     330             :     AtomicOrdering FailureOrdering) {
     331             :   return new (Allocator)
     332             :       MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
     333     2860082 :                         SSID, Ordering, FailureOrdering);
     334             : }
     335             : 
     336             : MachineMemOperand *
     337         932 : MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
     338             :                                       int64_t Offset, uint64_t Size) {
     339         923 :   if (MMO->getValue())
     340             :     return new (Allocator)
     341        3692 :                MachineMemOperand(MachinePointerInfo(MMO->getValue(),
     342         923 :                                                     MMO->getOffset()+Offset),
     343         923 :                                  MMO->getFlags(), Size, MMO->getBaseAlignment(),
     344        2769 :                                  AAMDNodes(), nullptr, MMO->getSyncScopeID(),
     345        4615 :                                  MMO->getOrdering(), MMO->getFailureOrdering());
     346             :   return new (Allocator)
     347          36 :              MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
     348           9 :                                                   MMO->getOffset()+Offset),
     349           9 :                                MMO->getFlags(), Size, MMO->getBaseAlignment(),
     350          27 :                                AAMDNodes(), nullptr, MMO->getSyncScopeID(),
     351          45 :                                MMO->getOrdering(), MMO->getFailureOrdering());
     352             : }
     353             : 
     354             : MachineMemOperand *
     355       27119 : MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
     356             :                                       const AAMDNodes &AAInfo) {
     357       27119 :   MachinePointerInfo MPI = MMO->getValue() ?
     358             :              MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
     359       54238 :              MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
     360             : 
     361             :   return new (Allocator)
     362       27119 :              MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
     363       27119 :                                MMO->getBaseAlignment(), AAInfo,
     364       54238 :                                MMO->getRanges(), MMO->getSyncScopeID(),
     365      108476 :                                MMO->getOrdering(), MMO->getFailureOrdering());
     366             : }
     367             : 
     368             : MachineInstr::mmo_iterator
     369     2498900 : MachineFunction::allocateMemRefsArray(unsigned long Num) {
     370     4997800 :   return Allocator.Allocate<MachineMemOperand *>(Num);
     371             : }
     372             : 
     373             : std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
     374        8892 : MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
     375             :                                     MachineInstr::mmo_iterator End) {
     376             :   // Count the number of load mem refs.
     377        8892 :   unsigned Num = 0;
     378       17795 :   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
     379       17806 :     if ((*I)->isLoad())
     380        8892 :       ++Num;
     381             : 
     382             :   // Allocate a new array and populate it with the load information.
     383        8892 :   MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
     384        8892 :   unsigned Index = 0;
     385       17795 :   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
     386       17806 :     if ((*I)->isLoad()) {
     387       17784 :       if (!(*I)->isStore())
     388             :         // Reuse the MMO.
     389        8892 :         Result[Index] = *I;
     390             :       else {
     391             :         // Clone the MMO and unset the store flag.
     392             :         MachineMemOperand *JustLoad =
     393           0 :           getMachineMemOperand((*I)->getPointerInfo(),
     394           0 :                                (*I)->getFlags() & ~MachineMemOperand::MOStore,
     395           0 :                                (*I)->getSize(), (*I)->getBaseAlignment(),
     396           0 :                                (*I)->getAAInfo(), nullptr,
     397           0 :                                (*I)->getSyncScopeID(), (*I)->getOrdering(),
     398           0 :                                (*I)->getFailureOrdering());
     399           0 :         Result[Index] = JustLoad;
     400             :       }
     401        8892 :       ++Index;
     402             :     }
     403             :   }
     404       17784 :   return std::make_pair(Result, Result + Num);
     405             : }
     406             : 
     407             : std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
     408          11 : MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
     409             :                                      MachineInstr::mmo_iterator End) {
     410             :   // Count the number of load mem refs.
     411          11 :   unsigned Num = 0;
     412          33 :   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
     413          44 :     if ((*I)->isStore())
     414          11 :       ++Num;
     415             : 
     416             :   // Allocate a new array and populate it with the store information.
     417          11 :   MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
     418          11 :   unsigned Index = 0;
     419          33 :   for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
     420          44 :     if ((*I)->isStore()) {
     421          22 :       if (!(*I)->isLoad())
     422             :         // Reuse the MMO.
     423          11 :         Result[Index] = *I;
     424             :       else {
     425             :         // Clone the MMO and unset the load flag.
     426             :         MachineMemOperand *JustStore =
     427           0 :           getMachineMemOperand((*I)->getPointerInfo(),
     428           0 :                                (*I)->getFlags() & ~MachineMemOperand::MOLoad,
     429           0 :                                (*I)->getSize(), (*I)->getBaseAlignment(),
     430           0 :                                (*I)->getAAInfo(), nullptr,
     431           0 :                                (*I)->getSyncScopeID(), (*I)->getOrdering(),
     432           0 :                                (*I)->getFailureOrdering());
     433           0 :         Result[Index] = JustStore;
     434             :       }
     435          11 :       ++Index;
     436             :     }
     437             :   }
     438          22 :   return std::make_pair(Result, Result + Num);
     439             : }
     440             : 
     441         155 : const char *MachineFunction::createExternalSymbolName(StringRef Name) {
     442         310 :   char *Dest = Allocator.Allocate<char>(Name.size() + 1);
     443         310 :   std::copy(Name.begin(), Name.end(), Dest);
     444         155 :   Dest[Name.size()] = 0;
     445         155 :   return Dest;
     446             : }
     447             : 
     448             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     449             : LLVM_DUMP_METHOD void MachineFunction::dump() const {
     450             :   print(dbgs());
     451             : }
     452             : #endif
     453             : 
     454       17539 : StringRef MachineFunction::getName() const {
     455             :   assert(getFunction() && "No function!");
     456       17539 :   return getFunction()->getName();
     457             : }
     458             : 
     459        1416 : void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
     460        1416 :   OS << "# Machine code for function " << getName() << ": ";
     461        1416 :   getProperties().print(OS);
     462        1416 :   OS << '\n';
     463             : 
     464             :   // Print Frame Information
     465        1416 :   FrameInfo->print(*this, OS);
     466             : 
     467             :   // Print JumpTable Information
     468        1416 :   if (JumpTableInfo)
     469         761 :     JumpTableInfo->print(OS);
     470             : 
     471             :   // Print Constant Pool
     472        1416 :   ConstantPool->print(OS);
     473             : 
     474        1416 :   const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
     475             : 
     476        2832 :   if (RegInfo && !RegInfo->livein_empty()) {
     477        1134 :     OS << "Function Live Ins: ";
     478             :     for (MachineRegisterInfo::livein_iterator
     479        6644 :          I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
     480        6324 :       OS << PrintReg(I->first, TRI);
     481        2108 :       if (I->second)
     482        3543 :         OS << " in " << PrintReg(I->second, TRI);
     483        4216 :       if (std::next(I) != E)
     484         974 :         OS << ", ";
     485             :     }
     486             :     OS << '\n';
     487             :   }
     488             : 
     489        2832 :   ModuleSlotTracker MST(getFunction()->getParent());
     490        1416 :   MST.incorporateFunction(*getFunction());
     491       16451 :   for (const auto &BB : *this) {
     492       12203 :     OS << '\n';
     493       12203 :     BB.print(OS, MST, Indexes);
     494             :   }
     495             : 
     496        1416 :   OS << "\n# End machine code for function " << getName() << ".\n\n";
     497        1416 : }
     498             : 
     499             : namespace llvm {
     500             :   template<>
     501             :   struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
     502             : 
     503             :   DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
     504             : 
     505             :     static std::string getGraphName(const MachineFunction *F) {
     506             :       return ("CFG for '" + F->getName() + "' function").str();
     507             :     }
     508             : 
     509             :     std::string getNodeLabel(const MachineBasicBlock *Node,
     510             :                              const MachineFunction *Graph) {
     511             :       std::string OutStr;
     512             :       {
     513             :         raw_string_ostream OSS(OutStr);
     514             : 
     515             :         if (isSimple()) {
     516             :           OSS << "BB#" << Node->getNumber();
     517             :           if (const BasicBlock *BB = Node->getBasicBlock())
     518             :             OSS << ": " << BB->getName();
     519             :         } else
     520             :           Node->print(OSS);
     521             :       }
     522             : 
     523             :       if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
     524             : 
     525             :       // Process string output to make it nicer...
     526             :       for (unsigned i = 0; i != OutStr.length(); ++i)
     527             :         if (OutStr[i] == '\n') {                            // Left justify
     528             :           OutStr[i] = '\\';
     529             :           OutStr.insert(OutStr.begin()+i+1, 'l');
     530             :         }
     531             :       return OutStr;
     532             :     }
     533             :   };
     534             : }
     535             : 
     536           0 : void MachineFunction::viewCFG() const
     537             : {
     538             : #ifndef NDEBUG
     539             :   ViewGraph(this, "mf" + getName());
     540             : #else
     541           0 :   errs() << "MachineFunction::viewCFG is only available in debug builds on "
     542           0 :          << "systems with Graphviz or gv!\n";
     543             : #endif // NDEBUG
     544           0 : }
     545             : 
     546           0 : void MachineFunction::viewCFGOnly() const
     547             : {
     548             : #ifndef NDEBUG
     549             :   ViewGraph(this, "mf" + getName(), true);
     550             : #else
     551           0 :   errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
     552           0 :          << "systems with Graphviz or gv!\n";
     553             : #endif // NDEBUG
     554           0 : }
     555             : 
     556             : /// Add the specified physical register as a live-in value and
     557             : /// create a corresponding virtual register for it.
     558      231151 : unsigned MachineFunction::addLiveIn(unsigned PReg,
     559             :                                     const TargetRegisterClass *RC) {
     560      231151 :   MachineRegisterInfo &MRI = getRegInfo();
     561      231151 :   unsigned VReg = MRI.getLiveInVirtReg(PReg);
     562      231151 :   if (VReg) {
     563             :     const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
     564             :     (void)VRegRC;
     565             :     // A physical register can be added several times.
     566             :     // Between two calls, the register class of the related virtual register
     567             :     // may have been constrained to match some operation constraints.
     568             :     // In that case, check that the current register class includes the
     569             :     // physical register and is a sub class of the specified RC.
     570             :     assert((VRegRC == RC || (VRegRC->contains(PReg) &&
     571             :                              RC->hasSubClassEq(VRegRC))) &&
     572             :             "Register class mismatch!");
     573             :     return VReg;
     574             :   }
     575      231107 :   VReg = MRI.createVirtualRegister(RC);
     576      231107 :   MRI.addLiveIn(PReg, VReg);
     577      231107 :   return VReg;
     578             : }
     579             : 
     580             : /// Return the MCSymbol for the specified non-empty jump table.
     581             : /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
     582             : /// normal 'L' label is returned.
     583         850 : MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
     584             :                                         bool isLinkerPrivate) const {
     585         850 :   const DataLayout &DL = getDataLayout();
     586             :   assert(JumpTableInfo && "No jump tables");
     587             :   assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
     588             : 
     589             :   StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
     590         857 :                                      : DL.getPrivateGlobalPrefix();
     591        1700 :   SmallString<60> Name;
     592        1700 :   raw_svector_ostream(Name)
     593        3400 :     << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
     594        1700 :   return Ctx.getOrCreateSymbol(Name);
     595             : }
     596             : 
     597             : /// Return a function-local symbol to represent the PIC base.
     598        5714 : MCSymbol *MachineFunction::getPICBaseSymbol() const {
     599        5714 :   const DataLayout &DL = getDataLayout();
     600       28570 :   return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
     601       22856 :                                Twine(getFunctionNumber()) + "$pb");
     602             : }
     603             : 
     604             : /// \name Exception Handling
     605             : /// \{
     606             : 
     607             : LandingPadInfo &
     608       70078 : MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
     609      140156 :   unsigned N = LandingPads.size();
     610     2197351 :   for (unsigned i = 0; i < N; ++i) {
     611     4351198 :     LandingPadInfo &LP = LandingPads[i];
     612     2175599 :     if (LP.LandingPadBlock == LandingPad)
     613             :       return LP;
     614             :   }
     615             : 
     616       65256 :   LandingPads.push_back(LandingPadInfo(LandingPad));
     617       43504 :   return LandingPads[N];
     618             : }
     619             : 
     620       26492 : void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
     621             :                                 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
     622       26492 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     623       26492 :   LP.BeginLabels.push_back(BeginLabel);
     624       26492 :   LP.EndLabels.push_back(EndLabel);
     625       26492 : }
     626             : 
     627       21752 : MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
     628       21752 :   MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
     629       21752 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     630       21752 :   LP.LandingPadLabel = LandingPadLabel;
     631       21752 :   return LandingPadLabel;
     632             : }
     633             : 
     634        6098 : void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
     635             :                                        ArrayRef<const GlobalValue *> TyInfo) {
     636        6098 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     637       12196 :   for (unsigned N = TyInfo.size(); N; --N)
     638       18294 :     LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
     639        6098 : }
     640             : 
     641          23 : void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
     642             :                                         ArrayRef<const GlobalValue *> TyInfo) {
     643          23 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     644          69 :   std::vector<unsigned> IdsInFilter(TyInfo.size());
     645          25 :   for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
     646           6 :     IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
     647          46 :   LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
     648          23 : }
     649             : 
     650        2698 : void MachineFunction::tidyLandingPads(DenseMap<MCSymbol*, uintptr_t> *LPMap) {
     651       27133 :   for (unsigned i = 0; i != LandingPads.size(); ) {
     652       43474 :     LandingPadInfo &LandingPad = LandingPads[i];
     653       43474 :     if (LandingPad.LandingPadLabel &&
     654       43474 :         !LandingPad.LandingPadLabel->isDefined() &&
     655           0 :         (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
     656           0 :       LandingPad.LandingPadLabel = nullptr;
     657             : 
     658             :     // Special case: we *should* emit LPs with null LP MBB. This indicates
     659             :     // "nounwind" case.
     660       21737 :     if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
     661           0 :       LandingPads.erase(LandingPads.begin() + i);
     662           0 :       continue;
     663             :     }
     664             : 
     665       91687 :     for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
     666       52952 :       MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
     667       52952 :       MCSymbol *EndLabel = LandingPad.EndLabels[j];
     668       79428 :       if ((BeginLabel->isDefined() ||
     669       52952 :            (LPMap && (*LPMap)[BeginLabel] != 0)) &&
     670       52952 :           (EndLabel->isDefined() ||
     671       26476 :            (LPMap && (*LPMap)[EndLabel] != 0))) continue;
     672             : 
     673           0 :       LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
     674           0 :       LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
     675           0 :       --j;
     676           0 :       --e;
     677             :     }
     678             : 
     679             :     // Remove landing pads with no try-ranges.
     680       43476 :     if (LandingPads[i].BeginLabels.empty()) {
     681          10 :       LandingPads.erase(LandingPads.begin() + i);
     682           2 :       continue;
     683             :     }
     684             : 
     685             :     // If there is no landing pad, ensure that the list of typeids is empty.
     686             :     // If the only typeid is a cleanup, this is the same as having no typeids.
     687       43470 :     if (!LandingPad.LandingPadBlock ||
     688       43399 :         (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
     689       15699 :       LandingPad.TypeIds.clear();
     690       21735 :     ++i;
     691             :   }
     692        2698 : }
     693             : 
     694       15713 : void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
     695       15713 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     696       31426 :   LP.TypeIds.push_back(0);
     697       15713 : }
     698             : 
     699           0 : void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
     700             :                                          const Function *Filter,
     701             :                                          const BlockAddress *RecoverBA) {
     702           0 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     703             :   SEHHandler Handler;
     704           0 :   Handler.FilterOrFinally = Filter;
     705           0 :   Handler.RecoverBA = RecoverBA;
     706           0 :   LP.SEHHandlers.push_back(Handler);
     707           0 : }
     708             : 
     709           0 : void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
     710             :                                            const Function *Cleanup) {
     711           0 :   LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
     712             :   SEHHandler Handler;
     713           0 :   Handler.FilterOrFinally = Cleanup;
     714           0 :   Handler.RecoverBA = nullptr;
     715           0 :   LP.SEHHandlers.push_back(Handler);
     716           0 : }
     717             : 
     718       21748 : void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
     719             :                                             ArrayRef<unsigned> Sites) {
     720       65244 :   LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
     721       21748 : }
     722             : 
     723        6172 : unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
     724       12493 :   for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
     725       10694 :     if (TypeInfos[i] == TI) return i + 1;
     726             : 
     727         974 :   TypeInfos.push_back(TI);
     728        1948 :   return TypeInfos.size();
     729             : }
     730             : 
     731          23 : int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
     732             :   // If the new filter coincides with the tail of an existing filter, then
     733             :   // re-use the existing filter.  Folding filters more than this requires
     734             :   // re-ordering filters and/or their elements - probably not worth it.
     735          46 :   for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
     736          69 :        E = FilterEnds.end(); I != E; ++I) {
     737          16 :     unsigned i = *I, j = TyIds.size();
     738             : 
     739           8 :     while (i && j)
     740           0 :       if (FilterIds[--i] != TyIds[--j])
     741             :         goto try_next;
     742             : 
     743           8 :     if (!j)
     744             :       // The new filter coincides with range [i, end) of the existing filter.
     745           8 :       return -(1 + i);
     746             : 
     747           0 : try_next:;
     748             :   }
     749             : 
     750             :   // Add the new filter.
     751          30 :   int FilterID = -(1 + FilterIds.size());
     752          30 :   FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
     753          75 :   FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
     754          45 :   FilterEnds.push_back(FilterIds.size());
     755          30 :   FilterIds.push_back(0); // terminator
     756          15 :   return FilterID;
     757             : }
     758             : 
     759       21752 : void llvm::addLandingPadInfo(const LandingPadInst &I, MachineBasicBlock &MBB) {
     760       21752 :   MachineFunction &MF = *MBB.getParent();
     761       43504 :   if (const auto *PF = dyn_cast<Function>(
     762       21751 :           I.getParent()->getParent()->getPersonalityFn()->stripPointerCasts()))
     763       21751 :     MF.getMMI().addPersonality(PF);
     764             : 
     765       21752 :   if (I.isCleanup())
     766       15713 :     MF.addCleanup(&MBB);
     767             : 
     768             :   // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct,
     769             :   //        but we need to do it this way because of how the DWARF EH emitter
     770             :   //        processes the clauses.
     771       21752 :   for (unsigned i = I.getNumClauses(); i != 0; --i) {
     772       12242 :     Value *Val = I.getClause(i - 1);
     773       12242 :     if (I.isCatch(i - 1)) {
     774        6098 :       MF.addCatchTypeInfo(&MBB,
     775       18294 :                           dyn_cast<GlobalValue>(Val->stripPointerCasts()));
     776             :     } else {
     777             :       // Add filters in a list.
     778          23 :       Constant *CVal = cast<Constant>(Val);
     779          46 :       SmallVector<const GlobalValue *, 4> FilterList;
     780          71 :       for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
     781          25 :            II != IE; ++II)
     782           6 :         FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
     783             : 
     784          23 :       MF.addFilterTypeInfo(&MBB, FilterList);
     785             :     }
     786             :   }
     787       21752 : }
     788             : 
     789             : /// \}
     790             : 
     791             : //===----------------------------------------------------------------------===//
     792             : //  MachineJumpTableInfo implementation
     793             : //===----------------------------------------------------------------------===//
     794             : 
     795             : /// Return the size of each entry in the jump table.
     796        3856 : unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
     797             :   // The size of a jump table entry is 4 bytes unless the entry is just the
     798             :   // address of a block, in which case it is the pointer size.
     799        3856 :   switch (getEntryKind()) {
     800        2209 :   case MachineJumpTableInfo::EK_BlockAddress:
     801        2209 :     return TD.getPointerSize();
     802             :   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
     803             :     return 8;
     804        1645 :   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
     805             :   case MachineJumpTableInfo::EK_LabelDifference32:
     806             :   case MachineJumpTableInfo::EK_Custom32:
     807        1645 :     return 4;
     808           0 :   case MachineJumpTableInfo::EK_Inline:
     809           0 :     return 0;
     810             :   }
     811           0 :   llvm_unreachable("Unknown jump table encoding!");
     812             : }
     813             : 
     814             : /// Return the alignment of each entry in the jump table.
     815         141 : unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
     816             :   // The alignment of a jump table entry is the alignment of int32 unless the
     817             :   // entry is just the address of a block, in which case it is the pointer
     818             :   // alignment.
     819         141 :   switch (getEntryKind()) {
     820          94 :   case MachineJumpTableInfo::EK_BlockAddress:
     821          94 :     return TD.getPointerABIAlignment();
     822           2 :   case MachineJumpTableInfo::EK_GPRel64BlockAddress:
     823           2 :     return TD.getABIIntegerTypeAlignment(64);
     824          45 :   case MachineJumpTableInfo::EK_GPRel32BlockAddress:
     825             :   case MachineJumpTableInfo::EK_LabelDifference32:
     826             :   case MachineJumpTableInfo::EK_Custom32:
     827          45 :     return TD.getABIIntegerTypeAlignment(32);
     828             :   case MachineJumpTableInfo::EK_Inline:
     829             :     return 1;
     830             :   }
     831           0 :   llvm_unreachable("Unknown jump table encoding!");
     832             : }
     833             : 
     834             : /// Create a new jump table entry in the jump table info.
     835         231 : unsigned MachineJumpTableInfo::createJumpTableIndex(
     836             :                                const std::vector<MachineBasicBlock*> &DestBBs) {
     837             :   assert(!DestBBs.empty() && "Cannot create an empty jump table!");
     838         924 :   JumpTables.push_back(MachineJumpTableEntry(DestBBs));
     839         462 :   return JumpTables.size()-1;
     840             : }
     841             : 
     842             : /// If Old is the target of any jump tables, update the jump tables to branch
     843             : /// to New instead.
     844         323 : bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
     845             :                                                   MachineBasicBlock *New) {
     846             :   assert(Old != New && "Not making a change?");
     847         323 :   bool MadeChange = false;
     848        1084 :   for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
     849         438 :     ReplaceMBBInJumpTable(i, Old, New);
     850         323 :   return MadeChange;
     851             : }
     852             : 
     853             : /// If Old is a target of the jump tables, update the jump table to branch to
     854             : /// New instead.
     855         454 : bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
     856             :                                                  MachineBasicBlock *Old,
     857             :                                                  MachineBasicBlock *New) {
     858             :   assert(Old != New && "Not making a change?");
     859         454 :   bool MadeChange = false;
     860         908 :   MachineJumpTableEntry &JTE = JumpTables[Idx];
     861       45794 :   for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
     862       89772 :     if (JTE.MBBs[j] == Old) {
     863        1930 :       JTE.MBBs[j] = New;
     864        1930 :       MadeChange = true;
     865             :     }
     866         454 :   return MadeChange;
     867             : }
     868             : 
     869         761 : void MachineJumpTableInfo::print(raw_ostream &OS) const {
     870        1522 :   if (JumpTables.empty()) return;
     871             : 
     872         761 :   OS << "Jump Tables:\n";
     873             : 
     874        2553 :   for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
     875        2062 :     OS << "  jt#" << i << ": ";
     876        9959 :     for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
     877       27464 :       OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
     878             :   }
     879             : 
     880             :   OS << '\n';
     881             : }
     882             : 
     883             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     884             : LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
     885             : #endif
     886             : 
     887             : 
     888             : //===----------------------------------------------------------------------===//
     889             : //  MachineConstantPool implementation
     890             : //===----------------------------------------------------------------------===//
     891             : 
     892           0 : void MachineConstantPoolValue::anchor() { }
     893             : 
     894       35430 : Type *MachineConstantPoolEntry::getType() const {
     895       70860 :   if (isMachineConstantPoolEntry())
     896         641 :     return Val.MachineCPVal->getType();
     897       34789 :   return Val.ConstVal->getType();
     898             : }
     899             : 
     900       16642 : bool MachineConstantPoolEntry::needsRelocation() const {
     901       33284 :   if (isMachineConstantPoolEntry())
     902             :     return true;
     903       16625 :   return Val.ConstVal->needsRelocation();
     904             : }
     905             : 
     906             : SectionKind
     907       16642 : MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
     908       16642 :   if (needsRelocation())
     909             :     return SectionKind::getReadOnlyWithRel();
     910       16617 :   switch (DL->getTypeAllocSize(getType())) {
     911             :   case 4:
     912             :     return SectionKind::getMergeableConst4();
     913             :   case 8:
     914             :     return SectionKind::getMergeableConst8();
     915             :   case 16:
     916             :     return SectionKind::getMergeableConst16();
     917             :   case 32:
     918             :     return SectionKind::getMergeableConst32();
     919             :   default:
     920             :     return SectionKind::getReadOnly();
     921             :   }
     922             : }
     923             : 
     924      581650 : MachineConstantPool::~MachineConstantPool() {
     925             :   // A constant may be a member of both Constants and MachineCPVsSharingEntries,
     926             :   // so keep track of which we've deleted to avoid double deletions.
     927      290825 :   DenseSet<MachineConstantPoolValue*> Deleted;
     928      309686 :   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
     929       56580 :     if (Constants[i].isMachineConstantPoolEntry()) {
     930        1290 :       Deleted.insert(Constants[i].Val.MachineCPVal);
     931        1290 :       delete Constants[i].Val.MachineCPVal;
     932             :     }
     933             :   for (DenseSet<MachineConstantPoolValue*>::iterator I =
     934      436239 :        MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
     935      290848 :        I != E; ++I) {
     936          22 :     if (Deleted.count(*I) == 0)
     937           7 :       delete *I;
     938             :   }
     939      145413 : }
     940             : 
     941             : /// Test whether the given two constants can be allocated the same constant pool
     942             : /// entry.
     943      134063 : static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
     944             :                                       const DataLayout &DL) {
     945             :   // Handle the trivial case quickly.
     946      134063 :   if (A == B) return true;
     947             : 
     948             :   // If they have the same type but weren't the same constant, quickly
     949             :   // reject them.
     950      130415 :   if (A->getType() == B->getType()) return false;
     951             : 
     952             :   // We can't handle structs or arrays.
     953      185770 :   if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
     954      185766 :       isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
     955             :     return false;
     956             : 
     957             :   // For now, only support constants with the same size.
     958       92874 :   uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
     959       92874 :   if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
     960             :     return false;
     961             : 
     962        3396 :   Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
     963             : 
     964             :   // Try constant folding a bitcast of both instructions to an integer.  If we
     965             :   // get two identical ConstantInt's, then we are good to share them.  We use
     966             :   // the constant folding APIs to do this so that we get the benefit of
     967             :   // DataLayout.
     968        6792 :   if (isa<PointerType>(A->getType()))
     969         459 :     A = ConstantFoldCastOperand(Instruction::PtrToInt,
     970             :                                 const_cast<Constant *>(A), IntTy, DL);
     971        2937 :   else if (A->getType() != IntTy)
     972        2809 :     A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
     973             :                                 IntTy, DL);
     974        6792 :   if (isa<PointerType>(B->getType()))
     975         364 :     B = ConstantFoldCastOperand(Instruction::PtrToInt,
     976             :                                 const_cast<Constant *>(B), IntTy, DL);
     977        3032 :   else if (B->getType() != IntTy)
     978        2846 :     B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
     979             :                                 IntTy, DL);
     980             : 
     981        3396 :   return A == B;
     982             : }
     983             : 
     984             : /// Create a new entry in the constant pool or return an existing one.
     985             : /// User must specify the log2 of the minimum required alignment for the object.
     986       21897 : unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
     987             :                                                    unsigned Alignment) {
     988             :   assert(Alignment && "Alignment must be specified!");
     989       21897 :   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
     990             : 
     991             :   // Check to see if we already have this constant.
     992             :   //
     993             :   // FIXME, this could be made much more efficient for large constant pools.
     994      174336 :   for (unsigned i = 0, e = Constants.size(); i != e; ++i)
     995      536735 :     if (!Constants[i].isMachineConstantPoolEntry() &&
     996      134063 :         CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
     997       11046 :       if ((unsigned)Constants[i].getAlignment() < Alignment)
     998           0 :         Constants[i].Alignment = Alignment;
     999             :       return i;
    1000             :     }
    1001             : 
    1002       54645 :   Constants.push_back(MachineConstantPoolEntry(C, Alignment));
    1003       36430 :   return Constants.size()-1;
    1004             : }
    1005             : 
    1006         656 : unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
    1007             :                                                    unsigned Alignment) {
    1008             :   assert(Alignment && "Alignment must be specified!");
    1009         656 :   if (Alignment > PoolAlignment) PoolAlignment = Alignment;
    1010             : 
    1011             :   // Check to see if we already have this constant.
    1012             :   //
    1013             :   // FIXME, this could be made much more efficient for large constant pools.
    1014         656 :   int Idx = V->getExistingMachineCPValue(this, Alignment);
    1015         656 :   if (Idx != -1) {
    1016          22 :     MachineCPVsSharingEntries.insert(V);
    1017          11 :     return (unsigned)Idx;
    1018             :   }
    1019             : 
    1020        1935 :   Constants.push_back(MachineConstantPoolEntry(V, Alignment));
    1021        1290 :   return Constants.size()-1;
    1022             : }
    1023             : 
    1024        1416 : void MachineConstantPool::print(raw_ostream &OS) const {
    1025        2832 :   if (Constants.empty()) return;
    1026             : 
    1027           1 :   OS << "Constant Pool:\n";
    1028           4 :   for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
    1029           4 :     OS << "  cp#" << i << ": ";
    1030           6 :     if (Constants[i].isMachineConstantPoolEntry())
    1031           2 :       Constants[i].Val.MachineCPVal->print(OS);
    1032             :     else
    1033           0 :       Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
    1034           8 :     OS << ", align=" << Constants[i].getAlignment();
    1035           2 :     OS << "\n";
    1036             :   }
    1037      216918 : }
    1038             : 
    1039             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    1040             : LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
    1041             : #endif

Generated by: LCOV version 1.13