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/DerivedTypes.h" 48*0b57cec5SDimitry Andric #include "llvm/IR/Function.h" 49*0b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h" 50*0b57cec5SDimitry Andric #include "llvm/IR/Instruction.h" 51*0b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 52*0b57cec5SDimitry Andric #include "llvm/IR/Metadata.h" 53*0b57cec5SDimitry Andric #include "llvm/IR/Module.h" 54*0b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h" 55*0b57cec5SDimitry Andric #include "llvm/IR/Value.h" 56*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 57*0b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 58*0b57cec5SDimitry Andric #include "llvm/MC/SectionKind.h" 59*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 60*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h" 61*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 62*0b57cec5SDimitry Andric #include "llvm/Support/DOTGraphTraits.h" 63*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 64*0b57cec5SDimitry Andric #include "llvm/Support/GraphWriter.h" 65*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 66*0b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h" 67*0b57cec5SDimitry Andric #include <algorithm> 68*0b57cec5SDimitry Andric #include <cassert> 69*0b57cec5SDimitry Andric #include <cstddef> 70*0b57cec5SDimitry Andric #include <cstdint> 71*0b57cec5SDimitry Andric #include <iterator> 72*0b57cec5SDimitry Andric #include <string> 735ffd83dbSDimitry Andric #include <type_traits> 74*0b57cec5SDimitry Andric #include <utility> 75*0b57cec5SDimitry Andric #include <vector> 76*0b57cec5SDimitry Andric 7704eeddc0SDimitry Andric #include "LiveDebugValues/LiveDebugValues.h" 7804eeddc0SDimitry 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 920eae32dcSDimitry Andric // clang-format off 93*0b57cec5SDimitry Andric switch(Prop) { 94*0b57cec5SDimitry Andric case P::FailedISel: return "FailedISel"; 95*0b57cec5SDimitry Andric case P::IsSSA: return "IsSSA"; 96*0b57cec5SDimitry Andric case P::Legalized: return "Legalized"; 97*0b57cec5SDimitry Andric case P::NoPHIs: return "NoPHIs"; 98*0b57cec5SDimitry Andric case P::NoVRegs: return "NoVRegs"; 99*0b57cec5SDimitry Andric case P::RegBankSelected: return "RegBankSelected"; 100*0b57cec5SDimitry Andric case P::Selected: return "Selected"; 101*0b57cec5SDimitry Andric case P::TracksLiveness: return "TracksLiveness"; 1025ffd83dbSDimitry Andric case P::TiedOpsRewritten: return "TiedOpsRewritten"; 103349cc55cSDimitry Andric case P::FailsVerification: return "FailsVerification"; 1040eae32dcSDimitry Andric case P::TracksDebugUserValues: return "TracksDebugUserValues"; 105*0b57cec5SDimitry Andric } 1060eae32dcSDimitry Andric // clang-format on 107*0b57cec5SDimitry Andric llvm_unreachable("Invalid machine function property"); 108*0b57cec5SDimitry Andric } 109*0b57cec5SDimitry Andric 11081ad6265SDimitry Andric void setUnsafeStackSize(const Function &F, MachineFrameInfo &FrameInfo) { 11181ad6265SDimitry Andric if (!F.hasFnAttribute(Attribute::SafeStack)) 11281ad6265SDimitry Andric return; 11381ad6265SDimitry Andric 11481ad6265SDimitry Andric auto *Existing = 11581ad6265SDimitry Andric dyn_cast_or_null<MDTuple>(F.getMetadata(LLVMContext::MD_annotation)); 11681ad6265SDimitry Andric 11781ad6265SDimitry Andric if (!Existing || Existing->getNumOperands() != 2) 11881ad6265SDimitry Andric return; 11981ad6265SDimitry Andric 12081ad6265SDimitry Andric auto *MetadataName = "unsafe-stack-size"; 12181ad6265SDimitry Andric if (auto &N = Existing->getOperand(0)) { 12281ad6265SDimitry Andric if (cast<MDString>(N.get())->getString() == MetadataName) { 12381ad6265SDimitry Andric if (auto &Op = Existing->getOperand(1)) { 12481ad6265SDimitry Andric auto Val = mdconst::extract<ConstantInt>(Op)->getZExtValue(); 12581ad6265SDimitry Andric FrameInfo.setUnsafeStackSize(Val); 12681ad6265SDimitry Andric } 12781ad6265SDimitry Andric } 12881ad6265SDimitry Andric } 12981ad6265SDimitry Andric } 13081ad6265SDimitry Andric 131*0b57cec5SDimitry Andric // Pin the vtable to this file. 132*0b57cec5SDimitry Andric void MachineFunction::Delegate::anchor() {} 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric void MachineFunctionProperties::print(raw_ostream &OS) const { 135*0b57cec5SDimitry Andric const char *Separator = ""; 136*0b57cec5SDimitry Andric for (BitVector::size_type I = 0; I < Properties.size(); ++I) { 137*0b57cec5SDimitry Andric if (!Properties[I]) 138*0b57cec5SDimitry Andric continue; 139*0b57cec5SDimitry Andric OS << Separator << getPropertyName(static_cast<Property>(I)); 140*0b57cec5SDimitry Andric Separator = ", "; 141*0b57cec5SDimitry Andric } 142*0b57cec5SDimitry Andric } 143*0b57cec5SDimitry Andric 144*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 145*0b57cec5SDimitry Andric // MachineFunction implementation 146*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 147*0b57cec5SDimitry Andric 148*0b57cec5SDimitry Andric // Out-of-line virtual method. 149*0b57cec5SDimitry Andric MachineFunctionInfo::~MachineFunctionInfo() = default; 150*0b57cec5SDimitry Andric 151*0b57cec5SDimitry Andric void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 1520eae32dcSDimitry Andric MBB->getParent()->deleteMachineBasicBlock(MBB); 153*0b57cec5SDimitry Andric } 154*0b57cec5SDimitry Andric 15581ad6265SDimitry Andric static inline Align getFnStackAlignment(const TargetSubtargetInfo *STI, 156*0b57cec5SDimitry Andric const Function &F) { 157349cc55cSDimitry Andric if (auto MA = F.getFnStackAlign()) 15881ad6265SDimitry Andric return *MA; 15981ad6265SDimitry Andric return STI->getFrameLowering()->getStackAlign(); 160*0b57cec5SDimitry Andric } 161*0b57cec5SDimitry Andric 1625ffd83dbSDimitry Andric MachineFunction::MachineFunction(Function &F, const LLVMTargetMachine &Target, 163*0b57cec5SDimitry Andric const TargetSubtargetInfo &STI, 164*0b57cec5SDimitry Andric unsigned FunctionNum, MachineModuleInfo &mmi) 165*0b57cec5SDimitry Andric : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) { 166*0b57cec5SDimitry Andric FunctionNumber = FunctionNum; 167*0b57cec5SDimitry Andric init(); 168*0b57cec5SDimitry Andric } 169*0b57cec5SDimitry Andric 170*0b57cec5SDimitry Andric void MachineFunction::handleInsertion(MachineInstr &MI) { 171*0b57cec5SDimitry Andric if (TheDelegate) 172*0b57cec5SDimitry Andric TheDelegate->MF_HandleInsertion(MI); 173*0b57cec5SDimitry Andric } 174*0b57cec5SDimitry Andric 175*0b57cec5SDimitry Andric void MachineFunction::handleRemoval(MachineInstr &MI) { 176*0b57cec5SDimitry Andric if (TheDelegate) 177*0b57cec5SDimitry Andric TheDelegate->MF_HandleRemoval(MI); 178*0b57cec5SDimitry Andric } 179*0b57cec5SDimitry Andric 180*0b57cec5SDimitry Andric void MachineFunction::init() { 181*0b57cec5SDimitry Andric // Assume the function starts in SSA form with correct liveness. 182*0b57cec5SDimitry Andric Properties.set(MachineFunctionProperties::Property::IsSSA); 183*0b57cec5SDimitry Andric Properties.set(MachineFunctionProperties::Property::TracksLiveness); 184*0b57cec5SDimitry Andric if (STI->getRegisterInfo()) 185*0b57cec5SDimitry Andric RegInfo = new (Allocator) MachineRegisterInfo(this); 186*0b57cec5SDimitry Andric else 187*0b57cec5SDimitry Andric RegInfo = nullptr; 188*0b57cec5SDimitry Andric 189*0b57cec5SDimitry Andric MFInfo = nullptr; 190bdd1243dSDimitry Andric 191*0b57cec5SDimitry Andric // We can realign the stack if the target supports it and the user hasn't 192*0b57cec5SDimitry Andric // explicitly asked us not to. 193*0b57cec5SDimitry Andric bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && 194*0b57cec5SDimitry Andric !F.hasFnAttribute("no-realign-stack"); 195*0b57cec5SDimitry Andric FrameInfo = new (Allocator) MachineFrameInfo( 196*0b57cec5SDimitry Andric getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, 197*0b57cec5SDimitry Andric /*ForcedRealign=*/CanRealignSP && 198*0b57cec5SDimitry Andric F.hasFnAttribute(Attribute::StackAlignment)); 199*0b57cec5SDimitry Andric 20081ad6265SDimitry Andric setUnsafeStackSize(F, *FrameInfo); 20181ad6265SDimitry Andric 202*0b57cec5SDimitry Andric if (F.hasFnAttribute(Attribute::StackAlignment)) 2035ffd83dbSDimitry Andric FrameInfo->ensureMaxAlignment(*F.getFnStackAlign()); 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); 206*0b57cec5SDimitry Andric Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); 207*0b57cec5SDimitry Andric 208*0b57cec5SDimitry Andric // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. 209*0b57cec5SDimitry Andric // FIXME: Use Function::hasOptSize(). 210*0b57cec5SDimitry Andric if (!F.hasFnAttribute(Attribute::OptimizeForSize)) 211*0b57cec5SDimitry Andric Alignment = std::max(Alignment, 212*0b57cec5SDimitry Andric STI->getTargetLowering()->getPrefFunctionAlignment()); 213*0b57cec5SDimitry Andric 214*0b57cec5SDimitry Andric if (AlignAllFunctions) 2158bcb0991SDimitry Andric Alignment = Align(1ULL << AlignAllFunctions); 216*0b57cec5SDimitry Andric 217*0b57cec5SDimitry Andric JumpTableInfo = nullptr; 218*0b57cec5SDimitry Andric 219*0b57cec5SDimitry Andric if (isFuncletEHPersonality(classifyEHPersonality( 220*0b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 221*0b57cec5SDimitry Andric WinEHInfo = new (Allocator) WinEHFuncInfo(); 222*0b57cec5SDimitry Andric } 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric if (isScopedEHPersonality(classifyEHPersonality( 225*0b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 226*0b57cec5SDimitry Andric WasmEHInfo = new (Allocator) WasmEHFuncInfo(); 227*0b57cec5SDimitry Andric } 228*0b57cec5SDimitry Andric 229*0b57cec5SDimitry Andric assert(Target.isCompatibleDataLayout(getDataLayout()) && 230*0b57cec5SDimitry Andric "Can't create a MachineFunction using a Module with a " 231*0b57cec5SDimitry Andric "Target-incompatible DataLayout attached\n"); 232*0b57cec5SDimitry Andric 23381ad6265SDimitry Andric PSVManager = std::make_unique<PseudoSourceValueManager>(getTarget()); 234*0b57cec5SDimitry Andric } 235*0b57cec5SDimitry Andric 236bdd1243dSDimitry Andric void MachineFunction::initTargetMachineFunctionInfo( 237bdd1243dSDimitry Andric const TargetSubtargetInfo &STI) { 238bdd1243dSDimitry Andric assert(!MFInfo && "MachineFunctionInfo already set"); 239bdd1243dSDimitry Andric MFInfo = Target.createMachineFunctionInfo(Allocator, F, &STI); 240bdd1243dSDimitry Andric } 241bdd1243dSDimitry Andric 242*0b57cec5SDimitry Andric MachineFunction::~MachineFunction() { 243*0b57cec5SDimitry Andric clear(); 244*0b57cec5SDimitry Andric } 245*0b57cec5SDimitry Andric 246*0b57cec5SDimitry Andric void MachineFunction::clear() { 247*0b57cec5SDimitry Andric Properties.reset(); 248*0b57cec5SDimitry Andric // Don't call destructors on MachineInstr and MachineOperand. All of their 249*0b57cec5SDimitry Andric // memory comes from the BumpPtrAllocator which is about to be purged. 250*0b57cec5SDimitry Andric // 251*0b57cec5SDimitry Andric // Do call MachineBasicBlock destructors, it contains std::vectors. 252*0b57cec5SDimitry Andric for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) 253*0b57cec5SDimitry Andric I->Insts.clearAndLeakNodesUnsafely(); 254*0b57cec5SDimitry Andric MBBNumbering.clear(); 255*0b57cec5SDimitry Andric 256*0b57cec5SDimitry Andric InstructionRecycler.clear(Allocator); 257*0b57cec5SDimitry Andric OperandRecycler.clear(Allocator); 258*0b57cec5SDimitry Andric BasicBlockRecycler.clear(Allocator); 259*0b57cec5SDimitry Andric CodeViewAnnotations.clear(); 260*0b57cec5SDimitry Andric VariableDbgInfos.clear(); 261*0b57cec5SDimitry Andric if (RegInfo) { 262*0b57cec5SDimitry Andric RegInfo->~MachineRegisterInfo(); 263*0b57cec5SDimitry Andric Allocator.Deallocate(RegInfo); 264*0b57cec5SDimitry Andric } 265*0b57cec5SDimitry Andric if (MFInfo) { 266*0b57cec5SDimitry Andric MFInfo->~MachineFunctionInfo(); 267*0b57cec5SDimitry Andric Allocator.Deallocate(MFInfo); 268*0b57cec5SDimitry Andric } 269*0b57cec5SDimitry Andric 270*0b57cec5SDimitry Andric FrameInfo->~MachineFrameInfo(); 271*0b57cec5SDimitry Andric Allocator.Deallocate(FrameInfo); 272*0b57cec5SDimitry Andric 273*0b57cec5SDimitry Andric ConstantPool->~MachineConstantPool(); 274*0b57cec5SDimitry Andric Allocator.Deallocate(ConstantPool); 275*0b57cec5SDimitry Andric 276*0b57cec5SDimitry Andric if (JumpTableInfo) { 277*0b57cec5SDimitry Andric JumpTableInfo->~MachineJumpTableInfo(); 278*0b57cec5SDimitry Andric Allocator.Deallocate(JumpTableInfo); 279*0b57cec5SDimitry Andric } 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric if (WinEHInfo) { 282*0b57cec5SDimitry Andric WinEHInfo->~WinEHFuncInfo(); 283*0b57cec5SDimitry Andric Allocator.Deallocate(WinEHInfo); 284*0b57cec5SDimitry Andric } 285*0b57cec5SDimitry Andric 286*0b57cec5SDimitry Andric if (WasmEHInfo) { 287*0b57cec5SDimitry Andric WasmEHInfo->~WasmEHFuncInfo(); 288*0b57cec5SDimitry Andric Allocator.Deallocate(WasmEHInfo); 289*0b57cec5SDimitry Andric } 290*0b57cec5SDimitry Andric } 291*0b57cec5SDimitry Andric 292*0b57cec5SDimitry Andric const DataLayout &MachineFunction::getDataLayout() const { 293*0b57cec5SDimitry Andric return F.getParent()->getDataLayout(); 294*0b57cec5SDimitry Andric } 295*0b57cec5SDimitry Andric 296*0b57cec5SDimitry Andric /// Get the JumpTableInfo for this function. 297*0b57cec5SDimitry Andric /// If it does not already exist, allocate one. 298*0b57cec5SDimitry Andric MachineJumpTableInfo *MachineFunction:: 299*0b57cec5SDimitry Andric getOrCreateJumpTableInfo(unsigned EntryKind) { 300*0b57cec5SDimitry Andric if (JumpTableInfo) return JumpTableInfo; 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric JumpTableInfo = new (Allocator) 303*0b57cec5SDimitry Andric MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 304*0b57cec5SDimitry Andric return JumpTableInfo; 305*0b57cec5SDimitry Andric } 306*0b57cec5SDimitry Andric 307480093f4SDimitry Andric DenormalMode MachineFunction::getDenormalMode(const fltSemantics &FPType) const { 308e8d8bef9SDimitry Andric return F.getDenormalMode(FPType); 309480093f4SDimitry Andric } 310480093f4SDimitry Andric 311*0b57cec5SDimitry Andric /// Should we be emitting segmented stack stuff for the function 312*0b57cec5SDimitry Andric bool MachineFunction::shouldSplitStack() const { 313*0b57cec5SDimitry Andric return getFunction().hasFnAttribute("split-stack"); 314*0b57cec5SDimitry Andric } 315*0b57cec5SDimitry Andric 316bdd1243dSDimitry Andric [[nodiscard]] unsigned 317*0b57cec5SDimitry Andric MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { 318*0b57cec5SDimitry Andric FrameInstructions.push_back(Inst); 319*0b57cec5SDimitry Andric return FrameInstructions.size() - 1; 320*0b57cec5SDimitry Andric } 321*0b57cec5SDimitry Andric 322*0b57cec5SDimitry Andric /// This discards all of the MachineBasicBlock numbers and recomputes them. 323*0b57cec5SDimitry Andric /// This guarantees that the MBB numbers are sequential, dense, and match the 324*0b57cec5SDimitry Andric /// ordering of the blocks within the function. If a specific MachineBasicBlock 325*0b57cec5SDimitry Andric /// is specified, only that block and those after it are renumbered. 326*0b57cec5SDimitry Andric void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 327*0b57cec5SDimitry Andric if (empty()) { MBBNumbering.clear(); return; } 328*0b57cec5SDimitry Andric MachineFunction::iterator MBBI, E = end(); 329*0b57cec5SDimitry Andric if (MBB == nullptr) 330*0b57cec5SDimitry Andric MBBI = begin(); 331*0b57cec5SDimitry Andric else 332*0b57cec5SDimitry Andric MBBI = MBB->getIterator(); 333*0b57cec5SDimitry Andric 334*0b57cec5SDimitry Andric // Figure out the block number this should have. 335*0b57cec5SDimitry Andric unsigned BlockNo = 0; 336*0b57cec5SDimitry Andric if (MBBI != begin()) 337*0b57cec5SDimitry Andric BlockNo = std::prev(MBBI)->getNumber() + 1; 338*0b57cec5SDimitry Andric 339*0b57cec5SDimitry Andric for (; MBBI != E; ++MBBI, ++BlockNo) { 340*0b57cec5SDimitry Andric if (MBBI->getNumber() != (int)BlockNo) { 341*0b57cec5SDimitry Andric // Remove use of the old number. 342*0b57cec5SDimitry Andric if (MBBI->getNumber() != -1) { 343*0b57cec5SDimitry Andric assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 344*0b57cec5SDimitry Andric "MBB number mismatch!"); 345*0b57cec5SDimitry Andric MBBNumbering[MBBI->getNumber()] = nullptr; 346*0b57cec5SDimitry Andric } 347*0b57cec5SDimitry Andric 348*0b57cec5SDimitry Andric // If BlockNo is already taken, set that block's number to -1. 349*0b57cec5SDimitry Andric if (MBBNumbering[BlockNo]) 350*0b57cec5SDimitry Andric MBBNumbering[BlockNo]->setNumber(-1); 351*0b57cec5SDimitry Andric 352*0b57cec5SDimitry Andric MBBNumbering[BlockNo] = &*MBBI; 353*0b57cec5SDimitry Andric MBBI->setNumber(BlockNo); 354*0b57cec5SDimitry Andric } 355*0b57cec5SDimitry Andric } 356*0b57cec5SDimitry Andric 357*0b57cec5SDimitry Andric // Okay, all the blocks are renumbered. If we have compactified the block 358*0b57cec5SDimitry Andric // numbering, shrink MBBNumbering now. 359*0b57cec5SDimitry Andric assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 360*0b57cec5SDimitry Andric MBBNumbering.resize(BlockNo); 361*0b57cec5SDimitry Andric } 362*0b57cec5SDimitry Andric 3635ffd83dbSDimitry Andric /// This method iterates over the basic blocks and assigns their IsBeginSection 3645ffd83dbSDimitry Andric /// and IsEndSection fields. This must be called after MBB layout is finalized 3655ffd83dbSDimitry Andric /// and the SectionID's are assigned to MBBs. 3665ffd83dbSDimitry Andric void MachineFunction::assignBeginEndSections() { 3675ffd83dbSDimitry Andric front().setIsBeginSection(); 3685ffd83dbSDimitry Andric auto CurrentSectionID = front().getSectionID(); 3695ffd83dbSDimitry Andric for (auto MBBI = std::next(begin()), E = end(); MBBI != E; ++MBBI) { 3705ffd83dbSDimitry Andric if (MBBI->getSectionID() == CurrentSectionID) 3715ffd83dbSDimitry Andric continue; 3725ffd83dbSDimitry Andric MBBI->setIsBeginSection(); 3735ffd83dbSDimitry Andric std::prev(MBBI)->setIsEndSection(); 3745ffd83dbSDimitry Andric CurrentSectionID = MBBI->getSectionID(); 3755ffd83dbSDimitry Andric } 3765ffd83dbSDimitry Andric back().setIsEndSection(); 3775ffd83dbSDimitry Andric } 3785ffd83dbSDimitry Andric 379*0b57cec5SDimitry Andric /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. 380*0b57cec5SDimitry Andric MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 3810eae32dcSDimitry Andric DebugLoc DL, 382e8d8bef9SDimitry Andric bool NoImplicit) { 383*0b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 3840eae32dcSDimitry Andric MachineInstr(*this, MCID, std::move(DL), NoImplicit); 385*0b57cec5SDimitry Andric } 386*0b57cec5SDimitry Andric 387*0b57cec5SDimitry Andric /// Create a new MachineInstr which is a copy of the 'Orig' instruction, 388*0b57cec5SDimitry Andric /// identical in all ways except the instruction has no parent, prev, or next. 389*0b57cec5SDimitry Andric MachineInstr * 390*0b57cec5SDimitry Andric MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 391*0b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 392*0b57cec5SDimitry Andric MachineInstr(*this, *Orig); 393*0b57cec5SDimitry Andric } 394*0b57cec5SDimitry Andric 3950eae32dcSDimitry Andric MachineInstr &MachineFunction::cloneMachineInstrBundle( 3960eae32dcSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore, 3970eae32dcSDimitry Andric const MachineInstr &Orig) { 398*0b57cec5SDimitry Andric MachineInstr *FirstClone = nullptr; 399*0b57cec5SDimitry Andric MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); 400*0b57cec5SDimitry Andric while (true) { 401*0b57cec5SDimitry Andric MachineInstr *Cloned = CloneMachineInstr(&*I); 402*0b57cec5SDimitry Andric MBB.insert(InsertBefore, Cloned); 403*0b57cec5SDimitry Andric if (FirstClone == nullptr) { 404*0b57cec5SDimitry Andric FirstClone = Cloned; 405*0b57cec5SDimitry Andric } else { 406*0b57cec5SDimitry Andric Cloned->bundleWithPred(); 407*0b57cec5SDimitry Andric } 408*0b57cec5SDimitry Andric 409*0b57cec5SDimitry Andric if (!I->isBundledWithSucc()) 410*0b57cec5SDimitry Andric break; 411*0b57cec5SDimitry Andric ++I; 412*0b57cec5SDimitry Andric } 4135ffd83dbSDimitry Andric // Copy over call site info to the cloned instruction if needed. If Orig is in 4145ffd83dbSDimitry Andric // a bundle, copyCallSiteInfo takes care of finding the call instruction in 4155ffd83dbSDimitry Andric // the bundle. 4165ffd83dbSDimitry Andric if (Orig.shouldUpdateCallSiteInfo()) 4175ffd83dbSDimitry Andric copyCallSiteInfo(&Orig, FirstClone); 418*0b57cec5SDimitry Andric return *FirstClone; 419*0b57cec5SDimitry Andric } 420*0b57cec5SDimitry Andric 421*0b57cec5SDimitry Andric /// Delete the given MachineInstr. 422*0b57cec5SDimitry Andric /// 423*0b57cec5SDimitry Andric /// This function also serves as the MachineInstr destructor - the real 424*0b57cec5SDimitry Andric /// ~MachineInstr() destructor must be empty. 4250eae32dcSDimitry Andric void MachineFunction::deleteMachineInstr(MachineInstr *MI) { 426*0b57cec5SDimitry Andric // Verify that a call site info is at valid state. This assertion should 427*0b57cec5SDimitry Andric // be triggered during the implementation of support for the 428*0b57cec5SDimitry Andric // call site info of a new architecture. If the assertion is triggered, 429*0b57cec5SDimitry Andric // back trace will tell where to insert a call to updateCallSiteInfo(). 4305ffd83dbSDimitry Andric assert((!MI->isCandidateForCallSiteEntry() || 431*0b57cec5SDimitry Andric CallSitesInfo.find(MI) == CallSitesInfo.end()) && 432*0b57cec5SDimitry Andric "Call site info was not updated!"); 433*0b57cec5SDimitry Andric // Strip it for parts. The operand array and the MI object itself are 434*0b57cec5SDimitry Andric // independently recyclable. 435*0b57cec5SDimitry Andric if (MI->Operands) 436*0b57cec5SDimitry Andric deallocateOperandArray(MI->CapOperands, MI->Operands); 437*0b57cec5SDimitry Andric // Don't call ~MachineInstr() which must be trivial anyway because 438*0b57cec5SDimitry Andric // ~MachineFunction drops whole lists of MachineInstrs wihout calling their 439*0b57cec5SDimitry Andric // destructors. 440*0b57cec5SDimitry Andric InstructionRecycler.Deallocate(Allocator, MI); 441*0b57cec5SDimitry Andric } 442*0b57cec5SDimitry Andric 443*0b57cec5SDimitry Andric /// Allocate a new MachineBasicBlock. Use this instead of 444*0b57cec5SDimitry Andric /// `new MachineBasicBlock'. 445*0b57cec5SDimitry Andric MachineBasicBlock * 446*0b57cec5SDimitry Andric MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 447bdd1243dSDimitry Andric MachineBasicBlock *MBB = 448bdd1243dSDimitry Andric new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 449*0b57cec5SDimitry Andric MachineBasicBlock(*this, bb); 450bdd1243dSDimitry Andric // Set BBID for `-basic-block=sections=labels` and 451bdd1243dSDimitry Andric // `-basic-block-sections=list` to allow robust mapping of profiles to basic 452bdd1243dSDimitry Andric // blocks. 453bdd1243dSDimitry Andric if (Target.getBBSectionsType() == BasicBlockSection::Labels || 454bdd1243dSDimitry Andric Target.getBBSectionsType() == BasicBlockSection::List) 455bdd1243dSDimitry Andric MBB->setBBID(NextBBID++); 456bdd1243dSDimitry Andric return MBB; 457*0b57cec5SDimitry Andric } 458*0b57cec5SDimitry Andric 459*0b57cec5SDimitry Andric /// Delete the given MachineBasicBlock. 4600eae32dcSDimitry Andric void MachineFunction::deleteMachineBasicBlock(MachineBasicBlock *MBB) { 461*0b57cec5SDimitry Andric assert(MBB->getParent() == this && "MBB parent mismatch!"); 462e8d8bef9SDimitry Andric // Clean up any references to MBB in jump tables before deleting it. 463e8d8bef9SDimitry Andric if (JumpTableInfo) 464e8d8bef9SDimitry Andric JumpTableInfo->RemoveMBBFromJumpTables(MBB); 465*0b57cec5SDimitry Andric MBB->~MachineBasicBlock(); 466*0b57cec5SDimitry Andric BasicBlockRecycler.Deallocate(Allocator, MBB); 467*0b57cec5SDimitry Andric } 468*0b57cec5SDimitry Andric 469*0b57cec5SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 470*0b57cec5SDimitry Andric MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, 4715ffd83dbSDimitry Andric Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 472*0b57cec5SDimitry Andric SyncScope::ID SSID, AtomicOrdering Ordering, 473*0b57cec5SDimitry Andric AtomicOrdering FailureOrdering) { 474*0b57cec5SDimitry Andric return new (Allocator) 475*0b57cec5SDimitry Andric MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, 476*0b57cec5SDimitry Andric SSID, Ordering, FailureOrdering); 477*0b57cec5SDimitry Andric } 478*0b57cec5SDimitry Andric 479e8d8bef9SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 480fe6060f1SDimitry Andric MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, 481fe6060f1SDimitry Andric Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 482fe6060f1SDimitry Andric SyncScope::ID SSID, AtomicOrdering Ordering, 483fe6060f1SDimitry Andric AtomicOrdering FailureOrdering) { 484fe6060f1SDimitry Andric return new (Allocator) 485fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, f, MemTy, base_alignment, AAInfo, Ranges, SSID, 486fe6060f1SDimitry Andric Ordering, FailureOrdering); 487fe6060f1SDimitry Andric } 488fe6060f1SDimitry Andric 489fe6060f1SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 490fe6060f1SDimitry Andric const MachineMemOperand *MMO, const MachinePointerInfo &PtrInfo, uint64_t Size) { 491fe6060f1SDimitry Andric return new (Allocator) 492fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, MMO->getFlags(), Size, MMO->getBaseAlign(), 493fe6060f1SDimitry Andric AAMDNodes(), nullptr, MMO->getSyncScopeID(), 494fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 495fe6060f1SDimitry Andric } 496fe6060f1SDimitry Andric 497fe6060f1SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 498fe6060f1SDimitry Andric const MachineMemOperand *MMO, const MachinePointerInfo &PtrInfo, LLT Ty) { 499fe6060f1SDimitry Andric return new (Allocator) 500fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, MMO->getFlags(), Ty, MMO->getBaseAlign(), 501fe6060f1SDimitry Andric AAMDNodes(), nullptr, MMO->getSyncScopeID(), 502fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 503e8d8bef9SDimitry Andric } 504e8d8bef9SDimitry Andric 505*0b57cec5SDimitry Andric MachineMemOperand * 506*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 507fe6060f1SDimitry Andric int64_t Offset, LLT Ty) { 508*0b57cec5SDimitry Andric const MachinePointerInfo &PtrInfo = MMO->getPointerInfo(); 509*0b57cec5SDimitry Andric 510*0b57cec5SDimitry Andric // If there is no pointer value, the offset isn't tracked so we need to adjust 511*0b57cec5SDimitry Andric // the base alignment. 5125ffd83dbSDimitry Andric Align Alignment = PtrInfo.V.isNull() 5135ffd83dbSDimitry Andric ? commonAlignment(MMO->getBaseAlign(), Offset) 5145ffd83dbSDimitry Andric : MMO->getBaseAlign(); 515*0b57cec5SDimitry Andric 516e8d8bef9SDimitry Andric // Do not preserve ranges, since we don't necessarily know what the high bits 517e8d8bef9SDimitry Andric // are anymore. 518fe6060f1SDimitry Andric return new (Allocator) MachineMemOperand( 519fe6060f1SDimitry Andric PtrInfo.getWithOffset(Offset), MMO->getFlags(), Ty, Alignment, 520fe6060f1SDimitry Andric MMO->getAAInfo(), nullptr, MMO->getSyncScopeID(), 521fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 522*0b57cec5SDimitry Andric } 523*0b57cec5SDimitry Andric 524*0b57cec5SDimitry Andric MachineMemOperand * 525*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 526*0b57cec5SDimitry Andric const AAMDNodes &AAInfo) { 527*0b57cec5SDimitry Andric MachinePointerInfo MPI = MMO->getValue() ? 528*0b57cec5SDimitry Andric MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : 529*0b57cec5SDimitry Andric MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); 530*0b57cec5SDimitry Andric 5315ffd83dbSDimitry Andric return new (Allocator) MachineMemOperand( 5325ffd83dbSDimitry Andric MPI, MMO->getFlags(), MMO->getSize(), MMO->getBaseAlign(), AAInfo, 533fe6060f1SDimitry Andric MMO->getRanges(), MMO->getSyncScopeID(), MMO->getSuccessOrdering(), 5345ffd83dbSDimitry Andric MMO->getFailureOrdering()); 535*0b57cec5SDimitry Andric } 536*0b57cec5SDimitry Andric 537*0b57cec5SDimitry Andric MachineMemOperand * 538*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 539*0b57cec5SDimitry Andric MachineMemOperand::Flags Flags) { 540*0b57cec5SDimitry Andric return new (Allocator) MachineMemOperand( 5415ffd83dbSDimitry Andric MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlign(), 542*0b57cec5SDimitry Andric MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(), 543fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 544*0b57cec5SDimitry Andric } 545*0b57cec5SDimitry Andric 546480093f4SDimitry Andric MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfo( 547c14a5a88SDimitry Andric ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol, 548bdd1243dSDimitry Andric MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker, MDNode *PCSections, 549bdd1243dSDimitry Andric uint32_t CFIType) { 550c14a5a88SDimitry Andric return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, 551bdd1243dSDimitry Andric PostInstrSymbol, HeapAllocMarker, 552bdd1243dSDimitry Andric PCSections, CFIType); 553*0b57cec5SDimitry Andric } 554*0b57cec5SDimitry Andric 555*0b57cec5SDimitry Andric const char *MachineFunction::createExternalSymbolName(StringRef Name) { 556*0b57cec5SDimitry Andric char *Dest = Allocator.Allocate<char>(Name.size() + 1); 557*0b57cec5SDimitry Andric llvm::copy(Name, Dest); 558*0b57cec5SDimitry Andric Dest[Name.size()] = 0; 559*0b57cec5SDimitry Andric return Dest; 560*0b57cec5SDimitry Andric } 561*0b57cec5SDimitry Andric 562*0b57cec5SDimitry Andric uint32_t *MachineFunction::allocateRegMask() { 563*0b57cec5SDimitry Andric unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); 564*0b57cec5SDimitry Andric unsigned Size = MachineOperand::getRegMaskSize(NumRegs); 565*0b57cec5SDimitry Andric uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); 566*0b57cec5SDimitry Andric memset(Mask, 0, Size * sizeof(Mask[0])); 567*0b57cec5SDimitry Andric return Mask; 568*0b57cec5SDimitry Andric } 569*0b57cec5SDimitry Andric 570480093f4SDimitry Andric ArrayRef<int> MachineFunction::allocateShuffleMask(ArrayRef<int> Mask) { 571480093f4SDimitry Andric int* AllocMask = Allocator.Allocate<int>(Mask.size()); 572480093f4SDimitry Andric copy(Mask, AllocMask); 573480093f4SDimitry Andric return {AllocMask, Mask.size()}; 574480093f4SDimitry Andric } 575480093f4SDimitry Andric 576*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 577*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineFunction::dump() const { 578*0b57cec5SDimitry Andric print(dbgs()); 579*0b57cec5SDimitry Andric } 580*0b57cec5SDimitry Andric #endif 581*0b57cec5SDimitry Andric 582*0b57cec5SDimitry Andric StringRef MachineFunction::getName() const { 583*0b57cec5SDimitry Andric return getFunction().getName(); 584*0b57cec5SDimitry Andric } 585*0b57cec5SDimitry Andric 586*0b57cec5SDimitry Andric void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { 587*0b57cec5SDimitry Andric OS << "# Machine code for function " << getName() << ": "; 588*0b57cec5SDimitry Andric getProperties().print(OS); 589*0b57cec5SDimitry Andric OS << '\n'; 590*0b57cec5SDimitry Andric 591*0b57cec5SDimitry Andric // Print Frame Information 592*0b57cec5SDimitry Andric FrameInfo->print(*this, OS); 593*0b57cec5SDimitry Andric 594*0b57cec5SDimitry Andric // Print JumpTable Information 595*0b57cec5SDimitry Andric if (JumpTableInfo) 596*0b57cec5SDimitry Andric JumpTableInfo->print(OS); 597*0b57cec5SDimitry Andric 598*0b57cec5SDimitry Andric // Print Constant Pool 599*0b57cec5SDimitry Andric ConstantPool->print(OS); 600*0b57cec5SDimitry Andric 601*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); 602*0b57cec5SDimitry Andric 603*0b57cec5SDimitry Andric if (RegInfo && !RegInfo->livein_empty()) { 604*0b57cec5SDimitry Andric OS << "Function Live Ins: "; 605*0b57cec5SDimitry Andric for (MachineRegisterInfo::livein_iterator 606*0b57cec5SDimitry Andric I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 607*0b57cec5SDimitry Andric OS << printReg(I->first, TRI); 608*0b57cec5SDimitry Andric if (I->second) 609*0b57cec5SDimitry Andric OS << " in " << printReg(I->second, TRI); 610*0b57cec5SDimitry Andric if (std::next(I) != E) 611*0b57cec5SDimitry Andric OS << ", "; 612*0b57cec5SDimitry Andric } 613*0b57cec5SDimitry Andric OS << '\n'; 614*0b57cec5SDimitry Andric } 615*0b57cec5SDimitry Andric 616*0b57cec5SDimitry Andric ModuleSlotTracker MST(getFunction().getParent()); 617*0b57cec5SDimitry Andric MST.incorporateFunction(getFunction()); 618*0b57cec5SDimitry Andric for (const auto &BB : *this) { 619*0b57cec5SDimitry Andric OS << '\n'; 620*0b57cec5SDimitry Andric // If we print the whole function, print it at its most verbose level. 621*0b57cec5SDimitry Andric BB.print(OS, MST, Indexes, /*IsStandalone=*/true); 622*0b57cec5SDimitry Andric } 623*0b57cec5SDimitry Andric 624*0b57cec5SDimitry Andric OS << "\n# End machine code for function " << getName() << ".\n\n"; 625*0b57cec5SDimitry Andric } 626*0b57cec5SDimitry Andric 627480093f4SDimitry Andric /// True if this function needs frame moves for debug or exceptions. 628480093f4SDimitry Andric bool MachineFunction::needsFrameMoves() const { 629480093f4SDimitry Andric return getMMI().hasDebugInfo() || 630480093f4SDimitry Andric getTarget().Options.ForceDwarfFrameSection || 631480093f4SDimitry Andric F.needsUnwindTableEntry(); 632480093f4SDimitry Andric } 633480093f4SDimitry Andric 634*0b57cec5SDimitry Andric namespace llvm { 635*0b57cec5SDimitry Andric 636*0b57cec5SDimitry Andric template<> 637*0b57cec5SDimitry Andric struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 638*0b57cec5SDimitry Andric DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 639*0b57cec5SDimitry Andric 640*0b57cec5SDimitry Andric static std::string getGraphName(const MachineFunction *F) { 641*0b57cec5SDimitry Andric return ("CFG for '" + F->getName() + "' function").str(); 642*0b57cec5SDimitry Andric } 643*0b57cec5SDimitry Andric 644*0b57cec5SDimitry Andric std::string getNodeLabel(const MachineBasicBlock *Node, 645*0b57cec5SDimitry Andric const MachineFunction *Graph) { 646*0b57cec5SDimitry Andric std::string OutStr; 647*0b57cec5SDimitry Andric { 648*0b57cec5SDimitry Andric raw_string_ostream OSS(OutStr); 649*0b57cec5SDimitry Andric 650*0b57cec5SDimitry Andric if (isSimple()) { 651*0b57cec5SDimitry Andric OSS << printMBBReference(*Node); 652*0b57cec5SDimitry Andric if (const BasicBlock *BB = Node->getBasicBlock()) 653*0b57cec5SDimitry Andric OSS << ": " << BB->getName(); 654*0b57cec5SDimitry Andric } else 655*0b57cec5SDimitry Andric Node->print(OSS); 656*0b57cec5SDimitry Andric } 657*0b57cec5SDimitry Andric 658*0b57cec5SDimitry Andric if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 659*0b57cec5SDimitry Andric 660*0b57cec5SDimitry Andric // Process string output to make it nicer... 661*0b57cec5SDimitry Andric for (unsigned i = 0; i != OutStr.length(); ++i) 662*0b57cec5SDimitry Andric if (OutStr[i] == '\n') { // Left justify 663*0b57cec5SDimitry Andric OutStr[i] = '\\'; 664*0b57cec5SDimitry Andric OutStr.insert(OutStr.begin()+i+1, 'l'); 665*0b57cec5SDimitry Andric } 666*0b57cec5SDimitry Andric return OutStr; 667*0b57cec5SDimitry Andric } 668*0b57cec5SDimitry Andric }; 669*0b57cec5SDimitry Andric 670*0b57cec5SDimitry Andric } // end namespace llvm 671*0b57cec5SDimitry Andric 672*0b57cec5SDimitry Andric void MachineFunction::viewCFG() const 673*0b57cec5SDimitry Andric { 674*0b57cec5SDimitry Andric #ifndef NDEBUG 675*0b57cec5SDimitry Andric ViewGraph(this, "mf" + getName()); 676*0b57cec5SDimitry Andric #else 677*0b57cec5SDimitry Andric errs() << "MachineFunction::viewCFG is only available in debug builds on " 678*0b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 679*0b57cec5SDimitry Andric #endif // NDEBUG 680*0b57cec5SDimitry Andric } 681*0b57cec5SDimitry Andric 682*0b57cec5SDimitry Andric void MachineFunction::viewCFGOnly() const 683*0b57cec5SDimitry Andric { 684*0b57cec5SDimitry Andric #ifndef NDEBUG 685*0b57cec5SDimitry Andric ViewGraph(this, "mf" + getName(), true); 686*0b57cec5SDimitry Andric #else 687*0b57cec5SDimitry Andric errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 688*0b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 689*0b57cec5SDimitry Andric #endif // NDEBUG 690*0b57cec5SDimitry Andric } 691*0b57cec5SDimitry Andric 692*0b57cec5SDimitry Andric /// Add the specified physical register as a live-in value and 693*0b57cec5SDimitry Andric /// create a corresponding virtual register for it. 6945ffd83dbSDimitry Andric Register MachineFunction::addLiveIn(MCRegister PReg, 695*0b57cec5SDimitry Andric const TargetRegisterClass *RC) { 696*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = getRegInfo(); 6975ffd83dbSDimitry Andric Register VReg = MRI.getLiveInVirtReg(PReg); 698*0b57cec5SDimitry Andric if (VReg) { 699*0b57cec5SDimitry Andric const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); 700*0b57cec5SDimitry Andric (void)VRegRC; 701*0b57cec5SDimitry Andric // A physical register can be added several times. 702*0b57cec5SDimitry Andric // Between two calls, the register class of the related virtual register 703*0b57cec5SDimitry Andric // may have been constrained to match some operation constraints. 704*0b57cec5SDimitry Andric // In that case, check that the current register class includes the 705*0b57cec5SDimitry Andric // physical register and is a sub class of the specified RC. 706*0b57cec5SDimitry Andric assert((VRegRC == RC || (VRegRC->contains(PReg) && 707*0b57cec5SDimitry Andric RC->hasSubClassEq(VRegRC))) && 708*0b57cec5SDimitry Andric "Register class mismatch!"); 709*0b57cec5SDimitry Andric return VReg; 710*0b57cec5SDimitry Andric } 711*0b57cec5SDimitry Andric VReg = MRI.createVirtualRegister(RC); 712*0b57cec5SDimitry Andric MRI.addLiveIn(PReg, VReg); 713*0b57cec5SDimitry Andric return VReg; 714*0b57cec5SDimitry Andric } 715*0b57cec5SDimitry Andric 716*0b57cec5SDimitry Andric /// Return the MCSymbol for the specified non-empty jump table. 717*0b57cec5SDimitry Andric /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 718*0b57cec5SDimitry Andric /// normal 'L' label is returned. 719*0b57cec5SDimitry Andric MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 720*0b57cec5SDimitry Andric bool isLinkerPrivate) const { 721*0b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 722*0b57cec5SDimitry Andric assert(JumpTableInfo && "No jump tables"); 723*0b57cec5SDimitry Andric assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 724*0b57cec5SDimitry Andric 725*0b57cec5SDimitry Andric StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() 726*0b57cec5SDimitry Andric : DL.getPrivateGlobalPrefix(); 727*0b57cec5SDimitry Andric SmallString<60> Name; 728*0b57cec5SDimitry Andric raw_svector_ostream(Name) 729*0b57cec5SDimitry Andric << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 730*0b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Name); 731*0b57cec5SDimitry Andric } 732*0b57cec5SDimitry Andric 733*0b57cec5SDimitry Andric /// Return a function-local symbol to represent the PIC base. 734*0b57cec5SDimitry Andric MCSymbol *MachineFunction::getPICBaseSymbol() const { 735*0b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 736*0b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 737*0b57cec5SDimitry Andric Twine(getFunctionNumber()) + "$pb"); 738*0b57cec5SDimitry Andric } 739*0b57cec5SDimitry Andric 740*0b57cec5SDimitry Andric /// \name Exception Handling 741*0b57cec5SDimitry Andric /// \{ 742*0b57cec5SDimitry Andric 743*0b57cec5SDimitry Andric LandingPadInfo & 744*0b57cec5SDimitry Andric MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { 745*0b57cec5SDimitry Andric unsigned N = LandingPads.size(); 746*0b57cec5SDimitry Andric for (unsigned i = 0; i < N; ++i) { 747*0b57cec5SDimitry Andric LandingPadInfo &LP = LandingPads[i]; 748*0b57cec5SDimitry Andric if (LP.LandingPadBlock == LandingPad) 749*0b57cec5SDimitry Andric return LP; 750*0b57cec5SDimitry Andric } 751*0b57cec5SDimitry Andric 752*0b57cec5SDimitry Andric LandingPads.push_back(LandingPadInfo(LandingPad)); 753*0b57cec5SDimitry Andric return LandingPads[N]; 754*0b57cec5SDimitry Andric } 755*0b57cec5SDimitry Andric 756*0b57cec5SDimitry Andric void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, 757*0b57cec5SDimitry Andric MCSymbol *BeginLabel, MCSymbol *EndLabel) { 758*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 759*0b57cec5SDimitry Andric LP.BeginLabels.push_back(BeginLabel); 760*0b57cec5SDimitry Andric LP.EndLabels.push_back(EndLabel); 761*0b57cec5SDimitry Andric } 762*0b57cec5SDimitry Andric 763*0b57cec5SDimitry Andric MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { 764*0b57cec5SDimitry Andric MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); 765*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 766*0b57cec5SDimitry Andric LP.LandingPadLabel = LandingPadLabel; 767*0b57cec5SDimitry Andric 768*0b57cec5SDimitry Andric const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); 769*0b57cec5SDimitry Andric if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { 770bdd1243dSDimitry Andric // If there's no typeid list specified, then "cleanup" is implicit. 771bdd1243dSDimitry Andric // Otherwise, id 0 is reserved for the cleanup action. 772bdd1243dSDimitry Andric if (LPI->isCleanup() && LPI->getNumClauses() != 0) 773bdd1243dSDimitry Andric LP.TypeIds.push_back(0); 774*0b57cec5SDimitry Andric 775*0b57cec5SDimitry Andric // FIXME: New EH - Add the clauses in reverse order. This isn't 100% 776*0b57cec5SDimitry Andric // correct, but we need to do it this way because of how the DWARF EH 777*0b57cec5SDimitry Andric // emitter processes the clauses. 778*0b57cec5SDimitry Andric for (unsigned I = LPI->getNumClauses(); I != 0; --I) { 779*0b57cec5SDimitry Andric Value *Val = LPI->getClause(I - 1); 780*0b57cec5SDimitry Andric if (LPI->isCatch(I - 1)) { 781bdd1243dSDimitry Andric LP.TypeIds.push_back( 782bdd1243dSDimitry Andric getTypeIDFor(dyn_cast<GlobalValue>(Val->stripPointerCasts()))); 783*0b57cec5SDimitry Andric } else { 784*0b57cec5SDimitry Andric // Add filters in a list. 785*0b57cec5SDimitry Andric auto *CVal = cast<Constant>(Val); 786bdd1243dSDimitry Andric SmallVector<unsigned, 4> FilterList; 787349cc55cSDimitry Andric for (const Use &U : CVal->operands()) 788bdd1243dSDimitry Andric FilterList.push_back( 789bdd1243dSDimitry Andric getTypeIDFor(cast<GlobalValue>(U->stripPointerCasts()))); 790*0b57cec5SDimitry Andric 791bdd1243dSDimitry Andric LP.TypeIds.push_back(getFilterIDFor(FilterList)); 792*0b57cec5SDimitry Andric } 793*0b57cec5SDimitry Andric } 794*0b57cec5SDimitry Andric 795*0b57cec5SDimitry Andric } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { 796bdd1243dSDimitry Andric for (unsigned I = CPI->arg_size(); I != 0; --I) { 797bdd1243dSDimitry Andric auto *TypeInfo = 798bdd1243dSDimitry Andric dyn_cast<GlobalValue>(CPI->getArgOperand(I - 1)->stripPointerCasts()); 799bdd1243dSDimitry Andric LP.TypeIds.push_back(getTypeIDFor(TypeInfo)); 800*0b57cec5SDimitry Andric } 801*0b57cec5SDimitry Andric 802*0b57cec5SDimitry Andric } else { 803*0b57cec5SDimitry Andric assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!"); 804*0b57cec5SDimitry Andric } 805*0b57cec5SDimitry Andric 806*0b57cec5SDimitry Andric return LandingPadLabel; 807*0b57cec5SDimitry Andric } 808*0b57cec5SDimitry Andric 809*0b57cec5SDimitry Andric void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, 810*0b57cec5SDimitry Andric ArrayRef<unsigned> Sites) { 811*0b57cec5SDimitry Andric LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); 812*0b57cec5SDimitry Andric } 813*0b57cec5SDimitry Andric 814*0b57cec5SDimitry Andric unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { 815*0b57cec5SDimitry Andric for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 816*0b57cec5SDimitry Andric if (TypeInfos[i] == TI) return i + 1; 817*0b57cec5SDimitry Andric 818*0b57cec5SDimitry Andric TypeInfos.push_back(TI); 819*0b57cec5SDimitry Andric return TypeInfos.size(); 820*0b57cec5SDimitry Andric } 821*0b57cec5SDimitry Andric 822bdd1243dSDimitry Andric int MachineFunction::getFilterIDFor(ArrayRef<unsigned> TyIds) { 823*0b57cec5SDimitry Andric // If the new filter coincides with the tail of an existing filter, then 824*0b57cec5SDimitry Andric // re-use the existing filter. Folding filters more than this requires 825*0b57cec5SDimitry Andric // re-ordering filters and/or their elements - probably not worth it. 826fe6060f1SDimitry Andric for (unsigned i : FilterEnds) { 827fe6060f1SDimitry Andric unsigned j = TyIds.size(); 828*0b57cec5SDimitry Andric 829*0b57cec5SDimitry Andric while (i && j) 830*0b57cec5SDimitry Andric if (FilterIds[--i] != TyIds[--j]) 831*0b57cec5SDimitry Andric goto try_next; 832*0b57cec5SDimitry Andric 833*0b57cec5SDimitry Andric if (!j) 834*0b57cec5SDimitry Andric // The new filter coincides with range [i, end) of the existing filter. 835*0b57cec5SDimitry Andric return -(1 + i); 836*0b57cec5SDimitry Andric 837*0b57cec5SDimitry Andric try_next:; 838*0b57cec5SDimitry Andric } 839*0b57cec5SDimitry Andric 840*0b57cec5SDimitry Andric // Add the new filter. 841*0b57cec5SDimitry Andric int FilterID = -(1 + FilterIds.size()); 842*0b57cec5SDimitry Andric FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 843e8d8bef9SDimitry Andric llvm::append_range(FilterIds, TyIds); 844*0b57cec5SDimitry Andric FilterEnds.push_back(FilterIds.size()); 845*0b57cec5SDimitry Andric FilterIds.push_back(0); // terminator 846*0b57cec5SDimitry Andric return FilterID; 847*0b57cec5SDimitry Andric } 848*0b57cec5SDimitry Andric 849480093f4SDimitry Andric MachineFunction::CallSiteInfoMap::iterator 850480093f4SDimitry Andric MachineFunction::getCallSiteInfo(const MachineInstr *MI) { 8515ffd83dbSDimitry Andric assert(MI->isCandidateForCallSiteEntry() && 8525ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates"); 853*0b57cec5SDimitry Andric 8545ffd83dbSDimitry Andric if (!Target.Options.EmitCallSiteInfo) 855480093f4SDimitry Andric return CallSitesInfo.end(); 856480093f4SDimitry Andric return CallSitesInfo.find(MI); 857*0b57cec5SDimitry Andric } 858*0b57cec5SDimitry Andric 8595ffd83dbSDimitry Andric /// Return the call machine instruction or find a call within bundle. 8605ffd83dbSDimitry Andric static const MachineInstr *getCallInstr(const MachineInstr *MI) { 8615ffd83dbSDimitry Andric if (!MI->isBundle()) 8625ffd83dbSDimitry Andric return MI; 863*0b57cec5SDimitry Andric 864fcaf7f86SDimitry Andric for (const auto &BMI : make_range(getBundleStart(MI->getIterator()), 8655ffd83dbSDimitry Andric getBundleEnd(MI->getIterator()))) 8665ffd83dbSDimitry Andric if (BMI.isCandidateForCallSiteEntry()) 8675ffd83dbSDimitry Andric return &BMI; 8688bcb0991SDimitry Andric 8695ffd83dbSDimitry Andric llvm_unreachable("Unexpected bundle without a call site candidate"); 8708bcb0991SDimitry Andric } 8718bcb0991SDimitry Andric 8728bcb0991SDimitry Andric void MachineFunction::eraseCallSiteInfo(const MachineInstr *MI) { 8735ffd83dbSDimitry Andric assert(MI->shouldUpdateCallSiteInfo() && 8745ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 8755ffd83dbSDimitry Andric "candidates inside bundles"); 8765ffd83dbSDimitry Andric 8775ffd83dbSDimitry Andric const MachineInstr *CallMI = getCallInstr(MI); 8785ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(CallMI); 8798bcb0991SDimitry Andric if (CSIt == CallSitesInfo.end()) 8808bcb0991SDimitry Andric return; 8818bcb0991SDimitry Andric CallSitesInfo.erase(CSIt); 8828bcb0991SDimitry Andric } 8838bcb0991SDimitry Andric 8848bcb0991SDimitry Andric void MachineFunction::copyCallSiteInfo(const MachineInstr *Old, 8858bcb0991SDimitry Andric const MachineInstr *New) { 8865ffd83dbSDimitry Andric assert(Old->shouldUpdateCallSiteInfo() && 8875ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 8885ffd83dbSDimitry Andric "candidates inside bundles"); 8898bcb0991SDimitry Andric 8905ffd83dbSDimitry Andric if (!New->isCandidateForCallSiteEntry()) 8915ffd83dbSDimitry Andric return eraseCallSiteInfo(Old); 8925ffd83dbSDimitry Andric 8935ffd83dbSDimitry Andric const MachineInstr *OldCallMI = getCallInstr(Old); 8945ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 8958bcb0991SDimitry Andric if (CSIt == CallSitesInfo.end()) 8968bcb0991SDimitry Andric return; 8978bcb0991SDimitry Andric 8988bcb0991SDimitry Andric CallSiteInfo CSInfo = CSIt->second; 899*0b57cec5SDimitry Andric CallSitesInfo[New] = CSInfo; 900*0b57cec5SDimitry Andric } 901*0b57cec5SDimitry Andric 9025ffd83dbSDimitry Andric void MachineFunction::moveCallSiteInfo(const MachineInstr *Old, 9035ffd83dbSDimitry Andric const MachineInstr *New) { 9045ffd83dbSDimitry Andric assert(Old->shouldUpdateCallSiteInfo() && 9055ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9065ffd83dbSDimitry Andric "candidates inside bundles"); 9075ffd83dbSDimitry Andric 9085ffd83dbSDimitry Andric if (!New->isCandidateForCallSiteEntry()) 9095ffd83dbSDimitry Andric return eraseCallSiteInfo(Old); 9105ffd83dbSDimitry Andric 9115ffd83dbSDimitry Andric const MachineInstr *OldCallMI = getCallInstr(Old); 9125ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 9135ffd83dbSDimitry Andric if (CSIt == CallSitesInfo.end()) 9145ffd83dbSDimitry Andric return; 9155ffd83dbSDimitry Andric 9165ffd83dbSDimitry Andric CallSiteInfo CSInfo = std::move(CSIt->second); 9175ffd83dbSDimitry Andric CallSitesInfo.erase(CSIt); 9185ffd83dbSDimitry Andric CallSitesInfo[New] = CSInfo; 9195ffd83dbSDimitry Andric } 9205ffd83dbSDimitry Andric 921e8d8bef9SDimitry Andric void MachineFunction::setDebugInstrNumberingCount(unsigned Num) { 922e8d8bef9SDimitry Andric DebugInstrNumberingCount = Num; 923e8d8bef9SDimitry Andric } 924e8d8bef9SDimitry Andric 925e8d8bef9SDimitry Andric void MachineFunction::makeDebugValueSubstitution(DebugInstrOperandPair A, 926fe6060f1SDimitry Andric DebugInstrOperandPair B, 927fe6060f1SDimitry Andric unsigned Subreg) { 928fe6060f1SDimitry Andric // Catch any accidental self-loops. 929fe6060f1SDimitry Andric assert(A.first != B.first); 930349cc55cSDimitry Andric // Don't allow any substitutions _from_ the memory operand number. 931349cc55cSDimitry Andric assert(A.second != DebugOperandMemNumber); 932349cc55cSDimitry Andric 933fe6060f1SDimitry Andric DebugValueSubstitutions.push_back({A, B, Subreg}); 934e8d8bef9SDimitry Andric } 935e8d8bef9SDimitry Andric 936e8d8bef9SDimitry Andric void MachineFunction::substituteDebugValuesForInst(const MachineInstr &Old, 937e8d8bef9SDimitry Andric MachineInstr &New, 938e8d8bef9SDimitry Andric unsigned MaxOperand) { 939e8d8bef9SDimitry Andric // If the Old instruction wasn't tracked at all, there is no work to do. 940e8d8bef9SDimitry Andric unsigned OldInstrNum = Old.peekDebugInstrNum(); 941e8d8bef9SDimitry Andric if (!OldInstrNum) 942e8d8bef9SDimitry Andric return; 943e8d8bef9SDimitry Andric 944e8d8bef9SDimitry Andric // Iterate over all operands looking for defs to create substitutions for. 945e8d8bef9SDimitry Andric // Avoid creating new instr numbers unless we create a new substitution. 946e8d8bef9SDimitry Andric // While this has no functional effect, it risks confusing someone reading 947e8d8bef9SDimitry Andric // MIR output. 948e8d8bef9SDimitry Andric // Examine all the operands, or the first N specified by the caller. 949e8d8bef9SDimitry Andric MaxOperand = std::min(MaxOperand, Old.getNumOperands()); 950fe6060f1SDimitry Andric for (unsigned int I = 0; I < MaxOperand; ++I) { 951e8d8bef9SDimitry Andric const auto &OldMO = Old.getOperand(I); 952e8d8bef9SDimitry Andric auto &NewMO = New.getOperand(I); 953e8d8bef9SDimitry Andric (void)NewMO; 954e8d8bef9SDimitry Andric 955e8d8bef9SDimitry Andric if (!OldMO.isReg() || !OldMO.isDef()) 956e8d8bef9SDimitry Andric continue; 957e8d8bef9SDimitry Andric assert(NewMO.isDef()); 958e8d8bef9SDimitry Andric 959e8d8bef9SDimitry Andric unsigned NewInstrNum = New.getDebugInstrNum(); 960e8d8bef9SDimitry Andric makeDebugValueSubstitution(std::make_pair(OldInstrNum, I), 961e8d8bef9SDimitry Andric std::make_pair(NewInstrNum, I)); 962e8d8bef9SDimitry Andric } 963e8d8bef9SDimitry Andric } 964e8d8bef9SDimitry Andric 96581ad6265SDimitry Andric auto MachineFunction::salvageCopySSA( 96681ad6265SDimitry Andric MachineInstr &MI, DenseMap<Register, DebugInstrOperandPair> &DbgPHICache) 96781ad6265SDimitry Andric -> DebugInstrOperandPair { 96881ad6265SDimitry Andric const TargetInstrInfo &TII = *getSubtarget().getInstrInfo(); 96981ad6265SDimitry Andric 97081ad6265SDimitry Andric // Check whether this copy-like instruction has already been salvaged into 97181ad6265SDimitry Andric // an operand pair. 97281ad6265SDimitry Andric Register Dest; 97381ad6265SDimitry Andric if (auto CopyDstSrc = TII.isCopyInstr(MI)) { 97481ad6265SDimitry Andric Dest = CopyDstSrc->Destination->getReg(); 97581ad6265SDimitry Andric } else { 97681ad6265SDimitry Andric assert(MI.isSubregToReg()); 97781ad6265SDimitry Andric Dest = MI.getOperand(0).getReg(); 97881ad6265SDimitry Andric } 97981ad6265SDimitry Andric 98081ad6265SDimitry Andric auto CacheIt = DbgPHICache.find(Dest); 98181ad6265SDimitry Andric if (CacheIt != DbgPHICache.end()) 98281ad6265SDimitry Andric return CacheIt->second; 98381ad6265SDimitry Andric 98481ad6265SDimitry Andric // Calculate the instruction number to use, or install a DBG_PHI. 98581ad6265SDimitry Andric auto OperandPair = salvageCopySSAImpl(MI); 98681ad6265SDimitry Andric DbgPHICache.insert({Dest, OperandPair}); 98781ad6265SDimitry Andric return OperandPair; 98881ad6265SDimitry Andric } 98981ad6265SDimitry Andric 99081ad6265SDimitry Andric auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI) 991fe6060f1SDimitry Andric -> DebugInstrOperandPair { 992fe6060f1SDimitry Andric MachineRegisterInfo &MRI = getRegInfo(); 993fe6060f1SDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 994fe6060f1SDimitry Andric const TargetInstrInfo &TII = *getSubtarget().getInstrInfo(); 995fe6060f1SDimitry Andric 996fe6060f1SDimitry Andric // Chase the value read by a copy-like instruction back to the instruction 997fe6060f1SDimitry Andric // that ultimately _defines_ that value. This may pass: 998fe6060f1SDimitry Andric // * Through multiple intermediate copies, including subregister moves / 999fe6060f1SDimitry Andric // copies, 1000fe6060f1SDimitry Andric // * Copies from physical registers that must then be traced back to the 1001fe6060f1SDimitry Andric // defining instruction, 1002fe6060f1SDimitry Andric // * Or, physical registers may be live-in to (only) the entry block, which 1003fe6060f1SDimitry Andric // requires a DBG_PHI to be created. 1004fe6060f1SDimitry Andric // We can pursue this problem in that order: trace back through copies, 1005fe6060f1SDimitry Andric // optionally through a physical register, to a defining instruction. We 1006fe6060f1SDimitry Andric // should never move from physreg to vreg. As we're still in SSA form, no need 1007fe6060f1SDimitry Andric // to worry about partial definitions of registers. 1008fe6060f1SDimitry Andric 1009fe6060f1SDimitry Andric // Helper lambda to interpret a copy-like instruction. Takes instruction, 1010fe6060f1SDimitry Andric // returns the register read and any subregister identifying which part is 1011fe6060f1SDimitry Andric // read. 1012fe6060f1SDimitry Andric auto GetRegAndSubreg = 1013fe6060f1SDimitry Andric [&](const MachineInstr &Cpy) -> std::pair<Register, unsigned> { 1014fe6060f1SDimitry Andric Register NewReg, OldReg; 1015fe6060f1SDimitry Andric unsigned SubReg; 1016fe6060f1SDimitry Andric if (Cpy.isCopy()) { 1017fe6060f1SDimitry Andric OldReg = Cpy.getOperand(0).getReg(); 1018fe6060f1SDimitry Andric NewReg = Cpy.getOperand(1).getReg(); 1019fe6060f1SDimitry Andric SubReg = Cpy.getOperand(1).getSubReg(); 1020fe6060f1SDimitry Andric } else if (Cpy.isSubregToReg()) { 1021fe6060f1SDimitry Andric OldReg = Cpy.getOperand(0).getReg(); 1022fe6060f1SDimitry Andric NewReg = Cpy.getOperand(2).getReg(); 1023fe6060f1SDimitry Andric SubReg = Cpy.getOperand(3).getImm(); 1024fe6060f1SDimitry Andric } else { 1025fe6060f1SDimitry Andric auto CopyDetails = *TII.isCopyInstr(Cpy); 1026fe6060f1SDimitry Andric const MachineOperand &Src = *CopyDetails.Source; 1027fe6060f1SDimitry Andric const MachineOperand &Dest = *CopyDetails.Destination; 1028fe6060f1SDimitry Andric OldReg = Dest.getReg(); 1029fe6060f1SDimitry Andric NewReg = Src.getReg(); 1030fe6060f1SDimitry Andric SubReg = Src.getSubReg(); 1031fe6060f1SDimitry Andric } 1032fe6060f1SDimitry Andric 1033fe6060f1SDimitry Andric return {NewReg, SubReg}; 1034fe6060f1SDimitry Andric }; 1035fe6060f1SDimitry Andric 1036fe6060f1SDimitry Andric // First seek either the defining instruction, or a copy from a physreg. 1037fe6060f1SDimitry Andric // During search, the current state is the current copy instruction, and which 1038fe6060f1SDimitry Andric // register we've read. Accumulate qualifying subregisters into SubregsSeen; 1039fe6060f1SDimitry Andric // deal with those later. 1040fe6060f1SDimitry Andric auto State = GetRegAndSubreg(MI); 1041fe6060f1SDimitry Andric auto CurInst = MI.getIterator(); 1042fe6060f1SDimitry Andric SmallVector<unsigned, 4> SubregsSeen; 1043fe6060f1SDimitry Andric while (true) { 1044fe6060f1SDimitry Andric // If we've found a copy from a physreg, first portion of search is over. 1045fe6060f1SDimitry Andric if (!State.first.isVirtual()) 1046fe6060f1SDimitry Andric break; 1047fe6060f1SDimitry Andric 1048fe6060f1SDimitry Andric // Record any subregister qualifier. 1049fe6060f1SDimitry Andric if (State.second) 1050fe6060f1SDimitry Andric SubregsSeen.push_back(State.second); 1051fe6060f1SDimitry Andric 1052fe6060f1SDimitry Andric assert(MRI.hasOneDef(State.first)); 1053fe6060f1SDimitry Andric MachineInstr &Inst = *MRI.def_begin(State.first)->getParent(); 1054fe6060f1SDimitry Andric CurInst = Inst.getIterator(); 1055fe6060f1SDimitry Andric 1056fe6060f1SDimitry Andric // Any non-copy instruction is the defining instruction we're seeking. 1057fe6060f1SDimitry Andric if (!Inst.isCopyLike() && !TII.isCopyInstr(Inst)) 1058fe6060f1SDimitry Andric break; 1059fe6060f1SDimitry Andric State = GetRegAndSubreg(Inst); 1060fe6060f1SDimitry Andric }; 1061fe6060f1SDimitry Andric 1062fe6060f1SDimitry Andric // Helper lambda to apply additional subregister substitutions to a known 1063fe6060f1SDimitry Andric // instruction/operand pair. Adds new (fake) substitutions so that we can 1064fe6060f1SDimitry Andric // record the subregister. FIXME: this isn't very space efficient if multiple 1065fe6060f1SDimitry Andric // values are tracked back through the same copies; cache something later. 1066fe6060f1SDimitry Andric auto ApplySubregisters = 1067fe6060f1SDimitry Andric [&](DebugInstrOperandPair P) -> DebugInstrOperandPair { 1068fe6060f1SDimitry Andric for (unsigned Subreg : reverse(SubregsSeen)) { 1069fe6060f1SDimitry Andric // Fetch a new instruction number, not attached to an actual instruction. 1070fe6060f1SDimitry Andric unsigned NewInstrNumber = getNewDebugInstrNum(); 1071fe6060f1SDimitry Andric // Add a substitution from the "new" number to the known one, with a 1072fe6060f1SDimitry Andric // qualifying subreg. 1073fe6060f1SDimitry Andric makeDebugValueSubstitution({NewInstrNumber, 0}, P, Subreg); 1074fe6060f1SDimitry Andric // Return the new number; to find the underlying value, consumers need to 1075fe6060f1SDimitry Andric // deal with the qualifying subreg. 1076fe6060f1SDimitry Andric P = {NewInstrNumber, 0}; 1077fe6060f1SDimitry Andric } 1078fe6060f1SDimitry Andric return P; 1079fe6060f1SDimitry Andric }; 1080fe6060f1SDimitry Andric 1081fe6060f1SDimitry Andric // If we managed to find the defining instruction after COPYs, return an 1082fe6060f1SDimitry Andric // instruction / operand pair after adding subregister qualifiers. 1083fe6060f1SDimitry Andric if (State.first.isVirtual()) { 1084fe6060f1SDimitry Andric // Virtual register def -- we can just look up where this happens. 1085fe6060f1SDimitry Andric MachineInstr *Inst = MRI.def_begin(State.first)->getParent(); 1086fe6060f1SDimitry Andric for (auto &MO : Inst->operands()) { 1087fe6060f1SDimitry Andric if (!MO.isReg() || !MO.isDef() || MO.getReg() != State.first) 1088fe6060f1SDimitry Andric continue; 1089fe6060f1SDimitry Andric return ApplySubregisters( 1090fe6060f1SDimitry Andric {Inst->getDebugInstrNum(), Inst->getOperandNo(&MO)}); 1091fe6060f1SDimitry Andric } 1092fe6060f1SDimitry Andric 1093fe6060f1SDimitry Andric llvm_unreachable("Vreg def with no corresponding operand?"); 1094fe6060f1SDimitry Andric } 1095fe6060f1SDimitry Andric 1096fe6060f1SDimitry Andric // Our search ended in a copy from a physreg: walk back up the function 1097fe6060f1SDimitry Andric // looking for whatever defines the physreg. 1098fe6060f1SDimitry Andric assert(CurInst->isCopyLike() || TII.isCopyInstr(*CurInst)); 1099fe6060f1SDimitry Andric State = GetRegAndSubreg(*CurInst); 1100fe6060f1SDimitry Andric Register RegToSeek = State.first; 1101fe6060f1SDimitry Andric 1102fe6060f1SDimitry Andric auto RMII = CurInst->getReverseIterator(); 1103fe6060f1SDimitry Andric auto PrevInstrs = make_range(RMII, CurInst->getParent()->instr_rend()); 1104fe6060f1SDimitry Andric for (auto &ToExamine : PrevInstrs) { 1105fe6060f1SDimitry Andric for (auto &MO : ToExamine.operands()) { 1106fe6060f1SDimitry Andric // Test for operand that defines something aliasing RegToSeek. 1107fe6060f1SDimitry Andric if (!MO.isReg() || !MO.isDef() || 1108fe6060f1SDimitry Andric !TRI.regsOverlap(RegToSeek, MO.getReg())) 1109fe6060f1SDimitry Andric continue; 1110fe6060f1SDimitry Andric 1111fe6060f1SDimitry Andric return ApplySubregisters( 1112fe6060f1SDimitry Andric {ToExamine.getDebugInstrNum(), ToExamine.getOperandNo(&MO)}); 1113fe6060f1SDimitry Andric } 1114fe6060f1SDimitry Andric } 1115fe6060f1SDimitry Andric 1116fe6060f1SDimitry Andric MachineBasicBlock &InsertBB = *CurInst->getParent(); 1117fe6060f1SDimitry Andric 1118fe6060f1SDimitry Andric // We reached the start of the block before finding a defining instruction. 111981ad6265SDimitry Andric // There are numerous scenarios where this can happen: 112081ad6265SDimitry Andric // * Constant physical registers, 112181ad6265SDimitry Andric // * Several intrinsics that allow LLVM-IR to read arbitary registers, 112281ad6265SDimitry Andric // * Arguments in the entry block, 112381ad6265SDimitry Andric // * Exception handling landing pads. 112481ad6265SDimitry Andric // Validating all of them is too difficult, so just insert a DBG_PHI reading 112581ad6265SDimitry Andric // the variable value at this position, rather than checking it makes sense. 1126fe6060f1SDimitry Andric 1127fe6060f1SDimitry Andric // Create DBG_PHI for specified physreg. 1128fe6060f1SDimitry Andric auto Builder = BuildMI(InsertBB, InsertBB.getFirstNonPHI(), DebugLoc(), 1129fe6060f1SDimitry Andric TII.get(TargetOpcode::DBG_PHI)); 1130349cc55cSDimitry Andric Builder.addReg(State.first); 1131fe6060f1SDimitry Andric unsigned NewNum = getNewDebugInstrNum(); 1132fe6060f1SDimitry Andric Builder.addImm(NewNum); 1133fe6060f1SDimitry Andric return ApplySubregisters({NewNum, 0u}); 1134fe6060f1SDimitry Andric } 1135fe6060f1SDimitry Andric 1136fe6060f1SDimitry Andric void MachineFunction::finalizeDebugInstrRefs() { 1137fe6060f1SDimitry Andric auto *TII = getSubtarget().getInstrInfo(); 1138fe6060f1SDimitry Andric 11394824e7fdSDimitry Andric auto MakeUndefDbgValue = [&](MachineInstr &MI) { 1140bdd1243dSDimitry Andric const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_VALUE_LIST); 1141fe6060f1SDimitry Andric MI.setDesc(RefII); 1142bdd1243dSDimitry Andric MI.setDebugValueUndef(); 1143fe6060f1SDimitry Andric }; 1144fe6060f1SDimitry Andric 114581ad6265SDimitry Andric DenseMap<Register, DebugInstrOperandPair> ArgDbgPHIs; 1146fe6060f1SDimitry Andric for (auto &MBB : *this) { 1147fe6060f1SDimitry Andric for (auto &MI : MBB) { 1148bdd1243dSDimitry Andric if (!MI.isDebugRef()) 1149fe6060f1SDimitry Andric continue; 1150fe6060f1SDimitry Andric 1151bdd1243dSDimitry Andric bool IsValidRef = true; 1152bdd1243dSDimitry Andric 1153bdd1243dSDimitry Andric for (MachineOperand &MO : MI.debug_operands()) { 1154bdd1243dSDimitry Andric if (!MO.isReg()) 1155bdd1243dSDimitry Andric continue; 1156bdd1243dSDimitry Andric 1157bdd1243dSDimitry Andric Register Reg = MO.getReg(); 1158fe6060f1SDimitry Andric 1159fe6060f1SDimitry Andric // Some vregs can be deleted as redundant in the meantime. Mark those 11604824e7fdSDimitry Andric // as DBG_VALUE $noreg. Additionally, some normal instructions are 11614824e7fdSDimitry Andric // quickly deleted, leaving dangling references to vregs with no def. 11624824e7fdSDimitry Andric if (Reg == 0 || !RegInfo->hasOneDef(Reg)) { 1163bdd1243dSDimitry Andric IsValidRef = false; 1164bdd1243dSDimitry Andric break; 1165fe6060f1SDimitry Andric } 1166fe6060f1SDimitry Andric 1167fe6060f1SDimitry Andric assert(Reg.isVirtual()); 1168fe6060f1SDimitry Andric MachineInstr &DefMI = *RegInfo->def_instr_begin(Reg); 1169fe6060f1SDimitry Andric 1170fe6060f1SDimitry Andric // If we've found a copy-like instruction, follow it back to the 1171fe6060f1SDimitry Andric // instruction that defines the source value, see salvageCopySSA docs 1172fe6060f1SDimitry Andric // for why this is important. 1173fe6060f1SDimitry Andric if (DefMI.isCopyLike() || TII->isCopyInstr(DefMI)) { 117481ad6265SDimitry Andric auto Result = salvageCopySSA(DefMI, ArgDbgPHIs); 1175bdd1243dSDimitry Andric MO.ChangeToDbgInstrRef(Result.first, Result.second); 1176fe6060f1SDimitry Andric } else { 1177fe6060f1SDimitry Andric // Otherwise, identify the operand number that the VReg refers to. 1178fe6060f1SDimitry Andric unsigned OperandIdx = 0; 1179bdd1243dSDimitry Andric for (const auto &DefMO : DefMI.operands()) { 1180bdd1243dSDimitry Andric if (DefMO.isReg() && DefMO.isDef() && DefMO.getReg() == Reg) 1181fe6060f1SDimitry Andric break; 1182fe6060f1SDimitry Andric ++OperandIdx; 1183fe6060f1SDimitry Andric } 1184fe6060f1SDimitry Andric assert(OperandIdx < DefMI.getNumOperands()); 1185fe6060f1SDimitry Andric 1186fe6060f1SDimitry Andric // Morph this instr ref to point at the given instruction and operand. 1187fe6060f1SDimitry Andric unsigned ID = DefMI.getDebugInstrNum(); 1188bdd1243dSDimitry Andric MO.ChangeToDbgInstrRef(ID, OperandIdx); 1189fe6060f1SDimitry Andric } 1190fe6060f1SDimitry Andric } 1191bdd1243dSDimitry Andric 1192bdd1243dSDimitry Andric if (!IsValidRef) 1193bdd1243dSDimitry Andric MakeUndefDbgValue(MI); 1194bdd1243dSDimitry Andric } 1195fe6060f1SDimitry Andric } 1196fe6060f1SDimitry Andric } 1197fe6060f1SDimitry Andric 1198bdd1243dSDimitry Andric bool MachineFunction::shouldUseDebugInstrRef() const { 1199349cc55cSDimitry Andric // Disable instr-ref at -O0: it's very slow (in compile time). We can still 1200349cc55cSDimitry Andric // have optimized code inlined into this unoptimized code, however with 1201349cc55cSDimitry Andric // fewer and less aggressive optimizations happening, coverage and accuracy 1202349cc55cSDimitry Andric // should not suffer. 1203349cc55cSDimitry Andric if (getTarget().getOptLevel() == CodeGenOpt::None) 1204349cc55cSDimitry Andric return false; 1205349cc55cSDimitry Andric 1206349cc55cSDimitry Andric // Don't use instr-ref if this function is marked optnone. 1207349cc55cSDimitry Andric if (F.hasFnAttribute(Attribute::OptimizeNone)) 1208349cc55cSDimitry Andric return false; 1209349cc55cSDimitry Andric 121004eeddc0SDimitry Andric if (llvm::debuginfoShouldUseDebugInstrRef(getTarget().getTargetTriple())) 1211349cc55cSDimitry Andric return true; 1212349cc55cSDimitry Andric 1213349cc55cSDimitry Andric return false; 1214349cc55cSDimitry Andric } 1215349cc55cSDimitry Andric 1216bdd1243dSDimitry Andric bool MachineFunction::useDebugInstrRef() const { 1217bdd1243dSDimitry Andric return UseDebugInstrRef; 1218bdd1243dSDimitry Andric } 1219bdd1243dSDimitry Andric 1220bdd1243dSDimitry Andric void MachineFunction::setUseDebugInstrRef(bool Use) { 1221bdd1243dSDimitry Andric UseDebugInstrRef = Use; 1222bdd1243dSDimitry Andric } 1223bdd1243dSDimitry Andric 1224349cc55cSDimitry Andric // Use one million as a high / reserved number. 1225349cc55cSDimitry Andric const unsigned MachineFunction::DebugOperandMemNumber = 1000000; 1226349cc55cSDimitry Andric 1227*0b57cec5SDimitry Andric /// \} 1228*0b57cec5SDimitry Andric 1229*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1230*0b57cec5SDimitry Andric // MachineJumpTableInfo implementation 1231*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1232*0b57cec5SDimitry Andric 1233*0b57cec5SDimitry Andric /// Return the size of each entry in the jump table. 1234*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { 1235*0b57cec5SDimitry Andric // The size of a jump table entry is 4 bytes unless the entry is just the 1236*0b57cec5SDimitry Andric // address of a block, in which case it is the pointer size. 1237*0b57cec5SDimitry Andric switch (getEntryKind()) { 1238*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 1239*0b57cec5SDimitry Andric return TD.getPointerSize(); 1240*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 1241*0b57cec5SDimitry Andric return 8; 1242*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 1243*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 1244*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 1245*0b57cec5SDimitry Andric return 4; 1246*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 1247*0b57cec5SDimitry Andric return 0; 1248*0b57cec5SDimitry Andric } 1249*0b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 1250*0b57cec5SDimitry Andric } 1251*0b57cec5SDimitry Andric 1252*0b57cec5SDimitry Andric /// Return the alignment of each entry in the jump table. 1253*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { 1254*0b57cec5SDimitry Andric // The alignment of a jump table entry is the alignment of int32 unless the 1255*0b57cec5SDimitry Andric // entry is just the address of a block, in which case it is the pointer 1256*0b57cec5SDimitry Andric // alignment. 1257*0b57cec5SDimitry Andric switch (getEntryKind()) { 1258*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 12598bcb0991SDimitry Andric return TD.getPointerABIAlignment(0).value(); 1260*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 12618bcb0991SDimitry Andric return TD.getABIIntegerTypeAlignment(64).value(); 1262*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 1263*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 1264*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 12658bcb0991SDimitry Andric return TD.getABIIntegerTypeAlignment(32).value(); 1266*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 1267*0b57cec5SDimitry Andric return 1; 1268*0b57cec5SDimitry Andric } 1269*0b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 1270*0b57cec5SDimitry Andric } 1271*0b57cec5SDimitry Andric 1272*0b57cec5SDimitry Andric /// Create a new jump table entry in the jump table info. 1273*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::createJumpTableIndex( 1274*0b57cec5SDimitry Andric const std::vector<MachineBasicBlock*> &DestBBs) { 1275*0b57cec5SDimitry Andric assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 1276*0b57cec5SDimitry Andric JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 1277*0b57cec5SDimitry Andric return JumpTables.size()-1; 1278*0b57cec5SDimitry Andric } 1279*0b57cec5SDimitry Andric 1280*0b57cec5SDimitry Andric /// If Old is the target of any jump tables, update the jump tables to branch 1281*0b57cec5SDimitry Andric /// to New instead. 1282*0b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 1283*0b57cec5SDimitry Andric MachineBasicBlock *New) { 1284*0b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 1285*0b57cec5SDimitry Andric bool MadeChange = false; 1286*0b57cec5SDimitry Andric for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 1287*0b57cec5SDimitry Andric ReplaceMBBInJumpTable(i, Old, New); 1288*0b57cec5SDimitry Andric return MadeChange; 1289*0b57cec5SDimitry Andric } 1290*0b57cec5SDimitry Andric 1291e8d8bef9SDimitry Andric /// If MBB is present in any jump tables, remove it. 1292e8d8bef9SDimitry Andric bool MachineJumpTableInfo::RemoveMBBFromJumpTables(MachineBasicBlock *MBB) { 1293e8d8bef9SDimitry Andric bool MadeChange = false; 1294e8d8bef9SDimitry Andric for (MachineJumpTableEntry &JTE : JumpTables) { 1295e8d8bef9SDimitry Andric auto removeBeginItr = std::remove(JTE.MBBs.begin(), JTE.MBBs.end(), MBB); 1296e8d8bef9SDimitry Andric MadeChange |= (removeBeginItr != JTE.MBBs.end()); 1297e8d8bef9SDimitry Andric JTE.MBBs.erase(removeBeginItr, JTE.MBBs.end()); 1298e8d8bef9SDimitry Andric } 1299e8d8bef9SDimitry Andric return MadeChange; 1300e8d8bef9SDimitry Andric } 1301e8d8bef9SDimitry Andric 1302*0b57cec5SDimitry Andric /// If Old is a target of the jump tables, update the jump table to branch to 1303*0b57cec5SDimitry Andric /// New instead. 1304*0b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 1305*0b57cec5SDimitry Andric MachineBasicBlock *Old, 1306*0b57cec5SDimitry Andric MachineBasicBlock *New) { 1307*0b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 1308*0b57cec5SDimitry Andric bool MadeChange = false; 1309*0b57cec5SDimitry Andric MachineJumpTableEntry &JTE = JumpTables[Idx]; 13104824e7fdSDimitry Andric for (MachineBasicBlock *&MBB : JTE.MBBs) 13114824e7fdSDimitry Andric if (MBB == Old) { 13124824e7fdSDimitry Andric MBB = New; 1313*0b57cec5SDimitry Andric MadeChange = true; 1314*0b57cec5SDimitry Andric } 1315*0b57cec5SDimitry Andric return MadeChange; 1316*0b57cec5SDimitry Andric } 1317*0b57cec5SDimitry Andric 1318*0b57cec5SDimitry Andric void MachineJumpTableInfo::print(raw_ostream &OS) const { 1319*0b57cec5SDimitry Andric if (JumpTables.empty()) return; 1320*0b57cec5SDimitry Andric 1321*0b57cec5SDimitry Andric OS << "Jump Tables:\n"; 1322*0b57cec5SDimitry Andric 1323*0b57cec5SDimitry Andric for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 1324*0b57cec5SDimitry Andric OS << printJumpTableEntryReference(i) << ':'; 13254824e7fdSDimitry Andric for (const MachineBasicBlock *MBB : JumpTables[i].MBBs) 13264824e7fdSDimitry Andric OS << ' ' << printMBBReference(*MBB); 1327*0b57cec5SDimitry Andric if (i != e) 1328*0b57cec5SDimitry Andric OS << '\n'; 1329*0b57cec5SDimitry Andric } 1330*0b57cec5SDimitry Andric 1331*0b57cec5SDimitry Andric OS << '\n'; 1332*0b57cec5SDimitry Andric } 1333*0b57cec5SDimitry Andric 1334*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1335*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } 1336*0b57cec5SDimitry Andric #endif 1337*0b57cec5SDimitry Andric 1338*0b57cec5SDimitry Andric Printable llvm::printJumpTableEntryReference(unsigned Idx) { 1339*0b57cec5SDimitry Andric return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); 1340*0b57cec5SDimitry Andric } 1341*0b57cec5SDimitry Andric 1342*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1343*0b57cec5SDimitry Andric // MachineConstantPool implementation 1344*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1345*0b57cec5SDimitry Andric 1346*0b57cec5SDimitry Andric void MachineConstantPoolValue::anchor() {} 1347*0b57cec5SDimitry Andric 1348e8d8bef9SDimitry Andric unsigned MachineConstantPoolValue::getSizeInBytes(const DataLayout &DL) const { 1349e8d8bef9SDimitry Andric return DL.getTypeAllocSize(Ty); 1350e8d8bef9SDimitry Andric } 1351e8d8bef9SDimitry Andric 1352e8d8bef9SDimitry Andric unsigned MachineConstantPoolEntry::getSizeInBytes(const DataLayout &DL) const { 1353*0b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 1354e8d8bef9SDimitry Andric return Val.MachineCPVal->getSizeInBytes(DL); 1355e8d8bef9SDimitry Andric return DL.getTypeAllocSize(Val.ConstVal->getType()); 1356*0b57cec5SDimitry Andric } 1357*0b57cec5SDimitry Andric 1358*0b57cec5SDimitry Andric bool MachineConstantPoolEntry::needsRelocation() const { 1359*0b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 1360*0b57cec5SDimitry Andric return true; 1361fe6060f1SDimitry Andric return Val.ConstVal->needsDynamicRelocation(); 1362*0b57cec5SDimitry Andric } 1363*0b57cec5SDimitry Andric 1364*0b57cec5SDimitry Andric SectionKind 1365*0b57cec5SDimitry Andric MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { 1366*0b57cec5SDimitry Andric if (needsRelocation()) 1367*0b57cec5SDimitry Andric return SectionKind::getReadOnlyWithRel(); 1368e8d8bef9SDimitry Andric switch (getSizeInBytes(*DL)) { 1369*0b57cec5SDimitry Andric case 4: 1370*0b57cec5SDimitry Andric return SectionKind::getMergeableConst4(); 1371*0b57cec5SDimitry Andric case 8: 1372*0b57cec5SDimitry Andric return SectionKind::getMergeableConst8(); 1373*0b57cec5SDimitry Andric case 16: 1374*0b57cec5SDimitry Andric return SectionKind::getMergeableConst16(); 1375*0b57cec5SDimitry Andric case 32: 1376*0b57cec5SDimitry Andric return SectionKind::getMergeableConst32(); 1377*0b57cec5SDimitry Andric default: 1378*0b57cec5SDimitry Andric return SectionKind::getReadOnly(); 1379*0b57cec5SDimitry Andric } 1380*0b57cec5SDimitry Andric } 1381*0b57cec5SDimitry Andric 1382*0b57cec5SDimitry Andric MachineConstantPool::~MachineConstantPool() { 1383*0b57cec5SDimitry Andric // A constant may be a member of both Constants and MachineCPVsSharingEntries, 1384*0b57cec5SDimitry Andric // so keep track of which we've deleted to avoid double deletions. 1385*0b57cec5SDimitry Andric DenseSet<MachineConstantPoolValue*> Deleted; 13860eae32dcSDimitry Andric for (const MachineConstantPoolEntry &C : Constants) 13870eae32dcSDimitry Andric if (C.isMachineConstantPoolEntry()) { 13880eae32dcSDimitry Andric Deleted.insert(C.Val.MachineCPVal); 13890eae32dcSDimitry Andric delete C.Val.MachineCPVal; 1390*0b57cec5SDimitry Andric } 1391fe6060f1SDimitry Andric for (MachineConstantPoolValue *CPV : MachineCPVsSharingEntries) { 1392fe6060f1SDimitry Andric if (Deleted.count(CPV) == 0) 1393fe6060f1SDimitry Andric delete CPV; 1394*0b57cec5SDimitry Andric } 1395*0b57cec5SDimitry Andric } 1396*0b57cec5SDimitry Andric 1397*0b57cec5SDimitry Andric /// Test whether the given two constants can be allocated the same constant pool 1398*0b57cec5SDimitry Andric /// entry. 1399*0b57cec5SDimitry Andric static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 1400*0b57cec5SDimitry Andric const DataLayout &DL) { 1401*0b57cec5SDimitry Andric // Handle the trivial case quickly. 1402*0b57cec5SDimitry Andric if (A == B) return true; 1403*0b57cec5SDimitry Andric 1404*0b57cec5SDimitry Andric // If they have the same type but weren't the same constant, quickly 1405*0b57cec5SDimitry Andric // reject them. 1406*0b57cec5SDimitry Andric if (A->getType() == B->getType()) return false; 1407*0b57cec5SDimitry Andric 1408*0b57cec5SDimitry Andric // We can't handle structs or arrays. 1409*0b57cec5SDimitry Andric if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || 1410*0b57cec5SDimitry Andric isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) 1411*0b57cec5SDimitry Andric return false; 1412*0b57cec5SDimitry Andric 1413*0b57cec5SDimitry Andric // For now, only support constants with the same size. 1414*0b57cec5SDimitry Andric uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); 1415*0b57cec5SDimitry Andric if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) 1416*0b57cec5SDimitry Andric return false; 1417*0b57cec5SDimitry Andric 1418*0b57cec5SDimitry Andric Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); 1419*0b57cec5SDimitry Andric 1420*0b57cec5SDimitry Andric // Try constant folding a bitcast of both instructions to an integer. If we 1421*0b57cec5SDimitry Andric // get two identical ConstantInt's, then we are good to share them. We use 1422*0b57cec5SDimitry Andric // the constant folding APIs to do this so that we get the benefit of 1423*0b57cec5SDimitry Andric // DataLayout. 1424*0b57cec5SDimitry Andric if (isa<PointerType>(A->getType())) 1425*0b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::PtrToInt, 1426*0b57cec5SDimitry Andric const_cast<Constant *>(A), IntTy, DL); 1427*0b57cec5SDimitry Andric else if (A->getType() != IntTy) 1428*0b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), 1429*0b57cec5SDimitry Andric IntTy, DL); 1430*0b57cec5SDimitry Andric if (isa<PointerType>(B->getType())) 1431*0b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::PtrToInt, 1432*0b57cec5SDimitry Andric const_cast<Constant *>(B), IntTy, DL); 1433*0b57cec5SDimitry Andric else if (B->getType() != IntTy) 1434*0b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), 1435*0b57cec5SDimitry Andric IntTy, DL); 1436*0b57cec5SDimitry Andric 1437*0b57cec5SDimitry Andric return A == B; 1438*0b57cec5SDimitry Andric } 1439*0b57cec5SDimitry Andric 1440*0b57cec5SDimitry Andric /// Create a new entry in the constant pool or return an existing one. 1441*0b57cec5SDimitry Andric /// User must specify the log2 of the minimum required alignment for the object. 1442*0b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 14435ffd83dbSDimitry Andric Align Alignment) { 1444*0b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1445*0b57cec5SDimitry Andric 1446*0b57cec5SDimitry Andric // Check to see if we already have this constant. 1447*0b57cec5SDimitry Andric // 1448*0b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 1449*0b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) 1450*0b57cec5SDimitry Andric if (!Constants[i].isMachineConstantPoolEntry() && 1451*0b57cec5SDimitry Andric CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { 14525ffd83dbSDimitry Andric if (Constants[i].getAlign() < Alignment) 1453*0b57cec5SDimitry Andric Constants[i].Alignment = Alignment; 1454*0b57cec5SDimitry Andric return i; 1455*0b57cec5SDimitry Andric } 1456*0b57cec5SDimitry Andric 1457*0b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 1458*0b57cec5SDimitry Andric return Constants.size()-1; 1459*0b57cec5SDimitry Andric } 1460*0b57cec5SDimitry Andric 1461*0b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 14625ffd83dbSDimitry Andric Align Alignment) { 1463*0b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1464*0b57cec5SDimitry Andric 1465*0b57cec5SDimitry Andric // Check to see if we already have this constant. 1466*0b57cec5SDimitry Andric // 1467*0b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 1468*0b57cec5SDimitry Andric int Idx = V->getExistingMachineCPValue(this, Alignment); 1469*0b57cec5SDimitry Andric if (Idx != -1) { 1470*0b57cec5SDimitry Andric MachineCPVsSharingEntries.insert(V); 1471*0b57cec5SDimitry Andric return (unsigned)Idx; 1472*0b57cec5SDimitry Andric } 1473*0b57cec5SDimitry Andric 1474*0b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 1475*0b57cec5SDimitry Andric return Constants.size()-1; 1476*0b57cec5SDimitry Andric } 1477*0b57cec5SDimitry Andric 1478*0b57cec5SDimitry Andric void MachineConstantPool::print(raw_ostream &OS) const { 1479*0b57cec5SDimitry Andric if (Constants.empty()) return; 1480*0b57cec5SDimitry Andric 1481*0b57cec5SDimitry Andric OS << "Constant Pool:\n"; 1482*0b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 1483*0b57cec5SDimitry Andric OS << " cp#" << i << ": "; 1484*0b57cec5SDimitry Andric if (Constants[i].isMachineConstantPoolEntry()) 1485*0b57cec5SDimitry Andric Constants[i].Val.MachineCPVal->print(OS); 1486*0b57cec5SDimitry Andric else 1487*0b57cec5SDimitry Andric Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); 14885ffd83dbSDimitry Andric OS << ", align=" << Constants[i].getAlign().value(); 1489*0b57cec5SDimitry Andric OS << "\n"; 1490*0b57cec5SDimitry Andric } 1491*0b57cec5SDimitry Andric } 1492*0b57cec5SDimitry Andric 1493*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1494*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } 1495*0b57cec5SDimitry Andric #endif 1496