MachineFunction.h 21 KB

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