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"
|
2007-12-31 05:13:23 +01:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.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"
|
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"
|
2002-04-07 22:49:59 +02:00
|
|
|
#include "llvm/Function.h"
|
2004-07-29 19:30:56 +02:00
|
|
|
#include "llvm/Instructions.h"
|
2006-08-27 14:54:02 +02:00
|
|
|
#include "llvm/Support/Compiler.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"
|
2006-10-03 22:19:23 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2005-10-12 14:09:05 +02:00
|
|
|
#include "llvm/Config/config.h"
|
2004-07-08 02:47:58 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2003-12-20 10:17:07 +01:00
|
|
|
using namespace llvm;
|
2002-02-03 08:54:50 +01:00
|
|
|
|
2002-10-30 00:18:43 +01:00
|
|
|
static AnnotationID MF_AID(
|
2004-09-05 20:41:35 +02:00
|
|
|
AnnotationManager::getID("CodeGen::MachineCodeForFunction"));
|
2002-02-03 08:54:50 +01:00
|
|
|
|
2006-07-15 01:08:47 +02:00
|
|
|
// Out of line virtual function to home classes.
|
|
|
|
void MachineFunctionPass::virtfn() {}
|
2002-10-28 02:12:41 +01:00
|
|
|
|
|
|
|
namespace {
|
2006-06-29 00:17:39 +02:00
|
|
|
struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID;
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2004-01-30 22:53:46 +01:00
|
|
|
std::ostream *OS;
|
2004-02-01 06:25:07 +01:00
|
|
|
const std::string Banner;
|
2004-01-30 22:53:46 +01:00
|
|
|
|
2008-07-21 21:48:15 +02:00
|
|
|
Printer (std::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();
|
|
|
|
}
|
|
|
|
|
2003-12-20 11:20:58 +01:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) {
|
2004-01-30 22:53:46 +01:00
|
|
|
(*OS) << Banner;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-01-30 22:53:46 +01:00
|
|
|
/// Returns a newly-created MachineFunction Printer pass. The default output
|
|
|
|
/// stream is std::cerr; the default banner is empty.
|
|
|
|
///
|
|
|
|
FunctionPass *llvm::createMachineFunctionPrinterPass(std::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
|
|
|
}
|
|
|
|
|
2004-02-15 01:03:15 +01:00
|
|
|
namespace {
|
2006-06-29 00:17:39 +02:00
|
|
|
struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID;
|
2008-09-04 19:05:41 +02:00
|
|
|
Deleter() : MachineFunctionPass(&ID) {}
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2004-02-15 01:03:15 +01:00
|
|
|
const char *getPassName() const { return "Machine Code Deleter"; }
|
|
|
|
|
|
|
|
bool runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
// Delete the annotation from the function now.
|
|
|
|
MachineFunction::destruct(MF.getFunction());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2007-05-03 03:11:54 +02:00
|
|
|
char Deleter::ID = 0;
|
2004-02-15 01:03:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// MachineCodeDeletion Pass - This pass deletes all of the machine code for
|
|
|
|
/// the current function, which should happen after the function has been
|
|
|
|
/// emitted to a .s file or to memory.
|
|
|
|
FunctionPass *llvm::createMachineCodeDeleter() {
|
|
|
|
return new Deleter();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-10-28 02:12:41 +01:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// MachineFunction implementation
|
|
|
|
//===---------------------------------------------------------------------===//
|
2005-01-29 19:41:25 +01:00
|
|
|
|
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
|
|
|
|
2002-10-30 01:48:05 +01:00
|
|
|
MachineFunction::MachineFunction(const Function *F,
|
2002-12-28 21:37:16 +01:00
|
|
|
const TargetMachine &TM)
|
2007-04-26 00:10:09 +02:00
|
|
|
: Annotation(MF_AID), 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());
|
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();
|
2007-02-14 06:52:17 +01:00
|
|
|
unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
|
2007-01-20 23:35:55 +01:00
|
|
|
: TD.getPointerABIAlignment();
|
2008-07-08 01:14:23 +02:00
|
|
|
JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
|
|
|
|
MachineJumpTableInfo(EntrySize, Alignment);
|
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);
|
2008-10-13 14:37:16 +02:00
|
|
|
if (RegInfo)
|
|
|
|
RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo);
|
2008-07-08 01:14:23 +02:00
|
|
|
if (MFInfo) {
|
|
|
|
MFInfo->~MachineFunctionInfo(); Allocator.Deallocate(MFInfo);
|
|
|
|
}
|
|
|
|
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 *
|
|
|
|
MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID, bool NoImp) {
|
|
|
|
return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
|
|
|
|
MachineInstr(TID, NoImp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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) {
|
|
|
|
// Clear the instructions memoperands. This must be done manually because
|
|
|
|
// the instruction's parent pointer is now null, so it can't properly
|
|
|
|
// deallocate them on its own.
|
|
|
|
MI->clearMemOperands(*this);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::dump() const {
|
|
|
|
print(*cerr.stream());
|
|
|
|
}
|
2002-10-30 01:48:05 +01:00
|
|
|
|
|
|
|
void MachineFunction::print(std::ostream &OS) const {
|
2004-03-29 23:58:31 +02:00
|
|
|
OS << "# Machine code for " << 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
|
2008-08-24 00:53:13 +02:00
|
|
|
{
|
|
|
|
raw_os_ostream OSS(OS);
|
|
|
|
ConstantPool->print(OSS);
|
|
|
|
}
|
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()) {
|
2005-09-01 00:34:59 +02:00
|
|
|
OS << "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)
|
2008-02-26 22:47:57 +01:00
|
|
|
OS << " " << TRI->getName(I->first);
|
2005-09-01 00:34:59 +02:00
|
|
|
else
|
|
|
|
OS << " Reg #" << I->first;
|
2006-05-16 07:55:30 +02:00
|
|
|
|
|
|
|
if (I->second)
|
|
|
|
OS << " in VR#" << I->second << " ";
|
2005-09-01 00:34:59 +02:00
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
2008-10-13 14:37:16 +02:00
|
|
|
if (RegInfo && !RegInfo->liveout_empty()) {
|
2005-09-01 00:34:59 +02:00
|
|
|
OS << "Live Outs:";
|
2007-12-31 05:13:23 +01:00
|
|
|
for (MachineRegisterInfo::liveout_iterator
|
|
|
|
I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
|
2008-02-10 19:45:23 +01:00
|
|
|
if (TRI)
|
2008-02-26 22:47:57 +01:00
|
|
|
OS << " " << TRI->getName(*I);
|
2005-09-01 00:34:59 +02:00
|
|
|
else
|
|
|
|
OS << " Reg #" << *I;
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2004-02-13 05:39:55 +01:00
|
|
|
for (const_iterator BB = begin(); BB != end(); ++BB)
|
|
|
|
BB->print(OS);
|
2004-03-29 23:58:31 +02:00
|
|
|
|
|
|
|
OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
|
2002-10-30 01:48:05 +01:00
|
|
|
}
|
|
|
|
|
2004-07-08 02:47:58 +02:00
|
|
|
/// CFGOnly flag - This is used to control whether or not the CFG graph printer
|
|
|
|
/// prints out the contents of basic blocks or not. This is acceptable because
|
|
|
|
/// this code is only really used for debugging purposes.
|
|
|
|
///
|
|
|
|
static bool CFGOnly = false;
|
|
|
|
|
|
|
|
namespace llvm {
|
2004-09-05 20:41:35 +02:00
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(const MachineFunction *F) {
|
|
|
|
return "CFG for '" + F->getFunction()->getName() + "' function";
|
|
|
|
}
|
2004-07-08 02:47:58 +02:00
|
|
|
|
2004-09-05 20:41:35 +02:00
|
|
|
static std::string getNodeLabel(const MachineBasicBlock *Node,
|
|
|
|
const MachineFunction *Graph) {
|
|
|
|
if (CFGOnly && Node->getBasicBlock() &&
|
|
|
|
!Node->getBasicBlock()->getName().empty())
|
|
|
|
return Node->getBasicBlock()->getName() + ":";
|
2004-07-08 02:47:58 +02:00
|
|
|
|
2004-09-05 20:41:35 +02:00
|
|
|
std::ostringstream Out;
|
|
|
|
if (CFGOnly) {
|
|
|
|
Out << Node->getNumber() << ':';
|
|
|
|
return Out.str();
|
|
|
|
}
|
2004-07-08 02:47:58 +02:00
|
|
|
|
2004-09-05 20:41:35 +02:00
|
|
|
Node->print(Out);
|
2004-07-08 02:47:58 +02:00
|
|
|
|
2004-09-05 20:41:35 +02:00
|
|
|
std::string OutStr = Out.str();
|
|
|
|
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
|
2006-06-27 18:49:46 +02:00
|
|
|
ViewGraph(this, "mf" + getFunction()->getName());
|
|
|
|
#else
|
2006-12-07 21:28:15 +01:00
|
|
|
cerr << "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
|
|
|
|
{
|
|
|
|
CFGOnly = true;
|
|
|
|
viewCFG();
|
|
|
|
CFGOnly = false;
|
|
|
|
}
|
|
|
|
|
2002-02-03 08:54:50 +01:00
|
|
|
// The next two methods are used to construct and to retrieve
|
2002-04-07 22:49:59 +02:00
|
|
|
// the MachineCodeForFunction object for the given function.
|
|
|
|
// construct() -- Allocates and initializes for a given function and target
|
2002-02-03 08:54:50 +01:00
|
|
|
// get() -- Returns a handle to the object.
|
|
|
|
// This should not be called before "construct()"
|
2002-04-07 22:49:59 +02:00
|
|
|
// for a given Function.
|
2004-09-05 20:41:35 +02:00
|
|
|
//
|
2002-10-28 01:28:31 +01:00
|
|
|
MachineFunction&
|
2002-10-28 06:58:46 +01:00
|
|
|
MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
|
2002-03-18 04:36:30 +01:00
|
|
|
{
|
2002-10-30 00:18:43 +01:00
|
|
|
assert(Fn->getAnnotation(MF_AID) == 0 &&
|
2002-04-07 22:49:59 +02:00
|
|
|
"Object already exists for this function!");
|
2002-10-28 06:58:46 +01:00
|
|
|
MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
|
|
|
|
Fn->addAnnotation(mcInfo);
|
2002-02-03 08:54:50 +01:00
|
|
|
return *mcInfo;
|
|
|
|
}
|
|
|
|
|
2003-12-20 11:20:58 +01:00
|
|
|
void MachineFunction::destruct(const Function *Fn) {
|
2002-10-30 00:18:43 +01:00
|
|
|
bool Deleted = Fn->deleteAnnotation(MF_AID);
|
2008-04-06 23:46:45 +02:00
|
|
|
assert(Deleted && "Machine code did not exist for function!");
|
|
|
|
Deleted = Deleted; // silence warning when no assertions.
|
2002-02-03 08:54:50 +01:00
|
|
|
}
|
|
|
|
|
2002-10-28 06:58:46 +01:00
|
|
|
MachineFunction& MachineFunction::get(const Function *F)
|
2002-03-18 04:36:30 +01:00
|
|
|
{
|
2002-10-30 00:18:43 +01:00
|
|
|
MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
|
2002-02-03 08:54:50 +01:00
|
|
|
assert(mc && "Call construct() method first to allocate the object");
|
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
bool Immutable) {
|
|
|
|
assert(Size != 0 && "Cannot allocate zero size fixed stack objects!");
|
|
|
|
Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable));
|
|
|
|
return -++NumFixedObjects;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-16 19:35:57 +01:00
|
|
|
void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
|
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
|
|
|
|
2002-12-28 21:37:16 +01:00
|
|
|
for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
|
|
|
|
const StackObject &SO = Objects[i];
|
2008-10-15 04:57:38 +02: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
|
2005-05-14 00:54:44 +02:00
|
|
|
OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
|
|
|
|
OS << " alignment is " << SO.Alignment << " byte"
|
|
|
|
<< (SO.Alignment != 1 ? "s," : ",");
|
2004-09-05 20:41:35 +02:00
|
|
|
|
2002-12-28 21:37:16 +01:00
|
|
|
if (i < NumFixedObjects)
|
|
|
|
OS << " fixed";
|
|
|
|
if (i < NumFixedObjects || SO.SPOffset != -1) {
|
2007-04-25 06:20:54 +02:00
|
|
|
int64_t Off = SO.SPOffset - ValOffset;
|
2002-12-28 21:37:16 +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";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasVarSizedObjects)
|
|
|
|
OS << " Stack frame contains variable sized objects\n";
|
|
|
|
}
|
|
|
|
|
2003-01-16 19:35:57 +01:00
|
|
|
void MachineFrameInfo::dump(const MachineFunction &MF) const {
|
2006-12-07 21:28:15 +01:00
|
|
|
print(MF, *cerr.stream());
|
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
|
|
|
for (unsigned i = 0, e = JumpTables.size(); i != e; ++i)
|
|
|
|
if (JumpTables[i].MBBs == DestBBs)
|
|
|
|
return i;
|
|
|
|
|
|
|
|
JumpTables.push_back(MachineJumpTableEntry(DestBBs));
|
|
|
|
return JumpTables.size()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MachineJumpTableInfo::print(std::ostream &OS) const {
|
|
|
|
// FIXME: this is lame, maybe we could print out the MBB numbers or something
|
|
|
|
// like {1, 2, 4, 5, 3, 0}
|
|
|
|
for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) {
|
2008-10-15 04:57:38 +02:00
|
|
|
OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
|
2006-04-22 20:53:45 +02:00
|
|
|
<< " entries\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-07 21:28:15 +01:00
|
|
|
void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
|
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())
|
|
|
|
return Val.MachineCPVal->getType();
|
|
|
|
return Val.ConstVal->getType();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Check to see if we already have this constant.
|
|
|
|
//
|
|
|
|
// FIXME, this could be made much more efficient for large constant pools.
|
|
|
|
unsigned AlignMask = (1 << Alignment)-1;
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i)
|
2006-09-12 23:00:35 +02:00
|
|
|
if (Constants[i].Val.ConstVal == C && (Constants[i].Offset & AlignMask)== 0)
|
2006-02-09 05:46:04 +01:00
|
|
|
return i;
|
|
|
|
|
|
|
|
unsigned Offset = 0;
|
|
|
|
if (!Constants.empty()) {
|
2006-09-14 09:41:12 +02:00
|
|
|
Offset = Constants.back().getOffset();
|
2009-01-12 21:38:59 +01:00
|
|
|
Offset += TD->getTypePaddedSize(Constants.back().getType());
|
2006-02-09 05:46:04 +01:00
|
|
|
Offset = (Offset+AlignMask)&~AlignMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Constants.push_back(MachineConstantPoolEntry(C, Offset));
|
|
|
|
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.
|
|
|
|
unsigned AlignMask = (1 << Alignment)-1;
|
|
|
|
int Idx = V->getExistingMachineCPValue(this, Alignment);
|
|
|
|
if (Idx != -1)
|
|
|
|
return (unsigned)Idx;
|
|
|
|
|
|
|
|
unsigned Offset = 0;
|
|
|
|
if (!Constants.empty()) {
|
2006-09-14 09:41:12 +02:00
|
|
|
Offset = Constants.back().getOffset();
|
2009-01-12 21:38:59 +01:00
|
|
|
Offset += TD->getTypePaddedSize(Constants.back().getType());
|
2006-09-12 23:00:35 +02:00
|
|
|
Offset = (Offset+AlignMask)&~AlignMask;
|
|
|
|
}
|
|
|
|
|
|
|
|
Constants.push_back(MachineConstantPoolEntry(V, Offset));
|
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2008-08-24 00:53:13 +02:00
|
|
|
void MachineConstantPool::print(raw_ostream &OS) const {
|
2006-01-31 23:23:14 +01:00
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
2008-10-15 04:57:38 +02:00
|
|
|
OS << " <cp#" << i << "> is";
|
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;
|
2006-12-22 03:04:05 +01:00
|
|
|
OS << " , offset=" << Constants[i].getOffset();
|
2006-01-31 23:23:14 +01:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2003-01-13 01:23:03 +01:00
|
|
|
}
|
|
|
|
|
2008-08-24 00:53:13 +02:00
|
|
|
void MachineConstantPool::dump() const { print(errs()); errs().flush(); }
|