GlobOptExpr.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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. case Js::OpCode::Conv_Prim_Sat:
  256. exprAttributes = ConvAttributes(instr->GetDst()->IsUnsigned(), instr->GetSrc1()->IsUnsigned());
  257. break;
  258. }
  259. ValueInfo *valueInfo = NULL;
  260. if (instr->GetDst())
  261. {
  262. if (!dstVal)
  263. {
  264. return;
  265. }
  266. valueInfo = dstVal->GetValueInfo();
  267. if(valueInfo->GetSymStore() == NULL &&
  268. !(isArray && valueInfo->HasIntConstantValue() && valueInfo->IsIntAndLikelyTagged() && instr->GetSrc1()->IsAddrOpnd()))
  269. {
  270. return;
  271. }
  272. }
  273. if (!this->GetHash(instr, src1Val, src2Val, exprAttributes, &hash))
  274. {
  275. return;
  276. }
  277. int32 intConstantValue;
  278. if(valueInfo && !valueInfo->GetSymStore() && valueInfo->TryGetIntConstantValue(&intConstantValue))
  279. {
  280. Assert(isArray);
  281. Assert(valueInfo->IsIntAndLikelyTagged());
  282. Assert(instr->GetSrc1()->IsAddrOpnd());
  283. // We need a sym associated with a value in the expression value table. Hoist the address into a stack sym associated
  284. // with the int constant value.
  285. StackSym *const constStackSym = GetOrCreateTaggedIntConstantStackSym(intConstantValue);
  286. instr->HoistSrc1(Js::OpCode::Ld_A, RegNOREG, constStackSym);
  287. currentBlock->globOptData.SetValue(dstVal, constStackSym);
  288. }
  289. // We have a candidate. Add it to the exprToValueMap.
  290. Value ** pVal = block->globOptData.exprToValueMap->FindOrInsertNew(hash);
  291. *pVal = dstVal;
  292. if (isArray)
  293. {
  294. block->globOptData.liveArrayValues->Set(hash);
  295. }
  296. if (MayNeedBailOnImplicitCall(instr, src1Val, src2Val))
  297. {
  298. this->currentBlock->globOptData.hasCSECandidates = true;
  299. // Use LiveFields to track is object.valueOf/toString could get overridden.
  300. IR::Opnd *src1 = instr->GetSrc1();
  301. if (src1)
  302. {
  303. if (src1->IsRegOpnd())
  304. {
  305. StackSym *varSym = src1->AsRegOpnd()->m_sym;
  306. if (varSym->IsTypeSpec())
  307. {
  308. varSym = varSym->GetVarEquivSym(this->func);
  309. }
  310. block->globOptData.liveFields->Set(varSym->m_id);
  311. }
  312. IR::Opnd *src2 = instr->GetSrc2();
  313. if (src2 && src2->IsRegOpnd())
  314. {
  315. StackSym *varSym = src2->AsRegOpnd()->m_sym;
  316. if (varSym->IsTypeSpec())
  317. {
  318. varSym = varSym->GetVarEquivSym(this->func);
  319. }
  320. block->globOptData.liveFields->Set(varSym->m_id);
  321. }
  322. }
  323. }
  324. }
  325. static void TransformIntoUnreachable(IntConstType errorCode, IR::Instr* instr)
  326. {
  327. instr->m_opcode = Js::OpCode::ThrowRuntimeError;
  328. instr->ReplaceSrc1(IR::IntConstOpnd::New(SCODE_CODE(errorCode), TyInt32, instr->m_func));
  329. instr->UnlinkDst();
  330. }
  331. void
  332. GlobOpt::OptimizeChecks(IR::Instr * const instr)
  333. {
  334. IR::Opnd* src1 = instr->GetSrc1();
  335. IR::Opnd* src2 = instr->GetSrc2();
  336. switch (instr->m_opcode)
  337. {
  338. case Js::OpCode::TrapIfZero:
  339. if (src1 && src1->IsImmediateOpnd())
  340. {
  341. int64 val = src1->GetImmediateValue(func);
  342. if (val != 0)
  343. {
  344. instr->m_opcode = Js::OpCode::Ld_I4;
  345. }
  346. else
  347. {
  348. TransformIntoUnreachable(WASMERR_DivideByZero, instr);
  349. InsertByteCodeUses(instr);
  350. RemoveCodeAfterNoFallthroughInstr(instr); //remove dead code
  351. }
  352. }
  353. break;
  354. case Js::OpCode::TrapIfMinIntOverNegOne:
  355. {
  356. int checksLeft = 2;
  357. if (src1 && src1->IsImmediateOpnd())
  358. {
  359. int64 val = src1->GetImmediateValue(func);
  360. bool isMintInt = src1->GetSize() == 8 ? val == LONGLONG_MIN : (int32)val == INT_MIN;
  361. if (!isMintInt)
  362. {
  363. instr->m_opcode = Js::OpCode::Ld_I4;
  364. }
  365. else
  366. {
  367. checksLeft--;
  368. }
  369. }
  370. if (src2 && src2->IsImmediateOpnd())
  371. {
  372. int64 val = src2->GetImmediateValue(func);
  373. bool isNegOne = src2->GetSize() == 8 ? val == -1 : (int32)val == -1;
  374. if (!isNegOne)
  375. {
  376. instr->m_opcode = Js::OpCode::Ld_I4;
  377. }
  378. else
  379. {
  380. checksLeft--;
  381. }
  382. }
  383. if (!checksLeft)
  384. {
  385. TransformIntoUnreachable(VBSERR_Overflow, instr);
  386. instr->FreeSrc2();
  387. InsertByteCodeUses(instr);
  388. RemoveCodeAfterNoFallthroughInstr(instr); //remove dead code
  389. }
  390. else if (instr->m_opcode == Js::OpCode::Ld_I4)
  391. {
  392. instr->FreeSrc2();
  393. }
  394. break;
  395. }
  396. case Js::OpCode::TrapIfUnalignedAccess:
  397. if (src1 && src1->IsImmediateOpnd())
  398. {
  399. int64 val = src1->GetImmediateValue(func);
  400. Assert(src2->IsImmediateOpnd());
  401. uint32 cmpValue = (uint32)src2->GetImmediateValue(func);
  402. uint32 mask = src2->GetSize() - 1;
  403. Assert((cmpValue & ~mask) == 0);
  404. if (((uint32)val & mask) == cmpValue)
  405. {
  406. instr->FreeSrc2();
  407. instr->m_opcode = Js::OpCode::Ld_I4;
  408. }
  409. else
  410. {
  411. TransformIntoUnreachable(WASMERR_UnalignedAtomicAccess, instr);
  412. InsertByteCodeUses(instr);
  413. RemoveCodeAfterNoFallthroughInstr(instr); //remove dead code
  414. }
  415. }
  416. break;
  417. default:
  418. return;
  419. }
  420. }
  421. bool
  422. GlobOpt::CSEOptimize(BasicBlock *block, IR::Instr * *const instrRef, Value **pSrc1Val, Value **pSrc2Val, Value **pSrc1IndirIndexVal, bool intMathExprOnly)
  423. {
  424. Assert(instrRef);
  425. IR::Instr *&instr = *instrRef;
  426. Assert(instr);
  427. if (!this->DoCSE())
  428. {
  429. return false;
  430. }
  431. Value *src1Val = *pSrc1Val;
  432. Value *src2Val = *pSrc2Val;
  433. Value *src1IndirIndexVal = *pSrc1IndirIndexVal;
  434. bool isArray = false;
  435. ExprAttributes exprAttributes;
  436. ExprHash hash;
  437. // For arrays, hash the value # of the baseOpnd and indexOpnd
  438. switch(instr->m_opcode)
  439. {
  440. case Js::OpCode::LdArrViewElem:
  441. case Js::OpCode::LdElemI_A:
  442. {
  443. if(intMathExprOnly)
  444. {
  445. return false;
  446. }
  447. IR::IndirOpnd *arrayOpnd = instr->GetSrc1()->AsIndirOpnd();
  448. src1Val = block->globOptData.FindValue(arrayOpnd->GetBaseOpnd()->m_sym);
  449. if(src1IndirIndexVal)
  450. {
  451. src2Val = src1IndirIndexVal;
  452. }
  453. else if (arrayOpnd->GetIndexOpnd())
  454. {
  455. src2Val = block->globOptData.FindValue(arrayOpnd->GetIndexOpnd()->m_sym);
  456. }
  457. else
  458. {
  459. return false;
  460. }
  461. // for typed array do not add instructions whose dst are guaranteed to be int or number
  462. // as we will try to eliminate bound check for these typed arrays
  463. if (src1Val->GetValueInfo()->IsLikelyOptimizedVirtualTypedArray())
  464. {
  465. exprAttributes = DstIsIntOrNumberAttributes(!instr->dstIsAlwaysConvertedToInt32, !instr->dstIsAlwaysConvertedToNumber);
  466. }
  467. isArray = true;
  468. break;
  469. }
  470. case Js::OpCode::Neg_A:
  471. case Js::OpCode::Add_A:
  472. case Js::OpCode::Sub_A:
  473. case Js::OpCode::Mul_A:
  474. case Js::OpCode::Div_A:
  475. case Js::OpCode::Rem_A:
  476. case Js::OpCode::ShrU_A:
  477. // If the previously-computed matching expression ignored int overflow or negative zero, those attributes must match
  478. // to be able to CSE this expression
  479. if(intMathExprOnly && !ignoredIntOverflowForCurrentInstr && !ignoredNegativeZeroForCurrentInstr)
  480. {
  481. // Already tried CSE with default attributes
  482. return false;
  483. }
  484. exprAttributes = IntMathExprAttributes(ignoredIntOverflowForCurrentInstr, ignoredNegativeZeroForCurrentInstr);
  485. break;
  486. case Js::OpCode::Conv_Prim:
  487. case Js::OpCode::Conv_Prim_Sat:
  488. exprAttributes = ConvAttributes(instr->GetDst()->IsUnsigned(), instr->GetSrc1()->IsUnsigned());
  489. break;
  490. default:
  491. if(intMathExprOnly)
  492. {
  493. return false;
  494. }
  495. break;
  496. }
  497. if (!this->GetHash(instr, src1Val, src2Val, exprAttributes, &hash))
  498. {
  499. return false;
  500. }
  501. // See if we have a value for that expression
  502. Value ** pVal = block->globOptData.exprToValueMap->Get(hash);
  503. if (pVal == NULL)
  504. {
  505. return false;
  506. }
  507. ValueInfo *valueInfo = NULL;
  508. Sym *symStore = NULL;
  509. Value *val = NULL;
  510. if (instr->GetDst())
  511. {
  512. if (*pVal == NULL)
  513. {
  514. return false;
  515. }
  516. val = *pVal;
  517. // Make sure the array value is still live. We can't CSE something like:
  518. // ... = A[i];
  519. // B[j] = ...;
  520. // ... = A[i];
  521. if (isArray && !block->globOptData.liveArrayValues->Test(hash))
  522. {
  523. return false;
  524. }
  525. // See if the symStore is still valid
  526. valueInfo = val->GetValueInfo();
  527. symStore = valueInfo->GetSymStore();
  528. Value * symStoreVal = NULL;
  529. int32 intConstantValue;
  530. if (!symStore && valueInfo->TryGetIntConstantValue(&intConstantValue))
  531. {
  532. // Handle:
  533. // A[i] = 10;
  534. // ... = A[i];
  535. if (!isArray)
  536. {
  537. return false;
  538. }
  539. if (!valueInfo->IsIntAndLikelyTagged())
  540. {
  541. return false;
  542. }
  543. symStore = GetTaggedIntConstantStackSym(intConstantValue);
  544. }
  545. if(!symStore || !symStore->IsStackSym())
  546. {
  547. return false;
  548. }
  549. symStoreVal = block->globOptData.FindValue(symStore);
  550. if (!symStoreVal || symStoreVal->GetValueNumber() != val->GetValueNumber())
  551. {
  552. return false;
  553. }
  554. val = symStoreVal;
  555. valueInfo = val->GetValueInfo();
  556. }
  557. // Make sure srcs still have same values
  558. if (instr->GetSrc1())
  559. {
  560. if (!src1Val)
  561. {
  562. return false;
  563. }
  564. if (hash.GetSrc1ValueNumber() != src1Val->GetValueNumber())
  565. {
  566. return false;
  567. }
  568. IR::Opnd *src1 = instr->GetSrc1();
  569. if (src1->IsSymOpnd() && src1->AsSymOpnd()->IsPropertySymOpnd())
  570. {
  571. Assert(instr->m_opcode == Js::OpCode::CheckFixedFld);
  572. IR::PropertySymOpnd *propOpnd = src1->AsSymOpnd()->AsPropertySymOpnd();
  573. if (!propOpnd->IsTypeChecked() && !propOpnd->IsRootObjectNonConfigurableFieldLoad())
  574. {
  575. // Require m_CachedTypeChecked for 2 reasons:
  576. // - We may be relying on this instruction to do a type check for a downstream reference.
  577. // - This instruction may have a different inline cache, and thus need to check a different type,
  578. // than the upstream check.
  579. // REVIEW: We could process this differently somehow to get the type check on the next reference.
  580. return false;
  581. }
  582. }
  583. if (instr->GetSrc2())
  584. {
  585. if (!src2Val)
  586. {
  587. return false;
  588. }
  589. if (hash.GetSrc2ValueNumber() != src2Val->GetValueNumber())
  590. {
  591. return false;
  592. }
  593. }
  594. }
  595. bool needsBailOnImplicitCall = false;
  596. // Need implicit call bailouts?
  597. if (this->MayNeedBailOnImplicitCall(instr, src1Val, src2Val))
  598. {
  599. needsBailOnImplicitCall = true;
  600. IR::Opnd *src1 = instr->GetSrc1();
  601. if (instr->m_opcode != Js::OpCode::StElemI_A && instr->m_opcode != Js::OpCode::StElemI_A_Strict
  602. && src1 && src1->IsRegOpnd())
  603. {
  604. StackSym *sym1 = src1->AsRegOpnd()->m_sym;
  605. if (block->globOptData.IsTypeSpecialized(sym1) || block->globOptData.liveInt32Syms->Test(sym1->m_id))
  606. {
  607. IR::Opnd *src2 = instr->GetSrc2();
  608. if (!src2 || src2->IsImmediateOpnd())
  609. {
  610. needsBailOnImplicitCall = false;
  611. }
  612. else if (src2->IsRegOpnd())
  613. {
  614. StackSym *sym2 = src2->AsRegOpnd()->m_sym;
  615. if (block->globOptData.IsTypeSpecialized(sym2) || block->globOptData.liveInt32Syms->Test(sym2->m_id))
  616. {
  617. needsBailOnImplicitCall = false;
  618. }
  619. }
  620. }
  621. }
  622. }
  623. if (needsBailOnImplicitCall)
  624. {
  625. IR::Opnd *src1 = instr->GetSrc1();
  626. if (src1)
  627. {
  628. if (src1->IsRegOpnd())
  629. {
  630. StackSym *varSym1 = src1->AsRegOpnd()->m_sym;
  631. Assert(!varSym1->IsTypeSpec());
  632. if (!block->globOptData.liveFields->Test(varSym1->m_id))
  633. {
  634. return false;
  635. }
  636. IR::Opnd *src2 = instr->GetSrc2();
  637. if (src2 && src2->IsRegOpnd())
  638. {
  639. StackSym *varSym2 = src2->AsRegOpnd()->m_sym;
  640. Assert(!varSym2->IsTypeSpec());
  641. if (!block->globOptData.liveFields->Test(varSym2->m_id))
  642. {
  643. return false;
  644. }
  645. }
  646. }
  647. }
  648. }
  649. // in asmjs we can have a symstore with a different type
  650. // x = HEAPF32[i >> 2]
  651. // y = HEAPI32[i >> 2]
  652. if (instr->GetDst() && (instr->GetDst()->GetType() != symStore->AsStackSym()->GetType()))
  653. {
  654. Assert(GetIsAsmJSFunc());
  655. return false;
  656. }
  657. // SIMD_JS
  658. if (instr->m_opcode == Js::OpCode::ExtendArg_A)
  659. {
  660. // 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.
  661. IR::Opnd *dst = instr->GetDst();
  662. Value *dstVal = this->currentBlock->globOptData.FindValue(symStore);
  663. this->currentBlock->globOptData.SetValue(dstVal, dst);
  664. dst->AsRegOpnd()->m_sym->CopySymAttrs(symStore->AsStackSym());
  665. return false;
  666. }
  667. //
  668. // Success, do the CSE rewrite.
  669. //
  670. #if DBG_DUMP
  671. if (Js::Configuration::Global.flags.Trace.IsEnabled(Js::CSEPhase, this->func->GetSourceContextId(), this->func->GetLocalFunctionId()))
  672. {
  673. Output::Print(_u(" --- CSE (%s): "), this->func->GetJITFunctionBody()->GetDisplayName());
  674. instr->Dump();
  675. }
  676. #endif
  677. #if ENABLE_DEBUG_CONFIG_OPTIONS
  678. if (Js::Configuration::Global.flags.TestTrace.IsEnabled(Js::CSEPhase, this->func->GetSourceContextId(), this->func->GetLocalFunctionId()))
  679. {
  680. Output::Print(_u(" --- CSE (%s): %s\n"), this->func->GetJITFunctionBody()->GetDisplayName(), Js::OpCodeUtil::GetOpCodeName(instr->m_opcode));
  681. }
  682. #endif
  683. this->CaptureByteCodeSymUses(instr);
  684. if (!instr->GetDst())
  685. {
  686. instr->m_opcode = Js::OpCode::Nop;
  687. return true;
  688. }
  689. AnalysisAssert(valueInfo);
  690. IR::Opnd *cseOpnd;
  691. cseOpnd = IR::RegOpnd::New(symStore->AsStackSym(), instr->GetDst()->GetType(), instr->m_func);
  692. cseOpnd->SetValueType(valueInfo->Type());
  693. cseOpnd->SetIsJITOptimizedReg(true);
  694. if (needsBailOnImplicitCall)
  695. {
  696. this->CaptureNoImplicitCallUses(cseOpnd, false);
  697. }
  698. int32 intConstantValue;
  699. if (valueInfo->TryGetIntConstantValue(&intConstantValue) && valueInfo->IsIntAndLikelyTagged())
  700. {
  701. cseOpnd->Free(func);
  702. cseOpnd = IR::AddrOpnd::New(Js::TaggedInt::ToVarUnchecked(intConstantValue), IR::AddrOpndKindConstantVar, instr->m_func);
  703. cseOpnd->SetValueType(valueInfo->Type());
  704. }
  705. *pSrc1Val = val;
  706. {
  707. // Profiled instructions have data that is interpreted differently based on the op code. Since we're changing the op
  708. // code and due to other similar potential issues, always create a new instr instead of changing the existing one.
  709. IR::Instr *const originalInstr = instr;
  710. instr = IR::Instr::New(Js::OpCode::Ld_A, instr->GetDst(), cseOpnd, instr->m_func);
  711. instr->SetByteCodeOffset(originalInstr);
  712. originalInstr->TransferDstAttributesTo(instr);
  713. block->InsertInstrBefore(instr, originalInstr);
  714. block->RemoveInstr(originalInstr);
  715. }
  716. *pSrc2Val = NULL;
  717. *pSrc1IndirIndexVal = NULL;
  718. return true;
  719. }
  720. void
  721. GlobOpt::ProcessArrayValueKills(IR::Instr *instr)
  722. {
  723. switch (instr->m_opcode)
  724. {
  725. case Js::OpCode::StElemI_A:
  726. case Js::OpCode::StElemI_A_Strict:
  727. case Js::OpCode::DeleteElemI_A:
  728. case Js::OpCode::DeleteElemIStrict_A:
  729. case Js::OpCode::StFld:
  730. case Js::OpCode::StRootFld:
  731. case Js::OpCode::StFldStrict:
  732. case Js::OpCode::StRootFldStrict:
  733. case Js::OpCode::StSlot:
  734. case Js::OpCode::StSlotChkUndecl:
  735. case Js::OpCode::DeleteFld:
  736. case Js::OpCode::DeleteRootFld:
  737. case Js::OpCode::DeleteFldStrict:
  738. case Js::OpCode::DeleteRootFldStrict:
  739. case Js::OpCode::StArrViewElem:
  740. // These array helpers may change A.length (and A[i] could be A.length)...
  741. case Js::OpCode::InlineArrayPush:
  742. case Js::OpCode::InlineArrayPop:
  743. this->currentBlock->globOptData.liveArrayValues->ClearAll();
  744. break;
  745. case Js::OpCode::CallDirect:
  746. Assert(instr->GetSrc1());
  747. switch(instr->GetSrc1()->AsHelperCallOpnd()->m_fnHelper)
  748. {
  749. // These array helpers may change A[i]
  750. case IR::HelperArray_Reverse:
  751. case IR::HelperArray_Shift:
  752. case IR::HelperArray_Unshift:
  753. case IR::HelperArray_Splice:
  754. case IR::HelperArray_Concat:
  755. this->currentBlock->globOptData.liveArrayValues->ClearAll();
  756. break;
  757. }
  758. break;
  759. default:
  760. if (instr->UsesAllFields())
  761. {
  762. this->currentBlock->globOptData.liveArrayValues->ClearAll();
  763. }
  764. break;
  765. }
  766. }
  767. bool
  768. GlobOpt::NeedBailOnImplicitCallForCSE(BasicBlock const *block, bool isForwardPass)
  769. {
  770. return isForwardPass && block->globOptData.hasCSECandidates;
  771. }
  772. bool
  773. GlobOpt::DoCSE()
  774. {
  775. if (PHASE_OFF(Js::CSEPhase, this->func))
  776. {
  777. return false;
  778. }
  779. if (this->IsLoopPrePass())
  780. {
  781. return false;
  782. }
  783. if (PHASE_FORCE(Js::CSEPhase, this->func))
  784. {
  785. // Force always turn it on
  786. return true;
  787. }
  788. if (!this->DoFieldOpts(this->currentBlock->loop) && !GetIsAsmJSFunc())
  789. {
  790. return false;
  791. }
  792. return true;
  793. }
  794. bool
  795. GlobOpt::CanCSEArrayStore(IR::Instr *instr)
  796. {
  797. IR::Opnd *arrayOpnd = instr->GetDst();
  798. Assert(arrayOpnd->IsIndirOpnd());
  799. IR::RegOpnd *baseOpnd = arrayOpnd->AsIndirOpnd()->GetBaseOpnd();
  800. ValueType baseValueType(baseOpnd->GetValueType());
  801. // Only handle definite arrays for now. Typed Arrays would require truncation of the CSE'd value.
  802. if (!baseValueType.IsArrayOrObjectWithArray())
  803. {
  804. return false;
  805. }
  806. return true;
  807. }
  808. #if DBG_DUMP
  809. void
  810. DumpExpr(ExprHash hash)
  811. {
  812. Output::Print(_u("Opcode: %10s src1Val: %d src2Val: %d\n"), Js::OpCodeUtil::GetOpCodeName(HashToOpCode[(int)hash.GetOpcode()]), hash.GetSrc1ValueNumber(), hash.GetSrc2ValueNumber());
  813. }
  814. #endif