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" 365ffd83dbSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 370b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 380b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 390b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 400b57cec5SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h" 410b57cec5SDimitry Andric #include "llvm/CodeGen/WinEHFuncInfo.h" 420b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 430b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 440b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 450b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 460b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 470b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 480b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 490b57cec5SDimitry Andric #include "llvm/IR/Function.h" 500b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 510b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 520b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 530b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 540b57cec5SDimitry Andric #include "llvm/IR/Module.h" 550b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h" 560b57cec5SDimitry Andric #include "llvm/IR/Value.h" 570b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 580b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 590b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h" 600b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 610b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 620b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 630b57cec5SDimitry Andric #include "llvm/Support/DOTGraphTraits.h" 640b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 650b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 660b57cec5SDimitry Andric #include "llvm/Support/GraphWriter.h" 670b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 680b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 690b57cec5SDimitry Andric #include <algorithm> 700b57cec5SDimitry Andric #include <cassert> 710b57cec5SDimitry Andric #include <cstddef> 720b57cec5SDimitry Andric #include <cstdint> 730b57cec5SDimitry Andric #include <iterator> 740b57cec5SDimitry Andric #include <string> 755ffd83dbSDimitry Andric #include <type_traits> 760b57cec5SDimitry Andric #include <utility> 770b57cec5SDimitry Andric #include <vector> 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric using namespace llvm; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric #define DEBUG_TYPE "codegen" 820b57cec5SDimitry Andric 838bcb0991SDimitry Andric static cl::opt<unsigned> AlignAllFunctions( 848bcb0991SDimitry Andric "align-all-functions", 858bcb0991SDimitry Andric cl::desc("Force the alignment of all functions in log2 format (e.g. 4 " 868bcb0991SDimitry Andric "means align on 16B boundaries)."), 870b57cec5SDimitry Andric cl::init(0), cl::Hidden); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric static const char *getPropertyName(MachineFunctionProperties::Property Prop) { 900b57cec5SDimitry Andric using P = MachineFunctionProperties::Property; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric switch(Prop) { 930b57cec5SDimitry Andric case P::FailedISel: return "FailedISel"; 940b57cec5SDimitry Andric case P::IsSSA: return "IsSSA"; 950b57cec5SDimitry Andric case P::Legalized: return "Legalized"; 960b57cec5SDimitry Andric case P::NoPHIs: return "NoPHIs"; 970b57cec5SDimitry Andric case P::NoVRegs: return "NoVRegs"; 980b57cec5SDimitry Andric case P::RegBankSelected: return "RegBankSelected"; 990b57cec5SDimitry Andric case P::Selected: return "Selected"; 1000b57cec5SDimitry Andric case P::TracksLiveness: return "TracksLiveness"; 1015ffd83dbSDimitry Andric case P::TiedOpsRewritten: return "TiedOpsRewritten"; 102*349cc55cSDimitry Andric case P::FailsVerification: return "FailsVerification"; 1030b57cec5SDimitry Andric } 1040b57cec5SDimitry Andric llvm_unreachable("Invalid machine function property"); 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // Pin the vtable to this file. 1080b57cec5SDimitry Andric void MachineFunction::Delegate::anchor() {} 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric void MachineFunctionProperties::print(raw_ostream &OS) const { 1110b57cec5SDimitry Andric const char *Separator = ""; 1120b57cec5SDimitry Andric for (BitVector::size_type I = 0; I < Properties.size(); ++I) { 1130b57cec5SDimitry Andric if (!Properties[I]) 1140b57cec5SDimitry Andric continue; 1150b57cec5SDimitry Andric OS << Separator << getPropertyName(static_cast<Property>(I)); 1160b57cec5SDimitry Andric Separator = ", "; 1170b57cec5SDimitry Andric } 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1210b57cec5SDimitry Andric // MachineFunction implementation 1220b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric // Out-of-line virtual method. 1250b57cec5SDimitry Andric MachineFunctionInfo::~MachineFunctionInfo() = default; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 1280b57cec5SDimitry Andric MBB->getParent()->DeleteMachineBasicBlock(MBB); 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI, 1320b57cec5SDimitry Andric const Function &F) { 133*349cc55cSDimitry Andric if (auto MA = F.getFnStackAlign()) 134*349cc55cSDimitry Andric return MA->value(); 1355ffd83dbSDimitry Andric return STI->getFrameLowering()->getStackAlign().value(); 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric 1385ffd83dbSDimitry Andric MachineFunction::MachineFunction(Function &F, const LLVMTargetMachine &Target, 1390b57cec5SDimitry Andric const TargetSubtargetInfo &STI, 1400b57cec5SDimitry Andric unsigned FunctionNum, MachineModuleInfo &mmi) 1410b57cec5SDimitry Andric : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) { 1420b57cec5SDimitry Andric FunctionNumber = FunctionNum; 1430b57cec5SDimitry Andric init(); 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric void MachineFunction::handleInsertion(MachineInstr &MI) { 1470b57cec5SDimitry Andric if (TheDelegate) 1480b57cec5SDimitry Andric TheDelegate->MF_HandleInsertion(MI); 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric void MachineFunction::handleRemoval(MachineInstr &MI) { 1520b57cec5SDimitry Andric if (TheDelegate) 1530b57cec5SDimitry Andric TheDelegate->MF_HandleRemoval(MI); 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric void MachineFunction::init() { 1570b57cec5SDimitry Andric // Assume the function starts in SSA form with correct liveness. 1580b57cec5SDimitry Andric Properties.set(MachineFunctionProperties::Property::IsSSA); 1590b57cec5SDimitry Andric Properties.set(MachineFunctionProperties::Property::TracksLiveness); 1600b57cec5SDimitry Andric if (STI->getRegisterInfo()) 1610b57cec5SDimitry Andric RegInfo = new (Allocator) MachineRegisterInfo(this); 1620b57cec5SDimitry Andric else 1630b57cec5SDimitry Andric RegInfo = nullptr; 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric MFInfo = nullptr; 1660b57cec5SDimitry Andric // We can realign the stack if the target supports it and the user hasn't 1670b57cec5SDimitry Andric // explicitly asked us not to. 1680b57cec5SDimitry Andric bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && 1690b57cec5SDimitry Andric !F.hasFnAttribute("no-realign-stack"); 1700b57cec5SDimitry Andric FrameInfo = new (Allocator) MachineFrameInfo( 1710b57cec5SDimitry Andric getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, 1720b57cec5SDimitry Andric /*ForcedRealign=*/CanRealignSP && 1730b57cec5SDimitry Andric F.hasFnAttribute(Attribute::StackAlignment)); 1740b57cec5SDimitry Andric 1750b57cec5SDimitry Andric if (F.hasFnAttribute(Attribute::StackAlignment)) 1765ffd83dbSDimitry Andric FrameInfo->ensureMaxAlignment(*F.getFnStackAlign()); 1770b57cec5SDimitry Andric 1780b57cec5SDimitry Andric ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); 1790b57cec5SDimitry Andric Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. 1820b57cec5SDimitry Andric // FIXME: Use Function::hasOptSize(). 1830b57cec5SDimitry Andric if (!F.hasFnAttribute(Attribute::OptimizeForSize)) 1840b57cec5SDimitry Andric Alignment = std::max(Alignment, 1850b57cec5SDimitry Andric STI->getTargetLowering()->getPrefFunctionAlignment()); 1860b57cec5SDimitry Andric 1870b57cec5SDimitry Andric if (AlignAllFunctions) 1888bcb0991SDimitry Andric Alignment = Align(1ULL << AlignAllFunctions); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric JumpTableInfo = nullptr; 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric if (isFuncletEHPersonality(classifyEHPersonality( 1930b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 1940b57cec5SDimitry Andric WinEHInfo = new (Allocator) WinEHFuncInfo(); 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric if (isScopedEHPersonality(classifyEHPersonality( 1980b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 1990b57cec5SDimitry Andric WasmEHInfo = new (Allocator) WasmEHFuncInfo(); 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric assert(Target.isCompatibleDataLayout(getDataLayout()) && 2030b57cec5SDimitry Andric "Can't create a MachineFunction using a Module with a " 2040b57cec5SDimitry Andric "Target-incompatible DataLayout attached\n"); 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric PSVManager = 2078bcb0991SDimitry Andric std::make_unique<PseudoSourceValueManager>(*(getSubtarget(). 2080b57cec5SDimitry Andric getInstrInfo())); 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric MachineFunction::~MachineFunction() { 2120b57cec5SDimitry Andric clear(); 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric void MachineFunction::clear() { 2160b57cec5SDimitry Andric Properties.reset(); 2170b57cec5SDimitry Andric // Don't call destructors on MachineInstr and MachineOperand. All of their 2180b57cec5SDimitry Andric // memory comes from the BumpPtrAllocator which is about to be purged. 2190b57cec5SDimitry Andric // 2200b57cec5SDimitry Andric // Do call MachineBasicBlock destructors, it contains std::vectors. 2210b57cec5SDimitry Andric for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) 2220b57cec5SDimitry Andric I->Insts.clearAndLeakNodesUnsafely(); 2230b57cec5SDimitry Andric MBBNumbering.clear(); 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric InstructionRecycler.clear(Allocator); 2260b57cec5SDimitry Andric OperandRecycler.clear(Allocator); 2270b57cec5SDimitry Andric BasicBlockRecycler.clear(Allocator); 2280b57cec5SDimitry Andric CodeViewAnnotations.clear(); 2290b57cec5SDimitry Andric VariableDbgInfos.clear(); 2300b57cec5SDimitry Andric if (RegInfo) { 2310b57cec5SDimitry Andric RegInfo->~MachineRegisterInfo(); 2320b57cec5SDimitry Andric Allocator.Deallocate(RegInfo); 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric if (MFInfo) { 2350b57cec5SDimitry Andric MFInfo->~MachineFunctionInfo(); 2360b57cec5SDimitry Andric Allocator.Deallocate(MFInfo); 2370b57cec5SDimitry Andric } 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric FrameInfo->~MachineFrameInfo(); 2400b57cec5SDimitry Andric Allocator.Deallocate(FrameInfo); 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric ConstantPool->~MachineConstantPool(); 2430b57cec5SDimitry Andric Allocator.Deallocate(ConstantPool); 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric if (JumpTableInfo) { 2460b57cec5SDimitry Andric JumpTableInfo->~MachineJumpTableInfo(); 2470b57cec5SDimitry Andric Allocator.Deallocate(JumpTableInfo); 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric if (WinEHInfo) { 2510b57cec5SDimitry Andric WinEHInfo->~WinEHFuncInfo(); 2520b57cec5SDimitry Andric Allocator.Deallocate(WinEHInfo); 2530b57cec5SDimitry Andric } 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric if (WasmEHInfo) { 2560b57cec5SDimitry Andric WasmEHInfo->~WasmEHFuncInfo(); 2570b57cec5SDimitry Andric Allocator.Deallocate(WasmEHInfo); 2580b57cec5SDimitry Andric } 2590b57cec5SDimitry Andric } 2600b57cec5SDimitry Andric 2610b57cec5SDimitry Andric const DataLayout &MachineFunction::getDataLayout() const { 2620b57cec5SDimitry Andric return F.getParent()->getDataLayout(); 2630b57cec5SDimitry Andric } 2640b57cec5SDimitry Andric 2650b57cec5SDimitry Andric /// Get the JumpTableInfo for this function. 2660b57cec5SDimitry Andric /// If it does not already exist, allocate one. 2670b57cec5SDimitry Andric MachineJumpTableInfo *MachineFunction:: 2680b57cec5SDimitry Andric getOrCreateJumpTableInfo(unsigned EntryKind) { 2690b57cec5SDimitry Andric if (JumpTableInfo) return JumpTableInfo; 2700b57cec5SDimitry Andric 2710b57cec5SDimitry Andric JumpTableInfo = new (Allocator) 2720b57cec5SDimitry Andric MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 2730b57cec5SDimitry Andric return JumpTableInfo; 2740b57cec5SDimitry Andric } 2750b57cec5SDimitry Andric 276480093f4SDimitry Andric DenormalMode MachineFunction::getDenormalMode(const fltSemantics &FPType) const { 277e8d8bef9SDimitry Andric return F.getDenormalMode(FPType); 278480093f4SDimitry Andric } 279480093f4SDimitry Andric 2800b57cec5SDimitry Andric /// Should we be emitting segmented stack stuff for the function 2810b57cec5SDimitry Andric bool MachineFunction::shouldSplitStack() const { 2820b57cec5SDimitry Andric return getFunction().hasFnAttribute("split-stack"); 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric LLVM_NODISCARD unsigned 2860b57cec5SDimitry Andric MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { 2870b57cec5SDimitry Andric FrameInstructions.push_back(Inst); 2880b57cec5SDimitry Andric return FrameInstructions.size() - 1; 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric /// This discards all of the MachineBasicBlock numbers and recomputes them. 2920b57cec5SDimitry Andric /// This guarantees that the MBB numbers are sequential, dense, and match the 2930b57cec5SDimitry Andric /// ordering of the blocks within the function. If a specific MachineBasicBlock 2940b57cec5SDimitry Andric /// is specified, only that block and those after it are renumbered. 2950b57cec5SDimitry Andric void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 2960b57cec5SDimitry Andric if (empty()) { MBBNumbering.clear(); return; } 2970b57cec5SDimitry Andric MachineFunction::iterator MBBI, E = end(); 2980b57cec5SDimitry Andric if (MBB == nullptr) 2990b57cec5SDimitry Andric MBBI = begin(); 3000b57cec5SDimitry Andric else 3010b57cec5SDimitry Andric MBBI = MBB->getIterator(); 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric // Figure out the block number this should have. 3040b57cec5SDimitry Andric unsigned BlockNo = 0; 3050b57cec5SDimitry Andric if (MBBI != begin()) 3060b57cec5SDimitry Andric BlockNo = std::prev(MBBI)->getNumber() + 1; 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric for (; MBBI != E; ++MBBI, ++BlockNo) { 3090b57cec5SDimitry Andric if (MBBI->getNumber() != (int)BlockNo) { 3100b57cec5SDimitry Andric // Remove use of the old number. 3110b57cec5SDimitry Andric if (MBBI->getNumber() != -1) { 3120b57cec5SDimitry Andric assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 3130b57cec5SDimitry Andric "MBB number mismatch!"); 3140b57cec5SDimitry Andric MBBNumbering[MBBI->getNumber()] = nullptr; 3150b57cec5SDimitry Andric } 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric // If BlockNo is already taken, set that block's number to -1. 3180b57cec5SDimitry Andric if (MBBNumbering[BlockNo]) 3190b57cec5SDimitry Andric MBBNumbering[BlockNo]->setNumber(-1); 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric MBBNumbering[BlockNo] = &*MBBI; 3220b57cec5SDimitry Andric MBBI->setNumber(BlockNo); 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric // Okay, all the blocks are renumbered. If we have compactified the block 3270b57cec5SDimitry Andric // numbering, shrink MBBNumbering now. 3280b57cec5SDimitry Andric assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 3290b57cec5SDimitry Andric MBBNumbering.resize(BlockNo); 3300b57cec5SDimitry Andric } 3310b57cec5SDimitry Andric 3325ffd83dbSDimitry Andric /// This method iterates over the basic blocks and assigns their IsBeginSection 3335ffd83dbSDimitry Andric /// and IsEndSection fields. This must be called after MBB layout is finalized 3345ffd83dbSDimitry Andric /// and the SectionID's are assigned to MBBs. 3355ffd83dbSDimitry Andric void MachineFunction::assignBeginEndSections() { 3365ffd83dbSDimitry Andric front().setIsBeginSection(); 3375ffd83dbSDimitry Andric auto CurrentSectionID = front().getSectionID(); 3385ffd83dbSDimitry Andric for (auto MBBI = std::next(begin()), E = end(); MBBI != E; ++MBBI) { 3395ffd83dbSDimitry Andric if (MBBI->getSectionID() == CurrentSectionID) 3405ffd83dbSDimitry Andric continue; 3415ffd83dbSDimitry Andric MBBI->setIsBeginSection(); 3425ffd83dbSDimitry Andric std::prev(MBBI)->setIsEndSection(); 3435ffd83dbSDimitry Andric CurrentSectionID = MBBI->getSectionID(); 3445ffd83dbSDimitry Andric } 3455ffd83dbSDimitry Andric back().setIsEndSection(); 3465ffd83dbSDimitry Andric } 3475ffd83dbSDimitry Andric 3480b57cec5SDimitry Andric /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. 3490b57cec5SDimitry Andric MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 3500b57cec5SDimitry Andric const DebugLoc &DL, 351e8d8bef9SDimitry Andric bool NoImplicit) { 3520b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 353e8d8bef9SDimitry Andric MachineInstr(*this, MCID, DL, NoImplicit); 3540b57cec5SDimitry Andric } 3550b57cec5SDimitry Andric 3560b57cec5SDimitry Andric /// Create a new MachineInstr which is a copy of the 'Orig' instruction, 3570b57cec5SDimitry Andric /// identical in all ways except the instruction has no parent, prev, or next. 3580b57cec5SDimitry Andric MachineInstr * 3590b57cec5SDimitry Andric MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 3600b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 3610b57cec5SDimitry Andric MachineInstr(*this, *Orig); 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB, 3650b57cec5SDimitry Andric MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) { 3660b57cec5SDimitry Andric MachineInstr *FirstClone = nullptr; 3670b57cec5SDimitry Andric MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); 3680b57cec5SDimitry Andric while (true) { 3690b57cec5SDimitry Andric MachineInstr *Cloned = CloneMachineInstr(&*I); 3700b57cec5SDimitry Andric MBB.insert(InsertBefore, Cloned); 3710b57cec5SDimitry Andric if (FirstClone == nullptr) { 3720b57cec5SDimitry Andric FirstClone = Cloned; 3730b57cec5SDimitry Andric } else { 3740b57cec5SDimitry Andric Cloned->bundleWithPred(); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric if (!I->isBundledWithSucc()) 3780b57cec5SDimitry Andric break; 3790b57cec5SDimitry Andric ++I; 3800b57cec5SDimitry Andric } 3815ffd83dbSDimitry Andric // Copy over call site info to the cloned instruction if needed. If Orig is in 3825ffd83dbSDimitry Andric // a bundle, copyCallSiteInfo takes care of finding the call instruction in 3835ffd83dbSDimitry Andric // the bundle. 3845ffd83dbSDimitry Andric if (Orig.shouldUpdateCallSiteInfo()) 3855ffd83dbSDimitry Andric copyCallSiteInfo(&Orig, FirstClone); 3860b57cec5SDimitry Andric return *FirstClone; 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric /// Delete the given MachineInstr. 3900b57cec5SDimitry Andric /// 3910b57cec5SDimitry Andric /// This function also serves as the MachineInstr destructor - the real 3920b57cec5SDimitry Andric /// ~MachineInstr() destructor must be empty. 3930b57cec5SDimitry Andric void 3940b57cec5SDimitry Andric MachineFunction::DeleteMachineInstr(MachineInstr *MI) { 3950b57cec5SDimitry Andric // Verify that a call site info is at valid state. This assertion should 3960b57cec5SDimitry Andric // be triggered during the implementation of support for the 3970b57cec5SDimitry Andric // call site info of a new architecture. If the assertion is triggered, 3980b57cec5SDimitry Andric // back trace will tell where to insert a call to updateCallSiteInfo(). 3995ffd83dbSDimitry Andric assert((!MI->isCandidateForCallSiteEntry() || 4000b57cec5SDimitry Andric CallSitesInfo.find(MI) == CallSitesInfo.end()) && 4010b57cec5SDimitry Andric "Call site info was not updated!"); 4020b57cec5SDimitry Andric // Strip it for parts. The operand array and the MI object itself are 4030b57cec5SDimitry Andric // independently recyclable. 4040b57cec5SDimitry Andric if (MI->Operands) 4050b57cec5SDimitry Andric deallocateOperandArray(MI->CapOperands, MI->Operands); 4060b57cec5SDimitry Andric // Don't call ~MachineInstr() which must be trivial anyway because 4070b57cec5SDimitry Andric // ~MachineFunction drops whole lists of MachineInstrs wihout calling their 4080b57cec5SDimitry Andric // destructors. 4090b57cec5SDimitry Andric InstructionRecycler.Deallocate(Allocator, MI); 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric 4120b57cec5SDimitry Andric /// Allocate a new MachineBasicBlock. Use this instead of 4130b57cec5SDimitry Andric /// `new MachineBasicBlock'. 4140b57cec5SDimitry Andric MachineBasicBlock * 4150b57cec5SDimitry Andric MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 4160b57cec5SDimitry Andric return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 4170b57cec5SDimitry Andric MachineBasicBlock(*this, bb); 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric /// Delete the given MachineBasicBlock. 4210b57cec5SDimitry Andric void 4220b57cec5SDimitry Andric MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 4230b57cec5SDimitry Andric assert(MBB->getParent() == this && "MBB parent mismatch!"); 424e8d8bef9SDimitry Andric // Clean up any references to MBB in jump tables before deleting it. 425e8d8bef9SDimitry Andric if (JumpTableInfo) 426e8d8bef9SDimitry Andric JumpTableInfo->RemoveMBBFromJumpTables(MBB); 4270b57cec5SDimitry Andric MBB->~MachineBasicBlock(); 4280b57cec5SDimitry Andric BasicBlockRecycler.Deallocate(Allocator, MBB); 4290b57cec5SDimitry Andric } 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 4320b57cec5SDimitry Andric MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, 4335ffd83dbSDimitry Andric Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 4340b57cec5SDimitry Andric SyncScope::ID SSID, AtomicOrdering Ordering, 4350b57cec5SDimitry Andric AtomicOrdering FailureOrdering) { 4360b57cec5SDimitry Andric return new (Allocator) 4370b57cec5SDimitry Andric MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, 4380b57cec5SDimitry Andric SSID, Ordering, FailureOrdering); 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 441e8d8bef9SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 442fe6060f1SDimitry Andric MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, 443fe6060f1SDimitry Andric Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 444fe6060f1SDimitry Andric SyncScope::ID SSID, AtomicOrdering Ordering, 445fe6060f1SDimitry Andric AtomicOrdering FailureOrdering) { 446fe6060f1SDimitry Andric return new (Allocator) 447fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, f, MemTy, base_alignment, AAInfo, Ranges, SSID, 448fe6060f1SDimitry Andric Ordering, FailureOrdering); 449fe6060f1SDimitry Andric } 450fe6060f1SDimitry Andric 451fe6060f1SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 452fe6060f1SDimitry Andric const MachineMemOperand *MMO, const MachinePointerInfo &PtrInfo, uint64_t Size) { 453fe6060f1SDimitry Andric return new (Allocator) 454fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, MMO->getFlags(), Size, MMO->getBaseAlign(), 455fe6060f1SDimitry Andric AAMDNodes(), nullptr, MMO->getSyncScopeID(), 456fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 457fe6060f1SDimitry Andric } 458fe6060f1SDimitry Andric 459fe6060f1SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 460fe6060f1SDimitry Andric const MachineMemOperand *MMO, const MachinePointerInfo &PtrInfo, LLT Ty) { 461fe6060f1SDimitry Andric return new (Allocator) 462fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, MMO->getFlags(), Ty, MMO->getBaseAlign(), 463fe6060f1SDimitry Andric AAMDNodes(), nullptr, MMO->getSyncScopeID(), 464fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 465e8d8bef9SDimitry Andric } 466e8d8bef9SDimitry Andric 4670b57cec5SDimitry Andric MachineMemOperand * 4680b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 469fe6060f1SDimitry Andric int64_t Offset, LLT Ty) { 4700b57cec5SDimitry Andric const MachinePointerInfo &PtrInfo = MMO->getPointerInfo(); 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric // If there is no pointer value, the offset isn't tracked so we need to adjust 4730b57cec5SDimitry Andric // the base alignment. 4745ffd83dbSDimitry Andric Align Alignment = PtrInfo.V.isNull() 4755ffd83dbSDimitry Andric ? commonAlignment(MMO->getBaseAlign(), Offset) 4765ffd83dbSDimitry Andric : MMO->getBaseAlign(); 4770b57cec5SDimitry Andric 478e8d8bef9SDimitry Andric // Do not preserve ranges, since we don't necessarily know what the high bits 479e8d8bef9SDimitry Andric // are anymore. 480fe6060f1SDimitry Andric return new (Allocator) MachineMemOperand( 481fe6060f1SDimitry Andric PtrInfo.getWithOffset(Offset), MMO->getFlags(), Ty, Alignment, 482fe6060f1SDimitry Andric MMO->getAAInfo(), nullptr, MMO->getSyncScopeID(), 483fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric MachineMemOperand * 4870b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 4880b57cec5SDimitry Andric const AAMDNodes &AAInfo) { 4890b57cec5SDimitry Andric MachinePointerInfo MPI = MMO->getValue() ? 4900b57cec5SDimitry Andric MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : 4910b57cec5SDimitry Andric MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); 4920b57cec5SDimitry Andric 4935ffd83dbSDimitry Andric return new (Allocator) MachineMemOperand( 4945ffd83dbSDimitry Andric MPI, MMO->getFlags(), MMO->getSize(), MMO->getBaseAlign(), AAInfo, 495fe6060f1SDimitry Andric MMO->getRanges(), MMO->getSyncScopeID(), MMO->getSuccessOrdering(), 4965ffd83dbSDimitry Andric MMO->getFailureOrdering()); 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric MachineMemOperand * 5000b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 5010b57cec5SDimitry Andric MachineMemOperand::Flags Flags) { 5020b57cec5SDimitry Andric return new (Allocator) MachineMemOperand( 5035ffd83dbSDimitry Andric MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlign(), 5040b57cec5SDimitry Andric MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(), 505fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 5060b57cec5SDimitry Andric } 5070b57cec5SDimitry Andric 508480093f4SDimitry Andric MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfo( 509c14a5a88SDimitry Andric ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol, 510c14a5a88SDimitry Andric MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) { 511c14a5a88SDimitry Andric return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, 512c14a5a88SDimitry Andric PostInstrSymbol, HeapAllocMarker); 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric const char *MachineFunction::createExternalSymbolName(StringRef Name) { 5160b57cec5SDimitry Andric char *Dest = Allocator.Allocate<char>(Name.size() + 1); 5170b57cec5SDimitry Andric llvm::copy(Name, Dest); 5180b57cec5SDimitry Andric Dest[Name.size()] = 0; 5190b57cec5SDimitry Andric return Dest; 5200b57cec5SDimitry Andric } 5210b57cec5SDimitry Andric 5220b57cec5SDimitry Andric uint32_t *MachineFunction::allocateRegMask() { 5230b57cec5SDimitry Andric unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); 5240b57cec5SDimitry Andric unsigned Size = MachineOperand::getRegMaskSize(NumRegs); 5250b57cec5SDimitry Andric uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); 5260b57cec5SDimitry Andric memset(Mask, 0, Size * sizeof(Mask[0])); 5270b57cec5SDimitry Andric return Mask; 5280b57cec5SDimitry Andric } 5290b57cec5SDimitry Andric 530480093f4SDimitry Andric ArrayRef<int> MachineFunction::allocateShuffleMask(ArrayRef<int> Mask) { 531480093f4SDimitry Andric int* AllocMask = Allocator.Allocate<int>(Mask.size()); 532480093f4SDimitry Andric copy(Mask, AllocMask); 533480093f4SDimitry Andric return {AllocMask, Mask.size()}; 534480093f4SDimitry Andric } 535480093f4SDimitry Andric 5360b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 5370b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineFunction::dump() const { 5380b57cec5SDimitry Andric print(dbgs()); 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric #endif 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric StringRef MachineFunction::getName() const { 5430b57cec5SDimitry Andric return getFunction().getName(); 5440b57cec5SDimitry Andric } 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andric void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { 5470b57cec5SDimitry Andric OS << "# Machine code for function " << getName() << ": "; 5480b57cec5SDimitry Andric getProperties().print(OS); 5490b57cec5SDimitry Andric OS << '\n'; 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric // Print Frame Information 5520b57cec5SDimitry Andric FrameInfo->print(*this, OS); 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric // Print JumpTable Information 5550b57cec5SDimitry Andric if (JumpTableInfo) 5560b57cec5SDimitry Andric JumpTableInfo->print(OS); 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric // Print Constant Pool 5590b57cec5SDimitry Andric ConstantPool->print(OS); 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); 5620b57cec5SDimitry Andric 5630b57cec5SDimitry Andric if (RegInfo && !RegInfo->livein_empty()) { 5640b57cec5SDimitry Andric OS << "Function Live Ins: "; 5650b57cec5SDimitry Andric for (MachineRegisterInfo::livein_iterator 5660b57cec5SDimitry Andric I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 5670b57cec5SDimitry Andric OS << printReg(I->first, TRI); 5680b57cec5SDimitry Andric if (I->second) 5690b57cec5SDimitry Andric OS << " in " << printReg(I->second, TRI); 5700b57cec5SDimitry Andric if (std::next(I) != E) 5710b57cec5SDimitry Andric OS << ", "; 5720b57cec5SDimitry Andric } 5730b57cec5SDimitry Andric OS << '\n'; 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric ModuleSlotTracker MST(getFunction().getParent()); 5770b57cec5SDimitry Andric MST.incorporateFunction(getFunction()); 5780b57cec5SDimitry Andric for (const auto &BB : *this) { 5790b57cec5SDimitry Andric OS << '\n'; 5800b57cec5SDimitry Andric // If we print the whole function, print it at its most verbose level. 5810b57cec5SDimitry Andric BB.print(OS, MST, Indexes, /*IsStandalone=*/true); 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric 5840b57cec5SDimitry Andric OS << "\n# End machine code for function " << getName() << ".\n\n"; 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric 587480093f4SDimitry Andric /// True if this function needs frame moves for debug or exceptions. 588480093f4SDimitry Andric bool MachineFunction::needsFrameMoves() const { 589480093f4SDimitry Andric return getMMI().hasDebugInfo() || 590480093f4SDimitry Andric getTarget().Options.ForceDwarfFrameSection || 591480093f4SDimitry Andric F.needsUnwindTableEntry(); 592480093f4SDimitry Andric } 593480093f4SDimitry Andric 5940b57cec5SDimitry Andric namespace llvm { 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric template<> 5970b57cec5SDimitry Andric struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 5980b57cec5SDimitry Andric DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric static std::string getGraphName(const MachineFunction *F) { 6010b57cec5SDimitry Andric return ("CFG for '" + F->getName() + "' function").str(); 6020b57cec5SDimitry Andric } 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric std::string getNodeLabel(const MachineBasicBlock *Node, 6050b57cec5SDimitry Andric const MachineFunction *Graph) { 6060b57cec5SDimitry Andric std::string OutStr; 6070b57cec5SDimitry Andric { 6080b57cec5SDimitry Andric raw_string_ostream OSS(OutStr); 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andric if (isSimple()) { 6110b57cec5SDimitry Andric OSS << printMBBReference(*Node); 6120b57cec5SDimitry Andric if (const BasicBlock *BB = Node->getBasicBlock()) 6130b57cec5SDimitry Andric OSS << ": " << BB->getName(); 6140b57cec5SDimitry Andric } else 6150b57cec5SDimitry Andric Node->print(OSS); 6160b57cec5SDimitry Andric } 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric // Process string output to make it nicer... 6210b57cec5SDimitry Andric for (unsigned i = 0; i != OutStr.length(); ++i) 6220b57cec5SDimitry Andric if (OutStr[i] == '\n') { // Left justify 6230b57cec5SDimitry Andric OutStr[i] = '\\'; 6240b57cec5SDimitry Andric OutStr.insert(OutStr.begin()+i+1, 'l'); 6250b57cec5SDimitry Andric } 6260b57cec5SDimitry Andric return OutStr; 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric }; 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric } // end namespace llvm 6310b57cec5SDimitry Andric 6320b57cec5SDimitry Andric void MachineFunction::viewCFG() const 6330b57cec5SDimitry Andric { 6340b57cec5SDimitry Andric #ifndef NDEBUG 6350b57cec5SDimitry Andric ViewGraph(this, "mf" + getName()); 6360b57cec5SDimitry Andric #else 6370b57cec5SDimitry Andric errs() << "MachineFunction::viewCFG is only available in debug builds on " 6380b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 6390b57cec5SDimitry Andric #endif // NDEBUG 6400b57cec5SDimitry Andric } 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric void MachineFunction::viewCFGOnly() const 6430b57cec5SDimitry Andric { 6440b57cec5SDimitry Andric #ifndef NDEBUG 6450b57cec5SDimitry Andric ViewGraph(this, "mf" + getName(), true); 6460b57cec5SDimitry Andric #else 6470b57cec5SDimitry Andric errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 6480b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 6490b57cec5SDimitry Andric #endif // NDEBUG 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric /// Add the specified physical register as a live-in value and 6530b57cec5SDimitry Andric /// create a corresponding virtual register for it. 6545ffd83dbSDimitry Andric Register MachineFunction::addLiveIn(MCRegister PReg, 6550b57cec5SDimitry Andric const TargetRegisterClass *RC) { 6560b57cec5SDimitry Andric MachineRegisterInfo &MRI = getRegInfo(); 6575ffd83dbSDimitry Andric Register VReg = MRI.getLiveInVirtReg(PReg); 6580b57cec5SDimitry Andric if (VReg) { 6590b57cec5SDimitry Andric const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); 6600b57cec5SDimitry Andric (void)VRegRC; 6610b57cec5SDimitry Andric // A physical register can be added several times. 6620b57cec5SDimitry Andric // Between two calls, the register class of the related virtual register 6630b57cec5SDimitry Andric // may have been constrained to match some operation constraints. 6640b57cec5SDimitry Andric // In that case, check that the current register class includes the 6650b57cec5SDimitry Andric // physical register and is a sub class of the specified RC. 6660b57cec5SDimitry Andric assert((VRegRC == RC || (VRegRC->contains(PReg) && 6670b57cec5SDimitry Andric RC->hasSubClassEq(VRegRC))) && 6680b57cec5SDimitry Andric "Register class mismatch!"); 6690b57cec5SDimitry Andric return VReg; 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric VReg = MRI.createVirtualRegister(RC); 6720b57cec5SDimitry Andric MRI.addLiveIn(PReg, VReg); 6730b57cec5SDimitry Andric return VReg; 6740b57cec5SDimitry Andric } 6750b57cec5SDimitry Andric 6760b57cec5SDimitry Andric /// Return the MCSymbol for the specified non-empty jump table. 6770b57cec5SDimitry Andric /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 6780b57cec5SDimitry Andric /// normal 'L' label is returned. 6790b57cec5SDimitry Andric MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 6800b57cec5SDimitry Andric bool isLinkerPrivate) const { 6810b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 6820b57cec5SDimitry Andric assert(JumpTableInfo && "No jump tables"); 6830b57cec5SDimitry Andric assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() 6860b57cec5SDimitry Andric : DL.getPrivateGlobalPrefix(); 6870b57cec5SDimitry Andric SmallString<60> Name; 6880b57cec5SDimitry Andric raw_svector_ostream(Name) 6890b57cec5SDimitry Andric << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 6900b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Name); 6910b57cec5SDimitry Andric } 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric /// Return a function-local symbol to represent the PIC base. 6940b57cec5SDimitry Andric MCSymbol *MachineFunction::getPICBaseSymbol() const { 6950b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 6960b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 6970b57cec5SDimitry Andric Twine(getFunctionNumber()) + "$pb"); 6980b57cec5SDimitry Andric } 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric /// \name Exception Handling 7010b57cec5SDimitry Andric /// \{ 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric LandingPadInfo & 7040b57cec5SDimitry Andric MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { 7050b57cec5SDimitry Andric unsigned N = LandingPads.size(); 7060b57cec5SDimitry Andric for (unsigned i = 0; i < N; ++i) { 7070b57cec5SDimitry Andric LandingPadInfo &LP = LandingPads[i]; 7080b57cec5SDimitry Andric if (LP.LandingPadBlock == LandingPad) 7090b57cec5SDimitry Andric return LP; 7100b57cec5SDimitry Andric } 7110b57cec5SDimitry Andric 7120b57cec5SDimitry Andric LandingPads.push_back(LandingPadInfo(LandingPad)); 7130b57cec5SDimitry Andric return LandingPads[N]; 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, 7170b57cec5SDimitry Andric MCSymbol *BeginLabel, MCSymbol *EndLabel) { 7180b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7190b57cec5SDimitry Andric LP.BeginLabels.push_back(BeginLabel); 7200b57cec5SDimitry Andric LP.EndLabels.push_back(EndLabel); 7210b57cec5SDimitry Andric } 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { 7240b57cec5SDimitry Andric MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); 7250b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7260b57cec5SDimitry Andric LP.LandingPadLabel = LandingPadLabel; 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); 7290b57cec5SDimitry Andric if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { 7300b57cec5SDimitry Andric if (const auto *PF = 7310b57cec5SDimitry Andric dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts())) 7320b57cec5SDimitry Andric getMMI().addPersonality(PF); 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric if (LPI->isCleanup()) 7350b57cec5SDimitry Andric addCleanup(LandingPad); 7360b57cec5SDimitry Andric 7370b57cec5SDimitry Andric // FIXME: New EH - Add the clauses in reverse order. This isn't 100% 7380b57cec5SDimitry Andric // correct, but we need to do it this way because of how the DWARF EH 7390b57cec5SDimitry Andric // emitter processes the clauses. 7400b57cec5SDimitry Andric for (unsigned I = LPI->getNumClauses(); I != 0; --I) { 7410b57cec5SDimitry Andric Value *Val = LPI->getClause(I - 1); 7420b57cec5SDimitry Andric if (LPI->isCatch(I - 1)) { 7430b57cec5SDimitry Andric addCatchTypeInfo(LandingPad, 7440b57cec5SDimitry Andric dyn_cast<GlobalValue>(Val->stripPointerCasts())); 7450b57cec5SDimitry Andric } else { 7460b57cec5SDimitry Andric // Add filters in a list. 7470b57cec5SDimitry Andric auto *CVal = cast<Constant>(Val); 7480b57cec5SDimitry Andric SmallVector<const GlobalValue *, 4> FilterList; 749*349cc55cSDimitry Andric for (const Use &U : CVal->operands()) 750*349cc55cSDimitry Andric FilterList.push_back(cast<GlobalValue>(U->stripPointerCasts())); 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric addFilterTypeInfo(LandingPad, FilterList); 7530b57cec5SDimitry Andric } 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { 7570b57cec5SDimitry Andric for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) { 7580b57cec5SDimitry Andric Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); 7590b57cec5SDimitry Andric addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo)); 7600b57cec5SDimitry Andric } 7610b57cec5SDimitry Andric 7620b57cec5SDimitry Andric } else { 7630b57cec5SDimitry Andric assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!"); 7640b57cec5SDimitry Andric } 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric return LandingPadLabel; 7670b57cec5SDimitry Andric } 7680b57cec5SDimitry Andric 7690b57cec5SDimitry Andric void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, 7700b57cec5SDimitry Andric ArrayRef<const GlobalValue *> TyInfo) { 7710b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7720b57cec5SDimitry Andric for (unsigned N = TyInfo.size(); N; --N) 7730b57cec5SDimitry Andric LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 7740b57cec5SDimitry Andric } 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, 7770b57cec5SDimitry Andric ArrayRef<const GlobalValue *> TyInfo) { 7780b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7790b57cec5SDimitry Andric std::vector<unsigned> IdsInFilter(TyInfo.size()); 7800b57cec5SDimitry Andric for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 7810b57cec5SDimitry Andric IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 7820b57cec5SDimitry Andric LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 7830b57cec5SDimitry Andric } 7840b57cec5SDimitry Andric 7850b57cec5SDimitry Andric void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap, 7860b57cec5SDimitry Andric bool TidyIfNoBeginLabels) { 7870b57cec5SDimitry Andric for (unsigned i = 0; i != LandingPads.size(); ) { 7880b57cec5SDimitry Andric LandingPadInfo &LandingPad = LandingPads[i]; 7890b57cec5SDimitry Andric if (LandingPad.LandingPadLabel && 7900b57cec5SDimitry Andric !LandingPad.LandingPadLabel->isDefined() && 7910b57cec5SDimitry Andric (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) 7920b57cec5SDimitry Andric LandingPad.LandingPadLabel = nullptr; 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric // Special case: we *should* emit LPs with null LP MBB. This indicates 7950b57cec5SDimitry Andric // "nounwind" case. 7960b57cec5SDimitry Andric if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 7970b57cec5SDimitry Andric LandingPads.erase(LandingPads.begin() + i); 7980b57cec5SDimitry Andric continue; 7990b57cec5SDimitry Andric } 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric if (TidyIfNoBeginLabels) { 8020b57cec5SDimitry Andric for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { 8030b57cec5SDimitry Andric MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; 8040b57cec5SDimitry Andric MCSymbol *EndLabel = LandingPad.EndLabels[j]; 8050b57cec5SDimitry Andric if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && 8060b57cec5SDimitry Andric (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) 8070b57cec5SDimitry Andric continue; 8080b57cec5SDimitry Andric 8090b57cec5SDimitry Andric LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 8100b57cec5SDimitry Andric LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 8110b57cec5SDimitry Andric --j; 8120b57cec5SDimitry Andric --e; 8130b57cec5SDimitry Andric } 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric // Remove landing pads with no try-ranges. 8160b57cec5SDimitry Andric if (LandingPads[i].BeginLabels.empty()) { 8170b57cec5SDimitry Andric LandingPads.erase(LandingPads.begin() + i); 8180b57cec5SDimitry Andric continue; 8190b57cec5SDimitry Andric } 8200b57cec5SDimitry Andric } 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric // If there is no landing pad, ensure that the list of typeids is empty. 8230b57cec5SDimitry Andric // If the only typeid is a cleanup, this is the same as having no typeids. 8240b57cec5SDimitry Andric if (!LandingPad.LandingPadBlock || 8250b57cec5SDimitry Andric (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 8260b57cec5SDimitry Andric LandingPad.TypeIds.clear(); 8270b57cec5SDimitry Andric ++i; 8280b57cec5SDimitry Andric } 8290b57cec5SDimitry Andric } 8300b57cec5SDimitry Andric 8310b57cec5SDimitry Andric void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { 8320b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 8330b57cec5SDimitry Andric LP.TypeIds.push_back(0); 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad, 8370b57cec5SDimitry Andric const Function *Filter, 8380b57cec5SDimitry Andric const BlockAddress *RecoverBA) { 8390b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 8400b57cec5SDimitry Andric SEHHandler Handler; 8410b57cec5SDimitry Andric Handler.FilterOrFinally = Filter; 8420b57cec5SDimitry Andric Handler.RecoverBA = RecoverBA; 8430b57cec5SDimitry Andric LP.SEHHandlers.push_back(Handler); 8440b57cec5SDimitry Andric } 8450b57cec5SDimitry Andric 8460b57cec5SDimitry Andric void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad, 8470b57cec5SDimitry Andric const Function *Cleanup) { 8480b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 8490b57cec5SDimitry Andric SEHHandler Handler; 8500b57cec5SDimitry Andric Handler.FilterOrFinally = Cleanup; 8510b57cec5SDimitry Andric Handler.RecoverBA = nullptr; 8520b57cec5SDimitry Andric LP.SEHHandlers.push_back(Handler); 8530b57cec5SDimitry Andric } 8540b57cec5SDimitry Andric 8550b57cec5SDimitry Andric void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, 8560b57cec5SDimitry Andric ArrayRef<unsigned> Sites) { 8570b57cec5SDimitry Andric LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); 8580b57cec5SDimitry Andric } 8590b57cec5SDimitry Andric 8600b57cec5SDimitry Andric unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { 8610b57cec5SDimitry Andric for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 8620b57cec5SDimitry Andric if (TypeInfos[i] == TI) return i + 1; 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric TypeInfos.push_back(TI); 8650b57cec5SDimitry Andric return TypeInfos.size(); 8660b57cec5SDimitry Andric } 8670b57cec5SDimitry Andric 8680b57cec5SDimitry Andric int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) { 8690b57cec5SDimitry Andric // If the new filter coincides with the tail of an existing filter, then 8700b57cec5SDimitry Andric // re-use the existing filter. Folding filters more than this requires 8710b57cec5SDimitry Andric // re-ordering filters and/or their elements - probably not worth it. 872fe6060f1SDimitry Andric for (unsigned i : FilterEnds) { 873fe6060f1SDimitry Andric unsigned j = TyIds.size(); 8740b57cec5SDimitry Andric 8750b57cec5SDimitry Andric while (i && j) 8760b57cec5SDimitry Andric if (FilterIds[--i] != TyIds[--j]) 8770b57cec5SDimitry Andric goto try_next; 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric if (!j) 8800b57cec5SDimitry Andric // The new filter coincides with range [i, end) of the existing filter. 8810b57cec5SDimitry Andric return -(1 + i); 8820b57cec5SDimitry Andric 8830b57cec5SDimitry Andric try_next:; 8840b57cec5SDimitry Andric } 8850b57cec5SDimitry Andric 8860b57cec5SDimitry Andric // Add the new filter. 8870b57cec5SDimitry Andric int FilterID = -(1 + FilterIds.size()); 8880b57cec5SDimitry Andric FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 889e8d8bef9SDimitry Andric llvm::append_range(FilterIds, TyIds); 8900b57cec5SDimitry Andric FilterEnds.push_back(FilterIds.size()); 8910b57cec5SDimitry Andric FilterIds.push_back(0); // terminator 8920b57cec5SDimitry Andric return FilterID; 8930b57cec5SDimitry Andric } 8940b57cec5SDimitry Andric 895480093f4SDimitry Andric MachineFunction::CallSiteInfoMap::iterator 896480093f4SDimitry Andric MachineFunction::getCallSiteInfo(const MachineInstr *MI) { 8975ffd83dbSDimitry Andric assert(MI->isCandidateForCallSiteEntry() && 8985ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates"); 8990b57cec5SDimitry Andric 9005ffd83dbSDimitry Andric if (!Target.Options.EmitCallSiteInfo) 901480093f4SDimitry Andric return CallSitesInfo.end(); 902480093f4SDimitry Andric return CallSitesInfo.find(MI); 9030b57cec5SDimitry Andric } 9040b57cec5SDimitry Andric 9055ffd83dbSDimitry Andric /// Return the call machine instruction or find a call within bundle. 9065ffd83dbSDimitry Andric static const MachineInstr *getCallInstr(const MachineInstr *MI) { 9075ffd83dbSDimitry Andric if (!MI->isBundle()) 9085ffd83dbSDimitry Andric return MI; 9090b57cec5SDimitry Andric 9105ffd83dbSDimitry Andric for (auto &BMI : make_range(getBundleStart(MI->getIterator()), 9115ffd83dbSDimitry Andric getBundleEnd(MI->getIterator()))) 9125ffd83dbSDimitry Andric if (BMI.isCandidateForCallSiteEntry()) 9135ffd83dbSDimitry Andric return &BMI; 9148bcb0991SDimitry Andric 9155ffd83dbSDimitry Andric llvm_unreachable("Unexpected bundle without a call site candidate"); 9168bcb0991SDimitry Andric } 9178bcb0991SDimitry Andric 9188bcb0991SDimitry Andric void MachineFunction::eraseCallSiteInfo(const MachineInstr *MI) { 9195ffd83dbSDimitry Andric assert(MI->shouldUpdateCallSiteInfo() && 9205ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9215ffd83dbSDimitry Andric "candidates inside bundles"); 9225ffd83dbSDimitry Andric 9235ffd83dbSDimitry Andric const MachineInstr *CallMI = getCallInstr(MI); 9245ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(CallMI); 9258bcb0991SDimitry Andric if (CSIt == CallSitesInfo.end()) 9268bcb0991SDimitry Andric return; 9278bcb0991SDimitry Andric CallSitesInfo.erase(CSIt); 9288bcb0991SDimitry Andric } 9298bcb0991SDimitry Andric 9308bcb0991SDimitry Andric void MachineFunction::copyCallSiteInfo(const MachineInstr *Old, 9318bcb0991SDimitry Andric const MachineInstr *New) { 9325ffd83dbSDimitry Andric assert(Old->shouldUpdateCallSiteInfo() && 9335ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9345ffd83dbSDimitry Andric "candidates inside bundles"); 9358bcb0991SDimitry Andric 9365ffd83dbSDimitry Andric if (!New->isCandidateForCallSiteEntry()) 9375ffd83dbSDimitry Andric return eraseCallSiteInfo(Old); 9385ffd83dbSDimitry Andric 9395ffd83dbSDimitry Andric const MachineInstr *OldCallMI = getCallInstr(Old); 9405ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 9418bcb0991SDimitry Andric if (CSIt == CallSitesInfo.end()) 9428bcb0991SDimitry Andric return; 9438bcb0991SDimitry Andric 9448bcb0991SDimitry Andric CallSiteInfo CSInfo = CSIt->second; 9450b57cec5SDimitry Andric CallSitesInfo[New] = CSInfo; 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric 9485ffd83dbSDimitry Andric void MachineFunction::moveCallSiteInfo(const MachineInstr *Old, 9495ffd83dbSDimitry Andric const MachineInstr *New) { 9505ffd83dbSDimitry Andric assert(Old->shouldUpdateCallSiteInfo() && 9515ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9525ffd83dbSDimitry Andric "candidates inside bundles"); 9535ffd83dbSDimitry Andric 9545ffd83dbSDimitry Andric if (!New->isCandidateForCallSiteEntry()) 9555ffd83dbSDimitry Andric return eraseCallSiteInfo(Old); 9565ffd83dbSDimitry Andric 9575ffd83dbSDimitry Andric const MachineInstr *OldCallMI = getCallInstr(Old); 9585ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 9595ffd83dbSDimitry Andric if (CSIt == CallSitesInfo.end()) 9605ffd83dbSDimitry Andric return; 9615ffd83dbSDimitry Andric 9625ffd83dbSDimitry Andric CallSiteInfo CSInfo = std::move(CSIt->second); 9635ffd83dbSDimitry Andric CallSitesInfo.erase(CSIt); 9645ffd83dbSDimitry Andric CallSitesInfo[New] = CSInfo; 9655ffd83dbSDimitry Andric } 9665ffd83dbSDimitry Andric 967e8d8bef9SDimitry Andric void MachineFunction::setDebugInstrNumberingCount(unsigned Num) { 968e8d8bef9SDimitry Andric DebugInstrNumberingCount = Num; 969e8d8bef9SDimitry Andric } 970e8d8bef9SDimitry Andric 971e8d8bef9SDimitry Andric void MachineFunction::makeDebugValueSubstitution(DebugInstrOperandPair A, 972fe6060f1SDimitry Andric DebugInstrOperandPair B, 973fe6060f1SDimitry Andric unsigned Subreg) { 974fe6060f1SDimitry Andric // Catch any accidental self-loops. 975fe6060f1SDimitry Andric assert(A.first != B.first); 976*349cc55cSDimitry Andric // Don't allow any substitutions _from_ the memory operand number. 977*349cc55cSDimitry Andric assert(A.second != DebugOperandMemNumber); 978*349cc55cSDimitry Andric 979fe6060f1SDimitry Andric DebugValueSubstitutions.push_back({A, B, Subreg}); 980e8d8bef9SDimitry Andric } 981e8d8bef9SDimitry Andric 982e8d8bef9SDimitry Andric void MachineFunction::substituteDebugValuesForInst(const MachineInstr &Old, 983e8d8bef9SDimitry Andric MachineInstr &New, 984e8d8bef9SDimitry Andric unsigned MaxOperand) { 985e8d8bef9SDimitry Andric // If the Old instruction wasn't tracked at all, there is no work to do. 986e8d8bef9SDimitry Andric unsigned OldInstrNum = Old.peekDebugInstrNum(); 987e8d8bef9SDimitry Andric if (!OldInstrNum) 988e8d8bef9SDimitry Andric return; 989e8d8bef9SDimitry Andric 990e8d8bef9SDimitry Andric // Iterate over all operands looking for defs to create substitutions for. 991e8d8bef9SDimitry Andric // Avoid creating new instr numbers unless we create a new substitution. 992e8d8bef9SDimitry Andric // While this has no functional effect, it risks confusing someone reading 993e8d8bef9SDimitry Andric // MIR output. 994e8d8bef9SDimitry Andric // Examine all the operands, or the first N specified by the caller. 995e8d8bef9SDimitry Andric MaxOperand = std::min(MaxOperand, Old.getNumOperands()); 996fe6060f1SDimitry Andric for (unsigned int I = 0; I < MaxOperand; ++I) { 997e8d8bef9SDimitry Andric const auto &OldMO = Old.getOperand(I); 998e8d8bef9SDimitry Andric auto &NewMO = New.getOperand(I); 999e8d8bef9SDimitry Andric (void)NewMO; 1000e8d8bef9SDimitry Andric 1001e8d8bef9SDimitry Andric if (!OldMO.isReg() || !OldMO.isDef()) 1002e8d8bef9SDimitry Andric continue; 1003e8d8bef9SDimitry Andric assert(NewMO.isDef()); 1004e8d8bef9SDimitry Andric 1005e8d8bef9SDimitry Andric unsigned NewInstrNum = New.getDebugInstrNum(); 1006e8d8bef9SDimitry Andric makeDebugValueSubstitution(std::make_pair(OldInstrNum, I), 1007e8d8bef9SDimitry Andric std::make_pair(NewInstrNum, I)); 1008e8d8bef9SDimitry Andric } 1009e8d8bef9SDimitry Andric } 1010e8d8bef9SDimitry Andric 1011fe6060f1SDimitry Andric auto MachineFunction::salvageCopySSA(MachineInstr &MI) 1012fe6060f1SDimitry Andric -> DebugInstrOperandPair { 1013fe6060f1SDimitry Andric MachineRegisterInfo &MRI = getRegInfo(); 1014fe6060f1SDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 1015fe6060f1SDimitry Andric const TargetInstrInfo &TII = *getSubtarget().getInstrInfo(); 1016fe6060f1SDimitry Andric 1017fe6060f1SDimitry Andric // Chase the value read by a copy-like instruction back to the instruction 1018fe6060f1SDimitry Andric // that ultimately _defines_ that value. This may pass: 1019fe6060f1SDimitry Andric // * Through multiple intermediate copies, including subregister moves / 1020fe6060f1SDimitry Andric // copies, 1021fe6060f1SDimitry Andric // * Copies from physical registers that must then be traced back to the 1022fe6060f1SDimitry Andric // defining instruction, 1023fe6060f1SDimitry Andric // * Or, physical registers may be live-in to (only) the entry block, which 1024fe6060f1SDimitry Andric // requires a DBG_PHI to be created. 1025fe6060f1SDimitry Andric // We can pursue this problem in that order: trace back through copies, 1026fe6060f1SDimitry Andric // optionally through a physical register, to a defining instruction. We 1027fe6060f1SDimitry Andric // should never move from physreg to vreg. As we're still in SSA form, no need 1028fe6060f1SDimitry Andric // to worry about partial definitions of registers. 1029fe6060f1SDimitry Andric 1030fe6060f1SDimitry Andric // Helper lambda to interpret a copy-like instruction. Takes instruction, 1031fe6060f1SDimitry Andric // returns the register read and any subregister identifying which part is 1032fe6060f1SDimitry Andric // read. 1033fe6060f1SDimitry Andric auto GetRegAndSubreg = 1034fe6060f1SDimitry Andric [&](const MachineInstr &Cpy) -> std::pair<Register, unsigned> { 1035fe6060f1SDimitry Andric Register NewReg, OldReg; 1036fe6060f1SDimitry Andric unsigned SubReg; 1037fe6060f1SDimitry Andric if (Cpy.isCopy()) { 1038fe6060f1SDimitry Andric OldReg = Cpy.getOperand(0).getReg(); 1039fe6060f1SDimitry Andric NewReg = Cpy.getOperand(1).getReg(); 1040fe6060f1SDimitry Andric SubReg = Cpy.getOperand(1).getSubReg(); 1041fe6060f1SDimitry Andric } else if (Cpy.isSubregToReg()) { 1042fe6060f1SDimitry Andric OldReg = Cpy.getOperand(0).getReg(); 1043fe6060f1SDimitry Andric NewReg = Cpy.getOperand(2).getReg(); 1044fe6060f1SDimitry Andric SubReg = Cpy.getOperand(3).getImm(); 1045fe6060f1SDimitry Andric } else { 1046fe6060f1SDimitry Andric auto CopyDetails = *TII.isCopyInstr(Cpy); 1047fe6060f1SDimitry Andric const MachineOperand &Src = *CopyDetails.Source; 1048fe6060f1SDimitry Andric const MachineOperand &Dest = *CopyDetails.Destination; 1049fe6060f1SDimitry Andric OldReg = Dest.getReg(); 1050fe6060f1SDimitry Andric NewReg = Src.getReg(); 1051fe6060f1SDimitry Andric SubReg = Src.getSubReg(); 1052fe6060f1SDimitry Andric } 1053fe6060f1SDimitry Andric 1054fe6060f1SDimitry Andric return {NewReg, SubReg}; 1055fe6060f1SDimitry Andric }; 1056fe6060f1SDimitry Andric 1057fe6060f1SDimitry Andric // First seek either the defining instruction, or a copy from a physreg. 1058fe6060f1SDimitry Andric // During search, the current state is the current copy instruction, and which 1059fe6060f1SDimitry Andric // register we've read. Accumulate qualifying subregisters into SubregsSeen; 1060fe6060f1SDimitry Andric // deal with those later. 1061fe6060f1SDimitry Andric auto State = GetRegAndSubreg(MI); 1062fe6060f1SDimitry Andric auto CurInst = MI.getIterator(); 1063fe6060f1SDimitry Andric SmallVector<unsigned, 4> SubregsSeen; 1064fe6060f1SDimitry Andric while (true) { 1065fe6060f1SDimitry Andric // If we've found a copy from a physreg, first portion of search is over. 1066fe6060f1SDimitry Andric if (!State.first.isVirtual()) 1067fe6060f1SDimitry Andric break; 1068fe6060f1SDimitry Andric 1069fe6060f1SDimitry Andric // Record any subregister qualifier. 1070fe6060f1SDimitry Andric if (State.second) 1071fe6060f1SDimitry Andric SubregsSeen.push_back(State.second); 1072fe6060f1SDimitry Andric 1073fe6060f1SDimitry Andric assert(MRI.hasOneDef(State.first)); 1074fe6060f1SDimitry Andric MachineInstr &Inst = *MRI.def_begin(State.first)->getParent(); 1075fe6060f1SDimitry Andric CurInst = Inst.getIterator(); 1076fe6060f1SDimitry Andric 1077fe6060f1SDimitry Andric // Any non-copy instruction is the defining instruction we're seeking. 1078fe6060f1SDimitry Andric if (!Inst.isCopyLike() && !TII.isCopyInstr(Inst)) 1079fe6060f1SDimitry Andric break; 1080fe6060f1SDimitry Andric State = GetRegAndSubreg(Inst); 1081fe6060f1SDimitry Andric }; 1082fe6060f1SDimitry Andric 1083fe6060f1SDimitry Andric // Helper lambda to apply additional subregister substitutions to a known 1084fe6060f1SDimitry Andric // instruction/operand pair. Adds new (fake) substitutions so that we can 1085fe6060f1SDimitry Andric // record the subregister. FIXME: this isn't very space efficient if multiple 1086fe6060f1SDimitry Andric // values are tracked back through the same copies; cache something later. 1087fe6060f1SDimitry Andric auto ApplySubregisters = 1088fe6060f1SDimitry Andric [&](DebugInstrOperandPair P) -> DebugInstrOperandPair { 1089fe6060f1SDimitry Andric for (unsigned Subreg : reverse(SubregsSeen)) { 1090fe6060f1SDimitry Andric // Fetch a new instruction number, not attached to an actual instruction. 1091fe6060f1SDimitry Andric unsigned NewInstrNumber = getNewDebugInstrNum(); 1092fe6060f1SDimitry Andric // Add a substitution from the "new" number to the known one, with a 1093fe6060f1SDimitry Andric // qualifying subreg. 1094fe6060f1SDimitry Andric makeDebugValueSubstitution({NewInstrNumber, 0}, P, Subreg); 1095fe6060f1SDimitry Andric // Return the new number; to find the underlying value, consumers need to 1096fe6060f1SDimitry Andric // deal with the qualifying subreg. 1097fe6060f1SDimitry Andric P = {NewInstrNumber, 0}; 1098fe6060f1SDimitry Andric } 1099fe6060f1SDimitry Andric return P; 1100fe6060f1SDimitry Andric }; 1101fe6060f1SDimitry Andric 1102fe6060f1SDimitry Andric // If we managed to find the defining instruction after COPYs, return an 1103fe6060f1SDimitry Andric // instruction / operand pair after adding subregister qualifiers. 1104fe6060f1SDimitry Andric if (State.first.isVirtual()) { 1105fe6060f1SDimitry Andric // Virtual register def -- we can just look up where this happens. 1106fe6060f1SDimitry Andric MachineInstr *Inst = MRI.def_begin(State.first)->getParent(); 1107fe6060f1SDimitry Andric for (auto &MO : Inst->operands()) { 1108fe6060f1SDimitry Andric if (!MO.isReg() || !MO.isDef() || MO.getReg() != State.first) 1109fe6060f1SDimitry Andric continue; 1110fe6060f1SDimitry Andric return ApplySubregisters( 1111fe6060f1SDimitry Andric {Inst->getDebugInstrNum(), Inst->getOperandNo(&MO)}); 1112fe6060f1SDimitry Andric } 1113fe6060f1SDimitry Andric 1114fe6060f1SDimitry Andric llvm_unreachable("Vreg def with no corresponding operand?"); 1115fe6060f1SDimitry Andric } 1116fe6060f1SDimitry Andric 1117fe6060f1SDimitry Andric // Our search ended in a copy from a physreg: walk back up the function 1118fe6060f1SDimitry Andric // looking for whatever defines the physreg. 1119fe6060f1SDimitry Andric assert(CurInst->isCopyLike() || TII.isCopyInstr(*CurInst)); 1120fe6060f1SDimitry Andric State = GetRegAndSubreg(*CurInst); 1121fe6060f1SDimitry Andric Register RegToSeek = State.first; 1122fe6060f1SDimitry Andric 1123fe6060f1SDimitry Andric auto RMII = CurInst->getReverseIterator(); 1124fe6060f1SDimitry Andric auto PrevInstrs = make_range(RMII, CurInst->getParent()->instr_rend()); 1125fe6060f1SDimitry Andric for (auto &ToExamine : PrevInstrs) { 1126fe6060f1SDimitry Andric for (auto &MO : ToExamine.operands()) { 1127fe6060f1SDimitry Andric // Test for operand that defines something aliasing RegToSeek. 1128fe6060f1SDimitry Andric if (!MO.isReg() || !MO.isDef() || 1129fe6060f1SDimitry Andric !TRI.regsOverlap(RegToSeek, MO.getReg())) 1130fe6060f1SDimitry Andric continue; 1131fe6060f1SDimitry Andric 1132fe6060f1SDimitry Andric return ApplySubregisters( 1133fe6060f1SDimitry Andric {ToExamine.getDebugInstrNum(), ToExamine.getOperandNo(&MO)}); 1134fe6060f1SDimitry Andric } 1135fe6060f1SDimitry Andric } 1136fe6060f1SDimitry Andric 1137fe6060f1SDimitry Andric MachineBasicBlock &InsertBB = *CurInst->getParent(); 1138fe6060f1SDimitry Andric 1139fe6060f1SDimitry Andric // We reached the start of the block before finding a defining instruction. 1140fe6060f1SDimitry Andric // It could be from a constant register, otherwise it must be an argument. 1141fe6060f1SDimitry Andric if (TRI.isConstantPhysReg(State.first)) { 1142fe6060f1SDimitry Andric // We can produce a DBG_PHI that identifies the constant physreg. Doesn't 1143fe6060f1SDimitry Andric // matter where we put it, as it's constant valued. 1144fe6060f1SDimitry Andric assert(CurInst->isCopy()); 1145fe6060f1SDimitry Andric } else if (State.first == TRI.getFrameRegister(*this)) { 1146fe6060f1SDimitry Andric // LLVM IR is allowed to read the framepointer by calling a 1147fe6060f1SDimitry Andric // llvm.frameaddress.* intrinsic. We can support this by emitting a 1148fe6060f1SDimitry Andric // DBG_PHI $fp. This isn't ideal, because it extends the behaviours / 1149fe6060f1SDimitry Andric // position that DBG_PHIs appear at, limiting what can be done later. 1150fe6060f1SDimitry Andric // TODO: see if there's a better way of expressing these variable 1151fe6060f1SDimitry Andric // locations. 1152fe6060f1SDimitry Andric ; 1153fe6060f1SDimitry Andric } else { 1154*349cc55cSDimitry Andric // Assert that this is the entry block, or an EH pad. If it isn't, then 1155*349cc55cSDimitry Andric // there is some code construct we don't recognise that deals with physregs 1156*349cc55cSDimitry Andric // across blocks. 1157fe6060f1SDimitry Andric assert(!State.first.isVirtual()); 1158*349cc55cSDimitry Andric assert(&*InsertBB.getParent()->begin() == &InsertBB || InsertBB.isEHPad()); 1159fe6060f1SDimitry Andric } 1160fe6060f1SDimitry Andric 1161fe6060f1SDimitry Andric // Create DBG_PHI for specified physreg. 1162fe6060f1SDimitry Andric auto Builder = BuildMI(InsertBB, InsertBB.getFirstNonPHI(), DebugLoc(), 1163fe6060f1SDimitry Andric TII.get(TargetOpcode::DBG_PHI)); 1164*349cc55cSDimitry Andric Builder.addReg(State.first); 1165fe6060f1SDimitry Andric unsigned NewNum = getNewDebugInstrNum(); 1166fe6060f1SDimitry Andric Builder.addImm(NewNum); 1167fe6060f1SDimitry Andric return ApplySubregisters({NewNum, 0u}); 1168fe6060f1SDimitry Andric } 1169fe6060f1SDimitry Andric 1170fe6060f1SDimitry Andric void MachineFunction::finalizeDebugInstrRefs() { 1171fe6060f1SDimitry Andric auto *TII = getSubtarget().getInstrInfo(); 1172fe6060f1SDimitry Andric 1173fe6060f1SDimitry Andric auto MakeDbgValue = [&](MachineInstr &MI) { 1174fe6060f1SDimitry Andric const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_VALUE); 1175fe6060f1SDimitry Andric MI.setDesc(RefII); 1176fe6060f1SDimitry Andric MI.getOperand(1).ChangeToRegister(0, false); 1177fe6060f1SDimitry Andric }; 1178fe6060f1SDimitry Andric 1179*349cc55cSDimitry Andric if (!useDebugInstrRef()) 1180fe6060f1SDimitry Andric return; 1181fe6060f1SDimitry Andric 1182fe6060f1SDimitry Andric for (auto &MBB : *this) { 1183fe6060f1SDimitry Andric for (auto &MI : MBB) { 1184fe6060f1SDimitry Andric if (!MI.isDebugRef() || !MI.getOperand(0).isReg()) 1185fe6060f1SDimitry Andric continue; 1186fe6060f1SDimitry Andric 1187fe6060f1SDimitry Andric Register Reg = MI.getOperand(0).getReg(); 1188fe6060f1SDimitry Andric 1189fe6060f1SDimitry Andric // Some vregs can be deleted as redundant in the meantime. Mark those 1190fe6060f1SDimitry Andric // as DBG_VALUE $noreg. 1191fe6060f1SDimitry Andric if (Reg == 0) { 1192fe6060f1SDimitry Andric MakeDbgValue(MI); 1193fe6060f1SDimitry Andric continue; 1194fe6060f1SDimitry Andric } 1195fe6060f1SDimitry Andric 1196fe6060f1SDimitry Andric assert(Reg.isVirtual()); 1197fe6060f1SDimitry Andric MachineInstr &DefMI = *RegInfo->def_instr_begin(Reg); 1198fe6060f1SDimitry Andric assert(RegInfo->hasOneDef(Reg)); 1199fe6060f1SDimitry Andric 1200fe6060f1SDimitry Andric // If we've found a copy-like instruction, follow it back to the 1201fe6060f1SDimitry Andric // instruction that defines the source value, see salvageCopySSA docs 1202fe6060f1SDimitry Andric // for why this is important. 1203fe6060f1SDimitry Andric if (DefMI.isCopyLike() || TII->isCopyInstr(DefMI)) { 1204fe6060f1SDimitry Andric auto Result = salvageCopySSA(DefMI); 1205fe6060f1SDimitry Andric MI.getOperand(0).ChangeToImmediate(Result.first); 1206fe6060f1SDimitry Andric MI.getOperand(1).setImm(Result.second); 1207fe6060f1SDimitry Andric } else { 1208fe6060f1SDimitry Andric // Otherwise, identify the operand number that the VReg refers to. 1209fe6060f1SDimitry Andric unsigned OperandIdx = 0; 1210fe6060f1SDimitry Andric for (const auto &MO : DefMI.operands()) { 1211fe6060f1SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() == Reg) 1212fe6060f1SDimitry Andric break; 1213fe6060f1SDimitry Andric ++OperandIdx; 1214fe6060f1SDimitry Andric } 1215fe6060f1SDimitry Andric assert(OperandIdx < DefMI.getNumOperands()); 1216fe6060f1SDimitry Andric 1217fe6060f1SDimitry Andric // Morph this instr ref to point at the given instruction and operand. 1218fe6060f1SDimitry Andric unsigned ID = DefMI.getDebugInstrNum(); 1219fe6060f1SDimitry Andric MI.getOperand(0).ChangeToImmediate(ID); 1220fe6060f1SDimitry Andric MI.getOperand(1).setImm(OperandIdx); 1221fe6060f1SDimitry Andric } 1222fe6060f1SDimitry Andric } 1223fe6060f1SDimitry Andric } 1224fe6060f1SDimitry Andric } 1225fe6060f1SDimitry Andric 1226*349cc55cSDimitry Andric bool MachineFunction::useDebugInstrRef() const { 1227*349cc55cSDimitry Andric // Disable instr-ref at -O0: it's very slow (in compile time). We can still 1228*349cc55cSDimitry Andric // have optimized code inlined into this unoptimized code, however with 1229*349cc55cSDimitry Andric // fewer and less aggressive optimizations happening, coverage and accuracy 1230*349cc55cSDimitry Andric // should not suffer. 1231*349cc55cSDimitry Andric if (getTarget().getOptLevel() == CodeGenOpt::None) 1232*349cc55cSDimitry Andric return false; 1233*349cc55cSDimitry Andric 1234*349cc55cSDimitry Andric // Don't use instr-ref if this function is marked optnone. 1235*349cc55cSDimitry Andric if (F.hasFnAttribute(Attribute::OptimizeNone)) 1236*349cc55cSDimitry Andric return false; 1237*349cc55cSDimitry Andric 1238*349cc55cSDimitry Andric if (getTarget().Options.ValueTrackingVariableLocations) 1239*349cc55cSDimitry Andric return true; 1240*349cc55cSDimitry Andric 1241*349cc55cSDimitry Andric return false; 1242*349cc55cSDimitry Andric } 1243*349cc55cSDimitry Andric 1244*349cc55cSDimitry Andric // Use one million as a high / reserved number. 1245*349cc55cSDimitry Andric const unsigned MachineFunction::DebugOperandMemNumber = 1000000; 1246*349cc55cSDimitry Andric 12470b57cec5SDimitry Andric /// \} 12480b57cec5SDimitry Andric 12490b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12500b57cec5SDimitry Andric // MachineJumpTableInfo implementation 12510b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric /// Return the size of each entry in the jump table. 12540b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { 12550b57cec5SDimitry Andric // The size of a jump table entry is 4 bytes unless the entry is just the 12560b57cec5SDimitry Andric // address of a block, in which case it is the pointer size. 12570b57cec5SDimitry Andric switch (getEntryKind()) { 12580b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 12590b57cec5SDimitry Andric return TD.getPointerSize(); 12600b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 12610b57cec5SDimitry Andric return 8; 12620b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 12630b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 12640b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 12650b57cec5SDimitry Andric return 4; 12660b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 12670b57cec5SDimitry Andric return 0; 12680b57cec5SDimitry Andric } 12690b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 12700b57cec5SDimitry Andric } 12710b57cec5SDimitry Andric 12720b57cec5SDimitry Andric /// Return the alignment of each entry in the jump table. 12730b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { 12740b57cec5SDimitry Andric // The alignment of a jump table entry is the alignment of int32 unless the 12750b57cec5SDimitry Andric // entry is just the address of a block, in which case it is the pointer 12760b57cec5SDimitry Andric // alignment. 12770b57cec5SDimitry Andric switch (getEntryKind()) { 12780b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 12798bcb0991SDimitry Andric return TD.getPointerABIAlignment(0).value(); 12800b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 12818bcb0991SDimitry Andric return TD.getABIIntegerTypeAlignment(64).value(); 12820b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 12830b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 12840b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 12858bcb0991SDimitry Andric return TD.getABIIntegerTypeAlignment(32).value(); 12860b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 12870b57cec5SDimitry Andric return 1; 12880b57cec5SDimitry Andric } 12890b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 12900b57cec5SDimitry Andric } 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric /// Create a new jump table entry in the jump table info. 12930b57cec5SDimitry Andric unsigned MachineJumpTableInfo::createJumpTableIndex( 12940b57cec5SDimitry Andric const std::vector<MachineBasicBlock*> &DestBBs) { 12950b57cec5SDimitry Andric assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 12960b57cec5SDimitry Andric JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 12970b57cec5SDimitry Andric return JumpTables.size()-1; 12980b57cec5SDimitry Andric } 12990b57cec5SDimitry Andric 13000b57cec5SDimitry Andric /// If Old is the target of any jump tables, update the jump tables to branch 13010b57cec5SDimitry Andric /// to New instead. 13020b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 13030b57cec5SDimitry Andric MachineBasicBlock *New) { 13040b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 13050b57cec5SDimitry Andric bool MadeChange = false; 13060b57cec5SDimitry Andric for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 13070b57cec5SDimitry Andric ReplaceMBBInJumpTable(i, Old, New); 13080b57cec5SDimitry Andric return MadeChange; 13090b57cec5SDimitry Andric } 13100b57cec5SDimitry Andric 1311e8d8bef9SDimitry Andric /// If MBB is present in any jump tables, remove it. 1312e8d8bef9SDimitry Andric bool MachineJumpTableInfo::RemoveMBBFromJumpTables(MachineBasicBlock *MBB) { 1313e8d8bef9SDimitry Andric bool MadeChange = false; 1314e8d8bef9SDimitry Andric for (MachineJumpTableEntry &JTE : JumpTables) { 1315e8d8bef9SDimitry Andric auto removeBeginItr = std::remove(JTE.MBBs.begin(), JTE.MBBs.end(), MBB); 1316e8d8bef9SDimitry Andric MadeChange |= (removeBeginItr != JTE.MBBs.end()); 1317e8d8bef9SDimitry Andric JTE.MBBs.erase(removeBeginItr, JTE.MBBs.end()); 1318e8d8bef9SDimitry Andric } 1319e8d8bef9SDimitry Andric return MadeChange; 1320e8d8bef9SDimitry Andric } 1321e8d8bef9SDimitry Andric 13220b57cec5SDimitry Andric /// If Old is a target of the jump tables, update the jump table to branch to 13230b57cec5SDimitry Andric /// New instead. 13240b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 13250b57cec5SDimitry Andric MachineBasicBlock *Old, 13260b57cec5SDimitry Andric MachineBasicBlock *New) { 13270b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 13280b57cec5SDimitry Andric bool MadeChange = false; 13290b57cec5SDimitry Andric MachineJumpTableEntry &JTE = JumpTables[Idx]; 13300b57cec5SDimitry Andric for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 13310b57cec5SDimitry Andric if (JTE.MBBs[j] == Old) { 13320b57cec5SDimitry Andric JTE.MBBs[j] = New; 13330b57cec5SDimitry Andric MadeChange = true; 13340b57cec5SDimitry Andric } 13350b57cec5SDimitry Andric return MadeChange; 13360b57cec5SDimitry Andric } 13370b57cec5SDimitry Andric 13380b57cec5SDimitry Andric void MachineJumpTableInfo::print(raw_ostream &OS) const { 13390b57cec5SDimitry Andric if (JumpTables.empty()) return; 13400b57cec5SDimitry Andric 13410b57cec5SDimitry Andric OS << "Jump Tables:\n"; 13420b57cec5SDimitry Andric 13430b57cec5SDimitry Andric for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 13440b57cec5SDimitry Andric OS << printJumpTableEntryReference(i) << ':'; 13450b57cec5SDimitry Andric for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 13460b57cec5SDimitry Andric OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]); 13470b57cec5SDimitry Andric if (i != e) 13480b57cec5SDimitry Andric OS << '\n'; 13490b57cec5SDimitry Andric } 13500b57cec5SDimitry Andric 13510b57cec5SDimitry Andric OS << '\n'; 13520b57cec5SDimitry Andric } 13530b57cec5SDimitry Andric 13540b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 13550b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } 13560b57cec5SDimitry Andric #endif 13570b57cec5SDimitry Andric 13580b57cec5SDimitry Andric Printable llvm::printJumpTableEntryReference(unsigned Idx) { 13590b57cec5SDimitry Andric return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); 13600b57cec5SDimitry Andric } 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13630b57cec5SDimitry Andric // MachineConstantPool implementation 13640b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13650b57cec5SDimitry Andric 13660b57cec5SDimitry Andric void MachineConstantPoolValue::anchor() {} 13670b57cec5SDimitry Andric 1368e8d8bef9SDimitry Andric unsigned MachineConstantPoolValue::getSizeInBytes(const DataLayout &DL) const { 1369e8d8bef9SDimitry Andric return DL.getTypeAllocSize(Ty); 1370e8d8bef9SDimitry Andric } 1371e8d8bef9SDimitry Andric 1372e8d8bef9SDimitry Andric unsigned MachineConstantPoolEntry::getSizeInBytes(const DataLayout &DL) const { 13730b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 1374e8d8bef9SDimitry Andric return Val.MachineCPVal->getSizeInBytes(DL); 1375e8d8bef9SDimitry Andric return DL.getTypeAllocSize(Val.ConstVal->getType()); 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric 13780b57cec5SDimitry Andric bool MachineConstantPoolEntry::needsRelocation() const { 13790b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 13800b57cec5SDimitry Andric return true; 1381fe6060f1SDimitry Andric return Val.ConstVal->needsDynamicRelocation(); 13820b57cec5SDimitry Andric } 13830b57cec5SDimitry Andric 13840b57cec5SDimitry Andric SectionKind 13850b57cec5SDimitry Andric MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { 13860b57cec5SDimitry Andric if (needsRelocation()) 13870b57cec5SDimitry Andric return SectionKind::getReadOnlyWithRel(); 1388e8d8bef9SDimitry Andric switch (getSizeInBytes(*DL)) { 13890b57cec5SDimitry Andric case 4: 13900b57cec5SDimitry Andric return SectionKind::getMergeableConst4(); 13910b57cec5SDimitry Andric case 8: 13920b57cec5SDimitry Andric return SectionKind::getMergeableConst8(); 13930b57cec5SDimitry Andric case 16: 13940b57cec5SDimitry Andric return SectionKind::getMergeableConst16(); 13950b57cec5SDimitry Andric case 32: 13960b57cec5SDimitry Andric return SectionKind::getMergeableConst32(); 13970b57cec5SDimitry Andric default: 13980b57cec5SDimitry Andric return SectionKind::getReadOnly(); 13990b57cec5SDimitry Andric } 14000b57cec5SDimitry Andric } 14010b57cec5SDimitry Andric 14020b57cec5SDimitry Andric MachineConstantPool::~MachineConstantPool() { 14030b57cec5SDimitry Andric // A constant may be a member of both Constants and MachineCPVsSharingEntries, 14040b57cec5SDimitry Andric // so keep track of which we've deleted to avoid double deletions. 14050b57cec5SDimitry Andric DenseSet<MachineConstantPoolValue*> Deleted; 14060b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) 14070b57cec5SDimitry Andric if (Constants[i].isMachineConstantPoolEntry()) { 14080b57cec5SDimitry Andric Deleted.insert(Constants[i].Val.MachineCPVal); 14090b57cec5SDimitry Andric delete Constants[i].Val.MachineCPVal; 14100b57cec5SDimitry Andric } 1411fe6060f1SDimitry Andric for (MachineConstantPoolValue *CPV : MachineCPVsSharingEntries) { 1412fe6060f1SDimitry Andric if (Deleted.count(CPV) == 0) 1413fe6060f1SDimitry Andric delete CPV; 14140b57cec5SDimitry Andric } 14150b57cec5SDimitry Andric } 14160b57cec5SDimitry Andric 14170b57cec5SDimitry Andric /// Test whether the given two constants can be allocated the same constant pool 14180b57cec5SDimitry Andric /// entry. 14190b57cec5SDimitry Andric static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 14200b57cec5SDimitry Andric const DataLayout &DL) { 14210b57cec5SDimitry Andric // Handle the trivial case quickly. 14220b57cec5SDimitry Andric if (A == B) return true; 14230b57cec5SDimitry Andric 14240b57cec5SDimitry Andric // If they have the same type but weren't the same constant, quickly 14250b57cec5SDimitry Andric // reject them. 14260b57cec5SDimitry Andric if (A->getType() == B->getType()) return false; 14270b57cec5SDimitry Andric 14280b57cec5SDimitry Andric // We can't handle structs or arrays. 14290b57cec5SDimitry Andric if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || 14300b57cec5SDimitry Andric isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) 14310b57cec5SDimitry Andric return false; 14320b57cec5SDimitry Andric 14330b57cec5SDimitry Andric // For now, only support constants with the same size. 14340b57cec5SDimitry Andric uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); 14350b57cec5SDimitry Andric if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) 14360b57cec5SDimitry Andric return false; 14370b57cec5SDimitry Andric 14380b57cec5SDimitry Andric Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric // Try constant folding a bitcast of both instructions to an integer. If we 14410b57cec5SDimitry Andric // get two identical ConstantInt's, then we are good to share them. We use 14420b57cec5SDimitry Andric // the constant folding APIs to do this so that we get the benefit of 14430b57cec5SDimitry Andric // DataLayout. 14440b57cec5SDimitry Andric if (isa<PointerType>(A->getType())) 14450b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::PtrToInt, 14460b57cec5SDimitry Andric const_cast<Constant *>(A), IntTy, DL); 14470b57cec5SDimitry Andric else if (A->getType() != IntTy) 14480b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), 14490b57cec5SDimitry Andric IntTy, DL); 14500b57cec5SDimitry Andric if (isa<PointerType>(B->getType())) 14510b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::PtrToInt, 14520b57cec5SDimitry Andric const_cast<Constant *>(B), IntTy, DL); 14530b57cec5SDimitry Andric else if (B->getType() != IntTy) 14540b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), 14550b57cec5SDimitry Andric IntTy, DL); 14560b57cec5SDimitry Andric 14570b57cec5SDimitry Andric return A == B; 14580b57cec5SDimitry Andric } 14590b57cec5SDimitry Andric 14600b57cec5SDimitry Andric /// Create a new entry in the constant pool or return an existing one. 14610b57cec5SDimitry Andric /// User must specify the log2 of the minimum required alignment for the object. 14620b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 14635ffd83dbSDimitry Andric Align Alignment) { 14640b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 14650b57cec5SDimitry Andric 14660b57cec5SDimitry Andric // Check to see if we already have this constant. 14670b57cec5SDimitry Andric // 14680b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 14690b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) 14700b57cec5SDimitry Andric if (!Constants[i].isMachineConstantPoolEntry() && 14710b57cec5SDimitry Andric CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { 14725ffd83dbSDimitry Andric if (Constants[i].getAlign() < Alignment) 14730b57cec5SDimitry Andric Constants[i].Alignment = Alignment; 14740b57cec5SDimitry Andric return i; 14750b57cec5SDimitry Andric } 14760b57cec5SDimitry Andric 14770b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 14780b57cec5SDimitry Andric return Constants.size()-1; 14790b57cec5SDimitry Andric } 14800b57cec5SDimitry Andric 14810b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 14825ffd83dbSDimitry Andric Align Alignment) { 14830b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric // Check to see if we already have this constant. 14860b57cec5SDimitry Andric // 14870b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 14880b57cec5SDimitry Andric int Idx = V->getExistingMachineCPValue(this, Alignment); 14890b57cec5SDimitry Andric if (Idx != -1) { 14900b57cec5SDimitry Andric MachineCPVsSharingEntries.insert(V); 14910b57cec5SDimitry Andric return (unsigned)Idx; 14920b57cec5SDimitry Andric } 14930b57cec5SDimitry Andric 14940b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 14950b57cec5SDimitry Andric return Constants.size()-1; 14960b57cec5SDimitry Andric } 14970b57cec5SDimitry Andric 14980b57cec5SDimitry Andric void MachineConstantPool::print(raw_ostream &OS) const { 14990b57cec5SDimitry Andric if (Constants.empty()) return; 15000b57cec5SDimitry Andric 15010b57cec5SDimitry Andric OS << "Constant Pool:\n"; 15020b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 15030b57cec5SDimitry Andric OS << " cp#" << i << ": "; 15040b57cec5SDimitry Andric if (Constants[i].isMachineConstantPoolEntry()) 15050b57cec5SDimitry Andric Constants[i].Val.MachineCPVal->print(OS); 15060b57cec5SDimitry Andric else 15070b57cec5SDimitry Andric Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); 15085ffd83dbSDimitry Andric OS << ", align=" << Constants[i].getAlign().value(); 15090b57cec5SDimitry Andric OS << "\n"; 15100b57cec5SDimitry Andric } 15110b57cec5SDimitry Andric } 15120b57cec5SDimitry Andric 15130b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 15140b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } 15150b57cec5SDimitry Andric #endif 1516