BasicBlock.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #pragma once
  2. #include <list>
  3. #include <vector>
  4. #include <bitset>
  5. #include <string>
  6. #include <llvm/ADT/ilist.h>
  7. #include <llvm/ADT/ilist_node.h>
  8. #include <boost/range/iterator_range.hpp>
  9. #include "icode.h"
  10. #include "types.h"
  11. #include "graph.h"
  12. //#include "icode.h"
  13. /* Basic block (BB) node definition */
  14. struct Function;
  15. class CIcodeRec;
  16. struct BB;
  17. struct LOCAL_ID;
  18. struct interval;
  19. //TODO: consider default address value -> INVALID
  20. struct TYPEADR_TYPE
  21. {
  22. uint32_t ip; /* Out edge icode address */
  23. BB * BBptr; /* Out edge pointer to next BB */
  24. interval *intPtr; /* Out edge ptr to next interval*/
  25. TYPEADR_TYPE(uint32_t addr=0) : ip(addr),BBptr(nullptr),intPtr(nullptr)
  26. {}
  27. TYPEADR_TYPE(interval *v) : ip(0),BBptr(nullptr),intPtr(v)
  28. {}
  29. };
  30. struct BB : public llvm::ilist_node<BB>
  31. {
  32. friend struct Function;
  33. private:
  34. BB(const BB&);
  35. BB() : nodeType(0),traversed(DFS_NONE),
  36. numHlIcodes(0),flg(0),
  37. inEdges(0),
  38. edges(0),beenOnH(0),inEdgeCount(0),reachingInt(0),
  39. inInterval(0),correspInt(0),liveUse(0),def(0),liveIn(0),liveOut(0),
  40. dfsFirstNum(0),dfsLastNum(0),immedDom(0),ifFollow(0),loopType(0),latchNode(0),
  41. numBackEdges(0),loopHead(0),loopFollow(0),caseHead(0),caseTail(0),index(0)
  42. {
  43. }
  44. //friend class SymbolTableListTraits<BB, Function>;
  45. typedef boost::iterator_range<iICODE> rCODE;
  46. rCODE instructions;
  47. rCODE &my_range() {return instructions;}
  48. public:
  49. struct ValidFunctor
  50. {
  51. bool operator()(BB *p) {return p->valid();}
  52. };
  53. iICODE begin();
  54. iICODE end() const;
  55. riICODE rbegin();
  56. riICODE rend();
  57. ICODE &front();
  58. ICODE &back();
  59. size_t size();
  60. uint8_t nodeType; /* Type of node */
  61. eDFS traversed; /* last traversal id is held here traversed yet? */
  62. int numHlIcodes; /* No. of high-level icodes */
  63. uint32_t flg; /* BB flags */
  64. /* In edges and out edges */
  65. std::vector<BB *> inEdges; // does not own held pointers
  66. //int numOutEdges; /* Number of out edges */
  67. std::vector<TYPEADR_TYPE> edges;/* Array of ptrs. to out edges */
  68. /* For interval construction */
  69. int beenOnH; /* #times been on header list H */
  70. int inEdgeCount; /* #inEdges (to find intervals) */
  71. BB * reachingInt; /* Reaching interval header */
  72. interval *inInterval; /* Node's interval */
  73. /* For derived sequence construction */
  74. interval *correspInt; //!< Corresponding interval in derived graph Gi-1
  75. // For live register analysis
  76. // LiveIn(b) = LiveUse(b) U (LiveOut(b) - Def(b))
  77. LivenessSet liveUse; /* LiveUse(b) */
  78. LivenessSet def; /* Def(b) */
  79. LivenessSet liveIn; /* LiveIn(b) */
  80. LivenessSet liveOut; /* LiveOut(b) */
  81. /* For structuring analysis */
  82. int dfsFirstNum; /* DFS #: first visit of node */
  83. int dfsLastNum; /* DFS #: last visit of node */
  84. int immedDom; /* Immediate dominator (dfsLast index) */
  85. int ifFollow; /* node that ends the if */
  86. int loopType; /* Type of loop (if any) */
  87. int latchNode; /* latching node of the loop */
  88. size_t numBackEdges; /* # of back edges */
  89. int loopHead; /* most nested loop head to which this node belongs (dfsLast) */
  90. int loopFollow; /* node that follows the loop */
  91. int caseHead; /* most nested case to which this node belongs (dfsLast) */
  92. int caseTail; /* tail node for the case */
  93. int index; /* Index, used in several ways */
  94. static BB * Create(void *ctx=0,const std::string &s="",Function *parent=0,BB *insertBefore=0);
  95. static BB * CreateIntervalBB(Function *parent);
  96. static BB * Create(const rCODE &r, uint8_t _nodeType, Function *parent);
  97. void writeCode(int indLevel, Function *pProc, int *numLoc, int latchNode, int ifFollow);
  98. void mergeFallThrough(CIcodeRec &Icode);
  99. void dfsNumbering(std::vector<BB *> &dfsLast, int *first, int *last);
  100. void displayDfs();
  101. void display();
  102. /// getParent - Return the enclosing method, or null if none
  103. ///
  104. const Function *getParent() const { return Parent; }
  105. Function *getParent() { return Parent; }
  106. void writeBB(std::ostream &ostr, int lev, Function *pProc, int *numLoc);
  107. BB * rmJMP(int marker, BB *pBB);
  108. void genDU1();
  109. void findBBExps(LOCAL_ID &locals, Function *f);
  110. bool valid() {return 0==(flg & INVALID_BB); }
  111. bool wasTraversedAtLevel(int l) const {return traversed==l;}
  112. ICODE * writeLoopHeader(int &indLevel, Function* pProc, int *numLoc, BB *&latch, boolT &repCond);
  113. void addOutEdge(uint32_t ip) // TODO: fix this
  114. {
  115. edges.push_back(TYPEADR_TYPE(ip));
  116. }
  117. void addOutEdgeInterval(interval *i) // TODO: fix this
  118. {
  119. edges.push_back(TYPEADR_TYPE(i));
  120. }
  121. void RemoveUnusedDefs(eReg regi, int defRegIdx, iICODE picode);
  122. private:
  123. bool FindUseBeforeDef(eReg regi, int defRegIdx, iICODE start_at);
  124. void ProcessUseDefForFunc(eReg regi, int defRegIdx, ICODE &picode);
  125. bool isEndOfPath(int latch_node_idx) const;
  126. Function *Parent;
  127. };