2002-10-28 02:16:38 +01:00
|
|
|
//===-- MachineFunction.cpp -----------------------------------------------===//
|
2004-09-05 20:41:35 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-09-05 20:41:35 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-09-05 20:41:35 +02:00
|
|
|
//
|
2002-10-28 02:16:38 +01: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 08:54:50 +01:00
|
|
|
|
2007-02-14 06:52:17 +01:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2009-07-01 00:38:32 +02:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Instructions.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/Config/config.h"
|
2007-12-31 05:13:23 +01:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2009-08-20 00:16:11 +02:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2003-12-20 11:20:58 +01:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-28 22:08:26 +01:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-12-31 05:13:23 +01:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2006-04-22 20:53:45 +02:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2007-12-31 05:13:23 +01:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2003-12-20 11:20:58 +01:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2006-05-12 08:33:49 +02:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-07-01 00:38:32 +02:00
|
|
|
#include "llvm/Target/TargetLowering.h"
|
2002-02-03 08:54:50 +01:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2002-12-28 22:00:25 +01:00
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
2006-10-03 22:19:23 +02:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2008-08-24 00:23:09 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-12-20 10:17:07 +01:00
|
|
|
using namespace llvm;
|
2002-02-03 08:54:50 +01:00
|
|
|
|
2002-10-28 02:12:41 +01:00
|
|
|
namespace {
|
2009-10-25 07:33:48 +01:00
|
|
|
struct Printer : public MachineFunctionPass {
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID;
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2009-08-23 05:13:20 +02:00
|
|
|
raw_ostream &OS;
|
2004-02-01 06:25:07 +01:00
|
|
|
const std::string Banner;
|
2004-01-30 22:53:46 +01:00
|
|
|
|
2009-08-23 05:13:20 +02:00
|
|
|
Printer(raw_ostream &os, const std::string &banner)
|
2008-09-04 19:05:41 +02:00
|
|
|
: MachineFunctionPass(&ID), OS(os), Banner(banner) {}
|
2004-01-30 22:53:46 +01:00
|
|
|
|
2002-10-30 01:48:05 +01:00
|
|
|
const char *getPassName() const { return "MachineFunction Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2009-07-31 20:16:33 +02:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2002-10-30 01:48:05 +01:00
|
|
|
}
|
|
|
|
|
2003-12-20 11:20:58 +01:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) {
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "# " << Banner << ":\n";
|
2009-08-23 05:13:20 +02:00
|
|
|
MF.print(OS);
|
2002-10-30 01:48:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2007-05-03 03:11:54 +02:00
|
|
|
char Printer::ID = 0;
|
2002-10-28 02:12:41 +01:00
|
|
|
}
|
|
|
|
|
2009-08-03 03:02:24 +02:00
|
|
|
/// Returns a newly-created MachineFunction Printer pass. The default banner is
|
|
|
|
/// empty.
|
2004-01-30 22:53:46 +01:00
|
|
|
///
|
2009-08-23 05:13:20 +02:00
|
|
|
FunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS,
|
2005-01-23 23:13:58 +01:00
|
|
|
const std::string &Banner){
|
2004-01-30 22:53:46 +01:00
|
|
|
return new Printer(OS, Banner);
|
2002-10-30 01:48:05 +01:00
|
|
|
}
|
|
|
|
|
2002-10-28 02:12:41 +01:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// MachineFunction implementation
|
|
|
|
//===---------------------------------------------------------------------===//
|
2005-01-29 19:41:25 +01:00
|
|
|
|
2009-09-16 00:44:26 +02:00
|
|
|
// Out of line virtual method.
|
|
|
|
MachineFunctionInfo::~MachineFunctionInfo() {}
|
|
|
|
|
2008-07-28 23:51:04 +02:00
|
|
|
void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
|
2008-07-08 01:14:23 +02:00
|
|
|
MBB->getParent()->DeleteMachineBasicBlock(MBB);
|
2004-05-24 08:11:51 +02:00
|
|
|
}
|
2002-10-28 02:12:41 +01:00
|
|
|
|
2009-07-31 20:16:33 +02:00
|
|
|
MachineFunction::MachineFunction(Function *F,
|
2002-12-28 21:37:16 +01:00
|
|
|
const TargetMachine &TM)
|
2009-07-31 20:35:51 +02:00
|
|
|
: Fn(F), Target(TM) {
|
2008-10-13 14:37:16 +02:00
|
|
|
if (TM.getRegisterInfo())
|
|
|
|
RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
|
|
|
|
MachineRegisterInfo(*TM.getRegisterInfo());
|
|
|
|
else
|
|
|
|
RegInfo = 0;
|
2004-08-17 00:36:34 +02:00
|
|
|
MFInfo = 0;
|
2008-07-08 01:14:23 +02:00
|
|
|
FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
|
|
|
|
MachineFrameInfo(*TM.getFrameInfo());
|
|
|
|
ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
|
|
|
|
MachineConstantPool(TM.getTargetData());
|
2009-07-01 00:38:32 +02:00
|
|
|
Alignment = TM.getTargetLowering()->getFunctionAlignment(F);
|
2009-06-25 02:32:48 +02:00
|
|
|
|
2006-12-14 20:17:33 +01:00
|
|
|
// Set up jump table.
|
|
|
|
const TargetData &TD = *TM.getTargetData();
|
|
|
|
bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
|
|
|
|
unsigned EntrySize = IsPic ? 4 : TD.getPointerSize();
|
2009-08-13 23:58:54 +02:00
|
|
|
unsigned TyAlignment = IsPic ?
|
|
|
|
TD.getABITypeAlignment(Type::getInt32Ty(F->getContext()))
|
2009-06-25 02:32:48 +02:00
|
|
|
: TD.getPointerABIAlignment();
|
2008-07-08 01:14:23 +02:00
|
|
|
JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
|
2009-06-25 02:32:48 +02:00
|
|
|
MachineJumpTableInfo(EntrySize, TyAlignment);
|
2002-12-25 06:03:22 +01:00
|
|
|
}
|
|
|
|
|
2004-09-05 20:41:35 +02:00
|
|
|
MachineFunction::~MachineFunction() {
|
2004-07-01 08:29:07 +02:00
|
|
|
BasicBlocks.clear();
|
2008-07-08 01:14:23 +02:00
|
|
|
InstructionRecycler.clear(Allocator);
|
|
|
|
BasicBlockRecycler.clear(Allocator);
|
2009-06-25 02:32:48 +02:00
|
|
|
if (RegInfo) {
|
|
|
|
RegInfo->~MachineRegisterInfo();
|
|
|
|
Allocator.Deallocate(RegInfo);
|
|
|
|
}
|
2008-07-08 01:14:23 +02:00
|
|
|
if (MFInfo) {
|
2009-06-25 02:32:48 +02:00
|
|
|
MFInfo->~MachineFunctionInfo();
|
|
|
|
Allocator.Deallocate(MFInfo);
|
2008-07-08 01:14:23 +02:00
|
|
|
}
|
|
|
|
FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo);
|
|
|
|
ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool);
|
|
|
|
JumpTableInfo->~MachineJumpTableInfo(); Allocator.Deallocate(JumpTableInfo);
|
2002-10-30 01:48:05 +01:00
|
|
|
}
|
|
|
|
|
2006-10-03 21:18:57 +02: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-03 22:19:23 +02:00
|
|
|
if (MBBI != begin())
|
|
|
|
BlockNo = prior(MBBI)->getNumber()+1;
|
2006-10-03 21:18:57 +02: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 01:14:23 +02:00
|
|
|
/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
|
|
|
|
/// of `new MachineInstr'.
|
|
|
|
///
|
|
|
|
MachineInstr *
|
2009-02-03 01:55:04 +01:00
|
|
|
MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
|
|
|
|
DebugLoc DL, bool NoImp) {
|
2008-07-08 01:14:23 +02:00
|
|
|
return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
|
2009-02-03 01:55:04 +01:00
|
|
|
MachineInstr(TID, DL, NoImp);
|
2008-07-08 01:14:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CloneMachineInstr - Create a new MachineInstr which is a copy of the
|
|
|
|
/// 'Orig' instruction, identical in all ways except the the instruction
|
|
|
|
/// 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-03 21:18:57 +02:00
|
|
|
|
2008-07-08 01:14:23 +02: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-25 22:36:54 +02: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-09 20:10:05 +02: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 01:14:23 +02:00
|
|
|
void MachineFunction::dump() const {
|
2009-08-23 03:12:47 +02:00
|
|
|
print(errs());
|
2008-07-08 01:14:23 +02:00
|
|
|
}
|
2002-10-30 01:48:05 +01:00
|
|
|
|
2009-08-23 03:12:47 +02:00
|
|
|
void MachineFunction::print(raw_ostream &OS) const {
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "# Machine code for function " << Fn->getName() << ":\n";
|
2002-12-28 21:37:16 +01:00
|
|
|
|
|
|
|
// Print Frame Information
|
2008-07-08 01:14:23 +02:00
|
|
|
FrameInfo->print(*this, OS);
|
2006-04-22 20:53:45 +02:00
|
|
|
|
|
|
|
// Print JumpTable Information
|
2008-07-08 01:14:23 +02:00
|
|
|
JumpTableInfo->print(OS);
|
2003-01-13 01:23:03 +01:00
|
|
|
|
|
|
|
// Print Constant Pool
|
2009-08-23 03:12:47 +02:00
|
|
|
ConstantPool->print(OS);
|
2005-09-01 00:34:59 +02:00
|
|
|
|
2008-02-10 19:45:23 +01:00
|
|
|
const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
|
2005-09-01 00:34:59 +02:00
|
|
|
|
2008-10-13 14:37:16 +02:00
|
|
|
if (RegInfo && !RegInfo->livein_empty()) {
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "Function Live Ins: ";
|
2007-12-31 05:13:23 +01:00
|
|
|
for (MachineRegisterInfo::livein_iterator
|
|
|
|
I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
|
2008-02-10 19:45:23 +01:00
|
|
|
if (TRI)
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "%" << TRI->getName(I->first);
|
2005-09-01 00:34:59 +02:00
|
|
|
else
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << " %physreg" << I->first;
|
2006-05-16 07:55:30 +02:00
|
|
|
|
|
|
|
if (I->second)
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << " in reg%" << I->second;
|
|
|
|
|
|
|
|
if (next(I) != E)
|
|
|
|
OS << ", ";
|
2005-09-01 00:34:59 +02:00
|
|
|
}
|
2009-08-23 03:12:47 +02:00
|
|
|
OS << '\n';
|
2005-09-01 00:34:59 +02:00
|
|
|
}
|
2008-10-13 14:37:16 +02:00
|
|
|
if (RegInfo && !RegInfo->liveout_empty()) {
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "Function Live Outs: ";
|
2007-12-31 05:13:23 +01:00
|
|
|
for (MachineRegisterInfo::liveout_iterator
|
2009-10-31 21:19:03 +01:00
|
|
|
I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){
|
2008-02-10 19:45:23 +01:00
|
|
|
if (TRI)
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << '%' << TRI->getName(*I);
|
2005-09-01 00:34:59 +02:00
|
|
|
else
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "%physreg" << *I;
|
|
|
|
|
|
|
|
if (next(I) != E)
|
|
|
|
OS << " ";
|
|
|
|
}
|
2009-08-23 03:12:47 +02:00
|
|
|
OS << '\n';
|
2005-09-01 00:34:59 +02:00
|
|
|
}
|
|
|
|
|
2009-10-31 21:19:03 +01:00
|
|
|
for (const_iterator BB = begin(), E = end(); BB != E; ++BB) {
|
|
|
|
OS << '\n';
|
2009-08-23 02:47:04 +02:00
|
|
|
BB->print(OS);
|
2009-10-31 21:19:03 +01:00
|
|
|
}
|
2004-03-29 23:58:31 +02:00
|
|
|
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "\n# End machine code for function " << Fn->getName() << ".\n\n";
|
2002-10-30 01:48:05 +01:00
|
|
|
}
|
|
|
|
|
2004-07-08 02:47:58 +02:00
|
|
|
namespace llvm {
|
2004-09-05 20:41:35 +02:00
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(const MachineFunction *F) {
|
2009-07-24 10:24:36 +02:00
|
|
|
return "CFG for '" + F->getFunction()->getNameStr() + "' function";
|
2004-09-05 20:41:35 +02:00
|
|
|
}
|
2004-07-08 02:47:58 +02:00
|
|
|
|
2004-09-05 20:41:35 +02:00
|
|
|
static std::string getNodeLabel(const MachineBasicBlock *Node,
|
2009-06-24 19:37:09 +02:00
|
|
|
const MachineFunction *Graph,
|
|
|
|
bool ShortNames) {
|
|
|
|
if (ShortNames && Node->getBasicBlock() &&
|
2004-09-05 20:41:35 +02:00
|
|
|
!Node->getBasicBlock()->getName().empty())
|
2009-07-24 10:24:36 +02:00
|
|
|
return Node->getBasicBlock()->getNameStr() + ":";
|
2004-07-08 02:47:58 +02:00
|
|
|
|
2009-08-23 05:13:20 +02:00
|
|
|
std::string OutStr;
|
|
|
|
{
|
|
|
|
raw_string_ostream OSS(OutStr);
|
|
|
|
|
|
|
|
if (ShortNames)
|
|
|
|
OSS << Node->getNumber() << ':';
|
|
|
|
else
|
|
|
|
Node->print(OSS);
|
2004-09-05 20:41:35 +02:00
|
|
|
}
|
2004-07-08 02:47:58 +02:00
|
|
|
|
2004-09-05 20:41:35 +02:00
|
|
|
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
|
2004-07-08 02:47:58 +02:00
|
|
|
|
2004-09-05 20:41:35 +02: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 02:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::viewCFG() const
|
|
|
|
{
|
2005-10-12 14:09:05 +02:00
|
|
|
#ifndef NDEBUG
|
2009-07-24 10:24:36 +02:00
|
|
|
ViewGraph(this, "mf" + getFunction()->getNameStr());
|
2006-06-27 18:49:46 +02:00
|
|
|
#else
|
2009-08-23 10:50:52 +02:00
|
|
|
errs() << "SelectionDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
2006-06-27 18:49:46 +02:00
|
|
|
#endif // NDEBUG
|
2004-07-08 02:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::viewCFGOnly() const
|
|
|
|
{
|
2009-06-24 19:37:09 +02:00
|
|
|
#ifndef NDEBUG
|
2009-07-24 10:24:36 +02:00
|
|
|
ViewGraph(this, "mf" + getFunction()->getNameStr(), true);
|
2009-06-24 19:37:09 +02:00
|
|
|
#else
|
2009-08-23 10:50:52 +02:00
|
|
|
errs() << "SelectionDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
2009-06-24 19:37:09 +02:00
|
|
|
#endif // NDEBUG
|
2004-07-08 02:47:58 +02:00
|
|
|
}
|
|
|
|
|
2009-04-20 20:36:57 +02: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;
|
|
|
|
}
|
|
|
|
|
2009-02-03 23:49:58 +01:00
|
|
|
/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
|
2009-02-04 01:05:34 +01:00
|
|
|
DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
|
2009-02-03 23:55:54 +01:00
|
|
|
unsigned Idx = DL.getIndex();
|
2009-02-03 23:49:58 +01:00
|
|
|
assert(Idx < DebugLocInfo.DebugLocations.size() &&
|
|
|
|
"Invalid index into debug locations!");
|
|
|
|
return DebugLocInfo.DebugLocations[Idx];
|
|
|
|
}
|
|
|
|
|
2002-12-28 21:37:16 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-28 22:08:26 +01:00
|
|
|
// MachineFrameInfo implementation
|
2002-12-28 21:37:16 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-25 08:19:06 +01: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-12 21:49:22 +01:00
|
|
|
bool Immutable, bool isSS) {
|
2008-01-25 08:19:06 +01:00
|
|
|
assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
|
2009-11-12 21:49:22 +01:00
|
|
|
Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable,
|
|
|
|
isSS));
|
2008-01-25 08:19:06 +01:00
|
|
|
return -++NumFixedObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-13 18:19:33 +02: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 03:12:47 +02:00
|
|
|
void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{
|
2009-10-31 21:19:03 +01:00
|
|
|
if (Objects.empty()) return;
|
|
|
|
|
2008-11-03 12:16:43 +01:00
|
|
|
const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
|
|
|
|
int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
|
2003-01-16 19:35:57 +01:00
|
|
|
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "Frame Objects:\n";
|
|
|
|
|
2002-12-28 21:37:16 +01:00
|
|
|
for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
|
|
|
|
const StackObject &SO = Objects[i];
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << " fi#" << (int)(i-NumFixedObjects) << ": ";
|
2008-02-27 04:04:06 +01:00
|
|
|
if (SO.Size == ~0ULL) {
|
|
|
|
OS << "dead\n";
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-28 21:37:16 +01:00
|
|
|
if (SO.Size == 0)
|
|
|
|
OS << "variable sized";
|
|
|
|
else
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << "size=" << SO.Size;
|
|
|
|
OS << ", align=" << SO.Alignment;
|
2004-09-05 20:41:35 +02:00
|
|
|
|
2002-12-28 21:37:16 +01:00
|
|
|
if (i < NumFixedObjects)
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << ", fixed";
|
2002-12-28 21:37:16 +01:00
|
|
|
if (i < NumFixedObjects || SO.SPOffset != -1) {
|
2007-04-25 06:20:54 +02:00
|
|
|
int64_t Off = SO.SPOffset - ValOffset;
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << ", at location [SP";
|
2003-01-16 19:35:57 +01:00
|
|
|
if (Off > 0)
|
2004-09-05 20:41:35 +02:00
|
|
|
OS << "+" << Off;
|
2003-01-16 19:35:57 +01:00
|
|
|
else if (Off < 0)
|
2004-09-05 20:41:35 +02:00
|
|
|
OS << Off;
|
2002-12-28 21:37:16 +01:00
|
|
|
OS << "]";
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-16 19:35:57 +01:00
|
|
|
void MachineFrameInfo::dump(const MachineFunction &MF) const {
|
2009-08-23 03:12:47 +02:00
|
|
|
print(MF, errs());
|
2003-01-16 19:35:57 +01:00
|
|
|
}
|
2002-12-28 21:37:16 +01:00
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MachineJumpTableInfo implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// getJumpTableIndex - Create a new jump table entry in the jump table info
|
|
|
|
/// or return an existing one.
|
|
|
|
///
|
|
|
|
unsigned MachineJumpTableInfo::getJumpTableIndex(
|
2006-10-28 20:17:09 +02:00
|
|
|
const std::vector<MachineBasicBlock*> &DestBBs) {
|
2006-10-28 20:11:20 +02:00
|
|
|
assert(!DestBBs.empty() && "Cannot create an empty jump table!");
|
2006-04-22 20:53:45 +02:00
|
|
|
JumpTables.push_back(MachineJumpTableEntry(DestBBs));
|
|
|
|
return JumpTables.size()-1;
|
|
|
|
}
|
|
|
|
|
2009-04-15 03:18:49 +02:00
|
|
|
/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update
|
|
|
|
/// the jump tables to branch to New instead.
|
|
|
|
bool
|
|
|
|
MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old,
|
|
|
|
MachineBasicBlock *New) {
|
|
|
|
assert(Old != New && "Not making a change?");
|
|
|
|
bool MadeChange = false;
|
2009-11-14 21:09:13 +01: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.
|
|
|
|
bool
|
|
|
|
MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx,
|
|
|
|
MachineBasicBlock *Old,
|
|
|
|
MachineBasicBlock *New) {
|
|
|
|
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 03:18:49 +02:00
|
|
|
return MadeChange;
|
|
|
|
}
|
2006-04-22 20:53:45 +02:00
|
|
|
|
2009-08-23 03:12:47 +02:00
|
|
|
void MachineJumpTableInfo::print(raw_ostream &OS) const {
|
2009-10-31 21:19:03 +01:00
|
|
|
if (JumpTables.empty()) return;
|
|
|
|
|
|
|
|
OS << "Jump Tables:\n";
|
|
|
|
|
2006-04-22 20:53:45 +02:00
|
|
|
for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
|
2009-10-31 21:19:03 +01: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-22 20:53:45 +02:00
|
|
|
}
|
2009-10-31 21:19:03 +01:00
|
|
|
|
|
|
|
OS << '\n';
|
2006-04-22 20:53:45 +02:00
|
|
|
}
|
|
|
|
|
2009-08-23 03:12:47 +02:00
|
|
|
void MachineJumpTableInfo::dump() const { print(errs()); }
|
2006-04-22 20:53:45 +02:00
|
|
|
|
|
|
|
|
2003-01-13 01:23:03 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MachineConstantPool implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-09-14 07:50:57 +02:00
|
|
|
const Type *MachineConstantPoolEntry::getType() const {
|
|
|
|
if (isMachineConstantPoolEntry())
|
2009-07-22 01:34:23 +02:00
|
|
|
return Val.MachineCPVal->getType();
|
2006-09-14 07:50:57 +02:00
|
|
|
return Val.ConstVal->getType();
|
|
|
|
}
|
|
|
|
|
2009-07-22 01:34:23 +02:00
|
|
|
|
2009-07-22 01:36:01 +02:00
|
|
|
unsigned MachineConstantPoolEntry::getRelocationInfo() const {
|
2009-07-22 01:34:23 +02:00
|
|
|
if (isMachineConstantPoolEntry())
|
2009-07-22 01:36:01 +02:00
|
|
|
return Val.MachineCPVal->getRelocationInfo();
|
2009-07-22 02:05:44 +02:00
|
|
|
return Val.ConstVal->getRelocationInfo();
|
2009-07-22 01:34:23 +02:00
|
|
|
}
|
|
|
|
|
2006-09-12 23:00:35 +02: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 02:12:16 +01: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 15:12:53 +01:00
|
|
|
if (!CanShareConstantPoolEntry(AV->getOperand(i),
|
|
|
|
BV->getOperand(i), TD))
|
2009-10-28 02:12:16 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle other cases.
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-02-09 05:46:04 +01:00
|
|
|
/// getConstantPoolIndex - Create a new entry in the constant pool or return
|
2008-09-16 22:45:53 +02:00
|
|
|
/// an existing one. User must specify the log2 of the minimum required
|
|
|
|
/// alignment for the object.
|
2006-02-09 05:46:04 +01:00
|
|
|
///
|
|
|
|
unsigned MachineConstantPool::getConstantPoolIndex(Constant *C,
|
|
|
|
unsigned Alignment) {
|
|
|
|
assert(Alignment && "Alignment must be specified!");
|
|
|
|
if (Alignment > PoolAlignment) PoolAlignment = Alignment;
|
2009-10-28 02:12:16 +01:00
|
|
|
|
2006-02-09 05:46:04 +01: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 02:12:16 +01: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 05:46:04 +01:00
|
|
|
return i;
|
2009-10-28 02:12:16 +01:00
|
|
|
}
|
2006-02-09 05:46:04 +01:00
|
|
|
|
2009-03-13 08:51:59 +01:00
|
|
|
Constants.push_back(MachineConstantPoolEntry(C, Alignment));
|
2006-02-09 05:46:04 +01:00
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2006-09-12 23:00:35 +02: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 08:51:59 +01:00
|
|
|
|
|
|
|
Constants.push_back(MachineConstantPoolEntry(V, Alignment));
|
2006-09-12 23:00:35 +02:00
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2008-08-24 00:53:13 +02:00
|
|
|
void MachineConstantPool::print(raw_ostream &OS) const {
|
2009-10-31 21:19:03 +01:00
|
|
|
if (Constants.empty()) return;
|
|
|
|
|
|
|
|
OS << "Constant Pool:\n";
|
2006-01-31 23:23:14 +01:00
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << " cp#" << i << ": ";
|
2006-09-12 23:00:35 +02:00
|
|
|
if (Constants[i].isMachineConstantPoolEntry())
|
|
|
|
Constants[i].Val.MachineCPVal->print(OS);
|
|
|
|
else
|
|
|
|
OS << *(Value*)Constants[i].Val.ConstVal;
|
2009-10-31 21:19:03 +01:00
|
|
|
OS << ", align=" << Constants[i].getAlignment();
|
2006-01-31 23:23:14 +01:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2003-01-13 01:23:03 +01:00
|
|
|
}
|
|
|
|
|
2009-03-23 16:57:19 +01:00
|
|
|
void MachineConstantPool::dump() const { print(errs()); }
|