MachineFunction.cpp revision 224145
10Sstevel@tonic-gate//===-- MachineFunction.cpp -----------------------------------------------===// 20Sstevel@tonic-gate// 30Sstevel@tonic-gate// The LLVM Compiler Infrastructure 40Sstevel@tonic-gate// 50Sstevel@tonic-gate// This file is distributed under the University of Illinois Open Source 60Sstevel@tonic-gate// License. See LICENSE.TXT for details. 70Sstevel@tonic-gate// 80Sstevel@tonic-gate//===----------------------------------------------------------------------===// 90Sstevel@tonic-gate// 100Sstevel@tonic-gate// Collect native machine code information for a function. This allows 110Sstevel@tonic-gate// target-specific information about the generated code to be stored with each 120Sstevel@tonic-gate// function. 130Sstevel@tonic-gate// 140Sstevel@tonic-gate//===----------------------------------------------------------------------===// 150Sstevel@tonic-gate 160Sstevel@tonic-gate#include "llvm/DerivedTypes.h" 170Sstevel@tonic-gate#include "llvm/Function.h" 180Sstevel@tonic-gate#include "llvm/Instructions.h" 190Sstevel@tonic-gate#include "llvm/Config/config.h" 200Sstevel@tonic-gate#include "llvm/CodeGen/MachineConstantPool.h" 210Sstevel@tonic-gate#include "llvm/CodeGen/MachineFunction.h" 220Sstevel@tonic-gate#include "llvm/CodeGen/MachineFunctionPass.h" 23802Scf46844#include "llvm/CodeGen/MachineFrameInfo.h" 240Sstevel@tonic-gate#include "llvm/CodeGen/MachineInstr.h" 250Sstevel@tonic-gate#include "llvm/CodeGen/MachineJumpTableInfo.h" 260Sstevel@tonic-gate#include "llvm/CodeGen/MachineModuleInfo.h" 270Sstevel@tonic-gate#include "llvm/CodeGen/MachineRegisterInfo.h" 280Sstevel@tonic-gate#include "llvm/CodeGen/Passes.h" 290Sstevel@tonic-gate#include "llvm/MC/MCAsmInfo.h" 300Sstevel@tonic-gate#include "llvm/MC/MCContext.h" 310Sstevel@tonic-gate#include "llvm/Analysis/DebugInfo.h" 320Sstevel@tonic-gate#include "llvm/Support/Debug.h" 330Sstevel@tonic-gate#include "llvm/Target/TargetData.h" 340Sstevel@tonic-gate#include "llvm/Target/TargetLowering.h" 350Sstevel@tonic-gate#include "llvm/Target/TargetMachine.h" 360Sstevel@tonic-gate#include "llvm/Target/TargetFrameLowering.h" 370Sstevel@tonic-gate#include "llvm/ADT/SmallString.h" 380Sstevel@tonic-gate#include "llvm/ADT/STLExtras.h" 390Sstevel@tonic-gate#include "llvm/Support/GraphWriter.h" 400Sstevel@tonic-gate#include "llvm/Support/raw_ostream.h" 410Sstevel@tonic-gateusing namespace llvm; 420Sstevel@tonic-gate 430Sstevel@tonic-gate//===----------------------------------------------------------------------===// 44802Scf46844// MachineFunction implementation 450Sstevel@tonic-gate//===----------------------------------------------------------------------===// 460Sstevel@tonic-gate 470Sstevel@tonic-gate// Out of line virtual method. 480Sstevel@tonic-gateMachineFunctionInfo::~MachineFunctionInfo() {} 490Sstevel@tonic-gate 500Sstevel@tonic-gatevoid ilist_traits<MachineBasicBlock>::deleteNode(MachineBasicBlock *MBB) { 510Sstevel@tonic-gate MBB->getParent()->DeleteMachineBasicBlock(MBB); 520Sstevel@tonic-gate} 530Sstevel@tonic-gate 540Sstevel@tonic-gateMachineFunction::MachineFunction(const Function *F, const TargetMachine &TM, 550Sstevel@tonic-gate unsigned FunctionNum, MachineModuleInfo &mmi, 560Sstevel@tonic-gate GCModuleInfo* gmi) 570Sstevel@tonic-gate : Fn(F), Target(TM), Ctx(mmi.getContext()), MMI(mmi), GMI(gmi) { 580Sstevel@tonic-gate if (TM.getRegisterInfo()) 590Sstevel@tonic-gate RegInfo = new (Allocator) MachineRegisterInfo(*TM.getRegisterInfo()); 600Sstevel@tonic-gate else 610Sstevel@tonic-gate RegInfo = 0; 620Sstevel@tonic-gate MFInfo = 0; 630Sstevel@tonic-gate FrameInfo = new (Allocator) MachineFrameInfo(*TM.getFrameLowering()); 640Sstevel@tonic-gate if (Fn->hasFnAttr(Attribute::StackAlignment)) 650Sstevel@tonic-gate FrameInfo->setMaxAlignment(Attribute::getStackAlignmentFromAttrs( 660Sstevel@tonic-gate Fn->getAttributes().getFnAttributes())); 670Sstevel@tonic-gate ConstantPool = new (Allocator) MachineConstantPool(TM.getTargetData()); 680Sstevel@tonic-gate Alignment = TM.getTargetLowering()->getMinFunctionAlignment(); 690Sstevel@tonic-gate // FIXME: Shouldn't use pref alignment if explicit alignment is set on Fn. 700Sstevel@tonic-gate if (!Fn->hasFnAttr(Attribute::OptimizeForSize)) 710Sstevel@tonic-gate Alignment = std::max(Alignment, 720Sstevel@tonic-gate TM.getTargetLowering()->getPrefFunctionAlignment()); 730Sstevel@tonic-gate FunctionNumber = FunctionNum; 740Sstevel@tonic-gate JumpTableInfo = 0; 750Sstevel@tonic-gate} 760Sstevel@tonic-gate 770Sstevel@tonic-gateMachineFunction::~MachineFunction() { 780Sstevel@tonic-gate BasicBlocks.clear(); 790Sstevel@tonic-gate InstructionRecycler.clear(Allocator); 800Sstevel@tonic-gate BasicBlockRecycler.clear(Allocator); 810Sstevel@tonic-gate if (RegInfo) { 820Sstevel@tonic-gate RegInfo->~MachineRegisterInfo(); 830Sstevel@tonic-gate Allocator.Deallocate(RegInfo); 840Sstevel@tonic-gate } 850Sstevel@tonic-gate if (MFInfo) { 860Sstevel@tonic-gate MFInfo->~MachineFunctionInfo(); 870Sstevel@tonic-gate Allocator.Deallocate(MFInfo); 880Sstevel@tonic-gate } 890Sstevel@tonic-gate FrameInfo->~MachineFrameInfo(); Allocator.Deallocate(FrameInfo); 90802Scf46844 ConstantPool->~MachineConstantPool(); Allocator.Deallocate(ConstantPool); 91802Scf46844 92802Scf46844 if (JumpTableInfo) { 93802Scf46844 JumpTableInfo->~MachineJumpTableInfo(); 940Sstevel@tonic-gate Allocator.Deallocate(JumpTableInfo); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate} 97802Scf46844 980Sstevel@tonic-gate/// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it 990Sstevel@tonic-gate/// does already exist, allocate one. 1000Sstevel@tonic-gateMachineJumpTableInfo *MachineFunction:: 1010Sstevel@tonic-gategetOrCreateJumpTableInfo(unsigned EntryKind) { 1020Sstevel@tonic-gate if (JumpTableInfo) return JumpTableInfo; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate JumpTableInfo = new (Allocator) 1050Sstevel@tonic-gate MachineJumpTableInfo((MachineJumpTableInfo::JTEntryKind)EntryKind); 1060Sstevel@tonic-gate return JumpTableInfo; 1070Sstevel@tonic-gate} 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate/// RenumberBlocks - This discards all of the MachineBasicBlock numbers and 1100Sstevel@tonic-gate/// recomputes them. This guarantees that the MBB numbers are sequential, 111802Scf46844/// dense, and match the ordering of the blocks within the function. If a 112802Scf46844/// specific MachineBasicBlock is specified, only that block and those after 113802Scf46844/// it are renumbered. 1140Sstevel@tonic-gatevoid MachineFunction::RenumberBlocks(MachineBasicBlock *MBB) { 1150Sstevel@tonic-gate if (empty()) { MBBNumbering.clear(); return; } 1160Sstevel@tonic-gate MachineFunction::iterator MBBI, E = end(); 1170Sstevel@tonic-gate if (MBB == 0) 1180Sstevel@tonic-gate MBBI = begin(); 1190Sstevel@tonic-gate else 1200Sstevel@tonic-gate MBBI = MBB; 121802Scf46844 1220Sstevel@tonic-gate // Figure out the block number this should have. 1230Sstevel@tonic-gate unsigned BlockNo = 0; 1240Sstevel@tonic-gate if (MBBI != begin()) 1250Sstevel@tonic-gate BlockNo = prior(MBBI)->getNumber()+1; 1260Sstevel@tonic-gate 1270Sstevel@tonic-gate for (; MBBI != E; ++MBBI, ++BlockNo) { 1280Sstevel@tonic-gate if (MBBI->getNumber() != (int)BlockNo) { 1290Sstevel@tonic-gate // Remove use of the old number. 130802Scf46844 if (MBBI->getNumber() != -1) { 1310Sstevel@tonic-gate assert(MBBNumbering[MBBI->getNumber()] == &*MBBI && 1320Sstevel@tonic-gate "MBB number mismatch!"); 1330Sstevel@tonic-gate MBBNumbering[MBBI->getNumber()] = 0; 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 136802Scf46844 // If BlockNo is already taken, set that block's number to -1. 1370Sstevel@tonic-gate if (MBBNumbering[BlockNo]) 1380Sstevel@tonic-gate MBBNumbering[BlockNo]->setNumber(-1); 1390Sstevel@tonic-gate 1400Sstevel@tonic-gate MBBNumbering[BlockNo] = MBBI; 1410Sstevel@tonic-gate MBBI->setNumber(BlockNo); 1420Sstevel@tonic-gate } 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate // Okay, all the blocks are renumbered. If we have compactified the block 1460Sstevel@tonic-gate // numbering, shrink MBBNumbering now. 1470Sstevel@tonic-gate assert(BlockNo <= MBBNumbering.size() && "Mismatch!"); 1480Sstevel@tonic-gate MBBNumbering.resize(BlockNo); 1490Sstevel@tonic-gate} 1500Sstevel@tonic-gate 1510Sstevel@tonic-gate/// CreateMachineInstr - Allocate a new MachineInstr. Use this instead 1520Sstevel@tonic-gate/// of `new MachineInstr'. 1530Sstevel@tonic-gate/// 1540Sstevel@tonic-gateMachineInstr * 1550Sstevel@tonic-gateMachineFunction::CreateMachineInstr(const MCInstrDesc &MCID, 1560Sstevel@tonic-gate DebugLoc DL, bool NoImp) { 1570Sstevel@tonic-gate return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 1580Sstevel@tonic-gate MachineInstr(MCID, DL, NoImp); 159802Scf46844} 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate/// CloneMachineInstr - Create a new MachineInstr which is a copy of the 1620Sstevel@tonic-gate/// 'Orig' instruction, identical in all ways except the instruction 1630Sstevel@tonic-gate/// has no parent, prev, or next. 1640Sstevel@tonic-gate/// 1650Sstevel@tonic-gateMachineInstr * 1660Sstevel@tonic-gateMachineFunction::CloneMachineInstr(const MachineInstr *Orig) { 1670Sstevel@tonic-gate return new (InstructionRecycler.Allocate<MachineInstr>(Allocator)) 1680Sstevel@tonic-gate MachineInstr(*this, *Orig); 1690Sstevel@tonic-gate} 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate/// DeleteMachineInstr - Delete the given MachineInstr. 1720Sstevel@tonic-gate/// 173802Scf46844void 174802Scf46844MachineFunction::DeleteMachineInstr(MachineInstr *MI) { 1750Sstevel@tonic-gate MI->~MachineInstr(); 1760Sstevel@tonic-gate InstructionRecycler.Deallocate(Allocator, MI); 1770Sstevel@tonic-gate} 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate/// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this 1800Sstevel@tonic-gate/// instead of `new MachineBasicBlock'. 1810Sstevel@tonic-gate/// 1820Sstevel@tonic-gateMachineBasicBlock * 1830Sstevel@tonic-gateMachineFunction::CreateMachineBasicBlock(const BasicBlock *bb) { 1840Sstevel@tonic-gate return new (BasicBlockRecycler.Allocate<MachineBasicBlock>(Allocator)) 1850Sstevel@tonic-gate MachineBasicBlock(*this, bb); 1860Sstevel@tonic-gate} 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate/// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. 1890Sstevel@tonic-gate/// 1900Sstevel@tonic-gatevoid 1910Sstevel@tonic-gateMachineFunction::DeleteMachineBasicBlock(MachineBasicBlock *MBB) { 1920Sstevel@tonic-gate assert(MBB->getParent() == this && "MBB parent mismatch!"); 1930Sstevel@tonic-gate MBB->~MachineBasicBlock(); 1940Sstevel@tonic-gate BasicBlockRecycler.Deallocate(Allocator, MBB); 1950Sstevel@tonic-gate} 1960Sstevel@tonic-gate 1970Sstevel@tonic-gateMachineMemOperand * 1980Sstevel@tonic-gateMachineFunction::getMachineMemOperand(MachinePointerInfo PtrInfo, unsigned f, 1990Sstevel@tonic-gate uint64_t s, unsigned base_alignment, 2000Sstevel@tonic-gate const MDNode *TBAAInfo) { 2010Sstevel@tonic-gate return new (Allocator) MachineMemOperand(PtrInfo, f, s, base_alignment, 2020Sstevel@tonic-gate TBAAInfo); 2030Sstevel@tonic-gate} 2040Sstevel@tonic-gate 2050Sstevel@tonic-gateMachineMemOperand * 2060Sstevel@tonic-gateMachineFunction::getMachineMemOperand(const MachineMemOperand *MMO, 2070Sstevel@tonic-gate int64_t Offset, uint64_t Size) { 2080Sstevel@tonic-gate return new (Allocator) 2090Sstevel@tonic-gate MachineMemOperand(MachinePointerInfo(MMO->getValue(), 2100Sstevel@tonic-gate MMO->getOffset()+Offset), 2110Sstevel@tonic-gate MMO->getFlags(), Size, 2120Sstevel@tonic-gate MMO->getBaseAlignment(), 0); 2130Sstevel@tonic-gate} 2140Sstevel@tonic-gate 2150Sstevel@tonic-gateMachineInstr::mmo_iterator 2160Sstevel@tonic-gateMachineFunction::allocateMemRefsArray(unsigned long Num) { 2170Sstevel@tonic-gate return Allocator.Allocate<MachineMemOperand *>(Num); 2180Sstevel@tonic-gate} 2190Sstevel@tonic-gate 2200Sstevel@tonic-gatestd::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> 2210Sstevel@tonic-gateMachineFunction::extractLoadMemRefs(MachineInstr::mmo_iterator Begin, 2220Sstevel@tonic-gate MachineInstr::mmo_iterator End) { 2230Sstevel@tonic-gate // Count the number of load mem refs. 2240Sstevel@tonic-gate unsigned Num = 0; 225802Scf46844 for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) 2260Sstevel@tonic-gate if ((*I)->isLoad()) 2270Sstevel@tonic-gate ++Num; 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate // Allocate a new array and populate it with the load information. 2300Sstevel@tonic-gate MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); 2310Sstevel@tonic-gate unsigned Index = 0; 2320Sstevel@tonic-gate for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { 2330Sstevel@tonic-gate if ((*I)->isLoad()) { 2340Sstevel@tonic-gate if (!(*I)->isStore()) 2350Sstevel@tonic-gate // Reuse the MMO. 2360Sstevel@tonic-gate Result[Index] = *I; 2370Sstevel@tonic-gate else { 2380Sstevel@tonic-gate // Clone the MMO and unset the store flag. 2390Sstevel@tonic-gate MachineMemOperand *JustLoad = 2400Sstevel@tonic-gate getMachineMemOperand((*I)->getPointerInfo(), 2410Sstevel@tonic-gate (*I)->getFlags() & ~MachineMemOperand::MOStore, 2420Sstevel@tonic-gate (*I)->getSize(), (*I)->getBaseAlignment(), 2430Sstevel@tonic-gate (*I)->getTBAAInfo()); 2440Sstevel@tonic-gate Result[Index] = JustLoad; 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate ++Index; 2470Sstevel@tonic-gate } 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate return std::make_pair(Result, Result + Num); 2500Sstevel@tonic-gate} 2510Sstevel@tonic-gate 2520Sstevel@tonic-gatestd::pair<MachineInstr::mmo_iterator, MachineInstr::mmo_iterator> 2530Sstevel@tonic-gateMachineFunction::extractStoreMemRefs(MachineInstr::mmo_iterator Begin, 2540Sstevel@tonic-gate MachineInstr::mmo_iterator End) { 2550Sstevel@tonic-gate // Count the number of load mem refs. 2560Sstevel@tonic-gate unsigned Num = 0; 2570Sstevel@tonic-gate for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) 2580Sstevel@tonic-gate if ((*I)->isStore()) 2590Sstevel@tonic-gate ++Num; 2600Sstevel@tonic-gate 2610Sstevel@tonic-gate // Allocate a new array and populate it with the store information. 2620Sstevel@tonic-gate MachineInstr::mmo_iterator Result = allocateMemRefsArray(Num); 2630Sstevel@tonic-gate unsigned Index = 0; 2640Sstevel@tonic-gate for (MachineInstr::mmo_iterator I = Begin; I != End; ++I) { 2650Sstevel@tonic-gate if ((*I)->isStore()) { 2660Sstevel@tonic-gate if (!(*I)->isLoad()) 2670Sstevel@tonic-gate // Reuse the MMO. 2680Sstevel@tonic-gate Result[Index] = *I; 2690Sstevel@tonic-gate else { 2700Sstevel@tonic-gate // Clone the MMO and unset the load flag. 2710Sstevel@tonic-gate MachineMemOperand *JustStore = 2720Sstevel@tonic-gate getMachineMemOperand((*I)->getPointerInfo(), 2730Sstevel@tonic-gate (*I)->getFlags() & ~MachineMemOperand::MOLoad, 2740Sstevel@tonic-gate (*I)->getSize(), (*I)->getBaseAlignment(), 2750Sstevel@tonic-gate (*I)->getTBAAInfo()); 2760Sstevel@tonic-gate Result[Index] = JustStore; 2770Sstevel@tonic-gate } 2780Sstevel@tonic-gate ++Index; 2790Sstevel@tonic-gate } 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate return std::make_pair(Result, Result + Num); 2820Sstevel@tonic-gate} 283802Scf46844 2840Sstevel@tonic-gatevoid MachineFunction::dump() const { 2850Sstevel@tonic-gate print(dbgs()); 2860Sstevel@tonic-gate} 2870Sstevel@tonic-gate 2880Sstevel@tonic-gatevoid MachineFunction::print(raw_ostream &OS, SlotIndexes *Indexes) const { 2890Sstevel@tonic-gate OS << "# Machine code for function " << Fn->getName() << ":\n"; 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate // Print Frame Information 292802Scf46844 FrameInfo->print(*this, OS); 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate // Print JumpTable Information 2950Sstevel@tonic-gate if (JumpTableInfo) 2960Sstevel@tonic-gate JumpTableInfo->print(OS); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate // Print Constant Pool 2990Sstevel@tonic-gate ConstantPool->print(OS); 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate const TargetRegisterInfo *TRI = getTarget().getRegisterInfo(); 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate if (RegInfo && !RegInfo->livein_empty()) { 3040Sstevel@tonic-gate OS << "Function Live Ins: "; 3050Sstevel@tonic-gate for (MachineRegisterInfo::livein_iterator 3060Sstevel@tonic-gate I = RegInfo->livein_begin(), E = RegInfo->livein_end(); I != E; ++I) { 3070Sstevel@tonic-gate OS << PrintReg(I->first, TRI); 3080Sstevel@tonic-gate if (I->second) 3090Sstevel@tonic-gate OS << " in " << PrintReg(I->second, TRI); 3100Sstevel@tonic-gate if (llvm::next(I) != E) 3110Sstevel@tonic-gate OS << ", "; 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate OS << '\n'; 3140Sstevel@tonic-gate } 3150Sstevel@tonic-gate if (RegInfo && !RegInfo->liveout_empty()) { 3160Sstevel@tonic-gate OS << "Function Live Outs:"; 3170Sstevel@tonic-gate for (MachineRegisterInfo::liveout_iterator 3180Sstevel@tonic-gate I = RegInfo->liveout_begin(), E = RegInfo->liveout_end(); I != E; ++I) 3190Sstevel@tonic-gate OS << ' ' << PrintReg(*I, TRI); 320802Scf46844 OS << '\n'; 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate for (const_iterator BB = begin(), E = end(); BB != E; ++BB) { 3240Sstevel@tonic-gate OS << '\n'; 325802Scf46844 BB->print(OS, Indexes); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate OS << "\n# End machine code for function " << Fn->getName() << ".\n\n"; 3290Sstevel@tonic-gate} 3300Sstevel@tonic-gate 3310Sstevel@tonic-gatenamespace llvm { 332802Scf46844 template<> 3330Sstevel@tonic-gate struct DOTGraphTraits<const MachineFunction*> : public DefaultDOTGraphTraits { 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate static std::string getGraphName(const MachineFunction *F) { 3380Sstevel@tonic-gate return "CFG for '" + F->getFunction()->getNameStr() + "' function"; 3390Sstevel@tonic-gate } 3400Sstevel@tonic-gate 341802Scf46844 std::string getNodeLabel(const MachineBasicBlock *Node, 342802Scf46844 const MachineFunction *Graph) { 3430Sstevel@tonic-gate std::string OutStr; 344802Scf46844 { 345802Scf46844 raw_string_ostream OSS(OutStr); 3460Sstevel@tonic-gate 3470Sstevel@tonic-gate if (isSimple()) { 3480Sstevel@tonic-gate OSS << "BB#" << Node->getNumber(); 3490Sstevel@tonic-gate if (const BasicBlock *BB = Node->getBasicBlock()) 3500Sstevel@tonic-gate OSS << ": " << BB->getName(); 3510Sstevel@tonic-gate } else 3520Sstevel@tonic-gate Node->print(OSS); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); 356802Scf46844 357802Scf46844 // Process string output to make it nicer... 3580Sstevel@tonic-gate for (unsigned i = 0; i != OutStr.length(); ++i) 3590Sstevel@tonic-gate if (OutStr[i] == '\n') { // Left justify 3600Sstevel@tonic-gate OutStr[i] = '\\'; 3610Sstevel@tonic-gate OutStr.insert(OutStr.begin()+i+1, 'l'); 3620Sstevel@tonic-gate } 3630Sstevel@tonic-gate return OutStr; 3640Sstevel@tonic-gate } 3650Sstevel@tonic-gate }; 3660Sstevel@tonic-gate} 3670Sstevel@tonic-gate 3680Sstevel@tonic-gatevoid MachineFunction::viewCFG() const 3690Sstevel@tonic-gate{ 3700Sstevel@tonic-gate#ifndef NDEBUG 3710Sstevel@tonic-gate ViewGraph(this, "mf" + getFunction()->getNameStr()); 3720Sstevel@tonic-gate#else 3730Sstevel@tonic-gate errs() << "MachineFunction::viewCFG is only available in debug builds on " 3740Sstevel@tonic-gate << "systems with Graphviz or gv!\n"; 3750Sstevel@tonic-gate#endif // NDEBUG 3760Sstevel@tonic-gate} 3770Sstevel@tonic-gate 3780Sstevel@tonic-gatevoid MachineFunction::viewCFGOnly() const 3790Sstevel@tonic-gate{ 3800Sstevel@tonic-gate#ifndef NDEBUG 3810Sstevel@tonic-gate ViewGraph(this, "mf" + getFunction()->getNameStr(), true); 3820Sstevel@tonic-gate#else 3830Sstevel@tonic-gate errs() << "MachineFunction::viewCFGOnly is only available in debug builds on " 3840Sstevel@tonic-gate << "systems with Graphviz or gv!\n"; 3850Sstevel@tonic-gate#endif // NDEBUG 3860Sstevel@tonic-gate} 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate/// addLiveIn - Add the specified physical register as a live-in value and 3890Sstevel@tonic-gate/// create a corresponding virtual register for it. 3900Sstevel@tonic-gateunsigned MachineFunction::addLiveIn(unsigned PReg, 3910Sstevel@tonic-gate const TargetRegisterClass *RC) { 3920Sstevel@tonic-gate MachineRegisterInfo &MRI = getRegInfo(); 3930Sstevel@tonic-gate unsigned VReg = MRI.getLiveInVirtReg(PReg); 3940Sstevel@tonic-gate if (VReg) { 3950Sstevel@tonic-gate assert(MRI.getRegClass(VReg) == RC && "Register class mismatch!"); 3960Sstevel@tonic-gate return VReg; 397802Scf46844 } 398802Scf46844 VReg = MRI.createVirtualRegister(RC); 3990Sstevel@tonic-gate MRI.addLiveIn(PReg, VReg); 4000Sstevel@tonic-gate return VReg; 4010Sstevel@tonic-gate} 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate/// getJTISymbol - Return the MCSymbol for the specified non-empty jump table. 404802Scf46844/// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 4050Sstevel@tonic-gate/// normal 'L' label is returned. 4060Sstevel@tonic-gateMCSymbol *MachineFunction::getJTISymbol(unsigned JTI, MCContext &Ctx, 4070Sstevel@tonic-gate bool isLinkerPrivate) const { 4080Sstevel@tonic-gate assert(JumpTableInfo && "No jump tables"); 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate assert(JTI < JumpTableInfo->getJumpTables().size() && "Invalid JTI!"); 4110Sstevel@tonic-gate const MCAsmInfo &MAI = *getTarget().getMCAsmInfo(); 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate const char *Prefix = isLinkerPrivate ? MAI.getLinkerPrivateGlobalPrefix() : 4140Sstevel@tonic-gate MAI.getPrivateGlobalPrefix(); 4150Sstevel@tonic-gate SmallString<60> Name; 4160Sstevel@tonic-gate raw_svector_ostream(Name) 4170Sstevel@tonic-gate << Prefix << "JTI" << getFunctionNumber() << '_' << JTI; 4180Sstevel@tonic-gate return Ctx.GetOrCreateSymbol(Name.str()); 4190Sstevel@tonic-gate} 4200Sstevel@tonic-gate 4210Sstevel@tonic-gate/// getPICBaseSymbol - Return a function-local symbol to represent the PIC 4220Sstevel@tonic-gate/// base. 4230Sstevel@tonic-gateMCSymbol *MachineFunction::getPICBaseSymbol() const { 4240Sstevel@tonic-gate const MCAsmInfo &MAI = *Target.getMCAsmInfo(); 4250Sstevel@tonic-gate return Ctx.GetOrCreateSymbol(Twine(MAI.getPrivateGlobalPrefix())+ 4260Sstevel@tonic-gate Twine(getFunctionNumber())+"$pb"); 4270Sstevel@tonic-gate} 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate//===----------------------------------------------------------------------===// 4300Sstevel@tonic-gate// MachineFrameInfo implementation 4310Sstevel@tonic-gate//===----------------------------------------------------------------------===// 4320Sstevel@tonic-gate 4330Sstevel@tonic-gate/// CreateFixedObject - Create a new object at a fixed location on the stack. 4340Sstevel@tonic-gate/// All fixed objects should be created before other objects are created for 4350Sstevel@tonic-gate/// efficiency. By default, fixed objects are immutable. This returns an 4360Sstevel@tonic-gate/// index with a negative value. 4370Sstevel@tonic-gate/// 4380Sstevel@tonic-gateint MachineFrameInfo::CreateFixedObject(uint64_t Size, int64_t SPOffset, 4390Sstevel@tonic-gate bool Immutable) { 4400Sstevel@tonic-gate assert(Size != 0 && "Cannot allocate zero size fixed stack objects!"); 4410Sstevel@tonic-gate // The alignment of the frame index can be determined from its offset from 4420Sstevel@tonic-gate // the incoming frame position. If the frame object is at offset 32 and 4430Sstevel@tonic-gate // the stack is guaranteed to be 16-byte aligned, then we know that the 4440Sstevel@tonic-gate // object is 16-byte aligned. 445 unsigned StackAlign = TFI.getStackAlignment(); 446 unsigned Align = MinAlign(SPOffset, StackAlign); 447 Objects.insert(Objects.begin(), StackObject(Size, Align, SPOffset, Immutable, 448 /*isSS*/false, false)); 449 return -++NumFixedObjects; 450} 451 452 453BitVector 454MachineFrameInfo::getPristineRegs(const MachineBasicBlock *MBB) const { 455 assert(MBB && "MBB must be valid"); 456 const MachineFunction *MF = MBB->getParent(); 457 assert(MF && "MBB must be part of a MachineFunction"); 458 const TargetMachine &TM = MF->getTarget(); 459 const TargetRegisterInfo *TRI = TM.getRegisterInfo(); 460 BitVector BV(TRI->getNumRegs()); 461 462 // Before CSI is calculated, no registers are considered pristine. They can be 463 // freely used and PEI will make sure they are saved. 464 if (!isCalleeSavedInfoValid()) 465 return BV; 466 467 for (const unsigned *CSR = TRI->getCalleeSavedRegs(MF); CSR && *CSR; ++CSR) 468 BV.set(*CSR); 469 470 // The entry MBB always has all CSRs pristine. 471 if (MBB == &MF->front()) 472 return BV; 473 474 // On other MBBs the saved CSRs are not pristine. 475 const std::vector<CalleeSavedInfo> &CSI = getCalleeSavedInfo(); 476 for (std::vector<CalleeSavedInfo>::const_iterator I = CSI.begin(), 477 E = CSI.end(); I != E; ++I) 478 BV.reset(I->getReg()); 479 480 return BV; 481} 482 483 484void MachineFrameInfo::print(const MachineFunction &MF, raw_ostream &OS) const{ 485 if (Objects.empty()) return; 486 487 const TargetFrameLowering *FI = MF.getTarget().getFrameLowering(); 488 int ValOffset = (FI ? FI->getOffsetOfLocalArea() : 0); 489 490 OS << "Frame Objects:\n"; 491 492 for (unsigned i = 0, e = Objects.size(); i != e; ++i) { 493 const StackObject &SO = Objects[i]; 494 OS << " fi#" << (int)(i-NumFixedObjects) << ": "; 495 if (SO.Size == ~0ULL) { 496 OS << "dead\n"; 497 continue; 498 } 499 if (SO.Size == 0) 500 OS << "variable sized"; 501 else 502 OS << "size=" << SO.Size; 503 OS << ", align=" << SO.Alignment; 504 505 if (i < NumFixedObjects) 506 OS << ", fixed"; 507 if (i < NumFixedObjects || SO.SPOffset != -1) { 508 int64_t Off = SO.SPOffset - ValOffset; 509 OS << ", at location [SP"; 510 if (Off > 0) 511 OS << "+" << Off; 512 else if (Off < 0) 513 OS << Off; 514 OS << "]"; 515 } 516 OS << "\n"; 517 } 518} 519 520void MachineFrameInfo::dump(const MachineFunction &MF) const { 521 print(MF, dbgs()); 522} 523 524//===----------------------------------------------------------------------===// 525// MachineJumpTableInfo implementation 526//===----------------------------------------------------------------------===// 527 528/// getEntrySize - Return the size of each entry in the jump table. 529unsigned MachineJumpTableInfo::getEntrySize(const TargetData &TD) const { 530 // The size of a jump table entry is 4 bytes unless the entry is just the 531 // address of a block, in which case it is the pointer size. 532 switch (getEntryKind()) { 533 case MachineJumpTableInfo::EK_BlockAddress: 534 return TD.getPointerSize(); 535 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 536 case MachineJumpTableInfo::EK_LabelDifference32: 537 case MachineJumpTableInfo::EK_Custom32: 538 return 4; 539 case MachineJumpTableInfo::EK_Inline: 540 return 0; 541 } 542 assert(0 && "Unknown jump table encoding!"); 543 return ~0; 544} 545 546/// getEntryAlignment - Return the alignment of each entry in the jump table. 547unsigned MachineJumpTableInfo::getEntryAlignment(const TargetData &TD) const { 548 // The alignment of a jump table entry is the alignment of int32 unless the 549 // entry is just the address of a block, in which case it is the pointer 550 // alignment. 551 switch (getEntryKind()) { 552 case MachineJumpTableInfo::EK_BlockAddress: 553 return TD.getPointerABIAlignment(); 554 case MachineJumpTableInfo::EK_GPRel32BlockAddress: 555 case MachineJumpTableInfo::EK_LabelDifference32: 556 case MachineJumpTableInfo::EK_Custom32: 557 return TD.getABIIntegerTypeAlignment(32); 558 case MachineJumpTableInfo::EK_Inline: 559 return 1; 560 } 561 assert(0 && "Unknown jump table encoding!"); 562 return ~0; 563} 564 565/// createJumpTableIndex - Create a new jump table entry in the jump table info. 566/// 567unsigned MachineJumpTableInfo::createJumpTableIndex( 568 const std::vector<MachineBasicBlock*> &DestBBs) { 569 assert(!DestBBs.empty() && "Cannot create an empty jump table!"); 570 JumpTables.push_back(MachineJumpTableEntry(DestBBs)); 571 return JumpTables.size()-1; 572} 573 574/// ReplaceMBBInJumpTables - If Old is the target of any jump tables, update 575/// the jump tables to branch to New instead. 576bool MachineJumpTableInfo::ReplaceMBBInJumpTables(MachineBasicBlock *Old, 577 MachineBasicBlock *New) { 578 assert(Old != New && "Not making a change?"); 579 bool MadeChange = false; 580 for (size_t i = 0, e = JumpTables.size(); i != e; ++i) 581 ReplaceMBBInJumpTable(i, Old, New); 582 return MadeChange; 583} 584 585/// ReplaceMBBInJumpTable - If Old is a target of the jump tables, update 586/// the jump table to branch to New instead. 587bool MachineJumpTableInfo::ReplaceMBBInJumpTable(unsigned Idx, 588 MachineBasicBlock *Old, 589 MachineBasicBlock *New) { 590 assert(Old != New && "Not making a change?"); 591 bool MadeChange = false; 592 MachineJumpTableEntry &JTE = JumpTables[Idx]; 593 for (size_t j = 0, e = JTE.MBBs.size(); j != e; ++j) 594 if (JTE.MBBs[j] == Old) { 595 JTE.MBBs[j] = New; 596 MadeChange = true; 597 } 598 return MadeChange; 599} 600 601void MachineJumpTableInfo::print(raw_ostream &OS) const { 602 if (JumpTables.empty()) return; 603 604 OS << "Jump Tables:\n"; 605 606 for (unsigned i = 0, e = JumpTables.size(); i != e; ++i) { 607 OS << " jt#" << i << ": "; 608 for (unsigned j = 0, f = JumpTables[i].MBBs.size(); j != f; ++j) 609 OS << " BB#" << JumpTables[i].MBBs[j]->getNumber(); 610 } 611 612 OS << '\n'; 613} 614 615void MachineJumpTableInfo::dump() const { print(dbgs()); } 616 617 618//===----------------------------------------------------------------------===// 619// MachineConstantPool implementation 620//===----------------------------------------------------------------------===// 621 622const Type *MachineConstantPoolEntry::getType() const { 623 if (isMachineConstantPoolEntry()) 624 return Val.MachineCPVal->getType(); 625 return Val.ConstVal->getType(); 626} 627 628 629unsigned MachineConstantPoolEntry::getRelocationInfo() const { 630 if (isMachineConstantPoolEntry()) 631 return Val.MachineCPVal->getRelocationInfo(); 632 return Val.ConstVal->getRelocationInfo(); 633} 634 635MachineConstantPool::~MachineConstantPool() { 636 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 637 if (Constants[i].isMachineConstantPoolEntry()) 638 delete Constants[i].Val.MachineCPVal; 639 for (DenseSet<MachineConstantPoolValue*>::iterator I = 640 MachineCPVsSharingEntries.begin(), E = MachineCPVsSharingEntries.end(); 641 I != E; ++I) 642 delete *I; 643} 644 645/// CanShareConstantPoolEntry - Test whether the given two constants 646/// can be allocated the same constant pool entry. 647static bool CanShareConstantPoolEntry(const Constant *A, const Constant *B, 648 const TargetData *TD) { 649 // Handle the trivial case quickly. 650 if (A == B) return true; 651 652 // If they have the same type but weren't the same constant, quickly 653 // reject them. 654 if (A->getType() == B->getType()) return false; 655 656 // For now, only support constants with the same size. 657 if (TD->getTypeStoreSize(A->getType()) != TD->getTypeStoreSize(B->getType())) 658 return false; 659 660 // If a floating-point value and an integer value have the same encoding, 661 // they can share a constant-pool entry. 662 if (const ConstantFP *AFP = dyn_cast<ConstantFP>(A)) 663 if (const ConstantInt *BI = dyn_cast<ConstantInt>(B)) 664 return AFP->getValueAPF().bitcastToAPInt() == BI->getValue(); 665 if (const ConstantFP *BFP = dyn_cast<ConstantFP>(B)) 666 if (const ConstantInt *AI = dyn_cast<ConstantInt>(A)) 667 return BFP->getValueAPF().bitcastToAPInt() == AI->getValue(); 668 669 // Two vectors can share an entry if each pair of corresponding 670 // elements could. 671 if (const ConstantVector *AV = dyn_cast<ConstantVector>(A)) 672 if (const ConstantVector *BV = dyn_cast<ConstantVector>(B)) { 673 if (AV->getType()->getNumElements() != BV->getType()->getNumElements()) 674 return false; 675 for (unsigned i = 0, e = AV->getType()->getNumElements(); i != e; ++i) 676 if (!CanShareConstantPoolEntry(AV->getOperand(i), 677 BV->getOperand(i), TD)) 678 return false; 679 return true; 680 } 681 682 // TODO: Handle other cases. 683 684 return false; 685} 686 687/// getConstantPoolIndex - Create a new entry in the constant pool or return 688/// an existing one. User must specify the log2 of the minimum required 689/// alignment for the object. 690/// 691unsigned MachineConstantPool::getConstantPoolIndex(const Constant *C, 692 unsigned Alignment) { 693 assert(Alignment && "Alignment must be specified!"); 694 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 695 696 // Check to see if we already have this constant. 697 // 698 // FIXME, this could be made much more efficient for large constant pools. 699 for (unsigned i = 0, e = Constants.size(); i != e; ++i) 700 if (!Constants[i].isMachineConstantPoolEntry() && 701 CanShareConstantPoolEntry(Constants[i].Val.ConstVal, C, TD)) { 702 if ((unsigned)Constants[i].getAlignment() < Alignment) 703 Constants[i].Alignment = Alignment; 704 return i; 705 } 706 707 Constants.push_back(MachineConstantPoolEntry(C, Alignment)); 708 return Constants.size()-1; 709} 710 711unsigned MachineConstantPool::getConstantPoolIndex(MachineConstantPoolValue *V, 712 unsigned Alignment) { 713 assert(Alignment && "Alignment must be specified!"); 714 if (Alignment > PoolAlignment) PoolAlignment = Alignment; 715 716 // Check to see if we already have this constant. 717 // 718 // FIXME, this could be made much more efficient for large constant pools. 719 int Idx = V->getExistingMachineCPValue(this, Alignment); 720 if (Idx != -1) { 721 MachineCPVsSharingEntries.insert(V); 722 return (unsigned)Idx; 723 } 724 725 Constants.push_back(MachineConstantPoolEntry(V, Alignment)); 726 return Constants.size()-1; 727} 728 729void MachineConstantPool::print(raw_ostream &OS) const { 730 if (Constants.empty()) return; 731 732 OS << "Constant Pool:\n"; 733 for (unsigned i = 0, e = Constants.size(); i != e; ++i) { 734 OS << " cp#" << i << ": "; 735 if (Constants[i].isMachineConstantPoolEntry()) 736 Constants[i].Val.MachineCPVal->print(OS); 737 else 738 OS << *(Value*)Constants[i].Val.ConstVal; 739 OS << ", align=" << Constants[i].getAlignment(); 740 OS << "\n"; 741 } 742} 743 744void MachineConstantPool::dump() const { print(dbgs()); } 745