MachineFunction.h revision f5395cee6a24699a016b2e379cf4804b09ce5030
19c6342d0cc919711ea1024b98b1b387573995922Chris Lattner//===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===// 2ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// 36fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// The LLVM Compiler Infrastructure 46fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// 56fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// This file was developed by the LLVM research group and is distributed under 66fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details. 7ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// 86fbcc26f1460eaee4e0eb8b426fc1ff0c7af11beJohn Criswell//===----------------------------------------------------------------------===// 9ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// 10cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner// Collect native machine code for a function. This class contains a list of 11cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner// MachineBasicBlock instances that make up the current compiled function. 12cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner// 13cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner// This class also contains pointers to various classes which hold 14cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner// target-specific information about the generated code. 15ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// 16f2868ce228ca20f72d1c6fbe241de01975cfe609Chris Lattner//===----------------------------------------------------------------------===// 17f2868ce228ca20f72d1c6fbe241de01975cfe609Chris Lattner 18fce1143bcfa73f61845002fa50473d1a01384202Misha Brukman#ifndef LLVM_CODEGEN_MACHINEFUNCTION_H 19fce1143bcfa73f61845002fa50473d1a01384202Misha Brukman#define LLVM_CODEGEN_MACHINEFUNCTION_H 20f2868ce228ca20f72d1c6fbe241de01975cfe609Chris Lattner 21f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey#include "llvm/CodeGen/MachineDebugInfo.h" 220551f542ab4c99420c6fbe471987f41d640ef170Chris Lattner#include "llvm/CodeGen/MachineBasicBlock.h" 23551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Annotation.h" 248e7ae9860bd1f29c95e4e10fe151a22aaafafef9Chris Lattner 25d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 26d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 278bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattnerclass Function; 288bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattnerclass TargetMachine; 298bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattnerclass SSARegMap; 308bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattnerclass MachineFrameInfo; 318bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattnerclass MachineConstantPool; 328bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner 33792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner// ilist_traits 34792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattnertemplate <> 351fca5ff62bb2ecb5bfc8974f4dbfc56e9d3ca721Chris Lattnerstruct ilist_traits<MachineBasicBlock> { 36792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner // this is only set by the MachineFunction owning the ilist 37792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner friend class MachineFunction; 3817fb34bf8cd10a798c9206eeef3bff151b4d3688Tanya Lattner MachineFunction* Parent; 39ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 40792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattnerpublic: 4117fb34bf8cd10a798c9206eeef3bff151b4d3688Tanya Lattner ilist_traits<MachineBasicBlock>() : Parent(0) { } 42ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 43792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner static MachineBasicBlock* getPrev(MachineBasicBlock* N) { return N->Prev; } 44792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner static MachineBasicBlock* getNext(MachineBasicBlock* N) { return N->Next; } 45ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 46792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner static const MachineBasicBlock* 47792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner getPrev(const MachineBasicBlock* N) { return N->Prev; } 48ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 49792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner static const MachineBasicBlock* 50792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner getNext(const MachineBasicBlock* N) { return N->Next; } 51ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 522fcd451a5e909857f81215bf5a77a2426ffab984Chris Lattner static void setPrev(MachineBasicBlock* N, MachineBasicBlock* prev) { 532fcd451a5e909857f81215bf5a77a2426ffab984Chris Lattner N->Prev = prev; 542fcd451a5e909857f81215bf5a77a2426ffab984Chris Lattner } 552fcd451a5e909857f81215bf5a77a2426ffab984Chris Lattner static void setNext(MachineBasicBlock* N, MachineBasicBlock* next) { 562fcd451a5e909857f81215bf5a77a2426ffab984Chris Lattner N->Next = next; 572fcd451a5e909857f81215bf5a77a2426ffab984Chris Lattner } 58274ba03f49e78ccb5670292a8cab53009dcca896Misha Brukman 59bbec41dbaca7ced24f77ec7173f0c1e317aae908Chris Lattner static MachineBasicBlock* createSentinel(); 60bbec41dbaca7ced24f77ec7173f0c1e317aae908Chris Lattner static void destroySentinel(MachineBasicBlock *MBB) { delete MBB; } 61792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner void addNodeToList(MachineBasicBlock* N); 62792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner void removeNodeFromList(MachineBasicBlock* N); 632fcd451a5e909857f81215bf5a77a2426ffab984Chris Lattner void transferNodesFromList(iplist<MachineBasicBlock, 642fcd451a5e909857f81215bf5a77a2426ffab984Chris Lattner ilist_traits<MachineBasicBlock> > &toList, 6500876a2808f1a8061f7e0852c7949fc5074ecb04Misha Brukman ilist_iterator<MachineBasicBlock> first, 6600876a2808f1a8061f7e0852c7949fc5074ecb04Misha Brukman ilist_iterator<MachineBasicBlock> last); 67792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner}; 68792699c46ef9bfc47dd459bbfa7e71bcb2cee29aTanya Lattner 698bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner/// MachineFunctionInfo - This class can be derived from and used by targets to 708bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner/// hold private target-specific information for each MachineFunction. Objects 718bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner/// of type are accessed/created with MF::getInfo and destroyed when the 728bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner/// MachineFunction is destroyed. 738bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattnerstruct MachineFunctionInfo { 748bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner virtual ~MachineFunctionInfo() {}; 758bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner}; 76f2868ce228ca20f72d1c6fbe241de01975cfe609Chris Lattner 77fce1143bcfa73f61845002fa50473d1a01384202Misha Brukmanclass MachineFunction : private Annotation { 78335d5c399b52c2507ad11048310edc51462f4811Chris Lattner const Function *Fn; 79335d5c399b52c2507ad11048310edc51462f4811Chris Lattner const TargetMachine &Target; 808e7ae9860bd1f29c95e4e10fe151a22aaafafef9Chris Lattner 818e7ae9860bd1f29c95e4e10fe151a22aaafafef9Chris Lattner // List of machine basic blocks in function 8217fb34bf8cd10a798c9206eeef3bff151b4d3688Tanya Lattner ilist<MachineBasicBlock> BasicBlocks; 839c6342d0cc919711ea1024b98b1b387573995922Chris Lattner 8403ab7af4dec01a3003e469c458e37bbf4ce4f2f3Chris Lattner // Keeping track of mapping from SSA values to registers 8503ab7af4dec01a3003e469c458e37bbf4ce4f2f3Chris Lattner SSARegMap *SSARegMapping; 8603ab7af4dec01a3003e469c458e37bbf4ce4f2f3Chris Lattner 878bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner // Used to keep track of target-specific per-machine function information for 888bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner // the target implementation. 898bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner MachineFunctionInfo *MFInfo; 90cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner 91cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner // Keep track of objects allocated on the stack. 92aa09b75d2257545c4583265c7ce10f2d0e3be72bChris Lattner MachineFrameInfo *FrameInfo; 931617e6c9974813030c8294243bf9e152f89ffd9bMisha Brukman 9440a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner // Keep track of constants which are spilled to memory 9540a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner MachineConstantPool *ConstantPool; 9640a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner 9725d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner // Function-level unique numbering for MachineBasicBlocks. When a 9825d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner // MachineBasicBlock is inserted into a MachineFunction is it automatically 9925d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner // numbered and this vector keeps track of the mapping from ID's to MBB's. 10025d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner std::vector<MachineBasicBlock*> MBBNumbering; 101f460f1679e8ac1935220e1878fd97cf44f093659Brian Gaeke 102d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// UsedPhysRegs - This is a new[]'d array of bools that is computed and set 103d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// by the register allocator, and must be kept up to date by passes that run 104d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// after register allocation (though most don't modify this). This is used 105d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// so that the code generator knows which callee save registers to save and 106d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// for other target specific uses. 107d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner bool *UsedPhysRegs; 108d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner 109dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner /// LiveIns/LiveOuts - Keep track of the physical registers that are 110dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner /// livein/liveout of the function. Live in values are typically arguments in 111dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner /// registers, live out values are typically return values in registers. 112712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner /// LiveIn values are allowed to have virtual registers associated with them, 113712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner /// stored in the second element. 114712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner std::vector<std::pair<unsigned, unsigned> > LiveIns; 115712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner std::vector<unsigned> LiveOuts; 116f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey 117f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey /// DebugInfo - Keep track of debug information for the function. 118f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey /// 119f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey MachineDebugInfo DebugInfo; 120dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner 121f2868ce228ca20f72d1c6fbe241de01975cfe609Chris Lattnerpublic: 12240a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner MachineFunction(const Function *Fn, const TargetMachine &TM); 12303ab7af4dec01a3003e469c458e37bbf4ce4f2f3Chris Lattner ~MachineFunction(); 124335d5c399b52c2507ad11048310edc51462f4811Chris Lattner 125335d5c399b52c2507ad11048310edc51462f4811Chris Lattner /// getFunction - Return the LLVM function that this machine code represents 126335d5c399b52c2507ad11048310edc51462f4811Chris Lattner /// 127335d5c399b52c2507ad11048310edc51462f4811Chris Lattner const Function *getFunction() const { return Fn; } 128335d5c399b52c2507ad11048310edc51462f4811Chris Lattner 129335d5c399b52c2507ad11048310edc51462f4811Chris Lattner /// getTarget - Return the target machine this machine code is compiled with 130335d5c399b52c2507ad11048310edc51462f4811Chris Lattner /// 131335d5c399b52c2507ad11048310edc51462f4811Chris Lattner const TargetMachine &getTarget() const { return Target; } 132dea73839f69b004314d223f75d136551132108ccChris Lattner 133cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// SSARegMap Interface... Keep track of information about each SSA virtual 134cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// register, such as which register class it belongs to. 135cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// 136cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner SSARegMap *getSSARegMap() const { return SSARegMapping; } 137cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner void clearSSARegMap(); 138cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner 139cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// getFrameInfo - Return the frame info object for the current function. 140cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// This object contains information about objects allocated on the stack 141cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// frame of the current function in an abstract way. 142cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// 143aa09b75d2257545c4583265c7ce10f2d0e3be72bChris Lattner MachineFrameInfo *getFrameInfo() const { return FrameInfo; } 144cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner 14540a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner /// getConstantPool - Return the constant pool object for the current 14640a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner /// function. 147274ba03f49e78ccb5670292a8cab53009dcca896Misha Brukman /// 14840a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner MachineConstantPool *getConstantPool() const { return ConstantPool; } 14940a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner 150cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// MachineFunctionInfo - Keep track of various per-function pieces of 15167d2562be7d69bd56f08b7225ed8c82711a4113fChris Lattner /// information for backends that would like to do so. 152cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner /// 1538bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner template<typename Ty> 1548bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner Ty *getInfo() { 1558bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner if (!MFInfo) MFInfo = new Ty(*this); 1568bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner 1578bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner assert((void*)dynamic_cast<Ty*>(MFInfo) == (void*)MFInfo && 1588bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner "Invalid concrete type or multiple inheritence for getInfo"); 1598bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner return static_cast<Ty*>(MFInfo); 1608bdf87d1d8abde45c40b2698dd9af8407007281cChris Lattner } 161cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner 162d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// setUsedPhysRegs - The register allocator should call this to initialized 163d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// the UsedPhysRegs set. This should be passed a new[]'d array with entries 164d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// for all of the physical registers that the target supports. Each array 165d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// entry should be set to true iff the physical register is used within the 166d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// function. 167d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner void setUsedPhysRegs(bool *UPR) { UsedPhysRegs = UPR; } 168d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner 169d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// getUsedPhysregs - This returns the UsedPhysRegs array. This returns null 170d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// before register allocation. 171471f09090d78a4915c020e094a2ac81ce9bb34e0Chris Lattner bool *getUsedPhysregs() { return UsedPhysRegs; } 172471f09090d78a4915c020e094a2ac81ce9bb34e0Chris Lattner const bool *getUsedPhysregs() const { return UsedPhysRegs; } 173d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner 174d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// isPhysRegUsed - Return true if the specified register is used in this 175d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// function. This only works after register allocation. 176d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner bool isPhysRegUsed(unsigned Reg) { return UsedPhysRegs[Reg]; } 177d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner 178d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// changePhyRegUsed - This method allows code that runs after register 179d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner /// allocation to keep the PhysRegsUsed array up-to-date. 180d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner void changePhyRegUsed(unsigned Reg, bool State) { UsedPhysRegs[Reg] = State; } 181d6b210ca3808a0e0ab9e1e2aefb73d61462fff0aChris Lattner 182dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner 183dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner // LiveIn/LiveOut management methods. 184dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner 185dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner /// addLiveIn/Out - Add the specified register as a live in/out. Note that it 186dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner /// is an error to add the same register to the same set more than once. 187712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner void addLiveIn(unsigned Reg, unsigned vreg = 0) { 188712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner LiveIns.push_back(std::make_pair(Reg, vreg)); 189712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner } 190dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); } 191ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 192dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner // Iteration support for live in/out sets. These sets are kept in sorted 193dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner // order by their register number. 194712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator 195712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner livein_iterator; 196712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner typedef std::vector<unsigned>::const_iterator liveout_iterator; 197712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner livein_iterator livein_begin() const { return LiveIns.begin(); } 198712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner livein_iterator livein_end() const { return LiveIns.end(); } 199712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner liveout_iterator liveout_begin() const { return LiveOuts.begin(); } 200712ad0c36dcfacb30620c793a6ffe4e80bd5d569Chris Lattner liveout_iterator liveout_end() const { return LiveOuts.end(); } 201dde3a9abc3c44ccc9cbf0c713f735d23afbcf301Chris Lattner 20225d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they 20325d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// are inserted into the machine function. The block number for a machine 20425d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// basic block can be found by using the MBB::getBlockNumber method, this 20525d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// method provides the inverse mapping. 206274ba03f49e78ccb5670292a8cab53009dcca896Misha Brukman /// 20725d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner MachineBasicBlock *getBlockNumbered(unsigned N) { 20825d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner assert(N < MBBNumbering.size() && "Illegal block number"); 20925d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner assert(MBBNumbering[N] && "Block was removed from the machine function!"); 21025d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner return MBBNumbering[N]; 21125d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner } 212cd0d1d1f0f44773b5c4bb57f557e6187891ff1d5Chris Lattner 213fec656c44cfec5566f94ff54e1429387aea5a6ceAlkis Evlogimenos /// getLastBlock - Returns the MachineBasicBlock with the greatest number 214fec656c44cfec5566f94ff54e1429387aea5a6ceAlkis Evlogimenos MachineBasicBlock *getLastBlock() { 215fec656c44cfec5566f94ff54e1429387aea5a6ceAlkis Evlogimenos return MBBNumbering.back(); 216fec656c44cfec5566f94ff54e1429387aea5a6ceAlkis Evlogimenos } 2174b97f23a0099bacb77d4262b47c7692417d378dfAlkis Evlogimenos const MachineBasicBlock *getLastBlock() const { 2184b97f23a0099bacb77d4262b47c7692417d378dfAlkis Evlogimenos return MBBNumbering.back(); 2194b97f23a0099bacb77d4262b47c7692417d378dfAlkis Evlogimenos } 220f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey 221f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey /// getDebugInfo - Returns the DebugInfo. 222f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey MachineDebugInfo &getDebugInfo() { 223f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey return DebugInfo; 224f5395cee6a24699a016b2e379cf4804b09ce5030Jim Laskey } 225fec656c44cfec5566f94ff54e1429387aea5a6ceAlkis Evlogimenos 226dea73839f69b004314d223f75d136551132108ccChris Lattner /// print - Print out the MachineFunction in a format suitable for debugging 227dea73839f69b004314d223f75d136551132108ccChris Lattner /// to the specified stream. 228dea73839f69b004314d223f75d136551132108ccChris Lattner /// 229dea73839f69b004314d223f75d136551132108ccChris Lattner void print(std::ostream &OS) const; 230dea73839f69b004314d223f75d136551132108ccChris Lattner 23171bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// viewCFG - This function is meant for use from the debugger. You can just 23271bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// say 'call F->viewCFG()' and a ghostview window should pop up from the 23371bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// program, displaying the CFG of the current function with the code for each 23471bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// basic block inside. This depends on there being a 'dot' and 'gv' program 23571bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// in your path. 23671bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// 23771bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos void viewCFG() const; 238ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 23971bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// viewCFGOnly - This function is meant for use from the debugger. It works 24071bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// just like viewCFG, but it does not include the contents of basic blocks 24171bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// into the nodes, just the label. If you are only interested in the CFG 24271bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// this can make the graph smaller. 24371bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos /// 24471bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos void viewCFGOnly() const; 24571bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos 246dea73839f69b004314d223f75d136551132108ccChris Lattner /// dump - Print the current MachineFunction to cerr, useful for debugger use. 247dea73839f69b004314d223f75d136551132108ccChris Lattner /// 248dea73839f69b004314d223f75d136551132108ccChris Lattner void dump() const; 249dea73839f69b004314d223f75d136551132108ccChris Lattner 250a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// construct - Allocate and initialize a MachineFunction for a given Function 251a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// and Target 252a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// 25340a75577ea6b4fb9cc92780ca0caa21604246b1bChris Lattner static MachineFunction& construct(const Function *F, const TargetMachine &TM); 254a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman 255a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// destruct - Destroy the MachineFunction corresponding to a given Function 256a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// 257e7506a366e8bd56c97d10beb68e4db953aebaecaChris Lattner static void destruct(const Function *F); 258a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman 259a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// get - Return a handle to a MachineFunction corresponding to the given 260a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// Function. This should not be called before "construct()" for a given 261a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// Function. 262a7afa37f3c8a8757e5729ddc22df0bc911bf6024Misha Brukman /// 263d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner static MachineFunction& get(const Function *F); 264d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner 265d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner // Provide accessors for the MachineBasicBlock list... 26617fb34bf8cd10a798c9206eeef3bff151b4d3688Tanya Lattner typedef ilist<MachineBasicBlock> BasicBlockListType; 267d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner typedef BasicBlockListType::iterator iterator; 268d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner typedef BasicBlockListType::const_iterator const_iterator; 269d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 270d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner typedef std::reverse_iterator<iterator> reverse_iterator; 271d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner 272d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner // Provide accessors for basic blocks... 273d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner const BasicBlockListType &getBasicBlockList() const { return BasicBlocks; } 274d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner BasicBlockListType &getBasicBlockList() { return BasicBlocks; } 275ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman 276d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner //===--------------------------------------------------------------------===// 277d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner // BasicBlock iterator forwarding functions 278d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner // 279d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner iterator begin() { return BasicBlocks.begin(); } 280d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner const_iterator begin() const { return BasicBlocks.begin(); } 281d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner iterator end () { return BasicBlocks.end(); } 282d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner const_iterator end () const { return BasicBlocks.end(); } 283d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner 284d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner reverse_iterator rbegin() { return BasicBlocks.rbegin(); } 285d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); } 286d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner reverse_iterator rend () { return BasicBlocks.rend(); } 287d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner const_reverse_iterator rend () const { return BasicBlocks.rend(); } 2889c6342d0cc919711ea1024b98b1b387573995922Chris Lattner 289d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner unsigned size() const { return BasicBlocks.size(); } 290d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner bool empty() const { return BasicBlocks.empty(); } 291d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner const MachineBasicBlock &front() const { return BasicBlocks.front(); } 292d0aa0cdbc6fee00f2b2019633a9b9d00d301ac68Chris Lattner MachineBasicBlock &front() { return BasicBlocks.front(); } 293d5806ffb40899aca4f3c23442645ed0ef4fa390fMisha Brukman const MachineBasicBlock & back() const { return BasicBlocks.back(); } 294d5806ffb40899aca4f3c23442645ed0ef4fa390fMisha Brukman MachineBasicBlock & back() { return BasicBlocks.back(); } 29525d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner 29625d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner //===--------------------------------------------------------------------===// 29725d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner // Internal functions used to automatically number MachineBasicBlocks 29825d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner // 29925d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner 30025d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// getNextMBBNumber - Returns the next unique number to be assigned 30125d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// to a MachineBasicBlock in this MachineFunction. 30225d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// 30325d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner unsigned addToMBBNumbering(MachineBasicBlock *MBB) { 30425d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner MBBNumbering.push_back(MBB); 30525d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner return MBBNumbering.size()-1; 30625d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner } 30725d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner 30825d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// removeFromMBBNumbering - Remove the specific machine basic block from our 30925d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// tracker, this is only really to be used by the MachineBasicBlock 31025d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner /// implementation. 31125d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner void removeFromMBBNumbering(unsigned N) { 31225d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner assert(N < MBBNumbering.size() && "Illegal basic block #"); 31325d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner MBBNumbering[N] = 0; 31425d8039bd6bdb53330c2d0968a5505e42d568048Chris Lattner } 315f2868ce228ca20f72d1c6fbe241de01975cfe609Chris Lattner}; 316f2868ce228ca20f72d1c6fbe241de01975cfe609Chris Lattner 31771bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos//===--------------------------------------------------------------------===// 31871bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// GraphTraits specializations for function basic block graphs (CFGs) 31971bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos//===--------------------------------------------------------------------===// 32071bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos 32171bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// Provide specializations of GraphTraits to be able to treat a 32271bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// machine function as a graph of machine basic blocks... these are 32371bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// the same as the machine basic block iterators, except that the root 32471bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// node is implicitly the first node of the function. 32571bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// 32671bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenostemplate <> struct GraphTraits<MachineFunction*> : 32771bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos public GraphTraits<MachineBasicBlock*> { 32871bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos static NodeType *getEntryNode(MachineFunction *F) { 32971bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos return &F->front(); 33071bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos } 33171bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos 33271bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 33371bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos typedef MachineFunction::iterator nodes_iterator; 33471bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); } 33571bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); } 33671bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos}; 33771bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenostemplate <> struct GraphTraits<const MachineFunction*> : 33871bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos public GraphTraits<const MachineBasicBlock*> { 33971bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos static NodeType *getEntryNode(const MachineFunction *F) { 34071bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos return &F->front(); 34171bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos } 34271bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos 34371bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 34471bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos typedef MachineFunction::const_iterator nodes_iterator; 34571bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos static nodes_iterator nodes_begin(const MachineFunction *F) { return F->begin(); } 34671bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos static nodes_iterator nodes_end (const MachineFunction *F) { return F->end(); } 34771bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos}; 34871bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos 34971bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos 350ea61c358720aa6c7a159d51658b34276316aa841Misha Brukman// Provide specializations of GraphTraits to be able to treat a function as a 35171bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// graph of basic blocks... and to walk it in inverse order. Inverse order for 35271bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// a function is considered to be when traversing the predecessor edges of a BB 35371bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// instead of the successor edges. 35471bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos// 35571bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenostemplate <> struct GraphTraits<Inverse<MachineFunction*> > : 35671bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos public GraphTraits<Inverse<MachineBasicBlock*> > { 35771bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos static NodeType *getEntryNode(Inverse<MachineFunction*> G) { 35871bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos return &G.Graph->front(); 35971bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos } 36071bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos}; 36171bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenostemplate <> struct GraphTraits<Inverse<const MachineFunction*> > : 36271bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos public GraphTraits<Inverse<const MachineBasicBlock*> > { 36371bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos static NodeType *getEntryNode(Inverse<const MachineFunction *> G) { 36471bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos return &G.Graph->front(); 36571bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos } 36671bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos}; 36771bf404e9b69122c276ad7ce66f38bf1e57cafebAlkis Evlogimenos 368d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 369d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 370f2868ce228ca20f72d1c6fbe241de01975cfe609Chris Lattner#endif 371