1 //===- MachineFunction.cpp ------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Collect native machine code information for a function. This allows
11 // target-specific information about the generated code to be stored with each
12 // function.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/Twine.h"
25 #include "llvm/Analysis/ConstantFolding.h"
26 #include "llvm/Analysis/EHPersonalities.h"
27 #include "llvm/CodeGen/MachineBasicBlock.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineFrameInfo.h"
30 #include "llvm/CodeGen/MachineInstr.h"
31 #include "llvm/CodeGen/MachineJumpTableInfo.h"
32 #include "llvm/CodeGen/MachineMemOperand.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/PseudoSourceValue.h"
36 #include "llvm/CodeGen/TargetFrameLowering.h"
37 #include "llvm/CodeGen/TargetLowering.h"
38 #include "llvm/CodeGen/TargetRegisterInfo.h"
39 #include "llvm/CodeGen/TargetSubtargetInfo.h"
40 #include "llvm/CodeGen/WasmEHFuncInfo.h"
41 #include "llvm/CodeGen/WinEHFuncInfo.h"
42 #include "llvm/Config/llvm-config.h"
43 #include "llvm/IR/Attributes.h"
44 #include "llvm/IR/BasicBlock.h"
45 #include "llvm/IR/Constant.h"
46 #include "llvm/IR/DataLayout.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"
68 #include <algorithm>
69 #include <cassert>
70 #include <cstddef>
71 #include <cstdint>
72 #include <iterator>
73 #include <string>
74 #include <utility>
75 #include <vector>
76
77 using namespace llvm;
78
79 #define DEBUG_TYPE "codegen"
80
81 static cl::opt<unsigned>
82 AlignAllFunctions("align-all-functions",
83 cl::desc("Force the alignment of all functions."),
84 cl::init(0), cl::Hidden);
85
getPropertyName(MachineFunctionProperties::Property Prop)86 static const char *getPropertyName(MachineFunctionProperties::Property Prop) {
87 using P = MachineFunctionProperties::Property;
88
89 switch(Prop) {
90 case P::FailedISel: return "FailedISel";
91 case P::IsSSA: return "IsSSA";
92 case P::Legalized: return "Legalized";
93 case P::NoPHIs: return "NoPHIs";
94 case P::NoVRegs: return "NoVRegs";
95 case P::RegBankSelected: return "RegBankSelected";
96 case P::Selected: return "Selected";
97 case P::TracksLiveness: return "TracksLiveness";
98 }
99 llvm_unreachable("Invalid machine function property");
100 }
101
102 // Pin the vtable to this file.
anchor()103 void MachineFunction::Delegate::anchor() {}
104
print(raw_ostream & OS) const105 void MachineFunctionProperties::print(raw_ostream &OS) const {
106 const char *Separator = "";
107 for (BitVector::size_type I = 0; I < Properties.size(); ++I) {
108 if (!Properties[I])
109 continue;
110 OS << Separator << getPropertyName(static_cast<Property>(I));
111 Separator = ", ";
112 }
113 }
114
115 //===----------------------------------------------------------------------===//
116 // MachineFunction implementation
117 //===----------------------------------------------------------------------===//
118
119 // Out-of-line virtual method.
120 MachineFunctionInfo::~MachineFunctionInfo() = default;
121
deleteNode(MachineBasicBlock * MBB)122 void ilist_alloc_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
123 MBB->getParent()->DeleteMachineBasicBlock(MBB);
124 }
125
getFnStackAlignment(const TargetSubtargetInfo * STI,const Function & F)126 static inline unsigned getFnStackAlignment(const TargetSubtargetInfo *STI,
127 const Function &F) {
128 if (F.hasFnAttribute(Attribute::StackAlignment))
129 return F.getFnStackAlignment();
130 return STI->getFrameLowering()->getStackAlignment();
131 }
132
MachineFunction(const Function & F,const LLVMTargetMachine & Target,const TargetSubtargetInfo & STI,unsigned FunctionNum,MachineModuleInfo & mmi)133 MachineFunction::MachineFunction(const Function &F,
134 const LLVMTargetMachine &Target,
135 const TargetSubtargetInfo &STI,
136 unsigned FunctionNum, MachineModuleInfo &mmi)
137 : F(F), Target(Target), STI(&STI), Ctx(mmi.getContext()), MMI(mmi) {
138 FunctionNumber = FunctionNum;
139 init();
140 }
141
handleInsertion(MachineInstr & MI)142 void MachineFunction::handleInsertion(MachineInstr &MI) {
143 if (TheDelegate)
144 TheDelegate->MF_HandleInsertion(MI);
145 }
146
handleRemoval(MachineInstr & MI)147 void MachineFunction::handleRemoval(MachineInstr &MI) {
148 if (TheDelegate)
149 TheDelegate->MF_HandleRemoval(MI);
150 }
151
init()152 void MachineFunction::init() {
153 // Assume the function starts in SSA form with correct liveness.
154 Properties.set(MachineFunctionProperties::Property::IsSSA);
155 Properties.set(MachineFunctionProperties::Property::TracksLiveness);
156 if (STI->getRegisterInfo())
157 RegInfo = new (Allocator) MachineRegisterInfo(this);
158 else
159 RegInfo = nullptr;
160
161 MFInfo = nullptr;
162 // We can realign the stack if the target supports it and the user hasn't
163 // explicitly asked us not to.
164 bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() &&
165 !F.hasFnAttribute("no-realign-stack");
166 FrameInfo = new (Allocator) MachineFrameInfo(
167 getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP,
168 /*ForceRealign=*/CanRealignSP &&
169 F.hasFnAttribute(Attribute::StackAlignment));
170
171 if (F.hasFnAttribute(Attribute::StackAlignment))
172 FrameInfo->ensureMaxAlignment(F.getFnStackAlignment());
173
174 ConstantPool = new (Allocator) MachineConstantPool(getDataLayout());
175 Alignment = STI->getTargetLowering()->getMinFunctionAlignment();
176
177 // FIXME: Shouldn't use pref alignment if explicit alignment is set on F.
178 // FIXME: Use Function::optForSize().
179 if (!F.hasFnAttribute(Attribute::OptimizeForSize))
180 Alignment = std::max(Alignment,
181 STI->getTargetLowering()->getPrefFunctionAlignment());
182
183 if (AlignAllFunctions)
184 Alignment = AlignAllFunctions;
185
186 JumpTableInfo = nullptr;
187
188 if (isFuncletEHPersonality(classifyEHPersonality(
189 F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
190 WinEHInfo = new (Allocator) WinEHFuncInfo();
191 }
192
193 if (isScopedEHPersonality(classifyEHPersonality(
194 F.hasPersonalityFn() ? F.getPersonalityFn() : nullptr))) {
195 WasmEHInfo = new (Allocator) WasmEHFuncInfo();
196 }
197
198 assert(Target.isCompatibleDataLayout(getDataLayout()) &&
199 "Can't create a MachineFunction using a Module with a "
200 "Target-incompatible DataLayout attached\n");
201
202 PSVManager =
203 llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget().
204 getInstrInfo()));
205 }
206
~MachineFunction()207 MachineFunction::~MachineFunction() {
208 clear();
209 }
210
clear()211 void MachineFunction::clear() {
212 Properties.reset();
213 // Don't call destructors on MachineInstr and MachineOperand. All of their
214 // memory comes from the BumpPtrAllocator which is about to be purged.
215 //
216 // Do call MachineBasicBlock destructors, it contains std::vectors.
217 for (iterator I = begin(), E = end(); I != E; I = BasicBlocks.erase(I))
218 I->Insts.clearAndLeakNodesUnsafely();
219 MBBNumbering.clear();
220
221 InstructionRecycler.clear(Allocator);
222 OperandRecycler.clear(Allocator);
223 BasicBlockRecycler.clear(Allocator);
224 CodeViewAnnotations.clear();
225 VariableDbgInfos.clear();
226 if (RegInfo) {
227 RegInfo->~MachineRegisterInfo();
228 Allocator.Deallocate(RegInfo);
229 }
230 if (MFInfo) {
231 MFInfo->~MachineFunctionInfo();
232 Allocator.Deallocate(MFInfo);
233 }
234
235 FrameInfo->~MachineFrameInfo();
236 Allocator.Deallocate(FrameInfo);
237
238 ConstantPool->~MachineConstantPool();
239 Allocator.Deallocate(ConstantPool);
240
241 if (JumpTableInfo) {
242 JumpTableInfo->~MachineJumpTableInfo();
243 Allocator.Deallocate(JumpTableInfo);
244 }
245
246 if (WinEHInfo) {
247 WinEHInfo->~WinEHFuncInfo();
248 Allocator.Deallocate(WinEHInfo);
249 }
250
251 if (WasmEHInfo) {
252 WasmEHInfo->~WasmEHFuncInfo();
253 Allocator.Deallocate(WasmEHInfo);
254 }
255 }
256
getDataLayout() const257 const DataLayout &MachineFunction::getDataLayout() const {
258 return F.getParent()->getDataLayout();
259 }
260
261 /// Get the JumpTableInfo for this function.
262 /// If it does not already exist, allocate one.
263 MachineJumpTableInfo *MachineFunction::
getOrCreateJumpTableInfo(unsigned EntryKind)264 getOrCreateJumpTableInfo(unsigned EntryKind) {
265 if (JumpTableInfo) return JumpTableInfo;
266
267 JumpTableInfo = new (Allocator)
268 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
269 return JumpTableInfo;
270 }
271
272 /// Should we be emitting segmented stack stuff for the function
shouldSplitStack() const273 bool MachineFunction::shouldSplitStack() const {
274 return getFunction().hasFnAttribute("split-stack");
275 }
276
277 /// This discards all of the MachineBasicBlock numbers and recomputes them.
278 /// This guarantees that the MBB numbers are sequential, dense, and match the
279 /// ordering of the blocks within the function. If a specific MachineBasicBlock
280 /// is specified, only that block and those after it are renumbered.
RenumberBlocks(MachineBasicBlock * MBB)281 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
282 if (empty()) { MBBNumbering.clear(); return; }
283 MachineFunction::iterator MBBI, E = end();
284 if (MBB == nullptr)
285 MBBI = begin();
286 else
287 MBBI = MBB->getIterator();
288
289 // Figure out the block number this should have.
290 unsigned BlockNo = 0;
291 if (MBBI != begin())
292 BlockNo = std::prev(MBBI)->getNumber() + 1;
293
294 for (; MBBI != E; ++MBBI, ++BlockNo) {
295 if (MBBI->getNumber() != (int)BlockNo) {
296 // Remove use of the old number.
297 if (MBBI->getNumber() != -1) {
298 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
299 "MBB number mismatch!");
300 MBBNumbering[MBBI->getNumber()] = nullptr;
301 }
302
303 // If BlockNo is already taken, set that block's number to -1.
304 if (MBBNumbering[BlockNo])
305 MBBNumbering[BlockNo]->setNumber(-1);
306
307 MBBNumbering[BlockNo] = &*MBBI;
308 MBBI->setNumber(BlockNo);
309 }
310 }
311
312 // Okay, all the blocks are renumbered. If we have compactified the block
313 // numbering, shrink MBBNumbering now.
314 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
315 MBBNumbering.resize(BlockNo);
316 }
317
318 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
CreateMachineInstr(const MCInstrDesc & MCID,const DebugLoc & DL,bool NoImp)319 MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
320 const DebugLoc &DL,
321 bool NoImp) {
322 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
323 MachineInstr(*this, MCID, DL, NoImp);
324 }
325
326 /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
327 /// identical in all ways except the instruction has no parent, prev, or next.
328 MachineInstr *
CloneMachineInstr(const MachineInstr * Orig)329 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
330 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
331 MachineInstr(*this, *Orig);
332 }
333
CloneMachineInstrBundle(MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,const MachineInstr & Orig)334 MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
335 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
336 MachineInstr *FirstClone = nullptr;
337 MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
338 while (true) {
339 MachineInstr *Cloned = CloneMachineInstr(&*I);
340 MBB.insert(InsertBefore, Cloned);
341 if (FirstClone == nullptr) {
342 FirstClone = Cloned;
343 } else {
344 Cloned->bundleWithPred();
345 }
346
347 if (!I->isBundledWithSucc())
348 break;
349 ++I;
350 }
351 return *FirstClone;
352 }
353
354 /// Delete the given MachineInstr.
355 ///
356 /// This function also serves as the MachineInstr destructor - the real
357 /// ~MachineInstr() destructor must be empty.
358 void
DeleteMachineInstr(MachineInstr * MI)359 MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
360 // Strip it for parts. The operand array and the MI object itself are
361 // independently recyclable.
362 if (MI->Operands)
363 deallocateOperandArray(MI->CapOperands, MI->Operands);
364 // Don't call ~MachineInstr() which must be trivial anyway because
365 // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
366 // destructors.
367 InstructionRecycler.Deallocate(Allocator, MI);
368 }
369
370 /// Allocate a new MachineBasicBlock. Use this instead of
371 /// `new MachineBasicBlock'.
372 MachineBasicBlock *
CreateMachineBasicBlock(const BasicBlock * bb)373 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
374 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
375 MachineBasicBlock(*this, bb);
376 }
377
378 /// Delete the given MachineBasicBlock.
379 void
DeleteMachineBasicBlock(MachineBasicBlock * MBB)380 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
381 assert(MBB->getParent() == this && "MBB parent mismatch!");
382 MBB->~MachineBasicBlock();
383 BasicBlockRecycler.Deallocate(Allocator, MBB);
384 }
385
getMachineMemOperand(MachinePointerInfo PtrInfo,MachineMemOperand::Flags f,uint64_t s,unsigned base_alignment,const AAMDNodes & AAInfo,const MDNode * Ranges,SyncScope::ID SSID,AtomicOrdering Ordering,AtomicOrdering FailureOrdering)386 MachineMemOperand *MachineFunction::getMachineMemOperand(
387 MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
388 unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
389 SyncScope::ID SSID, AtomicOrdering Ordering,
390 AtomicOrdering FailureOrdering) {
391 return new (Allocator)
392 MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
393 SSID, Ordering, FailureOrdering);
394 }
395
396 MachineMemOperand *
getMachineMemOperand(const MachineMemOperand * MMO,int64_t Offset,uint64_t Size)397 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
398 int64_t Offset, uint64_t Size) {
399 if (MMO->getValue())
400 return new (Allocator)
401 MachineMemOperand(MachinePointerInfo(MMO->getValue(),
402 MMO->getOffset()+Offset),
403 MMO->getFlags(), Size, MMO->getBaseAlignment(),
404 AAMDNodes(), nullptr, MMO->getSyncScopeID(),
405 MMO->getOrdering(), MMO->getFailureOrdering());
406 return new (Allocator)
407 MachineMemOperand(MachinePointerInfo(MMO->getPseudoValue(),
408 MMO->getOffset()+Offset),
409 MMO->getFlags(), Size, MMO->getBaseAlignment(),
410 AAMDNodes(), nullptr, MMO->getSyncScopeID(),
411 MMO->getOrdering(), MMO->getFailureOrdering());
412 }
413
414 MachineMemOperand *
getMachineMemOperand(const MachineMemOperand * MMO,const AAMDNodes & AAInfo)415 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
416 const AAMDNodes &AAInfo) {
417 MachinePointerInfo MPI = MMO->getValue() ?
418 MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
419 MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
420
421 return new (Allocator)
422 MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
423 MMO->getBaseAlignment(), AAInfo,
424 MMO->getRanges(), MMO->getSyncScopeID(),
425 MMO->getOrdering(), MMO->getFailureOrdering());
426 }
427
428 MachineInstr::ExtraInfo *
createMIExtraInfo(ArrayRef<MachineMemOperand * > MMOs,MCSymbol * PreInstrSymbol,MCSymbol * PostInstrSymbol)429 MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
430 MCSymbol *PreInstrSymbol,
431 MCSymbol *PostInstrSymbol) {
432 return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
433 PostInstrSymbol);
434 }
435
createExternalSymbolName(StringRef Name)436 const char *MachineFunction::createExternalSymbolName(StringRef Name) {
437 char *Dest = Allocator.Allocate<char>(Name.size() + 1);
438 llvm::copy(Name, Dest);
439 Dest[Name.size()] = 0;
440 return Dest;
441 }
442
allocateRegMask()443 uint32_t *MachineFunction::allocateRegMask() {
444 unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs();
445 unsigned Size = MachineOperand::getRegMaskSize(NumRegs);
446 uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
447 memset(Mask, 0, Size * sizeof(Mask[0]));
448 return Mask;
449 }
450
451 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const452 LLVM_DUMP_METHOD void MachineFunction::dump() const {
453 print(dbgs());
454 }
455 #endif
456
getName() const457 StringRef MachineFunction::getName() const {
458 return getFunction().getName();
459 }
460
print(raw_ostream & OS,const SlotIndexes * Indexes) const461 void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
462 OS << "# Machine code for function " << getName() << ": ";
463 getProperties().print(OS);
464 OS << '\n';
465
466 // Print Frame Information
467 FrameInfo->print(*this, OS);
468
469 // Print JumpTable Information
470 if (JumpTableInfo)
471 JumpTableInfo->print(OS);
472
473 // Print Constant Pool
474 ConstantPool->print(OS);
475
476 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
477
478 if (RegInfo && !RegInfo->livein_empty()) {
479 OS << "Function Live Ins: ";
480 for (MachineRegisterInfo::livein_iterator
481 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
482 OS << printReg(I->first, TRI);
483 if (I->second)
484 OS << " in " << printReg(I->second, TRI);
485 if (std::next(I) != E)
486 OS << ", ";
487 }
488 OS << '\n';
489 }
490
491 ModuleSlotTracker MST(getFunction().getParent());
492 MST.incorporateFunction(getFunction());
493 for (const auto &BB : *this) {
494 OS << '\n';
495 // If we print the whole function, print it at its most verbose level.
496 BB.print(OS, MST, Indexes, /*IsStandalone=*/true);
497 }
498
499 OS << "\n# End machine code for function " << getName() << ".\n\n";
500 }
501
502 namespace llvm {
503
504 template<>
505 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits506 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
507
getGraphNamellvm::DOTGraphTraits508 static std::string getGraphName(const MachineFunction *F) {
509 return ("CFG for '" + F->getName() + "' function").str();
510 }
511
getNodeLabelllvm::DOTGraphTraits512 std::string getNodeLabel(const MachineBasicBlock *Node,
513 const MachineFunction *Graph) {
514 std::string OutStr;
515 {
516 raw_string_ostream OSS(OutStr);
517
518 if (isSimple()) {
519 OSS << printMBBReference(*Node);
520 if (const BasicBlock *BB = Node->getBasicBlock())
521 OSS << ": " << BB->getName();
522 } else
523 Node->print(OSS);
524 }
525
526 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
527
528 // Process string output to make it nicer...
529 for (unsigned i = 0; i != OutStr.length(); ++i)
530 if (OutStr[i] == '\n') { // Left justify
531 OutStr[i] = '\\';
532 OutStr.insert(OutStr.begin()+i+1, 'l');
533 }
534 return OutStr;
535 }
536 };
537
538 } // end namespace llvm
539
viewCFG() const540 void MachineFunction::viewCFG() const
541 {
542 #ifndef NDEBUG
543 ViewGraph(this, "mf" + getName());
544 #else
545 errs() << "MachineFunction::viewCFG is only available in debug builds on "
546 << "systems with Graphviz or gv!\n";
547 #endif // NDEBUG
548 }
549
viewCFGOnly() const550 void MachineFunction::viewCFGOnly() const
551 {
552 #ifndef NDEBUG
553 ViewGraph(this, "mf" + getName(), true);
554 #else
555 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
556 << "systems with Graphviz or gv!\n";
557 #endif // NDEBUG
558 }
559
560 /// Add the specified physical register as a live-in value and
561 /// create a corresponding virtual register for it.
addLiveIn(unsigned PReg,const TargetRegisterClass * RC)562 unsigned MachineFunction::addLiveIn(unsigned PReg,
563 const TargetRegisterClass *RC) {
564 MachineRegisterInfo &MRI = getRegInfo();
565 unsigned VReg = MRI.getLiveInVirtReg(PReg);
566 if (VReg) {
567 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
568 (void)VRegRC;
569 // A physical register can be added several times.
570 // Between two calls, the register class of the related virtual register
571 // may have been constrained to match some operation constraints.
572 // In that case, check that the current register class includes the
573 // physical register and is a sub class of the specified RC.
574 assert((VRegRC == RC || (VRegRC->contains(PReg) &&
575 RC->hasSubClassEq(VRegRC))) &&
576 "Register class mismatch!");
577 return VReg;
578 }
579 VReg = MRI.createVirtualRegister(RC);
580 MRI.addLiveIn(PReg, VReg);
581 return VReg;
582 }
583
584 /// Return the MCSymbol for the specified non-empty jump table.
585 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
586 /// normal 'L' label is returned.
getJTISymbol(unsigned JTI,MCContext & Ctx,bool isLinkerPrivate) const587 MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
588 bool isLinkerPrivate) const {
589 const DataLayout &DL = getDataLayout();
590 assert(JumpTableInfo && "No jump tables");
591 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
592
593 StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
594 : DL.getPrivateGlobalPrefix();
595 SmallString<60> Name;
596 raw_svector_ostream(Name)
597 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
598 return Ctx.getOrCreateSymbol(Name);
599 }
600
601 /// Return a function-local symbol to represent the PIC base.
getPICBaseSymbol() const602 MCSymbol *MachineFunction::getPICBaseSymbol() const {
603 const DataLayout &DL = getDataLayout();
604 return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
605 Twine(getFunctionNumber()) + "$pb");
606 }
607
608 /// \name Exception Handling
609 /// \{
610
611 LandingPadInfo &
getOrCreateLandingPadInfo(MachineBasicBlock * LandingPad)612 MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
613 unsigned N = LandingPads.size();
614 for (unsigned i = 0; i < N; ++i) {
615 LandingPadInfo &LP = LandingPads[i];
616 if (LP.LandingPadBlock == LandingPad)
617 return LP;
618 }
619
620 LandingPads.push_back(LandingPadInfo(LandingPad));
621 return LandingPads[N];
622 }
623
addInvoke(MachineBasicBlock * LandingPad,MCSymbol * BeginLabel,MCSymbol * EndLabel)624 void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
625 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
626 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
627 LP.BeginLabels.push_back(BeginLabel);
628 LP.EndLabels.push_back(EndLabel);
629 }
630
addLandingPad(MachineBasicBlock * LandingPad)631 MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
632 MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
633 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
634 LP.LandingPadLabel = LandingPadLabel;
635
636 const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI();
637 if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) {
638 if (const auto *PF =
639 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()))
640 getMMI().addPersonality(PF);
641
642 if (LPI->isCleanup())
643 addCleanup(LandingPad);
644
645 // FIXME: New EH - Add the clauses in reverse order. This isn't 100%
646 // correct, but we need to do it this way because of how the DWARF EH
647 // emitter processes the clauses.
648 for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
649 Value *Val = LPI->getClause(I - 1);
650 if (LPI->isCatch(I - 1)) {
651 addCatchTypeInfo(LandingPad,
652 dyn_cast<GlobalValue>(Val->stripPointerCasts()));
653 } else {
654 // Add filters in a list.
655 auto *CVal = cast<Constant>(Val);
656 SmallVector<const GlobalValue *, 4> FilterList;
657 for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
658 II != IE; ++II)
659 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
660
661 addFilterTypeInfo(LandingPad, FilterList);
662 }
663 }
664
665 } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) {
666 for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) {
667 Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts();
668 addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo));
669 }
670
671 } else {
672 assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!");
673 }
674
675 return LandingPadLabel;
676 }
677
addCatchTypeInfo(MachineBasicBlock * LandingPad,ArrayRef<const GlobalValue * > TyInfo)678 void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
679 ArrayRef<const GlobalValue *> TyInfo) {
680 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
681 for (unsigned N = TyInfo.size(); N; --N)
682 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
683 }
684
addFilterTypeInfo(MachineBasicBlock * LandingPad,ArrayRef<const GlobalValue * > TyInfo)685 void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
686 ArrayRef<const GlobalValue *> TyInfo) {
687 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
688 std::vector<unsigned> IdsInFilter(TyInfo.size());
689 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
690 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
691 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
692 }
693
tidyLandingPads(DenseMap<MCSymbol *,uintptr_t> * LPMap,bool TidyIfNoBeginLabels)694 void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap,
695 bool TidyIfNoBeginLabels) {
696 for (unsigned i = 0; i != LandingPads.size(); ) {
697 LandingPadInfo &LandingPad = LandingPads[i];
698 if (LandingPad.LandingPadLabel &&
699 !LandingPad.LandingPadLabel->isDefined() &&
700 (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
701 LandingPad.LandingPadLabel = nullptr;
702
703 // Special case: we *should* emit LPs with null LP MBB. This indicates
704 // "nounwind" case.
705 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
706 LandingPads.erase(LandingPads.begin() + i);
707 continue;
708 }
709
710 if (TidyIfNoBeginLabels) {
711 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
712 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
713 MCSymbol *EndLabel = LandingPad.EndLabels[j];
714 if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) &&
715 (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0)))
716 continue;
717
718 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
719 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
720 --j;
721 --e;
722 }
723
724 // Remove landing pads with no try-ranges.
725 if (LandingPads[i].BeginLabels.empty()) {
726 LandingPads.erase(LandingPads.begin() + i);
727 continue;
728 }
729 }
730
731 // If there is no landing pad, ensure that the list of typeids is empty.
732 // If the only typeid is a cleanup, this is the same as having no typeids.
733 if (!LandingPad.LandingPadBlock ||
734 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
735 LandingPad.TypeIds.clear();
736 ++i;
737 }
738 }
739
addCleanup(MachineBasicBlock * LandingPad)740 void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
741 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
742 LP.TypeIds.push_back(0);
743 }
744
addSEHCatchHandler(MachineBasicBlock * LandingPad,const Function * Filter,const BlockAddress * RecoverBA)745 void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
746 const Function *Filter,
747 const BlockAddress *RecoverBA) {
748 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
749 SEHHandler Handler;
750 Handler.FilterOrFinally = Filter;
751 Handler.RecoverBA = RecoverBA;
752 LP.SEHHandlers.push_back(Handler);
753 }
754
addSEHCleanupHandler(MachineBasicBlock * LandingPad,const Function * Cleanup)755 void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
756 const Function *Cleanup) {
757 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
758 SEHHandler Handler;
759 Handler.FilterOrFinally = Cleanup;
760 Handler.RecoverBA = nullptr;
761 LP.SEHHandlers.push_back(Handler);
762 }
763
setCallSiteLandingPad(MCSymbol * Sym,ArrayRef<unsigned> Sites)764 void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
765 ArrayRef<unsigned> Sites) {
766 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
767 }
768
getTypeIDFor(const GlobalValue * TI)769 unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
770 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
771 if (TypeInfos[i] == TI) return i + 1;
772
773 TypeInfos.push_back(TI);
774 return TypeInfos.size();
775 }
776
getFilterIDFor(std::vector<unsigned> & TyIds)777 int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
778 // If the new filter coincides with the tail of an existing filter, then
779 // re-use the existing filter. Folding filters more than this requires
780 // re-ordering filters and/or their elements - probably not worth it.
781 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
782 E = FilterEnds.end(); I != E; ++I) {
783 unsigned i = *I, j = TyIds.size();
784
785 while (i && j)
786 if (FilterIds[--i] != TyIds[--j])
787 goto try_next;
788
789 if (!j)
790 // The new filter coincides with range [i, end) of the existing filter.
791 return -(1 + i);
792
793 try_next:;
794 }
795
796 // Add the new filter.
797 int FilterID = -(1 + FilterIds.size());
798 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
799 FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
800 FilterEnds.push_back(FilterIds.size());
801 FilterIds.push_back(0); // terminator
802 return FilterID;
803 }
804
805 /// \}
806
807 //===----------------------------------------------------------------------===//
808 // MachineJumpTableInfo implementation
809 //===----------------------------------------------------------------------===//
810
811 /// Return the size of each entry in the jump table.
getEntrySize(const DataLayout & TD) const812 unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
813 // The size of a jump table entry is 4 bytes unless the entry is just the
814 // address of a block, in which case it is the pointer size.
815 switch (getEntryKind()) {
816 case MachineJumpTableInfo::EK_BlockAddress:
817 return TD.getPointerSize();
818 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
819 return 8;
820 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
821 case MachineJumpTableInfo::EK_LabelDifference32:
822 case MachineJumpTableInfo::EK_Custom32:
823 return 4;
824 case MachineJumpTableInfo::EK_Inline:
825 return 0;
826 }
827 llvm_unreachable("Unknown jump table encoding!");
828 }
829
830 /// Return the alignment of each entry in the jump table.
getEntryAlignment(const DataLayout & TD) const831 unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
832 // The alignment of a jump table entry is the alignment of int32 unless the
833 // entry is just the address of a block, in which case it is the pointer
834 // alignment.
835 switch (getEntryKind()) {
836 case MachineJumpTableInfo::EK_BlockAddress:
837 return TD.getPointerABIAlignment(0);
838 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
839 return TD.getABIIntegerTypeAlignment(64);
840 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
841 case MachineJumpTableInfo::EK_LabelDifference32:
842 case MachineJumpTableInfo::EK_Custom32:
843 return TD.getABIIntegerTypeAlignment(32);
844 case MachineJumpTableInfo::EK_Inline:
845 return 1;
846 }
847 llvm_unreachable("Unknown jump table encoding!");
848 }
849
850 /// Create a new jump table entry in the jump table info.
createJumpTableIndex(const std::vector<MachineBasicBlock * > & DestBBs)851 unsigned MachineJumpTableInfo::createJumpTableIndex(
852 const std::vector<MachineBasicBlock*> &DestBBs) {
853 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
854 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
855 return JumpTables.size()-1;
856 }
857
858 /// If Old is the target of any jump tables, update the jump tables to branch
859 /// to New instead.
ReplaceMBBInJumpTables(MachineBasicBlock * Old,MachineBasicBlock * New)860 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
861 MachineBasicBlock *New) {
862 assert(Old != New && "Not making a change?");
863 bool MadeChange = false;
864 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
865 ReplaceMBBInJumpTable(i, Old, New);
866 return MadeChange;
867 }
868
869 /// If Old is a target of the jump tables, update the jump table to branch to
870 /// New instead.
ReplaceMBBInJumpTable(unsigned Idx,MachineBasicBlock * Old,MachineBasicBlock * New)871 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
872 MachineBasicBlock *Old,
873 MachineBasicBlock *New) {
874 assert(Old != New && "Not making a change?");
875 bool MadeChange = false;
876 MachineJumpTableEntry &JTE = JumpTables[Idx];
877 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
878 if (JTE.MBBs[j] == Old) {
879 JTE.MBBs[j] = New;
880 MadeChange = true;
881 }
882 return MadeChange;
883 }
884
print(raw_ostream & OS) const885 void MachineJumpTableInfo::print(raw_ostream &OS) const {
886 if (JumpTables.empty()) return;
887
888 OS << "Jump Tables:\n";
889
890 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
891 OS << printJumpTableEntryReference(i) << ": ";
892 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
893 OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]);
894 }
895
896 OS << '\n';
897 }
898
899 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const900 LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
901 #endif
902
printJumpTableEntryReference(unsigned Idx)903 Printable llvm::printJumpTableEntryReference(unsigned Idx) {
904 return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; });
905 }
906
907 //===----------------------------------------------------------------------===//
908 // MachineConstantPool implementation
909 //===----------------------------------------------------------------------===//
910
anchor()911 void MachineConstantPoolValue::anchor() {}
912
getType() const913 Type *MachineConstantPoolEntry::getType() const {
914 if (isMachineConstantPoolEntry())
915 return Val.MachineCPVal->getType();
916 return Val.ConstVal->getType();
917 }
918
needsRelocation() const919 bool MachineConstantPoolEntry::needsRelocation() const {
920 if (isMachineConstantPoolEntry())
921 return true;
922 return Val.ConstVal->needsRelocation();
923 }
924
925 SectionKind
getSectionKind(const DataLayout * DL) const926 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
927 if (needsRelocation())
928 return SectionKind::getReadOnlyWithRel();
929 switch (DL->getTypeAllocSize(getType())) {
930 case 4:
931 return SectionKind::getMergeableConst4();
932 case 8:
933 return SectionKind::getMergeableConst8();
934 case 16:
935 return SectionKind::getMergeableConst16();
936 case 32:
937 return SectionKind::getMergeableConst32();
938 default:
939 return SectionKind::getReadOnly();
940 }
941 }
942
~MachineConstantPool()943 MachineConstantPool::~MachineConstantPool() {
944 // A constant may be a member of both Constants and MachineCPVsSharingEntries,
945 // so keep track of which we've deleted to avoid double deletions.
946 DenseSet<MachineConstantPoolValue*> Deleted;
947 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
948 if (Constants[i].isMachineConstantPoolEntry()) {
949 Deleted.insert(Constants[i].Val.MachineCPVal);
950 delete Constants[i].Val.MachineCPVal;
951 }
952 for (DenseSet<MachineConstantPoolValue*>::iterator I =
953 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
954 I != E; ++I) {
955 if (Deleted.count(*I) == 0)
956 delete *I;
957 }
958 }
959
960 /// Test whether the given two constants can be allocated the same constant pool
961 /// entry.
CanShareConstantPoolEntry(const Constant * A,const Constant * B,const DataLayout & DL)962 static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
963 const DataLayout &DL) {
964 // Handle the trivial case quickly.
965 if (A == B) return true;
966
967 // If they have the same type but weren't the same constant, quickly
968 // reject them.
969 if (A->getType() == B->getType()) return false;
970
971 // We can't handle structs or arrays.
972 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
973 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
974 return false;
975
976 // For now, only support constants with the same size.
977 uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
978 if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
979 return false;
980
981 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
982
983 // Try constant folding a bitcast of both instructions to an integer. If we
984 // get two identical ConstantInt's, then we are good to share them. We use
985 // the constant folding APIs to do this so that we get the benefit of
986 // DataLayout.
987 if (isa<PointerType>(A->getType()))
988 A = ConstantFoldCastOperand(Instruction::PtrToInt,
989 const_cast<Constant *>(A), IntTy, DL);
990 else if (A->getType() != IntTy)
991 A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
992 IntTy, DL);
993 if (isa<PointerType>(B->getType()))
994 B = ConstantFoldCastOperand(Instruction::PtrToInt,
995 const_cast<Constant *>(B), IntTy, DL);
996 else if (B->getType() != IntTy)
997 B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
998 IntTy, DL);
999
1000 return A == B;
1001 }
1002
1003 /// Create a new entry in the constant pool or return an existing one.
1004 /// User must specify the log2 of the minimum required alignment for the object.
getConstantPoolIndex(const Constant * C,unsigned Alignment)1005 unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
1006 unsigned Alignment) {
1007 assert(Alignment && "Alignment must be specified!");
1008 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1009
1010 // Check to see if we already have this constant.
1011 //
1012 // FIXME, this could be made much more efficient for large constant pools.
1013 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
1014 if (!Constants[i].isMachineConstantPoolEntry() &&
1015 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
1016 if ((unsigned)Constants[i].getAlignment() < Alignment)
1017 Constants[i].Alignment = Alignment;
1018 return i;
1019 }
1020
1021 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
1022 return Constants.size()-1;
1023 }
1024
getConstantPoolIndex(MachineConstantPoolValue * V,unsigned Alignment)1025 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
1026 unsigned Alignment) {
1027 assert(Alignment && "Alignment must be specified!");
1028 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1029
1030 // Check to see if we already have this constant.
1031 //
1032 // FIXME, this could be made much more efficient for large constant pools.
1033 int Idx = V->getExistingMachineCPValue(this, Alignment);
1034 if (Idx != -1) {
1035 MachineCPVsSharingEntries.insert(V);
1036 return (unsigned)Idx;
1037 }
1038
1039 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
1040 return Constants.size()-1;
1041 }
1042
print(raw_ostream & OS) const1043 void MachineConstantPool::print(raw_ostream &OS) const {
1044 if (Constants.empty()) return;
1045
1046 OS << "Constant Pool:\n";
1047 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
1048 OS << " cp#" << i << ": ";
1049 if (Constants[i].isMachineConstantPoolEntry())
1050 Constants[i].Val.MachineCPVal->print(OS);
1051 else
1052 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1053 OS << ", align=" << Constants[i].getAlignment();
1054 OS << "\n";
1055 }
1056 }
1057
1058 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const1059 LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
1060 #endif
1061