1 //===- MachineFunction.cpp ------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Collect native machine code information for a function. This allows
10 // target-specific information about the generated code to be stored with each
11 // function.
12 //
13 //===----------------------------------------------------------------------===//
14
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"
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 /*ForcedRealign=*/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::hasOptSize().
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 LLVM_NODISCARD unsigned
addFrameInst(const MCCFIInstruction & Inst)278 MachineFunction::addFrameInst(const MCCFIInstruction &Inst) {
279 FrameInstructions.push_back(Inst);
280 return FrameInstructions.size() - 1;
281 }
282
283 /// This discards all of the MachineBasicBlock numbers and recomputes them.
284 /// This guarantees that the MBB numbers are sequential, dense, and match the
285 /// ordering of the blocks within the function. If a specific MachineBasicBlock
286 /// is specified, only that block and those after it are renumbered.
RenumberBlocks(MachineBasicBlock * MBB)287 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
288 if (empty()) { MBBNumbering.clear(); return; }
289 MachineFunction::iterator MBBI, E = end();
290 if (MBB == nullptr)
291 MBBI = begin();
292 else
293 MBBI = MBB->getIterator();
294
295 // Figure out the block number this should have.
296 unsigned BlockNo = 0;
297 if (MBBI != begin())
298 BlockNo = std::prev(MBBI)->getNumber() + 1;
299
300 for (; MBBI != E; ++MBBI, ++BlockNo) {
301 if (MBBI->getNumber() != (int)BlockNo) {
302 // Remove use of the old number.
303 if (MBBI->getNumber() != -1) {
304 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
305 "MBB number mismatch!");
306 MBBNumbering[MBBI->getNumber()] = nullptr;
307 }
308
309 // If BlockNo is already taken, set that block's number to -1.
310 if (MBBNumbering[BlockNo])
311 MBBNumbering[BlockNo]->setNumber(-1);
312
313 MBBNumbering[BlockNo] = &*MBBI;
314 MBBI->setNumber(BlockNo);
315 }
316 }
317
318 // Okay, all the blocks are renumbered. If we have compactified the block
319 // numbering, shrink MBBNumbering now.
320 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
321 MBBNumbering.resize(BlockNo);
322 }
323
324 /// Allocate a new MachineInstr. Use this instead of `new MachineInstr'.
CreateMachineInstr(const MCInstrDesc & MCID,const DebugLoc & DL,bool NoImp)325 MachineInstr *MachineFunction::CreateMachineInstr(const MCInstrDesc &MCID,
326 const DebugLoc &DL,
327 bool NoImp) {
328 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
329 MachineInstr(*this, MCID, DL, NoImp);
330 }
331
332 /// Create a new MachineInstr which is a copy of the 'Orig' instruction,
333 /// identical in all ways except the instruction has no parent, prev, or next.
334 MachineInstr *
CloneMachineInstr(const MachineInstr * Orig)335 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
336 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
337 MachineInstr(*this, *Orig);
338 }
339
CloneMachineInstrBundle(MachineBasicBlock & MBB,MachineBasicBlock::iterator InsertBefore,const MachineInstr & Orig)340 MachineInstr &MachineFunction::CloneMachineInstrBundle(MachineBasicBlock &MBB,
341 MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) {
342 MachineInstr *FirstClone = nullptr;
343 MachineBasicBlock::const_instr_iterator I = Orig.getIterator();
344 while (true) {
345 MachineInstr *Cloned = CloneMachineInstr(&*I);
346 MBB.insert(InsertBefore, Cloned);
347 if (FirstClone == nullptr) {
348 FirstClone = Cloned;
349 } else {
350 Cloned->bundleWithPred();
351 }
352
353 if (!I->isBundledWithSucc())
354 break;
355 ++I;
356 }
357 return *FirstClone;
358 }
359
360 /// Delete the given MachineInstr.
361 ///
362 /// This function also serves as the MachineInstr destructor - the real
363 /// ~MachineInstr() destructor must be empty.
364 void
DeleteMachineInstr(MachineInstr * MI)365 MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
366 // Verify that a call site info is at valid state. This assertion should
367 // be triggered during the implementation of support for the
368 // call site info of a new architecture. If the assertion is triggered,
369 // back trace will tell where to insert a call to updateCallSiteInfo().
370 assert((!MI->isCall(MachineInstr::IgnoreBundle) ||
371 CallSitesInfo.find(MI) == CallSitesInfo.end()) &&
372 "Call site info was not updated!");
373 // Strip it for parts. The operand array and the MI object itself are
374 // independently recyclable.
375 if (MI->Operands)
376 deallocateOperandArray(MI->CapOperands, MI->Operands);
377 // Don't call ~MachineInstr() which must be trivial anyway because
378 // ~MachineFunction drops whole lists of MachineInstrs wihout calling their
379 // destructors.
380 InstructionRecycler.Deallocate(Allocator, MI);
381 }
382
383 /// Allocate a new MachineBasicBlock. Use this instead of
384 /// `new MachineBasicBlock'.
385 MachineBasicBlock *
CreateMachineBasicBlock(const BasicBlock * bb)386 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
387 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
388 MachineBasicBlock(*this, bb);
389 }
390
391 /// Delete the given MachineBasicBlock.
392 void
DeleteMachineBasicBlock(MachineBasicBlock * MBB)393 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
394 assert(MBB->getParent() == this && "MBB parent mismatch!");
395 MBB->~MachineBasicBlock();
396 BasicBlockRecycler.Deallocate(Allocator, MBB);
397 }
398
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)399 MachineMemOperand *MachineFunction::getMachineMemOperand(
400 MachinePointerInfo PtrInfo, MachineMemOperand::Flags f, uint64_t s,
401 unsigned base_alignment, const AAMDNodes &AAInfo, const MDNode *Ranges,
402 SyncScope::ID SSID, AtomicOrdering Ordering,
403 AtomicOrdering FailureOrdering) {
404 return new (Allocator)
405 MachineMemOperand(PtrInfo, f, s, base_alignment, AAInfo, Ranges,
406 SSID, Ordering, FailureOrdering);
407 }
408
409 MachineMemOperand *
getMachineMemOperand(const MachineMemOperand * MMO,int64_t Offset,uint64_t Size)410 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
411 int64_t Offset, uint64_t Size) {
412 const MachinePointerInfo &PtrInfo = MMO->getPointerInfo();
413
414 // If there is no pointer value, the offset isn't tracked so we need to adjust
415 // the base alignment.
416 unsigned Align = PtrInfo.V.isNull()
417 ? MinAlign(MMO->getBaseAlignment(), Offset)
418 : MMO->getBaseAlignment();
419
420 return new (Allocator)
421 MachineMemOperand(PtrInfo.getWithOffset(Offset), MMO->getFlags(), Size,
422 Align, AAMDNodes(), nullptr, MMO->getSyncScopeID(),
423 MMO->getOrdering(), MMO->getFailureOrdering());
424 }
425
426 MachineMemOperand *
getMachineMemOperand(const MachineMemOperand * MMO,const AAMDNodes & AAInfo)427 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
428 const AAMDNodes &AAInfo) {
429 MachinePointerInfo MPI = MMO->getValue() ?
430 MachinePointerInfo(MMO->getValue(), MMO->getOffset()) :
431 MachinePointerInfo(MMO->getPseudoValue(), MMO->getOffset());
432
433 return new (Allocator)
434 MachineMemOperand(MPI, MMO->getFlags(), MMO->getSize(),
435 MMO->getBaseAlignment(), AAInfo,
436 MMO->getRanges(), MMO->getSyncScopeID(),
437 MMO->getOrdering(), MMO->getFailureOrdering());
438 }
439
440 MachineMemOperand *
getMachineMemOperand(const MachineMemOperand * MMO,MachineMemOperand::Flags Flags)441 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
442 MachineMemOperand::Flags Flags) {
443 return new (Allocator) MachineMemOperand(
444 MMO->getPointerInfo(), Flags, MMO->getSize(), MMO->getBaseAlignment(),
445 MMO->getAAInfo(), MMO->getRanges(), MMO->getSyncScopeID(),
446 MMO->getOrdering(), MMO->getFailureOrdering());
447 }
448
449 MachineInstr::ExtraInfo *
createMIExtraInfo(ArrayRef<MachineMemOperand * > MMOs,MCSymbol * PreInstrSymbol,MCSymbol * PostInstrSymbol)450 MachineFunction::createMIExtraInfo(ArrayRef<MachineMemOperand *> MMOs,
451 MCSymbol *PreInstrSymbol,
452 MCSymbol *PostInstrSymbol) {
453 return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
454 PostInstrSymbol, nullptr);
455 }
456
createMIExtraInfoWithMarker(ArrayRef<MachineMemOperand * > MMOs,MCSymbol * PreInstrSymbol,MCSymbol * PostInstrSymbol,MDNode * HeapAllocMarker)457 MachineInstr::ExtraInfo *MachineFunction::createMIExtraInfoWithMarker(
458 ArrayRef<MachineMemOperand *> MMOs, MCSymbol *PreInstrSymbol,
459 MCSymbol *PostInstrSymbol, MDNode *HeapAllocMarker) {
460 return MachineInstr::ExtraInfo::create(Allocator, MMOs, PreInstrSymbol,
461 PostInstrSymbol, HeapAllocMarker);
462 }
463
createExternalSymbolName(StringRef Name)464 const char *MachineFunction::createExternalSymbolName(StringRef Name) {
465 char *Dest = Allocator.Allocate<char>(Name.size() + 1);
466 llvm::copy(Name, Dest);
467 Dest[Name.size()] = 0;
468 return Dest;
469 }
470
allocateRegMask()471 uint32_t *MachineFunction::allocateRegMask() {
472 unsigned NumRegs = getSubtarget().getRegisterInfo()->getNumRegs();
473 unsigned Size = MachineOperand::getRegMaskSize(NumRegs);
474 uint32_t *Mask = Allocator.Allocate<uint32_t>(Size);
475 memset(Mask, 0, Size * sizeof(Mask[0]));
476 return Mask;
477 }
478
479 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const480 LLVM_DUMP_METHOD void MachineFunction::dump() const {
481 print(dbgs());
482 }
483 #endif
484
getName() const485 StringRef MachineFunction::getName() const {
486 return getFunction().getName();
487 }
488
print(raw_ostream & OS,const SlotIndexes * Indexes) const489 void MachineFunction::print(raw_ostream &OS, const SlotIndexes *Indexes) const {
490 OS << "# Machine code for function " << getName() << ": ";
491 getProperties().print(OS);
492 OS << '\n';
493
494 // Print Frame Information
495 FrameInfo->print(*this, OS);
496
497 // Print JumpTable Information
498 if (JumpTableInfo)
499 JumpTableInfo->print(OS);
500
501 // Print Constant Pool
502 ConstantPool->print(OS);
503
504 const TargetRegisterInfo *TRI = getSubtarget().getRegisterInfo();
505
506 if (RegInfo && !RegInfo->livein_empty()) {
507 OS << "Function Live Ins: ";
508 for (MachineRegisterInfo::livein_iterator
509 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
510 OS << printReg(I->first, TRI);
511 if (I->second)
512 OS << " in " << printReg(I->second, TRI);
513 if (std::next(I) != E)
514 OS << ", ";
515 }
516 OS << '\n';
517 }
518
519 ModuleSlotTracker MST(getFunction().getParent());
520 MST.incorporateFunction(getFunction());
521 for (const auto &BB : *this) {
522 OS << '\n';
523 // If we print the whole function, print it at its most verbose level.
524 BB.print(OS, MST, Indexes, /*IsStandalone=*/true);
525 }
526
527 OS << "\n# End machine code for function " << getName() << ".\n\n";
528 }
529
530 namespace llvm {
531
532 template<>
533 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
DOTGraphTraitsllvm::DOTGraphTraits534 DOTGraphTraits(bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
535
getGraphNamellvm::DOTGraphTraits536 static std::string getGraphName(const MachineFunction *F) {
537 return ("CFG for '" + F->getName() + "' function").str();
538 }
539
getNodeLabelllvm::DOTGraphTraits540 std::string getNodeLabel(const MachineBasicBlock *Node,
541 const MachineFunction *Graph) {
542 std::string OutStr;
543 {
544 raw_string_ostream OSS(OutStr);
545
546 if (isSimple()) {
547 OSS << printMBBReference(*Node);
548 if (const BasicBlock *BB = Node->getBasicBlock())
549 OSS << ": " << BB->getName();
550 } else
551 Node->print(OSS);
552 }
553
554 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
555
556 // Process string output to make it nicer...
557 for (unsigned i = 0; i != OutStr.length(); ++i)
558 if (OutStr[i] == '\n') { // Left justify
559 OutStr[i] = '\\';
560 OutStr.insert(OutStr.begin()+i+1, 'l');
561 }
562 return OutStr;
563 }
564 };
565
566 } // end namespace llvm
567
viewCFG() const568 void MachineFunction::viewCFG() const
569 {
570 #ifndef NDEBUG
571 ViewGraph(this, "mf" + getName());
572 #else
573 errs() << "MachineFunction::viewCFG is only available in debug builds on "
574 << "systems with Graphviz or gv!\n";
575 #endif // NDEBUG
576 }
577
viewCFGOnly() const578 void MachineFunction::viewCFGOnly() const
579 {
580 #ifndef NDEBUG
581 ViewGraph(this, "mf" + getName(), true);
582 #else
583 errs() << "MachineFunction::viewCFGOnly is only available in debug builds on "
584 << "systems with Graphviz or gv!\n";
585 #endif // NDEBUG
586 }
587
588 /// Add the specified physical register as a live-in value and
589 /// create a corresponding virtual register for it.
addLiveIn(unsigned PReg,const TargetRegisterClass * RC)590 unsigned MachineFunction::addLiveIn(unsigned PReg,
591 const TargetRegisterClass *RC) {
592 MachineRegisterInfo &MRI = getRegInfo();
593 unsigned VReg = MRI.getLiveInVirtReg(PReg);
594 if (VReg) {
595 const TargetRegisterClass *VRegRC = MRI.getRegClass(VReg);
596 (void)VRegRC;
597 // A physical register can be added several times.
598 // Between two calls, the register class of the related virtual register
599 // may have been constrained to match some operation constraints.
600 // In that case, check that the current register class includes the
601 // physical register and is a sub class of the specified RC.
602 assert((VRegRC == RC || (VRegRC->contains(PReg) &&
603 RC->hasSubClassEq(VRegRC))) &&
604 "Register class mismatch!");
605 return VReg;
606 }
607 VReg = MRI.createVirtualRegister(RC);
608 MRI.addLiveIn(PReg, VReg);
609 return VReg;
610 }
611
612 /// Return the MCSymbol for the specified non-empty jump table.
613 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
614 /// normal 'L' label is returned.
getJTISymbol(unsigned JTI,MCContext & Ctx,bool isLinkerPrivate) const615 MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
616 bool isLinkerPrivate) const {
617 const DataLayout &DL = getDataLayout();
618 assert(JumpTableInfo && "No jump tables");
619 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
620
621 StringRef Prefix = isLinkerPrivate ? DL.getLinkerPrivateGlobalPrefix()
622 : DL.getPrivateGlobalPrefix();
623 SmallString<60> Name;
624 raw_svector_ostream(Name)
625 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
626 return Ctx.getOrCreateSymbol(Name);
627 }
628
629 /// Return a function-local symbol to represent the PIC base.
getPICBaseSymbol() const630 MCSymbol *MachineFunction::getPICBaseSymbol() const {
631 const DataLayout &DL = getDataLayout();
632 return Ctx.getOrCreateSymbol(Twine(DL.getPrivateGlobalPrefix()) +
633 Twine(getFunctionNumber()) + "$pb");
634 }
635
636 /// \name Exception Handling
637 /// \{
638
639 LandingPadInfo &
getOrCreateLandingPadInfo(MachineBasicBlock * LandingPad)640 MachineFunction::getOrCreateLandingPadInfo(MachineBasicBlock *LandingPad) {
641 unsigned N = LandingPads.size();
642 for (unsigned i = 0; i < N; ++i) {
643 LandingPadInfo &LP = LandingPads[i];
644 if (LP.LandingPadBlock == LandingPad)
645 return LP;
646 }
647
648 LandingPads.push_back(LandingPadInfo(LandingPad));
649 return LandingPads[N];
650 }
651
addInvoke(MachineBasicBlock * LandingPad,MCSymbol * BeginLabel,MCSymbol * EndLabel)652 void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
653 MCSymbol *BeginLabel, MCSymbol *EndLabel) {
654 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
655 LP.BeginLabels.push_back(BeginLabel);
656 LP.EndLabels.push_back(EndLabel);
657 }
658
addLandingPad(MachineBasicBlock * LandingPad)659 MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
660 MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
661 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
662 LP.LandingPadLabel = LandingPadLabel;
663
664 const Instruction *FirstI = LandingPad->getBasicBlock()->getFirstNonPHI();
665 if (const auto *LPI = dyn_cast<LandingPadInst>(FirstI)) {
666 if (const auto *PF =
667 dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()))
668 getMMI().addPersonality(PF);
669
670 if (LPI->isCleanup())
671 addCleanup(LandingPad);
672
673 // FIXME: New EH - Add the clauses in reverse order. This isn't 100%
674 // correct, but we need to do it this way because of how the DWARF EH
675 // emitter processes the clauses.
676 for (unsigned I = LPI->getNumClauses(); I != 0; --I) {
677 Value *Val = LPI->getClause(I - 1);
678 if (LPI->isCatch(I - 1)) {
679 addCatchTypeInfo(LandingPad,
680 dyn_cast<GlobalValue>(Val->stripPointerCasts()));
681 } else {
682 // Add filters in a list.
683 auto *CVal = cast<Constant>(Val);
684 SmallVector<const GlobalValue *, 4> FilterList;
685 for (User::op_iterator II = CVal->op_begin(), IE = CVal->op_end();
686 II != IE; ++II)
687 FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts()));
688
689 addFilterTypeInfo(LandingPad, FilterList);
690 }
691 }
692
693 } else if (const auto *CPI = dyn_cast<CatchPadInst>(FirstI)) {
694 for (unsigned I = CPI->getNumArgOperands(); I != 0; --I) {
695 Value *TypeInfo = CPI->getArgOperand(I - 1)->stripPointerCasts();
696 addCatchTypeInfo(LandingPad, dyn_cast<GlobalValue>(TypeInfo));
697 }
698
699 } else {
700 assert(isa<CleanupPadInst>(FirstI) && "Invalid landingpad!");
701 }
702
703 return LandingPadLabel;
704 }
705
addCatchTypeInfo(MachineBasicBlock * LandingPad,ArrayRef<const GlobalValue * > TyInfo)706 void MachineFunction::addCatchTypeInfo(MachineBasicBlock *LandingPad,
707 ArrayRef<const GlobalValue *> TyInfo) {
708 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
709 for (unsigned N = TyInfo.size(); N; --N)
710 LP.TypeIds.push_back(getTypeIDFor(TyInfo[N - 1]));
711 }
712
addFilterTypeInfo(MachineBasicBlock * LandingPad,ArrayRef<const GlobalValue * > TyInfo)713 void MachineFunction::addFilterTypeInfo(MachineBasicBlock *LandingPad,
714 ArrayRef<const GlobalValue *> TyInfo) {
715 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
716 std::vector<unsigned> IdsInFilter(TyInfo.size());
717 for (unsigned I = 0, E = TyInfo.size(); I != E; ++I)
718 IdsInFilter[I] = getTypeIDFor(TyInfo[I]);
719 LP.TypeIds.push_back(getFilterIDFor(IdsInFilter));
720 }
721
tidyLandingPads(DenseMap<MCSymbol *,uintptr_t> * LPMap,bool TidyIfNoBeginLabels)722 void MachineFunction::tidyLandingPads(DenseMap<MCSymbol *, uintptr_t> *LPMap,
723 bool TidyIfNoBeginLabels) {
724 for (unsigned i = 0; i != LandingPads.size(); ) {
725 LandingPadInfo &LandingPad = LandingPads[i];
726 if (LandingPad.LandingPadLabel &&
727 !LandingPad.LandingPadLabel->isDefined() &&
728 (!LPMap || (*LPMap)[LandingPad.LandingPadLabel] == 0))
729 LandingPad.LandingPadLabel = nullptr;
730
731 // Special case: we *should* emit LPs with null LP MBB. This indicates
732 // "nounwind" case.
733 if (!LandingPad.LandingPadLabel && LandingPad.LandingPadBlock) {
734 LandingPads.erase(LandingPads.begin() + i);
735 continue;
736 }
737
738 if (TidyIfNoBeginLabels) {
739 for (unsigned j = 0, e = LandingPads[i].BeginLabels.size(); j != e; ++j) {
740 MCSymbol *BeginLabel = LandingPad.BeginLabels[j];
741 MCSymbol *EndLabel = LandingPad.EndLabels[j];
742 if ((BeginLabel->isDefined() || (LPMap && (*LPMap)[BeginLabel] != 0)) &&
743 (EndLabel->isDefined() || (LPMap && (*LPMap)[EndLabel] != 0)))
744 continue;
745
746 LandingPad.BeginLabels.erase(LandingPad.BeginLabels.begin() + j);
747 LandingPad.EndLabels.erase(LandingPad.EndLabels.begin() + j);
748 --j;
749 --e;
750 }
751
752 // Remove landing pads with no try-ranges.
753 if (LandingPads[i].BeginLabels.empty()) {
754 LandingPads.erase(LandingPads.begin() + i);
755 continue;
756 }
757 }
758
759 // If there is no landing pad, ensure that the list of typeids is empty.
760 // If the only typeid is a cleanup, this is the same as having no typeids.
761 if (!LandingPad.LandingPadBlock ||
762 (LandingPad.TypeIds.size() == 1 && !LandingPad.TypeIds[0]))
763 LandingPad.TypeIds.clear();
764 ++i;
765 }
766 }
767
addCleanup(MachineBasicBlock * LandingPad)768 void MachineFunction::addCleanup(MachineBasicBlock *LandingPad) {
769 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
770 LP.TypeIds.push_back(0);
771 }
772
addSEHCatchHandler(MachineBasicBlock * LandingPad,const Function * Filter,const BlockAddress * RecoverBA)773 void MachineFunction::addSEHCatchHandler(MachineBasicBlock *LandingPad,
774 const Function *Filter,
775 const BlockAddress *RecoverBA) {
776 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
777 SEHHandler Handler;
778 Handler.FilterOrFinally = Filter;
779 Handler.RecoverBA = RecoverBA;
780 LP.SEHHandlers.push_back(Handler);
781 }
782
addSEHCleanupHandler(MachineBasicBlock * LandingPad,const Function * Cleanup)783 void MachineFunction::addSEHCleanupHandler(MachineBasicBlock *LandingPad,
784 const Function *Cleanup) {
785 LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
786 SEHHandler Handler;
787 Handler.FilterOrFinally = Cleanup;
788 Handler.RecoverBA = nullptr;
789 LP.SEHHandlers.push_back(Handler);
790 }
791
setCallSiteLandingPad(MCSymbol * Sym,ArrayRef<unsigned> Sites)792 void MachineFunction::setCallSiteLandingPad(MCSymbol *Sym,
793 ArrayRef<unsigned> Sites) {
794 LPadToCallSiteMap[Sym].append(Sites.begin(), Sites.end());
795 }
796
getTypeIDFor(const GlobalValue * TI)797 unsigned MachineFunction::getTypeIDFor(const GlobalValue *TI) {
798 for (unsigned i = 0, N = TypeInfos.size(); i != N; ++i)
799 if (TypeInfos[i] == TI) return i + 1;
800
801 TypeInfos.push_back(TI);
802 return TypeInfos.size();
803 }
804
getFilterIDFor(std::vector<unsigned> & TyIds)805 int MachineFunction::getFilterIDFor(std::vector<unsigned> &TyIds) {
806 // If the new filter coincides with the tail of an existing filter, then
807 // re-use the existing filter. Folding filters more than this requires
808 // re-ordering filters and/or their elements - probably not worth it.
809 for (std::vector<unsigned>::iterator I = FilterEnds.begin(),
810 E = FilterEnds.end(); I != E; ++I) {
811 unsigned i = *I, j = TyIds.size();
812
813 while (i && j)
814 if (FilterIds[--i] != TyIds[--j])
815 goto try_next;
816
817 if (!j)
818 // The new filter coincides with range [i, end) of the existing filter.
819 return -(1 + i);
820
821 try_next:;
822 }
823
824 // Add the new filter.
825 int FilterID = -(1 + FilterIds.size());
826 FilterIds.reserve(FilterIds.size() + TyIds.size() + 1);
827 FilterIds.insert(FilterIds.end(), TyIds.begin(), TyIds.end());
828 FilterEnds.push_back(FilterIds.size());
829 FilterIds.push_back(0); // terminator
830 return FilterID;
831 }
832
addCodeViewHeapAllocSite(MachineInstr * I,MDNode * MD)833 void MachineFunction::addCodeViewHeapAllocSite(MachineInstr *I, MDNode *MD) {
834 MCSymbol *BeginLabel = Ctx.createTempSymbol("heapallocsite", true);
835 MCSymbol *EndLabel = Ctx.createTempSymbol("heapallocsite", true);
836 I->setPreInstrSymbol(*this, BeginLabel);
837 I->setPostInstrSymbol(*this, EndLabel);
838
839 DIType *DI = dyn_cast<DIType>(MD);
840 CodeViewHeapAllocSites.push_back(std::make_tuple(BeginLabel, EndLabel, DI));
841 }
842
updateCallSiteInfo(const MachineInstr * Old,const MachineInstr * New)843 void MachineFunction::updateCallSiteInfo(const MachineInstr *Old,
844 const MachineInstr *New) {
845 if (!Target.Options.EnableDebugEntryValues || Old == New)
846 return;
847
848 assert(Old->isCall() && (!New || New->isCall()) &&
849 "Call site info referes only to call instructions!");
850 CallSiteInfoMap::iterator CSIt = CallSitesInfo.find(Old);
851 if (CSIt == CallSitesInfo.end())
852 return;
853 CallSiteInfo CSInfo = std::move(CSIt->second);
854 CallSitesInfo.erase(CSIt);
855 if (New)
856 CallSitesInfo[New] = CSInfo;
857 }
858
859 /// \}
860
861 //===----------------------------------------------------------------------===//
862 // MachineJumpTableInfo implementation
863 //===----------------------------------------------------------------------===//
864
865 /// Return the size of each entry in the jump table.
getEntrySize(const DataLayout & TD) const866 unsigned MachineJumpTableInfo::getEntrySize(const DataLayout &TD) const {
867 // The size of a jump table entry is 4 bytes unless the entry is just the
868 // address of a block, in which case it is the pointer size.
869 switch (getEntryKind()) {
870 case MachineJumpTableInfo::EK_BlockAddress:
871 return TD.getPointerSize();
872 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
873 return 8;
874 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
875 case MachineJumpTableInfo::EK_LabelDifference32:
876 case MachineJumpTableInfo::EK_Custom32:
877 return 4;
878 case MachineJumpTableInfo::EK_Inline:
879 return 0;
880 }
881 llvm_unreachable("Unknown jump table encoding!");
882 }
883
884 /// Return the alignment of each entry in the jump table.
getEntryAlignment(const DataLayout & TD) const885 unsigned MachineJumpTableInfo::getEntryAlignment(const DataLayout &TD) const {
886 // The alignment of a jump table entry is the alignment of int32 unless the
887 // entry is just the address of a block, in which case it is the pointer
888 // alignment.
889 switch (getEntryKind()) {
890 case MachineJumpTableInfo::EK_BlockAddress:
891 return TD.getPointerABIAlignment(0);
892 case MachineJumpTableInfo::EK_GPRel64BlockAddress:
893 return TD.getABIIntegerTypeAlignment(64);
894 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
895 case MachineJumpTableInfo::EK_LabelDifference32:
896 case MachineJumpTableInfo::EK_Custom32:
897 return TD.getABIIntegerTypeAlignment(32);
898 case MachineJumpTableInfo::EK_Inline:
899 return 1;
900 }
901 llvm_unreachable("Unknown jump table encoding!");
902 }
903
904 /// Create a new jump table entry in the jump table info.
createJumpTableIndex(const std::vector<MachineBasicBlock * > & DestBBs)905 unsigned MachineJumpTableInfo::createJumpTableIndex(
906 const std::vector<MachineBasicBlock*> &DestBBs) {
907 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
908 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
909 return JumpTables.size()-1;
910 }
911
912 /// If Old is the target of any jump tables, update the jump tables to branch
913 /// to New instead.
ReplaceMBBInJumpTables(MachineBasicBlock * Old,MachineBasicBlock * New)914 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
915 MachineBasicBlock *New) {
916 assert(Old != New && "Not making a change?");
917 bool MadeChange = false;
918 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
919 ReplaceMBBInJumpTable(i, Old, New);
920 return MadeChange;
921 }
922
923 /// If Old is a target of the jump tables, update the jump table to branch to
924 /// New instead.
ReplaceMBBInJumpTable(unsigned Idx,MachineBasicBlock * Old,MachineBasicBlock * New)925 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
926 MachineBasicBlock *Old,
927 MachineBasicBlock *New) {
928 assert(Old != New && "Not making a change?");
929 bool MadeChange = false;
930 MachineJumpTableEntry &JTE = JumpTables[Idx];
931 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
932 if (JTE.MBBs[j] == Old) {
933 JTE.MBBs[j] = New;
934 MadeChange = true;
935 }
936 return MadeChange;
937 }
938
print(raw_ostream & OS) const939 void MachineJumpTableInfo::print(raw_ostream &OS) const {
940 if (JumpTables.empty()) return;
941
942 OS << "Jump Tables:\n";
943
944 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
945 OS << printJumpTableEntryReference(i) << ':';
946 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
947 OS << ' ' << printMBBReference(*JumpTables[i].MBBs[j]);
948 if (i != e)
949 OS << '\n';
950 }
951
952 OS << '\n';
953 }
954
955 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const956 LLVM_DUMP_METHOD void MachineJumpTableInfo::dump() const { print(dbgs()); }
957 #endif
958
printJumpTableEntryReference(unsigned Idx)959 Printable llvm::printJumpTableEntryReference(unsigned Idx) {
960 return Printable([Idx](raw_ostream &OS) { OS << "%jump-table." << Idx; });
961 }
962
963 //===----------------------------------------------------------------------===//
964 // MachineConstantPool implementation
965 //===----------------------------------------------------------------------===//
966
anchor()967 void MachineConstantPoolValue::anchor() {}
968
getType() const969 Type *MachineConstantPoolEntry::getType() const {
970 if (isMachineConstantPoolEntry())
971 return Val.MachineCPVal->getType();
972 return Val.ConstVal->getType();
973 }
974
needsRelocation() const975 bool MachineConstantPoolEntry::needsRelocation() const {
976 if (isMachineConstantPoolEntry())
977 return true;
978 return Val.ConstVal->needsRelocation();
979 }
980
981 SectionKind
getSectionKind(const DataLayout * DL) const982 MachineConstantPoolEntry::getSectionKind(const DataLayout *DL) const {
983 if (needsRelocation())
984 return SectionKind::getReadOnlyWithRel();
985 switch (DL->getTypeAllocSize(getType())) {
986 case 4:
987 return SectionKind::getMergeableConst4();
988 case 8:
989 return SectionKind::getMergeableConst8();
990 case 16:
991 return SectionKind::getMergeableConst16();
992 case 32:
993 return SectionKind::getMergeableConst32();
994 default:
995 return SectionKind::getReadOnly();
996 }
997 }
998
~MachineConstantPool()999 MachineConstantPool::~MachineConstantPool() {
1000 // A constant may be a member of both Constants and MachineCPVsSharingEntries,
1001 // so keep track of which we've deleted to avoid double deletions.
1002 DenseSet<MachineConstantPoolValue*> Deleted;
1003 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
1004 if (Constants[i].isMachineConstantPoolEntry()) {
1005 Deleted.insert(Constants[i].Val.MachineCPVal);
1006 delete Constants[i].Val.MachineCPVal;
1007 }
1008 for (DenseSet<MachineConstantPoolValue*>::iterator I =
1009 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end();
1010 I != E; ++I) {
1011 if (Deleted.count(*I) == 0)
1012 delete *I;
1013 }
1014 }
1015
1016 /// Test whether the given two constants can be allocated the same constant pool
1017 /// entry.
CanShareConstantPoolEntry(const Constant * A,const Constant * B,const DataLayout & DL)1018 static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B,
1019 const DataLayout &DL) {
1020 // Handle the trivial case quickly.
1021 if (A == B) return true;
1022
1023 // If they have the same type but weren't the same constant, quickly
1024 // reject them.
1025 if (A->getType() == B->getType()) return false;
1026
1027 // We can't handle structs or arrays.
1028 if (isa<StructType>(A->getType()) || isa<ArrayType>(A->getType()) ||
1029 isa<StructType>(B->getType()) || isa<ArrayType>(B->getType()))
1030 return false;
1031
1032 // For now, only support constants with the same size.
1033 uint64_t StoreSize = DL.getTypeStoreSize(A->getType());
1034 if (StoreSize != DL.getTypeStoreSize(B->getType()) || StoreSize > 128)
1035 return false;
1036
1037 Type *IntTy = IntegerType::get(A->getContext(), StoreSize*8);
1038
1039 // Try constant folding a bitcast of both instructions to an integer. If we
1040 // get two identical ConstantInt's, then we are good to share them. We use
1041 // the constant folding APIs to do this so that we get the benefit of
1042 // DataLayout.
1043 if (isa<PointerType>(A->getType()))
1044 A = ConstantFoldCastOperand(Instruction::PtrToInt,
1045 const_cast<Constant *>(A), IntTy, DL);
1046 else if (A->getType() != IntTy)
1047 A = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(A),
1048 IntTy, DL);
1049 if (isa<PointerType>(B->getType()))
1050 B = ConstantFoldCastOperand(Instruction::PtrToInt,
1051 const_cast<Constant *>(B), IntTy, DL);
1052 else if (B->getType() != IntTy)
1053 B = ConstantFoldCastOperand(Instruction::BitCast, const_cast<Constant *>(B),
1054 IntTy, DL);
1055
1056 return A == B;
1057 }
1058
1059 /// Create a new entry in the constant pool or return an existing one.
1060 /// User must specify the log2 of the minimum required alignment for the object.
getConstantPoolIndex(const Constant * C,unsigned Alignment)1061 unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C,
1062 unsigned Alignment) {
1063 assert(Alignment && "Alignment must be specified!");
1064 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1065
1066 // Check to see if we already have this constant.
1067 //
1068 // FIXME, this could be made much more efficient for large constant pools.
1069 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
1070 if (!Constants[i].isMachineConstantPoolEntry() &&
1071 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, DL)) {
1072 if ((unsigned)Constants[i].getAlignment() < Alignment)
1073 Constants[i].Alignment = Alignment;
1074 return i;
1075 }
1076
1077 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
1078 return Constants.size()-1;
1079 }
1080
getConstantPoolIndex(MachineConstantPoolValue * V,unsigned Alignment)1081 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
1082 unsigned Alignment) {
1083 assert(Alignment && "Alignment must be specified!");
1084 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
1085
1086 // Check to see if we already have this constant.
1087 //
1088 // FIXME, this could be made much more efficient for large constant pools.
1089 int Idx = V->getExistingMachineCPValue(this, Alignment);
1090 if (Idx != -1) {
1091 MachineCPVsSharingEntries.insert(V);
1092 return (unsigned)Idx;
1093 }
1094
1095 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
1096 return Constants.size()-1;
1097 }
1098
print(raw_ostream & OS) const1099 void MachineConstantPool::print(raw_ostream &OS) const {
1100 if (Constants.empty()) return;
1101
1102 OS << "Constant Pool:\n";
1103 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
1104 OS << " cp#" << i << ": ";
1105 if (Constants[i].isMachineConstantPoolEntry())
1106 Constants[i].Val.MachineCPVal->print(OS);
1107 else
1108 Constants[i].Val.ConstVal->printAsOperand(OS, /*PrintType=*/false);
1109 OS << ", align=" << Constants[i].getAlignment();
1110 OS << "\n";
1111 }
1112 }
1113
1114 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const1115 LLVM_DUMP_METHOD void MachineConstantPool::dump() const { print(dbgs()); }
1116 #endif
1117