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

Generated by: LCOV version 1.13