10b57cec5SDimitry Andric //===- MachineFunction.cpp ------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // Collect native machine code information for a function. This allows 100b57cec5SDimitry Andric // target-specific information about the generated code to be stored with each 110b57cec5SDimitry Andric // function. 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 160b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 170b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 180b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 190b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 200b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 210b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 220b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 230b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 240b57cec5SDimitry Andric #include "llvm/Analysis/ConstantFolding.h" 250b57cec5SDimitry Andric #include "llvm/Analysis/EHPersonalities.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h" 310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 320b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 330b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 340b57cec5SDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h" 350b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 360b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 370b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 380b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 390b57cec5SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h" 400b57cec5SDimitry Andric #include "llvm/CodeGen/WinEHFuncInfo.h" 410b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 420b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 430b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 440b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 450b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 460b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 470b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 480b57cec5SDimitry Andric #include "llvm/IR/Function.h" 490b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 500b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 510b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 520b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 530b57cec5SDimitry Andric #include "llvm/IR/Module.h" 540b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h" 550b57cec5SDimitry Andric #include "llvm/IR/Value.h" 560b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 570b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 580b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h" 590b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 600b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 610b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 620b57cec5SDimitry Andric #include "llvm/Support/DOTGraphTraits.h" 630b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 640b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 650b57cec5SDimitry Andric #include "llvm/Support/GraphWriter.h" 660b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 670b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 680b57cec5SDimitry Andric #include <algorithm> 690b57cec5SDimitry Andric #include <cassert> 700b57cec5SDimitry Andric #include <cstddef> 710b57cec5SDimitry Andric #include <cstdint> 720b57cec5SDimitry Andric #include <iterator> 730b57cec5SDimitry Andric #include <string> 740b57cec5SDimitry Andric #include <utility> 750b57cec5SDimitry Andric #include <vector> 760b57cec5SDimitry Andric 770b57cec5SDimitry Andric using namespace llvm; 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric #define DEBUG_TYPE "codegen" 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric static cl::opt<unsigned> 820b57cec5SDimitry Andric AlignAllFunctions("align-all-functions", 830b57cec5SDimitry Andric cl::desc("Force the alignment of all functions."), 840b57cec5SDimitry Andric cl::init(0), cl::Hidden); 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric static const char *getPropertyName(MachineFunctionProperties::Property Prop) { 870b57cec5SDimitry Andric using P = MachineFunctionProperties::Property; 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric switch(Prop) { 900b57cec5SDimitry Andric case P::FailedISel: return "FailedISel"; 910b57cec5SDimitry Andric case P::IsSSA: return "IsSSA"; 920b57cec5SDimitry Andric case P::Legalized: return "Legalized"; 930b57cec5SDimitry Andric case P::NoPHIs: return "NoPHIs"; 940b57cec5SDimitry Andric case P::NoVRegs: return "NoVRegs"; 950b57cec5SDimitry Andric case P::RegBankSelected: return "RegBankSelected"; 960b57cec5SDimitry Andric case P::Selected: return "Selected"; 970b57cec5SDimitry Andric case P::TracksLiveness: return "TracksLiveness"; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric llvm_unreachable("Invalid machine function property"); 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric // Pin the vtable to this file. 1030b57cec5SDimitry Andric void MachineFunction::Delegate::anchor() {} 1040b57cec5SDimitry Andric 1050b57cec5SDimitry Andric void MachineFunctionProperties::print(raw_ostream &OS) const { 1060b57cec5SDimitry Andric const char *Separator = ""; 1070b57cec5SDimitry Andric for (BitVector::size_type I = 0; I < Properties.size(); ++I) { 1080b57cec5SDimitry Andric if (!Properties[I]) 1090b57cec5SDimitry Andric continue; 1100b57cec5SDimitry Andric OS << Separator << getPropertyName(static_cast<Property>(I)); 1110b57cec5SDimitry Andric Separator = ", "; 1120b57cec5SDimitry Andric } 1130b57cec5SDimitry Andric } 1140b57cec5SDimitry Andric 1150b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1160b57cec5SDimitry Andric // MachineFunction implementation 1170b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric // Out-of-line virtual method. 1200b57cec5SDimitry Andric MachineFunctionInfo::~MachineFunctionInfo() = default; 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 1230b57cec5SDimitry Andric MBB->getParent()->DeleteMachineBasicBlock(MBB); 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI, 1270b57cec5SDimitry Andric const Function &F) { 1280b57cec5SDimitry Andric if (F.hasFnAttribute(Attribute::StackAlignment)) 1290b57cec5SDimitry Andric return F.getFnStackAlignment(); 1300b57cec5SDimitry Andric return STI->getFrameLowering()->getStackAlignment(); 1310b57cec5SDimitry Andric } 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric MachineFunction::MachineFunction(const Function &F, 1340b57cec5SDimitry Andric const LLVMTargetMachine &Target, 1350b57cec5SDimitry Andric const TargetSubtargetInfo &STI, 1360b57cec5SDimitry Andric unsigned FunctionNum, MachineModuleInfo &mmi) 1370b57cec5SDimitry Andric : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) { 1380b57cec5SDimitry Andric FunctionNumber = FunctionNum; 1390b57cec5SDimitry Andric init(); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric void MachineFunction::handleInsertion(MachineInstr &MI) { 1430b57cec5SDimitry Andric if (TheDelegate) 1440b57cec5SDimitry Andric TheDelegate->MF_HandleInsertion(MI); 1450b57cec5SDimitry Andric } 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric void MachineFunction::handleRemoval(MachineInstr &MI) { 1480b57cec5SDimitry Andric if (TheDelegate) 1490b57cec5SDimitry Andric TheDelegate->MF_HandleRemoval(MI); 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric void MachineFunction::init() { 1530b57cec5SDimitry Andric // Assume the function starts in SSA form with correct liveness. 1540b57cec5SDimitry Andric Properties.set(MachineFunctionProperties::Property::IsSSA); 1550b57cec5SDimitry Andric Properties.set(MachineFunctionProperties::Property::TracksLiveness); 1560b57cec5SDimitry Andric if (STI->getRegisterInfo()) 1570b57cec5SDimitry Andric RegInfo = new (Allocator) MachineRegisterInfo(this); 1580b57cec5SDimitry Andric else 1590b57cec5SDimitry Andric RegInfo = nullptr; 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric MFInfo = nullptr; 1620b57cec5SDimitry Andric // We can realign the stack if the target supports it and the user hasn't 1630b57cec5SDimitry Andric // explicitly asked us not to. 1640b57cec5SDimitry Andric bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && 1650b57cec5SDimitry Andric !F.hasFnAttribute("no-realign-stack"); 1660b57cec5SDimitry Andric FrameInfo = new (Allocator) MachineFrameInfo( 1670b57cec5SDimitry Andric getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, 1680b57cec5SDimitry Andric /*ForcedRealign=*/CanRealignSP && 1690b57cec5SDimitry Andric F.hasFnAttribute(Attribute::StackAlignment)); 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric if (F.hasFnAttribute(Attribute::StackAlignment)) 1720b57cec5SDimitry Andric FrameInfo->ensureMaxAlignment(F.getFnStackAlignment()); 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); 1750b57cec5SDimitry Andric Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. 1780b57cec5SDimitry Andric // FIXME: Use Function::hasOptSize(). 1790b57cec5SDimitry Andric if (!F.hasFnAttribute(Attribute::OptimizeForSize)) 1800b57cec5SDimitry Andric Alignment = std::max(Alignment, 1810b57cec5SDimitry Andric STI->getTargetLowering()->getPrefFunctionAlignment()); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric if (AlignAllFunctions) 1840b57cec5SDimitry Andric Alignment = AlignAllFunctions; 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric JumpTableInfo = nullptr; 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric if (isFuncletEHPersonality(classifyEHPersonality( 1890b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 1900b57cec5SDimitry Andric WinEHInfo = new (Allocator) WinEHFuncInfo(); 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric if (isScopedEHPersonality(classifyEHPersonality( 1940b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 1950b57cec5SDimitry Andric WasmEHInfo = new (Allocator) WasmEHFuncInfo(); 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 1980b57cec5SDimitry Andric assert(Target.isCompatibleDataLayout(getDataLayout()) && 1990b57cec5SDimitry Andric "Can't create a MachineFunction using a Module with a " 2000b57cec5SDimitry Andric "Target-incompatible DataLayout attached\n"); 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric PSVManager = 2030b57cec5SDimitry Andric llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget(). 2040b57cec5SDimitry Andric getInstrInfo())); 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric MachineFunction::~MachineFunction() { 2080b57cec5SDimitry Andric clear(); 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric void MachineFunction::clear() { 2120b57cec5SDimitry Andric Properties.reset(); 2130b57cec5SDimitry Andric // Don't call destructors on MachineInstr and MachineOperand. All of their 2140b57cec5SDimitry Andric // memory comes from the BumpPtrAllocator which is about to be purged. 2150b57cec5SDimitry Andric // 2160b57cec5SDimitry Andric // Do call MachineBasicBlock destructors, it contains std::vectors. 2170b57cec5SDimitry Andric for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) 2180b57cec5SDimitry Andric I->Insts.clearAndLeakNodesUnsafely(); 2190b57cec5SDimitry Andric MBBNumbering.clear(); 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric InstructionRecycler.clear(Allocator); 2220b57cec5SDimitry Andric OperandRecycler.clear(Allocator); 2230b57cec5SDimitry Andric BasicBlockRecycler.clear(Allocator); 2240b57cec5SDimitry Andric CodeViewAnnotations.clear(); 2250b57cec5SDimitry Andric VariableDbgInfos.clear(); 2260b57cec5SDimitry Andric if (RegInfo) { 2270b57cec5SDimitry Andric RegInfo->~MachineRegisterInfo(); 2280b57cec5SDimitry Andric Allocator.Deallocate(RegInfo); 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric if (MFInfo) { 2310b57cec5SDimitry Andric MFInfo->~MachineFunctionInfo(); 2320b57cec5SDimitry Andric Allocator.Deallocate(MFInfo); 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric FrameInfo->~MachineFrameInfo(); 2360b57cec5SDimitry Andric Allocator.Deallocate(FrameInfo); 2370b57cec5SDimitry Andric 2380b57cec5SDimitry Andric ConstantPool->~MachineConstantPool(); 2390b57cec5SDimitry Andric Allocator.Deallocate(ConstantPool); 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric if (JumpTableInfo) { 2420b57cec5SDimitry Andric JumpTableInfo->~MachineJumpTableInfo(); 2430b57cec5SDimitry Andric Allocator.Deallocate(JumpTableInfo); 2440b57cec5SDimitry Andric } 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric if (WinEHInfo) { 2470b57cec5SDimitry Andric WinEHInfo->~WinEHFuncInfo(); 2480b57cec5SDimitry Andric Allocator.Deallocate(WinEHInfo); 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric if (WasmEHInfo) { 2520b57cec5SDimitry Andric WasmEHInfo->~WasmEHFuncInfo(); 2530b57cec5SDimitry Andric Allocator.Deallocate(WasmEHInfo); 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric const DataLayout &MachineFunction::getDataLayout() const { 2580b57cec5SDimitry Andric return F.getParent()->getDataLayout(); 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric /// Get the JumpTableInfo for this function. 2620b57cec5SDimitry Andric /// If it does not already exist, allocate one. 2630b57cec5SDimitry Andric MachineJumpTableInfo *MachineFunction:: 2640b57cec5SDimitry Andric getOrCreateJumpTableInfo(unsigned EntryKind) { 2650b57cec5SDimitry Andric if (JumpTableInfo) return JumpTableInfo; 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric JumpTableInfo = new (Allocator) 2680b57cec5SDimitry Andric MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 2690b57cec5SDimitry Andric return JumpTableInfo; 2700b57cec5SDimitry Andric } 2710b57cec5SDimitry Andric 2720b57cec5SDimitry Andric /// Should we be emitting segmented stack stuff for the function 2730b57cec5SDimitry Andric bool MachineFunction::shouldSplitStack() const { 2740b57cec5SDimitry Andric return getFunction().hasFnAttribute("split-stack"); 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric LLVM_NODISCARD unsigned 2780b57cec5SDimitry Andric MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { 2790b57cec5SDimitry Andric FrameInstructions.push_back(Inst); 2800b57cec5SDimitry Andric return FrameInstructions.size() - 1; 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric /// This discards all of the MachineBasicBlock numbers and recomputes them. 2840b57cec5SDimitry Andric /// This guarantees that the MBB numbers are sequential, dense, and match the 2850b57cec5SDimitry Andric /// ordering of the blocks within the function. If a specific MachineBasicBlock 2860b57cec5SDimitry Andric /// is specified, only that block and those after it are renumbered. 2870b57cec5SDimitry Andric void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 2880b57cec5SDimitry Andric if (empty()) { MBBNumbering.clear(); return; } 2890b57cec5SDimitry Andric MachineFunction::iterator MBBI, E = end(); 2900b57cec5SDimitry Andric if (MBB == nullptr) 2910b57cec5SDimitry Andric MBBI = begin(); 2920b57cec5SDimitry Andric else 2930b57cec5SDimitry Andric MBBI = MBB->getIterator(); 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric // Figure out the block number this should have. 2960b57cec5SDimitry Andric unsigned BlockNo = 0; 2970b57cec5SDimitry Andric if (MBBI != begin()) 2980b57cec5SDimitry Andric BlockNo = std::prev(MBBI)->getNumber() + 1; 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric for (; MBBI != E; ++MBBI, ++BlockNo) { 3010b57cec5SDimitry Andric if (MBBI->getNumber() != (int)BlockNo) { 3020b57cec5SDimitry Andric // Remove use of the old number. 3030b57cec5SDimitry Andric if (MBBI->getNumber() != -1) { 3040b57cec5SDimitry Andric assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 3050b57cec5SDimitry Andric "MBB number mismatch!"); 3060b57cec5SDimitry Andric MBBNumbering[MBBI->getNumber()] = nullptr; 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric // If BlockNo is already taken, set that block's number to -1. 3100b57cec5SDimitry Andric if (MBBNumbering[BlockNo]) 3110b57cec5SDimitry Andric MBBNumbering[BlockNo]->setNumber(-1); 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric MBBNumbering[BlockNo] = &*MBBI; 3140b57cec5SDimitry Andric MBBI->setNumber(BlockNo); 3150b57cec5SDimitry Andric } 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric // Okay, all the blocks are renumbered. If we have compactified the block 3190b57cec5SDimitry Andric // numbering, shrink MBBNumbering now. 3200b57cec5SDimitry Andric assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 3210b57cec5SDimitry Andric MBBNumbering.resize(BlockNo); 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. 3250b57cec5SDimitry Andric MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 3260b57cec5SDimitry Andric const DebugLoc &DL, 3270b57cec5SDimitry Andric bool NoImp) { 3280b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 3290b57cec5SDimitry Andric MachineInstr(*this, MCID, DL, NoImp); 3300b57cec5SDimitry Andric } 3310b57cec5SDimitry Andric 3320b57cec5SDimitry Andric /// Create a new MachineInstr which is a copy of the 'Orig' instruction, 3330b57cec5SDimitry Andric /// identical in all ways except the instruction has no parent, prev, or next. 3340b57cec5SDimitry Andric MachineInstr * 3350b57cec5SDimitry Andric MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 3360b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 3370b57cec5SDimitry Andric MachineInstr(*this, *Orig); 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB, 3410b57cec5SDimitry Andric MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) { 3420b57cec5SDimitry Andric MachineInstr *FirstClone = nullptr; 3430b57cec5SDimitry Andric MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); 3440b57cec5SDimitry Andric while (true) { 3450b57cec5SDimitry Andric MachineInstr *Cloned = CloneMachineInstr(&*I); 3460b57cec5SDimitry Andric MBB.insert(InsertBefore, Cloned); 3470b57cec5SDimitry Andric if (FirstClone == nullptr) { 3480b57cec5SDimitry Andric FirstClone = Cloned; 3490b57cec5SDimitry Andric } else { 3500b57cec5SDimitry Andric Cloned->bundleWithPred(); 3510b57cec5SDimitry Andric } 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric if (!I->isBundledWithSucc()) 3540b57cec5SDimitry Andric break; 3550b57cec5SDimitry Andric ++I; 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric return *FirstClone; 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric /// Delete the given MachineInstr. 3610b57cec5SDimitry Andric /// 3620b57cec5SDimitry Andric /// This function also serves as the MachineInstr destructor - the real 3630b57cec5SDimitry Andric /// ~MachineInstr() destructor must be empty. 3640b57cec5SDimitry Andric void 3650b57cec5SDimitry Andric MachineFunction::DeleteMachineInstr(MachineInstr *MI) { 3660b57cec5SDimitry Andric // Verify that a call site info is at valid state. This assertion should 3670b57cec5SDimitry Andric // be triggered during the implementation of support for the 3680b57cec5SDimitry Andric // call site info of a new architecture. If the assertion is triggered, 3690b57cec5SDimitry Andric // back trace will tell where to insert a call to updateCallSiteInfo(). 3700b57cec5SDimitry Andric assert((!MI->isCall(MachineInstr::IgnoreBundle) || 3710b57cec5SDimitry Andric CallSitesInfo.find(MI) == CallSitesInfo.end()) && 3720b57cec5SDimitry Andric "Call site info was not updated!"); 3730b57cec5SDimitry Andric // Strip it for parts. The operand array and the MI object itself are 3740b57cec5SDimitry Andric // independently recyclable. 3750b57cec5SDimitry Andric if (MI->Operands) 3760b57cec5SDimitry Andric deallocateOperandArray(MI->CapOperands, MI->Operands); 3770b57cec5SDimitry Andric // Don't call ~MachineInstr() which must be trivial anyway because 3780b57cec5SDimitry Andric // ~MachineFunction drops whole lists of MachineInstrs wihout calling their 3790b57cec5SDimitry Andric // destructors. 3800b57cec5SDimitry Andric InstructionRecycler.Deallocate(Allocator, MI); 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric /// Allocate a new MachineBasicBlock. Use this instead of 3840b57cec5SDimitry Andric /// `new MachineBasicBlock'. 3850b57cec5SDimitry Andric MachineBasicBlock * 3860b57cec5SDimitry Andric MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 3870b57cec5SDimitry Andric return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 3880b57cec5SDimitry Andric MachineBasicBlock(*this, bb); 3890b57cec5SDimitry Andric } 3900b57cec5SDimitry Andric 3910b57cec5SDimitry Andric /// Delete the given MachineBasicBlock. 3920b57cec5SDimitry Andric void 3930b57cec5SDimitry Andric MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 3940b57cec5SDimitry Andric assert(MBB->getParent() == this && "MBB parent mismatch!"); 3950b57cec5SDimitry Andric MBB->~MachineBasicBlock(); 3960b57cec5SDimitry Andric BasicBlockRecycler.Deallocate(Allocator, MBB); 3970b57cec5SDimitry Andric } 3980b57cec5SDimitry Andric 3990b57cec5SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 4000b57cec5SDimitry Andric MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, 4010b57cec5SDimitry Andric unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 4020b57cec5SDimitry Andric SyncScope::ID SSID, AtomicOrdering Ordering, 4030b57cec5SDimitry Andric AtomicOrdering FailureOrdering) { 4040b57cec5SDimitry Andric return new (Allocator) 4050b57cec5SDimitry Andric MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, 4060b57cec5SDimitry Andric SSID, Ordering, FailureOrdering); 4070b57cec5SDimitry Andric } 4080b57cec5SDimitry Andric 4090b57cec5SDimitry Andric MachineMemOperand * 4100b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 4110b57cec5SDimitry Andric int64_t Offset, uint64_t Size) { 4120b57cec5SDimitry Andric const MachinePointerInfo &PtrInfo = MMO->getPointerInfo(); 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric // If there is no pointer value, the offset isn't tracked so we need to adjust 4150b57cec5SDimitry Andric // the base alignment. 4160b57cec5SDimitry Andric unsigned Align = PtrInfo.V.isNull() 4170b57cec5SDimitry Andric ? MinAlign(MMO->getBaseAlignment(), Offset) 4180b57cec5SDimitry Andric : MMO->getBaseAlignment(); 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric return new (Allocator) 4210b57cec5SDimitry Andric MachineMemOperand(PtrInfo.getWithOffset(Offset), MMO->getFlags(), Size, 4220b57cec5SDimitry Andric Align, AAMDNodes(), nullptr, MMO->getSyncScopeID(), 4230b57cec5SDimitry Andric MMO->getOrdering(), MMO->getFailureOrdering()); 4240b57cec5SDimitry Andric } 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric MachineMemOperand * 4270b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 4280b57cec5SDimitry Andric const AAMDNodes &AAInfo) { 4290b57cec5SDimitry Andric MachinePointerInfo MPI = MMO->getValue() ? 4300b57cec5SDimitry Andric MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : 4310b57cec5SDimitry Andric MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric return new (Allocator) 4340b57cec5SDimitry Andric MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(), 4350b57cec5SDimitry Andric MMO->getBaseAlignment(), AAInfo, 4360b57cec5SDimitry Andric MMO->getRanges(), MMO->getSyncScopeID(), 4370b57cec5SDimitry Andric MMO->getOrdering(), MMO->getFailureOrdering()); 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric MachineMemOperand * 4410b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 4420b57cec5SDimitry Andric MachineMemOperand::Flags Flags) { 4430b57cec5SDimitry Andric return new (Allocator) MachineMemOperand( 4440b57cec5SDimitry Andric MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlignment(), 4450b57cec5SDimitry Andric MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(), 4460b57cec5SDimitry Andric MMO->getOrdering(), MMO->getFailureOrdering()); 4470b57cec5SDimitry Andric } 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric MachineInstr::ExtraInfo * 4500b57cec5SDimitry Andric MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs, 4510b57cec5SDimitry Andric MCSymbol *PreInstrSymbol, 4520b57cec5SDimitry Andric MCSymbol *PostInstrSymbol) { 4530b57cec5SDimitry Andric return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, 454*c14a5a88SDimitry Andric PostInstrSymbol, nullptr); 455*c14a5a88SDimitry Andric } 456*c14a5a88SDimitry Andric 457*c14a5a88SDimitry Andric MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfoWithMarker( 458*c14a5a88SDimitry Andric ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol, 459*c14a5a88SDimitry Andric MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) { 460*c14a5a88SDimitry Andric return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, 461*c14a5a88SDimitry Andric PostInstrSymbol, HeapAllocMarker); 4620b57cec5SDimitry Andric } 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric const char *MachineFunction::createExternalSymbolName(StringRef Name) { 4650b57cec5SDimitry Andric char *Dest = Allocator.Allocate<char>(Name.size() + 1); 4660b57cec5SDimitry Andric llvm::copy(Name, Dest); 4670b57cec5SDimitry Andric Dest[Name.size()] = 0; 4680b57cec5SDimitry Andric return Dest; 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric uint32_t *MachineFunction::allocateRegMask() { 4720b57cec5SDimitry Andric unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); 4730b57cec5SDimitry Andric unsigned Size = MachineOperand::getRegMaskSize(NumRegs); 4740b57cec5SDimitry Andric uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); 4750b57cec5SDimitry Andric memset(Mask, 0, Size * sizeof(Mask[0])); 4760b57cec5SDimitry Andric return Mask; 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 4800b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineFunction::dump() const { 4810b57cec5SDimitry Andric print(dbgs()); 4820b57cec5SDimitry Andric } 4830b57cec5SDimitry Andric #endif 4840b57cec5SDimitry Andric 4850b57cec5SDimitry Andric StringRef MachineFunction::getName() const { 4860b57cec5SDimitry Andric return getFunction().getName(); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { 4900b57cec5SDimitry Andric OS << "# Machine code for function " << getName() << ": "; 4910b57cec5SDimitry Andric getProperties().print(OS); 4920b57cec5SDimitry Andric OS << '\n'; 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric // Print Frame Information 4950b57cec5SDimitry Andric FrameInfo->print(*this, OS); 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric // Print JumpTable Information 4980b57cec5SDimitry Andric if (JumpTableInfo) 4990b57cec5SDimitry Andric JumpTableInfo->print(OS); 5000b57cec5SDimitry Andric 5010b57cec5SDimitry Andric // Print Constant Pool 5020b57cec5SDimitry Andric ConstantPool->print(OS); 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric if (RegInfo && !RegInfo->livein_empty()) { 5070b57cec5SDimitry Andric OS << "Function Live Ins: "; 5080b57cec5SDimitry Andric for (MachineRegisterInfo::livein_iterator 5090b57cec5SDimitry Andric I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 5100b57cec5SDimitry Andric OS << printReg(I->first, TRI); 5110b57cec5SDimitry Andric if (I->second) 5120b57cec5SDimitry Andric OS << " in " << printReg(I->second, TRI); 5130b57cec5SDimitry Andric if (std::next(I) != E) 5140b57cec5SDimitry Andric OS << ", "; 5150b57cec5SDimitry Andric } 5160b57cec5SDimitry Andric OS << '\n'; 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric ModuleSlotTracker MST(getFunction().getParent()); 5200b57cec5SDimitry Andric MST.incorporateFunction(getFunction()); 5210b57cec5SDimitry Andric for (const auto &BB : *this) { 5220b57cec5SDimitry Andric OS << '\n'; 5230b57cec5SDimitry Andric // If we print the whole function, print it at its most verbose level. 5240b57cec5SDimitry Andric BB.print(OS, MST, Indexes, /*IsStandalone=*/true); 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric OS << "\n# End machine code for function " << getName() << ".\n\n"; 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 5300b57cec5SDimitry Andric namespace llvm { 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric template<> 5330b57cec5SDimitry Andric struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 5340b57cec5SDimitry Andric DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 5350b57cec5SDimitry Andric 5360b57cec5SDimitry Andric static std::string getGraphName(const MachineFunction *F) { 5370b57cec5SDimitry Andric return ("CFG for '" + F->getName() + "' function").str(); 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric 5400b57cec5SDimitry Andric std::string getNodeLabel(const MachineBasicBlock *Node, 5410b57cec5SDimitry Andric const MachineFunction *Graph) { 5420b57cec5SDimitry Andric std::string OutStr; 5430b57cec5SDimitry Andric { 5440b57cec5SDimitry Andric raw_string_ostream OSS(OutStr); 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andric if (isSimple()) { 5470b57cec5SDimitry Andric OSS << printMBBReference(*Node); 5480b57cec5SDimitry Andric if (const BasicBlock *BB = Node->getBasicBlock()) 5490b57cec5SDimitry Andric OSS << ": " << BB->getName(); 5500b57cec5SDimitry Andric } else 5510b57cec5SDimitry Andric Node->print(OSS); 5520b57cec5SDimitry Andric } 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 5550b57cec5SDimitry Andric 5560b57cec5SDimitry Andric // Process string output to make it nicer... 5570b57cec5SDimitry Andric for (unsigned i = 0; i != OutStr.length(); ++i) 5580b57cec5SDimitry Andric if (OutStr[i] == '\n') { // Left justify 5590b57cec5SDimitry Andric OutStr[i] = '\\'; 5600b57cec5SDimitry Andric OutStr.insert(OutStr.begin()+i+1, 'l'); 5610b57cec5SDimitry Andric } 5620b57cec5SDimitry Andric return OutStr; 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric }; 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric } // end namespace llvm 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric void MachineFunction::viewCFG() const 5690b57cec5SDimitry Andric { 5700b57cec5SDimitry Andric #ifndef NDEBUG 5710b57cec5SDimitry Andric ViewGraph(this, "mf" + getName()); 5720b57cec5SDimitry Andric #else 5730b57cec5SDimitry Andric errs() << "MachineFunction::viewCFG is only available in debug builds on " 5740b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 5750b57cec5SDimitry Andric #endif // NDEBUG 5760b57cec5SDimitry Andric } 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric void MachineFunction::viewCFGOnly() const 5790b57cec5SDimitry Andric { 5800b57cec5SDimitry Andric #ifndef NDEBUG 5810b57cec5SDimitry Andric ViewGraph(this, "mf" + getName(), true); 5820b57cec5SDimitry Andric #else 5830b57cec5SDimitry Andric errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 5840b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 5850b57cec5SDimitry Andric #endif // NDEBUG 5860b57cec5SDimitry Andric } 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric /// Add the specified physical register as a live-in value and 5890b57cec5SDimitry Andric /// create a corresponding virtual register for it. 5900b57cec5SDimitry Andric unsigned MachineFunction::addLiveIn(unsigned PReg, 5910b57cec5SDimitry Andric const TargetRegisterClass *RC) { 5920b57cec5SDimitry Andric MachineRegisterInfo &MRI = getRegInfo(); 5930b57cec5SDimitry Andric unsigned VReg = MRI.getLiveInVirtReg(PReg); 5940b57cec5SDimitry Andric if (VReg) { 5950b57cec5SDimitry Andric const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); 5960b57cec5SDimitry Andric (void)VRegRC; 5970b57cec5SDimitry Andric // A physical register can be added several times. 5980b57cec5SDimitry Andric // Between two calls, the register class of the related virtual register 5990b57cec5SDimitry Andric // may have been constrained to match some operation constraints. 6000b57cec5SDimitry Andric // In that case, check that the current register class includes the 6010b57cec5SDimitry Andric // physical register and is a sub class of the specified RC. 6020b57cec5SDimitry Andric assert((VRegRC == RC || (VRegRC->contains(PReg) && 6030b57cec5SDimitry Andric RC->hasSubClassEq(VRegRC))) && 6040b57cec5SDimitry Andric "Register class mismatch!"); 6050b57cec5SDimitry Andric return VReg; 6060b57cec5SDimitry Andric } 6070b57cec5SDimitry Andric VReg = MRI.createVirtualRegister(RC); 6080b57cec5SDimitry Andric MRI.addLiveIn(PReg, VReg); 6090b57cec5SDimitry Andric return VReg; 6100b57cec5SDimitry Andric } 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric /// Return the MCSymbol for the specified non-empty jump table. 6130b57cec5SDimitry Andric /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 6140b57cec5SDimitry Andric /// normal 'L' label is returned. 6150b57cec5SDimitry Andric MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 6160b57cec5SDimitry Andric bool isLinkerPrivate) const { 6170b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 6180b57cec5SDimitry Andric assert(JumpTableInfo && "No jump tables"); 6190b57cec5SDimitry Andric assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() 6220b57cec5SDimitry Andric : DL.getPrivateGlobalPrefix(); 6230b57cec5SDimitry Andric SmallString<60> Name; 6240b57cec5SDimitry Andric raw_svector_ostream(Name) 6250b57cec5SDimitry Andric << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 6260b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Name); 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric /// Return a function-local symbol to represent the PIC base. 6300b57cec5SDimitry Andric MCSymbol *MachineFunction::getPICBaseSymbol() const { 6310b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 6320b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 6330b57cec5SDimitry Andric Twine(getFunctionNumber()) + "$pb"); 6340b57cec5SDimitry Andric } 6350b57cec5SDimitry Andric 6360b57cec5SDimitry Andric /// \name Exception Handling 6370b57cec5SDimitry Andric /// \{ 6380b57cec5SDimitry Andric 6390b57cec5SDimitry Andric LandingPadInfo & 6400b57cec5SDimitry Andric MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { 6410b57cec5SDimitry Andric unsigned N = LandingPads.size(); 6420b57cec5SDimitry Andric for (unsigned i = 0; i < N; ++i) { 6430b57cec5SDimitry Andric LandingPadInfo &LP = LandingPads[i]; 6440b57cec5SDimitry Andric if (LP.LandingPadBlock == LandingPad) 6450b57cec5SDimitry Andric return LP; 6460b57cec5SDimitry Andric } 6470b57cec5SDimitry Andric 6480b57cec5SDimitry Andric LandingPads.push_back(LandingPadInfo(LandingPad)); 6490b57cec5SDimitry Andric return LandingPads[N]; 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, 6530b57cec5SDimitry Andric MCSymbol *BeginLabel, MCSymbol *EndLabel) { 6540b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 6550b57cec5SDimitry Andric LP.BeginLabels.push_back(BeginLabel); 6560b57cec5SDimitry Andric LP.EndLabels.push_back(EndLabel); 6570b57cec5SDimitry Andric } 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { 6600b57cec5SDimitry Andric MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); 6610b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 6620b57cec5SDimitry Andric LP.LandingPadLabel = LandingPadLabel; 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); 6650b57cec5SDimitry Andric if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { 6660b57cec5SDimitry Andric if (const auto *PF = 6670b57cec5SDimitry Andric dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts())) 6680b57cec5SDimitry Andric getMMI().addPersonality(PF); 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric if (LPI->isCleanup()) 6710b57cec5SDimitry Andric addCleanup(LandingPad); 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric // FIXME: New EH - Add the clauses in reverse order. This isn't 100% 6740b57cec5SDimitry Andric // correct, but we need to do it this way because of how the DWARF EH 6750b57cec5SDimitry Andric // emitter processes the clauses. 6760b57cec5SDimitry Andric for (unsigned I = LPI->getNumClauses(); I != 0; --I) { 6770b57cec5SDimitry Andric Value *Val = LPI->getClause(I - 1); 6780b57cec5SDimitry Andric if (LPI->isCatch(I - 1)) { 6790b57cec5SDimitry Andric addCatchTypeInfo(LandingPad, 6800b57cec5SDimitry Andric dyn_cast<GlobalValue>(Val->stripPointerCasts())); 6810b57cec5SDimitry Andric } else { 6820b57cec5SDimitry Andric // Add filters in a list. 6830b57cec5SDimitry Andric auto *CVal = cast<Constant>(Val); 6840b57cec5SDimitry Andric SmallVector<const GlobalValue *, 4> FilterList; 6850b57cec5SDimitry Andric for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end(); 6860b57cec5SDimitry Andric II != IE; ++II) 6870b57cec5SDimitry Andric FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric addFilterTypeInfo(LandingPad, FilterList); 6900b57cec5SDimitry Andric } 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { 6940b57cec5SDimitry Andric for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) { 6950b57cec5SDimitry Andric Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); 6960b57cec5SDimitry Andric addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo)); 6970b57cec5SDimitry Andric } 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric } else { 7000b57cec5SDimitry Andric assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!"); 7010b57cec5SDimitry Andric } 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric return LandingPadLabel; 7040b57cec5SDimitry Andric } 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, 7070b57cec5SDimitry Andric ArrayRef<const GlobalValue *> TyInfo) { 7080b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7090b57cec5SDimitry Andric for (unsigned N = TyInfo.size(); N; --N) 7100b57cec5SDimitry Andric LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 7110b57cec5SDimitry Andric } 7120b57cec5SDimitry Andric 7130b57cec5SDimitry Andric void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, 7140b57cec5SDimitry Andric ArrayRef<const GlobalValue *> TyInfo) { 7150b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7160b57cec5SDimitry Andric std::vector<unsigned> IdsInFilter(TyInfo.size()); 7170b57cec5SDimitry Andric for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 7180b57cec5SDimitry Andric IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 7190b57cec5SDimitry Andric LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap, 7230b57cec5SDimitry Andric bool TidyIfNoBeginLabels) { 7240b57cec5SDimitry Andric for (unsigned i = 0; i != LandingPads.size(); ) { 7250b57cec5SDimitry Andric LandingPadInfo &LandingPad = LandingPads[i]; 7260b57cec5SDimitry Andric if (LandingPad.LandingPadLabel && 7270b57cec5SDimitry Andric !LandingPad.LandingPadLabel->isDefined() && 7280b57cec5SDimitry Andric (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) 7290b57cec5SDimitry Andric LandingPad.LandingPadLabel = nullptr; 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric // Special case: we *should* emit LPs with null LP MBB. This indicates 7320b57cec5SDimitry Andric // "nounwind" case. 7330b57cec5SDimitry Andric if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 7340b57cec5SDimitry Andric LandingPads.erase(LandingPads.begin() + i); 7350b57cec5SDimitry Andric continue; 7360b57cec5SDimitry Andric } 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric if (TidyIfNoBeginLabels) { 7390b57cec5SDimitry Andric for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { 7400b57cec5SDimitry Andric MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; 7410b57cec5SDimitry Andric MCSymbol *EndLabel = LandingPad.EndLabels[j]; 7420b57cec5SDimitry Andric if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && 7430b57cec5SDimitry Andric (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) 7440b57cec5SDimitry Andric continue; 7450b57cec5SDimitry Andric 7460b57cec5SDimitry Andric LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 7470b57cec5SDimitry Andric LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 7480b57cec5SDimitry Andric --j; 7490b57cec5SDimitry Andric --e; 7500b57cec5SDimitry Andric } 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric // Remove landing pads with no try-ranges. 7530b57cec5SDimitry Andric if (LandingPads[i].BeginLabels.empty()) { 7540b57cec5SDimitry Andric LandingPads.erase(LandingPads.begin() + i); 7550b57cec5SDimitry Andric continue; 7560b57cec5SDimitry Andric } 7570b57cec5SDimitry Andric } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric // If there is no landing pad, ensure that the list of typeids is empty. 7600b57cec5SDimitry Andric // If the only typeid is a cleanup, this is the same as having no typeids. 7610b57cec5SDimitry Andric if (!LandingPad.LandingPadBlock || 7620b57cec5SDimitry Andric (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 7630b57cec5SDimitry Andric LandingPad.TypeIds.clear(); 7640b57cec5SDimitry Andric ++i; 7650b57cec5SDimitry Andric } 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { 7690b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7700b57cec5SDimitry Andric LP.TypeIds.push_back(0); 7710b57cec5SDimitry Andric } 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad, 7740b57cec5SDimitry Andric const Function *Filter, 7750b57cec5SDimitry Andric const BlockAddress *RecoverBA) { 7760b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7770b57cec5SDimitry Andric SEHHandler Handler; 7780b57cec5SDimitry Andric Handler.FilterOrFinally = Filter; 7790b57cec5SDimitry Andric Handler.RecoverBA = RecoverBA; 7800b57cec5SDimitry Andric LP.SEHHandlers.push_back(Handler); 7810b57cec5SDimitry Andric } 7820b57cec5SDimitry Andric 7830b57cec5SDimitry Andric void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad, 7840b57cec5SDimitry Andric const Function *Cleanup) { 7850b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7860b57cec5SDimitry Andric SEHHandler Handler; 7870b57cec5SDimitry Andric Handler.FilterOrFinally = Cleanup; 7880b57cec5SDimitry Andric Handler.RecoverBA = nullptr; 7890b57cec5SDimitry Andric LP.SEHHandlers.push_back(Handler); 7900b57cec5SDimitry Andric } 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, 7930b57cec5SDimitry Andric ArrayRef<unsigned> Sites) { 7940b57cec5SDimitry Andric LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); 7950b57cec5SDimitry Andric } 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { 7980b57cec5SDimitry Andric for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 7990b57cec5SDimitry Andric if (TypeInfos[i] == TI) return i + 1; 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric TypeInfos.push_back(TI); 8020b57cec5SDimitry Andric return TypeInfos.size(); 8030b57cec5SDimitry Andric } 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) { 8060b57cec5SDimitry Andric // If the new filter coincides with the tail of an existing filter, then 8070b57cec5SDimitry Andric // re-use the existing filter. Folding filters more than this requires 8080b57cec5SDimitry Andric // re-ordering filters and/or their elements - probably not worth it. 8090b57cec5SDimitry Andric for (std::vector<unsigned>::iterator I = FilterEnds.begin(), 8100b57cec5SDimitry Andric E = FilterEnds.end(); I != E; ++I) { 8110b57cec5SDimitry Andric unsigned i = *I, j = TyIds.size(); 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric while (i && j) 8140b57cec5SDimitry Andric if (FilterIds[--i] != TyIds[--j]) 8150b57cec5SDimitry Andric goto try_next; 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric if (!j) 8180b57cec5SDimitry Andric // The new filter coincides with range [i, end) of the existing filter. 8190b57cec5SDimitry Andric return -(1 + i); 8200b57cec5SDimitry Andric 8210b57cec5SDimitry Andric try_next:; 8220b57cec5SDimitry Andric } 8230b57cec5SDimitry Andric 8240b57cec5SDimitry Andric // Add the new filter. 8250b57cec5SDimitry Andric int FilterID = -(1 + FilterIds.size()); 8260b57cec5SDimitry Andric FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 8270b57cec5SDimitry Andric FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end()); 8280b57cec5SDimitry Andric FilterEnds.push_back(FilterIds.size()); 8290b57cec5SDimitry Andric FilterIds.push_back(0); // terminator 8300b57cec5SDimitry Andric return FilterID; 8310b57cec5SDimitry Andric } 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric void MachineFunction::addCodeViewHeapAllocSite(MachineInstr *I, MDNode *MD) { 8340b57cec5SDimitry Andric MCSymbol *BeginLabel = Ctx.createTempSymbol("heapallocsite", true); 8350b57cec5SDimitry Andric MCSymbol *EndLabel = Ctx.createTempSymbol("heapallocsite", true); 8360b57cec5SDimitry Andric I->setPreInstrSymbol(*this, BeginLabel); 8370b57cec5SDimitry Andric I->setPostInstrSymbol(*this, EndLabel); 8380b57cec5SDimitry Andric 8390b57cec5SDimitry Andric DIType *DI = dyn_cast<DIType>(MD); 8400b57cec5SDimitry Andric CodeViewHeapAllocSites.push_back(std::make_tuple(BeginLabel, EndLabel, DI)); 8410b57cec5SDimitry Andric } 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric void MachineFunction::updateCallSiteInfo(const MachineInstr *Old, 8440b57cec5SDimitry Andric const MachineInstr *New) { 8450b57cec5SDimitry Andric if (!Target.Options.EnableDebugEntryValues || Old == New) 8460b57cec5SDimitry Andric return; 8470b57cec5SDimitry Andric 8480b57cec5SDimitry Andric assert(Old->isCall() && (!New || New->isCall()) && 8490b57cec5SDimitry Andric "Call site info referes only to call instructions!"); 8500b57cec5SDimitry Andric CallSiteInfoMap::iterator CSIt = CallSitesInfo.find(Old); 8510b57cec5SDimitry Andric if (CSIt == CallSitesInfo.end()) 8520b57cec5SDimitry Andric return; 8530b57cec5SDimitry Andric CallSiteInfo CSInfo = std::move(CSIt->second); 8540b57cec5SDimitry Andric CallSitesInfo.erase(CSIt); 8550b57cec5SDimitry Andric if (New) 8560b57cec5SDimitry Andric CallSitesInfo[New] = CSInfo; 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric /// \} 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8620b57cec5SDimitry Andric // MachineJumpTableInfo implementation 8630b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric /// Return the size of each entry in the jump table. 8660b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { 8670b57cec5SDimitry Andric // The size of a jump table entry is 4 bytes unless the entry is just the 8680b57cec5SDimitry Andric // address of a block, in which case it is the pointer size. 8690b57cec5SDimitry Andric switch (getEntryKind()) { 8700b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 8710b57cec5SDimitry Andric return TD.getPointerSize(); 8720b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 8730b57cec5SDimitry Andric return 8; 8740b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 8750b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 8760b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 8770b57cec5SDimitry Andric return 4; 8780b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 8790b57cec5SDimitry Andric return 0; 8800b57cec5SDimitry Andric } 8810b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 8820b57cec5SDimitry Andric } 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric /// Return the alignment of each entry in the jump table. 8850b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { 8860b57cec5SDimitry Andric // The alignment of a jump table entry is the alignment of int32 unless the 8870b57cec5SDimitry Andric // entry is just the address of a block, in which case it is the pointer 8880b57cec5SDimitry Andric // alignment. 8890b57cec5SDimitry Andric switch (getEntryKind()) { 8900b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 8910b57cec5SDimitry Andric return TD.getPointerABIAlignment(0); 8920b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 8930b57cec5SDimitry Andric return TD.getABIIntegerTypeAlignment(64); 8940b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 8950b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 8960b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 8970b57cec5SDimitry Andric return TD.getABIIntegerTypeAlignment(32); 8980b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 8990b57cec5SDimitry Andric return 1; 9000b57cec5SDimitry Andric } 9010b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 9020b57cec5SDimitry Andric } 9030b57cec5SDimitry Andric 9040b57cec5SDimitry Andric /// Create a new jump table entry in the jump table info. 9050b57cec5SDimitry Andric unsigned MachineJumpTableInfo::createJumpTableIndex( 9060b57cec5SDimitry Andric const std::vector<MachineBasicBlock*> &DestBBs) { 9070b57cec5SDimitry Andric assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 9080b57cec5SDimitry Andric JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 9090b57cec5SDimitry Andric return JumpTables.size()-1; 9100b57cec5SDimitry Andric } 9110b57cec5SDimitry Andric 9120b57cec5SDimitry Andric /// If Old is the target of any jump tables, update the jump tables to branch 9130b57cec5SDimitry Andric /// to New instead. 9140b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 9150b57cec5SDimitry Andric MachineBasicBlock *New) { 9160b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 9170b57cec5SDimitry Andric bool MadeChange = false; 9180b57cec5SDimitry Andric for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 9190b57cec5SDimitry Andric ReplaceMBBInJumpTable(i, Old, New); 9200b57cec5SDimitry Andric return MadeChange; 9210b57cec5SDimitry Andric } 9220b57cec5SDimitry Andric 9230b57cec5SDimitry Andric /// If Old is a target of the jump tables, update the jump table to branch to 9240b57cec5SDimitry Andric /// New instead. 9250b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 9260b57cec5SDimitry Andric MachineBasicBlock *Old, 9270b57cec5SDimitry Andric MachineBasicBlock *New) { 9280b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 9290b57cec5SDimitry Andric bool MadeChange = false; 9300b57cec5SDimitry Andric MachineJumpTableEntry &JTE = JumpTables[Idx]; 9310b57cec5SDimitry Andric for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 9320b57cec5SDimitry Andric if (JTE.MBBs[j] == Old) { 9330b57cec5SDimitry Andric JTE.MBBs[j] = New; 9340b57cec5SDimitry Andric MadeChange = true; 9350b57cec5SDimitry Andric } 9360b57cec5SDimitry Andric return MadeChange; 9370b57cec5SDimitry Andric } 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric void MachineJumpTableInfo::print(raw_ostream &OS) const { 9400b57cec5SDimitry Andric if (JumpTables.empty()) return; 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric OS << "Jump Tables:\n"; 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 9450b57cec5SDimitry Andric OS << printJumpTableEntryReference(i) << ':'; 9460b57cec5SDimitry Andric for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 9470b57cec5SDimitry Andric OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]); 9480b57cec5SDimitry Andric if (i != e) 9490b57cec5SDimitry Andric OS << '\n'; 9500b57cec5SDimitry Andric } 9510b57cec5SDimitry Andric 9520b57cec5SDimitry Andric OS << '\n'; 9530b57cec5SDimitry Andric } 9540b57cec5SDimitry Andric 9550b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 9560b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } 9570b57cec5SDimitry Andric #endif 9580b57cec5SDimitry Andric 9590b57cec5SDimitry Andric Printable llvm::printJumpTableEntryReference(unsigned Idx) { 9600b57cec5SDimitry Andric return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); 9610b57cec5SDimitry Andric } 9620b57cec5SDimitry Andric 9630b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9640b57cec5SDimitry Andric // MachineConstantPool implementation 9650b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric void MachineConstantPoolValue::anchor() {} 9680b57cec5SDimitry Andric 9690b57cec5SDimitry Andric Type *MachineConstantPoolEntry::getType() const { 9700b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 9710b57cec5SDimitry Andric return Val.MachineCPVal->getType(); 9720b57cec5SDimitry Andric return Val.ConstVal->getType(); 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric bool MachineConstantPoolEntry::needsRelocation() const { 9760b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 9770b57cec5SDimitry Andric return true; 9780b57cec5SDimitry Andric return Val.ConstVal->needsRelocation(); 9790b57cec5SDimitry Andric } 9800b57cec5SDimitry Andric 9810b57cec5SDimitry Andric SectionKind 9820b57cec5SDimitry Andric MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { 9830b57cec5SDimitry Andric if (needsRelocation()) 9840b57cec5SDimitry Andric return SectionKind::getReadOnlyWithRel(); 9850b57cec5SDimitry Andric switch (DL->getTypeAllocSize(getType())) { 9860b57cec5SDimitry Andric case 4: 9870b57cec5SDimitry Andric return SectionKind::getMergeableConst4(); 9880b57cec5SDimitry Andric case 8: 9890b57cec5SDimitry Andric return SectionKind::getMergeableConst8(); 9900b57cec5SDimitry Andric case 16: 9910b57cec5SDimitry Andric return SectionKind::getMergeableConst16(); 9920b57cec5SDimitry Andric case 32: 9930b57cec5SDimitry Andric return SectionKind::getMergeableConst32(); 9940b57cec5SDimitry Andric default: 9950b57cec5SDimitry Andric return SectionKind::getReadOnly(); 9960b57cec5SDimitry Andric } 9970b57cec5SDimitry Andric } 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric MachineConstantPool::~MachineConstantPool() { 10000b57cec5SDimitry Andric // A constant may be a member of both Constants and MachineCPVsSharingEntries, 10010b57cec5SDimitry Andric // so keep track of which we've deleted to avoid double deletions. 10020b57cec5SDimitry Andric DenseSet<MachineConstantPoolValue*> Deleted; 10030b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) 10040b57cec5SDimitry Andric if (Constants[i].isMachineConstantPoolEntry()) { 10050b57cec5SDimitry Andric Deleted.insert(Constants[i].Val.MachineCPVal); 10060b57cec5SDimitry Andric delete Constants[i].Val.MachineCPVal; 10070b57cec5SDimitry Andric } 10080b57cec5SDimitry Andric for (DenseSet<MachineConstantPoolValue*>::iterator I = 10090b57cec5SDimitry Andric MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end(); 10100b57cec5SDimitry Andric I != E; ++I) { 10110b57cec5SDimitry Andric if (Deleted.count(*I) == 0) 10120b57cec5SDimitry Andric delete *I; 10130b57cec5SDimitry Andric } 10140b57cec5SDimitry Andric } 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric /// Test whether the given two constants can be allocated the same constant pool 10170b57cec5SDimitry Andric /// entry. 10180b57cec5SDimitry Andric static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 10190b57cec5SDimitry Andric const DataLayout &DL) { 10200b57cec5SDimitry Andric // Handle the trivial case quickly. 10210b57cec5SDimitry Andric if (A == B) return true; 10220b57cec5SDimitry Andric 10230b57cec5SDimitry Andric // If they have the same type but weren't the same constant, quickly 10240b57cec5SDimitry Andric // reject them. 10250b57cec5SDimitry Andric if (A->getType() == B->getType()) return false; 10260b57cec5SDimitry Andric 10270b57cec5SDimitry Andric // We can't handle structs or arrays. 10280b57cec5SDimitry Andric if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || 10290b57cec5SDimitry Andric isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) 10300b57cec5SDimitry Andric return false; 10310b57cec5SDimitry Andric 10320b57cec5SDimitry Andric // For now, only support constants with the same size. 10330b57cec5SDimitry Andric uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); 10340b57cec5SDimitry Andric if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) 10350b57cec5SDimitry Andric return false; 10360b57cec5SDimitry Andric 10370b57cec5SDimitry Andric Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); 10380b57cec5SDimitry Andric 10390b57cec5SDimitry Andric // Try constant folding a bitcast of both instructions to an integer. If we 10400b57cec5SDimitry Andric // get two identical ConstantInt's, then we are good to share them. We use 10410b57cec5SDimitry Andric // the constant folding APIs to do this so that we get the benefit of 10420b57cec5SDimitry Andric // DataLayout. 10430b57cec5SDimitry Andric if (isa<PointerType>(A->getType())) 10440b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::PtrToInt, 10450b57cec5SDimitry Andric const_cast<Constant *>(A), IntTy, DL); 10460b57cec5SDimitry Andric else if (A->getType() != IntTy) 10470b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), 10480b57cec5SDimitry Andric IntTy, DL); 10490b57cec5SDimitry Andric if (isa<PointerType>(B->getType())) 10500b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::PtrToInt, 10510b57cec5SDimitry Andric const_cast<Constant *>(B), IntTy, DL); 10520b57cec5SDimitry Andric else if (B->getType() != IntTy) 10530b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), 10540b57cec5SDimitry Andric IntTy, DL); 10550b57cec5SDimitry Andric 10560b57cec5SDimitry Andric return A == B; 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric /// Create a new entry in the constant pool or return an existing one. 10600b57cec5SDimitry Andric /// User must specify the log2 of the minimum required alignment for the object. 10610b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 10620b57cec5SDimitry Andric unsigned Alignment) { 10630b57cec5SDimitry Andric assert(Alignment && "Alignment must be specified!"); 10640b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 10650b57cec5SDimitry Andric 10660b57cec5SDimitry Andric // Check to see if we already have this constant. 10670b57cec5SDimitry Andric // 10680b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 10690b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) 10700b57cec5SDimitry Andric if (!Constants[i].isMachineConstantPoolEntry() && 10710b57cec5SDimitry Andric CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { 10720b57cec5SDimitry Andric if ((unsigned)Constants[i].getAlignment() < Alignment) 10730b57cec5SDimitry Andric Constants[i].Alignment = Alignment; 10740b57cec5SDimitry Andric return i; 10750b57cec5SDimitry Andric } 10760b57cec5SDimitry Andric 10770b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 10780b57cec5SDimitry Andric return Constants.size()-1; 10790b57cec5SDimitry Andric } 10800b57cec5SDimitry Andric 10810b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 10820b57cec5SDimitry Andric unsigned Alignment) { 10830b57cec5SDimitry Andric assert(Alignment && "Alignment must be specified!"); 10840b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 10850b57cec5SDimitry Andric 10860b57cec5SDimitry Andric // Check to see if we already have this constant. 10870b57cec5SDimitry Andric // 10880b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 10890b57cec5SDimitry Andric int Idx = V->getExistingMachineCPValue(this, Alignment); 10900b57cec5SDimitry Andric if (Idx != -1) { 10910b57cec5SDimitry Andric MachineCPVsSharingEntries.insert(V); 10920b57cec5SDimitry Andric return (unsigned)Idx; 10930b57cec5SDimitry Andric } 10940b57cec5SDimitry Andric 10950b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 10960b57cec5SDimitry Andric return Constants.size()-1; 10970b57cec5SDimitry Andric } 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andric void MachineConstantPool::print(raw_ostream &OS) const { 11000b57cec5SDimitry Andric if (Constants.empty()) return; 11010b57cec5SDimitry Andric 11020b57cec5SDimitry Andric OS << "Constant Pool:\n"; 11030b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 11040b57cec5SDimitry Andric OS << " cp#" << i << ": "; 11050b57cec5SDimitry Andric if (Constants[i].isMachineConstantPoolEntry()) 11060b57cec5SDimitry Andric Constants[i].Val.MachineCPVal->print(OS); 11070b57cec5SDimitry Andric else 11080b57cec5SDimitry Andric Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); 11090b57cec5SDimitry Andric OS << ", align=" << Constants[i].getAlignment(); 11100b57cec5SDimitry Andric OS << "\n"; 11110b57cec5SDimitry Andric } 11120b57cec5SDimitry Andric } 11130b57cec5SDimitry Andric 11140b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 11150b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } 11160b57cec5SDimitry Andric #endif 1117