shift_idioms.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. COND_EXPR *rhs,*lhs,*expr;
  29. eReg regH,regL;
  30. regH=m_icodes[0]->ll()->dst.regi;
  31. regL=m_icodes[1]->ll()->dst.regi;
  32. idx = m_func->localId.newLongReg (TYPE_LONG_SIGN, regH, regL, m_icodes[0]);
  33. lhs = COND_EXPR::idLongIdx (idx);
  34. m_icodes[0]->setRegDU( regL, USE_DEF);
  35. rhs = COND_EXPR::idKte(1,2);
  36. expr = COND_EXPR::boolOp(lhs, rhs, SHR);
  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. COND_EXPR *lhs,*rhs,*_exp;
  74. lhs = COND_EXPR::idReg (m_icodes[0]->ll()->dst.regi,
  75. m_icodes[0]->ll()->getFlag() & NO_SRC_B,
  76. &m_func->localId);
  77. rhs = COND_EXPR::idKte (m_icodes.size(), 2);
  78. _exp = COND_EXPR::boolOp (lhs, rhs, SHL);
  79. m_icodes[0]->setAsgn(lhs, _exp);
  80. for (size_t i=1; i<m_icodes.size()-1; ++i)
  81. {
  82. m_icodes[i]->invalidate();
  83. }
  84. return m_icodes.size();
  85. }
  86. /*****************************************************************************
  87. * idiom12 - Shift left long by 1
  88. * SHL reg, 1
  89. * RCL reg, 1
  90. * Eg: SHL ax, 1
  91. * RCL dx, 1
  92. * => dx:ax = dx:ax << 1
  93. * Found in Borland Turbo C code for long variable shift left.
  94. ****************************************************************************/
  95. bool Idiom12::match(iICODE pIcode)
  96. {
  97. if(distance(pIcode,m_end)<2)
  98. return false;
  99. m_icodes[0]=pIcode++;
  100. m_icodes[1]=pIcode++;
  101. if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
  102. if (m_icodes[1]->ll()->match(iRCL,I) && (m_icodes[1]->ll()->src().getImm2() == 1))
  103. return true;
  104. return false;
  105. }
  106. int Idiom12::action()
  107. {
  108. int idx;
  109. COND_EXPR *rhs,*lhs,*expr;
  110. eReg regH,regL;
  111. regL=m_icodes[0]->ll()->dst.regi;
  112. regH=m_icodes[1]->ll()->dst.regi;
  113. idx = m_func->localId.newLongReg (TYPE_LONG_UNSIGN, regH, regL,m_icodes[0]);
  114. lhs = COND_EXPR::idLongIdx (idx);
  115. m_icodes[0]->setRegDU( regH, USE_DEF);
  116. rhs = COND_EXPR::idKte (1, 2);
  117. expr = COND_EXPR::boolOp (lhs, rhs, SHL);
  118. m_icodes[0]->setAsgn(lhs, expr);
  119. m_icodes[1]->invalidate();
  120. return 2;
  121. }
  122. /*****************************************************************************
  123. * idiom9 - Shift right by 1 (unsigned long ops)
  124. * SHR reg, 1
  125. * RCR reg, 1
  126. * Eg: SHR dx, 1
  127. * RCR ax, 1
  128. * => dx:ax = dx:ax >> 1 (dx:ax are unsigned long)
  129. * Found in Microsoft C code for long unsigned variable shift right.
  130. ****************************************************************************/
  131. bool Idiom9::match(iICODE pIcode)
  132. {
  133. if(distance(pIcode,m_end)<2)
  134. return false;
  135. m_icodes[0]=pIcode++;
  136. m_icodes[1]=pIcode++;
  137. if (m_icodes[0]->ll()->testFlags(I) && (m_icodes[0]->ll()->src().getImm2() == 1))
  138. if (m_icodes[1]->ll()->match(iRCR,I) && (m_icodes[1]->ll()->src().getImm2() == 1))
  139. return true;
  140. return false;
  141. }
  142. int Idiom9::action()
  143. {
  144. int idx;
  145. COND_EXPR *rhs,*lhs,*expr;
  146. eReg regH,regL;
  147. regL=m_icodes[1]->ll()->dst.regi;
  148. regH=m_icodes[0]->ll()->dst.regi;
  149. idx = m_func->localId.newLongReg (TYPE_LONG_UNSIGN,regH,regL,m_icodes[0]);
  150. lhs = COND_EXPR::idLongIdx (idx);
  151. m_icodes[0]->setRegDU(regL, USE_DEF);
  152. rhs = COND_EXPR::idKte (1, 2);
  153. expr = COND_EXPR::boolOp (lhs, rhs, SHR);
  154. m_icodes[0]->setAsgn(lhs, expr);
  155. m_icodes[1]->invalidate();
  156. return 2;
  157. }