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; 190*0b57cec5SDimitry Andric // We can realign the stack if the target supports it and the user hasn't 191*0b57cec5SDimitry Andric // explicitly asked us not to. 192*0b57cec5SDimitry Andric bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && 193*0b57cec5SDimitry Andric !F.hasFnAttribute("no-realign-stack"); 194*0b57cec5SDimitry Andric FrameInfo = new (Allocator) MachineFrameInfo( 195*0b57cec5SDimitry Andric getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, 196*0b57cec5SDimitry Andric /*ForcedRealign=*/CanRealignSP && 197*0b57cec5SDimitry Andric F.hasFnAttribute(Attribute::StackAlignment)); 198*0b57cec5SDimitry Andric 19981ad6265SDimitry Andric setUnsafeStackSize(F, *FrameInfo); 20081ad6265SDimitry Andric 201*0b57cec5SDimitry Andric if (F.hasFnAttribute(Attribute::StackAlignment)) 2025ffd83dbSDimitry Andric FrameInfo->ensureMaxAlignment(*F.getFnStackAlign()); 203*0b57cec5SDimitry Andric 204*0b57cec5SDimitry Andric ConstantPool = new (Allocator) MachineConstantPool(getDataLayout()); 205*0b57cec5SDimitry Andric Alignment = STI->getTargetLowering()->getMinFunctionAlignment(); 206*0b57cec5SDimitry Andric 207*0b57cec5SDimitry Andric // FIXME: Shouldn't use pref alignment if explicit alignment is set on F. 208*0b57cec5SDimitry Andric // FIXME: Use Function::hasOptSize(). 209*0b57cec5SDimitry Andric if (!F.hasFnAttribute(Attribute::OptimizeForSize)) 210*0b57cec5SDimitry Andric Alignment = std::max(Alignment, 211*0b57cec5SDimitry Andric STI->getTargetLowering()->getPrefFunctionAlignment()); 212*0b57cec5SDimitry Andric 213*0b57cec5SDimitry Andric if (AlignAllFunctions) 2148bcb0991SDimitry Andric Alignment = Align(1ULL << AlignAllFunctions); 215*0b57cec5SDimitry Andric 216*0b57cec5SDimitry Andric JumpTableInfo = nullptr; 217*0b57cec5SDimitry Andric 218*0b57cec5SDimitry Andric if (isFuncletEHPersonality(classifyEHPersonality( 219*0b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 220*0b57cec5SDimitry Andric WinEHInfo = new (Allocator) WinEHFuncInfo(); 221*0b57cec5SDimitry Andric } 222*0b57cec5SDimitry Andric 223*0b57cec5SDimitry Andric if (isScopedEHPersonality(classifyEHPersonality( 224*0b57cec5SDimitry Andric F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) { 225*0b57cec5SDimitry Andric WasmEHInfo = new (Allocator) WasmEHFuncInfo(); 226*0b57cec5SDimitry Andric } 227*0b57cec5SDimitry Andric 228*0b57cec5SDimitry Andric assert(Target.isCompatibleDataLayout(getDataLayout()) && 229*0b57cec5SDimitry Andric "Can't create a MachineFunction using a Module with a " 230*0b57cec5SDimitry Andric "Target-incompatible DataLayout attached\n"); 231*0b57cec5SDimitry Andric 23281ad6265SDimitry Andric PSVManager = std::make_unique<PseudoSourceValueManager>(getTarget()); 233*0b57cec5SDimitry Andric } 234*0b57cec5SDimitry Andric 235*0b57cec5SDimitry Andric MachineFunction::~MachineFunction() { 236*0b57cec5SDimitry Andric clear(); 237*0b57cec5SDimitry Andric } 238*0b57cec5SDimitry Andric 239*0b57cec5SDimitry Andric void MachineFunction::clear() { 240*0b57cec5SDimitry Andric Properties.reset(); 241*0b57cec5SDimitry Andric // Don't call destructors on MachineInstr and MachineOperand. All of their 242*0b57cec5SDimitry Andric // memory comes from the BumpPtrAllocator which is about to be purged. 243*0b57cec5SDimitry Andric // 244*0b57cec5SDimitry Andric // Do call MachineBasicBlock destructors, it contains std::vectors. 245*0b57cec5SDimitry Andric for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I)) 246*0b57cec5SDimitry Andric I->Insts.clearAndLeakNodesUnsafely(); 247*0b57cec5SDimitry Andric MBBNumbering.clear(); 248*0b57cec5SDimitry Andric 249*0b57cec5SDimitry Andric InstructionRecycler.clear(Allocator); 250*0b57cec5SDimitry Andric OperandRecycler.clear(Allocator); 251*0b57cec5SDimitry Andric BasicBlockRecycler.clear(Allocator); 252*0b57cec5SDimitry Andric CodeViewAnnotations.clear(); 253*0b57cec5SDimitry Andric VariableDbgInfos.clear(); 254*0b57cec5SDimitry Andric if (RegInfo) { 255*0b57cec5SDimitry Andric RegInfo->~MachineRegisterInfo(); 256*0b57cec5SDimitry Andric Allocator.Deallocate(RegInfo); 257*0b57cec5SDimitry Andric } 258*0b57cec5SDimitry Andric if (MFInfo) { 259*0b57cec5SDimitry Andric MFInfo->~MachineFunctionInfo(); 260*0b57cec5SDimitry Andric Allocator.Deallocate(MFInfo); 261*0b57cec5SDimitry Andric } 262*0b57cec5SDimitry Andric 263*0b57cec5SDimitry Andric FrameInfo->~MachineFrameInfo(); 264*0b57cec5SDimitry Andric Allocator.Deallocate(FrameInfo); 265*0b57cec5SDimitry Andric 266*0b57cec5SDimitry Andric ConstantPool->~MachineConstantPool(); 267*0b57cec5SDimitry Andric Allocator.Deallocate(ConstantPool); 268*0b57cec5SDimitry Andric 269*0b57cec5SDimitry Andric if (JumpTableInfo) { 270*0b57cec5SDimitry Andric JumpTableInfo->~MachineJumpTableInfo(); 271*0b57cec5SDimitry Andric Allocator.Deallocate(JumpTableInfo); 272*0b57cec5SDimitry Andric } 273*0b57cec5SDimitry Andric 274*0b57cec5SDimitry Andric if (WinEHInfo) { 275*0b57cec5SDimitry Andric WinEHInfo->~WinEHFuncInfo(); 276*0b57cec5SDimitry Andric Allocator.Deallocate(WinEHInfo); 277*0b57cec5SDimitry Andric } 278*0b57cec5SDimitry Andric 279*0b57cec5SDimitry Andric if (WasmEHInfo) { 280*0b57cec5SDimitry Andric WasmEHInfo->~WasmEHFuncInfo(); 281*0b57cec5SDimitry Andric Allocator.Deallocate(WasmEHInfo); 282*0b57cec5SDimitry Andric } 283*0b57cec5SDimitry Andric } 284*0b57cec5SDimitry Andric 285*0b57cec5SDimitry Andric const DataLayout &MachineFunction::getDataLayout() const { 286*0b57cec5SDimitry Andric return F.getParent()->getDataLayout(); 287*0b57cec5SDimitry Andric } 288*0b57cec5SDimitry Andric 289*0b57cec5SDimitry Andric /// Get the JumpTableInfo for this function. 290*0b57cec5SDimitry Andric /// If it does not already exist, allocate one. 291*0b57cec5SDimitry Andric MachineJumpTableInfo *MachineFunction:: 292*0b57cec5SDimitry Andric getOrCreateJumpTableInfo(unsigned EntryKind) { 293*0b57cec5SDimitry Andric if (JumpTableInfo) return JumpTableInfo; 294*0b57cec5SDimitry Andric 295*0b57cec5SDimitry Andric JumpTableInfo = new (Allocator) 296*0b57cec5SDimitry Andric MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 297*0b57cec5SDimitry Andric return JumpTableInfo; 298*0b57cec5SDimitry Andric } 299*0b57cec5SDimitry Andric 300480093f4SDimitry Andric DenormalMode MachineFunction::getDenormalMode(const fltSemantics &FPType) const { 301e8d8bef9SDimitry Andric return F.getDenormalMode(FPType); 302480093f4SDimitry Andric } 303480093f4SDimitry Andric 304*0b57cec5SDimitry Andric /// Should we be emitting segmented stack stuff for the function 305*0b57cec5SDimitry Andric bool MachineFunction::shouldSplitStack() const { 306*0b57cec5SDimitry Andric return getFunction().hasFnAttribute("split-stack"); 307*0b57cec5SDimitry Andric } 308*0b57cec5SDimitry Andric 309*0b57cec5SDimitry Andric LLVM_NODISCARD unsigned 310*0b57cec5SDimitry Andric MachineFunction::addFrameInst(const MCCFIInstruction &Inst) { 311*0b57cec5SDimitry Andric FrameInstructions.push_back(Inst); 312*0b57cec5SDimitry Andric return FrameInstructions.size() - 1; 313*0b57cec5SDimitry Andric } 314*0b57cec5SDimitry Andric 315*0b57cec5SDimitry Andric /// This discards all of the MachineBasicBlock numbers and recomputes them. 316*0b57cec5SDimitry Andric /// This guarantees that the MBB numbers are sequential, dense, and match the 317*0b57cec5SDimitry Andric /// ordering of the blocks within the function. If a specific MachineBasicBlock 318*0b57cec5SDimitry Andric /// is specified, only that block and those after it are renumbered. 319*0b57cec5SDimitry Andric void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 320*0b57cec5SDimitry Andric if (empty()) { MBBNumbering.clear(); return; } 321*0b57cec5SDimitry Andric MachineFunction::iterator MBBI, E = end(); 322*0b57cec5SDimitry Andric if (MBB == nullptr) 323*0b57cec5SDimitry Andric MBBI = begin(); 324*0b57cec5SDimitry Andric else 325*0b57cec5SDimitry Andric MBBI = MBB->getIterator(); 326*0b57cec5SDimitry Andric 327*0b57cec5SDimitry Andric // Figure out the block number this should have. 328*0b57cec5SDimitry Andric unsigned BlockNo = 0; 329*0b57cec5SDimitry Andric if (MBBI != begin()) 330*0b57cec5SDimitry Andric BlockNo = std::prev(MBBI)->getNumber() + 1; 331*0b57cec5SDimitry Andric 332*0b57cec5SDimitry Andric for (; MBBI != E; ++MBBI, ++BlockNo) { 333*0b57cec5SDimitry Andric if (MBBI->getNumber() != (int)BlockNo) { 334*0b57cec5SDimitry Andric // Remove use of the old number. 335*0b57cec5SDimitry Andric if (MBBI->getNumber() != -1) { 336*0b57cec5SDimitry Andric assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 337*0b57cec5SDimitry Andric "MBB number mismatch!"); 338*0b57cec5SDimitry Andric MBBNumbering[MBBI->getNumber()] = nullptr; 339*0b57cec5SDimitry Andric } 340*0b57cec5SDimitry Andric 341*0b57cec5SDimitry Andric // If BlockNo is already taken, set that block's number to -1. 342*0b57cec5SDimitry Andric if (MBBNumbering[BlockNo]) 343*0b57cec5SDimitry Andric MBBNumbering[BlockNo]->setNumber(-1); 344*0b57cec5SDimitry Andric 345*0b57cec5SDimitry Andric MBBNumbering[BlockNo] = &*MBBI; 346*0b57cec5SDimitry Andric MBBI->setNumber(BlockNo); 347*0b57cec5SDimitry Andric } 348*0b57cec5SDimitry Andric } 349*0b57cec5SDimitry Andric 350*0b57cec5SDimitry Andric // Okay, all the blocks are renumbered. If we have compactified the block 351*0b57cec5SDimitry Andric // numbering, shrink MBBNumbering now. 352*0b57cec5SDimitry Andric assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 353*0b57cec5SDimitry Andric MBBNumbering.resize(BlockNo); 354*0b57cec5SDimitry Andric } 355*0b57cec5SDimitry Andric 3565ffd83dbSDimitry Andric /// This method iterates over the basic blocks and assigns their IsBeginSection 3575ffd83dbSDimitry Andric /// and IsEndSection fields. This must be called after MBB layout is finalized 3585ffd83dbSDimitry Andric /// and the SectionID's are assigned to MBBs. 3595ffd83dbSDimitry Andric void MachineFunction::assignBeginEndSections() { 3605ffd83dbSDimitry Andric front().setIsBeginSection(); 3615ffd83dbSDimitry Andric auto CurrentSectionID = front().getSectionID(); 3625ffd83dbSDimitry Andric for (auto MBBI = std::next(begin()), E = end(); MBBI != E; ++MBBI) { 3635ffd83dbSDimitry Andric if (MBBI->getSectionID() == CurrentSectionID) 3645ffd83dbSDimitry Andric continue; 3655ffd83dbSDimitry Andric MBBI->setIsBeginSection(); 3665ffd83dbSDimitry Andric std::prev(MBBI)->setIsEndSection(); 3675ffd83dbSDimitry Andric CurrentSectionID = MBBI->getSectionID(); 3685ffd83dbSDimitry Andric } 3695ffd83dbSDimitry Andric back().setIsEndSection(); 3705ffd83dbSDimitry Andric } 3715ffd83dbSDimitry Andric 372*0b57cec5SDimitry Andric /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'. 373*0b57cec5SDimitry Andric MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 3740eae32dcSDimitry Andric DebugLoc DL, 375e8d8bef9SDimitry Andric bool NoImplicit) { 376*0b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 3770eae32dcSDimitry Andric MachineInstr(*this, MCID, std::move(DL), NoImplicit); 378*0b57cec5SDimitry Andric } 379*0b57cec5SDimitry Andric 380*0b57cec5SDimitry Andric /// Create a new MachineInstr which is a copy of the 'Orig' instruction, 381*0b57cec5SDimitry Andric /// identical in all ways except the instruction has no parent, prev, or next. 382*0b57cec5SDimitry Andric MachineInstr * 383*0b57cec5SDimitry Andric MachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 384*0b57cec5SDimitry Andric return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 385*0b57cec5SDimitry Andric MachineInstr(*this, *Orig); 386*0b57cec5SDimitry Andric } 387*0b57cec5SDimitry Andric 3880eae32dcSDimitry Andric MachineInstr &MachineFunction::cloneMachineInstrBundle( 3890eae32dcSDimitry Andric MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore, 3900eae32dcSDimitry Andric const MachineInstr &Orig) { 391*0b57cec5SDimitry Andric MachineInstr *FirstClone = nullptr; 392*0b57cec5SDimitry Andric MachineBasicBlock::const_instr_iterator I = Orig.getIterator(); 393*0b57cec5SDimitry Andric while (true) { 394*0b57cec5SDimitry Andric MachineInstr *Cloned = CloneMachineInstr(&*I); 395*0b57cec5SDimitry Andric MBB.insert(InsertBefore, Cloned); 396*0b57cec5SDimitry Andric if (FirstClone == nullptr) { 397*0b57cec5SDimitry Andric FirstClone = Cloned; 398*0b57cec5SDimitry Andric } else { 399*0b57cec5SDimitry Andric Cloned->bundleWithPred(); 400*0b57cec5SDimitry Andric } 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric if (!I->isBundledWithSucc()) 403*0b57cec5SDimitry Andric break; 404*0b57cec5SDimitry Andric ++I; 405*0b57cec5SDimitry Andric } 4065ffd83dbSDimitry Andric // Copy over call site info to the cloned instruction if needed. If Orig is in 4075ffd83dbSDimitry Andric // a bundle, copyCallSiteInfo takes care of finding the call instruction in 4085ffd83dbSDimitry Andric // the bundle. 4095ffd83dbSDimitry Andric if (Orig.shouldUpdateCallSiteInfo()) 4105ffd83dbSDimitry Andric copyCallSiteInfo(&Orig, FirstClone); 411*0b57cec5SDimitry Andric return *FirstClone; 412*0b57cec5SDimitry Andric } 413*0b57cec5SDimitry Andric 414*0b57cec5SDimitry Andric /// Delete the given MachineInstr. 415*0b57cec5SDimitry Andric /// 416*0b57cec5SDimitry Andric /// This function also serves as the MachineInstr destructor - the real 417*0b57cec5SDimitry Andric /// ~MachineInstr() destructor must be empty. 4180eae32dcSDimitry Andric void MachineFunction::deleteMachineInstr(MachineInstr *MI) { 419*0b57cec5SDimitry Andric // Verify that a call site info is at valid state. This assertion should 420*0b57cec5SDimitry Andric // be triggered during the implementation of support for the 421*0b57cec5SDimitry Andric // call site info of a new architecture. If the assertion is triggered, 422*0b57cec5SDimitry Andric // back trace will tell where to insert a call to updateCallSiteInfo(). 4235ffd83dbSDimitry Andric assert((!MI->isCandidateForCallSiteEntry() || 424*0b57cec5SDimitry Andric CallSitesInfo.find(MI) == CallSitesInfo.end()) && 425*0b57cec5SDimitry Andric "Call site info was not updated!"); 426*0b57cec5SDimitry Andric // Strip it for parts. The operand array and the MI object itself are 427*0b57cec5SDimitry Andric // independently recyclable. 428*0b57cec5SDimitry Andric if (MI->Operands) 429*0b57cec5SDimitry Andric deallocateOperandArray(MI->CapOperands, MI->Operands); 430*0b57cec5SDimitry Andric // Don't call ~MachineInstr() which must be trivial anyway because 431*0b57cec5SDimitry Andric // ~MachineFunction drops whole lists of MachineInstrs wihout calling their 432*0b57cec5SDimitry Andric // destructors. 433*0b57cec5SDimitry Andric InstructionRecycler.Deallocate(Allocator, MI); 434*0b57cec5SDimitry Andric } 435*0b57cec5SDimitry Andric 436*0b57cec5SDimitry Andric /// Allocate a new MachineBasicBlock. Use this instead of 437*0b57cec5SDimitry Andric /// `new MachineBasicBlock'. 438*0b57cec5SDimitry Andric MachineBasicBlock * 439*0b57cec5SDimitry Andric MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 440*0b57cec5SDimitry Andric return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 441*0b57cec5SDimitry Andric MachineBasicBlock(*this, bb); 442*0b57cec5SDimitry Andric } 443*0b57cec5SDimitry Andric 444*0b57cec5SDimitry Andric /// Delete the given MachineBasicBlock. 4450eae32dcSDimitry Andric void MachineFunction::deleteMachineBasicBlock(MachineBasicBlock *MBB) { 446*0b57cec5SDimitry Andric assert(MBB->getParent() == this && "MBB parent mismatch!"); 447e8d8bef9SDimitry Andric // Clean up any references to MBB in jump tables before deleting it. 448e8d8bef9SDimitry Andric if (JumpTableInfo) 449e8d8bef9SDimitry Andric JumpTableInfo->RemoveMBBFromJumpTables(MBB); 450*0b57cec5SDimitry Andric MBB->~MachineBasicBlock(); 451*0b57cec5SDimitry Andric BasicBlockRecycler.Deallocate(Allocator, MBB); 452*0b57cec5SDimitry Andric } 453*0b57cec5SDimitry Andric 454*0b57cec5SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 455*0b57cec5SDimitry Andric MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s, 4565ffd83dbSDimitry Andric Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 457*0b57cec5SDimitry Andric SyncScope::ID SSID, AtomicOrdering Ordering, 458*0b57cec5SDimitry Andric AtomicOrdering FailureOrdering) { 459*0b57cec5SDimitry Andric return new (Allocator) 460*0b57cec5SDimitry Andric MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges, 461*0b57cec5SDimitry Andric SSID, Ordering, FailureOrdering); 462*0b57cec5SDimitry Andric } 463*0b57cec5SDimitry Andric 464e8d8bef9SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 465fe6060f1SDimitry Andric MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, LLT MemTy, 466fe6060f1SDimitry Andric Align base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges, 467fe6060f1SDimitry Andric SyncScope::ID SSID, AtomicOrdering Ordering, 468fe6060f1SDimitry Andric AtomicOrdering FailureOrdering) { 469fe6060f1SDimitry Andric return new (Allocator) 470fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, f, MemTy, base_alignment, AAInfo, Ranges, SSID, 471fe6060f1SDimitry Andric Ordering, FailureOrdering); 472fe6060f1SDimitry Andric } 473fe6060f1SDimitry Andric 474fe6060f1SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 475fe6060f1SDimitry Andric const MachineMemOperand *MMO, const MachinePointerInfo &PtrInfo, uint64_t Size) { 476fe6060f1SDimitry Andric return new (Allocator) 477fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, MMO->getFlags(), Size, MMO->getBaseAlign(), 478fe6060f1SDimitry Andric AAMDNodes(), nullptr, MMO->getSyncScopeID(), 479fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 480fe6060f1SDimitry Andric } 481fe6060f1SDimitry Andric 482fe6060f1SDimitry Andric MachineMemOperand *MachineFunction::getMachineMemOperand( 483fe6060f1SDimitry Andric const MachineMemOperand *MMO, const MachinePointerInfo &PtrInfo, LLT Ty) { 484fe6060f1SDimitry Andric return new (Allocator) 485fe6060f1SDimitry Andric MachineMemOperand(PtrInfo, MMO->getFlags(), Ty, MMO->getBaseAlign(), 486fe6060f1SDimitry Andric AAMDNodes(), nullptr, MMO->getSyncScopeID(), 487fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 488e8d8bef9SDimitry Andric } 489e8d8bef9SDimitry Andric 490*0b57cec5SDimitry Andric MachineMemOperand * 491*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 492fe6060f1SDimitry Andric int64_t Offset, LLT Ty) { 493*0b57cec5SDimitry Andric const MachinePointerInfo &PtrInfo = MMO->getPointerInfo(); 494*0b57cec5SDimitry Andric 495*0b57cec5SDimitry Andric // If there is no pointer value, the offset isn't tracked so we need to adjust 496*0b57cec5SDimitry Andric // the base alignment. 4975ffd83dbSDimitry Andric Align Alignment = PtrInfo.V.isNull() 4985ffd83dbSDimitry Andric ? commonAlignment(MMO->getBaseAlign(), Offset) 4995ffd83dbSDimitry Andric : MMO->getBaseAlign(); 500*0b57cec5SDimitry Andric 501e8d8bef9SDimitry Andric // Do not preserve ranges, since we don't necessarily know what the high bits 502e8d8bef9SDimitry Andric // are anymore. 503fe6060f1SDimitry Andric return new (Allocator) MachineMemOperand( 504fe6060f1SDimitry Andric PtrInfo.getWithOffset(Offset), MMO->getFlags(), Ty, Alignment, 505fe6060f1SDimitry Andric MMO->getAAInfo(), nullptr, MMO->getSyncScopeID(), 506fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 507*0b57cec5SDimitry Andric } 508*0b57cec5SDimitry Andric 509*0b57cec5SDimitry Andric MachineMemOperand * 510*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 511*0b57cec5SDimitry Andric const AAMDNodes &AAInfo) { 512*0b57cec5SDimitry Andric MachinePointerInfo MPI = MMO->getValue() ? 513*0b57cec5SDimitry Andric MachinePointerInfo(MMO->getValue(), MMO->getOffset()) : 514*0b57cec5SDimitry Andric MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset()); 515*0b57cec5SDimitry Andric 5165ffd83dbSDimitry Andric return new (Allocator) MachineMemOperand( 5175ffd83dbSDimitry Andric MPI, MMO->getFlags(), MMO->getSize(), MMO->getBaseAlign(), AAInfo, 518fe6060f1SDimitry Andric MMO->getRanges(), MMO->getSyncScopeID(), MMO->getSuccessOrdering(), 5195ffd83dbSDimitry Andric MMO->getFailureOrdering()); 520*0b57cec5SDimitry Andric } 521*0b57cec5SDimitry Andric 522*0b57cec5SDimitry Andric MachineMemOperand * 523*0b57cec5SDimitry Andric MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 524*0b57cec5SDimitry Andric MachineMemOperand::Flags Flags) { 525*0b57cec5SDimitry Andric return new (Allocator) MachineMemOperand( 5265ffd83dbSDimitry Andric MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlign(), 527*0b57cec5SDimitry Andric MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(), 528fe6060f1SDimitry Andric MMO->getSuccessOrdering(), MMO->getFailureOrdering()); 529*0b57cec5SDimitry Andric } 530*0b57cec5SDimitry Andric 531480093f4SDimitry Andric MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfo( 532c14a5a88SDimitry Andric ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol, 533c14a5a88SDimitry Andric MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) { 534c14a5a88SDimitry Andric return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol, 535c14a5a88SDimitry Andric PostInstrSymbol, HeapAllocMarker); 536*0b57cec5SDimitry Andric } 537*0b57cec5SDimitry Andric 538*0b57cec5SDimitry Andric const char *MachineFunction::createExternalSymbolName(StringRef Name) { 539*0b57cec5SDimitry Andric char *Dest = Allocator.Allocate<char>(Name.size() + 1); 540*0b57cec5SDimitry Andric llvm::copy(Name, Dest); 541*0b57cec5SDimitry Andric Dest[Name.size()] = 0; 542*0b57cec5SDimitry Andric return Dest; 543*0b57cec5SDimitry Andric } 544*0b57cec5SDimitry Andric 545*0b57cec5SDimitry Andric uint32_t *MachineFunction::allocateRegMask() { 546*0b57cec5SDimitry Andric unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs(); 547*0b57cec5SDimitry Andric unsigned Size = MachineOperand::getRegMaskSize(NumRegs); 548*0b57cec5SDimitry Andric uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); 549*0b57cec5SDimitry Andric memset(Mask, 0, Size * sizeof(Mask[0])); 550*0b57cec5SDimitry Andric return Mask; 551*0b57cec5SDimitry Andric } 552*0b57cec5SDimitry Andric 553480093f4SDimitry Andric ArrayRef<int> MachineFunction::allocateShuffleMask(ArrayRef<int> Mask) { 554480093f4SDimitry Andric int* AllocMask = Allocator.Allocate<int>(Mask.size()); 555480093f4SDimitry Andric copy(Mask, AllocMask); 556480093f4SDimitry Andric return {AllocMask, Mask.size()}; 557480093f4SDimitry Andric } 558480093f4SDimitry Andric 559*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 560*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineFunction::dump() const { 561*0b57cec5SDimitry Andric print(dbgs()); 562*0b57cec5SDimitry Andric } 563*0b57cec5SDimitry Andric #endif 564*0b57cec5SDimitry Andric 565*0b57cec5SDimitry Andric StringRef MachineFunction::getName() const { 566*0b57cec5SDimitry Andric return getFunction().getName(); 567*0b57cec5SDimitry Andric } 568*0b57cec5SDimitry Andric 569*0b57cec5SDimitry Andric void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const { 570*0b57cec5SDimitry Andric OS << "# Machine code for function " << getName() << ": "; 571*0b57cec5SDimitry Andric getProperties().print(OS); 572*0b57cec5SDimitry Andric OS << '\n'; 573*0b57cec5SDimitry Andric 574*0b57cec5SDimitry Andric // Print Frame Information 575*0b57cec5SDimitry Andric FrameInfo->print(*this, OS); 576*0b57cec5SDimitry Andric 577*0b57cec5SDimitry Andric // Print JumpTable Information 578*0b57cec5SDimitry Andric if (JumpTableInfo) 579*0b57cec5SDimitry Andric JumpTableInfo->print(OS); 580*0b57cec5SDimitry Andric 581*0b57cec5SDimitry Andric // Print Constant Pool 582*0b57cec5SDimitry Andric ConstantPool->print(OS); 583*0b57cec5SDimitry Andric 584*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo(); 585*0b57cec5SDimitry Andric 586*0b57cec5SDimitry Andric if (RegInfo && !RegInfo->livein_empty()) { 587*0b57cec5SDimitry Andric OS << "Function Live Ins: "; 588*0b57cec5SDimitry Andric for (MachineRegisterInfo::livein_iterator 589*0b57cec5SDimitry Andric I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 590*0b57cec5SDimitry Andric OS << printReg(I->first, TRI); 591*0b57cec5SDimitry Andric if (I->second) 592*0b57cec5SDimitry Andric OS << " in " << printReg(I->second, TRI); 593*0b57cec5SDimitry Andric if (std::next(I) != E) 594*0b57cec5SDimitry Andric OS << ", "; 595*0b57cec5SDimitry Andric } 596*0b57cec5SDimitry Andric OS << '\n'; 597*0b57cec5SDimitry Andric } 598*0b57cec5SDimitry Andric 599*0b57cec5SDimitry Andric ModuleSlotTracker MST(getFunction().getParent()); 600*0b57cec5SDimitry Andric MST.incorporateFunction(getFunction()); 601*0b57cec5SDimitry Andric for (const auto &BB : *this) { 602*0b57cec5SDimitry Andric OS << '\n'; 603*0b57cec5SDimitry Andric // If we print the whole function, print it at its most verbose level. 604*0b57cec5SDimitry Andric BB.print(OS, MST, Indexes, /*IsStandalone=*/true); 605*0b57cec5SDimitry Andric } 606*0b57cec5SDimitry Andric 607*0b57cec5SDimitry Andric OS << "\n# End machine code for function " << getName() << ".\n\n"; 608*0b57cec5SDimitry Andric } 609*0b57cec5SDimitry Andric 610480093f4SDimitry Andric /// True if this function needs frame moves for debug or exceptions. 611480093f4SDimitry Andric bool MachineFunction::needsFrameMoves() const { 612480093f4SDimitry Andric return getMMI().hasDebugInfo() || 613480093f4SDimitry Andric getTarget().Options.ForceDwarfFrameSection || 614480093f4SDimitry Andric F.needsUnwindTableEntry(); 615480093f4SDimitry Andric } 616480093f4SDimitry Andric 617*0b57cec5SDimitry Andric namespace llvm { 618*0b57cec5SDimitry Andric 619*0b57cec5SDimitry Andric template<> 620*0b57cec5SDimitry Andric struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 621*0b57cec5SDimitry Andric DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {} 622*0b57cec5SDimitry Andric 623*0b57cec5SDimitry Andric static std::string getGraphName(const MachineFunction *F) { 624*0b57cec5SDimitry Andric return ("CFG for '" + F->getName() + "' function").str(); 625*0b57cec5SDimitry Andric } 626*0b57cec5SDimitry Andric 627*0b57cec5SDimitry Andric std::string getNodeLabel(const MachineBasicBlock *Node, 628*0b57cec5SDimitry Andric const MachineFunction *Graph) { 629*0b57cec5SDimitry Andric std::string OutStr; 630*0b57cec5SDimitry Andric { 631*0b57cec5SDimitry Andric raw_string_ostream OSS(OutStr); 632*0b57cec5SDimitry Andric 633*0b57cec5SDimitry Andric if (isSimple()) { 634*0b57cec5SDimitry Andric OSS << printMBBReference(*Node); 635*0b57cec5SDimitry Andric if (const BasicBlock *BB = Node->getBasicBlock()) 636*0b57cec5SDimitry Andric OSS << ": " << BB->getName(); 637*0b57cec5SDimitry Andric } else 638*0b57cec5SDimitry Andric Node->print(OSS); 639*0b57cec5SDimitry Andric } 640*0b57cec5SDimitry Andric 641*0b57cec5SDimitry Andric if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 642*0b57cec5SDimitry Andric 643*0b57cec5SDimitry Andric // Process string output to make it nicer... 644*0b57cec5SDimitry Andric for (unsigned i = 0; i != OutStr.length(); ++i) 645*0b57cec5SDimitry Andric if (OutStr[i] == '\n') { // Left justify 646*0b57cec5SDimitry Andric OutStr[i] = '\\'; 647*0b57cec5SDimitry Andric OutStr.insert(OutStr.begin()+i+1, 'l'); 648*0b57cec5SDimitry Andric } 649*0b57cec5SDimitry Andric return OutStr; 650*0b57cec5SDimitry Andric } 651*0b57cec5SDimitry Andric }; 652*0b57cec5SDimitry Andric 653*0b57cec5SDimitry Andric } // end namespace llvm 654*0b57cec5SDimitry Andric 655*0b57cec5SDimitry Andric void MachineFunction::viewCFG() const 656*0b57cec5SDimitry Andric { 657*0b57cec5SDimitry Andric #ifndef NDEBUG 658*0b57cec5SDimitry Andric ViewGraph(this, "mf" + getName()); 659*0b57cec5SDimitry Andric #else 660*0b57cec5SDimitry Andric errs() << "MachineFunction::viewCFG is only available in debug builds on " 661*0b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 662*0b57cec5SDimitry Andric #endif // NDEBUG 663*0b57cec5SDimitry Andric } 664*0b57cec5SDimitry Andric 665*0b57cec5SDimitry Andric void MachineFunction::viewCFGOnly() const 666*0b57cec5SDimitry Andric { 667*0b57cec5SDimitry Andric #ifndef NDEBUG 668*0b57cec5SDimitry Andric ViewGraph(this, "mf" + getName(), true); 669*0b57cec5SDimitry Andric #else 670*0b57cec5SDimitry Andric errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 671*0b57cec5SDimitry Andric << "systems with Graphviz or gv!\n"; 672*0b57cec5SDimitry Andric #endif // NDEBUG 673*0b57cec5SDimitry Andric } 674*0b57cec5SDimitry Andric 675*0b57cec5SDimitry Andric /// Add the specified physical register as a live-in value and 676*0b57cec5SDimitry Andric /// create a corresponding virtual register for it. 6775ffd83dbSDimitry Andric Register MachineFunction::addLiveIn(MCRegister PReg, 678*0b57cec5SDimitry Andric const TargetRegisterClass *RC) { 679*0b57cec5SDimitry Andric MachineRegisterInfo &MRI = getRegInfo(); 6805ffd83dbSDimitry Andric Register VReg = MRI.getLiveInVirtReg(PReg); 681*0b57cec5SDimitry Andric if (VReg) { 682*0b57cec5SDimitry Andric const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg); 683*0b57cec5SDimitry Andric (void)VRegRC; 684*0b57cec5SDimitry Andric // A physical register can be added several times. 685*0b57cec5SDimitry Andric // Between two calls, the register class of the related virtual register 686*0b57cec5SDimitry Andric // may have been constrained to match some operation constraints. 687*0b57cec5SDimitry Andric // In that case, check that the current register class includes the 688*0b57cec5SDimitry Andric // physical register and is a sub class of the specified RC. 689*0b57cec5SDimitry Andric assert((VRegRC == RC || (VRegRC->contains(PReg) && 690*0b57cec5SDimitry Andric RC->hasSubClassEq(VRegRC))) && 691*0b57cec5SDimitry Andric "Register class mismatch!"); 692*0b57cec5SDimitry Andric return VReg; 693*0b57cec5SDimitry Andric } 694*0b57cec5SDimitry Andric VReg = MRI.createVirtualRegister(RC); 695*0b57cec5SDimitry Andric MRI.addLiveIn(PReg, VReg); 696*0b57cec5SDimitry Andric return VReg; 697*0b57cec5SDimitry Andric } 698*0b57cec5SDimitry Andric 699*0b57cec5SDimitry Andric /// Return the MCSymbol for the specified non-empty jump table. 700*0b57cec5SDimitry Andric /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 701*0b57cec5SDimitry Andric /// normal 'L' label is returned. 702*0b57cec5SDimitry Andric MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 703*0b57cec5SDimitry Andric bool isLinkerPrivate) const { 704*0b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 705*0b57cec5SDimitry Andric assert(JumpTableInfo && "No jump tables"); 706*0b57cec5SDimitry Andric assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 707*0b57cec5SDimitry Andric 708*0b57cec5SDimitry Andric StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix() 709*0b57cec5SDimitry Andric : DL.getPrivateGlobalPrefix(); 710*0b57cec5SDimitry Andric SmallString<60> Name; 711*0b57cec5SDimitry Andric raw_svector_ostream(Name) 712*0b57cec5SDimitry Andric << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 713*0b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Name); 714*0b57cec5SDimitry Andric } 715*0b57cec5SDimitry Andric 716*0b57cec5SDimitry Andric /// Return a function-local symbol to represent the PIC base. 717*0b57cec5SDimitry Andric MCSymbol *MachineFunction::getPICBaseSymbol() const { 718*0b57cec5SDimitry Andric const DataLayout &DL = getDataLayout(); 719*0b57cec5SDimitry Andric return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) + 720*0b57cec5SDimitry Andric Twine(getFunctionNumber()) + "$pb"); 721*0b57cec5SDimitry Andric } 722*0b57cec5SDimitry Andric 723*0b57cec5SDimitry Andric /// \name Exception Handling 724*0b57cec5SDimitry Andric /// \{ 725*0b57cec5SDimitry Andric 726*0b57cec5SDimitry Andric LandingPadInfo & 727*0b57cec5SDimitry Andric MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) { 728*0b57cec5SDimitry Andric unsigned N = LandingPads.size(); 729*0b57cec5SDimitry Andric for (unsigned i = 0; i < N; ++i) { 730*0b57cec5SDimitry Andric LandingPadInfo &LP = LandingPads[i]; 731*0b57cec5SDimitry Andric if (LP.LandingPadBlock == LandingPad) 732*0b57cec5SDimitry Andric return LP; 733*0b57cec5SDimitry Andric } 734*0b57cec5SDimitry Andric 735*0b57cec5SDimitry Andric LandingPads.push_back(LandingPadInfo(LandingPad)); 736*0b57cec5SDimitry Andric return LandingPads[N]; 737*0b57cec5SDimitry Andric } 738*0b57cec5SDimitry Andric 739*0b57cec5SDimitry Andric void MachineFunction::addInvoke(MachineBasicBlock *LandingPad, 740*0b57cec5SDimitry Andric MCSymbol *BeginLabel, MCSymbol *EndLabel) { 741*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 742*0b57cec5SDimitry Andric LP.BeginLabels.push_back(BeginLabel); 743*0b57cec5SDimitry Andric LP.EndLabels.push_back(EndLabel); 744*0b57cec5SDimitry Andric } 745*0b57cec5SDimitry Andric 746*0b57cec5SDimitry Andric MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) { 747*0b57cec5SDimitry Andric MCSymbol *LandingPadLabel = Ctx.createTempSymbol(); 748*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 749*0b57cec5SDimitry Andric LP.LandingPadLabel = LandingPadLabel; 750*0b57cec5SDimitry Andric 751*0b57cec5SDimitry Andric const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI(); 752*0b57cec5SDimitry Andric if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) { 753*0b57cec5SDimitry Andric if (const auto *PF = 754*0b57cec5SDimitry Andric dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts())) 755*0b57cec5SDimitry Andric getMMI().addPersonality(PF); 756*0b57cec5SDimitry Andric 757*0b57cec5SDimitry Andric if (LPI->isCleanup()) 758*0b57cec5SDimitry Andric addCleanup(LandingPad); 759*0b57cec5SDimitry Andric 760*0b57cec5SDimitry Andric // FIXME: New EH - Add the clauses in reverse order. This isn't 100% 761*0b57cec5SDimitry Andric // correct, but we need to do it this way because of how the DWARF EH 762*0b57cec5SDimitry Andric // emitter processes the clauses. 763*0b57cec5SDimitry Andric for (unsigned I = LPI->getNumClauses(); I != 0; --I) { 764*0b57cec5SDimitry Andric Value *Val = LPI->getClause(I - 1); 765*0b57cec5SDimitry Andric if (LPI->isCatch(I - 1)) { 766*0b57cec5SDimitry Andric addCatchTypeInfo(LandingPad, 767*0b57cec5SDimitry Andric dyn_cast<GlobalValue>(Val->stripPointerCasts())); 768*0b57cec5SDimitry Andric } else { 769*0b57cec5SDimitry Andric // Add filters in a list. 770*0b57cec5SDimitry Andric auto *CVal = cast<Constant>(Val); 771*0b57cec5SDimitry Andric SmallVector<const GlobalValue *, 4> FilterList; 772349cc55cSDimitry Andric for (const Use &U : CVal->operands()) 773349cc55cSDimitry Andric FilterList.push_back(cast<GlobalValue>(U->stripPointerCasts())); 774*0b57cec5SDimitry Andric 775*0b57cec5SDimitry Andric addFilterTypeInfo(LandingPad, FilterList); 776*0b57cec5SDimitry Andric } 777*0b57cec5SDimitry Andric } 778*0b57cec5SDimitry Andric 779*0b57cec5SDimitry Andric } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) { 780*0b57cec5SDimitry Andric for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) { 781*0b57cec5SDimitry Andric Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts(); 782*0b57cec5SDimitry Andric addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo)); 783*0b57cec5SDimitry Andric } 784*0b57cec5SDimitry Andric 785*0b57cec5SDimitry Andric } else { 786*0b57cec5SDimitry Andric assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!"); 787*0b57cec5SDimitry Andric } 788*0b57cec5SDimitry Andric 789*0b57cec5SDimitry Andric return LandingPadLabel; 790*0b57cec5SDimitry Andric } 791*0b57cec5SDimitry Andric 792*0b57cec5SDimitry Andric void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad, 793*0b57cec5SDimitry Andric ArrayRef<const GlobalValue *> TyInfo) { 794*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 7950eae32dcSDimitry Andric for (const GlobalValue *GV : llvm::reverse(TyInfo)) 7960eae32dcSDimitry Andric LP.TypeIds.push_back(getTypeIDFor(GV)); 797*0b57cec5SDimitry Andric } 798*0b57cec5SDimitry Andric 799*0b57cec5SDimitry Andric void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad, 800*0b57cec5SDimitry Andric ArrayRef<const GlobalValue *> TyInfo) { 801*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 802*0b57cec5SDimitry Andric std::vector<unsigned> IdsInFilter(TyInfo.size()); 803*0b57cec5SDimitry Andric for (unsigned I = 0, E = TyInfo.size(); I != E; ++I) 804*0b57cec5SDimitry Andric IdsInFilter[I] = getTypeIDFor(TyInfo[I]); 805*0b57cec5SDimitry Andric LP.TypeIds.push_back(getFilterIDFor(IdsInFilter)); 806*0b57cec5SDimitry Andric } 807*0b57cec5SDimitry Andric 808*0b57cec5SDimitry Andric void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap, 809*0b57cec5SDimitry Andric bool TidyIfNoBeginLabels) { 810*0b57cec5SDimitry Andric for (unsigned i = 0; i != LandingPads.size(); ) { 811*0b57cec5SDimitry Andric LandingPadInfo &LandingPad = LandingPads[i]; 812*0b57cec5SDimitry Andric if (LandingPad.LandingPadLabel && 813*0b57cec5SDimitry Andric !LandingPad.LandingPadLabel->isDefined() && 814*0b57cec5SDimitry Andric (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0)) 815*0b57cec5SDimitry Andric LandingPad.LandingPadLabel = nullptr; 816*0b57cec5SDimitry Andric 817*0b57cec5SDimitry Andric // Special case: we *should* emit LPs with null LP MBB. This indicates 818*0b57cec5SDimitry Andric // "nounwind" case. 819*0b57cec5SDimitry Andric if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) { 820*0b57cec5SDimitry Andric LandingPads.erase(LandingPads.begin() + i); 821*0b57cec5SDimitry Andric continue; 822*0b57cec5SDimitry Andric } 823*0b57cec5SDimitry Andric 824*0b57cec5SDimitry Andric if (TidyIfNoBeginLabels) { 825*0b57cec5SDimitry Andric for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) { 826*0b57cec5SDimitry Andric MCSymbol *BeginLabel = LandingPad.BeginLabels[j]; 827*0b57cec5SDimitry Andric MCSymbol *EndLabel = LandingPad.EndLabels[j]; 828*0b57cec5SDimitry Andric if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) && 829*0b57cec5SDimitry Andric (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0))) 830*0b57cec5SDimitry Andric continue; 831*0b57cec5SDimitry Andric 832*0b57cec5SDimitry Andric LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j); 833*0b57cec5SDimitry Andric LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j); 834*0b57cec5SDimitry Andric --j; 835*0b57cec5SDimitry Andric --e; 836*0b57cec5SDimitry Andric } 837*0b57cec5SDimitry Andric 838*0b57cec5SDimitry Andric // Remove landing pads with no try-ranges. 839*0b57cec5SDimitry Andric if (LandingPads[i].BeginLabels.empty()) { 840*0b57cec5SDimitry Andric LandingPads.erase(LandingPads.begin() + i); 841*0b57cec5SDimitry Andric continue; 842*0b57cec5SDimitry Andric } 843*0b57cec5SDimitry Andric } 844*0b57cec5SDimitry Andric 845*0b57cec5SDimitry Andric // If there is no landing pad, ensure that the list of typeids is empty. 846*0b57cec5SDimitry Andric // If the only typeid is a cleanup, this is the same as having no typeids. 847*0b57cec5SDimitry Andric if (!LandingPad.LandingPadBlock || 848*0b57cec5SDimitry Andric (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0])) 849*0b57cec5SDimitry Andric LandingPad.TypeIds.clear(); 850*0b57cec5SDimitry Andric ++i; 851*0b57cec5SDimitry Andric } 852*0b57cec5SDimitry Andric } 853*0b57cec5SDimitry Andric 854*0b57cec5SDimitry Andric void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) { 855*0b57cec5SDimitry Andric LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad); 856*0b57cec5SDimitry Andric LP.TypeIds.push_back(0); 857*0b57cec5SDimitry Andric } 858*0b57cec5SDimitry Andric 859*0b57cec5SDimitry Andric void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym, 860*0b57cec5SDimitry Andric ArrayRef<unsigned> Sites) { 861*0b57cec5SDimitry Andric LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end()); 862*0b57cec5SDimitry Andric } 863*0b57cec5SDimitry Andric 864*0b57cec5SDimitry Andric unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) { 865*0b57cec5SDimitry Andric for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i) 866*0b57cec5SDimitry Andric if (TypeInfos[i] == TI) return i + 1; 867*0b57cec5SDimitry Andric 868*0b57cec5SDimitry Andric TypeInfos.push_back(TI); 869*0b57cec5SDimitry Andric return TypeInfos.size(); 870*0b57cec5SDimitry Andric } 871*0b57cec5SDimitry Andric 872*0b57cec5SDimitry Andric int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) { 873*0b57cec5SDimitry Andric // If the new filter coincides with the tail of an existing filter, then 874*0b57cec5SDimitry Andric // re-use the existing filter. Folding filters more than this requires 875*0b57cec5SDimitry Andric // re-ordering filters and/or their elements - probably not worth it. 876fe6060f1SDimitry Andric for (unsigned i : FilterEnds) { 877fe6060f1SDimitry Andric unsigned j = TyIds.size(); 878*0b57cec5SDimitry Andric 879*0b57cec5SDimitry Andric while (i && j) 880*0b57cec5SDimitry Andric if (FilterIds[--i] != TyIds[--j]) 881*0b57cec5SDimitry Andric goto try_next; 882*0b57cec5SDimitry Andric 883*0b57cec5SDimitry Andric if (!j) 884*0b57cec5SDimitry Andric // The new filter coincides with range [i, end) of the existing filter. 885*0b57cec5SDimitry Andric return -(1 + i); 886*0b57cec5SDimitry Andric 887*0b57cec5SDimitry Andric try_next:; 888*0b57cec5SDimitry Andric } 889*0b57cec5SDimitry Andric 890*0b57cec5SDimitry Andric // Add the new filter. 891*0b57cec5SDimitry Andric int FilterID = -(1 + FilterIds.size()); 892*0b57cec5SDimitry Andric FilterIds.reserve(FilterIds.size() + TyIds.size() + 1); 893e8d8bef9SDimitry Andric llvm::append_range(FilterIds, TyIds); 894*0b57cec5SDimitry Andric FilterEnds.push_back(FilterIds.size()); 895*0b57cec5SDimitry Andric FilterIds.push_back(0); // terminator 896*0b57cec5SDimitry Andric return FilterID; 897*0b57cec5SDimitry Andric } 898*0b57cec5SDimitry Andric 899480093f4SDimitry Andric MachineFunction::CallSiteInfoMap::iterator 900480093f4SDimitry Andric MachineFunction::getCallSiteInfo(const MachineInstr *MI) { 9015ffd83dbSDimitry Andric assert(MI->isCandidateForCallSiteEntry() && 9025ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates"); 903*0b57cec5SDimitry Andric 9045ffd83dbSDimitry Andric if (!Target.Options.EmitCallSiteInfo) 905480093f4SDimitry Andric return CallSitesInfo.end(); 906480093f4SDimitry Andric return CallSitesInfo.find(MI); 907*0b57cec5SDimitry Andric } 908*0b57cec5SDimitry Andric 9095ffd83dbSDimitry Andric /// Return the call machine instruction or find a call within bundle. 9105ffd83dbSDimitry Andric static const MachineInstr *getCallInstr(const MachineInstr *MI) { 9115ffd83dbSDimitry Andric if (!MI->isBundle()) 9125ffd83dbSDimitry Andric return MI; 913*0b57cec5SDimitry Andric 914fcaf7f86SDimitry Andric for (const auto &BMI : make_range(getBundleStart(MI->getIterator()), 9155ffd83dbSDimitry Andric getBundleEnd(MI->getIterator()))) 9165ffd83dbSDimitry Andric if (BMI.isCandidateForCallSiteEntry()) 9175ffd83dbSDimitry Andric return &BMI; 9188bcb0991SDimitry Andric 9195ffd83dbSDimitry Andric llvm_unreachable("Unexpected bundle without a call site candidate"); 9208bcb0991SDimitry Andric } 9218bcb0991SDimitry Andric 9228bcb0991SDimitry Andric void MachineFunction::eraseCallSiteInfo(const MachineInstr *MI) { 9235ffd83dbSDimitry Andric assert(MI->shouldUpdateCallSiteInfo() && 9245ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9255ffd83dbSDimitry Andric "candidates inside bundles"); 9265ffd83dbSDimitry Andric 9275ffd83dbSDimitry Andric const MachineInstr *CallMI = getCallInstr(MI); 9285ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(CallMI); 9298bcb0991SDimitry Andric if (CSIt == CallSitesInfo.end()) 9308bcb0991SDimitry Andric return; 9318bcb0991SDimitry Andric CallSitesInfo.erase(CSIt); 9328bcb0991SDimitry Andric } 9338bcb0991SDimitry Andric 9348bcb0991SDimitry Andric void MachineFunction::copyCallSiteInfo(const MachineInstr *Old, 9358bcb0991SDimitry Andric const MachineInstr *New) { 9365ffd83dbSDimitry Andric assert(Old->shouldUpdateCallSiteInfo() && 9375ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9385ffd83dbSDimitry Andric "candidates inside bundles"); 9398bcb0991SDimitry Andric 9405ffd83dbSDimitry Andric if (!New->isCandidateForCallSiteEntry()) 9415ffd83dbSDimitry Andric return eraseCallSiteInfo(Old); 9425ffd83dbSDimitry Andric 9435ffd83dbSDimitry Andric const MachineInstr *OldCallMI = getCallInstr(Old); 9445ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 9458bcb0991SDimitry Andric if (CSIt == CallSitesInfo.end()) 9468bcb0991SDimitry Andric return; 9478bcb0991SDimitry Andric 9488bcb0991SDimitry Andric CallSiteInfo CSInfo = CSIt->second; 949*0b57cec5SDimitry Andric CallSitesInfo[New] = CSInfo; 950*0b57cec5SDimitry Andric } 951*0b57cec5SDimitry Andric 9525ffd83dbSDimitry Andric void MachineFunction::moveCallSiteInfo(const MachineInstr *Old, 9535ffd83dbSDimitry Andric const MachineInstr *New) { 9545ffd83dbSDimitry Andric assert(Old->shouldUpdateCallSiteInfo() && 9555ffd83dbSDimitry Andric "Call site info refers only to call (MI) candidates or " 9565ffd83dbSDimitry Andric "candidates inside bundles"); 9575ffd83dbSDimitry Andric 9585ffd83dbSDimitry Andric if (!New->isCandidateForCallSiteEntry()) 9595ffd83dbSDimitry Andric return eraseCallSiteInfo(Old); 9605ffd83dbSDimitry Andric 9615ffd83dbSDimitry Andric const MachineInstr *OldCallMI = getCallInstr(Old); 9625ffd83dbSDimitry Andric CallSiteInfoMap::iterator CSIt = getCallSiteInfo(OldCallMI); 9635ffd83dbSDimitry Andric if (CSIt == CallSitesInfo.end()) 9645ffd83dbSDimitry Andric return; 9655ffd83dbSDimitry Andric 9665ffd83dbSDimitry Andric CallSiteInfo CSInfo = std::move(CSIt->second); 9675ffd83dbSDimitry Andric CallSitesInfo.erase(CSIt); 9685ffd83dbSDimitry Andric CallSitesInfo[New] = CSInfo; 9695ffd83dbSDimitry Andric } 9705ffd83dbSDimitry Andric 971e8d8bef9SDimitry Andric void MachineFunction::setDebugInstrNumberingCount(unsigned Num) { 972e8d8bef9SDimitry Andric DebugInstrNumberingCount = Num; 973e8d8bef9SDimitry Andric } 974e8d8bef9SDimitry Andric 975e8d8bef9SDimitry Andric void MachineFunction::makeDebugValueSubstitution(DebugInstrOperandPair A, 976fe6060f1SDimitry Andric DebugInstrOperandPair B, 977fe6060f1SDimitry Andric unsigned Subreg) { 978fe6060f1SDimitry Andric // Catch any accidental self-loops. 979fe6060f1SDimitry Andric assert(A.first != B.first); 980349cc55cSDimitry Andric // Don't allow any substitutions _from_ the memory operand number. 981349cc55cSDimitry Andric assert(A.second != DebugOperandMemNumber); 982349cc55cSDimitry Andric 983fe6060f1SDimitry Andric DebugValueSubstitutions.push_back({A, B, Subreg}); 984e8d8bef9SDimitry Andric } 985e8d8bef9SDimitry Andric 986e8d8bef9SDimitry Andric void MachineFunction::substituteDebugValuesForInst(const MachineInstr &Old, 987e8d8bef9SDimitry Andric MachineInstr &New, 988e8d8bef9SDimitry Andric unsigned MaxOperand) { 989e8d8bef9SDimitry Andric // If the Old instruction wasn't tracked at all, there is no work to do. 990e8d8bef9SDimitry Andric unsigned OldInstrNum = Old.peekDebugInstrNum(); 991e8d8bef9SDimitry Andric if (!OldInstrNum) 992e8d8bef9SDimitry Andric return; 993e8d8bef9SDimitry Andric 994e8d8bef9SDimitry Andric // Iterate over all operands looking for defs to create substitutions for. 995e8d8bef9SDimitry Andric // Avoid creating new instr numbers unless we create a new substitution. 996e8d8bef9SDimitry Andric // While this has no functional effect, it risks confusing someone reading 997e8d8bef9SDimitry Andric // MIR output. 998e8d8bef9SDimitry Andric // Examine all the operands, or the first N specified by the caller. 999e8d8bef9SDimitry Andric MaxOperand = std::min(MaxOperand, Old.getNumOperands()); 1000fe6060f1SDimitry Andric for (unsigned int I = 0; I < MaxOperand; ++I) { 1001e8d8bef9SDimitry Andric const auto &OldMO = Old.getOperand(I); 1002e8d8bef9SDimitry Andric auto &NewMO = New.getOperand(I); 1003e8d8bef9SDimitry Andric (void)NewMO; 1004e8d8bef9SDimitry Andric 1005e8d8bef9SDimitry Andric if (!OldMO.isReg() || !OldMO.isDef()) 1006e8d8bef9SDimitry Andric continue; 1007e8d8bef9SDimitry Andric assert(NewMO.isDef()); 1008e8d8bef9SDimitry Andric 1009e8d8bef9SDimitry Andric unsigned NewInstrNum = New.getDebugInstrNum(); 1010e8d8bef9SDimitry Andric makeDebugValueSubstitution(std::make_pair(OldInstrNum, I), 1011e8d8bef9SDimitry Andric std::make_pair(NewInstrNum, I)); 1012e8d8bef9SDimitry Andric } 1013e8d8bef9SDimitry Andric } 1014e8d8bef9SDimitry Andric 101581ad6265SDimitry Andric auto MachineFunction::salvageCopySSA( 101681ad6265SDimitry Andric MachineInstr &MI, DenseMap<Register, DebugInstrOperandPair> &DbgPHICache) 101781ad6265SDimitry Andric -> DebugInstrOperandPair { 101881ad6265SDimitry Andric const TargetInstrInfo &TII = *getSubtarget().getInstrInfo(); 101981ad6265SDimitry Andric 102081ad6265SDimitry Andric // Check whether this copy-like instruction has already been salvaged into 102181ad6265SDimitry Andric // an operand pair. 102281ad6265SDimitry Andric Register Dest; 102381ad6265SDimitry Andric if (auto CopyDstSrc = TII.isCopyInstr(MI)) { 102481ad6265SDimitry Andric Dest = CopyDstSrc->Destination->getReg(); 102581ad6265SDimitry Andric } else { 102681ad6265SDimitry Andric assert(MI.isSubregToReg()); 102781ad6265SDimitry Andric Dest = MI.getOperand(0).getReg(); 102881ad6265SDimitry Andric } 102981ad6265SDimitry Andric 103081ad6265SDimitry Andric auto CacheIt = DbgPHICache.find(Dest); 103181ad6265SDimitry Andric if (CacheIt != DbgPHICache.end()) 103281ad6265SDimitry Andric return CacheIt->second; 103381ad6265SDimitry Andric 103481ad6265SDimitry Andric // Calculate the instruction number to use, or install a DBG_PHI. 103581ad6265SDimitry Andric auto OperandPair = salvageCopySSAImpl(MI); 103681ad6265SDimitry Andric DbgPHICache.insert({Dest, OperandPair}); 103781ad6265SDimitry Andric return OperandPair; 103881ad6265SDimitry Andric } 103981ad6265SDimitry Andric 104081ad6265SDimitry Andric auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI) 1041fe6060f1SDimitry Andric -> DebugInstrOperandPair { 1042fe6060f1SDimitry Andric MachineRegisterInfo &MRI = getRegInfo(); 1043fe6060f1SDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo(); 1044fe6060f1SDimitry Andric const TargetInstrInfo &TII = *getSubtarget().getInstrInfo(); 1045fe6060f1SDimitry Andric 1046fe6060f1SDimitry Andric // Chase the value read by a copy-like instruction back to the instruction 1047fe6060f1SDimitry Andric // that ultimately _defines_ that value. This may pass: 1048fe6060f1SDimitry Andric // * Through multiple intermediate copies, including subregister moves / 1049fe6060f1SDimitry Andric // copies, 1050fe6060f1SDimitry Andric // * Copies from physical registers that must then be traced back to the 1051fe6060f1SDimitry Andric // defining instruction, 1052fe6060f1SDimitry Andric // * Or, physical registers may be live-in to (only) the entry block, which 1053fe6060f1SDimitry Andric // requires a DBG_PHI to be created. 1054fe6060f1SDimitry Andric // We can pursue this problem in that order: trace back through copies, 1055fe6060f1SDimitry Andric // optionally through a physical register, to a defining instruction. We 1056fe6060f1SDimitry Andric // should never move from physreg to vreg. As we're still in SSA form, no need 1057fe6060f1SDimitry Andric // to worry about partial definitions of registers. 1058fe6060f1SDimitry Andric 1059fe6060f1SDimitry Andric // Helper lambda to interpret a copy-like instruction. Takes instruction, 1060fe6060f1SDimitry Andric // returns the register read and any subregister identifying which part is 1061fe6060f1SDimitry Andric // read. 1062fe6060f1SDimitry Andric auto GetRegAndSubreg = 1063fe6060f1SDimitry Andric [&](const MachineInstr &Cpy) -> std::pair<Register, unsigned> { 1064fe6060f1SDimitry Andric Register NewReg, OldReg; 1065fe6060f1SDimitry Andric unsigned SubReg; 1066fe6060f1SDimitry Andric if (Cpy.isCopy()) { 1067fe6060f1SDimitry Andric OldReg = Cpy.getOperand(0).getReg(); 1068fe6060f1SDimitry Andric NewReg = Cpy.getOperand(1).getReg(); 1069fe6060f1SDimitry Andric SubReg = Cpy.getOperand(1).getSubReg(); 1070fe6060f1SDimitry Andric } else if (Cpy.isSubregToReg()) { 1071fe6060f1SDimitry Andric OldReg = Cpy.getOperand(0).getReg(); 1072fe6060f1SDimitry Andric NewReg = Cpy.getOperand(2).getReg(); 1073fe6060f1SDimitry Andric SubReg = Cpy.getOperand(3).getImm(); 1074fe6060f1SDimitry Andric } else { 1075fe6060f1SDimitry Andric auto CopyDetails = *TII.isCopyInstr(Cpy); 1076fe6060f1SDimitry Andric const MachineOperand &Src = *CopyDetails.Source; 1077fe6060f1SDimitry Andric const MachineOperand &Dest = *CopyDetails.Destination; 1078fe6060f1SDimitry Andric OldReg = Dest.getReg(); 1079fe6060f1SDimitry Andric NewReg = Src.getReg(); 1080fe6060f1SDimitry Andric SubReg = Src.getSubReg(); 1081fe6060f1SDimitry Andric } 1082fe6060f1SDimitry Andric 1083fe6060f1SDimitry Andric return {NewReg, SubReg}; 1084fe6060f1SDimitry Andric }; 1085fe6060f1SDimitry Andric 1086fe6060f1SDimitry Andric // First seek either the defining instruction, or a copy from a physreg. 1087fe6060f1SDimitry Andric // During search, the current state is the current copy instruction, and which 1088fe6060f1SDimitry Andric // register we've read. Accumulate qualifying subregisters into SubregsSeen; 1089fe6060f1SDimitry Andric // deal with those later. 1090fe6060f1SDimitry Andric auto State = GetRegAndSubreg(MI); 1091fe6060f1SDimitry Andric auto CurInst = MI.getIterator(); 1092fe6060f1SDimitry Andric SmallVector<unsigned, 4> SubregsSeen; 1093fe6060f1SDimitry Andric while (true) { 1094fe6060f1SDimitry Andric // If we've found a copy from a physreg, first portion of search is over. 1095fe6060f1SDimitry Andric if (!State.first.isVirtual()) 1096fe6060f1SDimitry Andric break; 1097fe6060f1SDimitry Andric 1098fe6060f1SDimitry Andric // Record any subregister qualifier. 1099fe6060f1SDimitry Andric if (State.second) 1100fe6060f1SDimitry Andric SubregsSeen.push_back(State.second); 1101fe6060f1SDimitry Andric 1102fe6060f1SDimitry Andric assert(MRI.hasOneDef(State.first)); 1103fe6060f1SDimitry Andric MachineInstr &Inst = *MRI.def_begin(State.first)->getParent(); 1104fe6060f1SDimitry Andric CurInst = Inst.getIterator(); 1105fe6060f1SDimitry Andric 1106fe6060f1SDimitry Andric // Any non-copy instruction is the defining instruction we're seeking. 1107fe6060f1SDimitry Andric if (!Inst.isCopyLike() && !TII.isCopyInstr(Inst)) 1108fe6060f1SDimitry Andric break; 1109fe6060f1SDimitry Andric State = GetRegAndSubreg(Inst); 1110fe6060f1SDimitry Andric }; 1111fe6060f1SDimitry Andric 1112fe6060f1SDimitry Andric // Helper lambda to apply additional subregister substitutions to a known 1113fe6060f1SDimitry Andric // instruction/operand pair. Adds new (fake) substitutions so that we can 1114fe6060f1SDimitry Andric // record the subregister. FIXME: this isn't very space efficient if multiple 1115fe6060f1SDimitry Andric // values are tracked back through the same copies; cache something later. 1116fe6060f1SDimitry Andric auto ApplySubregisters = 1117fe6060f1SDimitry Andric [&](DebugInstrOperandPair P) -> DebugInstrOperandPair { 1118fe6060f1SDimitry Andric for (unsigned Subreg : reverse(SubregsSeen)) { 1119fe6060f1SDimitry Andric // Fetch a new instruction number, not attached to an actual instruction. 1120fe6060f1SDimitry Andric unsigned NewInstrNumber = getNewDebugInstrNum(); 1121fe6060f1SDimitry Andric // Add a substitution from the "new" number to the known one, with a 1122fe6060f1SDimitry Andric // qualifying subreg. 1123fe6060f1SDimitry Andric makeDebugValueSubstitution({NewInstrNumber, 0}, P, Subreg); 1124fe6060f1SDimitry Andric // Return the new number; to find the underlying value, consumers need to 1125fe6060f1SDimitry Andric // deal with the qualifying subreg. 1126fe6060f1SDimitry Andric P = {NewInstrNumber, 0}; 1127fe6060f1SDimitry Andric } 1128fe6060f1SDimitry Andric return P; 1129fe6060f1SDimitry Andric }; 1130fe6060f1SDimitry Andric 1131fe6060f1SDimitry Andric // If we managed to find the defining instruction after COPYs, return an 1132fe6060f1SDimitry Andric // instruction / operand pair after adding subregister qualifiers. 1133fe6060f1SDimitry Andric if (State.first.isVirtual()) { 1134fe6060f1SDimitry Andric // Virtual register def -- we can just look up where this happens. 1135fe6060f1SDimitry Andric MachineInstr *Inst = MRI.def_begin(State.first)->getParent(); 1136fe6060f1SDimitry Andric for (auto &MO : Inst->operands()) { 1137fe6060f1SDimitry Andric if (!MO.isReg() || !MO.isDef() || MO.getReg() != State.first) 1138fe6060f1SDimitry Andric continue; 1139fe6060f1SDimitry Andric return ApplySubregisters( 1140fe6060f1SDimitry Andric {Inst->getDebugInstrNum(), Inst->getOperandNo(&MO)}); 1141fe6060f1SDimitry Andric } 1142fe6060f1SDimitry Andric 1143fe6060f1SDimitry Andric llvm_unreachable("Vreg def with no corresponding operand?"); 1144fe6060f1SDimitry Andric } 1145fe6060f1SDimitry Andric 1146fe6060f1SDimitry Andric // Our search ended in a copy from a physreg: walk back up the function 1147fe6060f1SDimitry Andric // looking for whatever defines the physreg. 1148fe6060f1SDimitry Andric assert(CurInst->isCopyLike() || TII.isCopyInstr(*CurInst)); 1149fe6060f1SDimitry Andric State = GetRegAndSubreg(*CurInst); 1150fe6060f1SDimitry Andric Register RegToSeek = State.first; 1151fe6060f1SDimitry Andric 1152fe6060f1SDimitry Andric auto RMII = CurInst->getReverseIterator(); 1153fe6060f1SDimitry Andric auto PrevInstrs = make_range(RMII, CurInst->getParent()->instr_rend()); 1154fe6060f1SDimitry Andric for (auto &ToExamine : PrevInstrs) { 1155fe6060f1SDimitry Andric for (auto &MO : ToExamine.operands()) { 1156fe6060f1SDimitry Andric // Test for operand that defines something aliasing RegToSeek. 1157fe6060f1SDimitry Andric if (!MO.isReg() || !MO.isDef() || 1158fe6060f1SDimitry Andric !TRI.regsOverlap(RegToSeek, MO.getReg())) 1159fe6060f1SDimitry Andric continue; 1160fe6060f1SDimitry Andric 1161fe6060f1SDimitry Andric return ApplySubregisters( 1162fe6060f1SDimitry Andric {ToExamine.getDebugInstrNum(), ToExamine.getOperandNo(&MO)}); 1163fe6060f1SDimitry Andric } 1164fe6060f1SDimitry Andric } 1165fe6060f1SDimitry Andric 1166fe6060f1SDimitry Andric MachineBasicBlock &InsertBB = *CurInst->getParent(); 1167fe6060f1SDimitry Andric 1168fe6060f1SDimitry Andric // We reached the start of the block before finding a defining instruction. 116981ad6265SDimitry Andric // There are numerous scenarios where this can happen: 117081ad6265SDimitry Andric // * Constant physical registers, 117181ad6265SDimitry Andric // * Several intrinsics that allow LLVM-IR to read arbitary registers, 117281ad6265SDimitry Andric // * Arguments in the entry block, 117381ad6265SDimitry Andric // * Exception handling landing pads. 117481ad6265SDimitry Andric // Validating all of them is too difficult, so just insert a DBG_PHI reading 117581ad6265SDimitry Andric // the variable value at this position, rather than checking it makes sense. 1176fe6060f1SDimitry Andric 1177fe6060f1SDimitry Andric // Create DBG_PHI for specified physreg. 1178fe6060f1SDimitry Andric auto Builder = BuildMI(InsertBB, InsertBB.getFirstNonPHI(), DebugLoc(), 1179fe6060f1SDimitry Andric TII.get(TargetOpcode::DBG_PHI)); 1180349cc55cSDimitry Andric Builder.addReg(State.first); 1181fe6060f1SDimitry Andric unsigned NewNum = getNewDebugInstrNum(); 1182fe6060f1SDimitry Andric Builder.addImm(NewNum); 1183fe6060f1SDimitry Andric return ApplySubregisters({NewNum, 0u}); 1184fe6060f1SDimitry Andric } 1185fe6060f1SDimitry Andric 1186fe6060f1SDimitry Andric void MachineFunction::finalizeDebugInstrRefs() { 1187fe6060f1SDimitry Andric auto *TII = getSubtarget().getInstrInfo(); 1188fe6060f1SDimitry Andric 11894824e7fdSDimitry Andric auto MakeUndefDbgValue = [&](MachineInstr &MI) { 1190fe6060f1SDimitry Andric const MCInstrDesc &RefII = TII->get(TargetOpcode::DBG_VALUE); 1191fe6060f1SDimitry Andric MI.setDesc(RefII); 11924824e7fdSDimitry Andric MI.getOperand(0).setReg(0); 1193fe6060f1SDimitry Andric MI.getOperand(1).ChangeToRegister(0, false); 1194fe6060f1SDimitry Andric }; 1195fe6060f1SDimitry Andric 119681ad6265SDimitry Andric DenseMap<Register, DebugInstrOperandPair> ArgDbgPHIs; 1197fe6060f1SDimitry Andric for (auto &MBB : *this) { 1198fe6060f1SDimitry Andric for (auto &MI : MBB) { 1199fe6060f1SDimitry Andric if (!MI.isDebugRef() || !MI.getOperand(0).isReg()) 1200fe6060f1SDimitry Andric continue; 1201fe6060f1SDimitry Andric 1202fe6060f1SDimitry Andric Register Reg = MI.getOperand(0).getReg(); 1203fe6060f1SDimitry Andric 1204fe6060f1SDimitry Andric // Some vregs can be deleted as redundant in the meantime. Mark those 12054824e7fdSDimitry Andric // as DBG_VALUE $noreg. Additionally, some normal instructions are 12064824e7fdSDimitry Andric // quickly deleted, leaving dangling references to vregs with no def. 12074824e7fdSDimitry Andric if (Reg == 0 || !RegInfo->hasOneDef(Reg)) { 12084824e7fdSDimitry Andric MakeUndefDbgValue(MI); 1209fe6060f1SDimitry Andric continue; 1210fe6060f1SDimitry Andric } 1211fe6060f1SDimitry Andric 1212fe6060f1SDimitry Andric assert(Reg.isVirtual()); 1213fe6060f1SDimitry Andric MachineInstr &DefMI = *RegInfo->def_instr_begin(Reg); 1214fe6060f1SDimitry Andric 1215fe6060f1SDimitry Andric // If we've found a copy-like instruction, follow it back to the 1216fe6060f1SDimitry Andric // instruction that defines the source value, see salvageCopySSA docs 1217fe6060f1SDimitry Andric // for why this is important. 1218fe6060f1SDimitry Andric if (DefMI.isCopyLike() || TII->isCopyInstr(DefMI)) { 121981ad6265SDimitry Andric auto Result = salvageCopySSA(DefMI, ArgDbgPHIs); 1220fe6060f1SDimitry Andric MI.getOperand(0).ChangeToImmediate(Result.first); 1221fe6060f1SDimitry Andric MI.getOperand(1).setImm(Result.second); 1222fe6060f1SDimitry Andric } else { 1223fe6060f1SDimitry Andric // Otherwise, identify the operand number that the VReg refers to. 1224fe6060f1SDimitry Andric unsigned OperandIdx = 0; 1225fe6060f1SDimitry Andric for (const auto &MO : DefMI.operands()) { 1226fe6060f1SDimitry Andric if (MO.isReg() && MO.isDef() && MO.getReg() == Reg) 1227fe6060f1SDimitry Andric break; 1228fe6060f1SDimitry Andric ++OperandIdx; 1229fe6060f1SDimitry Andric } 1230fe6060f1SDimitry Andric assert(OperandIdx < DefMI.getNumOperands()); 1231fe6060f1SDimitry Andric 1232fe6060f1SDimitry Andric // Morph this instr ref to point at the given instruction and operand. 1233fe6060f1SDimitry Andric unsigned ID = DefMI.getDebugInstrNum(); 1234fe6060f1SDimitry Andric MI.getOperand(0).ChangeToImmediate(ID); 1235fe6060f1SDimitry Andric MI.getOperand(1).setImm(OperandIdx); 1236fe6060f1SDimitry Andric } 1237fe6060f1SDimitry Andric } 1238fe6060f1SDimitry Andric } 1239fe6060f1SDimitry Andric } 1240fe6060f1SDimitry Andric 1241349cc55cSDimitry Andric bool MachineFunction::useDebugInstrRef() const { 1242349cc55cSDimitry Andric // Disable instr-ref at -O0: it's very slow (in compile time). We can still 1243349cc55cSDimitry Andric // have optimized code inlined into this unoptimized code, however with 1244349cc55cSDimitry Andric // fewer and less aggressive optimizations happening, coverage and accuracy 1245349cc55cSDimitry Andric // should not suffer. 1246349cc55cSDimitry Andric if (getTarget().getOptLevel() == CodeGenOpt::None) 1247349cc55cSDimitry Andric return false; 1248349cc55cSDimitry Andric 1249349cc55cSDimitry Andric // Don't use instr-ref if this function is marked optnone. 1250349cc55cSDimitry Andric if (F.hasFnAttribute(Attribute::OptimizeNone)) 1251349cc55cSDimitry Andric return false; 1252349cc55cSDimitry Andric 125304eeddc0SDimitry Andric if (llvm::debuginfoShouldUseDebugInstrRef(getTarget().getTargetTriple())) 1254349cc55cSDimitry Andric return true; 1255349cc55cSDimitry Andric 1256349cc55cSDimitry Andric return false; 1257349cc55cSDimitry Andric } 1258349cc55cSDimitry Andric 1259349cc55cSDimitry Andric // Use one million as a high / reserved number. 1260349cc55cSDimitry Andric const unsigned MachineFunction::DebugOperandMemNumber = 1000000; 1261349cc55cSDimitry Andric 1262*0b57cec5SDimitry Andric /// \} 1263*0b57cec5SDimitry Andric 1264*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1265*0b57cec5SDimitry Andric // MachineJumpTableInfo implementation 1266*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1267*0b57cec5SDimitry Andric 1268*0b57cec5SDimitry Andric /// Return the size of each entry in the jump table. 1269*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const { 1270*0b57cec5SDimitry Andric // The size of a jump table entry is 4 bytes unless the entry is just the 1271*0b57cec5SDimitry Andric // address of a block, in which case it is the pointer size. 1272*0b57cec5SDimitry Andric switch (getEntryKind()) { 1273*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 1274*0b57cec5SDimitry Andric return TD.getPointerSize(); 1275*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 1276*0b57cec5SDimitry Andric return 8; 1277*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 1278*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 1279*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 1280*0b57cec5SDimitry Andric return 4; 1281*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 1282*0b57cec5SDimitry Andric return 0; 1283*0b57cec5SDimitry Andric } 1284*0b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 1285*0b57cec5SDimitry Andric } 1286*0b57cec5SDimitry Andric 1287*0b57cec5SDimitry Andric /// Return the alignment of each entry in the jump table. 1288*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const { 1289*0b57cec5SDimitry Andric // The alignment of a jump table entry is the alignment of int32 unless the 1290*0b57cec5SDimitry Andric // entry is just the address of a block, in which case it is the pointer 1291*0b57cec5SDimitry Andric // alignment. 1292*0b57cec5SDimitry Andric switch (getEntryKind()) { 1293*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_BlockAddress: 12948bcb0991SDimitry Andric return TD.getPointerABIAlignment(0).value(); 1295*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel64BlockAddress: 12968bcb0991SDimitry Andric return TD.getABIIntegerTypeAlignment(64).value(); 1297*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_GPRel32BlockAddress: 1298*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_LabelDifference32: 1299*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Custom32: 13008bcb0991SDimitry Andric return TD.getABIIntegerTypeAlignment(32).value(); 1301*0b57cec5SDimitry Andric case MachineJumpTableInfo::EK_Inline: 1302*0b57cec5SDimitry Andric return 1; 1303*0b57cec5SDimitry Andric } 1304*0b57cec5SDimitry Andric llvm_unreachable("Unknown jump table encoding!"); 1305*0b57cec5SDimitry Andric } 1306*0b57cec5SDimitry Andric 1307*0b57cec5SDimitry Andric /// Create a new jump table entry in the jump table info. 1308*0b57cec5SDimitry Andric unsigned MachineJumpTableInfo::createJumpTableIndex( 1309*0b57cec5SDimitry Andric const std::vector<MachineBasicBlock*> &DestBBs) { 1310*0b57cec5SDimitry Andric assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 1311*0b57cec5SDimitry Andric JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 1312*0b57cec5SDimitry Andric return JumpTables.size()-1; 1313*0b57cec5SDimitry Andric } 1314*0b57cec5SDimitry Andric 1315*0b57cec5SDimitry Andric /// If Old is the target of any jump tables, update the jump tables to branch 1316*0b57cec5SDimitry Andric /// to New instead. 1317*0b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 1318*0b57cec5SDimitry Andric MachineBasicBlock *New) { 1319*0b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 1320*0b57cec5SDimitry Andric bool MadeChange = false; 1321*0b57cec5SDimitry Andric for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 1322*0b57cec5SDimitry Andric ReplaceMBBInJumpTable(i, Old, New); 1323*0b57cec5SDimitry Andric return MadeChange; 1324*0b57cec5SDimitry Andric } 1325*0b57cec5SDimitry Andric 1326e8d8bef9SDimitry Andric /// If MBB is present in any jump tables, remove it. 1327e8d8bef9SDimitry Andric bool MachineJumpTableInfo::RemoveMBBFromJumpTables(MachineBasicBlock *MBB) { 1328e8d8bef9SDimitry Andric bool MadeChange = false; 1329e8d8bef9SDimitry Andric for (MachineJumpTableEntry &JTE : JumpTables) { 1330e8d8bef9SDimitry Andric auto removeBeginItr = std::remove(JTE.MBBs.begin(), JTE.MBBs.end(), MBB); 1331e8d8bef9SDimitry Andric MadeChange |= (removeBeginItr != JTE.MBBs.end()); 1332e8d8bef9SDimitry Andric JTE.MBBs.erase(removeBeginItr, JTE.MBBs.end()); 1333e8d8bef9SDimitry Andric } 1334e8d8bef9SDimitry Andric return MadeChange; 1335e8d8bef9SDimitry Andric } 1336e8d8bef9SDimitry Andric 1337*0b57cec5SDimitry Andric /// If Old is a target of the jump tables, update the jump table to branch to 1338*0b57cec5SDimitry Andric /// New instead. 1339*0b57cec5SDimitry Andric bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 1340*0b57cec5SDimitry Andric MachineBasicBlock *Old, 1341*0b57cec5SDimitry Andric MachineBasicBlock *New) { 1342*0b57cec5SDimitry Andric assert(Old != New && "Not making a change?"); 1343*0b57cec5SDimitry Andric bool MadeChange = false; 1344*0b57cec5SDimitry Andric MachineJumpTableEntry &JTE = JumpTables[Idx]; 13454824e7fdSDimitry Andric for (MachineBasicBlock *&MBB : JTE.MBBs) 13464824e7fdSDimitry Andric if (MBB == Old) { 13474824e7fdSDimitry Andric MBB = New; 1348*0b57cec5SDimitry Andric MadeChange = true; 1349*0b57cec5SDimitry Andric } 1350*0b57cec5SDimitry Andric return MadeChange; 1351*0b57cec5SDimitry Andric } 1352*0b57cec5SDimitry Andric 1353*0b57cec5SDimitry Andric void MachineJumpTableInfo::print(raw_ostream &OS) const { 1354*0b57cec5SDimitry Andric if (JumpTables.empty()) return; 1355*0b57cec5SDimitry Andric 1356*0b57cec5SDimitry Andric OS << "Jump Tables:\n"; 1357*0b57cec5SDimitry Andric 1358*0b57cec5SDimitry Andric for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 1359*0b57cec5SDimitry Andric OS << printJumpTableEntryReference(i) << ':'; 13604824e7fdSDimitry Andric for (const MachineBasicBlock *MBB : JumpTables[i].MBBs) 13614824e7fdSDimitry Andric OS << ' ' << printMBBReference(*MBB); 1362*0b57cec5SDimitry Andric if (i != e) 1363*0b57cec5SDimitry Andric OS << '\n'; 1364*0b57cec5SDimitry Andric } 1365*0b57cec5SDimitry Andric 1366*0b57cec5SDimitry Andric OS << '\n'; 1367*0b57cec5SDimitry Andric } 1368*0b57cec5SDimitry Andric 1369*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1370*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); } 1371*0b57cec5SDimitry Andric #endif 1372*0b57cec5SDimitry Andric 1373*0b57cec5SDimitry Andric Printable llvm::printJumpTableEntryReference(unsigned Idx) { 1374*0b57cec5SDimitry Andric return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; }); 1375*0b57cec5SDimitry Andric } 1376*0b57cec5SDimitry Andric 1377*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1378*0b57cec5SDimitry Andric // MachineConstantPool implementation 1379*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1380*0b57cec5SDimitry Andric 1381*0b57cec5SDimitry Andric void MachineConstantPoolValue::anchor() {} 1382*0b57cec5SDimitry Andric 1383e8d8bef9SDimitry Andric unsigned MachineConstantPoolValue::getSizeInBytes(const DataLayout &DL) const { 1384e8d8bef9SDimitry Andric return DL.getTypeAllocSize(Ty); 1385e8d8bef9SDimitry Andric } 1386e8d8bef9SDimitry Andric 1387e8d8bef9SDimitry Andric unsigned MachineConstantPoolEntry::getSizeInBytes(const DataLayout &DL) const { 1388*0b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 1389e8d8bef9SDimitry Andric return Val.MachineCPVal->getSizeInBytes(DL); 1390e8d8bef9SDimitry Andric return DL.getTypeAllocSize(Val.ConstVal->getType()); 1391*0b57cec5SDimitry Andric } 1392*0b57cec5SDimitry Andric 1393*0b57cec5SDimitry Andric bool MachineConstantPoolEntry::needsRelocation() const { 1394*0b57cec5SDimitry Andric if (isMachineConstantPoolEntry()) 1395*0b57cec5SDimitry Andric return true; 1396fe6060f1SDimitry Andric return Val.ConstVal->needsDynamicRelocation(); 1397*0b57cec5SDimitry Andric } 1398*0b57cec5SDimitry Andric 1399*0b57cec5SDimitry Andric SectionKind 1400*0b57cec5SDimitry Andric MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const { 1401*0b57cec5SDimitry Andric if (needsRelocation()) 1402*0b57cec5SDimitry Andric return SectionKind::getReadOnlyWithRel(); 1403e8d8bef9SDimitry Andric switch (getSizeInBytes(*DL)) { 1404*0b57cec5SDimitry Andric case 4: 1405*0b57cec5SDimitry Andric return SectionKind::getMergeableConst4(); 1406*0b57cec5SDimitry Andric case 8: 1407*0b57cec5SDimitry Andric return SectionKind::getMergeableConst8(); 1408*0b57cec5SDimitry Andric case 16: 1409*0b57cec5SDimitry Andric return SectionKind::getMergeableConst16(); 1410*0b57cec5SDimitry Andric case 32: 1411*0b57cec5SDimitry Andric return SectionKind::getMergeableConst32(); 1412*0b57cec5SDimitry Andric default: 1413*0b57cec5SDimitry Andric return SectionKind::getReadOnly(); 1414*0b57cec5SDimitry Andric } 1415*0b57cec5SDimitry Andric } 1416*0b57cec5SDimitry Andric 1417*0b57cec5SDimitry Andric MachineConstantPool::~MachineConstantPool() { 1418*0b57cec5SDimitry Andric // A constant may be a member of both Constants and MachineCPVsSharingEntries, 1419*0b57cec5SDimitry Andric // so keep track of which we've deleted to avoid double deletions. 1420*0b57cec5SDimitry Andric DenseSet<MachineConstantPoolValue*> Deleted; 14210eae32dcSDimitry Andric for (const MachineConstantPoolEntry &C : Constants) 14220eae32dcSDimitry Andric if (C.isMachineConstantPoolEntry()) { 14230eae32dcSDimitry Andric Deleted.insert(C.Val.MachineCPVal); 14240eae32dcSDimitry Andric delete C.Val.MachineCPVal; 1425*0b57cec5SDimitry Andric } 1426fe6060f1SDimitry Andric for (MachineConstantPoolValue *CPV : MachineCPVsSharingEntries) { 1427fe6060f1SDimitry Andric if (Deleted.count(CPV) == 0) 1428fe6060f1SDimitry Andric delete CPV; 1429*0b57cec5SDimitry Andric } 1430*0b57cec5SDimitry Andric } 1431*0b57cec5SDimitry Andric 1432*0b57cec5SDimitry Andric /// Test whether the given two constants can be allocated the same constant pool 1433*0b57cec5SDimitry Andric /// entry. 1434*0b57cec5SDimitry Andric static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 1435*0b57cec5SDimitry Andric const DataLayout &DL) { 1436*0b57cec5SDimitry Andric // Handle the trivial case quickly. 1437*0b57cec5SDimitry Andric if (A == B) return true; 1438*0b57cec5SDimitry Andric 1439*0b57cec5SDimitry Andric // If they have the same type but weren't the same constant, quickly 1440*0b57cec5SDimitry Andric // reject them. 1441*0b57cec5SDimitry Andric if (A->getType() == B->getType()) return false; 1442*0b57cec5SDimitry Andric 1443*0b57cec5SDimitry Andric // We can't handle structs or arrays. 1444*0b57cec5SDimitry Andric if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) || 1445*0b57cec5SDimitry Andric isa<StructType>(B->getType()) || isa<ArrayType>(B->getType())) 1446*0b57cec5SDimitry Andric return false; 1447*0b57cec5SDimitry Andric 1448*0b57cec5SDimitry Andric // For now, only support constants with the same size. 1449*0b57cec5SDimitry Andric uint64_t StoreSize = DL.getTypeStoreSize(A->getType()); 1450*0b57cec5SDimitry Andric if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128) 1451*0b57cec5SDimitry Andric return false; 1452*0b57cec5SDimitry Andric 1453*0b57cec5SDimitry Andric Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8); 1454*0b57cec5SDimitry Andric 1455*0b57cec5SDimitry Andric // Try constant folding a bitcast of both instructions to an integer. If we 1456*0b57cec5SDimitry Andric // get two identical ConstantInt's, then we are good to share them. We use 1457*0b57cec5SDimitry Andric // the constant folding APIs to do this so that we get the benefit of 1458*0b57cec5SDimitry Andric // DataLayout. 1459*0b57cec5SDimitry Andric if (isa<PointerType>(A->getType())) 1460*0b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::PtrToInt, 1461*0b57cec5SDimitry Andric const_cast<Constant *>(A), IntTy, DL); 1462*0b57cec5SDimitry Andric else if (A->getType() != IntTy) 1463*0b57cec5SDimitry Andric A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A), 1464*0b57cec5SDimitry Andric IntTy, DL); 1465*0b57cec5SDimitry Andric if (isa<PointerType>(B->getType())) 1466*0b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::PtrToInt, 1467*0b57cec5SDimitry Andric const_cast<Constant *>(B), IntTy, DL); 1468*0b57cec5SDimitry Andric else if (B->getType() != IntTy) 1469*0b57cec5SDimitry Andric B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B), 1470*0b57cec5SDimitry Andric IntTy, DL); 1471*0b57cec5SDimitry Andric 1472*0b57cec5SDimitry Andric return A == B; 1473*0b57cec5SDimitry Andric } 1474*0b57cec5SDimitry Andric 1475*0b57cec5SDimitry Andric /// Create a new entry in the constant pool or return an existing one. 1476*0b57cec5SDimitry Andric /// User must specify the log2 of the minimum required alignment for the object. 1477*0b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 14785ffd83dbSDimitry Andric Align Alignment) { 1479*0b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1480*0b57cec5SDimitry Andric 1481*0b57cec5SDimitry Andric // Check to see if we already have this constant. 1482*0b57cec5SDimitry Andric // 1483*0b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 1484*0b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) 1485*0b57cec5SDimitry Andric if (!Constants[i].isMachineConstantPoolEntry() && 1486*0b57cec5SDimitry Andric CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) { 14875ffd83dbSDimitry Andric if (Constants[i].getAlign() < Alignment) 1488*0b57cec5SDimitry Andric Constants[i].Alignment = Alignment; 1489*0b57cec5SDimitry Andric return i; 1490*0b57cec5SDimitry Andric } 1491*0b57cec5SDimitry Andric 1492*0b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 1493*0b57cec5SDimitry Andric return Constants.size()-1; 1494*0b57cec5SDimitry Andric } 1495*0b57cec5SDimitry Andric 1496*0b57cec5SDimitry Andric unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 14975ffd83dbSDimitry Andric Align Alignment) { 1498*0b57cec5SDimitry Andric if (Alignment > PoolAlignment) PoolAlignment = Alignment; 1499*0b57cec5SDimitry Andric 1500*0b57cec5SDimitry Andric // Check to see if we already have this constant. 1501*0b57cec5SDimitry Andric // 1502*0b57cec5SDimitry Andric // FIXME, this could be made much more efficient for large constant pools. 1503*0b57cec5SDimitry Andric int Idx = V->getExistingMachineCPValue(this, Alignment); 1504*0b57cec5SDimitry Andric if (Idx != -1) { 1505*0b57cec5SDimitry Andric MachineCPVsSharingEntries.insert(V); 1506*0b57cec5SDimitry Andric return (unsigned)Idx; 1507*0b57cec5SDimitry Andric } 1508*0b57cec5SDimitry Andric 1509*0b57cec5SDimitry Andric Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 1510*0b57cec5SDimitry Andric return Constants.size()-1; 1511*0b57cec5SDimitry Andric } 1512*0b57cec5SDimitry Andric 1513*0b57cec5SDimitry Andric void MachineConstantPool::print(raw_ostream &OS) const { 1514*0b57cec5SDimitry Andric if (Constants.empty()) return; 1515*0b57cec5SDimitry Andric 1516*0b57cec5SDimitry Andric OS << "Constant Pool:\n"; 1517*0b57cec5SDimitry Andric for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 1518*0b57cec5SDimitry Andric OS << " cp#" << i << ": "; 1519*0b57cec5SDimitry Andric if (Constants[i].isMachineConstantPoolEntry()) 1520*0b57cec5SDimitry Andric Constants[i].Val.MachineCPVal->print(OS); 1521*0b57cec5SDimitry Andric else 1522*0b57cec5SDimitry Andric Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false); 15235ffd83dbSDimitry Andric OS << ", align=" << Constants[i].getAlign().value(); 1524*0b57cec5SDimitry Andric OS << "\n"; 1525*0b57cec5SDimitry Andric } 1526*0b57cec5SDimitry Andric } 1527*0b57cec5SDimitry Andric 1528*0b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 1529*0b57cec5SDimitry Andric LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); } 1530*0b57cec5SDimitry Andric #endif 1531