1 //===- MachineFunction.cpp ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Collect native machine code information for a function. This allows
10 // target-specific information about the generated code to be stored with each
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallString.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Analysis/ConstantFolding.h"
25 #include "llvm/Analysis/EHPersonalities.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineConstantPool.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/CodeGen/MachineJumpTableInfo.h"
31 #include "llvm/CodeGen/MachineMemOperand.h"
32 #include "llvm/CodeGen/MachineModuleInfo.h"
33 #include "llvm/CodeGen/MachineRegisterInfo.h"
34 #include "llvm/CodeGen/PseudoSourceValue.h"
35 #include "llvm/CodeGen/TargetFrameLowering.h"
36 #include "llvm/CodeGen/TargetLowering.h"
37 #include "llvm/CodeGen/TargetRegisterInfo.h"
38 #include "llvm/CodeGen/TargetSubtargetInfo.h"
39 #include "llvm/CodeGen/WasmEHFuncInfo.h"
40 #include "llvm/CodeGen/WinEHFuncInfo.h"
41 #include "llvm/Config/llvm-config.h"
42 #include "llvm/IR/Attributes.h"
43 #include "llvm/IR/BasicBlock.h"
44 #include "llvm/IR/Constant.h"
45 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/DebugInfoMetadata.h"
47 #include "llvm/IR/DerivedTypes.h"
48 #include "llvm/IR/Function.h"
49 #include "llvm/IR/GlobalValue.h"
50 #include "llvm/IR/Instruction.h"
51 #include "llvm/IR/Instructions.h"
52 #include "llvm/IR/Metadata.h"
53 #include "llvm/IR/Module.h"
54 #include "llvm/IR/ModuleSlotTracker.h"
55 #include "llvm/IR/Value.h"
56 #include "llvm/MC/MCContext.h"
57 #include "llvm/MC/MCSymbol.h"
58 #include "llvm/MC/SectionKind.h"
59 #include "llvm/Support/Casting.h"
60 #include "llvm/Support/CommandLine.h"
61 #include "llvm/Support/Compiler.h"
62 #include "llvm/Support/DOTGraphTraits.h"
63 #include "llvm/Support/Debug.h"
64 #include "llvm/Support/ErrorHandling.h"
65 #include "llvm/Support/GraphWriter.h"
66 #include "llvm/Support/raw_ostream.h"
67 #include "llvm/Target/TargetMachine.h"
79 #define DEBUG_TYPE "codegen"
81 static cl::opt
<unsigned> AlignAllFunctions(
82 "align-all-functions",
83 cl::desc("Force the alignment of all functions in log2 format (e.g. 4 "
84 "means align on 16B boundaries)."),
85 cl::init(0), cl::Hidden
);
87 static const char *getPropertyName(MachineFunctionProperties::Property Prop
) {
88 using P
= MachineFunctionProperties::Property
;
91 case P::FailedISel
: return "FailedISel";
92 case P::IsSSA
: return "IsSSA";
93 case P::Legalized
: return "Legalized";
94 case P::NoPHIs
: return "NoPHIs";
95 case P::NoVRegs
: return "NoVRegs";
96 case P::RegBankSelected
: return "RegBankSelected";
97 case P::Selected
: return "Selected";
98 case P::TracksLiveness
: return "TracksLiveness";
100 llvm_unreachable("Invalid machine function property");
103 // Pin the vtable to this file.
104 void MachineFunction::Delegate::anchor() {}
106 void MachineFunctionProperties::print(raw_ostream
&OS
) const {
107 const char *Separator
= "";
108 for (BitVector::size_type I
= 0; I
< Properties
.size(); ++I
) {
111 OS
<< Separator
<< getPropertyName(static_cast<Property
>(I
));
116 //===----------------------------------------------------------------------===//
117 // MachineFunction implementation
118 //===----------------------------------------------------------------------===//
120 // Out-of-line virtual method.
121 MachineFunctionInfo::~MachineFunctionInfo() = default;
123 void ilist_alloc_traits
<MachineBasicBlock
>::deleteNode(MachineBasicBlock
*MBB
) {
124 MBB
->getParent()->DeleteMachineBasicBlock(MBB
);
127 static inline unsigned getFnStackAlignment(const TargetSubtargetInfo
*STI
,
129 if (F
.hasFnAttribute(Attribute::StackAlignment
))
130 return F
.getFnStackAlignment();
131 return STI
->getFrameLowering()->getStackAlignment();
134 MachineFunction::MachineFunction(const Function
&F
,
135 const LLVMTargetMachine
&Target
,
136 const TargetSubtargetInfo
&STI
,
137 unsigned FunctionNum
, MachineModuleInfo
&mmi
)
138 : F(F
), Target(Target
), STI(&STI
), Ctx(mmi
.getContext()), MMI(mmi
) {
139 FunctionNumber
= FunctionNum
;
143 void MachineFunction::handleInsertion(MachineInstr
&MI
) {
145 TheDelegate
->MF_HandleInsertion(MI
);
148 void MachineFunction::handleRemoval(MachineInstr
&MI
) {
150 TheDelegate
->MF_HandleRemoval(MI
);
153 void MachineFunction::init() {
154 // Assume the function starts in SSA form with correct liveness.
155 Properties
.set(MachineFunctionProperties::Property::IsSSA
);
156 Properties
.set(MachineFunctionProperties::Property::TracksLiveness
);
157 if (STI
->getRegisterInfo())
158 RegInfo
= new (Allocator
) MachineRegisterInfo(this);
163 // We can realign the stack if the target supports it and the user hasn't
164 // explicitly asked us not to.
165 bool CanRealignSP
= STI
->getFrameLowering()->isStackRealignable() &&
166 !F
.hasFnAttribute("no-realign-stack");
167 FrameInfo
= new (Allocator
) MachineFrameInfo(
168 getFnStackAlignment(STI
, F
), /*StackRealignable=*/CanRealignSP
,
169 /*ForcedRealign=*/CanRealignSP
&&
170 F
.hasFnAttribute(Attribute::StackAlignment
));
172 if (F
.hasFnAttribute(Attribute::StackAlignment
))
173 FrameInfo
->ensureMaxAlignment(F
.getFnStackAlignment());
175 ConstantPool
= new (Allocator
) MachineConstantPool(getDataLayout());
176 Alignment
= STI
->getTargetLowering()->getMinFunctionAlignment();
178 // FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
179 // FIXME: Use Function::hasOptSize().
180 if (!F
.hasFnAttribute(Attribute::OptimizeForSize
))
181 Alignment
= std::max(Alignment
,
182 STI
->getTargetLowering()->getPrefFunctionAlignment());
184 if (AlignAllFunctions
)
185 Alignment
= llvm::Align(1ULL << AlignAllFunctions
);
187 JumpTableInfo
= nullptr;
189 if (isFuncletEHPersonality(classifyEHPersonality(
190 F
.hasPersonalityFn() ? F
.getPersonalityFn() : nullptr))) {
191 WinEHInfo
= new (Allocator
) WinEHFuncInfo();
194 if (isScopedEHPersonality(classifyEHPersonality(
195 F
.hasPersonalityFn() ? F
.getPersonalityFn() : nullptr))) {
196 WasmEHInfo
= new (Allocator
) WasmEHFuncInfo();
199 assert(Target
.isCompatibleDataLayout(getDataLayout()) &&
200 "Can't create a MachineFunction using a Module with a "
201 "Target-incompatible DataLayout attached\n");
204 std::make_unique
<PseudoSourceValueManager
>(*(getSubtarget().
208 MachineFunction::~MachineFunction() {
212 void MachineFunction::clear() {
214 // Don't call destructors on MachineInstr and MachineOperand. All of their
215 // memory comes from the BumpPtrAllocator which is about to be purged.
217 // Do call MachineBasicBlock destructors, it contains std::vectors.
218 for (iterator I
= begin(), E
= end(); I
!= E
; I
= BasicBlocks
.erase(I
))
219 I
->Insts
.clearAndLeakNodesUnsafely();
220 MBBNumbering
.clear();
222 InstructionRecycler
.clear(Allocator
);
223 OperandRecycler
.clear(Allocator
);
224 BasicBlockRecycler
.clear(Allocator
);
225 CodeViewAnnotations
.clear();
226 VariableDbgInfos
.clear();
228 RegInfo
->~MachineRegisterInfo();
229 Allocator
.Deallocate(RegInfo
);
232 MFInfo
->~MachineFunctionInfo();
233 Allocator
.Deallocate(MFInfo
);
236 FrameInfo
->~MachineFrameInfo();
237 Allocator
.Deallocate(FrameInfo
);
239 ConstantPool
->~MachineConstantPool();
240 Allocator
.Deallocate(ConstantPool
);
243 JumpTableInfo
->~MachineJumpTableInfo();
244 Allocator
.Deallocate(JumpTableInfo
);
248 WinEHInfo
->~WinEHFuncInfo();
249 Allocator
.Deallocate(WinEHInfo
);
253 WasmEHInfo
->~WasmEHFuncInfo();
254 Allocator
.Deallocate(WasmEHInfo
);
258 const DataLayout
&MachineFunction::getDataLayout() const {
259 return F
.getParent()->getDataLayout();
262 /// Get the JumpTableInfo for this function.
263 /// If it does not already exist, allocate one.
264 MachineJumpTableInfo
*MachineFunction::
265 getOrCreateJumpTableInfo(unsigned EntryKind
) {
266 if (JumpTableInfo
) return JumpTableInfo
;
268 JumpTableInfo
= new (Allocator
)
269 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind
)EntryKind
);
270 return JumpTableInfo
;
273 /// Should we be emitting segmented stack stuff for the function
274 bool MachineFunction::shouldSplitStack() const {
275 return getFunction().hasFnAttribute("split-stack");
278 LLVM_NODISCARD
unsigned
279 MachineFunction::addFrameInst(const MCCFIInstruction
&Inst
) {
280 FrameInstructions
.push_back(Inst
);
281 return FrameInstructions
.size() - 1;
284 /// This discards all of the MachineBasicBlock numbers and recomputes them.
285 /// This guarantees that the MBB numbers are sequential, dense, and match the
286 /// ordering of the blocks within the function. If a specific MachineBasicBlock
287 /// is specified, only that block and those after it are renumbered.
288 void MachineFunction::RenumberBlocks(MachineBasicBlock
*MBB
) {
289 if (empty()) { MBBNumbering
.clear(); return; }
290 MachineFunction::iterator MBBI
, E
= end();
294 MBBI
= MBB
->getIterator();
296 // Figure out the block number this should have.
297 unsigned BlockNo
= 0;
299 BlockNo
= std::prev(MBBI
)->getNumber() + 1;
301 for (; MBBI
!= E
; ++MBBI
, ++BlockNo
) {
302 if (MBBI
->getNumber() != (int)BlockNo
) {
303 // Remove use of the old number.
304 if (MBBI
->getNumber() != -1) {
305 assert(MBBNumbering
[MBBI
->getNumber()] == &*MBBI
&&
306 "MBB number mismatch!");
307 MBBNumbering
[MBBI
->getNumber()] = nullptr;
310 // If BlockNo is already taken, set that block's number to -1.
311 if (MBBNumbering
[BlockNo
])
312 MBBNumbering
[BlockNo
]->setNumber(-1);
314 MBBNumbering
[BlockNo
] = &*MBBI
;
315 MBBI
->setNumber(BlockNo
);
319 // Okay, all the blocks are renumbered. If we have compactified the block
320 // numbering, shrink MBBNumbering now.
321 assert(BlockNo
<= MBBNumbering
.size() && "Mismatch!");
322 MBBNumbering
.resize(BlockNo
);
325 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
326 MachineInstr
*MachineFunction::CreateMachineInstr(const MCInstrDesc
&MCID
,
329 return new (InstructionRecycler
.Allocate
<MachineInstr
>(Allocator
))
330 MachineInstr(*this, MCID
, DL
, NoImp
);
333 /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
334 /// identical in all ways except the instruction has no parent, prev, or next.
336 MachineFunction::CloneMachineInstr(const MachineInstr
*Orig
) {
337 return new (InstructionRecycler
.Allocate
<MachineInstr
>(Allocator
))
338 MachineInstr(*this, *Orig
);
341 MachineInstr
&MachineFunction::CloneMachineInstrBundle(MachineBasicBlock
&MBB
,
342 MachineBasicBlock::iterator InsertBefore
, const MachineInstr
&Orig
) {
343 MachineInstr
*FirstClone
= nullptr;
344 MachineBasicBlock::const_instr_iterator I
= Orig
.getIterator();
346 MachineInstr
*Cloned
= CloneMachineInstr(&*I
);
347 MBB
.insert(InsertBefore
, Cloned
);
348 if (FirstClone
== nullptr) {
351 Cloned
->bundleWithPred();
354 if (!I
->isBundledWithSucc())
361 /// Delete the given MachineInstr.
363 /// This function also serves as the MachineInstr destructor - the real
364 /// ~MachineInstr() destructor must be empty.
366 MachineFunction::DeleteMachineInstr(MachineInstr
*MI
) {
367 // Verify that a call site info is at valid state. This assertion should
368 // be triggered during the implementation of support for the
369 // call site info of a new architecture. If the assertion is triggered,
370 // back trace will tell where to insert a call to updateCallSiteInfo().
371 assert((!MI
->isCall(MachineInstr::IgnoreBundle
) ||
372 CallSitesInfo
.find(MI
) == CallSitesInfo
.end()) &&
373 "Call site info was not updated!");
374 // Strip it for parts. The operand array and the MI object itself are
375 // independently recyclable.
377 deallocateOperandArray(MI
->CapOperands
, MI
->Operands
);
378 // Don't call ~MachineInstr() which must be trivial anyway because
379 // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
381 InstructionRecycler
.Deallocate(Allocator
, MI
);
384 /// Allocate a new MachineBasicBlock. Use this instead of
385 /// `new MachineBasicBlock'.
387 MachineFunction::CreateMachineBasicBlock(const BasicBlock
*bb
) {
388 return new (BasicBlockRecycler
.Allocate
<MachineBasicBlock
>(Allocator
))
389 MachineBasicBlock(*this, bb
);
392 /// Delete the given MachineBasicBlock.
394 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock
*MBB
) {
395 assert(MBB
->getParent() == this && "MBB parent mismatch!");
396 MBB
->~MachineBasicBlock();
397 BasicBlockRecycler
.Deallocate(Allocator
, MBB
);
400 MachineMemOperand
*MachineFunction::getMachineMemOperand(
401 MachinePointerInfo PtrInfo
, MachineMemOperand::Flags f
, uint64_t s
,
402 unsigned base_alignment
, const AAMDNodes
&AAInfo
, const MDNode
*Ranges
,
403 SyncScope::ID SSID
, AtomicOrdering Ordering
,
404 AtomicOrdering FailureOrdering
) {
405 return new (Allocator
)
406 MachineMemOperand(PtrInfo
, f
, s
, base_alignment
, AAInfo
, Ranges
,
407 SSID
, Ordering
, FailureOrdering
);
411 MachineFunction::getMachineMemOperand(const MachineMemOperand
*MMO
,
412 int64_t Offset
, uint64_t Size
) {
413 const MachinePointerInfo
&PtrInfo
= MMO
->getPointerInfo();
415 // If there is no pointer value, the offset isn't tracked so we need to adjust
416 // the base alignment.
417 unsigned Align
= PtrInfo
.V
.isNull()
418 ? MinAlign(MMO
->getBaseAlignment(), Offset
)
419 : MMO
->getBaseAlignment();
421 return new (Allocator
)
422 MachineMemOperand(PtrInfo
.getWithOffset(Offset
), MMO
->getFlags(), Size
,
423 Align
, AAMDNodes(), nullptr, MMO
->getSyncScopeID(),
424 MMO
->getOrdering(), MMO
->getFailureOrdering());
428 MachineFunction::getMachineMemOperand(const MachineMemOperand
*MMO
,
429 const AAMDNodes
&AAInfo
) {
430 MachinePointerInfo MPI
= MMO
->getValue() ?
431 MachinePointerInfo(MMO
->getValue(), MMO
->getOffset()) :
432 MachinePointerInfo(MMO
->getPseudoValue(), MMO
->getOffset());
434 return new (Allocator
)
435 MachineMemOperand(MPI
, MMO
->getFlags(), MMO
->getSize(),
436 MMO
->getBaseAlignment(), AAInfo
,
437 MMO
->getRanges(), MMO
->getSyncScopeID(),
438 MMO
->getOrdering(), MMO
->getFailureOrdering());
442 MachineFunction::getMachineMemOperand(const MachineMemOperand
*MMO
,
443 MachineMemOperand::Flags Flags
) {
444 return new (Allocator
) MachineMemOperand(
445 MMO
->getPointerInfo(), Flags
, MMO
->getSize(), MMO
->getBaseAlignment(),
446 MMO
->getAAInfo(), MMO
->getRanges(), MMO
->getSyncScopeID(),
447 MMO
->getOrdering(), MMO
->getFailureOrdering());
450 MachineInstr::ExtraInfo
*
451 MachineFunction::createMIExtraInfo(ArrayRef
<MachineMemOperand
*> MMOs
,
452 MCSymbol
*PreInstrSymbol
,
453 MCSymbol
*PostInstrSymbol
) {
454 return MachineInstr::ExtraInfo::create(Allocator
, MMOs
, PreInstrSymbol
,
458 const char *MachineFunction::createExternalSymbolName(StringRef Name
) {
459 char *Dest
= Allocator
.Allocate
<char>(Name
.size() + 1);
460 llvm::copy(Name
, Dest
);
461 Dest
[Name
.size()] = 0;
465 uint32_t *MachineFunction::allocateRegMask() {
466 unsigned NumRegs
= getSubtarget().getRegisterInfo()->getNumRegs();
467 unsigned Size
= MachineOperand::getRegMaskSize(NumRegs
);
468 uint32_t *Mask
= Allocator
.Allocate
<uint32_t>(Size
);
469 memset(Mask
, 0, Size
* sizeof(Mask
[0]));
473 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
474 LLVM_DUMP_METHOD
void MachineFunction::dump() const {
479 StringRef
MachineFunction::getName() const {
480 return getFunction().getName();
483 void MachineFunction::print(raw_ostream
&OS
, const SlotIndexes
*Indexes
) const {
484 OS
<< "# Machine code for function " << getName() << ": ";
485 getProperties().print(OS
);
488 // Print Frame Information
489 FrameInfo
->print(*this, OS
);
491 // Print JumpTable Information
493 JumpTableInfo
->print(OS
);
495 // Print Constant Pool
496 ConstantPool
->print(OS
);
498 const TargetRegisterInfo
*TRI
= getSubtarget().getRegisterInfo();
500 if (RegInfo
&& !RegInfo
->livein_empty()) {
501 OS
<< "Function Live Ins: ";
502 for (MachineRegisterInfo::livein_iterator
503 I
= RegInfo
->livein_begin(), E
= RegInfo
->livein_end(); I
!= E
; ++I
) {
504 OS
<< printReg(I
->first
, TRI
);
506 OS
<< " in " << printReg(I
->second
, TRI
);
507 if (std::next(I
) != E
)
513 ModuleSlotTracker
MST(getFunction().getParent());
514 MST
.incorporateFunction(getFunction());
515 for (const auto &BB
: *this) {
517 // If we print the whole function, print it at its most verbose level.
518 BB
.print(OS
, MST
, Indexes
, /*IsStandalone=*/true);
521 OS
<< "\n# End machine code for function " << getName() << ".\n\n";
527 struct DOTGraphTraits
<const MachineFunction
*> : public DefaultDOTGraphTraits
{
528 DOTGraphTraits(bool isSimple
= false) : DefaultDOTGraphTraits(isSimple
) {}
530 static std::string
getGraphName(const MachineFunction
*F
) {
531 return ("CFG for '" + F
->getName() + "' function").str();
534 std::string
getNodeLabel(const MachineBasicBlock
*Node
,
535 const MachineFunction
*Graph
) {
538 raw_string_ostream
OSS(OutStr
);
541 OSS
<< printMBBReference(*Node
);
542 if (const BasicBlock
*BB
= Node
->getBasicBlock())
543 OSS
<< ": " << BB
->getName();
548 if (OutStr
[0] == '\n') OutStr
.erase(OutStr
.begin());
550 // Process string output to make it nicer...
551 for (unsigned i
= 0; i
!= OutStr
.length(); ++i
)
552 if (OutStr
[i
] == '\n') { // Left justify
554 OutStr
.insert(OutStr
.begin()+i
+1, 'l');
560 } // end namespace llvm
562 void MachineFunction::viewCFG() const
565 ViewGraph(this, "mf" + getName());
567 errs() << "MachineFunction::viewCFG is only available in debug builds on "
568 << "systems with Graphviz or gv!\n";
572 void MachineFunction::viewCFGOnly() const
575 ViewGraph(this, "mf" + getName(), true);
577 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
578 << "systems with Graphviz or gv!\n";
582 /// Add the specified physical register as a live-in value and
583 /// create a corresponding virtual register for it.
584 unsigned MachineFunction::addLiveIn(unsigned PReg
,
585 const TargetRegisterClass
*RC
) {
586 MachineRegisterInfo
&MRI
= getRegInfo();
587 unsigned VReg
= MRI
.getLiveInVirtReg(PReg
);
589 const TargetRegisterClass
*VRegRC
= MRI
.getRegClass(VReg
);
591 // A physical register can be added several times.
592 // Between two calls, the register class of the related virtual register
593 // may have been constrained to match some operation constraints.
594 // In that case, check that the current register class includes the
595 // physical register and is a sub class of the specified RC.
596 assert((VRegRC
== RC
|| (VRegRC
->contains(PReg
) &&
597 RC
->hasSubClassEq(VRegRC
))) &&
598 "Register class mismatch!");
601 VReg
= MRI
.createVirtualRegister(RC
);
602 MRI
.addLiveIn(PReg
, VReg
);
606 /// Return the MCSymbol for the specified non-empty jump table.
607 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
608 /// normal 'L' label is returned.
609 MCSymbol
*MachineFunction::getJTISymbol(unsigned JTI
, MCContext
&Ctx
,
610 bool isLinkerPrivate
) const {
611 const DataLayout
&DL
= getDataLayout();
612 assert(JumpTableInfo
&& "No jump tables");
613 assert(JTI
< JumpTableInfo
->getJumpTables().size() && "Invalid JTI!");
615 StringRef Prefix
= isLinkerPrivate
? DL
.getLinkerPrivateGlobalPrefix()
616 : DL
.getPrivateGlobalPrefix();
617 SmallString
<60> Name
;
618 raw_svector_ostream(Name
)
619 << Prefix
<< "JTI" << getFunctionNumber() << '_' << JTI
;
620 return Ctx
.getOrCreateSymbol(Name
);
623 /// Return a function-local symbol to represent the PIC base.
624 MCSymbol
*MachineFunction::getPICBaseSymbol() const {
625 const DataLayout
&DL
= getDataLayout();
626 return Ctx
.getOrCreateSymbol(Twine(DL
.getPrivateGlobalPrefix()) +
627 Twine(getFunctionNumber()) + "$pb");
630 /// \name Exception Handling
634 MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock
*LandingPad
) {
635 unsigned N
= LandingPads
.size();
636 for (unsigned i
= 0; i
< N
; ++i
) {
637 LandingPadInfo
&LP
= LandingPads
[i
];
638 if (LP
.LandingPadBlock
== LandingPad
)
642 LandingPads
.push_back(LandingPadInfo(LandingPad
));
643 return LandingPads
[N
];
646 void MachineFunction::addInvoke(MachineBasicBlock
*LandingPad
,
647 MCSymbol
*BeginLabel
, MCSymbol
*EndLabel
) {
648 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
649 LP
.BeginLabels
.push_back(BeginLabel
);
650 LP
.EndLabels
.push_back(EndLabel
);
653 MCSymbol
*MachineFunction::addLandingPad(MachineBasicBlock
*LandingPad
) {
654 MCSymbol
*LandingPadLabel
= Ctx
.createTempSymbol();
655 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
656 LP
.LandingPadLabel
= LandingPadLabel
;
658 const Instruction
*FirstI
= LandingPad
->getBasicBlock()->getFirstNonPHI();
659 if (const auto *LPI
= dyn_cast
<LandingPadInst
>(FirstI
)) {
661 dyn_cast
<Function
>(F
.getPersonalityFn()->stripPointerCasts()))
662 getMMI().addPersonality(PF
);
664 if (LPI
->isCleanup())
665 addCleanup(LandingPad
);
667 // FIXME: New EH - Add the clauses in reverse order. This isn't 100%
668 // correct, but we need to do it this way because of how the DWARF EH
669 // emitter processes the clauses.
670 for (unsigned I
= LPI
->getNumClauses(); I
!= 0; --I
) {
671 Value
*Val
= LPI
->getClause(I
- 1);
672 if (LPI
->isCatch(I
- 1)) {
673 addCatchTypeInfo(LandingPad
,
674 dyn_cast
<GlobalValue
>(Val
->stripPointerCasts()));
676 // Add filters in a list.
677 auto *CVal
= cast
<Constant
>(Val
);
678 SmallVector
<const GlobalValue
*, 4> FilterList
;
679 for (User::op_iterator II
= CVal
->op_begin(), IE
= CVal
->op_end();
681 FilterList
.push_back(cast
<GlobalValue
>((*II
)->stripPointerCasts()));
683 addFilterTypeInfo(LandingPad
, FilterList
);
687 } else if (const auto *CPI
= dyn_cast
<CatchPadInst
>(FirstI
)) {
688 for (unsigned I
= CPI
->getNumArgOperands(); I
!= 0; --I
) {
689 Value
*TypeInfo
= CPI
->getArgOperand(I
- 1)->stripPointerCasts();
690 addCatchTypeInfo(LandingPad
, dyn_cast
<GlobalValue
>(TypeInfo
));
694 assert(isa
<CleanupPadInst
>(FirstI
) && "Invalid landingpad!");
697 return LandingPadLabel
;
700 void MachineFunction::addCatchTypeInfo(MachineBasicBlock
*LandingPad
,
701 ArrayRef
<const GlobalValue
*> TyInfo
) {
702 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
703 for (unsigned N
= TyInfo
.size(); N
; --N
)
704 LP
.TypeIds
.push_back(getTypeIDFor(TyInfo
[N
- 1]));
707 void MachineFunction::addFilterTypeInfo(MachineBasicBlock
*LandingPad
,
708 ArrayRef
<const GlobalValue
*> TyInfo
) {
709 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
710 std::vector
<unsigned> IdsInFilter(TyInfo
.size());
711 for (unsigned I
= 0, E
= TyInfo
.size(); I
!= E
; ++I
)
712 IdsInFilter
[I
] = getTypeIDFor(TyInfo
[I
]);
713 LP
.TypeIds
.push_back(getFilterIDFor(IdsInFilter
));
716 void MachineFunction::tidyLandingPads(DenseMap
<MCSymbol
*, uintptr_t> *LPMap
,
717 bool TidyIfNoBeginLabels
) {
718 for (unsigned i
= 0; i
!= LandingPads
.size(); ) {
719 LandingPadInfo
&LandingPad
= LandingPads
[i
];
720 if (LandingPad
.LandingPadLabel
&&
721 !LandingPad
.LandingPadLabel
->isDefined() &&
722 (!LPMap
|| (*LPMap
)[LandingPad
.LandingPadLabel
] == 0))
723 LandingPad
.LandingPadLabel
= nullptr;
725 // Special case: we *should* emit LPs with null LP MBB. This indicates
727 if (!LandingPad
.LandingPadLabel
&& LandingPad
.LandingPadBlock
) {
728 LandingPads
.erase(LandingPads
.begin() + i
);
732 if (TidyIfNoBeginLabels
) {
733 for (unsigned j
= 0, e
= LandingPads
[i
].BeginLabels
.size(); j
!= e
; ++j
) {
734 MCSymbol
*BeginLabel
= LandingPad
.BeginLabels
[j
];
735 MCSymbol
*EndLabel
= LandingPad
.EndLabels
[j
];
736 if ((BeginLabel
->isDefined() || (LPMap
&& (*LPMap
)[BeginLabel
] != 0)) &&
737 (EndLabel
->isDefined() || (LPMap
&& (*LPMap
)[EndLabel
] != 0)))
740 LandingPad
.BeginLabels
.erase(LandingPad
.BeginLabels
.begin() + j
);
741 LandingPad
.EndLabels
.erase(LandingPad
.EndLabels
.begin() + j
);
746 // Remove landing pads with no try-ranges.
747 if (LandingPads
[i
].BeginLabels
.empty()) {
748 LandingPads
.erase(LandingPads
.begin() + i
);
753 // If there is no landing pad, ensure that the list of typeids is empty.
754 // If the only typeid is a cleanup, this is the same as having no typeids.
755 if (!LandingPad
.LandingPadBlock
||
756 (LandingPad
.TypeIds
.size() == 1 && !LandingPad
.TypeIds
[0]))
757 LandingPad
.TypeIds
.clear();
762 void MachineFunction::addCleanup(MachineBasicBlock
*LandingPad
) {
763 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
764 LP
.TypeIds
.push_back(0);
767 void MachineFunction::addSEHCatchHandler(MachineBasicBlock
*LandingPad
,
768 const Function
*Filter
,
769 const BlockAddress
*RecoverBA
) {
770 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
772 Handler
.FilterOrFinally
= Filter
;
773 Handler
.RecoverBA
= RecoverBA
;
774 LP
.SEHHandlers
.push_back(Handler
);
777 void MachineFunction::addSEHCleanupHandler(MachineBasicBlock
*LandingPad
,
778 const Function
*Cleanup
) {
779 LandingPadInfo
&LP
= getOrCreateLandingPadInfo(LandingPad
);
781 Handler
.FilterOrFinally
= Cleanup
;
782 Handler
.RecoverBA
= nullptr;
783 LP
.SEHHandlers
.push_back(Handler
);
786 void MachineFunction::setCallSiteLandingPad(MCSymbol
*Sym
,
787 ArrayRef
<unsigned> Sites
) {
788 LPadToCallSiteMap
[Sym
].append(Sites
.begin(), Sites
.end());
791 unsigned MachineFunction::getTypeIDFor(const GlobalValue
*TI
) {
792 for (unsigned i
= 0, N
= TypeInfos
.size(); i
!= N
; ++i
)
793 if (TypeInfos
[i
] == TI
) return i
+ 1;
795 TypeInfos
.push_back(TI
);
796 return TypeInfos
.size();
799 int MachineFunction::getFilterIDFor(std::vector
<unsigned> &TyIds
) {
800 // If the new filter coincides with the tail of an existing filter, then
801 // re-use the existing filter. Folding filters more than this requires
802 // re-ordering filters and/or their elements - probably not worth it.
803 for (std::vector
<unsigned>::iterator I
= FilterEnds
.begin(),
804 E
= FilterEnds
.end(); I
!= E
; ++I
) {
805 unsigned i
= *I
, j
= TyIds
.size();
808 if (FilterIds
[--i
] != TyIds
[--j
])
812 // The new filter coincides with range [i, end) of the existing filter.
818 // Add the new filter.
819 int FilterID
= -(1 + FilterIds
.size());
820 FilterIds
.reserve(FilterIds
.size() + TyIds
.size() + 1);
821 FilterIds
.insert(FilterIds
.end(), TyIds
.begin(), TyIds
.end());
822 FilterEnds
.push_back(FilterIds
.size());
823 FilterIds
.push_back(0); // terminator
827 void MachineFunction::addCodeViewHeapAllocSite(MachineInstr
*I
,
829 MCSymbol
*BeginLabel
= Ctx
.createTempSymbol("heapallocsite", true);
830 MCSymbol
*EndLabel
= Ctx
.createTempSymbol("heapallocsite", true);
831 I
->setPreInstrSymbol(*this, BeginLabel
);
832 I
->setPostInstrSymbol(*this, EndLabel
);
834 const DIType
*DI
= dyn_cast
<DIType
>(MD
);
835 CodeViewHeapAllocSites
.push_back(std::make_tuple(BeginLabel
, EndLabel
, DI
));
838 void MachineFunction::updateCallSiteInfo(const MachineInstr
*Old
,
839 const MachineInstr
*New
) {
840 if (!Target
.Options
.EnableDebugEntryValues
|| Old
== New
)
843 assert(Old
->isCall() && (!New
|| New
->isCall()) &&
844 "Call site info referes only to call instructions!");
845 CallSiteInfoMap::iterator CSIt
= CallSitesInfo
.find(Old
);
846 if (CSIt
== CallSitesInfo
.end())
848 CallSiteInfo CSInfo
= std::move(CSIt
->second
);
849 CallSitesInfo
.erase(CSIt
);
851 CallSitesInfo
[New
] = CSInfo
;
856 //===----------------------------------------------------------------------===//
857 // MachineJumpTableInfo implementation
858 //===----------------------------------------------------------------------===//
860 /// Return the size of each entry in the jump table.
861 unsigned MachineJumpTableInfo::getEntrySize(const DataLayout
&TD
) const {
862 // The size of a jump table entry is 4 bytes unless the entry is just the
863 // address of a block, in which case it is the pointer size.
864 switch (getEntryKind()) {
865 case MachineJumpTableInfo::EK_BlockAddress
:
866 return TD
.getPointerSize();
867 case MachineJumpTableInfo::EK_GPRel64BlockAddress
:
869 case MachineJumpTableInfo::EK_GPRel32BlockAddress
:
870 case MachineJumpTableInfo::EK_LabelDifference32
:
871 case MachineJumpTableInfo::EK_Custom32
:
873 case MachineJumpTableInfo::EK_Inline
:
876 llvm_unreachable("Unknown jump table encoding!");
879 /// Return the alignment of each entry in the jump table.
880 unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout
&TD
) const {
881 // The alignment of a jump table entry is the alignment of int32 unless the
882 // entry is just the address of a block, in which case it is the pointer
884 switch (getEntryKind()) {
885 case MachineJumpTableInfo::EK_BlockAddress
:
886 return TD
.getPointerABIAlignment(0).value();
887 case MachineJumpTableInfo::EK_GPRel64BlockAddress
:
888 return TD
.getABIIntegerTypeAlignment(64).value();
889 case MachineJumpTableInfo::EK_GPRel32BlockAddress
:
890 case MachineJumpTableInfo::EK_LabelDifference32
:
891 case MachineJumpTableInfo::EK_Custom32
:
892 return TD
.getABIIntegerTypeAlignment(32).value();
893 case MachineJumpTableInfo::EK_Inline
:
896 llvm_unreachable("Unknown jump table encoding!");
899 /// Create a new jump table entry in the jump table info.
900 unsigned MachineJumpTableInfo::createJumpTableIndex(
901 const std::vector
<MachineBasicBlock
*> &DestBBs
) {
902 assert(!DestBBs
.empty() && "Cannot create an empty jump table!");
903 JumpTables
.push_back(MachineJumpTableEntry(DestBBs
));
904 return JumpTables
.size()-1;
907 /// If Old is the target of any jump tables, update the jump tables to branch
909 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock
*Old
,
910 MachineBasicBlock
*New
) {
911 assert(Old
!= New
&& "Not making a change?");
912 bool MadeChange
= false;
913 for (size_t i
= 0, e
= JumpTables
.size(); i
!= e
; ++i
)
914 ReplaceMBBInJumpTable(i
, Old
, New
);
918 /// If Old is a target of the jump tables, update the jump table to branch to
920 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx
,
921 MachineBasicBlock
*Old
,
922 MachineBasicBlock
*New
) {
923 assert(Old
!= New
&& "Not making a change?");
924 bool MadeChange
= false;
925 MachineJumpTableEntry
&JTE
= JumpTables
[Idx
];
926 for (size_t j
= 0, e
= JTE
.MBBs
.size(); j
!= e
; ++j
)
927 if (JTE
.MBBs
[j
] == Old
) {
934 void MachineJumpTableInfo::print(raw_ostream
&OS
) const {
935 if (JumpTables
.empty()) return;
937 OS
<< "Jump Tables:\n";
939 for (unsigned i
= 0, e
= JumpTables
.size(); i
!= e
; ++i
) {
940 OS
<< printJumpTableEntryReference(i
) << ':';
941 for (unsigned j
= 0, f
= JumpTables
[i
].MBBs
.size(); j
!= f
; ++j
)
942 OS
<< ' ' << printMBBReference(*JumpTables
[i
].MBBs
[j
]);
950 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
951 LLVM_DUMP_METHOD
void MachineJumpTableInfo::dump() const { print(dbgs()); }
954 Printable
llvm::printJumpTableEntryReference(unsigned Idx
) {
955 return Printable([Idx
](raw_ostream
&OS
) { OS
<< "%jump-table." << Idx
; });
958 //===----------------------------------------------------------------------===//
959 // MachineConstantPool implementation
960 //===----------------------------------------------------------------------===//
962 void MachineConstantPoolValue::anchor() {}
964 Type
*MachineConstantPoolEntry::getType() const {
965 if (isMachineConstantPoolEntry())
966 return Val
.MachineCPVal
->getType();
967 return Val
.ConstVal
->getType();
970 bool MachineConstantPoolEntry::needsRelocation() const {
971 if (isMachineConstantPoolEntry())
973 return Val
.ConstVal
->needsRelocation();
977 MachineConstantPoolEntry::getSectionKind(const DataLayout
*DL
) const {
978 if (needsRelocation())
979 return SectionKind::getReadOnlyWithRel();
980 switch (DL
->getTypeAllocSize(getType())) {
982 return SectionKind::getMergeableConst4();
984 return SectionKind::getMergeableConst8();
986 return SectionKind::getMergeableConst16();
988 return SectionKind::getMergeableConst32();
990 return SectionKind::getReadOnly();
994 MachineConstantPool::~MachineConstantPool() {
995 // A constant may be a member of both Constants and MachineCPVsSharingEntries,
996 // so keep track of which we've deleted to avoid double deletions.
997 DenseSet
<MachineConstantPoolValue
*> Deleted
;
998 for (unsigned i
= 0, e
= Constants
.size(); i
!= e
; ++i
)
999 if (Constants
[i
].isMachineConstantPoolEntry()) {
1000 Deleted
.insert(Constants
[i
].Val
.MachineCPVal
);
1001 delete Constants
[i
].Val
.MachineCPVal
;
1003 for (DenseSet
<MachineConstantPoolValue
*>::iterator I
=
1004 MachineCPVsSharingEntries
.begin(), E
= MachineCPVsSharingEntries
.end();
1006 if (Deleted
.count(*I
) == 0)
1011 /// Test whether the given two constants can be allocated the same constant pool
1013 static bool CanShareConstantPoolEntry(const Constant
*A
, const Constant
*B
,
1014 const DataLayout
&DL
) {
1015 // Handle the trivial case quickly.
1016 if (A
== B
) return true;
1018 // If they have the same type but weren't the same constant, quickly
1020 if (A
->getType() == B
->getType()) return false;
1022 // We can't handle structs or arrays.
1023 if (isa
<StructType
>(A
->getType()) || isa
<ArrayType
>(A
->getType()) ||
1024 isa
<StructType
>(B
->getType()) || isa
<ArrayType
>(B
->getType()))
1027 // For now, only support constants with the same size.
1028 uint64_t StoreSize
= DL
.getTypeStoreSize(A
->getType());
1029 if (StoreSize
!= DL
.getTypeStoreSize(B
->getType()) || StoreSize
> 128)
1032 Type
*IntTy
= IntegerType::get(A
->getContext(), StoreSize
*8);
1034 // Try constant folding a bitcast of both instructions to an integer. If we
1035 // get two identical ConstantInt's, then we are good to share them. We use
1036 // the constant folding APIs to do this so that we get the benefit of
1038 if (isa
<PointerType
>(A
->getType()))
1039 A
= ConstantFoldCastOperand(Instruction::PtrToInt
,
1040 const_cast<Constant
*>(A
), IntTy
, DL
);
1041 else if (A
->getType() != IntTy
)
1042 A
= ConstantFoldCastOperand(Instruction::BitCast
, const_cast<Constant
*>(A
),
1044 if (isa
<PointerType
>(B
->getType()))
1045 B
= ConstantFoldCastOperand(Instruction::PtrToInt
,
1046 const_cast<Constant
*>(B
), IntTy
, DL
);
1047 else if (B
->getType() != IntTy
)
1048 B
= ConstantFoldCastOperand(Instruction::BitCast
, const_cast<Constant
*>(B
),
1054 /// Create a new entry in the constant pool or return an existing one.
1055 /// User must specify the log2 of the minimum required alignment for the object.
1056 unsigned MachineConstantPool::getConstantPoolIndex(const Constant
*C
,
1057 unsigned Alignment
) {
1058 assert(Alignment
&& "Alignment must be specified!");
1059 if (Alignment
> PoolAlignment
) PoolAlignment
= Alignment
;
1061 // Check to see if we already have this constant.
1063 // FIXME, this could be made much more efficient for large constant pools.
1064 for (unsigned i
= 0, e
= Constants
.size(); i
!= e
; ++i
)
1065 if (!Constants
[i
].isMachineConstantPoolEntry() &&
1066 CanShareConstantPoolEntry(Constants
[i
].Val
.ConstVal
, C
, DL
)) {
1067 if ((unsigned)Constants
[i
].getAlignment() < Alignment
)
1068 Constants
[i
].Alignment
= Alignment
;
1072 Constants
.push_back(MachineConstantPoolEntry(C
, Alignment
));
1073 return Constants
.size()-1;
1076 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue
*V
,
1077 unsigned Alignment
) {
1078 assert(Alignment
&& "Alignment must be specified!");
1079 if (Alignment
> PoolAlignment
) PoolAlignment
= Alignment
;
1081 // Check to see if we already have this constant.
1083 // FIXME, this could be made much more efficient for large constant pools.
1084 int Idx
= V
->getExistingMachineCPValue(this, Alignment
);
1086 MachineCPVsSharingEntries
.insert(V
);
1087 return (unsigned)Idx
;
1090 Constants
.push_back(MachineConstantPoolEntry(V
, Alignment
));
1091 return Constants
.size()-1;
1094 void MachineConstantPool::print(raw_ostream
&OS
) const {
1095 if (Constants
.empty()) return;
1097 OS
<< "Constant Pool:\n";
1098 for (unsigned i
= 0, e
= Constants
.size(); i
!= e
; ++i
) {
1099 OS
<< " cp#" << i
<< ": ";
1100 if (Constants
[i
].isMachineConstantPoolEntry())
1101 Constants
[i
].Val
.MachineCPVal
->print(OS
);
1103 Constants
[i
].Val
.ConstVal
->printAsOperand(OS
, /*PrintType=*/false);
1104 OS
<< ", align=" << Constants
[i
].getAlignment();
1109 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1110 LLVM_DUMP_METHOD
void MachineConstantPool::dump() const { print(dbgs()); }