Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

524 lines
20 KiB

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