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