BasicBlock.h 5.3 KB

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