1*0b57cec5SDimitry Andric //===- MachineFunction.cpp ------------------------------------------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // Collect native machine code information for a function. This allows 10*0b57cec5SDimitry Andric // target-specific information about the generated code to be stored with each 11*0b57cec5SDimitry Andric // function. 12*0b57cec5SDimitry Andric // 13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 14*0b57cec5SDimitry Andric 15*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 17*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 18*0b57cec5SDimitry Andric #include "llvm/ADT/DenseSet.h" 19*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 20*0b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 21*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 22*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 23*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 24*0b57cec5SDimitry Andric #include "llvm/Analysis/ConstantFolding.h" 25*0b57cec5SDimitry Andric #include "llvm/Analysis/EHPersonalities.h" 26*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 27*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineConstantPool.h" 28*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 29*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 30*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineJumpTableInfo.h" 31*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineMemOperand.h" 32*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineModuleInfo.h" 33*0b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 34*0b57cec5SDimitry Andric #include "llvm/CodeGen/PseudoSourceValue.h" 35*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 365ffd83dbSDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 37*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 38*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 39*0b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 40*0b57cec5SDimitry Andric #include "llvm/CodeGen/WasmEHFuncInfo.h" 41*0b57cec5SDimitry Andric #include "llvm/CodeGen/WinEHFuncInfo.h" 42*0b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h" 43*0b57cec5SDimitry Andric #include "llvm/IR/Attributes.h" 44*0b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h" 45*0b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 46*0b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 47*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h" 48*0b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 49*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 50*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 51*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 52*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 53*0b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 54*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 55*0b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h" 56*0b57cec5SDimitry Andric #include "llvm/IR/Value.h" 57*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 58*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 59*0b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h" 60*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 61*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 62*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 63*0b57cec5SDimitry Andric #include "llvm/Support/DOTGraphTraits.h" 64*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 65*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 66*0b57cec5SDimitry Andric #include "llvm/Support/GraphWriter.h" 67*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 68*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 69*0b57cec5SDimitry Andric #include <algorithm> 70*0b57cec5SDimitry Andric #include <cassert> 71*0b57cec5SDimitry Andric #include <cstddef> 72*0b57cec5SDimitry Andric #include <cstdint> 73*0b57cec5SDimitry Andric #include <iterator> 74*0b57cec5SDimitry Andric #include <string> 755ffd83dbSDimitry Andric #include <type_traits> 76*0b57cec5SDimitry Andric #include <utility> 77*0b57cec5SDimitry Andric #include <vector> 78*0b57cec5SDimitry Andric 79*0b57cec5SDimitry Andric using namespace llvm; 80*0b57cec5SDimitry Andric 81*0b57cec5SDimitry Andric #define DEBUG_TYPE "codegen" 82*0b57cec5SDimitry 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)."), 87*0b57cec5SDimitry Andric cl::init(0), cl::Hidden); 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric static const char *getPropertyName(MachineFunctionProperties::Property Prop) { 90*0b57cec5SDimitry Andric using P = MachineFunctionProperties::Property; 91*0b57cec5SDimitry Andric 92*0b57cec5SDimitry Andric switch(Prop) { 93*0b57cec5SDimitry Andric case P::FailedISel: return "FailedISel"; 94*0b57cec5SDimitry Andric case P::IsSSA: return "IsSSA"; 95*0b57cec5SDimitry Andric case P::Legalized: return "Legalized"; 96*0b57cec5SDimitry Andric case P::NoPHIs: return "NoPHIs"; 97*0b57cec5SDimitry Andric case P::NoVRegs: return "NoVRegs"; 98*0b57cec5SDimitry Andric case P::RegBankSelected: return "RegBankSelected"; 99*0b57cec5SDimitry Andric case P::Selected: return "Selected"; 100*0b57cec5SDimitry Andric case P::TracksLiveness: return "TracksLiveness"; 1015ffd83dbSDimitry Andric case P::TiedOpsRewritten: return "TiedOpsRewritten"; 102*0b57cec5SDimitry Andric } 103*0b57cec5SDimitry Andric llvm_unreachable("Invalid machine function property"); 104*0b57cec5SDimitry Andric } 105*0b57cec5SDimitry Andric 106*0b57cec5SDimitry Andric // Pin the vtable to this file. 107*0b57cec5SDimitry Andric void MachineFunction::Delegate::anchor() {} 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric void MachineFunctionProperties::print(raw_ostream &OS) const { 110*0b57cec5SDimitry Andric const char *Separator = ""; 111*0b57cec5SDimitry Andric for (BitVector::size_type I = 0; I < Properties.size(); ++I) { 112*0b57cec5SDimitry Andric if (!Properties[I]) 113*0b57cec5SDimitry Andric continue; 114*0b57cec5SDimitry Andric OS << Separator << getPropertyName(static_cast<Property>(I)); 115*0b57cec5SDimitry Andric Separator = ", "; 116*0b57cec5SDimitry Andric } 117*0b57cec5SDimitry Andric } 118*0b57cec5SDimitry Andric 119*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120*0b57cec5SDimitry Andric // MachineFunction implementation 121*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 122*0b57cec5SDimitry Andric 123*0b57cec5SDimitry Andric // Out-of-line virtual method. 124*0b57cec5SDimitry Andric MachineFunctionInfo::~MachineFunctionInfo() = default; 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 127*0b57cec5SDimitry Andric MBB->getParent()->DeleteMachineBasicBlock(MBB); 128*0b57cec5SDimitry Andric } 129*0b57cec5SDimitry Andric 130*0b57cec5SDimitry Andric static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI, 131*0b57cec5SDimitry Andric const Function &F) { 132*0b57cec5SDimitry Andric if (F.hasFnAttribute(Attribute::StackAlignment)) 133*0b57cec5SDimitry Andric return F.getFnStackAlignment(); 1345ffd83dbSDimitry Andric return STI->getFrameLowering()->getStackAlign().value(); 135*0b57cec5SDimitry Andric } 136*0b57cec5SDimitry Andric 1375ffd83dbSDimitry Andric MachineFunction::MachineFunction(Function &F, const LLVMTargetMachine &Target, 138*0b57cec5SDimitry Andric const TargetSubtargetInfo &STI, 139*0b57cec5SDimitry Andric unsigned FunctionNum, MachineModuleInfo &mmi) 140*0b57cec5SDimitry Andric : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) { 141*0b57cec5SDimitry Andric FunctionNumber = FunctionNum; 142*0b57cec5SDimitry Andric init(); 143*0b57cec5SDimitry Andric } 144*0b57cec5SDimitry Andric 145*0b57cec5SDimitry Andric void MachineFunction::handleInsertion(MachineInstr &MI) { 146*0b57cec5SDimitry Andric if (TheDelegate) 147*0b57cec5SDimitry Andric TheDelegate->MF_HandleInsertion(MI); 148*0b57cec5SDimitry Andric } 149*0b57cec5SDimitry Andric 150*0b57cec5SDimitry Andric void MachineFunction::handleRemoval(MachineInstr &MI) { 151*0b57cec5SDimitry Andric if (TheDelegate) 152*0b57cec5SDimitry Andric TheDelegate->MF_HandleRemoval(MI); 153*0b57cec5SDimitry Andric } 154*0b57cec5SDimitry Andric 155*0b57cec5SDimitry Andric void MachineFunction::init() { 156*0b57cec5SDimitry Andric // Assume the function starts in SSA form with correct liveness. 157*0b57cec5SDimitry Andric Properties.set(MachineFunctionProperties::Property::IsSSA); 158*0b57cec5SDimitry Andric Properties.set(MachineFunctionProperties::Property::TracksLiveness); 159*0b57cec5SDimitry Andric if (STI->getRegisterInfo()) 160*0b57cec5SDimitry Andric RegInfo = new (Allocator) MachineRegisterInfo(this); 161*0b57cec5SDimitry Andric else 162*0b57cec5SDimitry Andric RegInfo = nullptr; 163*0b57cec5SDimitry Andric 164*0b57cec5SDimitry Andric MFInfo = nullptr; 165*0b57cec5SDimitry Andric // We can realign the stack if the target supports it and the user hasn't 166*0b57cec5SDimitry Andric // explicitly asked us not to. 167*0b57cec5SDimitry Andric bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && 168*0b57cec5SDimitry Andric !F.hasFnAttribute("no-realign-stack"); 169*0b57cec5SDimitry Andric FrameInfo = new (Allocator) MachineFrameInfo( 170*0b57cec5SDimitry Andric getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, 171*0b57cec5SDimitry Andric /*ForcedRealign=*/CanRealignSP && 172*0b57cec5SDimitry Andric F.hasFnAttribute(Attribute::StackAlignment)); 173*0b57cec5SDimitry Andric 174*0b57cec5SDimitry Andric if (F.hasFnAttribute(Attribute::StackAlignment)) 1755ffd83dbSDimitry Andric FrameInfo->ensureMaxAlignment(*F.getFnStackAlign()); 176*0b57cec5SDimitry Andric 177*0b57cec5SDimitry Andric ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); 178*0b57cec5SDimitry Andric Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. 181*0b57cec5SDimitry Andric // FIXME: Use Function::hasOptSize(). 182*0b57cec5SDimitry Andric if (!F.hasFnAttribute(Attribute::OptimizeForSize)) 183*0b57cec5SDimitry Andric Alignment = std::max(Alignment, 184*0b57cec5SDimitry Andric STI->getTargetLowering()->getPrefFunctionAlignment()); 185*0b57cec5SDimitry Andric 186*0b57cec5SDimitry Andric if (AlignAllFunctions) 1878bcb0991SDimitry Andric Alignment = Align(1ULL << AlignAllFunctions); 188*0b57cec5SDimitry Andric 189*0b57cec5SDimitry Andric JumpTableInfo = nullptr; 190*0b57cec5SDimitry Andric 191*0b57cec5SDimitry Andric if (isFuncletEHPersonality(classifyEHPersonality( 192*0b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 193*0b57cec5SDimitry Andric WinEHInfo = new (Allocator) WinEHFuncInfo(); 194*0b57cec5SDimitry Andric } 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric if (isScopedEHPersonality(classifyEHPersonality( 197*0b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 198*0b57cec5SDimitry Andric WasmEHInfo = new (Allocator) WasmEHFuncInfo(); 199*0b57cec5SDimitry Andric } 200*0b57cec5SDimitry Andric 201*0b57cec5SDimitry Andric assert(Target.isCompatibleDataLayout(getDataLayout()) && 202*0b57cec5SDimitry Andric "Can't create a MachineFunction using a Module with a " 203*0b57cec5SDimitry Andric "Target-incompatible DataLayout attached\n"); 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric PSVManager = 2068bcb0991SDimitry Andric std::make_unique<PseudoSourceValueManager>(*(getSubtarget(). 207*0b57cec5SDimitry Andric getInstrInfo())); 208*0b57cec5SDimitry Andric } 209*0b57cec5SDimitry Andric 210*0b57cec5SDimitry Andric MachineFunction::~MachineFunction() { 211*0b57cec5SDimitry Andric clear(); 212*0b57cec5SDimitry Andric } 213*0b57cec5SDimitry Andric 214*0b57cec5SDimitry Andric void MachineFunction::clear() { 215*0b57cec5SDimitry Andric Properties.reset(); 216*0b57cec5SDimitry Andric // Don't call destructors on MachineInstr and MachineOperand. All of their 217*0b57cec5SDimitry Andric // memory comes from the BumpPtrAllocator which is about to be purged. 218*0b57cec5SDimitry Andric // 219*0b57cec5SDimitry Andric // Do call MachineBasicBlock destructors, it contains std::vectors. 220*0b57cec5SDimitry Andric for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) 221*0b57cec5SDimitry Andric I->Insts.clearAndLeakNodesUnsafely(); 222*0b57cec5SDimitry Andric MBBNumbering.clear(); 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric InstructionRecycler.clear(Allocator); 225*0b57cec5SDimitry Andric OperandRecycler.clear(Allocator); 226*0b57cec5SDimitry Andric BasicBlockRecycler.clear(Allocator); 227*0b57cec5SDimitry Andric CodeViewAnnotations.clear(); 228*0b57cec5SDimitry Andric VariableDbgInfos.clear(); 229*0b57cec5SDimitry Andric if (RegInfo) { 230*0b57cec5SDimitry Andric RegInfo->~MachineRegisterInfo(); 231*0b57cec5SDimitry Andric Allocator.Deallocate(RegInfo); 232*0b57cec5SDimitry Andric } 233*0b57cec5SDimitry Andric if (MFInfo) { 234*0b57cec5SDimitry Andric MFInfo->~MachineFunctionInfo(); 235*0b57cec5SDimitry Andric Allocator.Deallocate(MFInfo); 236*0b57cec5SDimitry Andric } 237*0b57cec5SDimitry Andric 238*0b57cec5SDimitry Andric FrameInfo->~MachineFrameInfo(); 239*0b57cec5SDimitry Andric Allocator.Deallocate(FrameInfo); 240*0b57cec5SDimitry Andric 241*0b57cec5SDimitry Andric ConstantPool->~MachineConstantPool(); 242*0b57cec5SDimitry Andric Allocator.Deallocate(ConstantPool); 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric if (JumpTableInfo) { 245*0b57cec5SDimitry Andric JumpTableInfo->~MachineJumpTableInfo(); 246*0b57cec5SDimitry Andric Allocator.Deallocate(JumpTableInfo); 247*0b57cec5SDimitry Andric } 248*0b57cec5SDimitry Andric 249*0b57cec5SDimitry Andric if (WinEHInfo) { 250*0b57cec5SDimitry Andric WinEHInfo->~WinEHFuncInfo(); 251*0b57cec5SDimitry Andric Allocator.Deallocate(WinEHInfo); 252*0b57cec5SDimitry Andric } 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric if (WasmEHInfo) { 255*0b57cec5SDimitry Andric WasmEHInfo->~WasmEHFuncInfo(); 256*0b57cec5SDimitry Andric Allocator.Deallocate(WasmEHInfo); 257*0b57cec5SDimitry Andric } 258*0b57cec5SDimitry Andric } 259*0b57cec5SDimitry Andric 260*0b57cec5SDimitry Andric const DataLayout &MachineFunction::getDataLayout() const { 261*0b57cec5SDimitry Andric return F.getParent()->getDataLayout(); 262*0b57cec5SDimitry Andric } 263*0b57cec5SDimitry Andric 264*0b57cec5SDimitry Andric /// Get the JumpTableInfo for this function. 265*0b57cec5SDimitry Andric /// If it does not already exist, allocate one. 266*0b57cec5SDimitry Andric MachineJumpTableInfo *MachineFunction:: 267*0b57cec5SDimitry Andric getOrCreateJumpTableInfo(unsigned EntryKind) { 268*0b57cec5SDimitry Andric if (JumpTableInfo) return JumpTableInfo; 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric JumpTableInfo = new (Allocator) 271*0b57cec5SDimitry Andric MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 272*0b57cec5SDimitry Andric return JumpTableInfo; 273*0b57cec5SDimitry Andric } 274*0b57cec5SDimitry Andric 275480093f4SDimitry Andric DenormalMode MachineFunction::getDenormalMode(const fltSemantics &FPType) const { 276e8d8bef9SDimitry Andric return F.getDenormalMode(FPType); 277480093f4SDimitry Andric } 278480093f4SDimitry Andric 279*0b57cec5SDimitry Andric /// Should we be emitting segmented stack stuff for the function 280*0b57cec5SDimitry Andric bool MachineFunction::shouldSplitStack() const { 281*0b57cec5SDimitry Andric return getFunction().hasFnAttribute("split-stack"); 282*0b57cec5SDimitry Andric } 283*0b57cec5SDimitry Andric 284*0b57cec5SDimitry Andric LLVM_NODISCARD unsigned 285*0b57cec5SDimitry Andric MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { 286*0b57cec5SDimitry Andric FrameInstructions.push_back(Inst); 287*0b57cec5SDimitry Andric return FrameInstructions.size() - 1; 288*0b57cec5SDimitry Andric } 289*0b57cec5SDimitry Andric 290*0b57cec5SDimitry Andric /// This discards all of the MachineBasicBlock numbers and recomputes them. 291*0b57cec5SDimitry Andric /// This guarantees that the MBB numbers are sequential, dense, and match the 292*0b57cec5SDimitry Andric /// ordering of the blocks within the function. If a specific MachineBasicBlock 293*0b57cec5SDimitry Andric /// is specified, only that block and those after it are renumbered. 294*0b57cec5SDimitry Andric void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 295*0b57cec5SDimitry Andric if (empty()) { MBBNumbering.clear(); return; } 296*0b57cec5SDimitry Andric MachineFunction::iterator MBBI, E = end(); 297*0b57cec5SDimitry Andric if (MBB == nullptr) 298*0b57cec5SDimitry Andric MBBI = begin(); 299*0b57cec5SDimitry Andric else 300*0b57cec5SDimitry Andric MBBI = MBB->getIterator(); 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric // Figure out the block number this should have. 303*0b57cec5SDimitry Andric unsigned BlockNo = 0; 304*0b57cec5SDimitry Andric if (MBBI != begin()) 305*0b57cec5SDimitry Andric BlockNo = std::prev(MBBI)->getNumber() + 1; 306*0b57cec5SDimitry Andric 307*0b57cec5SDimitry Andric for (; MBBI != E; ++MBBI, ++BlockNo) { 308*0b57cec5SDimitry Andric if (MBBI->getNumber() != (int)BlockNo) { 309*0b57cec5SDimitry Andric // Remove use of the old number. 310*0b57cec5SDimitry Andric if (MBBI->getNumber() != -1) { 311*0b57cec5SDimitry Andric assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 312*0b57cec5SDimitry Andric "MBB number mismatch!"); 313*0b57cec5SDimitry Andric MBBNumbering[MBBI->getNumber()] = nullptr; 314*0b57cec5SDimitry Andric } 315*0b57cec5SDimitry Andric 316*0b57cec5SDimitry Andric // If BlockNo is already taken, set that block's number to -1. 317*0b57cec5SDimitry Andric if (MBBNumbering[BlockNo]) 318*0b57cec5SDimitry Andric MBBNumbering[BlockNo]->setNumber(-1); 319*0b57cec5SDimitry Andric 320*0b57cec5SDimitry Andric MBBNumbering[BlockNo] = &*MBBI; 321*0b57cec5SDimitry Andric MBBI->setNumber(BlockNo); 322*0b57cec5SDimitry Andric } 323*0b57cec5SDimitry Andric } 324*0b57cec5SDimitry Andric 325*0b57cec5SDimitry Andric // Okay, all the blocks are renumbered. If we have compactified the block 326*0b57cec5SDimitry Andric // numbering, shrink MBBNumbering now. 327*0b57cec5SDimitry Andric assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 328*0b57cec5SDimitry Andric MBBNumbering.resize(BlockNo); 329*0b57cec5SDimitry Andric } 330*0b57cec5SDimitry Andric 3315ffd83dbSDimitry Andric /// This method iterates over the basic blocks and assigns their IsBeginSection 3325ffd83dbSDimitry Andric /// and IsEndSection fields. This must be called after MBB layout is finalized 3335ffd83dbSDimitry Andric /// and the SectionID's are assigned to MBBs. 3345ffd83dbSDimitry Andric void MachineFunction::assignBeginEndSections() { 3355ffd83dbSDimitry Andric front().setIsBeginSection(); 3365ffd83dbSDimitry Andric auto CurrentSectionID = front().getSectionID(); 3375ffd83dbSDimitry Andric for (auto MBBI = std::next(begin()), E = end(); MBBI != E; ++MBBI) { 3385ffd83dbSDimitry Andric if (MBBI->getSectionID() == CurrentSectionID) 3395ffd83dbSDimitry Andric continue; 3405ffd83dbSDimitry Andric MBBI->setIsBeginSection(); 3415ffd83dbSDimitry Andric std::prev(MBBI)->setIsEndSection(); 3425ffd83dbSDimitry Andric CurrentSectionID = MBBI->getSectionID(); 3435ffd83dbSDimitry Andric } 3445ffd83dbSDimitry Andric back().setIsEndSection(); 3455ffd83dbSDimitry Andric } 3465ffd83dbSDimitry Andric 347*0b57cec5SDimitry Andric /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. 348*0b57cec5SDimitry Andric MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 349*0b57cec5SDimitry Andric const DebugLoc &DL, 350e8d8bef9SDimitry Andric bool NoImplicit) { 351*0b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 352e8d8bef9SDimitry Andric MachineInstr(*this, MCID, DL, NoImplicit); 353*0b57cec5SDimitry Andric } 354*0b57cec5SDimitry Andric 355*0b57cec5SDimitry Andric /// Create a new MachineInstr which is a copy of the 'Orig' instruction, 356*0b57cec5SDimitry Andric /// identical in all ways except the instruction has no parent, prev, or next. 357*0b57cec5SDimitry Andric MachineInstr * 358*0b57cec5SDimitry Andric MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 359*0b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 360*0b57cec5SDimitry Andric MachineInstr(*this, *Orig); 361*0b57cec5SDimitry Andric } 362*0b57cec5SDimitry Andric 363*0b57cec5SDimitry Andric MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB, 364*0b57cec5SDimitry Andric MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) { 365*0b57cec5SDimitry Andric MachineInstr *FirstClone = nullptr; 366*0b57cec5SDimitry Andric MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); 367*0b57cec5SDimitry Andric while (true) { 368*0b57cec5SDimitry Andric MachineInstr *Cloned = CloneMachineInstr(&*I); 369*0b57cec5SDimitry Andric MBB.insert(InsertBefore, Cloned); 370*0b57cec5SDimitry Andric if (FirstClone == nullptr) { 371*0b57cec5SDimitry Andric FirstClone = Cloned; 372*0b57cec5SDimitry Andric } else { 373*0b57cec5SDimitry Andric Cloned->bundleWithPred(); 374*0b57cec5SDimitry Andric } 375*0b57cec5SDimitry Andric 376*0b57cec5SDimitry Andric if (!I->isBundledWithSucc()) 377*0b57cec5SDimitry Andric break; 378*0b57cec5SDimitry Andric ++I; 379*0b57cec5SDimitry Andric } 3805ffd83dbSDimitry Andric // Copy over call site info to the cloned instruction if needed. If Orig is in 3815ffd83dbSDimitry Andric // a bundle, copyCallSiteInfo takes care of finding the call instruction in 3825ffd83dbSDimitry Andric // the bundle. 3835ffd83dbSDimitry Andric if (Orig.shouldUpdateCallSiteInfo()) 3845ffd83dbSDimitry Andric copyCallSiteInfo(&Orig, FirstClone); 385*0b57cec5SDimitry Andric return *FirstClone; 386*0b57cec5SDimitry Andric } 387*0b57cec5SDimitry Andric 388*0b57cec5SDimitry Andric /// Delete the given MachineInstr. 389*0b57cec5SDimitry Andric /// 390*0b57cec5SDimitry Andric /// This function also serves as the MachineInstr destructor - the real 391*0b57cec5SDimitry Andric /// ~MachineInstr() destructor must be empty. 392*0b57cec5SDimitry Andric void 393*0b57cec5SDimitry Andric MachineFunction::DeleteMachineInstr(MachineInstr *MI) { 394*0b57cec5SDimitry Andric // Verify that a call site info is at valid state. This assertion should 395*0b57cec5SDimitry Andric // be triggered during the implementation of support for the 396*0b57cec5SDimitry Andric // call site info of a new architecture. If the assertion is triggered, 397*0b57cec5SDimitry Andric // back trace will tell where to insert a call to updateCallSiteInfo(). 3985ffd83dbSDimitry Andric assert((!MI->isCandidateForCallSiteEntry() || 399*0b57cec5SDimitry Andric CallSitesInfo.find(MI) == CallSitesInfo.end()) && 400*0b57cec5SDimitry Andric "Call site info was not updated!"); 401*0b57cec5SDimitry Andric // Strip it for parts. The operand array and the MI object itself are 402*0b57cec5SDimitry Andric // independently recyclable. 403*0b57cec5SDimitry Andric if (MI->Operands) 404*0b57cec5SDimitry Andric deallocateOperandArray(MI->CapOperands, MI->Operands); 405*0b57cec5SDimitry Andric // Don't call ~MachineInstr() which must be trivial anyway because 406*0b57cec5SDimitry Andric // ~MachineFunction drops whole lists of MachineInstrs wihout calling their 407*0b57cec5SDimitry Andric // destructors. 408*0b57cec5SDimitry Andric InstructionRecycler.Deallocate(Allocator, MI); 409*0b57cec5SDimitry Andric } 410*0b57cec5SDimitry Andric 411*0b57cec5SDimitry Andric /// Allocate a new MachineBasicBlock. Use this instead of 412*0b57cec5SDimitry Andric /// `new MachineBasicBlock'. 413*0b57cec5SDimitry Andric MachineBasicBlock * 414*0b57cec5SDimitry Andric MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 415*0b57cec5SDimitry Andric return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 416*0b57cec5SDimitry Andric MachineBasicBlock(*this, bb); 417*0b57cec5SDimitry Andric } 418*0b57cec5SDimitry Andric 419*0b57cec5SDimitry Andric /// Delete the given MachineBasicBlock. 420*0b57cec5SDimitry Andric void 421*0b57cec5SDimitry Andric MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 422*0b57cec5SDimitry Andric assert(MBB->getParent() == this && "MBB parent mismatch!"); 423e8d8bef9SDimitry Andric // Clean up any references to MBB in jump tables before deleting it. 424e8d8bef9SDimitry Andric if (JumpTableInfo) 425e8d8bef9SDimitry Andric JumpTableInfo->RemoveMBBFromJumpTables(MBB); 426*0b57cec5SDimitry Andric MBB->~MachineBasicBlock(); 427*0b57cec5SDimitry Andric BasicBlockRecycler.Deallocate(Allocator, MBB); 428*0b57cec5SDimitry Andric } 429*0b57cec5SDimitry Andric 430*0b57cec5SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 431*0b57cec5SDimitry Andric MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, 4325ffd83dbSDimitry Andric Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 433*0b57cec5SDimitry Andric SyncScope::ID SSID, AtomicOrdering Ordering, 434*0b57cec5SDimitry Andric AtomicOrdering FailureOrdering) { 435*0b57cec5SDimitry Andric return new (Allocator) 436*0b57cec5SDimitry Andric MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, 437*0b57cec5SDimitry Andric SSID, Ordering, FailureOrdering); 438*0b57cec5SDimitry Andric } 439*0b57cec5SDimitry Andric 440e8d8bef9SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 441e8d8bef9SDimitry Andric const MachineMemOperand *MMO, MachinePointerInfo &PtrInfo, uint64_t Size) { 442e8d8bef9SDimitry Andric return new (Allocator) MachineMemOperand( 443e8d8bef9SDimitry Andric PtrInfo, MMO->getFlags(), Size, MMO->getBaseAlign(), AAMDNodes(), nullptr, 444e8d8bef9SDimitry Andric MMO->getSyncScopeID(), MMO->getOrdering(), MMO->getFailureOrdering()); 445e8d8bef9SDimitry Andric } 446e8d8bef9SDimitry Andric 447*0b57cec5SDimitry Andric MachineMemOperand * 448*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 449*0b57cec5SDimitry Andric int64_t Offset, uint64_t Size) { 450*0b57cec5SDimitry Andric const MachinePointerInfo &PtrInfo = MMO->getPointerInfo(); 451*0b57cec5SDimitry Andric 452*0b57cec5SDimitry Andric // If there is no pointer value, the offset isn't tracked so we need to adjust 453*0b57cec5SDimitry Andric // the base alignment. 4545ffd83dbSDimitry Andric Align Alignment = PtrInfo.V.isNull() 4555ffd83dbSDimitry Andric ? commonAlignment(MMO->getBaseAlign(), Offset) 4565ffd83dbSDimitry Andric : MMO->getBaseAlign(); 457*0b57cec5SDimitry Andric 458e8d8bef9SDimitry Andric // Do not preserve ranges, since we don't necessarily know what the high bits 459e8d8bef9SDimitry Andric // are anymore. 460*0b57cec5SDimitry Andric return new (Allocator) 461*0b57cec5SDimitry Andric MachineMemOperand(PtrInfo.getWithOffset(Offset), MMO->getFlags(), Size, 462e8d8bef9SDimitry Andric Alignment, MMO->getAAInfo(), nullptr, MMO->getSyncScopeID(), 463*0b57cec5SDimitry Andric MMO->getOrdering(), MMO->getFailureOrdering()); 464*0b57cec5SDimitry Andric } 465*0b57cec5SDimitry Andric 466*0b57cec5SDimitry Andric MachineMemOperand * 467*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 468*0b57cec5SDimitry Andric const AAMDNodes &AAInfo) { 469*0b57cec5SDimitry Andric MachinePointerInfo MPI = MMO->getValue() ? 470*0b57cec5SDimitry Andric MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : 471*0b57cec5SDimitry Andric MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); 472*0b57cec5SDimitry Andric 4735ffd83dbSDimitry Andric return new (Allocator) MachineMemOperand( 4745ffd83dbSDimitry Andric MPI, MMO->getFlags(), MMO->getSize(), MMO->getBaseAlign(), AAInfo, 4755ffd83dbSDimitry Andric MMO->getRanges(), MMO->getSyncScopeID(), MMO->getOrdering(), 4765ffd83dbSDimitry Andric MMO->getFailureOrdering()); 477*0b57cec5SDimitry Andric } 478*0b57cec5SDimitry Andric 479*0b57cec5SDimitry Andric MachineMemOperand * 480*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 481*0b57cec5SDimitry Andric MachineMemOperand::Flags Flags) { 482*0b57cec5SDimitry Andric return new (Allocator) MachineMemOperand( 4835ffd83dbSDimitry Andric MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlign(), 484*0b57cec5SDimitry Andric MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(), 485*0b57cec5SDimitry Andric MMO->getOrdering(), MMO->getFailureOrdering()); 486*0b57cec5SDimitry Andric } 487*0b57cec5SDimitry Andric 488480093f4SDimitry Andric MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfo( 489c14a5a88SDimitry Andric ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol, 490c14a5a88SDimitry Andric MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) { 491c14a5a88SDimitry Andric return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, 492c14a5a88SDimitry Andric PostInstrSymbol, HeapAllocMarker); 493*0b57cec5SDimitry Andric } 494*0b57cec5SDimitry Andric 495*0b57cec5SDimitry Andric const char *MachineFunction::createExternalSymbolName(StringRef Name) { 496*0b57cec5SDimitry Andric char *Dest = Allocator.Allocate<char>(Name.size() + 1); 497*0b57cec5SDimitry Andric llvm::copy(Name, Dest); 498*0b57cec5SDimitry Andric Dest[Name.size()] = 0; 499*0b57cec5SDimitry Andric return Dest; 500*0b57cec5SDimitry Andric } 501*0b57cec5SDimitry Andric 502*0b57cec5SDimitry Andric uint32_t *MachineFunction::allocateRegMask() { 503*0b57cec5SDimitry Andric unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); 504*0b57cec5SDimitry Andric unsigned Size = MachineOperand::getRegMaskSize(NumRegs); 505*0b57cec5SDimitry Andric uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); 506*0b57cec5SDimitry Andric memset(Mask, 0, Size * sizeof(Mask[0])); 507*0b57cec5SDimitry Andric return Mask; 508*0b57cec5SDimitry Andric } 509*0b57cec5SDimitry Andric 510480093f4SDimitry Andric ArrayRef<int> MachineFunction::allocateShuffleMask(ArrayRef<int> Mask) { 511480093f4SDimitry Andric int* AllocMask = Allocator.Allocate<int>(Mask.size()); 512480093f4SDimitry Andric copy(Mask, AllocMask); 513480093f4SDimitry Andric return {AllocMask, Mask.size()}; 514480093f4SDimitry Andric } 515480093f4SDimitry Andric 516*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 517*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineFunction::dump() const { 518*0b57cec5SDimitry Andric print(dbgs()); 519*0b57cec5SDimitry Andric } 520*0b57cec5SDimitry Andric #endif 521*0b57cec5SDimitry Andric 522*0b57cec5SDimitry Andric StringRef MachineFunction::getName() const { 523*0b57cec5SDimitry Andric return getFunction().getName(); 524*0b57cec5SDimitry Andric } 525*0b57cec5SDimitry Andric 526*0b57cec5SDimitry Andric void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { 527*0b57cec5SDimitry Andric OS << "# Machine code for function " << getName() << ": "; 528*0b57cec5SDimitry Andric getProperties().print(OS); 529*0b57cec5SDimitry Andric OS << '\n'; 530*0b57cec5SDimitry Andric 531*0b57cec5SDimitry Andric // Print Frame Information 532*0b57cec5SDimitry Andric FrameInfo->print(*this, OS); 533*0b57cec5SDimitry Andric 534*0b57cec5SDimitry Andric // Print JumpTable Information 535*0b57cec5SDimitry Andric if (JumpTableInfo) 536*0b57cec5SDimitry Andric JumpTableInfo->print(OS); 537*0b57cec5SDimitry Andric 538*0b57cec5SDimitry Andric // Print Constant Pool 539*0b57cec5SDimitry Andric ConstantPool->print(OS); 540*0b57cec5SDimitry Andric 541*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); 542*0b57cec5SDimitry Andric 543*0b57cec5SDimitry Andric if (RegInfo && !RegInfo->livein_empty()) { 544*0b57cec5SDimitry Andric OS << "Function Live Ins: "; 545*0b57cec5SDimitry Andric for (MachineRegisterInfo::livein_iterator 546*0b57cec5SDimitry Andric I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 547*0b57cec5SDimitry Andric OS << printReg(I->first, TRI); 548*0b57cec5SDimitry Andric if (I->second) 549*0b57cec5SDimitry Andric OS << " in " << printReg(I->second, TRI); 550*0b57cec5SDimitry Andric if (std::next(I) != E) 551*0b57cec5SDimitry Andric OS << ", "; 552*0b57cec5SDimitry Andric } 553*0b57cec5SDimitry Andric OS << '\n'; 554*0b57cec5SDimitry Andric } 555*0b57cec5SDimitry Andric 556*0b57cec5SDimitry Andric ModuleSlotTracker MST(getFunction().getParent()); 557*0b57cec5SDimitry Andric MST.incorporateFunction(getFunction()); 558*0b57cec5SDimitry Andric for (const auto &BB : *this) { 559*0b57cec5SDimitry Andric OS << '\n'; 560*0b57cec5SDimitry Andric // If we print the whole function, print it at its most verbose level. 561*0b57cec5SDimitry Andric BB.print(OS, MST, Indexes, /*IsStandalone=*/true); 562*0b57cec5SDimitry Andric } 563*0b57cec5SDimitry Andric 564*0b57cec5SDimitry Andric OS << "\n# End machine code for function " << getName() << ".\n\n"; 565*0b57cec5SDimitry Andric } 566*0b57cec5SDimitry Andric 567480093f4SDimitry Andric /// True if this function needs frame moves for debug or exceptions. 568480093f4SDimitry Andric bool MachineFunction::needsFrameMoves() const { 569480093f4SDimitry Andric return getMMI().hasDebugInfo() || 570480093f4SDimitry Andric getTarget().Options.ForceDwarfFrameSection || 571480093f4SDimitry Andric F.needsUnwindTableEntry(); 572480093f4SDimitry Andric } 573480093f4SDimitry Andric 574*0b57cec5SDimitry Andric namespace llvm { 575*0b57cec5SDimitry Andric 576*0b57cec5SDimitry Andric template<> 577*0b57cec5SDimitry Andric struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 578*0b57cec5SDimitry Andric DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 579*0b57cec5SDimitry Andric 580*0b57cec5SDimitry Andric static std::string getGraphName(const MachineFunction *F) { 581*0b57cec5SDimitry Andric return ("CFG for '" + F->getName() + "' function").str(); 582*0b57cec5SDimitry Andric } 583*0b57cec5SDimitry Andric 584*0b57cec5SDimitry Andric std::string getNodeLabel(const MachineBasicBlock *Node, 585*0b57cec5SDimitry Andric const MachineFunction *Graph) { 586*0b57cec5SDimitry Andric std::string OutStr; 587*0b57cec5SDimitry Andric { 588*0b57cec5SDimitry Andric raw_string_ostream OSS(OutStr); 589*0b57cec5SDimitry Andric 590*0b57cec5SDimitry Andric if (isSimple()) { 591*0b57cec5SDimitry Andric OSS << printMBBReference(*Node); 592*0b57cec5SDimitry Andric if (const BasicBlock *BB = Node->getBasicBlock()) 593*0b57cec5SDimitry Andric OSS << ": " << BB->getName(); 594*0b57cec5SDimitry Andric } else 595*0b57cec5SDimitry Andric Node->print(OSS); 596*0b57cec5SDimitry Andric } 597*0b57cec5SDimitry Andric 598*0b57cec5SDimitry Andric if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 599*0b57cec5SDimitry Andric 600*0b57cec5SDimitry Andric // Process string output to make it nicer... 601*0b57cec5SDimitry Andric for (unsigned i = 0; i != OutStr.length(); ++i) 602*0b57cec5SDimitry Andric if (OutStr[i] == '\n') { // Left justify 603*0b57cec5SDimitry Andric OutStr[i] = '\\'; 604*0b57cec5SDimitry Andric OutStr.insert(OutStr.begin()+i+1, 'l'); 605*0b57cec5SDimitry Andric } 606*0b57cec5SDimitry Andric return OutStr; 607*0b57cec5SDimitry Andric } 608*0b57cec5SDimitry Andric }; 609*0b57cec5SDimitry Andric 610*0b57cec5SDimitry Andric } // end namespace llvm 611*0b57cec5SDimitry Andric 612*0b57cec5SDimitry Andric void MachineFunction::viewCFG() const 613*0b57cec5SDimitry Andric { 614*0b57cec5SDimitry Andric #ifndef NDEBUG 615*0b57cec5SDimitry Andric ViewGraph(this, "mf" + getName()); 616*0b57cec5SDimitry Andric #else 617*0b57cec5SDimitry Andric errs() << "MachineFunction::viewCFG is only available in debug builds on " 618*0b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 619*0b57cec5SDimitry Andric #endif // NDEBUG 620*0b57cec5SDimitry Andric } 621*0b57cec5SDimitry Andric 622*0b57cec5SDimitry Andric void MachineFunction::viewCFGOnly() const 623*0b57cec5SDimitry Andric { 624*0b57cec5SDimitry Andric #ifndef NDEBUG 625*0b57cec5SDimitry Andric ViewGraph(this, "mf" + getName(), true); 626*0b57cec5SDimitry Andric #else 627*0b57cec5SDimitry Andric errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 628*0b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 629*0b57cec5SDimitry Andric #endif // NDEBUG 630*0b57cec5SDimitry Andric } 631*0b57cec5SDimitry Andric 632*0b57cec5SDimitry Andric /// Add the specified physical register as a live-in value and 633*0b57cec5SDimitry Andric /// create a corresponding virtual register for it. 6345ffd83dbSDimitry Andric Register MachineFunction::addLiveIn(MCRegister PReg, 635*0b57cec5SDimitry Andric const TargetRegisterClass *RC) { 636*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = getRegInfo(); 6375ffd83dbSDimitry Andric Register VReg = MRI.getLiveInVirtReg(PReg); 638*0b57cec5SDimitry Andric if (VReg) { 639*0b57cec5SDimitry Andric const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); 640*0b57cec5SDimitry Andric (void)VRegRC; 641*0b57cec5SDimitry Andric // A physical register can be added several times. 642*0b57cec5SDimitry Andric // Between two calls, the register class of the related virtual register 643*0b57cec5SDimitry Andric // may have been constrained to match some operation constraints. 644*0b57cec5SDimitry Andric // In that case, check that the current register class includes the 645*0b57cec5SDimitry Andric // physical register and is a sub class of the specified RC. 646*0b57cec5SDimitry Andric assert((VRegRC == RC || (VRegRC->contains(PReg) && 647*0b57cec5SDimitry Andric RC->hasSubClassEq(VRegRC))) && 648*0b57cec5SDimitry Andric "Register class mismatch!"); 649*0b57cec5SDimitry Andric return VReg; 650*0b57cec5SDimitry Andric } 651*0b57cec5SDimitry Andric VReg = MRI.createVirtualRegister(RC); 652*0b57cec5SDimitry Andric MRI.addLiveIn(PReg, VReg); 653*0b57cec5SDimitry Andric return VReg; 654*0b57cec5SDimitry Andric } 655*0b57cec5SDimitry Andric 656*0b57cec5SDimitry Andric /// Return the MCSymbol for the specified non-empty jump table. 657*0b57cec5SDimitry Andric /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 658*0b57cec5SDimitry Andric /// normal 'L' label is returned. 659*0b57cec5SDimitry Andric MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 660*0b57cec5SDimitry Andric bool isLinkerPrivate) const { 661*0b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 662*0b57cec5SDimitry Andric assert(JumpTableInfo && "No jump tables"); 663*0b57cec5SDimitry Andric assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 664*0b57cec5SDimitry Andric 665*0b57cec5SDimitry Andric StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() 666*0b57cec5SDimitry Andric : DL.getPrivateGlobalPrefix(); 667*0b57cec5SDimitry Andric SmallString<60> Name; 668*0b57cec5SDimitry Andric raw_svector_ostream(Name) 669*0b57cec5SDimitry Andric << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 670*0b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Name); 671*0b57cec5SDimitry Andric } 672*0b57cec5SDimitry Andric 673*0b57cec5SDimitry Andric /// Return a function-local symbol to represent the PIC base. 674*0b57cec5SDimitry Andric MCSymbol *MachineFunction::getPICBaseSymbol() const { 675*0b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 676*0b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 677*0b57cec5SDimitry Andric Twine(getFunctionNumber()) + "$pb"); 678*0b57cec5SDimitry Andric } 679*0b57cec5SDimitry Andric 680*0b57cec5SDimitry Andric /// \name Exception Handling 681*0b57cec5SDimitry Andric /// \{ 682*0b57cec5SDimitry Andric 683*0b57cec5SDimitry Andric LandingPadInfo & 684*0b57cec5SDimitry Andric MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { 685*0b57cec5SDimitry Andric unsigned N = LandingPads.size(); 686*0b57cec5SDimitry Andric for (unsigned i = 0; i < N; ++i) { 687*0b57cec5SDimitry Andric LandingPadInfo &LP = LandingPads[i]; 688*0b57cec5SDimitry Andric if (LP.LandingPadBlock == LandingPad) 689*0b57cec5SDimitry Andric return LP; 690*0b57cec5SDimitry Andric } 691*0b57cec5SDimitry Andric 692*0b57cec5SDimitry Andric LandingPads.push_back(LandingPadInfo(LandingPad)); 693*0b57cec5SDimitry Andric return LandingPads[N]; 694*0b57cec5SDimitry Andric } 695*0b57cec5SDimitry Andric 696*0b57cec5SDimitry Andric void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, 697*0b57cec5SDimitry Andric MCSymbol *BeginLabel, MCSymbol *EndLabel) { 698*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 699*0b57cec5SDimitry Andric LP.BeginLabels.push_back(BeginLabel); 700*0b57cec5SDimitry Andric LP.EndLabels.push_back(EndLabel); 701*0b57cec5SDimitry Andric } 702*0b57cec5SDimitry Andric 703*0b57cec5SDimitry Andric MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { 704*0b57cec5SDimitry Andric MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); 705*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 706*0b57cec5SDimitry Andric LP.LandingPadLabel = LandingPadLabel; 707*0b57cec5SDimitry Andric 708*0b57cec5SDimitry Andric const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); 709*0b57cec5SDimitry Andric if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { 710*0b57cec5SDimitry Andric if (const auto *PF = 711*0b57cec5SDimitry Andric dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts())) 712*0b57cec5SDimitry Andric getMMI().addPersonality(PF); 713*0b57cec5SDimitry Andric 714*0b57cec5SDimitry Andric if (LPI->isCleanup()) 715*0b57cec5SDimitry Andric addCleanup(LandingPad); 716*0b57cec5SDimitry Andric 717*0b57cec5SDimitry Andric // FIXME: New EH - Add the clauses in reverse order. This isn't 100% 718*0b57cec5SDimitry Andric // correct, but we need to do it this way because of how the DWARF EH 719*0b57cec5SDimitry Andric // emitter processes the clauses. 720*0b57cec5SDimitry Andric for (unsigned I = LPI->getNumClauses(); I != 0; --I) { 721*0b57cec5SDimitry Andric Value *Val = LPI->getClause(I - 1); 722*0b57cec5SDimitry Andric if (LPI->isCatch(I - 1)) { 723*0b57cec5SDimitry Andric addCatchTypeInfo(LandingPad, 724*0b57cec5SDimitry Andric dyn_cast<GlobalValue>(Val->stripPointerCasts())); 725*0b57cec5SDimitry Andric } else { 726*0b57cec5SDimitry Andric // Add filters in a list. 727*0b57cec5SDimitry Andric auto *CVal = cast<Constant>(Val); 728*0b57cec5SDimitry Andric SmallVector<const GlobalValue *, 4> FilterList; 729*0b57cec5SDimitry Andric for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end(); 730*0b57cec5SDimitry Andric II != IE; ++II) 731*0b57cec5SDimitry Andric FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); 732*0b57cec5SDimitry Andric 733*0b57cec5SDimitry Andric addFilterTypeInfo(LandingPad, FilterList); 734*0b57cec5SDimitry Andric } 735*0b57cec5SDimitry Andric } 736*0b57cec5SDimitry Andric 737*0b57cec5SDimitry Andric } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { 738*0b57cec5SDimitry Andric for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) { 739*0b57cec5SDimitry Andric Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); 740*0b57cec5SDimitry Andric addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo)); 741*0b57cec5SDimitry Andric } 742*0b57cec5SDimitry Andric 743*0b57cec5SDimitry Andric } else { 744*0b57cec5SDimitry Andric assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!"); 745*0b57cec5SDimitry Andric } 746*0b57cec5SDimitry Andric 747*0b57cec5SDimitry Andric return LandingPadLabel; 748*0b57cec5SDimitry Andric } 749*0b57cec5SDimitry Andric 750*0b57cec5SDimitry Andric void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, 751*0b57cec5SDimitry Andric ArrayRef<const GlobalValue *> TyInfo) { 752*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 753*0b57cec5SDimitry Andric for (unsigned N = TyInfo.size(); N; --N) 754*0b57cec5SDimitry Andric LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1])); 755*0b57cec5SDimitry Andric } 756*0b57cec5SDimitry Andric 757*0b57cec5SDimitry Andric void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, 758*0b57cec5SDimitry Andric ArrayRef<const GlobalValue *> TyInfo) { 759*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 760*0b57cec5SDimitry Andric std::vector<unsigned> IdsInFilter(TyInfo.size()); 761*0b57cec5SDimitry Andric for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 762*0b57cec5SDimitry Andric IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 763*0b57cec5SDimitry Andric LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 764*0b57cec5SDimitry Andric } 765*0b57cec5SDimitry Andric 766*0b57cec5SDimitry Andric void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap, 767*0b57cec5SDimitry Andric bool TidyIfNoBeginLabels) { 768*0b57cec5SDimitry Andric for (unsigned i = 0; i != LandingPads.size(); ) { 769*0b57cec5SDimitry Andric LandingPadInfo &LandingPad = LandingPads[i]; 770*0b57cec5SDimitry Andric if (LandingPad.LandingPadLabel && 771*0b57cec5SDimitry Andric !LandingPad.LandingPadLabel->isDefined() && 772*0b57cec5SDimitry Andric (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) 773*0b57cec5SDimitry Andric LandingPad.LandingPadLabel = nullptr; 774*0b57cec5SDimitry Andric 775*0b57cec5SDimitry Andric // Special case: we *should* emit LPs with null LP MBB. This indicates 776*0b57cec5SDimitry Andric // "nounwind" case. 777*0b57cec5SDimitry Andric if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 778*0b57cec5SDimitry Andric LandingPads.erase(LandingPads.begin() + i); 779*0b57cec5SDimitry Andric continue; 780*0b57cec5SDimitry Andric } 781*0b57cec5SDimitry Andric 782*0b57cec5SDimitry Andric if (TidyIfNoBeginLabels) { 783*0b57cec5SDimitry Andric for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { 784*0b57cec5SDimitry Andric MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; 785*0b57cec5SDimitry Andric MCSymbol *EndLabel = LandingPad.EndLabels[j]; 786*0b57cec5SDimitry Andric if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && 787*0b57cec5SDimitry Andric (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) 788*0b57cec5SDimitry Andric continue; 789*0b57cec5SDimitry Andric 790*0b57cec5SDimitry Andric LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 791*0b57cec5SDimitry Andric LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 792*0b57cec5SDimitry Andric --j; 793*0b57cec5SDimitry Andric --e; 794*0b57cec5SDimitry Andric } 795*0b57cec5SDimitry Andric 796*0b57cec5SDimitry Andric // Remove landing pads with no try-ranges. 797*0b57cec5SDimitry Andric if (LandingPads[i].BeginLabels.empty()) { 798*0b57cec5SDimitry Andric LandingPads.erase(LandingPads.begin() + i); 799*0b57cec5SDimitry Andric continue; 800*0b57cec5SDimitry Andric } 801*0b57cec5SDimitry Andric } 802*0b57cec5SDimitry Andric 803*0b57cec5SDimitry Andric // If there is no landing pad, ensure that the list of typeids is empty. 804*0b57cec5SDimitry Andric // If the only typeid is a cleanup, this is the same as having no typeids. 805*0b57cec5SDimitry Andric if (!LandingPad.LandingPadBlock || 806*0b57cec5SDimitry Andric (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 807*0b57cec5SDimitry Andric LandingPad.TypeIds.clear(); 808*0b57cec5SDimitry Andric ++i; 809*0b57cec5SDimitry Andric } 810*0b57cec5SDimitry Andric } 811*0b57cec5SDimitry Andric 812*0b57cec5SDimitry Andric void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { 813*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 814*0b57cec5SDimitry Andric LP.TypeIds.push_back(0); 815*0b57cec5SDimitry Andric } 816*0b57cec5SDimitry Andric 817*0b57cec5SDimitry Andric void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad, 818*0b57cec5SDimitry Andric const Function *Filter, 819*0b57cec5SDimitry Andric const BlockAddress *RecoverBA) { 820*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 821*0b57cec5SDimitry Andric SEHHandler Handler; 822*0b57cec5SDimitry Andric Handler.FilterOrFinally = Filter; 823*0b57cec5SDimitry Andric Handler.RecoverBA = RecoverBA; 824*0b57cec5SDimitry Andric LP.SEHHandlers.push_back(Handler); 825*0b57cec5SDimitry Andric } 826*0b57cec5SDimitry Andric 827*0b57cec5SDimitry Andric void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad, 828*0b57cec5SDimitry Andric const Function *Cleanup) { 829*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 830*0b57cec5SDimitry Andric SEHHandler Handler; 831*0b57cec5SDimitry Andric Handler.FilterOrFinally = Cleanup; 832*0b57cec5SDimitry Andric Handler.RecoverBA = nullptr; 833*0b57cec5SDimitry Andric LP.SEHHandlers.push_back(Handler); 834*0b57cec5SDimitry Andric } 835*0b57cec5SDimitry Andric 836*0b57cec5SDimitry Andric void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, 837*0b57cec5SDimitry Andric ArrayRef<unsigned> Sites) { 838*0b57cec5SDimitry Andric LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); 839*0b57cec5SDimitry Andric } 840*0b57cec5SDimitry Andric 841*0b57cec5SDimitry Andric unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { 842*0b57cec5SDimitry Andric for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 843*0b57cec5SDimitry Andric if (TypeInfos[i] == TI) return i + 1; 844*0b57cec5SDimitry Andric 845*0b57cec5SDimitry Andric TypeInfos.push_back(TI); 846*0b57cec5SDimitry Andric return TypeInfos.size(); 847*0b57cec5SDimitry Andric } 848*0b57cec5SDimitry Andric 849*0b57cec5SDimitry Andric int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) { 850*0b57cec5SDimitry Andric // If the new filter coincides with the tail of an existing filter, then 851*0b57cec5SDimitry Andric // re-use the existing filter. Folding filters more than this requires 852*0b57cec5SDimitry Andric // re-ordering filters and/or their elements - probably not worth it. 853*0b57cec5SDimitry Andric for (std::vector<unsigned>::iterator I = FilterEnds.begin(), 854*0b57cec5SDimitry Andric E = FilterEnds.end(); I != E; ++I) { 855*0b57cec5SDimitry Andric unsigned i = *I, j = TyIds.size(); 856*0b57cec5SDimitry Andric 857*0b57cec5SDimitry Andric while (i && j) 858*0b57cec5SDimitry Andric if (FilterIds[--i] != TyIds[--j]) 859*0b57cec5SDimitry Andric goto try_next; 860*0b57cec5SDimitry Andric 861*0b57cec5SDimitry Andric if (!j) 862*0b57cec5SDimitry Andric // The new filter coincides with range [i, end) of the existing filter. 863*0b57cec5SDimitry Andric return -(1 + i); 864*0b57cec5SDimitry Andric 865*0b57cec5SDimitry Andric try_next:; 866*0b57cec5SDimitry Andric } 867*0b57cec5SDimitry Andric 868*0b57cec5SDimitry Andric // Add the new filter. 869*0b57cec5SDimitry Andric int FilterID = -(1 + FilterIds.size()); 870*0b57cec5SDimitry Andric FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 871e8d8bef9SDimitry Andric llvm::append_range(FilterIds, TyIds); 872*0b57cec5SDimitry Andric FilterEnds.push_back(FilterIds.size()); 873*0b57cec5SDimitry Andric FilterIds.push_back(0); // terminator 874*0b57cec5SDimitry Andric return FilterID; 875*0b57cec5SDimitry Andric } 876*0b57cec5SDimitry Andric 877480093f4SDimitry Andric MachineFunction::CallSiteInfoMap::iterator 878480093f4SDimitry Andric MachineFunction::getCallSiteInfo(const MachineInstr *MI) { 8795ffd83dbSDimitry Andric assert(MI->isCandidateForCallSiteEntry() && 8805ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates"); 881*0b57cec5SDimitry Andric 8825ffd83dbSDimitry Andric if (!Target.Options.EmitCallSiteInfo) 883480093f4SDimitry Andric return CallSitesInfo.end(); 884480093f4SDimitry Andric return CallSitesInfo.find(MI); 885*0b57cec5SDimitry Andric } 886*0b57cec5SDimitry Andric 8875ffd83dbSDimitry Andric /// Return the call machine instruction or find a call within bundle. 8885ffd83dbSDimitry Andric static const MachineInstr *getCallInstr(const MachineInstr *MI) { 8895ffd83dbSDimitry Andric if (!MI->isBundle()) 8905ffd83dbSDimitry Andric return MI; 891*0b57cec5SDimitry Andric 8925ffd83dbSDimitry Andric for (auto &BMI : make_range(getBundleStart(MI->getIterator()), 8935ffd83dbSDimitry Andric getBundleEnd(MI->getIterator()))) 8945ffd83dbSDimitry Andric if (BMI.isCandidateForCallSiteEntry()) 8955ffd83dbSDimitry Andric return &BMI; 8968bcb0991SDimitry Andric 8975ffd83dbSDimitry Andric llvm_unreachable("Unexpected bundle without a call site candidate"); 8988bcb0991SDimitry Andric } 8998bcb0991SDimitry Andric 9008bcb0991SDimitry Andric void MachineFunction::eraseCallSiteInfo(const MachineInstr *MI) { 9015ffd83dbSDimitry Andric assert(MI->shouldUpdateCallSiteInfo() && 9025ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9035ffd83dbSDimitry Andric "candidates inside bundles"); 9045ffd83dbSDimitry Andric 9055ffd83dbSDimitry Andric const MachineInstr *CallMI = getCallInstr(MI); 9065ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(CallMI); 9078bcb0991SDimitry Andric if (CSIt == CallSitesInfo.end()) 9088bcb0991SDimitry Andric return; 9098bcb0991SDimitry Andric CallSitesInfo.erase(CSIt); 9108bcb0991SDimitry Andric } 9118bcb0991SDimitry Andric 9128bcb0991SDimitry Andric void MachineFunction::copyCallSiteInfo(const MachineInstr *Old, 9138bcb0991SDimitry Andric const MachineInstr *New) { 9145ffd83dbSDimitry Andric assert(Old->shouldUpdateCallSiteInfo() && 9155ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9165ffd83dbSDimitry Andric "candidates inside bundles"); 9178bcb0991SDimitry Andric 9185ffd83dbSDimitry Andric if (!New->isCandidateForCallSiteEntry()) 9195ffd83dbSDimitry Andric return eraseCallSiteInfo(Old); 9205ffd83dbSDimitry Andric 9215ffd83dbSDimitry Andric const MachineInstr *OldCallMI = getCallInstr(Old); 9225ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 9238bcb0991SDimitry Andric if (CSIt == CallSitesInfo.end()) 9248bcb0991SDimitry Andric return; 9258bcb0991SDimitry Andric 9268bcb0991SDimitry Andric CallSiteInfo CSInfo = CSIt->second; 927*0b57cec5SDimitry Andric CallSitesInfo[New] = CSInfo; 928*0b57cec5SDimitry Andric } 929*0b57cec5SDimitry Andric 9305ffd83dbSDimitry Andric void MachineFunction::moveCallSiteInfo(const MachineInstr *Old, 9315ffd83dbSDimitry 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"); 9355ffd83dbSDimitry 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); 9415ffd83dbSDimitry Andric if (CSIt == CallSitesInfo.end()) 9425ffd83dbSDimitry Andric return; 9435ffd83dbSDimitry Andric 9445ffd83dbSDimitry Andric CallSiteInfo CSInfo = std::move(CSIt->second); 9455ffd83dbSDimitry Andric CallSitesInfo.erase(CSIt); 9465ffd83dbSDimitry Andric CallSitesInfo[New] = CSInfo; 9475ffd83dbSDimitry Andric } 9485ffd83dbSDimitry Andric 949e8d8bef9SDimitry Andric void MachineFunction::setDebugInstrNumberingCount(unsigned Num) { 950e8d8bef9SDimitry Andric DebugInstrNumberingCount = Num; 951e8d8bef9SDimitry Andric } 952e8d8bef9SDimitry Andric 953e8d8bef9SDimitry Andric void MachineFunction::makeDebugValueSubstitution(DebugInstrOperandPair A, 954e8d8bef9SDimitry Andric DebugInstrOperandPair B) { 955e8d8bef9SDimitry Andric auto Result = DebugValueSubstitutions.insert(std::make_pair(A, B)); 956e8d8bef9SDimitry Andric (void)Result; 957e8d8bef9SDimitry Andric assert(Result.second && "Substitution for an already substituted value?"); 958e8d8bef9SDimitry Andric } 959e8d8bef9SDimitry Andric 960e8d8bef9SDimitry Andric void MachineFunction::substituteDebugValuesForInst(const MachineInstr &Old, 961e8d8bef9SDimitry Andric MachineInstr &New, 962e8d8bef9SDimitry Andric unsigned MaxOperand) { 963e8d8bef9SDimitry Andric // If the Old instruction wasn't tracked at all, there is no work to do. 964e8d8bef9SDimitry Andric unsigned OldInstrNum = Old.peekDebugInstrNum(); 965e8d8bef9SDimitry Andric if (!OldInstrNum) 966e8d8bef9SDimitry Andric return; 967e8d8bef9SDimitry Andric 968e8d8bef9SDimitry Andric // Iterate over all operands looking for defs to create substitutions for. 969e8d8bef9SDimitry Andric // Avoid creating new instr numbers unless we create a new substitution. 970e8d8bef9SDimitry Andric // While this has no functional effect, it risks confusing someone reading 971e8d8bef9SDimitry Andric // MIR output. 972e8d8bef9SDimitry Andric // Examine all the operands, or the first N specified by the caller. 973e8d8bef9SDimitry Andric MaxOperand = std::min(MaxOperand, Old.getNumOperands()); 974e8d8bef9SDimitry Andric for (unsigned int I = 0; I < Old.getNumOperands(); ++I) { 975e8d8bef9SDimitry Andric const auto &OldMO = Old.getOperand(I); 976e8d8bef9SDimitry Andric auto &NewMO = New.getOperand(I); 977e8d8bef9SDimitry Andric (void)NewMO; 978e8d8bef9SDimitry Andric 979e8d8bef9SDimitry Andric if (!OldMO.isReg() || !OldMO.isDef()) 980e8d8bef9SDimitry Andric continue; 981e8d8bef9SDimitry Andric assert(NewMO.isDef()); 982e8d8bef9SDimitry Andric 983e8d8bef9SDimitry Andric unsigned NewInstrNum = New.getDebugInstrNum(); 984e8d8bef9SDimitry Andric makeDebugValueSubstitution(std::make_pair(OldInstrNum, I), 985e8d8bef9SDimitry Andric std::make_pair(NewInstrNum, I)); 986e8d8bef9SDimitry Andric } 987e8d8bef9SDimitry Andric } 988e8d8bef9SDimitry Andric 989*0b57cec5SDimitry Andric /// \} 990*0b57cec5SDimitry Andric 991*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 992*0b57cec5SDimitry Andric // MachineJumpTableInfo implementation 993*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 994*0b57cec5SDimitry Andric 995*0b57cec5SDimitry Andric /// Return the size of each entry in the jump table. 996*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { 997*0b57cec5SDimitry Andric // The size of a jump table entry is 4 bytes unless the entry is just the 998*0b57cec5SDimitry Andric // address of a block, in which case it is the pointer size. 999*0b57cec5SDimitry Andric switch (getEntryKind()) { 1000*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 1001*0b57cec5SDimitry Andric return TD.getPointerSize(); 1002*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 1003*0b57cec5SDimitry Andric return 8; 1004*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 1005*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 1006*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 1007*0b57cec5SDimitry Andric return 4; 1008*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 1009*0b57cec5SDimitry Andric return 0; 1010*0b57cec5SDimitry Andric } 1011*0b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 1012*0b57cec5SDimitry Andric } 1013*0b57cec5SDimitry Andric 1014*0b57cec5SDimitry Andric /// Return the alignment of each entry in the jump table. 1015*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { 1016*0b57cec5SDimitry Andric // The alignment of a jump table entry is the alignment of int32 unless the 1017*0b57cec5SDimitry Andric // entry is just the address of a block, in which case it is the pointer 1018*0b57cec5SDimitry Andric // alignment. 1019*0b57cec5SDimitry Andric switch (getEntryKind()) { 1020*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 10218bcb0991SDimitry Andric return TD.getPointerABIAlignment(0).value(); 1022*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 10238bcb0991SDimitry Andric return TD.getABIIntegerTypeAlignment(64).value(); 1024*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 1025*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 1026*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 10278bcb0991SDimitry Andric return TD.getABIIntegerTypeAlignment(32).value(); 1028*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 1029*0b57cec5SDimitry Andric return 1; 1030*0b57cec5SDimitry Andric } 1031*0b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 1032*0b57cec5SDimitry Andric } 1033*0b57cec5SDimitry Andric 1034*0b57cec5SDimitry Andric /// Create a new jump table entry in the jump table info. 1035*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::createJumpTableIndex( 1036*0b57cec5SDimitry Andric const std::vector<MachineBasicBlock*> &DestBBs) { 1037*0b57cec5SDimitry Andric assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 1038*0b57cec5SDimitry Andric JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 1039*0b57cec5SDimitry Andric return JumpTables.size()-1; 1040*0b57cec5SDimitry Andric } 1041*0b57cec5SDimitry Andric 1042*0b57cec5SDimitry Andric /// If Old is the target of any jump tables, update the jump tables to branch 1043*0b57cec5SDimitry Andric /// to New instead. 1044*0b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 1045*0b57cec5SDimitry Andric MachineBasicBlock *New) { 1046*0b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 1047*0b57cec5SDimitry Andric bool MadeChange = false; 1048*0b57cec5SDimitry Andric for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 1049*0b57cec5SDimitry Andric ReplaceMBBInJumpTable(i, Old, New); 1050*0b57cec5SDimitry Andric return MadeChange; 1051*0b57cec5SDimitry Andric } 1052*0b57cec5SDimitry Andric 1053e8d8bef9SDimitry Andric /// If MBB is present in any jump tables, remove it. 1054e8d8bef9SDimitry Andric bool MachineJumpTableInfo::RemoveMBBFromJumpTables(MachineBasicBlock *MBB) { 1055e8d8bef9SDimitry Andric bool MadeChange = false; 1056e8d8bef9SDimitry Andric for (MachineJumpTableEntry &JTE : JumpTables) { 1057e8d8bef9SDimitry Andric auto removeBeginItr = std::remove(JTE.MBBs.begin(), JTE.MBBs.end(), MBB); 1058e8d8bef9SDimitry Andric MadeChange |= (removeBeginItr != JTE.MBBs.end()); 1059e8d8bef9SDimitry Andric JTE.MBBs.erase(removeBeginItr, JTE.MBBs.end()); 1060e8d8bef9SDimitry Andric } 1061e8d8bef9SDimitry Andric return MadeChange; 1062e8d8bef9SDimitry Andric } 1063e8d8bef9SDimitry Andric 1064*0b57cec5SDimitry Andric /// If Old is a target of the jump tables, update the jump table to branch to 1065*0b57cec5SDimitry Andric /// New instead. 1066*0b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 1067*0b57cec5SDimitry Andric MachineBasicBlock *Old, 1068*0b57cec5SDimitry Andric MachineBasicBlock *New) { 1069*0b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 1070*0b57cec5SDimitry Andric bool MadeChange = false; 1071*0b57cec5SDimitry Andric MachineJumpTableEntry &JTE = JumpTables[Idx]; 1072*0b57cec5SDimitry Andric for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 1073*0b57cec5SDimitry Andric if (JTE.MBBs[j] == Old) { 1074*0b57cec5SDimitry Andric JTE.MBBs[j] = New; 1075*0b57cec5SDimitry Andric MadeChange = true; 1076*0b57cec5SDimitry Andric } 1077*0b57cec5SDimitry Andric return MadeChange; 1078*0b57cec5SDimitry Andric } 1079*0b57cec5SDimitry Andric 1080*0b57cec5SDimitry Andric void MachineJumpTableInfo::print(raw_ostream &OS) const { 1081*0b57cec5SDimitry Andric if (JumpTables.empty()) return; 1082*0b57cec5SDimitry Andric 1083*0b57cec5SDimitry Andric OS << "Jump Tables:\n"; 1084*0b57cec5SDimitry Andric 1085*0b57cec5SDimitry Andric for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 1086*0b57cec5SDimitry Andric OS << printJumpTableEntryReference(i) << ':'; 1087*0b57cec5SDimitry Andric for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 1088*0b57cec5SDimitry Andric OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]); 1089*0b57cec5SDimitry Andric if (i != e) 1090*0b57cec5SDimitry Andric OS << '\n'; 1091*0b57cec5SDimitry Andric } 1092*0b57cec5SDimitry Andric 1093*0b57cec5SDimitry Andric OS << '\n'; 1094*0b57cec5SDimitry Andric } 1095*0b57cec5SDimitry Andric 1096*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1097*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } 1098*0b57cec5SDimitry Andric #endif 1099*0b57cec5SDimitry Andric 1100*0b57cec5SDimitry Andric Printable llvm::printJumpTableEntryReference(unsigned Idx) { 1101*0b57cec5SDimitry Andric return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); 1102*0b57cec5SDimitry Andric } 1103*0b57cec5SDimitry Andric 1104*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1105*0b57cec5SDimitry Andric // MachineConstantPool implementation 1106*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1107*0b57cec5SDimitry Andric 1108*0b57cec5SDimitry Andric void MachineConstantPoolValue::anchor() {} 1109*0b57cec5SDimitry Andric 1110e8d8bef9SDimitry Andric unsigned MachineConstantPoolValue::getSizeInBytes(const DataLayout &DL) const { 1111e8d8bef9SDimitry Andric return DL.getTypeAllocSize(Ty); 1112e8d8bef9SDimitry Andric } 1113e8d8bef9SDimitry Andric 1114e8d8bef9SDimitry Andric unsigned MachineConstantPoolEntry::getSizeInBytes(const DataLayout &DL) const { 1115*0b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 1116e8d8bef9SDimitry Andric return Val.MachineCPVal->getSizeInBytes(DL); 1117e8d8bef9SDimitry Andric return DL.getTypeAllocSize(Val.ConstVal->getType()); 1118*0b57cec5SDimitry Andric } 1119*0b57cec5SDimitry Andric 1120*0b57cec5SDimitry Andric bool MachineConstantPoolEntry::needsRelocation() const { 1121*0b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 1122*0b57cec5SDimitry Andric return true; 1123*0b57cec5SDimitry Andric return Val.ConstVal->needsRelocation(); 1124*0b57cec5SDimitry Andric } 1125*0b57cec5SDimitry Andric 1126*0b57cec5SDimitry Andric SectionKind 1127*0b57cec5SDimitry Andric MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { 1128*0b57cec5SDimitry Andric if (needsRelocation()) 1129*0b57cec5SDimitry Andric return SectionKind::getReadOnlyWithRel(); 1130e8d8bef9SDimitry Andric switch (getSizeInBytes(*DL)) { 1131*0b57cec5SDimitry Andric case 4: 1132*0b57cec5SDimitry Andric return SectionKind::getMergeableConst4(); 1133*0b57cec5SDimitry Andric case 8: 1134*0b57cec5SDimitry Andric return SectionKind::getMergeableConst8(); 1135*0b57cec5SDimitry Andric case 16: 1136*0b57cec5SDimitry Andric return SectionKind::getMergeableConst16(); 1137*0b57cec5SDimitry Andric case 32: 1138*0b57cec5SDimitry Andric return SectionKind::getMergeableConst32(); 1139*0b57cec5SDimitry Andric default: 1140*0b57cec5SDimitry Andric return SectionKind::getReadOnly(); 1141*0b57cec5SDimitry Andric } 1142*0b57cec5SDimitry Andric } 1143*0b57cec5SDimitry Andric 1144*0b57cec5SDimitry Andric MachineConstantPool::~MachineConstantPool() { 1145*0b57cec5SDimitry Andric // A constant may be a member of both Constants and MachineCPVsSharingEntries, 1146*0b57cec5SDimitry Andric // so keep track of which we've deleted to avoid double deletions. 1147*0b57cec5SDimitry Andric DenseSet<MachineConstantPoolValue*> Deleted; 1148*0b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) 1149*0b57cec5SDimitry Andric if (Constants[i].isMachineConstantPoolEntry()) { 1150*0b57cec5SDimitry Andric Deleted.insert(Constants[i].Val.MachineCPVal); 1151*0b57cec5SDimitry Andric delete Constants[i].Val.MachineCPVal; 1152*0b57cec5SDimitry Andric } 1153*0b57cec5SDimitry Andric for (DenseSet<MachineConstantPoolValue*>::iterator I = 1154*0b57cec5SDimitry Andric MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end(); 1155*0b57cec5SDimitry Andric I != E; ++I) { 1156*0b57cec5SDimitry Andric if (Deleted.count(*I) == 0) 1157*0b57cec5SDimitry Andric delete *I; 1158*0b57cec5SDimitry Andric } 1159*0b57cec5SDimitry Andric } 1160*0b57cec5SDimitry Andric 1161*0b57cec5SDimitry Andric /// Test whether the given two constants can be allocated the same constant pool 1162*0b57cec5SDimitry Andric /// entry. 1163*0b57cec5SDimitry Andric static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 1164*0b57cec5SDimitry Andric const DataLayout &DL) { 1165*0b57cec5SDimitry Andric // Handle the trivial case quickly. 1166*0b57cec5SDimitry Andric if (A == B) return true; 1167*0b57cec5SDimitry Andric 1168*0b57cec5SDimitry Andric // If they have the same type but weren't the same constant, quickly 1169*0b57cec5SDimitry Andric // reject them. 1170*0b57cec5SDimitry Andric if (A->getType() == B->getType()) return false; 1171*0b57cec5SDimitry Andric 1172*0b57cec5SDimitry Andric // We can't handle structs or arrays. 1173*0b57cec5SDimitry Andric if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || 1174*0b57cec5SDimitry Andric isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) 1175*0b57cec5SDimitry Andric return false; 1176*0b57cec5SDimitry Andric 1177*0b57cec5SDimitry Andric // For now, only support constants with the same size. 1178*0b57cec5SDimitry Andric uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); 1179*0b57cec5SDimitry Andric if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) 1180*0b57cec5SDimitry Andric return false; 1181*0b57cec5SDimitry Andric 1182*0b57cec5SDimitry Andric Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); 1183*0b57cec5SDimitry Andric 1184*0b57cec5SDimitry Andric // Try constant folding a bitcast of both instructions to an integer. If we 1185*0b57cec5SDimitry Andric // get two identical ConstantInt's, then we are good to share them. We use 1186*0b57cec5SDimitry Andric // the constant folding APIs to do this so that we get the benefit of 1187*0b57cec5SDimitry Andric // DataLayout. 1188*0b57cec5SDimitry Andric if (isa<PointerType>(A->getType())) 1189*0b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::PtrToInt, 1190*0b57cec5SDimitry Andric const_cast<Constant *>(A), IntTy, DL); 1191*0b57cec5SDimitry Andric else if (A->getType() != IntTy) 1192*0b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), 1193*0b57cec5SDimitry Andric IntTy, DL); 1194*0b57cec5SDimitry Andric if (isa<PointerType>(B->getType())) 1195*0b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::PtrToInt, 1196*0b57cec5SDimitry Andric const_cast<Constant *>(B), IntTy, DL); 1197*0b57cec5SDimitry Andric else if (B->getType() != IntTy) 1198*0b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), 1199*0b57cec5SDimitry Andric IntTy, DL); 1200*0b57cec5SDimitry Andric 1201*0b57cec5SDimitry Andric return A == B; 1202*0b57cec5SDimitry Andric } 1203*0b57cec5SDimitry Andric 1204*0b57cec5SDimitry Andric /// Create a new entry in the constant pool or return an existing one. 1205*0b57cec5SDimitry Andric /// User must specify the log2 of the minimum required alignment for the object. 1206*0b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 12075ffd83dbSDimitry Andric Align Alignment) { 1208*0b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1209*0b57cec5SDimitry Andric 1210*0b57cec5SDimitry Andric // Check to see if we already have this constant. 1211*0b57cec5SDimitry Andric // 1212*0b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 1213*0b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) 1214*0b57cec5SDimitry Andric if (!Constants[i].isMachineConstantPoolEntry() && 1215*0b57cec5SDimitry Andric CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { 12165ffd83dbSDimitry Andric if (Constants[i].getAlign() < Alignment) 1217*0b57cec5SDimitry Andric Constants[i].Alignment = Alignment; 1218*0b57cec5SDimitry Andric return i; 1219*0b57cec5SDimitry Andric } 1220*0b57cec5SDimitry Andric 1221*0b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 1222*0b57cec5SDimitry Andric return Constants.size()-1; 1223*0b57cec5SDimitry Andric } 1224*0b57cec5SDimitry Andric 1225*0b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 12265ffd83dbSDimitry Andric Align Alignment) { 1227*0b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1228*0b57cec5SDimitry Andric 1229*0b57cec5SDimitry Andric // Check to see if we already have this constant. 1230*0b57cec5SDimitry Andric // 1231*0b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 1232*0b57cec5SDimitry Andric int Idx = V->getExistingMachineCPValue(this, Alignment); 1233*0b57cec5SDimitry Andric if (Idx != -1) { 1234*0b57cec5SDimitry Andric MachineCPVsSharingEntries.insert(V); 1235*0b57cec5SDimitry Andric return (unsigned)Idx; 1236*0b57cec5SDimitry Andric } 1237*0b57cec5SDimitry Andric 1238*0b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 1239*0b57cec5SDimitry Andric return Constants.size()-1; 1240*0b57cec5SDimitry Andric } 1241*0b57cec5SDimitry Andric 1242*0b57cec5SDimitry Andric void MachineConstantPool::print(raw_ostream &OS) const { 1243*0b57cec5SDimitry Andric if (Constants.empty()) return; 1244*0b57cec5SDimitry Andric 1245*0b57cec5SDimitry Andric OS << "Constant Pool:\n"; 1246*0b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 1247*0b57cec5SDimitry Andric OS << " cp#" << i << ": "; 1248*0b57cec5SDimitry Andric if (Constants[i].isMachineConstantPoolEntry()) 1249*0b57cec5SDimitry Andric Constants[i].Val.MachineCPVal->print(OS); 1250*0b57cec5SDimitry Andric else 1251*0b57cec5SDimitry Andric Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); 12525ffd83dbSDimitry Andric OS << ", align=" << Constants[i].getAlign().value(); 1253*0b57cec5SDimitry Andric OS << "\n"; 1254*0b57cec5SDimitry Andric } 1255*0b57cec5SDimitry Andric } 1256*0b57cec5SDimitry Andric 1257*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1258*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } 1259*0b57cec5SDimitry Andric #endif 1260