MachineFunction.h 18 KB

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