2002-10-28 01:16:38 +00:00
|
|
|
//===-- MachineFunction.cpp -----------------------------------------------===//
|
2004-09-05 18:41:35 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-09-05 18:41:35 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2004-09-05 18:41:35 +00:00
|
|
|
//
|
2002-10-28 01:16:38 +00: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 07:54:50 +00:00
|
|
|
|
2007-02-14 05:52:17 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineConstantPool.h"
|
2003-12-20 10:20:58 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2002-12-28 21:08:26 +00:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2006-04-22 18:53:45 +00:00
|
|
|
#include "llvm/CodeGen/MachineJumpTableInfo.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2003-12-20 10:20:58 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2006-05-12 06:33:49 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2002-02-03 07:54:50 +00:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2002-12-28 21:00:25 +00:00
|
|
|
#include "llvm/Target/TargetFrameInfo.h"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2006-08-27 12:54:02 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2006-10-03 20:19:23 +00:00
|
|
|
#include "llvm/Support/GraphWriter.h"
|
2008-08-23 22:23:09 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2006-10-03 20:19:23 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2005-10-12 12:09:05 +00:00
|
|
|
#include "llvm/Config/config.h"
|
2004-07-08 00:47:58 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2003-12-20 09:17:07 +00:00
|
|
|
using namespace llvm;
|
2002-02-03 07:54:50 +00:00
|
|
|
|
2009-04-13 05:44:34 +00:00
|
|
|
bool MachineFunctionPass::runOnFunction(Function &F) {
|
|
|
|
// Do not codegen any 'available_externally' functions at all, they have
|
|
|
|
// definitions outside the translation unit.
|
|
|
|
if (F.hasAvailableExternallyLinkage())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return runOnMachineFunction(MachineFunction::get(&F));
|
|
|
|
}
|
2002-10-28 01:12:41 +00:00
|
|
|
|
|
|
|
namespace {
|
2006-06-28 22:17:39 +00:00
|
|
|
struct VISIBILITY_HIDDEN Printer : public MachineFunctionPass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2004-01-30 21:53:46 +00:00
|
|
|
std::ostream *OS;
|
2004-02-01 05:25:07 +00:00
|
|
|
const std::string Banner;
|
2004-01-30 21:53:46 +00:00
|
|
|
|
2008-07-21 19:48:15 +00:00
|
|
|
Printer (std::ostream *os, const std::string &banner)
|
2008-09-04 17:05:41 +00:00
|
|
|
: MachineFunctionPass(&ID), OS(os), Banner(banner) {}
|
2004-01-30 21:53:46 +00:00
|
|
|
|
2002-10-30 00:48:05 +00:00
|
|
|
const char *getPassName() const { return "MachineFunction Printer"; }
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
|
2003-12-20 10:20:58 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) {
|
2004-01-30 21:53:46 +00:00
|
|
|
(*OS) << Banner;
|
|
|
|
MF.print (*OS);
|
2002-10-30 00:48:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2007-05-03 01:11:54 +00:00
|
|
|
char Printer::ID = 0;
|
2002-10-28 01:12:41 +00:00
|
|
|
}
|
|
|
|
|
2004-01-30 21:53:46 +00: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 22:13:58 +00:00
|
|
|
const std::string &Banner){
|
2004-01-30 21:53:46 +00:00
|
|
|
return new Printer(OS, Banner);
|
2002-10-30 00:48:05 +00:00
|
|
|
}
|
|
|
|
|
2004-02-15 00:03:15 +00:00
|
|
|
namespace {
|
2006-06-28 22:17:39 +00:00
|
|
|
struct VISIBILITY_HIDDEN Deleter : public MachineFunctionPass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2008-09-04 17:05:41 +00:00
|
|
|
Deleter() : MachineFunctionPass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2004-02-15 00:03:15 +00: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 01:11:54 +00:00
|
|
|
char Deleter::ID = 0;
|
2004-02-15 00:03:15 +00: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 01:12:41 +00:00
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
// MachineFunction implementation
|
|
|
|
//===---------------------------------------------------------------------===//
|
2005-01-29 18:41:25 +00:00
|
|
|
|
2008-07-28 21:51:04 +00:00
|
|
|
void ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) {
|
2008-07-07 23:14:23 +00:00
|
|
|
MBB->getParent()->DeleteMachineBasicBlock(MBB);
|
2004-05-24 06:11:51 +00:00
|
|
|
}
|
2002-10-28 01:12:41 +00:00
|
|
|
|
2002-10-30 00:48:05 +00:00
|
|
|
MachineFunction::MachineFunction(const Function *F,
|
2002-12-28 20:37:16 +00:00
|
|
|
const TargetMachine &TM)
|
2009-05-14 19:17:24 +00:00
|
|
|
: Annotation(AnnotationManager::getID("CodeGen::MachineCodeForFunction")),
|
|
|
|
Fn(F), Target(TM) {
|
2008-10-13 12:37:16 +00:00
|
|
|
if (TM.getRegisterInfo())
|
|
|
|
RegInfo = new (Allocator.Allocate<MachineRegisterInfo>())
|
|
|
|
MachineRegisterInfo(*TM.getRegisterInfo());
|
|
|
|
else
|
|
|
|
RegInfo = 0;
|
2004-08-16 22:36:34 +00:00
|
|
|
MFInfo = 0;
|
2008-07-07 23:14:23 +00:00
|
|
|
FrameInfo = new (Allocator.Allocate<MachineFrameInfo>())
|
|
|
|
MachineFrameInfo(*TM.getFrameInfo());
|
|
|
|
ConstantPool = new (Allocator.Allocate<MachineConstantPool>())
|
|
|
|
MachineConstantPool(TM.getTargetData());
|
2006-12-14 19:17:33 +00: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 05:52:17 +00:00
|
|
|
unsigned Alignment = IsPic ? TD.getABITypeAlignment(Type::Int32Ty)
|
2007-01-20 22:35:55 +00:00
|
|
|
: TD.getPointerABIAlignment();
|
2008-07-07 23:14:23 +00:00
|
|
|
JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>())
|
|
|
|
MachineJumpTableInfo(EntrySize, Alignment);
|
2002-12-25 05:03:22 +00:00
|
|
|
}
|
|
|
|
|
2004-09-05 18:41:35 +00:00
|
|
|
MachineFunction::~MachineFunction() {
|
2004-07-01 06:29:07 +00:00
|
|
|
BasicBlocks.clear();
|
2008-07-07 23:14:23 +00:00
|
|
|
InstructionRecycler.clear(Allocator);
|
|
|
|
BasicBlockRecycler.clear(Allocator);
|
2008-10-13 12:37:16 +00:00
|
|
|
if (RegInfo)
|
|
|
|
RegInfo->~MachineRegisterInfo(); Allocator.Deallocate(RegInfo);
|
2008-07-07 23:14:23 +00: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 00:48:05 +00:00
|
|
|
}
|
|
|
|
|
2006-10-03 19:18:57 +00: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 20:19:23 +00:00
|
|
|
if (MBBI != begin())
|
|
|
|
BlockNo = prior(MBBI)->getNumber()+1;
|
2006-10-03 19:18:57 +00: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-07 23:14:23 +00:00
|
|
|
/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead
|
|
|
|
/// of `new MachineInstr'.
|
|
|
|
///
|
|
|
|
MachineInstr *
|
2009-02-03 00:55:04 +00:00
|
|
|
MachineFunction::CreateMachineInstr(const TargetInstrDesc &TID,
|
|
|
|
DebugLoc DL, bool NoImp) {
|
2008-07-07 23:14:23 +00:00
|
|
|
return new (InstructionRecycler.Allocate<MachineInstr>(Allocator))
|
2009-02-03 00:55:04 +00:00
|
|
|
MachineInstr(TID, DL, NoImp);
|
2008-07-07 23:14:23 +00: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) {
|
|
|
|
// 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 19:18:57 +00:00
|
|
|
|
2008-07-07 23:14:23 +00: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 00:48:05 +00:00
|
|
|
|
|
|
|
void MachineFunction::print(std::ostream &OS) const {
|
2004-03-29 21:58:31 +00:00
|
|
|
OS << "# Machine code for " << Fn->getName () << "():\n";
|
2002-12-28 20:37:16 +00:00
|
|
|
|
|
|
|
// Print Frame Information
|
2008-07-07 23:14:23 +00:00
|
|
|
FrameInfo->print(*this, OS);
|
2006-04-22 18:53:45 +00:00
|
|
|
|
|
|
|
// Print JumpTable Information
|
2008-07-07 23:14:23 +00:00
|
|
|
JumpTableInfo->print(OS);
|
2003-01-13 00:23:03 +00:00
|
|
|
|
|
|
|
// Print Constant Pool
|
2008-08-23 22:53:13 +00:00
|
|
|
{
|
|
|
|
raw_os_ostream OSS(OS);
|
|
|
|
ConstantPool->print(OSS);
|
|
|
|
}
|
2005-08-31 22:34:59 +00:00
|
|
|
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo *TRI = getTarget().getRegisterInfo();
|
2005-08-31 22:34:59 +00:00
|
|
|
|
2008-10-13 12:37:16 +00:00
|
|
|
if (RegInfo && !RegInfo->livein_empty()) {
|
2005-08-31 22:34:59 +00:00
|
|
|
OS << "Live Ins:";
|
2007-12-31 04:13:23 +00:00
|
|
|
for (MachineRegisterInfo::livein_iterator
|
|
|
|
I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) {
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TRI)
|
2008-02-26 21:47:57 +00:00
|
|
|
OS << " " << TRI->getName(I->first);
|
2005-08-31 22:34:59 +00:00
|
|
|
else
|
|
|
|
OS << " Reg #" << I->first;
|
2006-05-16 05:55:30 +00:00
|
|
|
|
|
|
|
if (I->second)
|
|
|
|
OS << " in VR#" << I->second << " ";
|
2005-08-31 22:34:59 +00:00
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
2008-10-13 12:37:16 +00:00
|
|
|
if (RegInfo && !RegInfo->liveout_empty()) {
|
2005-08-31 22:34:59 +00:00
|
|
|
OS << "Live Outs:";
|
2007-12-31 04:13:23 +00:00
|
|
|
for (MachineRegisterInfo::liveout_iterator
|
|
|
|
I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I)
|
2008-02-10 18:45:23 +00:00
|
|
|
if (TRI)
|
2008-02-26 21:47:57 +00:00
|
|
|
OS << " " << TRI->getName(*I);
|
2005-08-31 22:34:59 +00:00
|
|
|
else
|
|
|
|
OS << " Reg #" << *I;
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2004-02-13 04:39:55 +00:00
|
|
|
for (const_iterator BB = begin(); BB != end(); ++BB)
|
|
|
|
BB->print(OS);
|
2004-03-29 21:58:31 +00:00
|
|
|
|
|
|
|
OS << "\n# End machine code for " << Fn->getName () << "().\n\n";
|
2002-10-30 00:48:05 +00:00
|
|
|
}
|
|
|
|
|
2004-07-08 00:47:58 +00:00
|
|
|
namespace llvm {
|
2004-09-05 18:41:35 +00: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 00:47:58 +00:00
|
|
|
|
2004-09-05 18:41:35 +00:00
|
|
|
static std::string getNodeLabel(const MachineBasicBlock *Node,
|
2009-06-24 17:37:09 +00:00
|
|
|
const MachineFunction *Graph,
|
|
|
|
bool ShortNames) {
|
|
|
|
if (ShortNames && Node->getBasicBlock() &&
|
2004-09-05 18:41:35 +00:00
|
|
|
!Node->getBasicBlock()->getName().empty())
|
|
|
|
return Node->getBasicBlock()->getName() + ":";
|
2004-07-08 00:47:58 +00:00
|
|
|
|
2004-09-05 18:41:35 +00:00
|
|
|
std::ostringstream Out;
|
2009-06-24 17:37:09 +00:00
|
|
|
if (ShortNames) {
|
2004-09-05 18:41:35 +00:00
|
|
|
Out << Node->getNumber() << ':';
|
|
|
|
return Out.str();
|
|
|
|
}
|
2004-07-08 00:47:58 +00:00
|
|
|
|
2004-09-05 18:41:35 +00:00
|
|
|
Node->print(Out);
|
2004-07-08 00:47:58 +00:00
|
|
|
|
2004-09-05 18:41:35 +00:00
|
|
|
std::string OutStr = Out.str();
|
|
|
|
if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
|
2004-07-08 00:47:58 +00:00
|
|
|
|
2004-09-05 18:41:35 +00: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 00:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::viewCFG() const
|
|
|
|
{
|
2005-10-12 12:09:05 +00:00
|
|
|
#ifndef NDEBUG
|
2006-06-27 16:49:46 +00:00
|
|
|
ViewGraph(this, "mf" + getFunction()->getName());
|
|
|
|
#else
|
2006-12-07 20:28:15 +00:00
|
|
|
cerr << "SelectionDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
2006-06-27 16:49:46 +00:00
|
|
|
#endif // NDEBUG
|
2004-07-08 00:47:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MachineFunction::viewCFGOnly() const
|
|
|
|
{
|
2009-06-24 17:37:09 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
ViewGraph(this, "mf" + getFunction()->getName(), true);
|
|
|
|
#else
|
|
|
|
cerr << "SelectionDAG::viewGraph is only available in debug builds on "
|
|
|
|
<< "systems with Graphviz or gv!\n";
|
|
|
|
#endif // NDEBUG
|
2004-07-08 00:47:58 +00:00
|
|
|
}
|
|
|
|
|
2002-02-03 07:54:50 +00:00
|
|
|
// The next two methods are used to construct and to retrieve
|
2002-04-07 20:49:59 +00:00
|
|
|
// the MachineCodeForFunction object for the given function.
|
|
|
|
// construct() -- Allocates and initializes for a given function and target
|
2002-02-03 07:54:50 +00:00
|
|
|
// get() -- Returns a handle to the object.
|
|
|
|
// This should not be called before "construct()"
|
2002-04-07 20:49:59 +00:00
|
|
|
// for a given Function.
|
2004-09-05 18:41:35 +00:00
|
|
|
//
|
2002-10-28 00:28:31 +00:00
|
|
|
MachineFunction&
|
2002-10-28 05:58:46 +00:00
|
|
|
MachineFunction::construct(const Function *Fn, const TargetMachine &Tar)
|
2002-03-18 03:36:30 +00:00
|
|
|
{
|
2009-05-14 19:17:24 +00:00
|
|
|
AnnotationID MF_AID =
|
|
|
|
AnnotationManager::getID("CodeGen::MachineCodeForFunction");
|
2002-10-29 23:18:43 +00:00
|
|
|
assert(Fn->getAnnotation(MF_AID) == 0 &&
|
2002-04-07 20:49:59 +00:00
|
|
|
"Object already exists for this function!");
|
2002-10-28 05:58:46 +00:00
|
|
|
MachineFunction* mcInfo = new MachineFunction(Fn, Tar);
|
|
|
|
Fn->addAnnotation(mcInfo);
|
2002-02-03 07:54:50 +00:00
|
|
|
return *mcInfo;
|
|
|
|
}
|
|
|
|
|
2003-12-20 10:20:58 +00:00
|
|
|
void MachineFunction::destruct(const Function *Fn) {
|
2009-05-14 19:17:24 +00:00
|
|
|
AnnotationID MF_AID =
|
|
|
|
AnnotationManager::getID("CodeGen::MachineCodeForFunction");
|
2002-10-29 23:18:43 +00:00
|
|
|
bool Deleted = Fn->deleteAnnotation(MF_AID);
|
2008-04-06 21:46:45 +00:00
|
|
|
assert(Deleted && "Machine code did not exist for function!");
|
|
|
|
Deleted = Deleted; // silence warning when no assertions.
|
2002-02-03 07:54:50 +00:00
|
|
|
}
|
|
|
|
|
2002-10-28 05:58:46 +00:00
|
|
|
MachineFunction& MachineFunction::get(const Function *F)
|
2002-03-18 03:36:30 +00:00
|
|
|
{
|
2009-05-14 19:17:24 +00:00
|
|
|
AnnotationID MF_AID =
|
|
|
|
AnnotationManager::getID("CodeGen::MachineCodeForFunction");
|
2002-10-29 23:18:43 +00:00
|
|
|
MachineFunction *mc = (MachineFunction*)F->getAnnotation(MF_AID);
|
2002-02-03 07:54:50 +00:00
|
|
|
assert(mc && "Call construct() method first to allocate the object");
|
|
|
|
return *mc;
|
|
|
|
}
|
|
|
|
|
2009-04-20 18:36:57 +00: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-01-27 21:15:07 +00:00
|
|
|
/// getOrCreateDebugLocID - Look up the DebugLocTuple index with the given
|
2009-02-03 22:49:58 +00:00
|
|
|
/// source file, line, and column. If none currently exists, create a new
|
|
|
|
/// DebugLocTuple, and insert it into the DebugIdMap.
|
2009-04-30 23:22:31 +00:00
|
|
|
unsigned MachineFunction::getOrCreateDebugLocID(GlobalVariable *CompileUnit,
|
|
|
|
unsigned Line, unsigned Col) {
|
2009-05-21 00:04:55 +00:00
|
|
|
DebugLocTuple Tuple(CompileUnit, Line, Col);
|
2009-01-27 21:15:07 +00:00
|
|
|
DenseMap<DebugLocTuple, unsigned>::iterator II
|
|
|
|
= DebugLocInfo.DebugIdMap.find(Tuple);
|
2009-01-26 07:41:49 +00:00
|
|
|
if (II != DebugLocInfo.DebugIdMap.end())
|
|
|
|
return II->second;
|
|
|
|
// Add a new tuple.
|
2009-01-26 23:47:30 +00:00
|
|
|
unsigned Id = DebugLocInfo.DebugLocations.size();
|
2009-01-26 07:41:49 +00:00
|
|
|
DebugLocInfo.DebugLocations.push_back(Tuple);
|
2009-01-26 23:47:30 +00:00
|
|
|
DebugLocInfo.DebugIdMap[Tuple] = Id;
|
|
|
|
return Id;
|
2009-01-26 07:41:49 +00:00
|
|
|
}
|
|
|
|
|
2009-02-03 22:49:58 +00:00
|
|
|
/// getDebugLocTuple - Get the DebugLocTuple for a given DebugLoc object.
|
2009-02-04 00:05:34 +00:00
|
|
|
DebugLocTuple MachineFunction::getDebugLocTuple(DebugLoc DL) const {
|
2009-02-03 22:55:54 +00:00
|
|
|
unsigned Idx = DL.getIndex();
|
2009-02-03 22:49:58 +00:00
|
|
|
assert(Idx < DebugLocInfo.DebugLocations.size() &&
|
|
|
|
"Invalid index into debug locations!");
|
|
|
|
return DebugLocInfo.DebugLocations[Idx];
|
|
|
|
}
|
|
|
|
|
2002-12-28 20:37:16 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-28 21:08:26 +00:00
|
|
|
// MachineFrameInfo implementation
|
2002-12-28 20:37:16 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-25 07:19:06 +00: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 18:35:57 +00:00
|
|
|
void MachineFrameInfo::print(const MachineFunction &MF, std::ostream &OS) const{
|
2008-11-03 11:16:43 +00:00
|
|
|
const TargetFrameInfo *FI = MF.getTarget().getFrameInfo();
|
|
|
|
int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0);
|
2003-01-16 18:35:57 +00:00
|
|
|
|
2002-12-28 20:37:16 +00:00
|
|
|
for (unsigned i = 0, e = Objects.size(); i != e; ++i) {
|
|
|
|
const StackObject &SO = Objects[i];
|
2008-10-15 02:57:38 +00:00
|
|
|
OS << " <fi#" << (int)(i-NumFixedObjects) << ">: ";
|
2008-02-27 03:04:06 +00:00
|
|
|
if (SO.Size == ~0ULL) {
|
|
|
|
OS << "dead\n";
|
|
|
|
continue;
|
|
|
|
}
|
2002-12-28 20:37:16 +00:00
|
|
|
if (SO.Size == 0)
|
|
|
|
OS << "variable sized";
|
|
|
|
else
|
2005-05-13 22:54:44 +00:00
|
|
|
OS << "size is " << SO.Size << " byte" << (SO.Size != 1 ? "s," : ",");
|
|
|
|
OS << " alignment is " << SO.Alignment << " byte"
|
|
|
|
<< (SO.Alignment != 1 ? "s," : ",");
|
2004-09-05 18:41:35 +00:00
|
|
|
|
2002-12-28 20:37:16 +00:00
|
|
|
if (i < NumFixedObjects)
|
|
|
|
OS << " fixed";
|
|
|
|
if (i < NumFixedObjects || SO.SPOffset != -1) {
|
2007-04-25 04:20:54 +00:00
|
|
|
int64_t Off = SO.SPOffset - ValOffset;
|
2002-12-28 20:37:16 +00:00
|
|
|
OS << " at location [SP";
|
2003-01-16 18:35:57 +00:00
|
|
|
if (Off > 0)
|
2004-09-05 18:41:35 +00:00
|
|
|
OS << "+" << Off;
|
2003-01-16 18:35:57 +00:00
|
|
|
else if (Off < 0)
|
2004-09-05 18:41:35 +00:00
|
|
|
OS << Off;
|
2002-12-28 20:37:16 +00:00
|
|
|
OS << "]";
|
|
|
|
}
|
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasVarSizedObjects)
|
|
|
|
OS << " Stack frame contains variable sized objects\n";
|
|
|
|
}
|
|
|
|
|
2003-01-16 18:35:57 +00:00
|
|
|
void MachineFrameInfo::dump(const MachineFunction &MF) const {
|
2006-12-07 20:28:15 +00:00
|
|
|
print(MF, *cerr.stream());
|
2003-01-16 18:35:57 +00:00
|
|
|
}
|
2002-12-28 20:37:16 +00:00
|
|
|
|
|
|
|
|
2006-04-22 18:53:45 +00: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 18:17:09 +00:00
|
|
|
const std::vector<MachineBasicBlock*> &DestBBs) {
|
2006-10-28 18:11:20 +00:00
|
|
|
assert(!DestBBs.empty() && "Cannot create an empty jump table!");
|
2006-04-22 18:53:45 +00: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;
|
|
|
|
}
|
|
|
|
|
2009-04-15 01:18:49 +00: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;
|
|
|
|
for (size_t i = 0, e = JumpTables.size(); i != e; ++i) {
|
|
|
|
MachineJumpTableEntry &JTE = JumpTables[i];
|
|
|
|
for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j)
|
|
|
|
if (JTE.MBBs[j] == Old) {
|
|
|
|
JTE.MBBs[j] = New;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MadeChange;
|
|
|
|
}
|
2006-04-22 18:53:45 +00:00
|
|
|
|
|
|
|
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 02:57:38 +00:00
|
|
|
OS << " <jt#" << i << "> has " << JumpTables[i].MBBs.size()
|
2006-04-22 18:53:45 +00:00
|
|
|
<< " entries\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-07 20:28:15 +00:00
|
|
|
void MachineJumpTableInfo::dump() const { print(*cerr.stream()); }
|
2006-04-22 18:53:45 +00:00
|
|
|
|
|
|
|
|
2003-01-13 00:23:03 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MachineConstantPool implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-09-14 05:50:57 +00:00
|
|
|
const Type *MachineConstantPoolEntry::getType() const {
|
|
|
|
if (isMachineConstantPoolEntry())
|
|
|
|
return Val.MachineCPVal->getType();
|
|
|
|
return Val.ConstVal->getType();
|
|
|
|
}
|
|
|
|
|
2006-09-12 21:00:35 +00: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 04:46:04 +00:00
|
|
|
/// getConstantPoolIndex - Create a new entry in the constant pool or return
|
2008-09-16 20:45:53 +00:00
|
|
|
/// an existing one. User must specify the log2 of the minimum required
|
|
|
|
/// alignment for the object.
|
2006-02-09 04:46:04 +00: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.
|
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i)
|
2009-03-13 07:51:59 +00:00
|
|
|
if (Constants[i].Val.ConstVal == C &&
|
|
|
|
(Constants[i].getAlignment() & (Alignment - 1)) == 0)
|
2006-02-09 04:46:04 +00:00
|
|
|
return i;
|
|
|
|
|
2009-03-13 07:51:59 +00:00
|
|
|
Constants.push_back(MachineConstantPoolEntry(C, Alignment));
|
2006-02-09 04:46:04 +00:00
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2006-09-12 21:00:35 +00: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 07:51:59 +00:00
|
|
|
|
|
|
|
Constants.push_back(MachineConstantPoolEntry(V, Alignment));
|
2006-09-12 21:00:35 +00:00
|
|
|
return Constants.size()-1;
|
|
|
|
}
|
|
|
|
|
2008-08-23 22:53:13 +00:00
|
|
|
void MachineConstantPool::print(raw_ostream &OS) const {
|
2006-01-31 22:23:14 +00:00
|
|
|
for (unsigned i = 0, e = Constants.size(); i != e; ++i) {
|
2008-10-15 02:57:38 +00:00
|
|
|
OS << " <cp#" << i << "> is";
|
2006-09-12 21:00:35 +00:00
|
|
|
if (Constants[i].isMachineConstantPoolEntry())
|
|
|
|
Constants[i].Val.MachineCPVal->print(OS);
|
|
|
|
else
|
|
|
|
OS << *(Value*)Constants[i].Val.ConstVal;
|
2009-03-13 07:51:59 +00:00
|
|
|
OS << " , alignment=" << Constants[i].getAlignment();
|
2006-01-31 22:23:14 +00:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2003-01-13 00:23:03 +00:00
|
|
|
}
|
|
|
|
|
2009-03-23 15:57:19 +00:00
|
|
|
void MachineConstantPool::dump() const { print(errs()); }
|