PreLowerPeeps.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "Backend.h"
  6. IR::Instr *Lowerer::PreLowerPeepInstr(IR::Instr *instr, IR::Instr **pInstrPrev)
  7. {
  8. if (PHASE_OFF(Js::PreLowererPeepsPhase, this->m_func))
  9. {
  10. return instr;
  11. }
  12. switch (instr->m_opcode)
  13. {
  14. case Js::OpCode::Shl_I4:
  15. instr = this->PeepShl(instr);
  16. *pInstrPrev = instr->m_prev;
  17. break;
  18. case Js::OpCode::BrTrue_I4:
  19. case Js::OpCode::BrFalse_I4:
  20. instr = this->PeepBrBool(instr);
  21. *pInstrPrev = instr->m_prev;
  22. break;
  23. }
  24. return instr;
  25. }
  26. IR::Instr *Lowerer::PeepShl(IR::Instr *instrShl)
  27. {
  28. IR::Opnd *src1;
  29. IR::Opnd *src2;
  30. IR::Instr *instrDef;
  31. src1 = instrShl->GetSrc1();
  32. src2 = instrShl->GetSrc2();
  33. // Peep:
  34. // t1 = SHR X, cst
  35. // t2 = SHL t1, cst
  36. //
  37. // Into:
  38. // t2 = AND X, mask
  39. if (!src1->IsRegOpnd() || !src2->IsIntConstOpnd())
  40. {
  41. return instrShl;
  42. }
  43. if (!src1->AsRegOpnd()->m_sym->IsSingleDef())
  44. {
  45. return instrShl;
  46. }
  47. if (instrShl->HasBailOutInfo())
  48. {
  49. return instrShl;
  50. }
  51. instrDef = src1->AsRegOpnd()->m_sym->GetInstrDef();
  52. if (instrDef->m_opcode != Js::OpCode::Shr_I4 || !instrDef->GetSrc2()->IsIntConstOpnd()
  53. || instrDef->GetSrc2()->AsIntConstOpnd()->GetValue() != src2->AsIntConstOpnd()->GetValue()
  54. || !instrDef->GetSrc1()->IsRegOpnd())
  55. {
  56. return instrShl;
  57. }
  58. if (!src1->GetIsDead())
  59. {
  60. return instrShl;
  61. }
  62. if (instrDef->HasBailOutInfo())
  63. {
  64. return instrShl;
  65. }
  66. FOREACH_INSTR_IN_RANGE(instrIter, instrDef->m_next, instrShl->m_prev)
  67. {
  68. if (instrIter->HasBailOutInfo())
  69. {
  70. return instrShl;
  71. }
  72. if (instrIter->FindRegDef(instrDef->GetSrc1()->AsRegOpnd()->m_sym))
  73. {
  74. return instrShl;
  75. }
  76. if (instrIter->FindRegUse(src1->AsRegOpnd()->m_sym))
  77. {
  78. return instrShl;
  79. }
  80. // if any branch between def-use, don't do peeps on it because branch target might depend on the def
  81. if (instrIter->IsBranchInstr())
  82. {
  83. return instrShl;
  84. }
  85. } NEXT_INSTR_IN_RANGE;
  86. instrShl->FreeSrc1();
  87. instrShl->SetSrc1(instrDef->UnlinkSrc1());
  88. instrDef->Remove();
  89. IntConstType oldValue = src2->AsIntConstOpnd()->GetValue();
  90. // Left shift operator (<<) on arm32 is implemented by LSL which doesn't discard bits beyond lowerest 5-bit.
  91. // Need to discard such bits to conform to << in JavaScript. This is not a problem for x86 and x64 because
  92. // behavior of SHL is consistent with JavaScript but keep the below line for clarity.
  93. oldValue %= sizeof(int32) * 8;
  94. oldValue = ~((1 << oldValue) - 1);
  95. src2->AsIntConstOpnd()->SetValue(oldValue);
  96. instrShl->m_opcode = Js::OpCode::And_I4;
  97. return instrShl;
  98. }
  99. IR::Instr *Lowerer::PeepBrBool(IR::Instr *instrBr)
  100. {
  101. IR::Opnd *src1;
  102. IR::Instr *instrBinOp, *instrCm1, *instrCm2;
  103. // Peep:
  104. // t1 = CmCC_I4 a, b
  105. // t2 = CmCC_i4 c, d
  106. // t3 = AND/OR t1, t2
  107. // BrTrue/False t3, $L_true
  108. //
  109. // Into:
  110. // BrCC a, b, $L_true/false
  111. // BrCC c, d, $L_true
  112. //$L_false:
  113. src1 = instrBr->GetSrc1();
  114. if (!src1->IsRegOpnd())
  115. {
  116. return instrBr;
  117. }
  118. Assert(!instrBr->HasBailOutInfo());
  119. instrBinOp = instrBr->GetPrevRealInstrOrLabel();
  120. if (instrBinOp->m_opcode != Js::OpCode::And_I4 && instrBinOp->m_opcode != Js::OpCode::Or_I4)
  121. {
  122. return instrBr;
  123. }
  124. if (!instrBinOp->GetDst()->IsEqual(src1))
  125. {
  126. return instrBr;
  127. }
  128. IR::RegOpnd *src1Reg = src1->AsRegOpnd();
  129. if (!src1Reg->m_sym->IsSingleDef() || !src1Reg->GetIsDead())
  130. {
  131. return instrBr;
  132. }
  133. Assert(!instrBinOp->HasBailOutInfo());
  134. instrCm2 = instrBinOp->GetPrevRealInstrOrLabel();
  135. if (!instrCm2->IsCmCC_I4())
  136. {
  137. return instrBr;
  138. }
  139. IR::RegOpnd *cm2DstReg = instrCm2->GetDst()->AsRegOpnd();
  140. if (!cm2DstReg->m_sym->IsSingleDef())
  141. {
  142. return instrBr;
  143. }
  144. if (cm2DstReg->IsEqual(instrBinOp->GetSrc1()))
  145. {
  146. if (!instrBinOp->GetSrc1()->AsRegOpnd()->GetIsDead())
  147. {
  148. return instrBr;
  149. }
  150. }
  151. else if (cm2DstReg->IsEqual(instrBinOp->GetSrc2()))
  152. {
  153. if (!instrBinOp->GetSrc2()->AsRegOpnd()->GetIsDead())
  154. {
  155. return instrBr;
  156. }
  157. }
  158. else
  159. {
  160. return instrBr;
  161. }
  162. Assert(!instrCm2->HasBailOutInfo());
  163. instrCm1 = instrCm2->GetPrevRealInstrOrLabel();
  164. if (!instrCm1->IsCmCC_I4())
  165. {
  166. return instrBr;
  167. }
  168. Assert(!instrCm1->GetDst()->IsEqual(instrCm2->GetDst()));
  169. IR::RegOpnd *cm1DstReg = instrCm1->GetDst()->AsRegOpnd();
  170. if (!cm1DstReg->m_sym->IsSingleDef())
  171. {
  172. return instrBr;
  173. }
  174. if (cm1DstReg->IsEqual(instrBinOp->GetSrc1()))
  175. {
  176. if (!instrBinOp->GetSrc1()->AsRegOpnd()->GetIsDead())
  177. {
  178. return instrBr;
  179. }
  180. }
  181. else if (cm1DstReg->IsEqual(instrBinOp->GetSrc2()))
  182. {
  183. if (!instrBinOp->GetSrc2()->AsRegOpnd()->GetIsDead())
  184. {
  185. return instrBr;
  186. }
  187. }
  188. else
  189. {
  190. return instrBr;
  191. }
  192. Assert(!instrCm1->HasBailOutInfo());
  193. IR::LabelInstr *falseLabel = instrBr->AsBranchInstr()->GetTarget();
  194. IR::LabelInstr *trueLabel = IR::LabelInstr::New(Js::OpCode::Label, this->m_func);
  195. instrBr->InsertAfter(trueLabel);
  196. IR::BranchInstr *instrBr1;
  197. IR::BranchInstr *instrBr2;
  198. if (instrBinOp->m_opcode == Js::OpCode::And_I4)
  199. {
  200. instrBr1 = instrCm1->ChangeCmCCToBranchInstr(instrBr->m_opcode == Js::OpCode::BrFalse_I4 ? falseLabel : trueLabel);
  201. instrBr1->Invert();
  202. instrBr2 = instrCm2->ChangeCmCCToBranchInstr(falseLabel);
  203. if (instrBr->m_opcode == Js::OpCode::BrFalse_I4)
  204. {
  205. instrBr2->Invert();
  206. }
  207. }
  208. else
  209. {
  210. Assert(instrBinOp->m_opcode == Js::OpCode::Or_I4);
  211. instrBr1 = instrCm1->ChangeCmCCToBranchInstr(instrBr->m_opcode == Js::OpCode::BrTrue_I4 ? falseLabel : trueLabel);
  212. instrBr2 = instrCm2->ChangeCmCCToBranchInstr(falseLabel);
  213. if (instrBr->m_opcode == Js::OpCode::BrFalse_I4)
  214. {
  215. instrBr2->Invert();
  216. }
  217. }
  218. instrBinOp->Remove();
  219. instrBr->Remove();
  220. return instrBr2;
  221. }