1193323Sed//===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// Collect native machine code for a function. This class contains a list of 11193323Sed// MachineBasicBlock instances that make up the current compiled function. 12193323Sed// 13193323Sed// This class also contains pointers to various classes which hold 14193323Sed// target-specific information about the generated code. 15193323Sed// 16193323Sed//===----------------------------------------------------------------------===// 17193323Sed 18193323Sed#ifndef LLVM_CODEGEN_MACHINEFUNCTION_H 19193323Sed#define LLVM_CODEGEN_MACHINEFUNCTION_H 20193323Sed 21249423Sdim#include "llvm/ADT/ilist.h" 22198090Srdivacky#include "llvm/CodeGen/MachineBasicBlock.h" 23249423Sdim#include "llvm/Support/Allocator.h" 24249423Sdim#include "llvm/Support/ArrayRecycler.h" 25194612Sed#include "llvm/Support/DebugLoc.h" 26193323Sed#include "llvm/Support/Recycler.h" 27193323Sed 28193323Sednamespace llvm { 29193323Sed 30198090Srdivackyclass Value; 31193323Sedclass Function; 32218893Sdimclass GCModuleInfo; 33193323Sedclass MachineRegisterInfo; 34193323Sedclass MachineFrameInfo; 35193323Sedclass MachineConstantPool; 36193323Sedclass MachineJumpTableInfo; 37206274Srdivackyclass MachineModuleInfo; 38205218Srdivackyclass MCContext; 39203954Srdivackyclass Pass; 40193323Sedclass TargetMachine; 41193323Sedclass TargetRegisterClass; 42218893Sdimstruct MachinePointerInfo; 43193323Sed 44193323Sedtemplate <> 45193323Sedstruct ilist_traits<MachineBasicBlock> 46193323Sed : public ilist_default_traits<MachineBasicBlock> { 47198090Srdivacky mutable ilist_half_node<MachineBasicBlock> Sentinel; 48193323Sedpublic: 49193323Sed MachineBasicBlock *createSentinel() const { 50193323Sed return static_cast<MachineBasicBlock*>(&Sentinel); 51193323Sed } 52193323Sed void destroySentinel(MachineBasicBlock *) const {} 53193323Sed 54193323Sed MachineBasicBlock *provideInitialHead() const { return createSentinel(); } 55193323Sed MachineBasicBlock *ensureHead(MachineBasicBlock*) const { 56193323Sed return createSentinel(); 57193323Sed } 58193323Sed static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {} 59193323Sed 60193323Sed void addNodeToList(MachineBasicBlock* MBB); 61193323Sed void removeNodeFromList(MachineBasicBlock* MBB); 62193323Sed void deleteNode(MachineBasicBlock *MBB); 63193323Sedprivate: 64193323Sed void createNode(const MachineBasicBlock &); 65193323Sed}; 66193323Sed 67193323Sed/// MachineFunctionInfo - This class can be derived from and used by targets to 68193323Sed/// hold private target-specific information for each MachineFunction. Objects 69193323Sed/// of type are accessed/created with MF::getInfo and destroyed when the 70193323Sed/// MachineFunction is destroyed. 71193323Sedstruct MachineFunctionInfo { 72198090Srdivacky virtual ~MachineFunctionInfo(); 73193323Sed}; 74193323Sed 75198090Srdivackyclass MachineFunction { 76207618Srdivacky const Function *Fn; 77193323Sed const TargetMachine &Target; 78205218Srdivacky MCContext &Ctx; 79206274Srdivacky MachineModuleInfo &MMI; 80218893Sdim GCModuleInfo *GMI; 81206274Srdivacky 82193323Sed // RegInfo - Information about each register in use in the function. 83193323Sed MachineRegisterInfo *RegInfo; 84193323Sed 85193323Sed // Used to keep track of target-specific per-machine function information for 86193323Sed // the target implementation. 87193323Sed MachineFunctionInfo *MFInfo; 88193323Sed 89193323Sed // Keep track of objects allocated on the stack. 90193323Sed MachineFrameInfo *FrameInfo; 91193323Sed 92193323Sed // Keep track of constants which are spilled to memory 93193323Sed MachineConstantPool *ConstantPool; 94193323Sed 95193323Sed // Keep track of jump tables for switch instructions 96193323Sed MachineJumpTableInfo *JumpTableInfo; 97193323Sed 98193323Sed // Function-level unique numbering for MachineBasicBlocks. When a 99193323Sed // MachineBasicBlock is inserted into a MachineFunction is it automatically 100193323Sed // numbered and this vector keeps track of the mapping from ID's to MBB's. 101193323Sed std::vector<MachineBasicBlock*> MBBNumbering; 102193323Sed 103193323Sed // Pool-allocate MachineFunction-lifetime and IR objects. 104193323Sed BumpPtrAllocator Allocator; 105193323Sed 106193323Sed // Allocation management for instructions in function. 107193323Sed Recycler<MachineInstr> InstructionRecycler; 108193323Sed 109249423Sdim // Allocation management for operand arrays on instructions. 110249423Sdim ArrayRecycler<MachineOperand> OperandRecycler; 111249423Sdim 112193323Sed // Allocation management for basic blocks in function. 113193323Sed Recycler<MachineBasicBlock> BasicBlockRecycler; 114193323Sed 115193323Sed // List of machine basic blocks in function 116193323Sed typedef ilist<MachineBasicBlock> BasicBlockListType; 117193323Sed BasicBlockListType BasicBlocks; 118193323Sed 119203954Srdivacky /// FunctionNumber - This provides a unique ID for each function emitted in 120203954Srdivacky /// this translation unit. 121203954Srdivacky /// 122203954Srdivacky unsigned FunctionNumber; 123203954Srdivacky 124208599Srdivacky /// Alignment - The alignment of the function. 125195340Sed unsigned Alignment; 126195340Sed 127234353Sdim /// ExposesReturnsTwice - True if the function calls setjmp or related 128234353Sdim /// functions with attribute "returns twice", but doesn't have 129234353Sdim /// the attribute itself. 130234353Sdim /// This is used to limit optimizations which cannot reason 131234353Sdim /// about the control flow of such functions. 132234353Sdim bool ExposesReturnsTwice; 133208599Srdivacky 134263765Sdim /// True if the function includes any inline assembly. 135263765Sdim bool HasInlineAsm; 136249423Sdim 137243830Sdim MachineFunction(const MachineFunction &) LLVM_DELETED_FUNCTION; 138243830Sdim void operator=(const MachineFunction&) LLVM_DELETED_FUNCTION; 139193323Sedpublic: 140207618Srdivacky MachineFunction(const Function *Fn, const TargetMachine &TM, 141218893Sdim unsigned FunctionNum, MachineModuleInfo &MMI, 142218893Sdim GCModuleInfo* GMI); 143193323Sed ~MachineFunction(); 144193323Sed 145206274Srdivacky MachineModuleInfo &getMMI() const { return MMI; } 146218893Sdim GCModuleInfo *getGMI() const { return GMI; } 147205218Srdivacky MCContext &getContext() const { return Ctx; } 148243830Sdim 149193323Sed /// getFunction - Return the LLVM function that this machine code represents 150193323Sed /// 151207618Srdivacky const Function *getFunction() const { return Fn; } 152193323Sed 153243830Sdim /// getName - Return the name of the corresponding LLVM function. 154243830Sdim /// 155243830Sdim StringRef getName() const; 156243830Sdim 157203954Srdivacky /// getFunctionNumber - Return a unique ID for the current function. 158203954Srdivacky /// 159203954Srdivacky unsigned getFunctionNumber() const { return FunctionNumber; } 160243830Sdim 161193323Sed /// getTarget - Return the target machine this machine code is compiled with 162193323Sed /// 163193323Sed const TargetMachine &getTarget() const { return Target; } 164193323Sed 165193323Sed /// getRegInfo - Return information about the registers currently in use. 166193323Sed /// 167193323Sed MachineRegisterInfo &getRegInfo() { return *RegInfo; } 168193323Sed const MachineRegisterInfo &getRegInfo() const { return *RegInfo; } 169193323Sed 170193323Sed /// getFrameInfo - Return the frame info object for the current function. 171193323Sed /// This object contains information about objects allocated on the stack 172193323Sed /// frame of the current function in an abstract way. 173193323Sed /// 174193323Sed MachineFrameInfo *getFrameInfo() { return FrameInfo; } 175193323Sed const MachineFrameInfo *getFrameInfo() const { return FrameInfo; } 176193323Sed 177193323Sed /// getJumpTableInfo - Return the jump table info object for the current 178203954Srdivacky /// function. This object contains information about jump tables in the 179203954Srdivacky /// current function. If the current function has no jump tables, this will 180203954Srdivacky /// return null. 181203954Srdivacky const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; } 182193323Sed MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; } 183203954Srdivacky 184203954Srdivacky /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it 185203954Srdivacky /// does already exist, allocate one. 186203954Srdivacky MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind); 187203954Srdivacky 188193323Sed 189193323Sed /// getConstantPool - Return the constant pool object for the current 190193323Sed /// function. 191193323Sed /// 192193323Sed MachineConstantPool *getConstantPool() { return ConstantPool; } 193193323Sed const MachineConstantPool *getConstantPool() const { return ConstantPool; } 194193323Sed 195195340Sed /// getAlignment - Return the alignment (log2, not bytes) of the function. 196195340Sed /// 197195340Sed unsigned getAlignment() const { return Alignment; } 198195340Sed 199195340Sed /// setAlignment - Set the alignment (log2, not bytes) of the function. 200195340Sed /// 201195340Sed void setAlignment(unsigned A) { Alignment = A; } 202195340Sed 203239462Sdim /// ensureAlignment - Make sure the function is at least 1 << A bytes aligned. 204239462Sdim void ensureAlignment(unsigned A) { 205203954Srdivacky if (Alignment < A) Alignment = A; 206203954Srdivacky } 207208599Srdivacky 208234353Sdim /// exposesReturnsTwice - Returns true if the function calls setjmp or 209234353Sdim /// any other similar functions with attribute "returns twice" without 210234353Sdim /// having the attribute itself. 211234353Sdim bool exposesReturnsTwice() const { 212234353Sdim return ExposesReturnsTwice; 213208599Srdivacky } 214208599Srdivacky 215234353Sdim /// setCallsSetJmp - Set a flag that indicates if there's a call to 216234353Sdim /// a "returns twice" function. 217234353Sdim void setExposesReturnsTwice(bool B) { 218234353Sdim ExposesReturnsTwice = B; 219208599Srdivacky } 220249423Sdim 221263765Sdim /// Returns true if the function contains any inline assembly. 222263765Sdim bool hasInlineAsm() const { 223263765Sdim return HasInlineAsm; 224249423Sdim } 225249423Sdim 226263765Sdim /// Set a flag that indicates that the function contains inline assembly. 227263765Sdim void setHasInlineAsm(bool B) { 228263765Sdim HasInlineAsm = B; 229249423Sdim } 230203954Srdivacky 231198090Srdivacky /// getInfo - Keep track of various per-function pieces of information for 232198090Srdivacky /// backends that would like to do so. 233193323Sed /// 234193323Sed template<typename Ty> 235193323Sed Ty *getInfo() { 236193323Sed if (!MFInfo) { 237193323Sed // This should be just `new (Allocator.Allocate<Ty>()) Ty(*this)', but 238193323Sed // that apparently breaks GCC 3.3. 239193323Sed Ty *Loc = static_cast<Ty*>(Allocator.Allocate(sizeof(Ty), 240193323Sed AlignOf<Ty>::Alignment)); 241193323Sed MFInfo = new (Loc) Ty(*this); 242193323Sed } 243193323Sed return static_cast<Ty*>(MFInfo); 244193323Sed } 245193323Sed 246193323Sed template<typename Ty> 247193323Sed const Ty *getInfo() const { 248193323Sed return const_cast<MachineFunction*>(this)->getInfo<Ty>(); 249193323Sed } 250193323Sed 251193323Sed /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they 252193323Sed /// are inserted into the machine function. The block number for a machine 253193323Sed /// basic block can be found by using the MBB::getBlockNumber method, this 254193323Sed /// method provides the inverse mapping. 255193323Sed /// 256193323Sed MachineBasicBlock *getBlockNumbered(unsigned N) const { 257193323Sed assert(N < MBBNumbering.size() && "Illegal block number"); 258193323Sed assert(MBBNumbering[N] && "Block was removed from the machine function!"); 259193323Sed return MBBNumbering[N]; 260193323Sed } 261193323Sed 262193323Sed /// getNumBlockIDs - Return the number of MBB ID's allocated. 263193323Sed /// 264193323Sed unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); } 265193323Sed 266193323Sed /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and 267193323Sed /// recomputes them. This guarantees that the MBB numbers are sequential, 268193323Sed /// dense, and match the ordering of the blocks within the function. If a 269193323Sed /// specific MachineBasicBlock is specified, only that block and those after 270193323Sed /// it are renumbered. 271193323Sed void RenumberBlocks(MachineBasicBlock *MBBFrom = 0); 272193323Sed 273193323Sed /// print - Print out the MachineFunction in a format suitable for debugging 274193323Sed /// to the specified stream. 275193323Sed /// 276218893Sdim void print(raw_ostream &OS, SlotIndexes* = 0) const; 277193323Sed 278193323Sed /// viewCFG - This function is meant for use from the debugger. You can just 279193323Sed /// say 'call F->viewCFG()' and a ghostview window should pop up from the 280193323Sed /// program, displaying the CFG of the current function with the code for each 281193323Sed /// basic block inside. This depends on there being a 'dot' and 'gv' program 282193323Sed /// in your path. 283193323Sed /// 284193323Sed void viewCFG() const; 285193323Sed 286193323Sed /// viewCFGOnly - This function is meant for use from the debugger. It works 287193323Sed /// just like viewCFG, but it does not include the contents of basic blocks 288193323Sed /// into the nodes, just the label. If you are only interested in the CFG 289193323Sed /// this can make the graph smaller. 290193323Sed /// 291193323Sed void viewCFGOnly() const; 292193323Sed 293193323Sed /// dump - Print the current MachineFunction to cerr, useful for debugger use. 294193323Sed /// 295193323Sed void dump() const; 296193323Sed 297199481Srdivacky /// verify - Run the current MachineFunction through the machine code 298199481Srdivacky /// verifier, useful for debugger use. 299218893Sdim void verify(Pass *p = NULL, const char *Banner = NULL) const; 300199481Srdivacky 301193323Sed // Provide accessors for the MachineBasicBlock list... 302193323Sed typedef BasicBlockListType::iterator iterator; 303193323Sed typedef BasicBlockListType::const_iterator const_iterator; 304193323Sed typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 305193323Sed typedef std::reverse_iterator<iterator> reverse_iterator; 306193323Sed 307193323Sed /// addLiveIn - Add the specified physical register as a live-in value and 308193323Sed /// create a corresponding virtual register for it. 309219077Sdim unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC); 310193323Sed 311193323Sed //===--------------------------------------------------------------------===// 312193323Sed // BasicBlock accessor functions. 313193323Sed // 314193323Sed iterator begin() { return BasicBlocks.begin(); } 315193323Sed const_iterator begin() const { return BasicBlocks.begin(); } 316193323Sed iterator end () { return BasicBlocks.end(); } 317193323Sed const_iterator end () const { return BasicBlocks.end(); } 318193323Sed 319193323Sed reverse_iterator rbegin() { return BasicBlocks.rbegin(); } 320193323Sed const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); } 321193323Sed reverse_iterator rend () { return BasicBlocks.rend(); } 322193323Sed const_reverse_iterator rend () const { return BasicBlocks.rend(); } 323193323Sed 324193323Sed unsigned size() const { return (unsigned)BasicBlocks.size();} 325193323Sed bool empty() const { return BasicBlocks.empty(); } 326193323Sed const MachineBasicBlock &front() const { return BasicBlocks.front(); } 327193323Sed MachineBasicBlock &front() { return BasicBlocks.front(); } 328193323Sed const MachineBasicBlock & back() const { return BasicBlocks.back(); } 329193323Sed MachineBasicBlock & back() { return BasicBlocks.back(); } 330193323Sed 331193323Sed void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); } 332193323Sed void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); } 333193323Sed void insert(iterator MBBI, MachineBasicBlock *MBB) { 334193323Sed BasicBlocks.insert(MBBI, MBB); 335193323Sed } 336193323Sed void splice(iterator InsertPt, iterator MBBI) { 337193323Sed BasicBlocks.splice(InsertPt, BasicBlocks, MBBI); 338193323Sed } 339198396Srdivacky void splice(iterator InsertPt, iterator MBBI, iterator MBBE) { 340198396Srdivacky BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE); 341198396Srdivacky } 342193323Sed 343193323Sed void remove(iterator MBBI) { 344193323Sed BasicBlocks.remove(MBBI); 345193323Sed } 346193323Sed void erase(iterator MBBI) { 347193323Sed BasicBlocks.erase(MBBI); 348193323Sed } 349193323Sed 350193323Sed //===--------------------------------------------------------------------===// 351193323Sed // Internal functions used to automatically number MachineBasicBlocks 352193323Sed // 353193323Sed 354251662Sdim /// \brief Adds the MBB to the internal numbering. Returns the unique number 355251662Sdim /// assigned to the MBB. 356193323Sed /// 357193323Sed unsigned addToMBBNumbering(MachineBasicBlock *MBB) { 358193323Sed MBBNumbering.push_back(MBB); 359193323Sed return (unsigned)MBBNumbering.size()-1; 360193323Sed } 361193323Sed 362193323Sed /// removeFromMBBNumbering - Remove the specific machine basic block from our 363193323Sed /// tracker, this is only really to be used by the MachineBasicBlock 364193323Sed /// implementation. 365193323Sed void removeFromMBBNumbering(unsigned N) { 366193323Sed assert(N < MBBNumbering.size() && "Illegal basic block #"); 367193323Sed MBBNumbering[N] = 0; 368193323Sed } 369193323Sed 370193323Sed /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead 371193323Sed /// of `new MachineInstr'. 372193323Sed /// 373224145Sdim MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID, 374193323Sed DebugLoc DL, 375193323Sed bool NoImp = false); 376193323Sed 377193323Sed /// CloneMachineInstr - Create a new MachineInstr which is a copy of the 378203954Srdivacky /// 'Orig' instruction, identical in all ways except the instruction 379193323Sed /// has no parent, prev, or next. 380193323Sed /// 381202375Srdivacky /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned 382202375Srdivacky /// instructions. 383193323Sed MachineInstr *CloneMachineInstr(const MachineInstr *Orig); 384193323Sed 385193323Sed /// DeleteMachineInstr - Delete the given MachineInstr. 386193323Sed /// 387193323Sed void DeleteMachineInstr(MachineInstr *MI); 388193323Sed 389193323Sed /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this 390193323Sed /// instead of `new MachineBasicBlock'. 391193323Sed /// 392193323Sed MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = 0); 393193323Sed 394193323Sed /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. 395193323Sed /// 396193323Sed void DeleteMachineBasicBlock(MachineBasicBlock *MBB); 397193323Sed 398198090Srdivacky /// getMachineMemOperand - Allocate a new MachineMemOperand. 399198090Srdivacky /// MachineMemOperands are owned by the MachineFunction and need not be 400198090Srdivacky /// explicitly deallocated. 401218893Sdim MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo, 402218893Sdim unsigned f, uint64_t s, 403218893Sdim unsigned base_alignment, 404234353Sdim const MDNode *TBAAInfo = 0, 405234353Sdim const MDNode *Ranges = 0); 406218893Sdim 407198090Srdivacky /// getMachineMemOperand - Allocate a new MachineMemOperand by copying 408198090Srdivacky /// an existing one, adjusting by an offset and using the given size. 409198090Srdivacky /// MachineMemOperands are owned by the MachineFunction and need not be 410198090Srdivacky /// explicitly deallocated. 411198090Srdivacky MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO, 412198090Srdivacky int64_t Offset, uint64_t Size); 413198090Srdivacky 414249423Sdim typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity; 415249423Sdim 416249423Sdim /// Allocate an array of MachineOperands. This is only intended for use by 417249423Sdim /// internal MachineInstr functions. 418249423Sdim MachineOperand *allocateOperandArray(OperandCapacity Cap) { 419249423Sdim return OperandRecycler.allocate(Cap, Allocator); 420249423Sdim } 421249423Sdim 422249423Sdim /// Dellocate an array of MachineOperands and recycle the memory. This is 423249423Sdim /// only intended for use by internal MachineInstr functions. 424249423Sdim /// Cap must be the same capacity that was used to allocate the array. 425249423Sdim void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) { 426249423Sdim OperandRecycler.deallocate(Cap, Array); 427249423Sdim } 428249423Sdim 429198090Srdivacky /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand 430198090Srdivacky /// pointers. This array is owned by the MachineFunction. 431198090Srdivacky MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num); 432198090Srdivacky 433198090Srdivacky /// extractLoadMemRefs - Allocate an array and populate it with just the 434198090Srdivacky /// load information from the given MachineMemOperand sequence. 435198090Srdivacky std::pair<MachineInstr::mmo_iterator, 436198090Srdivacky MachineInstr::mmo_iterator> 437198090Srdivacky extractLoadMemRefs(MachineInstr::mmo_iterator Begin, 438198090Srdivacky MachineInstr::mmo_iterator End); 439198090Srdivacky 440198090Srdivacky /// extractStoreMemRefs - Allocate an array and populate it with just the 441198090Srdivacky /// store information from the given MachineMemOperand sequence. 442198090Srdivacky std::pair<MachineInstr::mmo_iterator, 443198090Srdivacky MachineInstr::mmo_iterator> 444198090Srdivacky extractStoreMemRefs(MachineInstr::mmo_iterator Begin, 445198090Srdivacky MachineInstr::mmo_iterator End); 446198090Srdivacky 447193323Sed //===--------------------------------------------------------------------===// 448203954Srdivacky // Label Manipulation. 449203954Srdivacky // 450203954Srdivacky 451203954Srdivacky /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table. 452203954Srdivacky /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 453203954Srdivacky /// normal 'L' label is returned. 454203954Srdivacky MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx, 455203954Srdivacky bool isLinkerPrivate = false) const; 456218893Sdim 457218893Sdim /// getPICBaseSymbol - Return a function-local symbol to represent the PIC 458218893Sdim /// base. 459218893Sdim MCSymbol *getPICBaseSymbol() const; 460193323Sed}; 461193323Sed 462193323Sed//===--------------------------------------------------------------------===// 463193323Sed// GraphTraits specializations for function basic block graphs (CFGs) 464193323Sed//===--------------------------------------------------------------------===// 465193323Sed 466193323Sed// Provide specializations of GraphTraits to be able to treat a 467193323Sed// machine function as a graph of machine basic blocks... these are 468193323Sed// the same as the machine basic block iterators, except that the root 469193323Sed// node is implicitly the first node of the function. 470193323Sed// 471193323Sedtemplate <> struct GraphTraits<MachineFunction*> : 472193323Sed public GraphTraits<MachineBasicBlock*> { 473193323Sed static NodeType *getEntryNode(MachineFunction *F) { 474193323Sed return &F->front(); 475193323Sed } 476193323Sed 477193323Sed // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 478193323Sed typedef MachineFunction::iterator nodes_iterator; 479193323Sed static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); } 480193323Sed static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); } 481234353Sdim static unsigned size (MachineFunction *F) { return F->size(); } 482193323Sed}; 483193323Sedtemplate <> struct GraphTraits<const MachineFunction*> : 484193323Sed public GraphTraits<const MachineBasicBlock*> { 485193323Sed static NodeType *getEntryNode(const MachineFunction *F) { 486193323Sed return &F->front(); 487193323Sed } 488193323Sed 489193323Sed // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 490193323Sed typedef MachineFunction::const_iterator nodes_iterator; 491193323Sed static nodes_iterator nodes_begin(const MachineFunction *F) { 492193323Sed return F->begin(); 493193323Sed } 494193323Sed static nodes_iterator nodes_end (const MachineFunction *F) { 495193323Sed return F->end(); 496193323Sed } 497234353Sdim static unsigned size (const MachineFunction *F) { 498234353Sdim return F->size(); 499234353Sdim } 500193323Sed}; 501193323Sed 502193323Sed 503193323Sed// Provide specializations of GraphTraits to be able to treat a function as a 504193323Sed// graph of basic blocks... and to walk it in inverse order. Inverse order for 505193323Sed// a function is considered to be when traversing the predecessor edges of a BB 506193323Sed// instead of the successor edges. 507193323Sed// 508193323Sedtemplate <> struct GraphTraits<Inverse<MachineFunction*> > : 509193323Sed public GraphTraits<Inverse<MachineBasicBlock*> > { 510193323Sed static NodeType *getEntryNode(Inverse<MachineFunction*> G) { 511193323Sed return &G.Graph->front(); 512193323Sed } 513193323Sed}; 514193323Sedtemplate <> struct GraphTraits<Inverse<const MachineFunction*> > : 515193323Sed public GraphTraits<Inverse<const MachineBasicBlock*> > { 516193323Sed static NodeType *getEntryNode(Inverse<const MachineFunction *> G) { 517193323Sed return &G.Graph->front(); 518193323Sed } 519193323Sed}; 520193323Sed 521193323Sed} // End llvm namespace 522193323Sed 523193323Sed#endif 524