GlobOptExpr.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft Corporation and contributors. 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. uint8 OpCodeToHash[(uint)Js::OpCode::Count];
  7. static Js::OpCode HashToOpCode[(uint)Js::OpCode::Count];
  8. class CSEInit
  9. {
  10. public:
  11. // Initializer for OpCodeToHash and HashToOpCode maps.
  12. CSEInit()
  13. {
  14. uint8 hash = 1;
  15. for (Js::OpCode opcode = (Js::OpCode)0; opcode < Js::OpCode::Count; opcode = (Js::OpCode)(opcode + 1))
  16. {
  17. if (Js::OpCodeUtil::IsValidOpcode(opcode) && OpCodeAttr::CanCSE(opcode) && !OpCodeAttr::ByteCodeOnly(opcode))
  18. {
  19. OpCodeToHash[(int)opcode] = hash;
  20. HashToOpCode[hash] = opcode;
  21. hash++;
  22. AssertMsg(hash != 0, "Too many CSE'able opcodes");
  23. }
  24. }
  25. }
  26. }CSEInit_Dummy;
  27. bool
  28. GlobOpt::GetHash(IR::Instr *instr, Value *src1Val, Value *src2Val, ExprAttributes exprAttributes, ExprHash *pHash)
  29. {
  30. Js::OpCode opcode = instr->m_opcode;
  31. // Candidate?
  32. if (!OpCodeAttr::CanCSE(opcode) && (opcode != Js::OpCode::StElemI_A && opcode != Js::OpCode::StElemI_A_Strict))
  33. {
  34. return false;
  35. }
  36. ValueNumber valNum1 = 0;
  37. ValueNumber valNum2 = 0;
  38. // Get the value number of src1 and src2
  39. if (instr->GetSrc1())
  40. {
  41. if (!src1Val)
  42. {
  43. return false;
  44. }
  45. valNum1 = src1Val->GetValueNumber();
  46. if (instr->GetSrc2())
  47. {
  48. if (!src2Val)
  49. {
  50. return false;
  51. }
  52. valNum2 = src2Val->GetValueNumber();
  53. }
  54. }
  55. if (src1Val)
  56. {
  57. valNum1 = src1Val->GetValueNumber();
  58. if (src2Val)
  59. {
  60. valNum2 = src2Val->GetValueNumber();
  61. }
  62. }
  63. switch (opcode)
  64. {
  65. case Js::OpCode::Ld_I4:
  66. case Js::OpCode::Ld_A:
  67. // Copy-prop should handle these
  68. return false;
  69. case Js::OpCode::Add_I4:
  70. opcode = Js::OpCode::Add_A;
  71. break;
  72. case Js::OpCode::Sub_I4:
  73. opcode = Js::OpCode::Sub_A;
  74. break;
  75. case Js::OpCode::Mul_I4:
  76. opcode = Js::OpCode::Mul_A;
  77. break;
  78. case Js::OpCode::Rem_I4:
  79. opcode = Js::OpCode::Rem_A;
  80. break;
  81. case Js::OpCode::Div_I4:
  82. opcode = Js::OpCode::Div_A;
  83. break;
  84. case Js::OpCode::Neg_I4:
  85. opcode = Js::OpCode::Neg_A;
  86. break;
  87. case Js::OpCode::Not_I4:
  88. opcode = Js::OpCode::Not_A;
  89. break;
  90. case Js::OpCode::And_I4:
  91. opcode = Js::OpCode::And_A;
  92. break;
  93. case Js::OpCode::Or_I4:
  94. opcode = Js::OpCode::Or_A;
  95. break;
  96. case Js::OpCode::Xor_I4:
  97. opcode = Js::OpCode::Xor_A;
  98. break;
  99. case Js::OpCode::Shl_I4:
  100. opcode = Js::OpCode::Shl_A;
  101. break;
  102. case Js::OpCode::Shr_I4:
  103. opcode = Js::OpCode::Shr_A;
  104. break;
  105. case Js::OpCode::ShrU_I4:
  106. opcode = Js::OpCode::ShrU_A;
  107. break;
  108. case Js::OpCode::StElemI_A:
  109. case Js::OpCode::StElemI_A_Strict:
  110. // Make the load available as a CSE
  111. opcode = Js::OpCode::LdElemI_A;
  112. break;
  113. case Js::OpCode::CmEq_I4:
  114. opcode = Js::OpCode::CmEq_A;
  115. break;
  116. case Js::OpCode::CmNeq_I4:
  117. opcode = Js::OpCode::CmNeq_A;
  118. break;
  119. case Js::OpCode::CmLt_I4:
  120. opcode = Js::OpCode::CmLt_A;
  121. break;
  122. case Js::OpCode::CmLe_I4:
  123. opcode = Js::OpCode::CmLe_A;
  124. break;
  125. case Js::OpCode::CmGt_I4:
  126. opcode = Js::OpCode::CmGt_A;
  127. break;
  128. case Js::OpCode::CmGe_I4:
  129. opcode = Js::OpCode::CmGe_A;
  130. break;
  131. case Js::OpCode::CmUnLt_I4:
  132. opcode = Js::OpCode::CmUnLt_A;
  133. break;
  134. case Js::OpCode::CmUnLe_I4:
  135. opcode = Js::OpCode::CmUnLe_A;
  136. break;
  137. case Js::OpCode::CmUnGt_I4:
  138. opcode = Js::OpCode::CmUnGt_A;
  139. break;
  140. case Js::OpCode::CmUnGe_I4:
  141. opcode = Js::OpCode::CmUnGe_A;
  142. break;
  143. }
  144. pHash->Init(opcode, valNum1, valNum2, exprAttributes);
  145. #if DBG_DUMP
  146. if (!pHash->IsValid() && Js::Configuration::Global.flags.Trace.IsEnabled(Js::CSEPhase, this->func->GetSourceContextId(), this->func->GetLocalFunctionId()))
  147. {
  148. Output::Print(_u(" >>>> CSE: Value numbers too big to be hashed in function %s!\n"), this->func->GetJITFunctionBody()->GetDisplayName());
  149. }
  150. #endif
  151. #if ENABLE_DEBUG_CONFIG_OPTIONS
  152. if (!pHash->IsValid() && Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::CSEPhase, this->func->GetSourceContextId(), this->func->GetLocalFunctionId()))
  153. {
  154. Output::Print(_u(" >>>> CSE: Value numbers too big to be hashed in function %s!\n"), this->func->GetJITFunctionBody()->GetDisplayName());
  155. }
  156. #endif
  157. return pHash->IsValid();
  158. }
  159. void
  160. GlobOpt::CSEAddInstr(
  161. BasicBlock *block,
  162. IR::Instr *instr,
  163. Value *dstVal,
  164. Value *src1Val,
  165. Value *src2Val,
  166. Value *dstIndirIndexVal,
  167. Value *src1IndirIndexVal)
  168. {
  169. ExprAttributes exprAttributes;
  170. ExprHash hash;
  171. if (!this->DoCSE())
  172. {
  173. return;
  174. }
  175. bool isArray = false;
  176. switch(instr->m_opcode)
  177. {
  178. case Js::OpCode::LdElemI_A:
  179. case Js::OpCode::LdArrViewElem:
  180. case Js::OpCode::StElemI_A:
  181. case Js::OpCode::StElemI_A_Strict:
  182. {
  183. // For arrays, hash the value # of the baseOpnd and indexOpnd
  184. IR::IndirOpnd *arrayOpnd;
  185. Value *indirIndexVal;
  186. if (instr->m_opcode == Js::OpCode::StElemI_A || instr->m_opcode == Js::OpCode::StElemI_A_Strict)
  187. {
  188. if (!this->CanCSEArrayStore(instr))
  189. {
  190. return;
  191. }
  192. dstVal = src1Val;
  193. arrayOpnd = instr->GetDst()->AsIndirOpnd();
  194. indirIndexVal = dstIndirIndexVal;
  195. }
  196. else
  197. {
  198. // all the LdElem and Ld*ArrViewElem
  199. arrayOpnd = instr->GetSrc1()->AsIndirOpnd();
  200. indirIndexVal = src1IndirIndexVal;
  201. }
  202. src1Val = block->globOptData.FindValue(arrayOpnd->GetBaseOpnd()->m_sym);
  203. if(indirIndexVal)
  204. {
  205. src2Val = indirIndexVal;
  206. }
  207. else if (arrayOpnd->GetIndexOpnd())
  208. {
  209. src2Val = block->globOptData.FindValue(arrayOpnd->GetIndexOpnd()->m_sym);
  210. }
  211. else
  212. {
  213. return;
  214. }
  215. isArray = true;
  216. // for typed array do not add instructions whose dst are guaranteed to be int or number
  217. // as we will try to eliminate bound check for these typed arrays
  218. if (src1Val->GetValueInfo()->IsLikelyOptimizedVirtualTypedArray())
  219. {
  220. exprAttributes = DstIsIntOrNumberAttributes(!instr->dstIsAlwaysConvertedToInt32, !instr->dstIsAlwaysConvertedToNumber);
  221. }
  222. break;
  223. }
  224. case Js::OpCode::Mul_I4:
  225. // If int32 overflow is ignored, we only add MULs with 53-bit overflow check to expr map
  226. if (instr->HasBailOutInfo() && (instr->GetBailOutKind() & IR::BailOutOnMulOverflow) &&
  227. !instr->ShouldCheckFor32BitOverflow() && instr->ignoreOverflowBitCount != 53)
  228. {
  229. return;
  230. }
  231. // fall-through
  232. case Js::OpCode::Neg_I4:
  233. case Js::OpCode::Add_I4:
  234. case Js::OpCode::Sub_I4:
  235. case Js::OpCode::DivU_I4:
  236. case Js::OpCode::Div_I4:
  237. case Js::OpCode::RemU_I4:
  238. case Js::OpCode::Rem_I4:
  239. case Js::OpCode::ShrU_I4:
  240. {
  241. // Can't CSE and Add where overflow doesn't matter (and no bailout) with one where it does matter... Record whether int
  242. // overflow or negative zero were ignored.
  243. exprAttributes = IntMathExprAttributes(ignoredIntOverflowForCurrentInstr, ignoredNegativeZeroForCurrentInstr);
  244. break;
  245. }
  246. case Js::OpCode::InlineMathFloor:
  247. case Js::OpCode::InlineMathCeil:
  248. case Js::OpCode::InlineMathRound:
  249. if (!instr->ShouldCheckForNegativeZero())
  250. {
  251. return;
  252. }
  253. break;
  254. case Js::OpCode::Conv_Prim:
  255. exprAttributes = ConvAttributes(instr->GetDst()->IsUnsigned(), instr->GetSrc1()->IsUnsigned());
  256. break;
  257. }
  258. ValueInfo *valueInfo = NULL;
  259. if (instr->GetDst())
  260. {
  261. if (!dstVal)
  262. {
  263. return;
  264. }
  265. valueInfo = dstVal->GetValueInfo();
  266. if(valueInfo->GetSymStore() == NULL &&
  267. !(isArray && valueInfo->HasIntConstantValue() && valueInfo->IsIntAndLikelyTagged() && instr->GetSrc1()->IsAddrOpnd()))
  268. {
  269. return;
  270. }
  271. }
  272. if (!this->GetHash(instr, src1Val, src2Val, exprAttributes, &hash))
  273. {
  274. return;
  275. }
  276. int32 intConstantValue;
  277. if(valueInfo && !valueInfo->GetSymStore() && valueInfo->TryGetIntConstantValue(&intConstantValue))
  278. {
  279. Assert(isArray);
  280. Assert(valueInfo->IsIntAndLikelyTagged());
  281. Assert(instr->GetSrc1()->IsAddrOpnd());
  282. // We need a sym associated with a value in the expression value table. Hoist the address into a stack sym associated
  283. // with the int constant value.
  284. StackSym *const constStackSym = GetOrCreateTaggedIntConstantStackSym(intConstantValue);
  285. instr->HoistSrc1(Js::OpCode::Ld_A, RegNOREG, constStackSym);
  286. currentBlock->globOptData.SetValue(dstVal, constStackSym);
  287. }
  288. // We have a candidate. Add it to the exprToValueMap.
  289. Value ** pVal = block->globOptData.exprToValueMap->FindOrInsertNew(hash);
  290. *pVal = dstVal;
  291. if (isArray)
  292. {
  293. block->globOptData.liveArrayValues->Set(hash);
  294. }
  295. if (MayNeedBailOnImplicitCall(instr, src1Val, src2Val))
  296. {
  297. this->currentBlock->globOptData.hasCSECandidates = true;
  298. // Use LiveFields to track is object.valueOf/toString could get overridden.
  299. IR::Opnd *src1 = instr->GetSrc1();
  300. if (src1)
  301. {
  302. if (src1->IsRegOpnd())
  303. {
  304. StackSym *varSym = src1->AsRegOpnd()->m_sym;
  305. if (varSym->IsTypeSpec())
  306. {
  307. varSym = varSym->GetVarEquivSym(this->func);
  308. }
  309. block->globOptData.liveFields->Set(varSym->m_id);
  310. }
  311. IR::Opnd *src2 = instr->GetSrc2();
  312. if (src2 && src2->IsRegOpnd())
  313. {
  314. StackSym *varSym = src2->AsRegOpnd()->m_sym;
  315. if (varSym->IsTypeSpec())
  316. {
  317. varSym = varSym->GetVarEquivSym(this->func);
  318. }
  319. block->globOptData.liveFields->Set(varSym->m_id);
  320. }
  321. }
  322. }
  323. }
  324. static void TransformIntoUnreachable(IntConstType errorCode, IR::Instr* instr)
  325. {
  326. instr->m_opcode = Js::OpCode::ThrowRuntimeError;
  327. instr->ReplaceSrc1(IR::IntConstOpnd::New(SCODE_CODE(errorCode), TyInt32, instr->m_func));
  328. instr->UnlinkDst();
  329. }
  330. void
  331. GlobOpt::OptimizeChecks(IR::Instr * const instr)
  332. {
  333. IR::Opnd* src1 = instr->GetSrc1();
  334. IR::Opnd* src2 = instr->GetSrc2();
  335. switch (instr->m_opcode)
  336. {
  337. case Js::OpCode::TrapIfZero:
  338. if (src1 && src1->IsImmediateOpnd())
  339. {
  340. int64 val = src1->GetImmediateValue(func);
  341. if (val != 0)
  342. {
  343. instr->m_opcode = Js::OpCode::Ld_I4;
  344. }
  345. else
  346. {
  347. TransformIntoUnreachable(WASMERR_DivideByZero, instr);
  348. InsertByteCodeUses(instr);
  349. RemoveCodeAfterNoFallthroughInstr(instr); //remove dead code
  350. }
  351. }
  352. break;
  353. case Js::OpCode::TrapIfMinIntOverNegOne:
  354. {
  355. int checksLeft = 2;
  356. if (src1 && src1->IsImmediateOpnd())
  357. {
  358. int64 val = src1->GetImmediateValue(func);
  359. bool isMintInt = src1->GetSize() == 8 ? val == LONGLONG_MIN : (int32)val == INT_MIN;
  360. if (!isMintInt)
  361. {
  362. instr->m_opcode = Js::OpCode::Ld_I4;
  363. }
  364. else
  365. {
  366. checksLeft--;
  367. }
  368. }
  369. if (src2 && src2->IsImmediateOpnd())
  370. {
  371. int64 val = src2->GetImmediateValue(func);
  372. bool isNegOne = src2->GetSize() == 8 ? val == -1 : (int32)val == -1;
  373. if (!isNegOne)
  374. {
  375. instr->m_opcode = Js::OpCode::Ld_I4;
  376. }
  377. else
  378. {
  379. checksLeft--;
  380. }
  381. }
  382. if (!checksLeft)
  383. {
  384. TransformIntoUnreachable(VBSERR_Overflow, instr);
  385. instr->FreeSrc2();
  386. InsertByteCodeUses(instr);
  387. RemoveCodeAfterNoFallthroughInstr(instr); //remove dead code
  388. }
  389. else if (instr->m_opcode == Js::OpCode::Ld_I4)
  390. {
  391. instr->FreeSrc2();
  392. }
  393. break;
  394. }
  395. default:
  396. return;
  397. }
  398. }
  399. bool
  400. GlobOpt::CSEOptimize(BasicBlock *block, IR::Instr * *const instrRef, Value **pSrc1Val, Value **pSrc2Val, Value **pSrc1IndirIndexVal, bool intMathExprOnly)
  401. {
  402. Assert(instrRef);
  403. IR::Instr *&instr = *instrRef;
  404. Assert(instr);
  405. if (!this->DoCSE())
  406. {
  407. return false;
  408. }
  409. Value *src1Val = *pSrc1Val;
  410. Value *src2Val = *pSrc2Val;
  411. Value *src1IndirIndexVal = *pSrc1IndirIndexVal;
  412. bool isArray = false;
  413. ExprAttributes exprAttributes;
  414. ExprHash hash;
  415. // For arrays, hash the value # of the baseOpnd and indexOpnd
  416. switch(instr->m_opcode)
  417. {
  418. case Js::OpCode::LdArrViewElem:
  419. case Js::OpCode::LdElemI_A:
  420. {
  421. if(intMathExprOnly)
  422. {
  423. return false;
  424. }
  425. IR::IndirOpnd *arrayOpnd = instr->GetSrc1()->AsIndirOpnd();
  426. src1Val = block->globOptData.FindValue(arrayOpnd->GetBaseOpnd()->m_sym);
  427. if(src1IndirIndexVal)
  428. {
  429. src2Val = src1IndirIndexVal;
  430. }
  431. else if (arrayOpnd->GetIndexOpnd())
  432. {
  433. src2Val = block->globOptData.FindValue(arrayOpnd->GetIndexOpnd()->m_sym);
  434. }
  435. else
  436. {
  437. return false;
  438. }
  439. // for typed array do not add instructions whose dst are guaranteed to be int or number
  440. // as we will try to eliminate bound check for these typed arrays
  441. if (src1Val->GetValueInfo()->IsLikelyOptimizedVirtualTypedArray())
  442. {
  443. exprAttributes = DstIsIntOrNumberAttributes(!instr->dstIsAlwaysConvertedToInt32, !instr->dstIsAlwaysConvertedToNumber);
  444. }
  445. isArray = true;
  446. break;
  447. }
  448. case Js::OpCode::Neg_A:
  449. case Js::OpCode::Add_A:
  450. case Js::OpCode::Sub_A:
  451. case Js::OpCode::Mul_A:
  452. case Js::OpCode::Div_A:
  453. case Js::OpCode::Rem_A:
  454. case Js::OpCode::ShrU_A:
  455. // If the previously-computed matching expression ignored int overflow or negative zero, those attributes must match
  456. // to be able to CSE this expression
  457. if(intMathExprOnly && !ignoredIntOverflowForCurrentInstr && !ignoredNegativeZeroForCurrentInstr)
  458. {
  459. // Already tried CSE with default attributes
  460. return false;
  461. }
  462. exprAttributes = IntMathExprAttributes(ignoredIntOverflowForCurrentInstr, ignoredNegativeZeroForCurrentInstr);
  463. break;
  464. case Js::OpCode::Conv_Prim:
  465. exprAttributes = ConvAttributes(instr->GetDst()->IsUnsigned(), instr->GetSrc1()->IsUnsigned());
  466. break;
  467. default:
  468. if(intMathExprOnly)
  469. {
  470. return false;
  471. }
  472. break;
  473. }
  474. if (!this->GetHash(instr, src1Val, src2Val, exprAttributes, &hash))
  475. {
  476. return false;
  477. }
  478. // See if we have a value for that expression
  479. Value ** pVal = block->globOptData.exprToValueMap->Get(hash);
  480. if (pVal == NULL)
  481. {
  482. return false;
  483. }
  484. ValueInfo *valueInfo = NULL;
  485. Sym *symStore = NULL;
  486. Value *val = NULL;
  487. if (instr->GetDst())
  488. {
  489. if (*pVal == NULL)
  490. {
  491. return false;
  492. }
  493. val = *pVal;
  494. // Make sure the array value is still live. We can't CSE something like:
  495. // ... = A[i];
  496. // B[j] = ...;
  497. // ... = A[i];
  498. if (isArray && !block->globOptData.liveArrayValues->Test(hash))
  499. {
  500. return false;
  501. }
  502. // See if the symStore is still valid
  503. valueInfo = val->GetValueInfo();
  504. symStore = valueInfo->GetSymStore();
  505. Value * symStoreVal = NULL;
  506. int32 intConstantValue;
  507. if (!symStore && valueInfo->TryGetIntConstantValue(&intConstantValue))
  508. {
  509. // Handle:
  510. // A[i] = 10;
  511. // ... = A[i];
  512. if (!isArray)
  513. {
  514. return false;
  515. }
  516. if (!valueInfo->IsIntAndLikelyTagged())
  517. {
  518. return false;
  519. }
  520. symStore = GetTaggedIntConstantStackSym(intConstantValue);
  521. }
  522. if(!symStore || !symStore->IsStackSym())
  523. {
  524. return false;
  525. }
  526. symStoreVal = block->globOptData.FindValue(symStore);
  527. if (!symStoreVal || symStoreVal->GetValueNumber() != val->GetValueNumber())
  528. {
  529. return false;
  530. }
  531. val = symStoreVal;
  532. valueInfo = val->GetValueInfo();
  533. }
  534. // Make sure srcs still have same values
  535. if (instr->GetSrc1())
  536. {
  537. if (!src1Val)
  538. {
  539. return false;
  540. }
  541. if (hash.GetSrc1ValueNumber() != src1Val->GetValueNumber())
  542. {
  543. return false;
  544. }
  545. IR::Opnd *src1 = instr->GetSrc1();
  546. if (src1->IsSymOpnd() && src1->AsSymOpnd()->IsPropertySymOpnd())
  547. {
  548. Assert(instr->m_opcode == Js::OpCode::CheckFixedFld);
  549. IR::PropertySymOpnd *propOpnd = src1->AsSymOpnd()->AsPropertySymOpnd();
  550. if (!propOpnd->IsTypeChecked() && !propOpnd->IsRootObjectNonConfigurableFieldLoad())
  551. {
  552. // Require m_CachedTypeChecked for 2 reasons:
  553. // - We may be relying on this instruction to do a type check for a downstream reference.
  554. // - This instruction may have a different inline cache, and thus need to check a different type,
  555. // than the upstream check.
  556. // REVIEW: We could process this differently somehow to get the type check on the next reference.
  557. return false;
  558. }
  559. }
  560. if (instr->GetSrc2())
  561. {
  562. if (!src2Val)
  563. {
  564. return false;
  565. }
  566. if (hash.GetSrc2ValueNumber() != src2Val->GetValueNumber())
  567. {
  568. return false;
  569. }
  570. }
  571. }
  572. bool needsBailOnImplicitCall = false;
  573. // Need implicit call bailouts?
  574. if (this->MayNeedBailOnImplicitCall(instr, src1Val, src2Val))
  575. {
  576. needsBailOnImplicitCall = true;
  577. IR::Opnd *src1 = instr->GetSrc1();
  578. if (instr->m_opcode != Js::OpCode::StElemI_A && instr->m_opcode != Js::OpCode::StElemI_A_Strict
  579. && src1 && src1->IsRegOpnd())
  580. {
  581. StackSym *sym1 = src1->AsRegOpnd()->m_sym;
  582. if (block->globOptData.IsTypeSpecialized(sym1) || block->globOptData.liveInt32Syms->Test(sym1->m_id))
  583. {
  584. IR::Opnd *src2 = instr->GetSrc2();
  585. if (!src2 || src2->IsImmediateOpnd())
  586. {
  587. needsBailOnImplicitCall = false;
  588. }
  589. else if (src2->IsRegOpnd())
  590. {
  591. StackSym *sym2 = src2->AsRegOpnd()->m_sym;
  592. if (block->globOptData.IsTypeSpecialized(sym2) || block->globOptData.liveInt32Syms->Test(sym2->m_id))
  593. {
  594. needsBailOnImplicitCall = false;
  595. }
  596. }
  597. }
  598. }
  599. }
  600. if (needsBailOnImplicitCall)
  601. {
  602. IR::Opnd *src1 = instr->GetSrc1();
  603. if (src1)
  604. {
  605. if (src1->IsRegOpnd())
  606. {
  607. StackSym *varSym1 = src1->AsRegOpnd()->m_sym;
  608. Assert(!varSym1->IsTypeSpec());
  609. if (!block->globOptData.liveFields->Test(varSym1->m_id))
  610. {
  611. return false;
  612. }
  613. IR::Opnd *src2 = instr->GetSrc2();
  614. if (src2 && src2->IsRegOpnd())
  615. {
  616. StackSym *varSym2 = src2->AsRegOpnd()->m_sym;
  617. Assert(!varSym2->IsTypeSpec());
  618. if (!block->globOptData.liveFields->Test(varSym2->m_id))
  619. {
  620. return false;
  621. }
  622. }
  623. }
  624. }
  625. }
  626. // in asmjs we can have a symstore with a different type
  627. // x = HEAPF32[i >> 2]
  628. // y = HEAPI32[i >> 2]
  629. if (instr->GetDst() && (instr->GetDst()->GetType() != symStore->AsStackSym()->GetType()))
  630. {
  631. Assert(GetIsAsmJSFunc());
  632. return false;
  633. }
  634. // SIMD_JS
  635. if (instr->m_opcode == Js::OpCode::ExtendArg_A)
  636. {
  637. // we don't want to CSE ExtendArgs, only the operation using them. To do that, we mimic CSE by transferring the symStore valueInfo to the dst.
  638. IR::Opnd *dst = instr->GetDst();
  639. Value *dstVal = this->currentBlock->globOptData.FindValue(symStore);
  640. this->currentBlock->globOptData.SetValue(dstVal, dst);
  641. dst->AsRegOpnd()->m_sym->CopySymAttrs(symStore->AsStackSym());
  642. return false;
  643. }
  644. //
  645. // Success, do the CSE rewrite.
  646. //
  647. #if DBG_DUMP
  648. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::CSEPhase, this->func->GetSourceContextId(), this->func->GetLocalFunctionId()))
  649. {
  650. Output::Print(_u(" --- CSE (%s): "), this->func->GetJITFunctionBody()->GetDisplayName());
  651. instr->Dump();
  652. }
  653. #endif
  654. #if ENABLE_DEBUG_CONFIG_OPTIONS
  655. if (Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::CSEPhase, this->func->GetSourceContextId(), this->func->GetLocalFunctionId()))
  656. {
  657. Output::Print(_u(" --- CSE (%s): %s\n"), this->func->GetJITFunctionBody()->GetDisplayName(), Js::OpCodeUtil::GetOpCodeName(instr->m_opcode));
  658. }
  659. #endif
  660. this->CaptureByteCodeSymUses(instr);
  661. if (!instr->GetDst())
  662. {
  663. instr->m_opcode = Js::OpCode::Nop;
  664. return true;
  665. }
  666. AnalysisAssert(valueInfo);
  667. IR::Opnd *cseOpnd;
  668. cseOpnd = IR::RegOpnd::New(symStore->AsStackSym(), instr->GetDst()->GetType(), instr->m_func);
  669. cseOpnd->SetValueType(valueInfo->Type());
  670. cseOpnd->SetIsJITOptimizedReg(true);
  671. if (needsBailOnImplicitCall)
  672. {
  673. this->CaptureNoImplicitCallUses(cseOpnd, false);
  674. }
  675. int32 intConstantValue;
  676. if (valueInfo->TryGetIntConstantValue(&intConstantValue) && valueInfo->IsIntAndLikelyTagged())
  677. {
  678. cseOpnd->Free(func);
  679. cseOpnd = IR::AddrOpnd::New(Js::TaggedInt::ToVarUnchecked(intConstantValue), IR::AddrOpndKindConstantVar, instr->m_func);
  680. cseOpnd->SetValueType(valueInfo->Type());
  681. }
  682. *pSrc1Val = val;
  683. {
  684. // Profiled instructions have data that is interpreted differently based on the op code. Since we're changing the op
  685. // code and due to other similar potential issues, always create a new instr instead of changing the existing one.
  686. IR::Instr *const originalInstr = instr;
  687. instr = IR::Instr::New(Js::OpCode::Ld_A, instr->GetDst(), cseOpnd, instr->m_func);
  688. originalInstr->TransferDstAttributesTo(instr);
  689. block->InsertInstrBefore(instr, originalInstr);
  690. block->RemoveInstr(originalInstr);
  691. }
  692. *pSrc2Val = NULL;
  693. *pSrc1IndirIndexVal = NULL;
  694. return true;
  695. }
  696. void
  697. GlobOpt::ProcessArrayValueKills(IR::Instr *instr)
  698. {
  699. switch (instr->m_opcode)
  700. {
  701. case Js::OpCode::StElemI_A:
  702. case Js::OpCode::StElemI_A_Strict:
  703. case Js::OpCode::DeleteElemI_A:
  704. case Js::OpCode::DeleteElemIStrict_A:
  705. case Js::OpCode::StFld:
  706. case Js::OpCode::StRootFld:
  707. case Js::OpCode::StFldStrict:
  708. case Js::OpCode::StRootFldStrict:
  709. case Js::OpCode::StSlot:
  710. case Js::OpCode::StSlotChkUndecl:
  711. case Js::OpCode::DeleteFld:
  712. case Js::OpCode::DeleteRootFld:
  713. case Js::OpCode::DeleteFldStrict:
  714. case Js::OpCode::DeleteRootFldStrict:
  715. case Js::OpCode::StArrViewElem:
  716. // These array helpers may change A.length (and A[i] could be A.length)...
  717. case Js::OpCode::InlineArrayPush:
  718. case Js::OpCode::InlineArrayPop:
  719. this->currentBlock->globOptData.liveArrayValues->ClearAll();
  720. break;
  721. case Js::OpCode::CallDirect:
  722. Assert(instr->GetSrc1());
  723. switch(instr->GetSrc1()->AsHelperCallOpnd()->m_fnHelper)
  724. {
  725. // These array helpers may change A[i]
  726. case IR::HelperArray_Reverse:
  727. case IR::HelperArray_Shift:
  728. case IR::HelperArray_Unshift:
  729. case IR::HelperArray_Splice:
  730. this->currentBlock->globOptData.liveArrayValues->ClearAll();
  731. break;
  732. }
  733. break;
  734. default:
  735. if (instr->UsesAllFields())
  736. {
  737. this->currentBlock->globOptData.liveArrayValues->ClearAll();
  738. }
  739. break;
  740. }
  741. }
  742. bool
  743. GlobOpt::NeedBailOnImplicitCallForCSE(BasicBlock const *block, bool isForwardPass)
  744. {
  745. return isForwardPass && block->globOptData.hasCSECandidates;
  746. }
  747. bool
  748. GlobOpt::DoCSE()
  749. {
  750. if (PHASE_OFF(Js::CSEPhase, this->func))
  751. {
  752. return false;
  753. }
  754. if (this->IsLoopPrePass())
  755. {
  756. return false;
  757. }
  758. if (PHASE_FORCE(Js::CSEPhase, this->func))
  759. {
  760. // Force always turn it on
  761. return true;
  762. }
  763. if (!this->DoFieldOpts(this->currentBlock->loop) && !GetIsAsmJSFunc())
  764. {
  765. return false;
  766. }
  767. return true;
  768. }
  769. bool
  770. GlobOpt::CanCSEArrayStore(IR::Instr *instr)
  771. {
  772. IR::Opnd *arrayOpnd = instr->GetDst();
  773. Assert(arrayOpnd->IsIndirOpnd());
  774. IR::RegOpnd *baseOpnd = arrayOpnd->AsIndirOpnd()->GetBaseOpnd();
  775. ValueType baseValueType(baseOpnd->GetValueType());
  776. // Only handle definite arrays for now. Typed Arrays would require truncation of the CSE'd value.
  777. if (!baseValueType.IsArrayOrObjectWithArray())
  778. {
  779. return false;
  780. }
  781. return true;
  782. }
  783. #if DBG_DUMP
  784. void
  785. DumpExpr(ExprHash hash)
  786. {
  787. Output::Print(_u("Opcode: %10s src1Val: %d src2Val: %d\n"), Js::OpCodeUtil::GetOpCodeName(HashToOpCode[(int)hash.GetOpcode()]), hash.GetSrc1ValueNumber(), hash.GetSrc2ValueNumber());
  788. }
  789. #endif