MachineFunction.cpp revision 205218
117680Spst//===-- MachineFunction.cpp -----------------------------------------------===// 239300Sfenner// 317680Spst// The LLVM Compiler Infrastructure 417680Spst// 517680Spst// This file is distributed under the University of Illinois Open Source 617680Spst// License. See LICENSE.TXT for details. 717680Spst// 817680Spst//===----------------------------------------------------------------------===// 917680Spst// 1017680Spst// Collect native machine code information for a function. This allows 1117680Spst// target-specific information about the generated code to be stored with each 1217680Spst// function. 1317680Spst// 1417680Spst//===----------------------------------------------------------------------===// 1517680Spst 1617680Spst#include "llvm/DerivedTypes.h" 1717680Spst#include "llvm/Function.h" 1817680Spst#include "llvm/Instructions.h" 1917680Spst#include "llvm/Config/config.h" 2056896Sfenner#include "llvm/CodeGen/MachineConstantPool.h" 2156896Sfenner#include "llvm/CodeGen/MachineFunction.h" 2217680Spst#include "llvm/CodeGen/MachineFunctionPass.h" 2317680Spst#include "llvm/CodeGen/MachineFrameInfo.h" 2417680Spst#include "llvm/CodeGen/MachineInstr.h" 2526183Sfenner#include "llvm/CodeGen/MachineJumpTableInfo.h" 2680234Sfenner#include "llvm/CodeGen/MachineRegisterInfo.h" 2717680Spst#include "llvm/CodeGen/Passes.h" 2817680Spst#include "llvm/MC/MCAsmInfo.h" 2956896Sfenner#include "llvm/MC/MCContext.h" 3056896Sfenner#include "llvm/Analysis/DebugInfo.h" 3156896Sfenner#include "llvm/Support/Debug.h" 3256896Sfenner#include "llvm/Target/TargetData.h" 3317680Spst#include "llvm/Target/TargetLowering.h" 3417680Spst#include "llvm/Target/TargetMachine.h" 3517680Spst#include "llvm/Target/TargetFrameInfo.h" 3617680Spst#include "llvm/ADT/SmallString.h" 3717680Spst#include "llvm/ADT/STLExtras.h" 3839300Sfenner#include "llvm/Support/GraphWriter.h" 3917680Spst#include "llvm/Support/raw_ostream.h" 4039300Sfennerusing namespace llvm; 4139300Sfenner 4217680Spstnamespace { 4339300Sfenner struct Printer : public MachineFunctionPass { 4475118Sfenner static char ID; 4517680Spst 4617680Spst raw_ostream &OS; 4756896Sfenner const std::string Banner; 4817680Spst 4917680Spst Printer(raw_ostream &os, const std::string &banner) 5017680Spst : MachineFunctionPass(&ID), OS(os), Banner(banner) {} 5117680Spst 5217680Spst const char *getPassName() const { return "MachineFunction Printer"; } 5317680Spst 5417680Spst virtual void getAnalysisUsage(AnalysisUsage &AU) const { 5517680Spst AU.setPreservesAll(); 5617680Spst MachineFunctionPass::getAnalysisUsage(AU); 5717680Spst } 5817680Spst 5917680Spst bool runOnMachineFunction(MachineFunction &MF) { 6017680Spst OS << "# " << Banner << ":\n"; 6117680Spst MF.print(OS); 6217680Spst return false; 6317680Spst } 6417680Spst }; 6517680Spst char Printer::ID = 0; 6617680Spst} 6717680Spst 6817680Spst/// Returns a newly-created MachineFunction Printer pass. The default banner is 6917680Spst/// empty. 7017680Spst/// 7117680SpstFunctionPass *llvm::createMachineFunctionPrinterPass(raw_ostream &OS, 7217680Spst const std::string &Banner){ 7317680Spst return new Printer(OS, Banner); 7417680Spst} 7517680Spst 7617680Spst//===----------------------------------------------------------------------===// 7717680Spst// MachineFunction implementation 7817680Spst//===----------------------------------------------------------------------===// 7917680Spst 8017680Spst// Out of line virtual method. 8117680SpstMachineFunctionInfo::~MachineFunctionInfo() {} 8217680Spst 8317680Spstvoid ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 8417680Spst MBB->getParent()->DeleteMachineBasicBlock(MBB); 8517680Spst} 8617680Spst 8717680SpstMachineFunction::MachineFunction(Function *F, const TargetMachine &TM, 8817680Spst unsigned FunctionNum, MCContext &ctx) 8917680Spst : Fn(F), Target(TM), Ctx(ctx) { 9017680Spst if (TM.getRegisterInfo()) 9117680Spst RegInfo = new (Allocator.Allocate<MachineRegisterInfo>()) 9217680Spst MachineRegisterInfo(*TM.getRegisterInfo()); 9317680Spst else 9417680Spst RegInfo = 0; 9517680Spst MFInfo = 0; 9656896Sfenner FrameInfo = new (Allocator.Allocate<MachineFrameInfo>()) 9756896Sfenner MachineFrameInfo(*TM.getFrameInfo()); 9856896Sfenner if (Fn->hasFnAttr(Attribute::StackAlignment)) 9956896Sfenner FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs( 10056896Sfenner Fn->getAttributes().getFnAttributes())); 10156896Sfenner ConstantPool = new (Allocator.Allocate<MachineConstantPool>()) 10256896Sfenner MachineConstantPool(TM.getTargetData()); 10356896Sfenner Alignment = TM.getTargetLowering()->getFunctionAlignment(F); 10456896Sfenner FunctionNumber = FunctionNum; 10556896Sfenner JumpTableInfo = 0; 10656896Sfenner} 10756896Sfenner 10856896SfennerMachineFunction::~MachineFunction() { 10956896Sfenner BasicBlocks.clear(); 11056896Sfenner InstructionRecycler.clear(Allocator); 11156896Sfenner BasicBlockRecycler.clear(Allocator); 11256896Sfenner if (RegInfo) { 11356896Sfenner RegInfo->~MachineRegisterInfo(); 11456896Sfenner Allocator.Deallocate(RegInfo); 11575118Sfenner } 11656896Sfenner if (MFInfo) { 11775118Sfenner MFInfo->~MachineFunctionInfo(); 11875118Sfenner Allocator.Deallocate(MFInfo); 11975118Sfenner } 12017680Spst FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo); 12175118Sfenner ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool); 12275118Sfenner 12375118Sfenner if (JumpTableInfo) { 12475118Sfenner JumpTableInfo->~MachineJumpTableInfo(); 12517680Spst Allocator.Deallocate(JumpTableInfo); 12617680Spst } 12717680Spst} 12817680Spst 12917680Spst/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it 13017680Spst/// does already exist, allocate one. 13117680SpstMachineJumpTableInfo *MachineFunction:: 13217680SpstgetOrCreateJumpTableInfo(unsigned EntryKind) { 13317680Spst if (JumpTableInfo) return JumpTableInfo; 13417680Spst 13517680Spst JumpTableInfo = new (Allocator.Allocate<MachineJumpTableInfo>()) 13617680Spst MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 13717680Spst return JumpTableInfo; 13817680Spst} 13917680Spst 14017680Spst/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and 14117680Spst/// recomputes them. This guarantees that the MBB numbers are sequential, 14217680Spst/// dense, and match the ordering of the blocks within the function. If a 14317680Spst/// specific MachineBasicBlock is specified, only that block and those after 14417680Spst/// it are renumbered. 14517680Spstvoid MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 14617680Spst if (empty()) { MBBNumbering.clear(); return; } 14717680Spst MachineFunction::iterator MBBI, E = end(); 14817680Spst if (MBB == 0) 14917680Spst MBBI = begin(); 15017680Spst else 15117680Spst MBBI = MBB; 15217680Spst 15317680Spst // Figure out the block number this should have. 15417680Spst unsigned BlockNo = 0; 15517680Spst if (MBBI != begin()) 15617680Spst BlockNo = prior(MBBI)->getNumber()+1; 15717680Spst 15817680Spst for (; MBBI != E; ++MBBI, ++BlockNo) { 15917680Spst if (MBBI->getNumber() != (int)BlockNo) { 16075118Sfenner // Remove use of the old number. 16175118Sfenner if (MBBI->getNumber() != -1) { 16217680Spst assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 16375118Sfenner "MBB number mismatch!"); 16475118Sfenner MBBNumbering[MBBI->getNumber()] = 0; 16575118Sfenner } 16675118Sfenner 16775118Sfenner // If BlockNo is already taken, set that block's number to -1. 16875118Sfenner if (MBBNumbering[BlockNo]) 16975118Sfenner MBBNumbering[BlockNo]->setNumber(-1); 17075118Sfenner 17175118Sfenner MBBNumbering[BlockNo] = MBBI; 17275118Sfenner MBBI->setNumber(BlockNo); 17375118Sfenner } 17475118Sfenner } 17575118Sfenner 17617680Spst // Okay, all the blocks are renumbered. If we have compactified the block 17717680Spst // numbering, shrink MBBNumbering now. 17817680Spst assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 17917680Spst MBBNumbering.resize(BlockNo); 18017680Spst} 18117680Spst 18217680Spst/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead 18375118Sfenner/// of `new MachineInstr'. 18417680Spst/// 18575118SfennerMachineInstr * 18675118SfennerMachineFunction::CreateMachineInstr(const TargetInstrDesc &TID, 18775118Sfenner DebugLoc DL, bool NoImp) { 18875118Sfenner return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 18975118Sfenner MachineInstr(TID, DL, NoImp); 19075118Sfenner} 19175118Sfenner 19275118Sfenner/// CloneMachineInstr - Create a new MachineInstr which is a copy of the 19375118Sfenner/// 'Orig' instruction, identical in all ways except the instruction 19475118Sfenner/// has no parent, prev, or next. 19575118Sfenner/// 19675118SfennerMachineInstr * 19775118SfennerMachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 19875118Sfenner return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 19975118Sfenner MachineInstr(*this, *Orig); 20075118Sfenner} 20175118Sfenner 20275118Sfenner/// DeleteMachineInstr - Delete the given MachineInstr. 20375118Sfenner/// 20475118Sfennervoid 20575118SfennerMachineFunction::DeleteMachineInstr(MachineInstr *MI) { 20675118Sfenner MI->~MachineInstr(); 20775118Sfenner InstructionRecycler.Deallocate(Allocator, MI); 20875118Sfenner} 20975118Sfenner 21075118Sfenner/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this 21175118Sfenner/// instead of `new MachineBasicBlock'. 21275118Sfenner/// 21375118SfennerMachineBasicBlock * 21475118SfennerMachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 21575118Sfenner return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 21675118Sfenner MachineBasicBlock(*this, bb); 21775118Sfenner} 21875118Sfenner 21975118Sfenner/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. 22075118Sfenner/// 22117680Spstvoid 22217680SpstMachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 22375118Sfenner assert(MBB->getParent() == this && "MBB parent mismatch!"); 22475118Sfenner MBB->~MachineBasicBlock(); 22575118Sfenner BasicBlockRecycler.Deallocate(Allocator, MBB); 22675118Sfenner} 22775118Sfenner 22875118SfennerMachineMemOperand * 22975118SfennerMachineFunction::getMachineMemOperand(const Value *v, unsigned f, 23075118Sfenner int64_t o, uint64_t s, 23175118Sfenner unsigned base_alignment) { 23275118Sfenner return new (Allocator.Allocate<MachineMemOperand>()) 23375118Sfenner MachineMemOperand(v, f, o, s, base_alignment); 23475118Sfenner} 23575118Sfenner 23617680SpstMachineMemOperand * 23775118SfennerMachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 23875118Sfenner int64_t Offset, uint64_t Size) { 23975118Sfenner return new (Allocator.Allocate<MachineMemOperand>()) 24075118Sfenner MachineMemOperand(MMO->getValue(), MMO->getFlags(), 24175118Sfenner int64_t(uint64_t(MMO->getOffset()) + 24275118Sfenner uint64_t(Offset)), 24375118Sfenner Size, MMO->getBaseAlignment()); 24475118Sfenner} 24575118Sfenner 24675118SfennerMachineInstr::mmo_iterator 24775118SfennerMachineFunction::allocateMemRefsArray(unsigned long Num) { 24875118Sfenner return Allocator.Allocate<MachineMemOperand *>(Num); 24975118Sfenner} 25075118Sfenner 25175118Sfennerstd::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> 25275118SfennerMachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin, 25375118Sfenner MachineInstr::mmo_iterator End) { 25475118Sfenner // Count the number of load mem refs. 25575118Sfenner unsigned Num = 0; 25617680Spst for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) 25775118Sfenner if ((*I)->isLoad()) 25875118Sfenner ++Num; 25975118Sfenner 26017680Spst // Allocate a new array and populate it with the load information. 26117680Spst MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); 26217680Spst unsigned Index = 0; 26375118Sfenner for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { 26475118Sfenner if ((*I)->isLoad()) { 26575118Sfenner if (!(*I)->isStore()) 26675118Sfenner // Reuse the MMO. 26717680Spst Result[Index] = *I; 26875118Sfenner else { 26975118Sfenner // Clone the MMO and unset the store flag. 27075118Sfenner MachineMemOperand *JustLoad = 27175118Sfenner getMachineMemOperand((*I)->getValue(), 27217680Spst (*I)->getFlags() & ~MachineMemOperand::MOStore, 27375118Sfenner (*I)->getOffset(), (*I)->getSize(), 27475118Sfenner (*I)->getBaseAlignment()); 27575118Sfenner Result[Index] = JustLoad; 27675118Sfenner } 27775118Sfenner ++Index; 27875118Sfenner } 27975118Sfenner } 28075118Sfenner return std::make_pair(Result, Result + Num); 28175118Sfenner} 28275118Sfenner 28375118Sfennerstd::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> 28475118SfennerMachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin, 28517680Spst MachineInstr::mmo_iterator End) { 28617680Spst // Count the number of load mem refs. 28775118Sfenner unsigned Num = 0; 28875118Sfenner for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) 28975118Sfenner if ((*I)->isStore()) 29075118Sfenner ++Num; 29175118Sfenner 29275118Sfenner // Allocate a new array and populate it with the store information. 29375118Sfenner MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); 29475118Sfenner unsigned Index = 0; 29575118Sfenner for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { 29675118Sfenner if ((*I)->isStore()) { 29775118Sfenner if (!(*I)->isLoad()) 29875118Sfenner // Reuse the MMO. 29975118Sfenner Result[Index] = *I; 30075118Sfenner else { 30175118Sfenner // Clone the MMO and unset the load flag. 30275118Sfenner MachineMemOperand *JustStore = 30375118Sfenner getMachineMemOperand((*I)->getValue(), 30475118Sfenner (*I)->getFlags() & ~MachineMemOperand::MOLoad, 30517680Spst (*I)->getOffset(), (*I)->getSize(), 30675118Sfenner (*I)->getBaseAlignment()); 30775118Sfenner Result[Index] = JustStore; 30817680Spst } 30975118Sfenner ++Index; 31017680Spst } 31175118Sfenner } 31217680Spst return std::make_pair(Result, Result + Num); 31317680Spst} 31417680Spst 31517680Spstvoid MachineFunction::dump() const { 31617680Spst print(dbgs()); 31717680Spst} 31817680Spst 31917680Spstvoid MachineFunction::print(raw_ostream &OS) const { 32017680Spst OS << "# Machine code for function " << Fn->getName() << ":\n"; 32117680Spst 32217680Spst // Print Frame Information 32317680Spst FrameInfo->print(*this, OS); 32475118Sfenner 32575118Sfenner // Print JumpTable Information 32617680Spst if (JumpTableInfo) 32717680Spst JumpTableInfo->print(OS); 32817680Spst 32917680Spst // Print Constant Pool 33017680Spst ConstantPool->print(OS); 33117680Spst 33217680Spst const TargetRegisterInfo *TRI = getTarget().getRegisterInfo(); 33317680Spst 33417680Spst if (RegInfo && !RegInfo->livein_empty()) { 33517680Spst OS << "Function Live Ins: "; 33617680Spst for (MachineRegisterInfo::livein_iterator 33717680Spst I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 33817680Spst if (TRI) 33917680Spst OS << "%" << TRI->getName(I->first); 34017680Spst else 34117680Spst OS << " %physreg" << I->first; 34217680Spst 34317680Spst if (I->second) 34417680Spst OS << " in reg%" << I->second; 34517680Spst 34617680Spst if (llvm::next(I) != E) 34717680Spst OS << ", "; 34817680Spst } 34917680Spst OS << '\n'; 35017680Spst } 35117680Spst if (RegInfo && !RegInfo->liveout_empty()) { 35217680Spst OS << "Function Live Outs: "; 35317680Spst for (MachineRegisterInfo::liveout_iterator 35417680Spst I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I){ 35517680Spst if (TRI) 35617680Spst OS << '%' << TRI->getName(*I); 35717680Spst else 35817680Spst OS << "%physreg" << *I; 35917680Spst 36056896Sfenner if (llvm::next(I) != E) 36156896Sfenner OS << " "; 36256896Sfenner } 36356896Sfenner OS << '\n'; 36456896Sfenner } 36556896Sfenner 36656896Sfenner for (const_iterator BB = begin(), E = end(); BB != E; ++BB) { 36756896Sfenner OS << '\n'; 36875118Sfenner BB->print(OS); 36975118Sfenner } 37039300Sfenner 37139300Sfenner OS << "\n# End machine code for function " << Fn->getName() << ".\n\n"; 37239300Sfenner} 37317680Spst 37417680Spstnamespace llvm { 37517680Spst template<> 37617680Spst struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 37717680Spst 37817680Spst DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} 37917680Spst 38017680Spst static std::string getGraphName(const MachineFunction *F) { 38117680Spst return "CFG for '" + F->getFunction()->getNameStr() + "' function"; 38217680Spst } 38317680Spst 38417680Spst std::string getNodeLabel(const MachineBasicBlock *Node, 38517680Spst const MachineFunction *Graph) { 38617680Spst if (isSimple () && Node->getBasicBlock() && 38717680Spst !Node->getBasicBlock()->getName().empty()) 38817680Spst return Node->getBasicBlock()->getNameStr() + ":"; 38917680Spst 39017680Spst std::string OutStr; 39175118Sfenner { 39217680Spst raw_string_ostream OSS(OutStr); 39317680Spst 39417680Spst if (isSimple()) 39517680Spst OSS << Node->getNumber() << ':'; 39617680Spst else 39717680Spst Node->print(OSS); 39817680Spst } 39975118Sfenner 40075118Sfenner if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 40117680Spst 40217680Spst // Process string output to make it nicer... 40317680Spst for (unsigned i = 0; i != OutStr.length(); ++i) 40417680Spst if (OutStr[i] == '\n') { // Left justify 40517680Spst OutStr[i] = '\\'; 40617680Spst OutStr.insert(OutStr.begin()+i+1, 'l'); 40717680Spst } 40817680Spst return OutStr; 40917680Spst } 41017680Spst }; 41117680Spst} 41275118Sfenner 41375118Sfennervoid MachineFunction::viewCFG() const 41417680Spst{ 41517680Spst#ifndef NDEBUG 41617680Spst ViewGraph(this, "mf" + getFunction()->getNameStr()); 41717680Spst#else 41817680Spst errs() << "SelectionDAG::viewGraph is only available in debug builds on " 41917680Spst << "systems with Graphviz or gv!\n"; 42075118Sfenner#endif // NDEBUG 42117680Spst} 42217680Spst 42317680Spstvoid MachineFunction::viewCFGOnly() const 42417680Spst{ 42517680Spst#ifndef NDEBUG 42675118Sfenner ViewGraph(this, "mf" + getFunction()->getNameStr(), true); 42775118Sfenner#else 42817680Spst errs() << "SelectionDAG::viewGraph is only available in debug builds on " 42917680Spst << "systems with Graphviz or gv!\n"; 43017680Spst#endif // NDEBUG 43175118Sfenner} 43217680Spst 43317680Spst/// addLiveIn - Add the specified physical register as a live-in value and 43417680Spst/// create a corresponding virtual register for it. 43517680Spstunsigned MachineFunction::addLiveIn(unsigned PReg, 43617680Spst const TargetRegisterClass *RC) { 43775118Sfenner assert(RC->contains(PReg) && "Not the correct regclass!"); 43875118Sfenner unsigned VReg = getRegInfo().createVirtualRegister(RC); 43975118Sfenner getRegInfo().addLiveIn(PReg, VReg); 44075118Sfenner return VReg; 44117680Spst} 44217680Spst 44317680Spst/// getDILocation - Get the DILocation for a given DebugLoc object. 44417680SpstDILocation MachineFunction::getDILocation(DebugLoc DL) const { 44517680Spst unsigned Idx = DL.getIndex(); 44617680Spst assert(Idx < DebugLocInfo.DebugLocations.size() && 44717680Spst "Invalid index into debug locations!"); 44817680Spst return DILocation(DebugLocInfo.DebugLocations[Idx]); 44917680Spst} 45017680Spst 45175118Sfenner 45275118Sfenner/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table. 45375118Sfenner/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 45417680Spst/// normal 'L' label is returned. 45517680SpstMCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 45675118Sfenner bool isLinkerPrivate) const { 45775118Sfenner assert(JumpTableInfo && "No jump tables"); 45817680Spst 45917680Spst assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 46017680Spst const MCAsmInfo &MAI = *getTarget().getMCAsmInfo(); 46117680Spst 46217680Spst const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() : 46317680Spst MAI.getPrivateGlobalPrefix(); 46456896Sfenner SmallString<60> Name; 46575118Sfenner raw_svector_ostream(Name) 46656896Sfenner << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 46717680Spst if (isLinkerPrivate) 46875118Sfenner return Ctx.GetOrCreateSymbol(Name.str()); 46975118Sfenner return Ctx.GetOrCreateTemporarySymbol(Name.str()); 47017680Spst} 47117680Spst 47275118Sfenner 47375118Sfenner//===----------------------------------------------------------------------===// 47475118Sfenner// MachineFrameInfo implementation 47575118Sfenner//===----------------------------------------------------------------------===// 47675118Sfenner 47775118Sfenner/// CreateFixedObject - Create a new object at a fixed location on the stack. 47875118Sfenner/// All fixed objects should be created before other objects are created for 47975118Sfenner/// efficiency. By default, fixed objects are immutable. This returns an 48075118Sfenner/// index with a negative value. 48175118Sfenner/// 48275118Sfennerint MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset, 48375118Sfenner bool Immutable, bool isSS) { 48475118Sfenner assert(Size != 0 && "Cannot allocate zero size fixed stack objects!"); 48575118Sfenner Objects.insert(Objects.begin(), StackObject(Size, 1, SPOffset, Immutable, 48675118Sfenner isSS)); 48775118Sfenner return -++NumFixedObjects; 48875118Sfenner} 48975118Sfenner 49075118Sfenner 49175118SfennerBitVector 49275118SfennerMachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const { 49375118Sfenner assert(MBB && "MBB must be valid"); 49417680Spst const MachineFunction *MF = MBB->getParent(); 49517680Spst assert(MF && "MBB must be part of a MachineFunction"); 49675118Sfenner const TargetMachine &TM = MF->getTarget(); 49775118Sfenner const TargetRegisterInfo *TRI = TM.getRegisterInfo(); 49875118Sfenner BitVector BV(TRI->getNumRegs()); 49975118Sfenner 50017680Spst // Before CSI is calculated, no registers are considered pristine. They can be 50117680Spst // freely used and PEI will make sure they are saved. 50217680Spst if (!isCalleeSavedInfoValid()) 50317680Spst return BV; 50417680Spst 50517680Spst for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR) 50617680Spst BV.set(*CSR); 50717680Spst 50856896Sfenner // The entry MBB always has all CSRs pristine. 50956896Sfenner if (MBB == &MF->front()) 51075118Sfenner return BV; 51175118Sfenner 51256896Sfenner // On other MBBs the saved CSRs are not pristine. 51356896Sfenner const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo(); 51456896Sfenner for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), 51575118Sfenner E = CSI.end(); I != E; ++I) 51656896Sfenner BV.reset(I->getReg()); 51756896Sfenner 51875118Sfenner return BV; 51956896Sfenner} 52075118Sfenner 52175118Sfenner 52275118Sfennervoid MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{ 52375118Sfenner if (Objects.empty()) return; 52475118Sfenner 52575118Sfenner const TargetFrameInfo *FI = MF.getTarget().getFrameInfo(); 52675118Sfenner int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0); 52775118Sfenner 52875118Sfenner OS << "Frame Objects:\n"; 52975118Sfenner 53075118Sfenner for (unsigned i = 0, e = Objects.size(); i != e; ++i) { 53175118Sfenner const StackObject &SO = Objects[i]; 53275118Sfenner OS << " fi#" << (int)(i-NumFixedObjects) << ": "; 53375118Sfenner if (SO.Size == ~0ULL) { 53475118Sfenner OS << "dead\n"; 53556896Sfenner continue; 53656896Sfenner } 53756896Sfenner if (SO.Size == 0) 53856896Sfenner OS << "variable sized"; 53975118Sfenner else 54075118Sfenner OS << "size=" << SO.Size; 54175118Sfenner OS << ", align=" << SO.Alignment; 54275118Sfenner 54317680Spst if (i < NumFixedObjects) 54475118Sfenner OS << ", fixed"; 54575118Sfenner if (i < NumFixedObjects || SO.SPOffset != -1) { 54675118Sfenner int64_t Off = SO.SPOffset - ValOffset; 54717680Spst OS << ", at location [SP"; 54817680Spst if (Off > 0) 54926183Sfenner OS << "+" << Off; 55017680Spst else if (Off < 0) 55117680Spst OS << Off; 55217680Spst OS << "]"; 55317680Spst } 55417680Spst OS << "\n"; 55517680Spst } 55617680Spst} 55775118Sfenner 55817680Spstvoid MachineFrameInfo::dump(const MachineFunction &MF) const { 55917680Spst print(MF, dbgs()); 56080234Sfenner} 56117680Spst 56217680Spst//===----------------------------------------------------------------------===// 56317680Spst// MachineJumpTableInfo implementation 56417680Spst//===----------------------------------------------------------------------===// 56517680Spst 56617680Spst/// getEntrySize - Return the size of each entry in the jump table. 56775118Sfennerunsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const { 56817680Spst // The size of a jump table entry is 4 bytes unless the entry is just the 56975118Sfenner // address of a block, in which case it is the pointer size. 57017680Spst switch (getEntryKind()) { 57175118Sfenner case MachineJumpTableInfo::EK_BlockAddress: 57275118Sfenner return TD.getPointerSize(); 57375118Sfenner case MachineJumpTableInfo::EK_GPRel32BlockAddress: 57475118Sfenner case MachineJumpTableInfo::EK_LabelDifference32: 57575118Sfenner case MachineJumpTableInfo::EK_Custom32: 57675118Sfenner return 4; 57775118Sfenner case MachineJumpTableInfo::EK_Inline: 57817680Spst return 0; 57917680Spst } 58017680Spst assert(0 && "Unknown jump table encoding!"); 58117680Spst return ~0; 58275118Sfenner} 58375118Sfenner 58475118Sfenner/// getEntryAlignment - Return the alignment of each entry in the jump table. 58575118Sfennerunsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const { 58675118Sfenner // The alignment of a jump table entry is the alignment of int32 unless the 58775118Sfenner // entry is just the address of a block, in which case it is the pointer 58875118Sfenner // alignment. 58975118Sfenner switch (getEntryKind()) { 59075118Sfenner case MachineJumpTableInfo::EK_BlockAddress: 59175118Sfenner return TD.getPointerABIAlignment(); 59217680Spst case MachineJumpTableInfo::EK_GPRel32BlockAddress: 59317680Spst case MachineJumpTableInfo::EK_LabelDifference32: 59475118Sfenner case MachineJumpTableInfo::EK_Custom32: 59575118Sfenner return TD.getABIIntegerTypeAlignment(32); 59617680Spst case MachineJumpTableInfo::EK_Inline: 59717680Spst return 1; 59875118Sfenner } 59975118Sfenner assert(0 && "Unknown jump table encoding!"); 60017680Spst return ~0; 60117680Spst} 60275118Sfenner 60375118Sfenner/// getJumpTableIndex - Create a new jump table entry in the jump table info 60475118Sfenner/// or return an existing one. 60575118Sfenner/// 60675118Sfennerunsigned MachineJumpTableInfo::getJumpTableIndex( 60775118Sfenner const std::vector<MachineBasicBlock*> &DestBBs) { 60875118Sfenner assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 60975118Sfenner JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 61075118Sfenner return JumpTables.size()-1; 61175118Sfenner} 61275118Sfenner 61375118Sfenner 61475118Sfenner/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update 61575118Sfenner/// the jump tables to branch to New instead. 61675118Sfennerbool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 61775118Sfenner MachineBasicBlock *New) { 61875118Sfenner assert(Old != New && "Not making a change?"); 61975118Sfenner bool MadeChange = false; 62075118Sfenner for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 62175118Sfenner ReplaceMBBInJumpTable(i, Old, New); 62275118Sfenner return MadeChange; 62375118Sfenner} 62475118Sfenner 62517680Spst/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update 62617680Spst/// the jump table to branch to New instead. 62717680Spstbool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 62875118Sfenner MachineBasicBlock *Old, 62975118Sfenner MachineBasicBlock *New) { 63075118Sfenner assert(Old != New && "Not making a change?"); 63117680Spst bool MadeChange = false; 63217680Spst MachineJumpTableEntry &JTE = JumpTables[Idx]; 63375118Sfenner for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 63417680Spst if (JTE.MBBs[j] == Old) { 63517680Spst JTE.MBBs[j] = New; 63675118Sfenner MadeChange = true; 63717680Spst } 63817680Spst return MadeChange; 63917680Spst} 64017680Spst 64117680Spstvoid MachineJumpTableInfo::print(raw_ostream &OS) const { 64217680Spst if (JumpTables.empty()) return; 64317680Spst 64417680Spst OS << "Jump Tables:\n"; 64517680Spst 64617680Spst for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 64717680Spst OS << " jt#" << i << ": "; 64817680Spst for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 64917680Spst OS << " BB#" << JumpTables[i].MBBs[j]->getNumber(); 65017680Spst } 65117680Spst 65217680Spst OS << '\n'; 65375118Sfenner} 65475118Sfenner 65575118Sfennervoid MachineJumpTableInfo::dump() const { print(dbgs()); } 65675118Sfenner 65775118Sfenner 65875118Sfenner//===----------------------------------------------------------------------===// 65975118Sfenner// MachineConstantPool implementation 66075118Sfenner//===----------------------------------------------------------------------===// 66175118Sfenner 66275118Sfennerconst Type *MachineConstantPoolEntry::getType() const { 66375118Sfenner if (isMachineConstantPoolEntry()) 66475118Sfenner return Val.MachineCPVal->getType(); 66575118Sfenner return Val.ConstVal->getType(); 66675118Sfenner} 66775118Sfenner 66875118Sfenner 66975118Sfennerunsigned MachineConstantPoolEntry::getRelocationInfo() const { 67075118Sfenner if (isMachineConstantPoolEntry()) 67175118Sfenner return Val.MachineCPVal->getRelocationInfo(); 67275118Sfenner return Val.ConstVal->getRelocationInfo(); 67375118Sfenner} 67475118Sfenner 67575118SfennerMachineConstantPool::~MachineConstantPool() { 67675118Sfenner for (unsigned i = 0, e = Constants.size(); i != e; ++i) 67775118Sfenner if (Constants[i].isMachineConstantPoolEntry()) 67875118Sfenner delete Constants[i].Val.MachineCPVal; 67975118Sfenner} 68075118Sfenner 68175118Sfenner/// CanShareConstantPoolEntry - Test whether the given two constants 68275118Sfenner/// can be allocated the same constant pool entry. 68375118Sfennerstatic bool CanShareConstantPoolEntry(Constant *A, Constant *B, 68475118Sfenner const TargetData *TD) { 68575118Sfenner // Handle the trivial case quickly. 68675118Sfenner if (A == B) return true; 68775118Sfenner 68875118Sfenner // If they have the same type but weren't the same constant, quickly 68975118Sfenner // reject them. 69075118Sfenner if (A->getType() == B->getType()) return false; 69175118Sfenner 69275118Sfenner // For now, only support constants with the same size. 69375118Sfenner if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType())) 69475118Sfenner return false; 69575118Sfenner 69675118Sfenner // If a floating-point value and an integer value have the same encoding, 69775118Sfenner // they can share a constant-pool entry. 69875118Sfenner if (ConstantFP *AFP = dyn_cast<ConstantFP>(A)) 69975118Sfenner if (ConstantInt *BI = dyn_cast<ConstantInt>(B)) 70075118Sfenner return AFP->getValueAPF().bitcastToAPInt() == BI->getValue(); 70175118Sfenner if (ConstantFP *BFP = dyn_cast<ConstantFP>(B)) 70217680Spst if (ConstantInt *AI = dyn_cast<ConstantInt>(A)) 70317680Spst return BFP->getValueAPF().bitcastToAPInt() == AI->getValue(); 70475118Sfenner 70575118Sfenner // Two vectors can share an entry if each pair of corresponding 70675118Sfenner // elements could. 70775118Sfenner if (ConstantVector *AV = dyn_cast<ConstantVector>(A)) 70875118Sfenner if (ConstantVector *BV = dyn_cast<ConstantVector>(B)) { 70917680Spst if (AV->getType()->getNumElements() != BV->getType()->getNumElements()) 710 return false; 711 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i) 712 if (!CanShareConstantPoolEntry(AV->getOperand(i), 713 BV->getOperand(i), TD)) 714 return false; 715 return true; 716 } 717 718 // TODO: Handle other cases. 719 720 return false; 721} 722 723/// getConstantPoolIndex - Create a new entry in the constant pool or return 724/// an existing one. User must specify the log2 of the minimum required 725/// alignment for the object. 726/// 727unsigned MachineConstantPool::getConstantPoolIndex(Constant *C, 728 unsigned Alignment) { 729 assert(Alignment && "Alignment must be specified!"); 730 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 731 732 // Check to see if we already have this constant. 733 // 734 // FIXME, this could be made much more efficient for large constant pools. 735 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 736 if (!Constants[i].isMachineConstantPoolEntry() && 737 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) { 738 if ((unsigned)Constants[i].getAlignment() < Alignment) 739 Constants[i].Alignment = Alignment; 740 return i; 741 } 742 743 Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 744 return Constants.size()-1; 745} 746 747unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 748 unsigned Alignment) { 749 assert(Alignment && "Alignment must be specified!"); 750 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 751 752 // Check to see if we already have this constant. 753 // 754 // FIXME, this could be made much more efficient for large constant pools. 755 int Idx = V->getExistingMachineCPValue(this, Alignment); 756 if (Idx != -1) 757 return (unsigned)Idx; 758 759 Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 760 return Constants.size()-1; 761} 762 763void MachineConstantPool::print(raw_ostream &OS) const { 764 if (Constants.empty()) return; 765 766 OS << "Constant Pool:\n"; 767 for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 768 OS << " cp#" << i << ": "; 769 if (Constants[i].isMachineConstantPoolEntry()) 770 Constants[i].Val.MachineCPVal->print(OS); 771 else 772 OS << *(Value*)Constants[i].Val.ConstVal; 773 OS << ", align=" << Constants[i].getAlignment(); 774 OS << "\n"; 775 } 776} 777 778void MachineConstantPool::dump() const { print(dbgs()); } 779