shift_idioms.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "dcc.h"
  2. #include "shift_idioms.h"
  3. using namespace std;
  4. /*****************************************************************************
  5. * idiom8 - Shift right by 1 (signed long ops)
  6. * SAR reg, 1
  7. * RCR reg, 1
  8. * Eg: SAR dx, 1
  9. * RCR ax, 1
  10. * => dx:ax = dx:ax >> 1 (dx:ax are signed long)
  11. * Found in Microsoft C code for long signed variable shift right.
  12. ****************************************************************************/
  13. bool Idiom8::match(iICODE pIcode)
  14. {
  15. if(distance(pIcode,m_end)<2)
  16. return false;
  17. m_icodes[0]=pIcode++;
  18. m_icodes[1]=pIcode++;
  19. if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
  20. if ( m_icodes[1]->ll()->match(iRCR,I) &&
  21. (m_icodes[1]->ll()->src().getImm2() == 1))
  22. return true;
  23. return false;
  24. }
  25. int Idiom8::action()
  26. {
  27. int idx;
  28. AstIdent *lhs;
  29. Expr *expr;
  30. eReg regH,regL;
  31. regH=m_icodes[0]->ll()->dst.regi;
  32. regL=m_icodes[1]->ll()->dst.regi;
  33. idx = m_func->localId.newLongReg (TYPE_LONG_SIGN, regH, regL, m_icodes[0]);
  34. lhs = AstIdent::LongIdx (idx);
  35. m_icodes[0]->setRegDU( regL, USE_DEF);
  36. expr = new BinaryOperator(SHR,lhs, new Constant(1, 2));
  37. m_icodes[0]->setAsgn(lhs, expr);
  38. m_icodes[1]->invalidate();
  39. return 2;
  40. }
  41. /*****************************************************************************
  42. * idiom 15 - Shift left by n
  43. * SHL reg, 1
  44. * SHL reg, 1
  45. * [...]
  46. * [SHL reg, 1]
  47. * Eg: SHL ax, 1
  48. * SHL ax, 1
  49. * => ax = ax << 2
  50. * Found in Borland Turbo C code to index an array (array multiplication)
  51. ****************************************************************************/
  52. bool Idiom15::match(iICODE pIcode)
  53. {
  54. uint8_t regi;
  55. if(distance(pIcode,m_end)<2)
  56. return false;
  57. /* Match SHL reg, 1 */
  58. if (not pIcode->ll()->testFlags(I) or (pIcode->ll()->src().getImm2() != 1))
  59. return false;
  60. m_icodes.clear();
  61. regi = pIcode->ll()->dst.regi;
  62. m_icodes.push_back(pIcode++);
  63. while( (pIcode!=m_end) and
  64. pIcode->ll()->match(iSHL,(eReg)regi,I) and
  65. (pIcode->ll()->src().getImm2() == 1) )
  66. {
  67. m_icodes.push_back(pIcode++);
  68. }
  69. return m_icodes.size()>1;
  70. }
  71. int Idiom15::action()
  72. {
  73. AstIdent *lhs;
  74. Expr *rhs,*_exp;
  75. lhs = new RegisterNode(m_icodes[0]->ll()->dst.regi,
  76. m_icodes[0]->ll()->getFlag() & NO_SRC_B,
  77. &m_func->localId);
  78. rhs = new Constant(m_icodes.size(), 2);
  79. _exp = new BinaryOperator(SHL,lhs, rhs);
  80. m_icodes[0]->setAsgn(lhs, _exp);
  81. for (size_t i=1; i<m_icodes.size()-1; ++i)
  82. {
  83. m_icodes[i]->invalidate();
  84. }
  85. return m_icodes.size();
  86. }
  87. /*****************************************************************************
  88. * idiom12 - Shift left long by 1
  89. * SHL reg, 1
  90. * RCL reg, 1
  91. * Eg: SHL ax, 1
  92. * RCL dx, 1
  93. * => dx:ax = dx:ax << 1
  94. * Found in Borland Turbo C code for long variable shift left.
  95. ****************************************************************************/
  96. bool Idiom12::match(iICODE pIcode)
  97. {
  98. if(distance(pIcode,m_end)<2)
  99. return false;
  100. m_icodes[0]=pIcode++;
  101. m_icodes[1]=pIcode++;
  102. if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
  103. if (m_icodes[1]->ll()->match(iRCL,I) && (m_icodes[1]->ll()->src().getImm2() == 1))
  104. return true;
  105. return false;
  106. }
  107. int Idiom12::action()
  108. {
  109. int idx;
  110. Expr *expr;
  111. AstIdent *lhs;
  112. eReg regH,regL;
  113. regL=m_icodes[0]->ll()->dst.regi;
  114. regH=m_icodes[1]->ll()->dst.regi;
  115. idx = m_func->localId.newLongReg (TYPE_LONG_UNSIGN, regH, regL,m_icodes[0]);
  116. lhs = AstIdent::LongIdx (idx);
  117. m_icodes[0]->setRegDU( regH, USE_DEF);
  118. expr = new BinaryOperator(SHL,lhs, new Constant(1, 2));
  119. m_icodes[0]->setAsgn(lhs, expr);
  120. m_icodes[1]->invalidate();
  121. return 2;
  122. }
  123. /*****************************************************************************
  124. * idiom9 - Shift right by 1 (unsigned long ops)
  125. * SHR reg, 1
  126. * RCR reg, 1
  127. * Eg: SHR dx, 1
  128. * RCR ax, 1
  129. * => dx:ax = dx:ax >> 1 (dx:ax are unsigned long)
  130. * Found in Microsoft C code for long unsigned variable shift right.
  131. ****************************************************************************/
  132. bool Idiom9::match(iICODE pIcode)
  133. {
  134. if(distance(pIcode,m_end)<2)
  135. return false;
  136. m_icodes[0]=pIcode++;
  137. m_icodes[1]=pIcode++;
  138. if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
  139. if (m_icodes[1]->ll()->match(iRCR,I) && (m_icodes[1]->ll()->src().getImm2() == 1))
  140. return true;
  141. return false;
  142. }
  143. int Idiom9::action()
  144. {
  145. int idx;
  146. AstIdent *lhs;
  147. Expr *rhs,*expr;
  148. eReg regH,regL;
  149. regL=m_icodes[1]->ll()->dst.regi;
  150. regH=m_icodes[0]->ll()->dst.regi;
  151. idx = m_func->localId.newLongReg (TYPE_LONG_UNSIGN,regH,regL,m_icodes[0]);
  152. lhs = AstIdent::LongIdx (idx);
  153. m_icodes[0]->setRegDU(regL, USE_DEF);
  154. expr = new BinaryOperator(SHR,lhs, new Constant(1, 2));
  155. m_icodes[0]->setAsgn(lhs, expr);
  156. m_icodes[1]->invalidate();
  157. return 2;
  158. }