2002-10-28 09:16:38 +08:00
|
|
|
//===-- MachineFunction.cpp -----------------------------------------------===//
|
2004-09-06 02:41:35 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-09-06 02:41:35 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-09-06 02:41:35 +08:00
|
|
|
//
|
2002-10-28 09:16:38 +08:00
|
|
|
// Collect native machine code information for a function. This allows
|
|
|
|
// target-specific information about the generated code to be stored with each
|
|
|
|
// function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-03 15:54:50 +08:00
|
|
|
|
2007-02-14 13:52:17 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2009-07-01 06:38:32 +08:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/Config/config.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2009-08-20 06:16:11 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2003-12-20 18:20:58 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-29 05:08:26 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2006-04-23 02:53:45 +08:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2003-12-20 18:20:58 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2010-01-26 13:58:28 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2010-01-19 14:09:04 +08:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
2010-01-05 07:39:17 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2006-05-12 14:33:49 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-07-01 06:38:32 +08:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2002-02-03 15:54:50 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2002-12-29 05:00:25 +08:00
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
2010-01-26 13:58:28 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2006-10-04 04:19:23 +08:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2008-08-24 06:23:09 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-12-20 17:17:07 +08:00
|
|
|
using namespace llvm;
|
2002-02-03 15:54:50 +08:00
|
|
|
|
2002-10-28 09:12:41 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
struct Printer : public MachineFunctionPass {
|
2007-05-03 09:11:54 +08:00
|
|
|
static char ID;
|
2007-05-02 05:15:47 +08:00
|
|
|
|
2009-08-23 11:13:20 +08:00
|
|
|
raw_ostream &OS;
|
2004-02-01 13:25:07 +08:00
|
|
|
const std::string Banner;
|
2004-01-31 05:53:46 +08:00
|
|
|
|
2009-08-23 11:13:20 +08:00
|
|
|
Printer(raw_ostream &os, const std::string &banner)
|
2008-09-05 01:05:41 +08:00
|
|
|
: MachineFunctionPass(&ID), OS(os), Banner(banner) {}
|
2004-01-31 05:53:46 +08:00
|
|
|
|
2002-10-30 08:48:05 +08:00
|
|
|
const char *getPassName() const { return "MachineFunction Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2009-08-01 02:16:33 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2002-10-30 08:48:05 +08:00
|
|
|
}
|
|
|
|
|
2003-12-20 18:20:58 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "# " << Banner << ":\n";
|
2009-08-23 11:13:20 +08:00
|
|
|
MF.print(OS);
|
2002-10-30 08:48:05 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2007-05-03 09:11:54 +08:00
|
|
|
char Printer::ID = 0;
|
2002-10-28 09:12:41 +08:00
|
|
|
}
|
|
|
|
|
2009-08-03 09:02:24 +08:00
|
|
|
/// Returns a newly-created MachineFunction Printer pass. The default banner is
|
|
|
|
/// empty.
|
2004-01-31 05:53:46 +08:00
|
|
|
///
|
2009-08-23 11:13:20 +08:00
|
|
|
FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS,
|
2005-01-24 06:13:58 +08:00
|
|
|
const std::string &Banner){
|
2004-01-31 05:53:46 +08:00
|
|
|
return new Printer(OS, Banner);
|
2002-10-30 08:48:05 +08:00
|
|
|
}
|
|
|
|
|
2010-01-26 12:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-28 09:12:41 +08:00
|
|
|
// MachineFunction implementation
|
2010-01-26 12:35:26 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-01-30 02:41:25 +08:00
|
|
|
|
2009-09-16 06:44:26 +08:00
|
|
|
// Out of line virtual method.
|
|
|
|
MachineFunctionInfo::~MachineFunctionInfo() {}
|
|
|
|
|
2008-07-29 05:51:04 +08:00
|
|
|
void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
|
2008-07-08 07:14:23 +08:00
|
|
|
MBB->getParent()->DeleteMachineBasicBlock(MBB);
|
2004-05-24 14:11:51 +08:00
|
|
|
}
|
2002-10-28 09:12:41 +08:00
|
|
|
|
2010-01-26 12:35:26 +08:00
|
|
|
MachineFunction::MachineFunction(Function *F, const TargetMachine &TM,
|
2010-03-14 04:55:24 +08:00
|
|
|
unsigned FunctionNum, MCContext &ctx)
|
|
|
|
: Fn(F), Target(TM), Ctx(ctx) {
|
2008-10-13 20:37:16 +08:00
|
|
|
if (TM.getRegisterInfo())
|
|
|
|
RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
|
|
|
|
MachineRegisterInfo(*TM.getRegisterInfo());
|
|
|
|
else
|
|
|
|
RegInfo = 0;
|
2004-08-17 06:36:34 +08:00
|
|
|
MFInfo = 0;
|
2008-07-08 07:14:23 +08:00
|
|
|
FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
|
|
|
|
MachineFrameInfo(*TM.getFrameInfo());
|
2010-02-20 02:17:13 +08:00
|
|
|
if (Fn->hasFnAttr(Attribute::StackAlignment))
|
|
|
|
FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs(
|
|
|
|
Fn->getAttributes().getFnAttributes()));
|
2008-07-08 07:14:23 +08:00
|
|
|
ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
|
|
|
|
MachineConstantPool(TM.getTargetData());
|
2009-07-01 06:38:32 +08:00
|
|
|
Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
|
2010-01-26 12:35:26 +08:00
|
|
|
FunctionNumber = FunctionNum;
|
2010-01-26 07:26:13 +08:00
|
|
|
JumpTableInfo = 0;
|
2002-12-25 13:03:22 +08:00
|
|
|
}
|
|
|
|
|
2004-09-06 02:41:35 +08:00
|
|
|
MachineFunction::~MachineFunction() {
|
2004-07-01 14:29:07 +08:00
|
|
|
BasicBlocks.clear();
|
2008-07-08 07:14:23 +08:00
|
|
|
InstructionRecycler.clear(Allocator);
|
|
|
|
BasicBlockRecycler.clear(Allocator);
|
2009-06-25 08:32:48 +08:00
|
|
|
if (RegInfo) {
|
|
|
|
RegInfo->~MachineRegisterInfo();
|
|
|
|
Allocator.Deallocate(RegInfo);
|
|
|
|
}
|
2008-07-08 07:14:23 +08:00
|
|
|
if (MFInfo) {
|
2009-06-25 08:32:48 +08:00
|
|
|
MFInfo->~MachineFunctionInfo();
|
|
|
|
Allocator.Deallocate(MFInfo);
|
2008-07-08 07:14:23 +08:00
|
|
|
}
|
|
|
|
FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
|
|
|
|
ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
|
2010-01-26 07:26:13 +08:00
|
|
|
|
|
|
|
if (JumpTableInfo) {
|
|
|
|
JumpTableInfo->~MachineJumpTableInfo();
|
|
|
|
Allocator.Deallocate(JumpTableInfo);
|
|
|
|
}
|
2002-10-30 08:48:05 +08:00
|
|
|
}
|
|
|
|
|
2010-01-26 07:26:13 +08:00
|
|
|
/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it
|
|
|
|
/// does already exist, allocate one.
|
|
|
|
MachineJumpTableInfo *MachineFunction::
|
|
|
|
getOrCreateJumpTableInfo(unsigned EntryKind) {
|
|
|
|
if (JumpTableInfo) return JumpTableInfo;
|
|
|
|
|
|
|
|
JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
|
|
|
|
MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind);
|
|
|
|
return JumpTableInfo;
|
|
|
|
}
|
2006-10-04 03:18:57 +08:00
|
|
|
|
|
|
|
/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and
|
|
|
|
/// recomputes them. This guarantees that the MBB numbers are sequential,
|
|
|
|
/// dense, and match the ordering of the blocks within the function. If a
|
|
|
|
/// specific MachineBasicBlock is specified, only that block and those after
|
|
|
|
/// it are renumbered.
|
|
|
|
void MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) {
|
|
|
|
if (empty()) { MBBNumbering.clear(); return; }
|
|
|
|
MachineFunction::iterator MBBI, E = end();
|
|
|
|
if (MBB == 0)
|
|
|
|
MBBI = begin();
|
|
|
|
else
|
|
|
|
MBBI = MBB;
|
|
|
|
|
|
|
|
// Figure out the block number this should have.
|
|
|
|
unsigned BlockNo = 0;
|
2006-10-04 04:19:23 +08:00
|
|
|
if (MBBI != begin())
|
|
|
|
BlockNo = prior(MBBI)->getNumber()+1;
|
2006-10-04 03:18:57 +08:00
|
|
|
|
|
|
|
for (; MBBI != E; ++MBBI, ++BlockNo) {
|
|
|
|
if (MBBI->getNumber() != (int)BlockNo) {
|
|
|
|
// Remove use of the old number.
|
|
|
|
if (MBBI->getNumber() != -1) {
|
|
|
|
assert(MBBNumbering[MBBI->getNumber()] == &*MBBI &&
|
|
|
|
"MBB number mismatch!");
|
|
|
|
MBBNumbering[MBBI->getNumber()] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If BlockNo is already taken, set that block's number to -1.
|
|
|
|
if (MBBNumbering[BlockNo])
|
|
|
|
MBBNumbering[BlockNo]->setNumber(-1);
|
|
|
|
|
|
|
|
MBBNumbering[BlockNo] = MBBI;
|
|
|
|
MBBI->setNumber(BlockNo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, all the blocks are renumbered. If we have compactified the block
|
|
|
|
// numbering, shrink MBBNumbering now.
|
|
|
|
assert(BlockNo <= MBBNumbering.size() && "Mismatch!");
|
|
|
|
MBBNumbering.resize(BlockNo);
|
|
|
|
}
|
|
|
|
|
2008-07-08 07:14:23 +08:00
|
|
|
/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
|
|
|
|
/// of `new MachineInstr'.
|
|
|
|
///
|
|
|
|
MachineInstr *
|
2009-02-03 08:55:04 +08:00
|
|
|
MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
|
|
|
|
DebugLoc DL, bool NoImp) {
|
2008-07-08 07:14:23 +08:00
|
|
|
return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
|
2009-02-03 08:55:04 +08:00
|
|
|
MachineInstr(TID, DL, NoImp);
|
2008-07-08 07:14:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
|
2010-02-11 00:03:48 +08:00
|
|
|
/// 'Orig' instruction, identical in all ways except the instruction
|
2008-07-08 07:14:23 +08:00
|
|
|
/// has no parent, prev, or next.
|
|
|
|
///
|
|
|
|
MachineInstr *
|
|
|
|
MachineFunction::CloneMachineInstr(const MachineInstr *Orig) {
|
|
|
|
return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
|
|
|
|
MachineInstr(*this, *Orig);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// DeleteMachineInstr - Delete the given MachineInstr.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
MachineFunction::DeleteMachineInstr(MachineInstr *MI) {
|
|
|
|
MI->~MachineInstr();
|
|
|
|
InstructionRecycler.Deallocate(Allocator, MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this
|
|
|
|
/// instead of `new MachineBasicBlock'.
|
|
|
|
///
|
|
|
|
MachineBasicBlock *
|
|
|
|
MachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) {
|
|
|
|
return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator))
|
|
|
|
MachineBasicBlock(*this, bb);
|
|
|
|
}
|
2006-10-04 03:18:57 +08:00
|
|
|
|
2008-07-08 07:14:23 +08:00
|
|
|
/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock.
|
|
|
|
///
|
|
|
|
void
|
|
|
|
MachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) {
|
|
|
|
assert(MBB->getParent() == this && "MBB parent mismatch!");
|
|
|
|
MBB->~MachineBasicBlock();
|
|
|
|
BasicBlockRecycler.Deallocate(Allocator, MBB);
|
|
|
|
}
|
|
|
|
|
2009-09-26 04:36:54 +08:00
|
|
|
MachineMemOperand *
|
|
|
|
MachineFunction::getMachineMemOperand(const Value *v, unsigned f,
|
|
|
|
int64_t o, uint64_t s,
|
|
|
|
unsigned base_alignment) {
|
|
|
|
return new (Allocator.Allocate<MachineMemOperand>())
|
|
|
|
MachineMemOperand(v, f, o, s, base_alignment);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineMemOperand *
|
|
|
|
MachineFunction::getMachineMemOperand(const MachineMemOperand *MMO,
|
|
|
|
int64_t Offset, uint64_t Size) {
|
|
|
|
return new (Allocator.Allocate<MachineMemOperand>())
|
|
|
|
MachineMemOperand(MMO->getValue(), MMO->getFlags(),
|
|
|
|
int64_t(uint64_t(MMO->getOffset()) +
|
|
|
|
uint64_t(Offset)),
|
|
|
|
Size, MMO->getBaseAlignment());
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr::mmo_iterator
|
|
|
|
MachineFunction::allocateMemRefsArray(unsigned long Num) {
|
|
|
|
return Allocator.Allocate<MachineMemOperand *>(Num);
|
|
|
|
}
|
|
|
|
|
2009-10-10 02:10:05 +08:00
|
|
|
std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
|
|
|
|
MachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin,
|
|
|
|
MachineInstr::mmo_iterator End) {
|
|
|
|
// Count the number of load mem refs.
|
|
|
|
unsigned Num = 0;
|
|
|
|
for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
|
|
|
|
if ((*I)->isLoad())
|
|
|
|
++Num;
|
|
|
|
|
|
|
|
// Allocate a new array and populate it with the load information.
|
|
|
|
MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
|
|
|
|
if ((*I)->isLoad()) {
|
|
|
|
if (!(*I)->isStore())
|
|
|
|
// Reuse the MMO.
|
|
|
|
Result[Index] = *I;
|
|
|
|
else {
|
|
|
|
// Clone the MMO and unset the store flag.
|
|
|
|
MachineMemOperand *JustLoad =
|
|
|
|
getMachineMemOperand((*I)->getValue(),
|
|
|
|
(*I)->getFlags() & ~MachineMemOperand::MOStore,
|
|
|
|
(*I)->getOffset(), (*I)->getSize(),
|
|
|
|
(*I)->getBaseAlignment());
|
|
|
|
Result[Index] = JustLoad;
|
|
|
|
}
|
|
|
|
++Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(Result, Result + Num);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator>
|
|
|
|
MachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin,
|
|
|
|
MachineInstr::mmo_iterator End) {
|
|
|
|
// Count the number of load mem refs.
|
|
|
|
unsigned Num = 0;
|
|
|
|
for (MachineInstr::mmo_iterator I = Begin; I != End; ++I)
|
|
|
|
if ((*I)->isStore())
|
|
|
|
++Num;
|
|
|
|
|
|
|
|
// Allocate a new array and populate it with the store information.
|
|
|
|
MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num);
|
|
|
|
unsigned Index = 0;
|
|
|
|
for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) {
|
|
|
|
if ((*I)->isStore()) {
|
|
|
|
if (!(*I)->isLoad())
|
|
|
|
// Reuse the MMO.
|
|
|
|
Result[Index] = *I;
|
|
|
|
else {
|
|
|
|
// Clone the MMO and unset the load flag.
|
|
|
|
MachineMemOperand *JustStore =
|
|
|
|
getMachineMemOperand((*I)->getValue(),
|
|
|
|
(*I)->getFlags() & ~MachineMemOperand::MOLoad,
|
|
|
|
(*I)->getOffset(), (*I)->getSize(),
|
|
|
|
(*I)->getBaseAlignment());
|
|
|
|
Result[Index] = JustStore;
|
|
|
|
}
|
|
|
|
++Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::make_pair(Result, Result + Num);
|
|
|
|
}
|
|
|
|
|
2008-07-08 07:14:23 +08:00
|
|
|
void MachineFunction::dump() const {
|
2010-01-05 07:39:17 +08:00
|
|
|
print(dbgs());
|
2008-07-08 07:14:23 +08:00
|
|
|
}
|
2002-10-30 08:48:05 +08:00
|
|
|
|
2009-08-23 09:12:47 +08:00
|
|
|
void MachineFunction::print(raw_ostream &OS) const {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "# Machine code for function " << Fn->getName() << ":\n";
|
2002-12-29 04:37:16 +08:00
|
|
|
|
|
|
|
// Print Frame Information
|
2008-07-08 07:14:23 +08:00
|
|
|
FrameInfo->print(*this, OS);
|
2006-04-23 02:53:45 +08:00
|
|
|
|
|
|
|
// Print JumpTable Information
|
2010-01-26 07:26:13 +08:00
|
|
|
if (JumpTableInfo)
|
|
|
|
JumpTableInfo->print(OS);
|
2003-01-13 08:23:03 +08:00
|
|
|
|
|
|
|
// Print Constant Pool
|
2009-08-23 09:12:47 +08:00
|
|
|
ConstantPool->print(OS);
|
2005-09-01 06:34:59 +08:00
|
|
|
|
2008-02-11 02:45:23 +08:00
|
|
|
const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
|
2005-09-01 06:34:59 +08:00
|
|
|
|
2008-10-13 20:37:16 +08:00
|
|
|
if (RegInfo && !RegInfo->livein_empty()) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "Function Live Ins: ";
|
2007-12-31 12:13:23 +08:00
|
|
|
for (MachineRegisterInfo::livein_iterator
|
|
|
|
I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
|
2008-02-11 02:45:23 +08:00
|
|
|
if (TRI)
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "%" << TRI->getName(I->first);
|
2005-09-01 06:34:59 +08:00
|
|
|
else
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " %physreg" << I->first;
|
2006-05-16 13:55:30 +08:00
|
|
|
|
|
|
|
if (I->second)
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " in reg%" << I->second;
|
|
|
|
|
2009-12-03 08:50:42 +08:00
|
|
|
if (llvm::next(I) != E)
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << ", ";
|
2005-09-01 06:34:59 +08:00
|
|
|
}
|
2009-08-23 09:12:47 +08:00
|
|
|
OS << '\n';
|
2005-09-01 06:34:59 +08:00
|
|
|
}
|
2008-10-13 20:37:16 +08:00
|
|
|
if (RegInfo && !RegInfo->liveout_empty()) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "Function Live Outs: ";
|
2007-12-31 12:13:23 +08:00
|
|
|
for (MachineRegisterInfo::liveout_iterator
|
2009-11-01 04:19:03 +08:00
|
|
|
I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
|
2008-02-11 02:45:23 +08:00
|
|
|
if (TRI)
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << '%' << TRI->getName(*I);
|
2005-09-01 06:34:59 +08:00
|
|
|
else
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "%physreg" << *I;
|
|
|
|
|
2009-12-03 08:50:42 +08:00
|
|
|
if (llvm::next(I) != E)
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " ";
|
|
|
|
}
|
2009-08-23 09:12:47 +08:00
|
|
|
OS << '\n';
|
2005-09-01 06:34:59 +08:00
|
|
|
}
|
|
|
|
|
2009-11-01 04:19:03 +08:00
|
|
|
for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
|
|
|
|
OS << '\n';
|
2009-08-23 08:47:04 +08:00
|
|
|
BB->print(OS);
|
2009-11-01 04:19:03 +08:00
|
|
|
}
|
2004-03-30 05:58:31 +08:00
|
|
|
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
|
2002-10-30 08:48:05 +08:00
|
|
|
}
|
|
|
|
|
2004-07-08 08:47:58 +08:00
|
|
|
namespace llvm {
|
2004-09-06 02:41:35 +08:00
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
|
2009-11-30 20:38:13 +08:00
|
|
|
|
|
|
|
DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
|
|
|
|
|
2004-09-06 02:41:35 +08:00
|
|
|
static std::string getGraphName(const MachineFunction *F) {
|
2009-07-24 16:24:36 +08:00
|
|
|
return "CFG for '" + F->getFunction()->getNameStr() + "' function";
|
2004-09-06 02:41:35 +08:00
|
|
|
}
|
2004-07-08 08:47:58 +08:00
|
|
|
|
2009-11-30 20:38:47 +08:00
|
|
|
std::string getNodeLabel(const MachineBasicBlock *Node,
|
|
|
|
const MachineFunction *Graph) {
|
|
|
|
if (isSimple () && Node->getBasicBlock() &&
|
2004-09-06 02:41:35 +08:00
|
|
|
!Node->getBasicBlock()->getName().empty())
|
2009-07-24 16:24:36 +08:00
|
|
|
return Node->getBasicBlock()->getNameStr() + ":";
|
2004-07-08 08:47:58 +08:00
|
|
|
|
2009-08-23 11:13:20 +08:00
|
|
|
std::string OutStr;
|
|
|
|
{
|
|
|
|
raw_string_ostream OSS(OutStr);
|
|
|
|
|
2009-11-30 20:38:47 +08:00
|
|
|
if (isSimple())
|
2009-08-23 11:13:20 +08:00
|
|
|
OSS << Node->getNumber() << ':';
|
|
|
|
else
|
|
|
|
Node->print(OSS);
|
2004-09-06 02:41:35 +08:00
|
|
|
}
|
2004-07-08 08:47:58 +08:00
|
|
|
|
2004-09-06 02:41:35 +08:00
|
|
|
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
|
2004-07-08 08:47:58 +08:00
|
|
|
|
2004-09-06 02:41:35 +08:00
|
|
|
// Process string output to make it nicer...
|
|
|
|
for (unsigned i = 0; i != OutStr.length(); ++i)
|
|
|
|
if (OutStr[i] == '\n') { // Left justify
|
|
|
|
OutStr[i] = '\\';
|
|
|
|
OutStr.insert(OutStr.begin()+i+1, 'l');
|
|
|
|
}
|
|
|
|
return OutStr;
|
|
|
|
}
|
|
|
|
};
|
2004-07-08 08:47:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::viewCFG() const
|
|
|
|
{
|
2005-10-12 20:09:05 +08:00
|
|
|
#ifndef NDEBUG
|
2009-07-24 16:24:36 +08:00
|
|
|
ViewGraph(this, "mf" + getFunction()->getNameStr());
|
2006-06-28 00:49:46 +08:00
|
|
|
#else
|
2009-08-23 16:50:52 +08:00
|
|
|
errs() << "SelectionDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
2006-06-28 00:49:46 +08:00
|
|
|
#endif // NDEBUG
|
2004-07-08 08:47:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::viewCFGOnly() const
|
|
|
|
{
|
2009-06-25 01:37:09 +08:00
|
|
|
#ifndef NDEBUG
|
2009-07-24 16:24:36 +08:00
|
|
|
ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
|
2009-06-25 01:37:09 +08:00
|
|
|
#else
|
2009-08-23 16:50:52 +08:00
|
|
|
errs() << "SelectionDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
2009-06-25 01:37:09 +08:00
|
|
|
#endif // NDEBUG
|
2004-07-08 08:47:58 +08:00
|
|
|
}
|
|
|
|
|
2009-04-21 02:36:57 +08:00
|
|
|
/// addLiveIn - Add the specified physical register as a live-in value and
|
|
|
|
/// create a corresponding virtual register for it.
|
|
|
|
unsigned MachineFunction::addLiveIn(unsigned PReg,
|
|
|
|
const TargetRegisterClass *RC) {
|
|
|
|
assert(RC->contains(PReg) && "Not the correct regclass!");
|
|
|
|
unsigned VReg = getRegInfo().createVirtualRegister(RC);
|
|
|
|
getRegInfo().addLiveIn(PReg, VReg);
|
|
|
|
return VReg;
|
|
|
|
}
|
|
|
|
|
2010-01-16 14:09:35 +08:00
|
|
|
/// getDILocation - Get the DILocation for a given DebugLoc object.
|
|
|
|
DILocation MachineFunction::getDILocation(DebugLoc DL) const {
|
2009-02-04 06:55:54 +08:00
|
|
|
unsigned Idx = DL.getIndex();
|
2009-02-04 06:49:58 +08:00
|
|
|
assert(Idx < DebugLocInfo.DebugLocations.size() &&
|
|
|
|
"Invalid index into debug locations!");
|
2010-01-16 14:09:35 +08:00
|
|
|
return DILocation(DebugLocInfo.DebugLocations[Idx]);
|
2009-02-04 06:49:58 +08:00
|
|
|
}
|
|
|
|
|
2010-01-26 14:28:43 +08:00
|
|
|
|
|
|
|
/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table.
|
|
|
|
/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a
|
|
|
|
/// normal 'L' label is returned.
|
|
|
|
MCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx,
|
|
|
|
bool isLinkerPrivate) const {
|
|
|
|
assert(JumpTableInfo && "No jump tables");
|
|
|
|
|
2010-01-27 18:27:10 +08:00
|
|
|
assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!");
|
2010-01-26 14:28:43 +08:00
|
|
|
const MCAsmInfo &MAI = *getTarget().getMCAsmInfo();
|
|
|
|
|
|
|
|
const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() :
|
|
|
|
MAI.getPrivateGlobalPrefix();
|
|
|
|
SmallString<60> Name;
|
|
|
|
raw_svector_ostream(Name)
|
|
|
|
<< Prefix << "JTI" << getFunctionNumber() << '_' << JTI;
|
2010-03-10 10:25:11 +08:00
|
|
|
if (isLinkerPrivate)
|
|
|
|
return Ctx.GetOrCreateSymbol(Name.str());
|
|
|
|
return Ctx.GetOrCreateTemporarySymbol(Name.str());
|
2010-01-26 14:28:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-29 04:37:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-29 05:08:26 +08:00
|
|
|
// MachineFrameInfo implementation
|
2002-12-29 04:37:16 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-25 15:19:06 +08:00
|
|
|
/// CreateFixedObject - Create a new object at a fixed location on the stack.
|
|
|
|
/// All fixed objects should be created before other objects are created for
|
|
|
|
/// efficiency. By default, fixed objects are immutable. This returns an
|
|
|
|
/// index with a negative value.
|
|
|
|
///
|
|
|
|
int MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset,
|
2009-11-13 04:49:22 +08:00
|
|
|
bool Immutable, bool isSS) {
|
2008-01-25 15:19:06 +08:00
|
|
|
assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
|
2009-11-13 04:49:22 +08:00
|
|
|
Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
|
|
|
|
isSS));
|
2008-01-25 15:19:06 +08:00
|
|
|
return -++NumFixedObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-14 00:19:33 +08:00
|
|
|
BitVector
|
|
|
|
MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const {
|
|
|
|
assert(MBB && "MBB must be valid");
|
|
|
|
const MachineFunction *MF = MBB->getParent();
|
|
|
|
assert(MF && "MBB must be part of a MachineFunction");
|
|
|
|
const TargetMachine &TM = MF->getTarget();
|
|
|
|
const TargetRegisterInfo *TRI = TM.getRegisterInfo();
|
|
|
|
BitVector BV(TRI->getNumRegs());
|
|
|
|
|
|
|
|
// Before CSI is calculated, no registers are considered pristine. They can be
|
|
|
|
// freely used and PEI will make sure they are saved.
|
|
|
|
if (!isCalleeSavedInfoValid())
|
|
|
|
return BV;
|
|
|
|
|
|
|
|
for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR)
|
|
|
|
BV.set(*CSR);
|
|
|
|
|
|
|
|
// The entry MBB always has all CSRs pristine.
|
|
|
|
if (MBB == &MF->front())
|
|
|
|
return BV;
|
|
|
|
|
|
|
|
// On other MBBs the saved CSRs are not pristine.
|
|
|
|
const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo();
|
|
|
|
for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(),
|
|
|
|
E = CSI.end(); I != E; ++I)
|
|
|
|
BV.reset(I->getReg());
|
|
|
|
|
|
|
|
return BV;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-23 09:12:47 +08:00
|
|
|
void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
|
2009-11-01 04:19:03 +08:00
|
|
|
if (Objects.empty()) return;
|
|
|
|
|
2008-11-03 19:16:43 +08:00
|
|
|
const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
|
|
|
|
int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
|
2003-01-17 02:35:57 +08:00
|
|
|
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "Frame Objects:\n";
|
|
|
|
|
2002-12-29 04:37:16 +08:00
|
|
|
for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
|
|
|
|
const StackObject &SO = Objects[i];
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
|
2008-02-27 11:04:06 +08:00
|
|
|
if (SO.Size == ~0ULL) {
|
|
|
|
OS << "dead\n";
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-29 04:37:16 +08:00
|
|
|
if (SO.Size == 0)
|
|
|
|
OS << "variable sized";
|
|
|
|
else
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << "size=" << SO.Size;
|
|
|
|
OS << ", align=" << SO.Alignment;
|
2004-09-06 02:41:35 +08:00
|
|
|
|
2002-12-29 04:37:16 +08:00
|
|
|
if (i < NumFixedObjects)
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << ", fixed";
|
2002-12-29 04:37:16 +08:00
|
|
|
if (i < NumFixedObjects || SO.SPOffset != -1) {
|
2007-04-25 12:20:54 +08:00
|
|
|
int64_t Off = SO.SPOffset - ValOffset;
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << ", at location [SP";
|
2003-01-17 02:35:57 +08:00
|
|
|
if (Off > 0)
|
2004-09-06 02:41:35 +08:00
|
|
|
OS << "+" << Off;
|
2003-01-17 02:35:57 +08:00
|
|
|
else if (Off < 0)
|
2004-09-06 02:41:35 +08:00
|
|
|
OS << Off;
|
2002-12-29 04:37:16 +08:00
|
|
|
OS << "]";
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-17 02:35:57 +08:00
|
|
|
void MachineFrameInfo::dump(const MachineFunction &MF) const {
|
2010-01-05 07:39:17 +08:00
|
|
|
print(MF, dbgs());
|
2003-01-17 02:35:57 +08:00
|
|
|
}
|
2002-12-29 04:37:16 +08:00
|
|
|
|
2006-04-23 02:53:45 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MachineJumpTableInfo implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-01-26 07:26:13 +08:00
|
|
|
/// getEntrySize - Return the size of each entry in the jump table.
|
|
|
|
unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const {
|
|
|
|
// The size of a jump table entry is 4 bytes unless the entry is just the
|
|
|
|
// address of a block, in which case it is the pointer size.
|
|
|
|
switch (getEntryKind()) {
|
|
|
|
case MachineJumpTableInfo::EK_BlockAddress:
|
|
|
|
return TD.getPointerSize();
|
|
|
|
case MachineJumpTableInfo::EK_GPRel32BlockAddress:
|
|
|
|
case MachineJumpTableInfo::EK_LabelDifference32:
|
2010-01-26 12:05:28 +08:00
|
|
|
case MachineJumpTableInfo::EK_Custom32:
|
2010-01-26 07:26:13 +08:00
|
|
|
return 4;
|
2010-03-11 22:58:16 +08:00
|
|
|
case MachineJumpTableInfo::EK_Inline:
|
|
|
|
return 0;
|
2010-01-26 07:26:13 +08:00
|
|
|
}
|
|
|
|
assert(0 && "Unknown jump table encoding!");
|
|
|
|
return ~0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getEntryAlignment - Return the alignment of each entry in the jump table.
|
|
|
|
unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const {
|
|
|
|
// The alignment of a jump table entry is the alignment of int32 unless the
|
|
|
|
// entry is just the address of a block, in which case it is the pointer
|
|
|
|
// alignment.
|
|
|
|
switch (getEntryKind()) {
|
|
|
|
case MachineJumpTableInfo::EK_BlockAddress:
|
|
|
|
return TD.getPointerABIAlignment();
|
|
|
|
case MachineJumpTableInfo::EK_GPRel32BlockAddress:
|
|
|
|
case MachineJumpTableInfo::EK_LabelDifference32:
|
2010-01-26 12:05:28 +08:00
|
|
|
case MachineJumpTableInfo::EK_Custom32:
|
2010-01-26 07:26:13 +08:00
|
|
|
return TD.getABIIntegerTypeAlignment(32);
|
2010-03-11 22:58:16 +08:00
|
|
|
case MachineJumpTableInfo::EK_Inline:
|
|
|
|
return 1;
|
2010-01-26 07:26:13 +08:00
|
|
|
}
|
|
|
|
assert(0 && "Unknown jump table encoding!");
|
|
|
|
return ~0;
|
|
|
|
}
|
|
|
|
|
2006-04-23 02:53:45 +08:00
|
|
|
/// getJumpTableIndex - Create a new jump table entry in the jump table info
|
|
|
|
/// or return an existing one.
|
|
|
|
///
|
|
|
|
unsigned MachineJumpTableInfo::getJumpTableIndex(
|
2006-10-29 02:17:09 +08:00
|
|
|
const std::vector<MachineBasicBlock*> &DestBBs) {
|
2006-10-29 02:11:20 +08:00
|
|
|
assert(!DestBBs.empty() && "Cannot create an empty jump table!");
|
2006-04-23 02:53:45 +08:00
|
|
|
JumpTables.push_back(MachineJumpTableEntry(DestBBs));
|
|
|
|
return JumpTables.size()-1;
|
|
|
|
}
|
|
|
|
|
2010-01-26 13:58:28 +08:00
|
|
|
|
2009-04-15 09:18:49 +08:00
|
|
|
/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
|
|
|
|
/// the jump tables to branch to New instead.
|
2010-01-26 13:58:28 +08:00
|
|
|
bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
|
|
|
|
MachineBasicBlock *New) {
|
2009-04-15 09:18:49 +08:00
|
|
|
assert(Old != New && "Not making a change?");
|
|
|
|
bool MadeChange = false;
|
2009-11-15 04:09:13 +08:00
|
|
|
for (size_t i = 0, e = JumpTables.size(); i != e; ++i)
|
|
|
|
ReplaceMBBInJumpTable(i, Old, New);
|
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update
|
|
|
|
/// the jump table to branch to New instead.
|
2010-01-26 13:58:28 +08:00
|
|
|
bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
|
|
|
|
MachineBasicBlock *Old,
|
|
|
|
MachineBasicBlock *New) {
|
2009-11-15 04:09:13 +08:00
|
|
|
assert(Old != New && "Not making a change?");
|
|
|
|
bool MadeChange = false;
|
|
|
|
MachineJumpTableEntry &JTE = JumpTables[Idx];
|
|
|
|
for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
|
|
|
|
if (JTE.MBBs[j] == Old) {
|
|
|
|
JTE.MBBs[j] = New;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2009-04-15 09:18:49 +08:00
|
|
|
return MadeChange;
|
|
|
|
}
|
2006-04-23 02:53:45 +08:00
|
|
|
|
2009-08-23 09:12:47 +08:00
|
|
|
void MachineJumpTableInfo::print(raw_ostream &OS) const {
|
2009-11-01 04:19:03 +08:00
|
|
|
if (JumpTables.empty()) return;
|
|
|
|
|
|
|
|
OS << "Jump Tables:\n";
|
|
|
|
|
2006-04-23 02:53:45 +08:00
|
|
|
for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " jt#" << i << ": ";
|
|
|
|
for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j)
|
|
|
|
OS << " BB#" << JumpTables[i].MBBs[j]->getNumber();
|
2006-04-23 02:53:45 +08:00
|
|
|
}
|
2009-11-01 04:19:03 +08:00
|
|
|
|
|
|
|
OS << '\n';
|
2006-04-23 02:53:45 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 07:39:17 +08:00
|
|
|
void MachineJumpTableInfo::dump() const { print(dbgs()); }
|
2006-04-23 02:53:45 +08:00
|
|
|
|
|
|
|
|
2003-01-13 08:23:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MachineConstantPool implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-09-14 13:50:57 +08:00
|
|
|
const Type *MachineConstantPoolEntry::getType() const {
|
|
|
|
if (isMachineConstantPoolEntry())
|
2009-07-22 07:34:23 +08:00
|
|
|
return Val.MachineCPVal->getType();
|
2006-09-14 13:50:57 +08:00
|
|
|
return Val.ConstVal->getType();
|
|
|
|
}
|
|
|
|
|
2009-07-22 07:34:23 +08:00
|
|
|
|
2009-07-22 07:36:01 +08:00
|
|
|
unsigned MachineConstantPoolEntry::getRelocationInfo() const {
|
2009-07-22 07:34:23 +08:00
|
|
|
if (isMachineConstantPoolEntry())
|
2009-07-22 07:36:01 +08:00
|
|
|
return Val.MachineCPVal->getRelocationInfo();
|
2009-07-22 08:05:44 +08:00
|
|
|
return Val.ConstVal->getRelocationInfo();
|
2009-07-22 07:34:23 +08:00
|
|
|
}
|
|
|
|
|
2006-09-13 05:00:35 +08:00
|
|
|
MachineConstantPool::~MachineConstantPool() {
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i)
|
|
|
|
if (Constants[i].isMachineConstantPoolEntry())
|
|
|
|
delete Constants[i].Val.MachineCPVal;
|
|
|
|
}
|
|
|
|
|
2009-10-28 09:12:16 +08:00
|
|
|
/// CanShareConstantPoolEntry - Test whether the given two constants
|
|
|
|
/// can be allocated the same constant pool entry.
|
|
|
|
static bool CanShareConstantPoolEntry(Constant *A, Constant *B,
|
|
|
|
const TargetData *TD) {
|
|
|
|
// Handle the trivial case quickly.
|
|
|
|
if (A == B) return true;
|
|
|
|
|
|
|
|
// If they have the same type but weren't the same constant, quickly
|
|
|
|
// reject them.
|
|
|
|
if (A->getType() == B->getType()) return false;
|
|
|
|
|
|
|
|
// For now, only support constants with the same size.
|
|
|
|
if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If a floating-point value and an integer value have the same encoding,
|
|
|
|
// they can share a constant-pool entry.
|
|
|
|
if (ConstantFP *AFP = dyn_cast<ConstantFP>(A))
|
|
|
|
if (ConstantInt *BI = dyn_cast<ConstantInt>(B))
|
|
|
|
return AFP->getValueAPF().bitcastToAPInt() == BI->getValue();
|
|
|
|
if (ConstantFP *BFP = dyn_cast<ConstantFP>(B))
|
|
|
|
if (ConstantInt *AI = dyn_cast<ConstantInt>(A))
|
|
|
|
return BFP->getValueAPF().bitcastToAPInt() == AI->getValue();
|
|
|
|
|
|
|
|
// Two vectors can share an entry if each pair of corresponding
|
|
|
|
// elements could.
|
|
|
|
if (ConstantVector *AV = dyn_cast<ConstantVector>(A))
|
|
|
|
if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) {
|
|
|
|
if (AV->getType()->getNumElements() != BV->getType()->getNumElements())
|
|
|
|
return false;
|
|
|
|
for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i)
|
2009-10-31 22:12:53 +08:00
|
|
|
if (!CanShareConstantPoolEntry(AV->getOperand(i),
|
|
|
|
BV->getOperand(i), TD))
|
2009-10-28 09:12:16 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle other cases.
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-09 12:46:04 +08:00
|
|
|
/// getConstantPoolIndex - Create a new entry in the constant pool or return
|
2008-09-17 04:45:53 +08:00
|
|
|
/// an existing one. User must specify the log2 of the minimum required
|
|
|
|
/// alignment for the object.
|
2006-02-09 12:46:04 +08:00
|
|
|
///
|
|
|
|
unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
|
|
|
|
unsigned Alignment) {
|
|
|
|
assert(Alignment && "Alignment must be specified!");
|
|
|
|
if (Alignment > PoolAlignment) PoolAlignment = Alignment;
|
2009-10-28 09:12:16 +08:00
|
|
|
|
2006-02-09 12:46:04 +08:00
|
|
|
// Check to see if we already have this constant.
|
|
|
|
//
|
|
|
|
// FIXME, this could be made much more efficient for large constant pools.
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i)
|
2009-10-28 09:12:16 +08:00
|
|
|
if (!Constants[i].isMachineConstantPoolEntry() &&
|
|
|
|
CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) {
|
|
|
|
if ((unsigned)Constants[i].getAlignment() < Alignment)
|
|
|
|
Constants[i].Alignment = Alignment;
|
2006-02-09 12:46:04 +08:00
|
|
|
return i;
|
2009-10-28 09:12:16 +08:00
|
|
|
}
|
2006-02-09 12:46:04 +08:00
|
|
|
|
2009-03-13 15:51:59 +08:00
|
|
|
Constants.push_back(MachineConstantPoolEntry(C, Alignment));
|
2006-02-09 12:46:04 +08:00
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2006-09-13 05:00:35 +08:00
|
|
|
unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V,
|
|
|
|
unsigned Alignment) {
|
|
|
|
assert(Alignment && "Alignment must be specified!");
|
|
|
|
if (Alignment > PoolAlignment) PoolAlignment = Alignment;
|
|
|
|
|
|
|
|
// Check to see if we already have this constant.
|
|
|
|
//
|
|
|
|
// FIXME, this could be made much more efficient for large constant pools.
|
|
|
|
int Idx = V->getExistingMachineCPValue(this, Alignment);
|
|
|
|
if (Idx != -1)
|
|
|
|
return (unsigned)Idx;
|
2009-03-13 15:51:59 +08:00
|
|
|
|
|
|
|
Constants.push_back(MachineConstantPoolEntry(V, Alignment));
|
2006-09-13 05:00:35 +08:00
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2008-08-24 06:53:13 +08:00
|
|
|
void MachineConstantPool::print(raw_ostream &OS) const {
|
2009-11-01 04:19:03 +08:00
|
|
|
if (Constants.empty()) return;
|
|
|
|
|
|
|
|
OS << "Constant Pool:\n";
|
2006-02-01 06:23:14 +08:00
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << " cp#" << i << ": ";
|
2006-09-13 05:00:35 +08:00
|
|
|
if (Constants[i].isMachineConstantPoolEntry())
|
|
|
|
Constants[i].Val.MachineCPVal->print(OS);
|
|
|
|
else
|
|
|
|
OS << *(Value*)Constants[i].Val.ConstVal;
|
2009-11-01 04:19:03 +08:00
|
|
|
OS << ", align=" << Constants[i].getAlignment();
|
2006-02-01 06:23:14 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2003-01-13 08:23:03 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 07:39:17 +08:00
|
|
|
void MachineConstantPool::dump() const { print(dbgs()); }
|