1 //===-- llvm/CodeGen/MachineFunction.h --------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Collect native machine code for a function. This class contains a list of 11 // MachineBasicBlock instances that make up the current compiled function. 12 // 13 // This class also contains pointers to various classes which hold 14 // target-specific information about the generated code. 15 // 16 //===----------------------------------------------------------------------===// 17 18 #ifndef LLVM_CODEGEN_MACHINEFUNCTION_H 19 #define LLVM_CODEGEN_MACHINEFUNCTION_H 20 21 #include "llvm/ADT/ilist.h" 22 #include "llvm/CodeGen/MachineBasicBlock.h" 23 #include "llvm/IR/DebugLoc.h" 24 #include "llvm/IR/Metadata.h" 25 #include "llvm/Support/Allocator.h" 26 #include "llvm/Support/ArrayRecycler.h" 27 #include "llvm/Support/Recycler.h" 28 29 namespace llvm { 30 31 class Value; 32 class Function; 33 class GCModuleInfo; 34 class MachineRegisterInfo; 35 class MachineFrameInfo; 36 class MachineConstantPool; 37 class MachineJumpTableInfo; 38 class MachineModuleInfo; 39 class MCContext; 40 class Pass; 41 class PseudoSourceValueManager; 42 class TargetMachine; 43 class TargetSubtargetInfo; 44 class TargetRegisterClass; 45 struct MachinePointerInfo; 46 struct WinEHFuncInfo; 47 48 template <> 49 struct ilist_traits<MachineBasicBlock> 50 : public ilist_default_traits<MachineBasicBlock> { 51 mutable ilist_half_node<MachineBasicBlock> Sentinel; 52 public: 53 MachineBasicBlock *createSentinel() const { 54 return static_cast<MachineBasicBlock*>(&Sentinel); 55 } 56 void destroySentinel(MachineBasicBlock *) const {} 57 58 MachineBasicBlock *provideInitialHead() const { return createSentinel(); } 59 MachineBasicBlock *ensureHead(MachineBasicBlock*) const { 60 return createSentinel(); 61 } 62 static void noteHead(MachineBasicBlock*, MachineBasicBlock*) {} 63 64 void addNodeToList(MachineBasicBlock* MBB); 65 void removeNodeFromList(MachineBasicBlock* MBB); 66 void deleteNode(MachineBasicBlock *MBB); 67 private: 68 void createNode(const MachineBasicBlock &); 69 }; 70 71 /// MachineFunctionInfo - This class can be derived from and used by targets to 72 /// hold private target-specific information for each MachineFunction. Objects 73 /// of type are accessed/created with MF::getInfo and destroyed when the 74 /// MachineFunction is destroyed. 75 struct MachineFunctionInfo { 76 virtual ~MachineFunctionInfo(); 77 78 /// \brief Factory function: default behavior is to call new using the 79 /// supplied allocator. 80 /// 81 /// This function can be overridden in a derive class. 82 template<typename Ty> 83 static Ty *create(BumpPtrAllocator &Allocator, MachineFunction &MF) { 84 return new (Allocator.Allocate<Ty>()) Ty(MF); 85 } 86 }; 87 88 class MachineFunction { 89 const Function *Fn; 90 const TargetMachine &Target; 91 const TargetSubtargetInfo *STI; 92 MCContext &Ctx; 93 MachineModuleInfo &MMI; 94 95 // RegInfo - Information about each register in use in the function. 96 MachineRegisterInfo *RegInfo; 97 98 // Used to keep track of target-specific per-machine function information for 99 // the target implementation. 100 MachineFunctionInfo *MFInfo; 101 102 // Keep track of objects allocated on the stack. 103 MachineFrameInfo *FrameInfo; 104 105 // Keep track of constants which are spilled to memory 106 MachineConstantPool *ConstantPool; 107 108 // Keep track of jump tables for switch instructions 109 MachineJumpTableInfo *JumpTableInfo; 110 111 // Keeps track of Windows exception handling related data. This will be null 112 // for functions that aren't using a funclet-based EH personality. 113 WinEHFuncInfo *WinEHInfo = nullptr; 114 115 // Function-level unique numbering for MachineBasicBlocks. When a 116 // MachineBasicBlock is inserted into a MachineFunction is it automatically 117 // numbered and this vector keeps track of the mapping from ID's to MBB's. 118 std::vector<MachineBasicBlock*> MBBNumbering; 119 120 // Pool-allocate MachineFunction-lifetime and IR objects. 121 BumpPtrAllocator Allocator; 122 123 // Allocation management for instructions in function. 124 Recycler<MachineInstr> InstructionRecycler; 125 126 // Allocation management for operand arrays on instructions. 127 ArrayRecycler<MachineOperand> OperandRecycler; 128 129 // Allocation management for basic blocks in function. 130 Recycler<MachineBasicBlock> BasicBlockRecycler; 131 132 // List of machine basic blocks in function 133 typedef ilist<MachineBasicBlock> BasicBlockListType; 134 BasicBlockListType BasicBlocks; 135 136 /// FunctionNumber - This provides a unique ID for each function emitted in 137 /// this translation unit. 138 /// 139 unsigned FunctionNumber; 140 141 /// Alignment - The alignment of the function. 142 unsigned Alignment; 143 144 /// ExposesReturnsTwice - True if the function calls setjmp or related 145 /// functions with attribute "returns twice", but doesn't have 146 /// the attribute itself. 147 /// This is used to limit optimizations which cannot reason 148 /// about the control flow of such functions. 149 bool ExposesReturnsTwice; 150 151 /// True if the function includes any inline assembly. 152 bool HasInlineAsm; 153 154 // Allocation management for pseudo source values. 155 std::unique_ptr<PseudoSourceValueManager> PSVManager; 156 157 MachineFunction(const MachineFunction &) = delete; 158 void operator=(const MachineFunction&) = delete; 159 public: 160 MachineFunction(const Function *Fn, const TargetMachine &TM, 161 unsigned FunctionNum, MachineModuleInfo &MMI); 162 ~MachineFunction(); 163 164 MachineModuleInfo &getMMI() const { return MMI; } 165 MCContext &getContext() const { return Ctx; } 166 167 PseudoSourceValueManager &getPSVManager() const { return *PSVManager; } 168 169 /// Return the DataLayout attached to the Module associated to this MF. 170 const DataLayout &getDataLayout() const; 171 172 /// getFunction - Return the LLVM function that this machine code represents 173 /// 174 const Function *getFunction() const { return Fn; } 175 176 /// getName - Return the name of the corresponding LLVM function. 177 /// 178 StringRef getName() const; 179 180 /// getFunctionNumber - Return a unique ID for the current function. 181 /// 182 unsigned getFunctionNumber() const { return FunctionNumber; } 183 184 /// getTarget - Return the target machine this machine code is compiled with 185 /// 186 const TargetMachine &getTarget() const { return Target; } 187 188 /// getSubtarget - Return the subtarget for which this machine code is being 189 /// compiled. 190 const TargetSubtargetInfo &getSubtarget() const { return *STI; } 191 void setSubtarget(const TargetSubtargetInfo *ST) { STI = ST; } 192 193 /// getSubtarget - This method returns a pointer to the specified type of 194 /// TargetSubtargetInfo. In debug builds, it verifies that the object being 195 /// returned is of the correct type. 196 template<typename STC> const STC &getSubtarget() const { 197 return *static_cast<const STC *>(STI); 198 } 199 200 /// getRegInfo - Return information about the registers currently in use. 201 /// 202 MachineRegisterInfo &getRegInfo() { return *RegInfo; } 203 const MachineRegisterInfo &getRegInfo() const { return *RegInfo; } 204 205 /// getFrameInfo - Return the frame info object for the current function. 206 /// This object contains information about objects allocated on the stack 207 /// frame of the current function in an abstract way. 208 /// 209 MachineFrameInfo *getFrameInfo() { return FrameInfo; } 210 const MachineFrameInfo *getFrameInfo() const { return FrameInfo; } 211 212 /// getJumpTableInfo - Return the jump table info object for the current 213 /// function. This object contains information about jump tables in the 214 /// current function. If the current function has no jump tables, this will 215 /// return null. 216 const MachineJumpTableInfo *getJumpTableInfo() const { return JumpTableInfo; } 217 MachineJumpTableInfo *getJumpTableInfo() { return JumpTableInfo; } 218 219 /// getOrCreateJumpTableInfo - Get the JumpTableInfo for this function, if it 220 /// does already exist, allocate one. 221 MachineJumpTableInfo *getOrCreateJumpTableInfo(unsigned JTEntryKind); 222 223 /// getConstantPool - Return the constant pool object for the current 224 /// function. 225 /// 226 MachineConstantPool *getConstantPool() { return ConstantPool; } 227 const MachineConstantPool *getConstantPool() const { return ConstantPool; } 228 229 /// getWinEHFuncInfo - Return information about how the current function uses 230 /// Windows exception handling. Returns null for functions that don't use 231 /// funclets for exception handling. 232 const WinEHFuncInfo *getWinEHFuncInfo() const { return WinEHInfo; } 233 WinEHFuncInfo *getWinEHFuncInfo() { return WinEHInfo; } 234 235 /// getAlignment - Return the alignment (log2, not bytes) of the function. 236 /// 237 unsigned getAlignment() const { return Alignment; } 238 239 /// setAlignment - Set the alignment (log2, not bytes) of the function. 240 /// 241 void setAlignment(unsigned A) { Alignment = A; } 242 243 /// ensureAlignment - Make sure the function is at least 1 << A bytes aligned. 244 void ensureAlignment(unsigned A) { 245 if (Alignment < A) Alignment = A; 246 } 247 248 /// exposesReturnsTwice - Returns true if the function calls setjmp or 249 /// any other similar functions with attribute "returns twice" without 250 /// having the attribute itself. 251 bool exposesReturnsTwice() const { 252 return ExposesReturnsTwice; 253 } 254 255 /// setCallsSetJmp - Set a flag that indicates if there's a call to 256 /// a "returns twice" function. 257 void setExposesReturnsTwice(bool B) { 258 ExposesReturnsTwice = B; 259 } 260 261 /// Returns true if the function contains any inline assembly. 262 bool hasInlineAsm() const { 263 return HasInlineAsm; 264 } 265 266 /// Set a flag that indicates that the function contains inline assembly. 267 void setHasInlineAsm(bool B) { 268 HasInlineAsm = B; 269 } 270 271 /// getInfo - Keep track of various per-function pieces of information for 272 /// backends that would like to do so. 273 /// 274 template<typename Ty> 275 Ty *getInfo() { 276 if (!MFInfo) 277 MFInfo = Ty::template create<Ty>(Allocator, *this); 278 return static_cast<Ty*>(MFInfo); 279 } 280 281 template<typename Ty> 282 const Ty *getInfo() const { 283 return const_cast<MachineFunction*>(this)->getInfo<Ty>(); 284 } 285 286 /// getBlockNumbered - MachineBasicBlocks are automatically numbered when they 287 /// are inserted into the machine function. The block number for a machine 288 /// basic block can be found by using the MBB::getBlockNumber method, this 289 /// method provides the inverse mapping. 290 /// 291 MachineBasicBlock *getBlockNumbered(unsigned N) const { 292 assert(N < MBBNumbering.size() && "Illegal block number"); 293 assert(MBBNumbering[N] && "Block was removed from the machine function!"); 294 return MBBNumbering[N]; 295 } 296 297 /// Should we be emitting segmented stack stuff for the function 298 bool shouldSplitStack(); 299 300 /// getNumBlockIDs - Return the number of MBB ID's allocated. 301 /// 302 unsigned getNumBlockIDs() const { return (unsigned)MBBNumbering.size(); } 303 304 /// RenumberBlocks - This discards all of the MachineBasicBlock numbers and 305 /// recomputes them. This guarantees that the MBB numbers are sequential, 306 /// dense, and match the ordering of the blocks within the function. If a 307 /// specific MachineBasicBlock is specified, only that block and those after 308 /// it are renumbered. 309 void RenumberBlocks(MachineBasicBlock *MBBFrom = nullptr); 310 311 /// print - Print out the MachineFunction in a format suitable for debugging 312 /// to the specified stream. 313 /// 314 void print(raw_ostream &OS, SlotIndexes* = nullptr) const; 315 316 /// viewCFG - This function is meant for use from the debugger. You can just 317 /// say 'call F->viewCFG()' and a ghostview window should pop up from the 318 /// program, displaying the CFG of the current function with the code for each 319 /// basic block inside. This depends on there being a 'dot' and 'gv' program 320 /// in your path. 321 /// 322 void viewCFG() const; 323 324 /// viewCFGOnly - This function is meant for use from the debugger. It works 325 /// just like viewCFG, but it does not include the contents of basic blocks 326 /// into the nodes, just the label. If you are only interested in the CFG 327 /// this can make the graph smaller. 328 /// 329 void viewCFGOnly() const; 330 331 /// dump - Print the current MachineFunction to cerr, useful for debugger use. 332 /// 333 void dump() const; 334 335 /// verify - Run the current MachineFunction through the machine code 336 /// verifier, useful for debugger use. 337 void verify(Pass *p = nullptr, const char *Banner = nullptr) const; 338 339 // Provide accessors for the MachineBasicBlock list... 340 typedef BasicBlockListType::iterator iterator; 341 typedef BasicBlockListType::const_iterator const_iterator; 342 typedef std::reverse_iterator<const_iterator> const_reverse_iterator; 343 typedef std::reverse_iterator<iterator> reverse_iterator; 344 345 /// Support for MachineBasicBlock::getNextNode(). 346 static BasicBlockListType MachineFunction::* 347 getSublistAccess(MachineBasicBlock *) { 348 return &MachineFunction::BasicBlocks; 349 } 350 351 /// addLiveIn - Add the specified physical register as a live-in value and 352 /// create a corresponding virtual register for it. 353 unsigned addLiveIn(unsigned PReg, const TargetRegisterClass *RC); 354 355 //===--------------------------------------------------------------------===// 356 // BasicBlock accessor functions. 357 // 358 iterator begin() { return BasicBlocks.begin(); } 359 const_iterator begin() const { return BasicBlocks.begin(); } 360 iterator end () { return BasicBlocks.end(); } 361 const_iterator end () const { return BasicBlocks.end(); } 362 363 reverse_iterator rbegin() { return BasicBlocks.rbegin(); } 364 const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); } 365 reverse_iterator rend () { return BasicBlocks.rend(); } 366 const_reverse_iterator rend () const { return BasicBlocks.rend(); } 367 368 unsigned size() const { return (unsigned)BasicBlocks.size();} 369 bool empty() const { return BasicBlocks.empty(); } 370 const MachineBasicBlock &front() const { return BasicBlocks.front(); } 371 MachineBasicBlock &front() { return BasicBlocks.front(); } 372 const MachineBasicBlock & back() const { return BasicBlocks.back(); } 373 MachineBasicBlock & back() { return BasicBlocks.back(); } 374 375 void push_back (MachineBasicBlock *MBB) { BasicBlocks.push_back (MBB); } 376 void push_front(MachineBasicBlock *MBB) { BasicBlocks.push_front(MBB); } 377 void insert(iterator MBBI, MachineBasicBlock *MBB) { 378 BasicBlocks.insert(MBBI, MBB); 379 } 380 void splice(iterator InsertPt, iterator MBBI) { 381 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI); 382 } 383 void splice(iterator InsertPt, MachineBasicBlock *MBB) { 384 BasicBlocks.splice(InsertPt, BasicBlocks, MBB); 385 } 386 void splice(iterator InsertPt, iterator MBBI, iterator MBBE) { 387 BasicBlocks.splice(InsertPt, BasicBlocks, MBBI, MBBE); 388 } 389 390 void remove(iterator MBBI) { BasicBlocks.remove(MBBI); } 391 void remove(MachineBasicBlock *MBBI) { BasicBlocks.remove(MBBI); } 392 void erase(iterator MBBI) { BasicBlocks.erase(MBBI); } 393 void erase(MachineBasicBlock *MBBI) { BasicBlocks.erase(MBBI); } 394 395 template <typename Comp> 396 void sort(Comp comp) { 397 BasicBlocks.sort(comp); 398 } 399 400 //===--------------------------------------------------------------------===// 401 // Internal functions used to automatically number MachineBasicBlocks 402 // 403 404 /// \brief Adds the MBB to the internal numbering. Returns the unique number 405 /// assigned to the MBB. 406 /// 407 unsigned addToMBBNumbering(MachineBasicBlock *MBB) { 408 MBBNumbering.push_back(MBB); 409 return (unsigned)MBBNumbering.size()-1; 410 } 411 412 /// removeFromMBBNumbering - Remove the specific machine basic block from our 413 /// tracker, this is only really to be used by the MachineBasicBlock 414 /// implementation. 415 void removeFromMBBNumbering(unsigned N) { 416 assert(N < MBBNumbering.size() && "Illegal basic block #"); 417 MBBNumbering[N] = nullptr; 418 } 419 420 /// CreateMachineInstr - Allocate a new MachineInstr. Use this instead 421 /// of `new MachineInstr'. 422 /// 423 MachineInstr *CreateMachineInstr(const MCInstrDesc &MCID, 424 DebugLoc DL, 425 bool NoImp = false); 426 427 /// CloneMachineInstr - Create a new MachineInstr which is a copy of the 428 /// 'Orig' instruction, identical in all ways except the instruction 429 /// has no parent, prev, or next. 430 /// 431 /// See also TargetInstrInfo::duplicate() for target-specific fixes to cloned 432 /// instructions. 433 MachineInstr *CloneMachineInstr(const MachineInstr *Orig); 434 435 /// DeleteMachineInstr - Delete the given MachineInstr. 436 /// 437 void DeleteMachineInstr(MachineInstr *MI); 438 439 /// CreateMachineBasicBlock - Allocate a new MachineBasicBlock. Use this 440 /// instead of `new MachineBasicBlock'. 441 /// 442 MachineBasicBlock *CreateMachineBasicBlock(const BasicBlock *bb = nullptr); 443 444 /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. 445 /// 446 void DeleteMachineBasicBlock(MachineBasicBlock *MBB); 447 448 /// getMachineMemOperand - Allocate a new MachineMemOperand. 449 /// MachineMemOperands are owned by the MachineFunction and need not be 450 /// explicitly deallocated. 451 MachineMemOperand *getMachineMemOperand(MachinePointerInfo PtrInfo, 452 unsigned f, uint64_t s, 453 unsigned base_alignment, 454 const AAMDNodes &AAInfo = AAMDNodes(), 455 const MDNode *Ranges = nullptr); 456 457 /// getMachineMemOperand - Allocate a new MachineMemOperand by copying 458 /// an existing one, adjusting by an offset and using the given size. 459 /// MachineMemOperands are owned by the MachineFunction and need not be 460 /// explicitly deallocated. 461 MachineMemOperand *getMachineMemOperand(const MachineMemOperand *MMO, 462 int64_t Offset, uint64_t Size); 463 464 typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity; 465 466 /// Allocate an array of MachineOperands. This is only intended for use by 467 /// internal MachineInstr functions. 468 MachineOperand *allocateOperandArray(OperandCapacity Cap) { 469 return OperandRecycler.allocate(Cap, Allocator); 470 } 471 472 /// Dellocate an array of MachineOperands and recycle the memory. This is 473 /// only intended for use by internal MachineInstr functions. 474 /// Cap must be the same capacity that was used to allocate the array. 475 void deallocateOperandArray(OperandCapacity Cap, MachineOperand *Array) { 476 OperandRecycler.deallocate(Cap, Array); 477 } 478 479 /// \brief Allocate and initialize a register mask with @p NumRegister bits. 480 uint32_t *allocateRegisterMask(unsigned NumRegister) { 481 unsigned Size = (NumRegister + 31) / 32; 482 uint32_t *Mask = Allocator.Allocate<uint32_t>(Size); 483 for (unsigned i = 0; i != Size; ++i) 484 Mask[i] = 0; 485 return Mask; 486 } 487 488 /// allocateMemRefsArray - Allocate an array to hold MachineMemOperand 489 /// pointers. This array is owned by the MachineFunction. 490 MachineInstr::mmo_iterator allocateMemRefsArray(unsigned long Num); 491 492 /// extractLoadMemRefs - Allocate an array and populate it with just the 493 /// load information from the given MachineMemOperand sequence. 494 std::pair<MachineInstr::mmo_iterator, 495 MachineInstr::mmo_iterator> 496 extractLoadMemRefs(MachineInstr::mmo_iterator Begin, 497 MachineInstr::mmo_iterator End); 498 499 /// extractStoreMemRefs - Allocate an array and populate it with just the 500 /// store information from the given MachineMemOperand sequence. 501 std::pair<MachineInstr::mmo_iterator, 502 MachineInstr::mmo_iterator> 503 extractStoreMemRefs(MachineInstr::mmo_iterator Begin, 504 MachineInstr::mmo_iterator End); 505 506 /// Allocate a string and populate it with the given external symbol name. 507 const char *createExternalSymbolName(StringRef Name); 508 509 //===--------------------------------------------------------------------===// 510 // Label Manipulation. 511 // 512 513 /// getJTISymbol - Return the MCSymbol for the specified non-empty jump table. 514 /// If isLinkerPrivate is specified, an 'l' label is returned, otherwise a 515 /// normal 'L' label is returned. 516 MCSymbol *getJTISymbol(unsigned JTI, MCContext &Ctx, 517 bool isLinkerPrivate = false) const; 518 519 /// getPICBaseSymbol - Return a function-local symbol to represent the PIC 520 /// base. 521 MCSymbol *getPICBaseSymbol() const; 522 }; 523 524 //===--------------------------------------------------------------------===// 525 // GraphTraits specializations for function basic block graphs (CFGs) 526 //===--------------------------------------------------------------------===// 527 528 // Provide specializations of GraphTraits to be able to treat a 529 // machine function as a graph of machine basic blocks... these are 530 // the same as the machine basic block iterators, except that the root 531 // node is implicitly the first node of the function. 532 // 533 template <> struct GraphTraits<MachineFunction*> : 534 public GraphTraits<MachineBasicBlock*> { 535 static NodeType *getEntryNode(MachineFunction *F) { 536 return &F->front(); 537 } 538 539 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 540 typedef MachineFunction::iterator nodes_iterator; 541 static nodes_iterator nodes_begin(MachineFunction *F) { return F->begin(); } 542 static nodes_iterator nodes_end (MachineFunction *F) { return F->end(); } 543 static unsigned size (MachineFunction *F) { return F->size(); } 544 }; 545 template <> struct GraphTraits<const MachineFunction*> : 546 public GraphTraits<const MachineBasicBlock*> { 547 static NodeType *getEntryNode(const MachineFunction *F) { 548 return &F->front(); 549 } 550 551 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph 552 typedef MachineFunction::const_iterator nodes_iterator; 553 static nodes_iterator nodes_begin(const MachineFunction *F) { 554 return F->begin(); 555 } 556 static nodes_iterator nodes_end (const MachineFunction *F) { 557 return F->end(); 558 } 559 static unsigned size (const MachineFunction *F) { 560 return F->size(); 561 } 562 }; 563 564 565 // Provide specializations of GraphTraits to be able to treat a function as a 566 // graph of basic blocks... and to walk it in inverse order. Inverse order for 567 // a function is considered to be when traversing the predecessor edges of a BB 568 // instead of the successor edges. 569 // 570 template <> struct GraphTraits<Inverse<MachineFunction*> > : 571 public GraphTraits<Inverse<MachineBasicBlock*> > { 572 static NodeType *getEntryNode(Inverse<MachineFunction*> G) { 573 return &G.Graph->front(); 574 } 575 }; 576 template <> struct GraphTraits<Inverse<const MachineFunction*> > : 577 public GraphTraits<Inverse<const MachineBasicBlock*> > { 578 static NodeType *getEntryNode(Inverse<const MachineFunction *> G) { 579 return &G.Graph->front(); 580 } 581 }; 582 583 } // End llvm namespace 584 585 #endif 586