Procedure.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include <llvm/ADT/ilist.h>
  3. #include <llvm/ADT/ilist_node.h>
  4. #include <bitset>
  5. #include "BasicBlock.h"
  6. #include "types.h"
  7. #include "ast.h"
  8. #include "icode.h"
  9. #include "locident.h"
  10. #include "error.h"
  11. #include "graph.h"
  12. #include "bundle.h"
  13. #include "StackFrame.h"
  14. /* PROCEDURE NODE */
  15. struct CALL_GRAPH;
  16. namespace llvm
  17. {
  18. // Traits for intrusive list of basic blocks...
  19. template<>
  20. struct ilist_traits<BB> : public ilist_default_traits<BB>
  21. {
  22. // createSentinel is used to get hold of the node that marks the end of the
  23. // list... (same trick used here as in ilist_traits<Instruction>)
  24. BB *createSentinel() const {
  25. return static_cast<BB*>(&Sentinel);
  26. }
  27. static void destroySentinel(BB*) {}
  28. BB *provideInitialHead() const { return createSentinel(); }
  29. BB *ensureHead(BB*) const { return createSentinel(); }
  30. static void noteHead(BB*, BB*) {}
  31. //static ValueSymbolTable *getSymTab(Function *ItemParent);
  32. private:
  33. mutable ilist_half_node<BB> Sentinel;
  34. };
  35. }
  36. struct FunctionType
  37. {
  38. bool m_vararg;
  39. bool isVarArg() const {return m_vararg;}
  40. };
  41. struct Assignment
  42. {
  43. COND_EXPR *lhs;
  44. COND_EXPR *rhs;
  45. };
  46. struct Function : public llvm::ilist_node<Function>
  47. {
  48. typedef llvm::iplist<BB> BasicBlockListType;
  49. // BasicBlock iterators...
  50. typedef BasicBlockListType::iterator iterator;
  51. typedef BasicBlockListType::const_iterator const_iterator;
  52. private:
  53. BasicBlockListType BasicBlocks; ///< The basic blocks
  54. public:
  55. dword procEntry; /* label number */
  56. std::string name; /* Meaningful name for this proc */
  57. STATE state; /* Entry state */
  58. Int depth; /* Depth at which we found it - for printing */
  59. flags32 flg; /* Combination of Icode & Proc flags */
  60. int16 cbParam; /* Probable no. of bytes of parameters */
  61. STKFRAME args; /* Array of arguments */
  62. LOCAL_ID localId; /* Local identifiers */
  63. ID retVal; /* Return value - identifier */
  64. /* Icodes and control flow graph */
  65. CIcodeRec Icode; /* Object with ICODE records */
  66. std::vector<BB*> m_cfg; /* Ptr. to BB list/CFG */
  67. std::vector<BB*> m_dfsLast;
  68. std::vector<BB*> heldBBs;
  69. //BB * *dfsLast; /* Array of pointers to BBs in dfsLast
  70. // * (reverse postorder) order */
  71. Int numBBs; /* Number of BBs in the graph cfg */
  72. boolT hasCase; /* Procedure has a case node */
  73. /* For interprocedural live analysis */
  74. std::bitset<32> liveIn; /* Registers used before defined */
  75. std::bitset<32> liveOut; /* Registers that may be used in successors */
  76. bool liveAnal; /* Procedure has been analysed already */
  77. Function(void *ty=0) : procEntry(0),depth(0),flg(0),cbParam(0),m_cfg(0),m_dfsLast(0),numBBs(0),
  78. hasCase(false),liveIn(0),liveOut(0),liveAnal(0)//,next(0),prev(0)
  79. {
  80. }
  81. public:
  82. static Function *Create(void *ty=0,int Linkage=0,const std::string &nm="",void *module=0)
  83. {
  84. Function *r=new Function(ty);
  85. r->name = nm;
  86. return r;
  87. }
  88. void compoundCond();
  89. void writeProcComments();
  90. void lowLevelAnalysis();
  91. void bindIcodeOff();
  92. void dataFlow(std::bitset<32> &liveOut);
  93. void compressCFG();
  94. void highLevelGen();
  95. void structure(derSeq *derivedG);
  96. derSeq *checkReducibility();
  97. void createCFG();
  98. void markImpure();
  99. void findImmedDom();
  100. void FollowCtrl(CALL_GRAPH *pcallGraph, STATE *pstate);
  101. void process_operands(ICODE *pIcode, STATE *pstate);
  102. boolT process_JMP(ICODE *pIcode, STATE *pstate, CALL_GRAPH *pcallGraph);
  103. boolT process_CALL(ICODE *pIcode, CALL_GRAPH *pcallGraph, STATE *pstate);
  104. void displayCFG();
  105. void freeCFG();
  106. void codeGen(std::ostream &fs);
  107. void displayStats();
  108. void mergeFallThrough(BB *pBB);
  109. void structIfs();
  110. void structLoops(derSeq *derivedG);
  111. void buildCFG();
  112. void controlFlowAnalysis();
  113. void newRegArg(ICODE *picode, ICODE *ticode);
  114. protected:
  115. // TODO: replace those with friend visitor ?
  116. void propLongReg(Int loc_ident_idx, const ID &pLocId);
  117. void propLongStk(Int i, const ID &pLocId);
  118. void propLongGlb(Int i, const ID &pLocId);
  119. int findBackwarLongDefs(int loc_ident_idx, const ID &pLocId, iICODE iter);
  120. int findForwardLongUses(int loc_ident_idx, const ID &pLocId, iICODE beg);
  121. void structCases();
  122. void findExps();
  123. void genDU1();
  124. void elimCondCodes();
  125. void liveRegAnalysis(std::bitset<32> &in_liveOut);
  126. void findIdioms();
  127. void propLong();
  128. void genLiveKtes();
  129. byte findDerivedSeq (derSeq *derivedGi);
  130. bool nextOrderGraph(derSeq *derivedGi);
  131. };