1 //===-- MachineFunction.cpp -----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect native machine code information for a function. This allows
11 // target-specific information about the generated code to be stored with each
14 //===----------------------------------------------------------------------===//
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Config/config.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/CodeGen/MachineJumpTableInfo.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/Passes.h"
28 #include "llvm/MC/MCAsmInfo.h"
29 #include "llvm/MC/MCContext.h"
30 #include "llvm/Analysis/DebugInfo.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLowering.h"
34 #include "llvm/Target/TargetMachine.h"
35 #include "llvm/Target/TargetFrameInfo.h"
36 #include "llvm/ADT/SmallString.h"
37 #include "llvm/ADT/STLExtras.h"
38 #include "llvm/Support/GraphWriter.h"
39 #include "llvm/Support/raw_ostream.h"
43 struct Printer : public MachineFunctionPass {
47 const std::string Banner;
49 Printer(raw_ostream &os, const std::string &banner)
50 : MachineFunctionPass(&ID), OS(os), Banner(banner) {}
52 const char *getPassName() const { return "MachineFunction Printer"; }
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 MachineFunctionPass::getAnalysisUsage(AU);
59 bool runOnMachineFunction(MachineFunction &MF) {
60 OS << "# " << Banner << ":\n";
68 /// Returns a newly-created MachineFunction Printer pass. The default banner is
71 FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS,
72 const std::string &Banner){
73 return new Printer(OS, Banner);
76 //===----------------------------------------------------------------------===//
77 // MachineFunction implementation
78 //===----------------------------------------------------------------------===//
80 // Out of line virtual method.
81 MachineFunctionInfo::~MachineFunctionInfo() {}
83 void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
84 MBB->getParent()->DeleteMachineBasicBlock(MBB);
87 MachineFunction::MachineFunction(Function *F, const TargetMachine &TM,
88 unsigned FunctionNum, MCContext &ctx)
89 : Fn(F), Target(TM), Ctx(ctx) {
90 if (TM.getRegisterInfo())
91 RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
92 MachineRegisterInfo(*TM.getRegisterInfo());
96 FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
97 MachineFrameInfo(*TM.getFrameInfo());
98 if (Fn->hasFnAttr(Attribute::StackAlignment))
99 FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs(
100 Fn->getAttributes().getFnAttributes()));
101 ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
102 MachineConstantPool(TM.getTargetData());
103 Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
104 FunctionNumber = FunctionNum;
108 MachineFunction::~MachineFunction() {
110 InstructionRecycler.clear(Allocator);
111 BasicBlockRecycler.clear(Allocator);
113 RegInfo->~MachineRegisterInfo();
114 Allocator.Deallocate(RegInfo);
117 MFInfo->~MachineFunctionInfo();
118 Allocator.Deallocate(MFInfo);
120 FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
121 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
124 JumpTableInfo->~MachineJumpTableInfo();
125 Allocator.Deallocate(JumpTableInfo);
129 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
130 /// does already exist, allocate one.
131 MachineJumpTableInfo *MachineFunction::
132 getOrCreateJumpTableInfo(unsigned EntryKind) {
133 if (JumpTableInfo) return JumpTableInfo;
135 JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
136 MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
137 return JumpTableInfo;
140 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
141 /// recomputes them. This guarantees that the MBB numbers are sequential,
142 /// dense, and match the ordering of the blocks within the function. If a
143 /// specific MachineBasicBlock is specified, only that block and those after
144 /// it are renumbered.
145 void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
146 if (empty()) { MBBNumbering.clear(); return; }
147 MachineFunction::iterator MBBI, E = end();
153 // Figure out the block number this should have.
154 unsigned BlockNo = 0;
156 BlockNo = prior(MBBI)->getNumber()+1;
158 for (; MBBI != E; ++MBBI, ++BlockNo) {
159 if (MBBI->getNumber() != (int)BlockNo) {
160 // Remove use of the old number.
161 if (MBBI->getNumber() != -1) {
162 assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
163 "MBB number mismatch!");
164 MBBNumbering[MBBI->getNumber()] = 0;
167 // If BlockNo is already taken, set that block's number to -1.
168 if (MBBNumbering[BlockNo])
169 MBBNumbering[BlockNo]->setNumber(-1);
171 MBBNumbering[BlockNo] = MBBI;
172 MBBI->setNumber(BlockNo);
176 // Okay, all the blocks are renumbered. If we have compactified the block
177 // numbering, shrink MBBNumbering now.
178 assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
179 MBBNumbering.resize(BlockNo);
182 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
183 /// of `new MachineInstr'.
186 MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
187 DebugLoc DL, bool NoImp) {
188 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
189 MachineInstr(TID, DL, NoImp);
192 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the
193 /// 'Orig' instruction, identical in all ways except the instruction
194 /// has no parent, prev, or next.
197 MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
198 return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
199 MachineInstr(*this, *Orig);
202 /// DeleteMachineInstr - Delete the given MachineInstr.
205 MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
207 InstructionRecycler.Deallocate(Allocator, MI);
210 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
211 /// instead of `new MachineBasicBlock'.
214 MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
215 return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
216 MachineBasicBlock(*this, bb);
219 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
222 MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
223 assert(MBB->getParent() == this && "MBB parent mismatch!");
224 MBB->~MachineBasicBlock();
225 BasicBlockRecycler.Deallocate(Allocator, MBB);
229 MachineFunction::getMachineMemOperand(const Value *v, unsigned f,
230 int64_t o, uint64_t s,
231 unsigned base_alignment) {
232 return new (Allocator.Allocate<MachineMemOperand>())
233 MachineMemOperand(v, f, o, s, base_alignment);
237 MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
238 int64_t Offset, uint64_t Size) {
239 return new (Allocator.Allocate<MachineMemOperand>())
240 MachineMemOperand(MMO->getValue(), MMO->getFlags(),
241 int64_t(uint64_t(MMO->getOffset()) +
243 Size, MMO->getBaseAlignment());
246 MachineInstr::mmo_iterator
247 MachineFunction::allocateMemRefsArray(unsigned long Num) {
248 return Allocator.Allocate<MachineMemOperand *>(Num);
251 std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
252 MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
253 MachineInstr::mmo_iterator End) {
254 // Count the number of load mem refs.
256 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
260 // Allocate a new array and populate it with the load information.
261 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
263 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
264 if ((*I)->isLoad()) {
265 if (!(*I)->isStore())
269 // Clone the MMO and unset the store flag.
270 MachineMemOperand *JustLoad =
271 getMachineMemOperand((*I)->getValue(),
272 (*I)->getFlags() & ~MachineMemOperand::MOStore,
273 (*I)->getOffset(), (*I)->getSize(),
274 (*I)->getBaseAlignment());
275 Result[Index] = JustLoad;
280 return std::make_pair(Result, Result + Num);
283 std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
284 MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
285 MachineInstr::mmo_iterator End) {
286 // Count the number of load mem refs.
288 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
292 // Allocate a new array and populate it with the store information.
293 MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
295 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
296 if ((*I)->isStore()) {
301 // Clone the MMO and unset the load flag.
302 MachineMemOperand *JustStore =
303 getMachineMemOperand((*I)->getValue(),
304 (*I)->getFlags() & ~MachineMemOperand::MOLoad,
305 (*I)->getOffset(), (*I)->getSize(),
306 (*I)->getBaseAlignment());
307 Result[Index] = JustStore;
312 return std::make_pair(Result, Result + Num);
315 void MachineFunction::dump() const {
319 void MachineFunction::print(raw_ostream &OS) const {
320 OS << "# Machine code for function " << Fn->getName() << ":\n";
322 // Print Frame Information
323 FrameInfo->print(*this, OS);
325 // Print JumpTable Information
327 JumpTableInfo->print(OS);
329 // Print Constant Pool
330 ConstantPool->print(OS);
332 const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
334 if (RegInfo && !RegInfo->livein_empty()) {
335 OS << "Function Live Ins: ";
336 for (MachineRegisterInfo::livein_iterator
337 I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
339 OS << "%" << TRI->getName(I->first);
341 OS << " %physreg" << I->first;
344 OS << " in reg%" << I->second;
346 if (llvm::next(I) != E)
351 if (RegInfo && !RegInfo->liveout_empty()) {
352 OS << "Function Live Outs: ";
353 for (MachineRegisterInfo::liveout_iterator
354 I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
356 OS << '%' << TRI->getName(*I);
358 OS << "%physreg" << *I;
360 if (llvm::next(I) != E)
366 for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
371 OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
376 struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
378 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
380 static std::string getGraphName(const MachineFunction *F) {
381 return "CFG for '" + F->getFunction()->getNameStr() + "' function";
384 std::string getNodeLabel(const MachineBasicBlock *Node,
385 const MachineFunction *Graph) {
386 if (isSimple () && Node->getBasicBlock() &&
387 !Node->getBasicBlock()->getName().empty())
388 return Node->getBasicBlock()->getNameStr() + ":";
392 raw_string_ostream OSS(OutStr);
395 OSS << Node->getNumber() << ':';
400 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
402 // Process string output to make it nicer...
403 for (unsigned i = 0; i != OutStr.length(); ++i)
404 if (OutStr[i] == '\n') { // Left justify
406 OutStr.insert(OutStr.begin()+i+1, 'l');
413 void MachineFunction::viewCFG() const
416 ViewGraph(this, "mf" + getFunction()->getNameStr());
418 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
419 << "systems with Graphviz or gv!\n";
423 void MachineFunction::viewCFGOnly() const
426 ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
428 errs() << "SelectionDAG::viewGraph is only available in debug builds on "
429 << "systems with Graphviz or gv!\n";
433 /// addLiveIn - Add the specified physical register as a live-in value and
434 /// create a corresponding virtual register for it.
435 unsigned MachineFunction::addLiveIn(unsigned PReg,
436 const TargetRegisterClass *RC) {
437 assert(RC->contains(PReg) && "Not the correct regclass!");
438 unsigned VReg = getRegInfo().createVirtualRegister(RC);
439 getRegInfo().addLiveIn(PReg, VReg);
443 /// getDILocation - Get the DILocation for a given DebugLoc object.
444 DILocation MachineFunction::getDILocation(DebugLoc DL) const {
445 unsigned Idx = DL.getIndex();
446 assert(Idx < DebugLocInfo.DebugLocations.size() &&
447 "Invalid index into debug locations!");
448 return DILocation(DebugLocInfo.DebugLocations[Idx]);
452 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
453 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
454 /// normal 'L' label is returned.
455 MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
456 bool isLinkerPrivate) const {
457 assert(JumpTableInfo && "No jump tables");
459 assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
460 const MCAsmInfo &MAI = *getTarget().getMCAsmInfo();
462 const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() :
463 MAI.getPrivateGlobalPrefix();
464 SmallString<60> Name;
465 raw_svector_ostream(Name)
466 << Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
468 return Ctx.GetOrCreateSymbol(Name.str());
469 return Ctx.GetOrCreateTemporarySymbol(Name.str());
473 //===----------------------------------------------------------------------===//
474 // MachineFrameInfo implementation
475 //===----------------------------------------------------------------------===//
477 /// CreateFixedObject - Create a new object at a fixed location on the stack.
478 /// All fixed objects should be created before other objects are created for
479 /// efficiency. By default, fixed objects are immutable. This returns an
480 /// index with a negative value.
482 int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
483 bool Immutable, bool isSS) {
484 assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
485 Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
487 return -++NumFixedObjects;
492 MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
493 assert(MBB && "MBB must be valid");
494 const MachineFunction *MF = MBB->getParent();
495 assert(MF && "MBB must be part of a MachineFunction");
496 const TargetMachine &TM = MF->getTarget();
497 const TargetRegisterInfo *TRI = TM.getRegisterInfo();
498 BitVector BV(TRI->getNumRegs());
500 // Before CSI is calculated, no registers are considered pristine. They can be
501 // freely used and PEI will make sure they are saved.
502 if (!isCalleeSavedInfoValid())
505 for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
508 // The entry MBB always has all CSRs pristine.
509 if (MBB == &MF->front())
512 // On other MBBs the saved CSRs are not pristine.
513 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
514 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
515 E = CSI.end(); I != E; ++I)
516 BV.reset(I->getReg());
522 void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
523 if (Objects.empty()) return;
525 const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
526 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
528 OS << "Frame Objects:\n";
530 for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
531 const StackObject &SO = Objects[i];
532 OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
533 if (SO.Size == ~0ULL) {
538 OS << "variable sized";
540 OS << "size=" << SO.Size;
541 OS << ", align=" << SO.Alignment;
543 if (i < NumFixedObjects)
545 if (i < NumFixedObjects || SO.SPOffset != -1) {
546 int64_t Off = SO.SPOffset - ValOffset;
547 OS << ", at location [SP";
558 void MachineFrameInfo::dump(const MachineFunction &MF) const {
562 //===----------------------------------------------------------------------===//
563 // MachineJumpTableInfo implementation
564 //===----------------------------------------------------------------------===//
566 /// getEntrySize - Return the size of each entry in the jump table.
567 unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const {
568 // The size of a jump table entry is 4 bytes unless the entry is just the
569 // address of a block, in which case it is the pointer size.
570 switch (getEntryKind()) {
571 case MachineJumpTableInfo::EK_BlockAddress:
572 return TD.getPointerSize();
573 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
574 case MachineJumpTableInfo::EK_LabelDifference32:
575 case MachineJumpTableInfo::EK_Custom32:
577 case MachineJumpTableInfo::EK_Inline:
580 assert(0 && "Unknown jump table encoding!");
584 /// getEntryAlignment - Return the alignment of each entry in the jump table.
585 unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const {
586 // The alignment of a jump table entry is the alignment of int32 unless the
587 // entry is just the address of a block, in which case it is the pointer
589 switch (getEntryKind()) {
590 case MachineJumpTableInfo::EK_BlockAddress:
591 return TD.getPointerABIAlignment();
592 case MachineJumpTableInfo::EK_GPRel32BlockAddress:
593 case MachineJumpTableInfo::EK_LabelDifference32:
594 case MachineJumpTableInfo::EK_Custom32:
595 return TD.getABIIntegerTypeAlignment(32);
596 case MachineJumpTableInfo::EK_Inline:
599 assert(0 && "Unknown jump table encoding!");
603 /// createJumpTableIndex - Create a new jump table entry in the jump table info.
605 unsigned MachineJumpTableInfo::createJumpTableIndex(
606 const std::vector<MachineBasicBlock*> &DestBBs) {
607 assert(!DestBBs.empty() && "Cannot create an empty jump table!");
608 JumpTables.push_back(MachineJumpTableEntry(DestBBs));
609 return JumpTables.size()-1;
612 /// getJumpTableIndex - Return the index for an existing jump table entry in
613 /// the jump table info.
614 unsigned MachineJumpTableInfo::getJumpTableIndex(
615 const std::vector<MachineBasicBlock*> &DestBBs) {
616 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
617 if (JumpTables[i].MBBs == DestBBs)
619 assert(false && "getJumpTableIndex failed to find matching table");
623 /// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
624 /// the jump tables to branch to New instead.
625 bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
626 MachineBasicBlock *New) {
627 assert(Old != New && "Not making a change?");
628 bool MadeChange = false;
629 for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
630 ReplaceMBBInJumpTable(i, Old, New);
634 /// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
635 /// the jump table to branch to New instead.
636 bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
637 MachineBasicBlock *Old,
638 MachineBasicBlock *New) {
639 assert(Old != New && "Not making a change?");
640 bool MadeChange = false;
641 MachineJumpTableEntry &JTE = JumpTables[Idx];
642 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
643 if (JTE.MBBs[j] == Old) {
650 void MachineJumpTableInfo::print(raw_ostream &OS) const {
651 if (JumpTables.empty()) return;
653 OS << "Jump Tables:\n";
655 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
656 OS << " jt#" << i << ": ";
657 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
658 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
664 void MachineJumpTableInfo::dump() const { print(dbgs()); }
667 //===----------------------------------------------------------------------===//
668 // MachineConstantPool implementation
669 //===----------------------------------------------------------------------===//
671 const Type *MachineConstantPoolEntry::getType() const {
672 if (isMachineConstantPoolEntry())
673 return Val.MachineCPVal->getType();
674 return Val.ConstVal->getType();
678 unsigned MachineConstantPoolEntry::getRelocationInfo() const {
679 if (isMachineConstantPoolEntry())
680 return Val.MachineCPVal->getRelocationInfo();
681 return Val.ConstVal->getRelocationInfo();
684 MachineConstantPool::~MachineConstantPool() {
685 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
686 if (Constants[i].isMachineConstantPoolEntry())
687 delete Constants[i].Val.MachineCPVal;
690 /// CanShareConstantPoolEntry - Test whether the given two constants
691 /// can be allocated the same constant pool entry.
692 static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
693 const TargetData *TD) {
694 // Handle the trivial case quickly.
695 if (A == B) return true;
697 // If they have the same type but weren't the same constant, quickly
699 if (A->getType() == B->getType()) return false;
701 // For now, only support constants with the same size.
702 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
705 // If a floating-point value and an integer value have the same encoding,
706 // they can share a constant-pool entry.
707 if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
708 if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
709 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
710 if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
711 if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
712 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
714 // Two vectors can share an entry if each pair of corresponding
716 if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
717 if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
718 if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
720 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
721 if (!CanShareConstantPoolEntry(AV->getOperand(i),
722 BV->getOperand(i), TD))
727 // TODO: Handle other cases.
732 /// getConstantPoolIndex - Create a new entry in the constant pool or return
733 /// an existing one. User must specify the log2 of the minimum required
734 /// alignment for the object.
736 unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
737 unsigned Alignment) {
738 assert(Alignment && "Alignment must be specified!");
739 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
741 // Check to see if we already have this constant.
743 // FIXME, this could be made much more efficient for large constant pools.
744 for (unsigned i = 0, e = Constants.size(); i != e; ++i)
745 if (!Constants[i].isMachineConstantPoolEntry() &&
746 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
747 if ((unsigned)Constants[i].getAlignment() < Alignment)
748 Constants[i].Alignment = Alignment;
752 Constants.push_back(MachineConstantPoolEntry(C, Alignment));
753 return Constants.size()-1;
756 unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
757 unsigned Alignment) {
758 assert(Alignment && "Alignment must be specified!");
759 if (Alignment > PoolAlignment) PoolAlignment = Alignment;
761 // Check to see if we already have this constant.
763 // FIXME, this could be made much more efficient for large constant pools.
764 int Idx = V->getExistingMachineCPValue(this, Alignment);
766 return (unsigned)Idx;
768 Constants.push_back(MachineConstantPoolEntry(V, Alignment));
769 return Constants.size()-1;
772 void MachineConstantPool::print(raw_ostream &OS) const {
773 if (Constants.empty()) return;
775 OS << "Constant Pool:\n";
776 for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
777 OS << " cp#" << i << ": ";
778 if (Constants[i].isMachineConstantPoolEntry())
779 Constants[i].Val.MachineCPVal->print(OS);
781 OS << *(Value*)Constants[i].Val.ConstVal;
782 OS << ", align=" << Constants[i].getAlignment();
787 void MachineConstantPool::dump() const { print(dbgs()); }