SwitchIRBuilder.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. #include "DataStructures\QuickSort.h"
  7. ///----------------------------------------------------------------------------
  8. ///
  9. /// IRBuilderSwitchAdapter
  10. ///
  11. /// Implementation for IRBuilderSwitchAdapter, which passes actions generated
  12. /// by a SwitchIRBuilder to an IRBuilder instance
  13. ///----------------------------------------------------------------------------
  14. void
  15. IRBuilderSwitchAdapter::AddBranchInstr(IR::BranchInstr * instr, uint32 offset, uint32 targetOffset, bool clearBackEdge)
  16. {
  17. BranchReloc * reloc = m_builder->AddBranchInstr(instr, offset, targetOffset);
  18. if (clearBackEdge)
  19. {
  20. reloc->SetNotBackEdge();
  21. }
  22. }
  23. void
  24. IRBuilderSwitchAdapter::AddInstr(IR::Instr * instr, uint32 offset)
  25. {
  26. m_builder->AddInstr(instr, offset);
  27. }
  28. void
  29. IRBuilderSwitchAdapter::CreateRelocRecord(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset, bool clearBackEdge)
  30. {
  31. BranchReloc * reloc = m_builder->CreateRelocRecord(
  32. branchInstr,
  33. offset,
  34. targetOffset);
  35. if (clearBackEdge)
  36. {
  37. reloc->SetNotBackEdge();
  38. }
  39. }
  40. void
  41. IRBuilderSwitchAdapter::ConvertToBailOut(IR::Instr * instr, IR::BailOutKind kind)
  42. {
  43. instr = instr->ConvertToBailOutInstr(instr, kind);
  44. Assert(instr->GetByteCodeOffset() < m_builder->m_offsetToInstructionCount);
  45. m_builder->m_offsetToInstruction[instr->GetByteCodeOffset()] = instr;
  46. }
  47. ///----------------------------------------------------------------------------
  48. ///
  49. /// IRBuilderAsmJsSwitchAdapter
  50. ///
  51. /// Implementation for IRBuilderSwitchAdapter, which passes actions generated
  52. /// by a SwitchIRBuilder to an IRBuilder instance
  53. ///----------------------------------------------------------------------------
  54. void
  55. IRBuilderAsmJsSwitchAdapter::AddBranchInstr(IR::BranchInstr * instr, uint32 offset, uint32 targetOffset, bool clearBackEdge)
  56. {
  57. BranchReloc * reloc = m_builder->AddBranchInstr(instr, offset, targetOffset);
  58. if (clearBackEdge)
  59. {
  60. reloc->SetNotBackEdge();
  61. }
  62. }
  63. void
  64. IRBuilderAsmJsSwitchAdapter::AddInstr(IR::Instr * instr, uint32 offset)
  65. {
  66. m_builder->AddInstr(instr, offset);
  67. }
  68. void
  69. IRBuilderAsmJsSwitchAdapter::CreateRelocRecord(IR::BranchInstr * branchInstr, uint32 offset, uint32 targetOffset, bool clearBackEdge)
  70. {
  71. BranchReloc * reloc = m_builder->CreateRelocRecord(
  72. branchInstr,
  73. offset,
  74. targetOffset);
  75. if (clearBackEdge)
  76. {
  77. reloc->SetNotBackEdge();
  78. }
  79. }
  80. void
  81. IRBuilderAsmJsSwitchAdapter::ConvertToBailOut(IR::Instr * instr, IR::BailOutKind kind)
  82. {
  83. Assert(false);
  84. // ConvertToBailOut should never get called for AsmJs
  85. // switches, since we already know ahead of time that the
  86. // switch expression is Int32
  87. }
  88. ///----------------------------------------------------------------------------
  89. ///
  90. /// SwitchIRBuilder::Init
  91. ///
  92. /// Initializes the function and temporary allocator for the SwitchIRBuilder
  93. ///----------------------------------------------------------------------------
  94. void
  95. SwitchIRBuilder::Init(Func * func, JitArenaAllocator * tempAlloc, bool isAsmJs)
  96. {
  97. m_func = func;
  98. m_tempAlloc = tempAlloc;
  99. m_isAsmJs = isAsmJs;
  100. // caseNodes is a list of Case instructions
  101. m_caseNodes = CaseNodeList::New(tempAlloc);
  102. m_seenOnlySingleCharStrCaseNodes = true;
  103. m_intConstSwitchCases = JitAnew(tempAlloc, BVSparse<JitArenaAllocator>, tempAlloc);
  104. m_strConstSwitchCases = StrSwitchCaseList::New(tempAlloc);
  105. m_eqOp = isAsmJs ? Js::OpCode::BrEq_I4 : Js::OpCode::BrSrEq_A;
  106. m_ltOp = isAsmJs ? Js::OpCode::BrLt_I4 : Js::OpCode::BrLt_A;
  107. m_leOp = isAsmJs ? Js::OpCode::BrLe_I4 : Js::OpCode::BrLe_A;
  108. m_gtOp = isAsmJs ? Js::OpCode::BrGt_I4 : Js::OpCode::BrGt_A;
  109. m_geOp = isAsmJs ? Js::OpCode::BrGe_I4 : Js::OpCode::BrGe_A;
  110. m_subOp = isAsmJs ? Js::OpCode::Sub_I4 : Js::OpCode::Sub_A;
  111. }
  112. ///----------------------------------------------------------------------------
  113. ///
  114. /// SwitchIRBuilder::BeginSwitch
  115. ///
  116. /// Prepares the SwitchIRBuilder for building a new switch statement
  117. ///----------------------------------------------------------------------------
  118. void
  119. SwitchIRBuilder::BeginSwitch()
  120. {
  121. m_intConstSwitchCases->ClearAll();
  122. m_strConstSwitchCases->Clear();
  123. if (m_isAsmJs)
  124. {
  125. // never build bailout information for asmjs
  126. m_switchOptBuildBail = false;
  127. // asm.js switch is always integer
  128. m_switchIntDynProfile = true;
  129. AssertMsg(!m_switchStrDynProfile, "String profiling should not be enabled for an asm.js switch statement");
  130. }
  131. }
  132. ///----------------------------------------------------------------------------
  133. ///
  134. /// SwitchIRBuilder::EndSwitch
  135. ///
  136. /// Notifies the switch builder the switch being generated has been completed
  137. ///----------------------------------------------------------------------------
  138. void
  139. SwitchIRBuilder::EndSwitch(uint32 offset, uint32 targetOffset)
  140. {
  141. FlushCases(targetOffset);
  142. AssertMsg(m_caseNodes->Count() == 0, "Not all switch case nodes built by end of switch");
  143. // only generate the final unconditional jump at the end of the switch
  144. IR::BranchInstr * branch = IR::BranchInstr::New(Js::OpCode::Br, nullptr, m_func);
  145. m_adapter->AddBranchInstr(branch, offset, targetOffset, true);
  146. m_profiledSwitchInstr = nullptr;
  147. }
  148. ///----------------------------------------------------------------------------
  149. ///
  150. /// SwitchIRBuilder::SetProfiledInstruction
  151. ///
  152. /// Sets the profiled switch instruction for the switch statement that
  153. /// is being built
  154. ///----------------------------------------------------------------------------
  155. void
  156. SwitchIRBuilder::SetProfiledInstruction(IR::Instr * instr, Js::ProfileId profileId)
  157. {
  158. m_profiledSwitchInstr = instr;
  159. m_switchOptBuildBail = true;
  160. //don't optimize if the switch expression is not an Integer (obtained via dynamic profiling data of the BeginSwitch opcode)
  161. bool hasProfile = m_profiledSwitchInstr->IsProfiledInstr() && m_profiledSwitchInstr->m_func->HasProfileInfo();
  162. if (hasProfile)
  163. {
  164. const ValueType valueType(m_profiledSwitchInstr->m_func->GetProfileInfo()->GetSwitchProfileInfo(m_profiledSwitchInstr->m_func->GetJnFunction(), profileId));
  165. instr->AsProfiledInstr()->u.FldInfo().valueType = valueType;
  166. m_switchIntDynProfile = valueType.IsLikelyTaggedInt();
  167. m_switchStrDynProfile = valueType.IsLikelyString();
  168. if (PHASE_TESTTRACE1(Js::SwitchOptPhase))
  169. {
  170. char valueTypeStr[VALUE_TYPE_MAX_STRING_SIZE];
  171. valueType.ToString(valueTypeStr);
  172. #if ENABLE_DEBUG_CONFIG_OPTIONS
  173. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  174. #endif
  175. PHASE_PRINT_TESTTRACE1(Js::SwitchOptPhase, L"Func %s, Switch %d: Expression Type : %S\n",
  176. m_profiledSwitchInstr->m_func->GetJnFunction()->GetDebugNumberSet(debugStringBuffer),
  177. m_profiledSwitchInstr->AsProfiledInstr()->u.profileId, valueTypeStr);
  178. }
  179. }
  180. }
  181. ///----------------------------------------------------------------------------
  182. ///
  183. /// SwitchIRBuilder::OnCase
  184. ///
  185. /// Handles a case instruction, generating the appropriate branches, or
  186. /// storing the case to generate a more optimized branch later
  187. ///
  188. ///----------------------------------------------------------------------------
  189. void
  190. SwitchIRBuilder::OnCase(IR::RegOpnd * src1Opnd, IR::RegOpnd * src2Opnd, uint32 offset, uint32 targetOffset)
  191. {
  192. IR::BranchInstr * branchInstr;
  193. if (src2Opnd->m_sym->m_isIntConst && m_intConstSwitchCases->TestAndSet(src2Opnd->m_sym->GetIntConstValue()))
  194. {
  195. // We've already seen a case statement with the same int const value. No need to emit anything for this.
  196. return;
  197. }
  198. if (src2Opnd->m_sym->m_isStrConst && TestAndAddStringCaseConst(Js::JavascriptString::FromVar(src2Opnd->GetStackSym()->GetConstAddress())))
  199. {
  200. // We've already seen a case statement with the same string const value. No need to emit anything for this.
  201. return;
  202. }
  203. branchInstr = IR::BranchInstr::New(m_eqOp, nullptr, src1Opnd, src2Opnd, m_func);
  204. branchInstr->m_isSwitchBr = true;
  205. /*
  206. // Switch optimization
  207. // For Integers - Binary Search or jump table optimization technique is used
  208. // For Strings - Dictionary look up technique is used.
  209. //
  210. // For optimizing, the Load instruction corresponding to the switch instruction is profiled in the interpreter.
  211. // Based on the dynamic profile data, optimization technique is decided.
  212. */
  213. bool deferred = false;
  214. if (GlobOpt::IsSwitchOptEnabled(m_func->GetTopFunc()))
  215. {
  216. if (m_switchIntDynProfile && src2Opnd->m_sym->IsIntConst())
  217. {
  218. CaseNode* caseNode = JitAnew(m_tempAlloc, CaseNode, branchInstr, offset, targetOffset, src2Opnd);
  219. m_caseNodes->Add(caseNode);
  220. deferred = true;
  221. }
  222. else if (m_switchStrDynProfile && src2Opnd->m_sym->m_isStrConst)
  223. {
  224. CaseNode* caseNode = JitAnew(m_tempAlloc, CaseNode, branchInstr, offset, targetOffset, src2Opnd);
  225. m_caseNodes->Add(caseNode);
  226. m_seenOnlySingleCharStrCaseNodes = m_seenOnlySingleCharStrCaseNodes && caseNode->GetSrc2StringConst()->GetLength() == 1;
  227. deferred = true;
  228. }
  229. }
  230. if (!deferred)
  231. {
  232. FlushCases(offset);
  233. m_adapter->AddBranchInstr(branchInstr, offset, targetOffset);
  234. }
  235. }
  236. ///----------------------------------------------------------------------------
  237. ///
  238. /// SwitchIRBuilder::FlushCases
  239. ///
  240. /// Called when a scenario for which optimized switch cases cannot be
  241. /// generated occurs, and generates optimized branches for all cases that
  242. /// have been stored up to this point
  243. ///
  244. ///----------------------------------------------------------------------------
  245. void
  246. SwitchIRBuilder::FlushCases(uint32 targetOffset)
  247. {
  248. if (m_caseNodes->Empty())
  249. {
  250. return;
  251. }
  252. if (m_switchIntDynProfile)
  253. {
  254. BuildCaseBrInstr(targetOffset);
  255. }
  256. else if (m_switchStrDynProfile)
  257. {
  258. BuildMultiBrCaseInstrForStrings(targetOffset);
  259. }
  260. else
  261. {
  262. Assert(false);
  263. }
  264. }
  265. ///----------------------------------------------------------------------------
  266. ///
  267. /// SwitchIRBuilder::RefineCaseNodes
  268. ///
  269. /// Filter IR instructions for case statements that contain no case blocks.
  270. /// Also sets upper bound and lower bound for case instructions that has a
  271. /// consecutive set of cases with just one case block.
  272. ///----------------------------------------------------------------------------
  273. void
  274. SwitchIRBuilder::RefineCaseNodes()
  275. {
  276. m_caseNodes->Sort();
  277. CaseNodeList * tmpCaseNodes = CaseNodeList::New(m_tempAlloc);
  278. for (int currCaseIndex = 1; currCaseIndex < m_caseNodes->Count(); currCaseIndex++)
  279. {
  280. CaseNode * prevCaseNode = m_caseNodes->Item(currCaseIndex - 1);
  281. CaseNode * currCaseNode = m_caseNodes->Item(currCaseIndex);
  282. uint32 prevCaseTargetOffset = prevCaseNode->GetTargetOffset();
  283. uint32 currCaseTargetOffset = currCaseNode->GetTargetOffset();
  284. int prevCaseConstValue = prevCaseNode->GetSrc2IntConst();
  285. int currCaseConstValue = currCaseNode->GetSrc2IntConst();
  286. /*To handle empty case statements with/without repetition*/
  287. if (prevCaseTargetOffset == currCaseTargetOffset &&
  288. (prevCaseConstValue + 1 == currCaseConstValue || prevCaseConstValue == currCaseConstValue))
  289. {
  290. m_caseNodes->Item(currCaseIndex)->SetLowerBound(prevCaseNode->GetLowerBound());
  291. }
  292. else
  293. {
  294. if (tmpCaseNodes->Count() != 0)
  295. {
  296. int lastTmpCaseConstValue = tmpCaseNodes->Item(tmpCaseNodes->Count() - 1)->GetSrc2IntConst();
  297. /*To handle duplicate non empty case statements*/
  298. if (lastTmpCaseConstValue != prevCaseConstValue)
  299. {
  300. tmpCaseNodes->Add(prevCaseNode);
  301. }
  302. }
  303. else
  304. {
  305. tmpCaseNodes->Add(prevCaseNode); //Adding for the first time in tmpCaseNodes
  306. }
  307. }
  308. }
  309. //Adds the last caseNode in the caseNodes list.
  310. tmpCaseNodes->Add(m_caseNodes->Item(m_caseNodes->Count() - 1));
  311. m_caseNodes = tmpCaseNodes;
  312. }
  313. ///--------------------------------------------------------------------------------------
  314. ///
  315. /// SwitchIRBuilder::BuildBinaryTraverseInstr
  316. ///
  317. /// Build IR instructions for case statements in a binary search traversal fashion.
  318. /// defaultLeafBranch: offset of the next instruction to be branched after
  319. /// the set of case instructions under investigation
  320. ///--------------------------------------------------------------------------------------
  321. void
  322. SwitchIRBuilder::BuildBinaryTraverseInstr(int start, int end, uint32 defaultLeafBranch)
  323. {
  324. int mid;
  325. if (start > end)
  326. {
  327. return;
  328. }
  329. if (end - start <= CONFIG_FLAG(MaxLinearIntCaseCount) - 1) // -1 for handling zero index as the base
  330. {
  331. //if only 3 elements, then do linear search on the elements
  332. BuildLinearTraverseInstr(start, end, defaultLeafBranch);
  333. return;
  334. }
  335. mid = start + ((end - start + 1) / 2);
  336. CaseNode* midNode = m_caseNodes->Item(mid);
  337. CaseNode* startNode = m_caseNodes->Item(start);
  338. // if the value that we are switching on is greater than the start case value
  339. // then we branch right to the right half of the binary search
  340. IR::BranchInstr* caseInstr = startNode->GetCaseInstr();
  341. IR::BranchInstr* branchInstr = IR::BranchInstr::New(m_geOp, nullptr, caseInstr->GetSrc1(), midNode->GetLowerBound(), m_func);
  342. branchInstr->m_isSwitchBr = true;
  343. m_adapter->AddBranchInstr(branchInstr, startNode->GetOffset(), midNode->GetOffset(), true);
  344. BuildBinaryTraverseInstr(start, mid - 1, defaultLeafBranch);
  345. BuildBinaryTraverseInstr(mid, end, defaultLeafBranch);
  346. }
  347. ///------------------------------------------------------------------------------------------
  348. ///
  349. /// SwitchIRBuilder::BuildEmptyCasesInstr
  350. ///
  351. /// Build IR instructions for Empty consecutive case statements (with only one case block).
  352. /// defaultLeafBranch: offset of the next instruction to be branched after
  353. /// the set of case instructions under investigation
  354. ///
  355. ///------------------------------------------------------------------------------------------
  356. void
  357. SwitchIRBuilder::BuildEmptyCasesInstr(CaseNode* caseNode, uint32 fallThrOffset)
  358. {
  359. IR::BranchInstr* branchInstr;
  360. IR::Opnd* src1Opnd;
  361. src1Opnd = caseNode->GetCaseInstr()->GetSrc1();
  362. AssertMsg(caseNode->GetLowerBound() != caseNode->GetUpperBound(), "The upper bound and lower bound should not be the same");
  363. //Generate <lb instruction
  364. branchInstr = IR::BranchInstr::New(m_ltOp, nullptr, src1Opnd, caseNode->GetLowerBound(), m_func);
  365. branchInstr->m_isSwitchBr = true;
  366. m_adapter->AddBranchInstr(branchInstr, caseNode->GetOffset(), fallThrOffset, true);
  367. //Generate <=ub instruction
  368. branchInstr = IR::BranchInstr::New(m_leOp, nullptr, src1Opnd, caseNode->GetUpperBound(), m_func);
  369. branchInstr->m_isSwitchBr = true;
  370. m_adapter->AddBranchInstr(branchInstr, caseNode->GetOffset(), caseNode->GetTargetOffset(), true);
  371. BuildBailOnNotInteger();
  372. }
  373. ///----------------------------------------------------------------------------
  374. ///
  375. /// SwitchIRBuilder::BuildLinearTraverseInstr
  376. ///
  377. /// Build IR instr for case statements less than a threshold.
  378. /// defaultLeafBranch: offset of the next instruction to be branched after
  379. /// the set of case instructions under investigation
  380. ///
  381. ///----------------------------------------------------------------------------
  382. void
  383. SwitchIRBuilder::BuildLinearTraverseInstr(int start, int end, uint fallThrOffset)
  384. {
  385. Assert(fallThrOffset);
  386. for (int index = start; index <= end; index++)
  387. {
  388. CaseNode* currCaseNode = m_caseNodes->Item(index);
  389. bool dontBuildEmptyCases = false;
  390. if (currCaseNode->IsSrc2IntConst())
  391. {
  392. int lowerBoundCaseConstValue = currCaseNode->GetLowerBound()->GetStackSym()->GetIntConstValue();
  393. int upperBoundCaseConstValue = currCaseNode->GetUpperBound()->GetStackSym()->GetIntConstValue();
  394. if (lowerBoundCaseConstValue == upperBoundCaseConstValue)
  395. {
  396. dontBuildEmptyCases = true;
  397. }
  398. }
  399. else if (currCaseNode->IsSrc2StrConst())
  400. {
  401. dontBuildEmptyCases = true;
  402. }
  403. else
  404. {
  405. AssertMsg(false, "An integer/String CaseNode is required for BuildLinearTraverseInstr");
  406. }
  407. if (dontBuildEmptyCases)
  408. {
  409. // only if the instruction is not part of a cluster of empty consecutive case statements.
  410. m_adapter->AddBranchInstr(currCaseNode->GetCaseInstr(), currCaseNode->GetOffset(), currCaseNode->GetTargetOffset());
  411. }
  412. else
  413. {
  414. BuildEmptyCasesInstr(currCaseNode, fallThrOffset);
  415. }
  416. }
  417. // Adds an unconditional branch instruction at the end
  418. IR::BranchInstr* branchInstr = IR::BranchInstr::New(Js::OpCode::Br, nullptr, m_func);
  419. branchInstr->m_isSwitchBr = true;
  420. m_adapter->AddBranchInstr(branchInstr, Js::Constants::NoByteCodeOffset, fallThrOffset, true);
  421. }
  422. ///----------------------------------------------------------------------------
  423. ///
  424. /// SwitchIRBuilder::ResetCaseNodes
  425. ///
  426. /// Resets SwitchIRBuilder to begin building another switch statement.
  427. ///
  428. ///----------------------------------------------------------------------------
  429. void
  430. SwitchIRBuilder::ResetCaseNodes()
  431. {
  432. m_caseNodes->Clear();
  433. m_seenOnlySingleCharStrCaseNodes = true;
  434. }
  435. ////////////////////////////////////////////////////////////////////////////////////////////
  436. ///
  437. ///SwitchIRBuilder::BuildCaseBrInstr
  438. /// Generates the branch instructions to optimize the switch case execution flow
  439. /// -Sorts, Refines and generates instructions in binary traversal fashion
  440. ////////////////////////////////////////////////////////////////////////////////////////////
  441. void
  442. SwitchIRBuilder::BuildCaseBrInstr(uint32 targetOffset)
  443. {
  444. Assert(m_isAsmJs || m_profiledSwitchInstr);
  445. int start = 0;
  446. int end = m_caseNodes->Count() - 1;
  447. if (m_caseNodes->Count() <= CONFIG_FLAG(MaxLinearIntCaseCount))
  448. {
  449. BuildLinearTraverseInstr(start, end, targetOffset);
  450. ResetCaseNodes();
  451. return;
  452. }
  453. RefineCaseNodes();
  454. BuildOptimizedIntegerCaseInstrs(targetOffset);
  455. ResetCaseNodes(); // clear the list for the next new set of integers - or for a new switch case statement
  456. //optimization is definitely performed when the number of cases is greater than the threshold
  457. if (end - start > CONFIG_FLAG(MaxLinearIntCaseCount) - 1) // -1 for handling zero index as the base
  458. {
  459. BuildBailOnNotInteger();
  460. }
  461. }
  462. ////////////////////////////////////////////////////////////////////////////////////////////
  463. ///
  464. ///SwitchIRBuilder::BuildOptimizedIntegerCaseInstrs
  465. /// Identify chunks of integers cases(consecutive integers)
  466. /// Apply jump table or binary traversal based on the density of the case arms
  467. ///
  468. ////////////////////////////////////////////////////////////////////////////////////////////
  469. void
  470. SwitchIRBuilder::BuildOptimizedIntegerCaseInstrs(uint32 targetOffset)
  471. {
  472. int startjmpTableIndex = 0;
  473. int endjmpTableIndex = 0;
  474. int startBinaryTravIndex = 0;
  475. int endBinaryTravIndex = 0;
  476. IR::MultiBranchInstr * multiBranchInstr = nullptr;
  477. /*
  478. * Algorithm to find chunks of consecutive integers in a given set of case arms(sorted)
  479. * -Start and end indices for jump table and binary tree are maintained.
  480. * -The corresponding start and end indices indicate that they are suitable candidate for their respective category(binaryTree/jumpTable)
  481. * -All holes are filled with an offset corresponding to the default fallthrough instruction and each block is filled with an offset corresponding to the start of the next block
  482. * A Block here refers either to a jump table or to a binary tree.
  483. * -Blocks of BinaryTrav/Jump table are traversed in a linear fashion.
  484. **/
  485. for (int currentIndex = 0; currentIndex < m_caseNodes->Count() - 1; currentIndex++)
  486. {
  487. int nextIndex = currentIndex + 1;
  488. //Check if there is no missing value between subsequent case arms
  489. if (m_caseNodes->Item(currentIndex)->GetSrc2IntConst() + 1 != m_caseNodes->Item(nextIndex)->GetSrc2IntConst())
  490. {
  491. //value of the case nodes are guaranteed to be 32 bits or less than 32bits at this point(if it is more, the Switch Opt will not kick in)
  492. Assert(nextIndex == endjmpTableIndex + 1);
  493. int64 speculatedEndJmpCaseValue = m_caseNodes->Item(nextIndex)->GetSrc2IntConst();
  494. int64 endJmpCaseValue = m_caseNodes->Item(endjmpTableIndex)->GetSrc2IntConst();
  495. int64 startJmpCaseValue = m_caseNodes->Item(startjmpTableIndex)->GetSrc2IntConst();
  496. int64 speculatedJmpTableSize = speculatedEndJmpCaseValue - startJmpCaseValue + 1;
  497. int64 jmpTableSize = endJmpCaseValue - startJmpCaseValue + 1;
  498. int numFilledEntries = nextIndex - startjmpTableIndex + 1;
  499. //Checks if the % of filled entries(unique targets from the case arms) in the jump table is within the threshold
  500. if (speculatedJmpTableSize != 0 && ((numFilledEntries)* 100 / speculatedJmpTableSize) < (100 - CONFIG_FLAG(SwitchOptHolesThreshold)))
  501. {
  502. if (jmpTableSize >= CONFIG_FLAG(MinSwitchJumpTableSize))
  503. {
  504. uint32 fallThrOffset = m_caseNodes->Item(endjmpTableIndex)->GetOffset();
  505. TryBuildBinaryTreeOrMultiBrForSwitchInts(multiBranchInstr, fallThrOffset, startjmpTableIndex, endjmpTableIndex, startBinaryTravIndex, targetOffset);
  506. //Reset start/end indices of BinaryTrav to the next index.
  507. startBinaryTravIndex = nextIndex;
  508. endBinaryTravIndex = nextIndex;
  509. }
  510. //Reset start/end indices of the jump table to the next index.
  511. startjmpTableIndex = nextIndex;
  512. endjmpTableIndex = nextIndex;
  513. }
  514. else
  515. {
  516. endjmpTableIndex++;
  517. }
  518. }
  519. else
  520. {
  521. endjmpTableIndex++;
  522. }
  523. }
  524. int64 endJmpCaseValue = m_caseNodes->Item(endjmpTableIndex)->GetSrc2IntConst();
  525. int64 startJmpCaseValue = m_caseNodes->Item(startjmpTableIndex)->GetSrc2IntConst();
  526. int64 jmpTableSize = endJmpCaseValue - startJmpCaseValue + 1;
  527. if (jmpTableSize < CONFIG_FLAG(MinSwitchJumpTableSize))
  528. {
  529. endBinaryTravIndex = endjmpTableIndex;
  530. BuildBinaryTraverseInstr(startBinaryTravIndex, endBinaryTravIndex, targetOffset);
  531. if (multiBranchInstr)
  532. {
  533. FixUpMultiBrJumpTable(multiBranchInstr, multiBranchInstr->GetNextRealInstr()->GetByteCodeOffset());
  534. multiBranchInstr = nullptr;
  535. }
  536. }
  537. else
  538. {
  539. uint32 fallthrOffset = m_caseNodes->Item(endjmpTableIndex)->GetOffset();
  540. TryBuildBinaryTreeOrMultiBrForSwitchInts(multiBranchInstr, fallthrOffset, startjmpTableIndex, endjmpTableIndex, startBinaryTravIndex, targetOffset);
  541. FixUpMultiBrJumpTable(multiBranchInstr, targetOffset);
  542. }
  543. }
  544. ////////////////////////////////////////////////////////////////////////////////////////////
  545. ///
  546. ///SwitchIRBuilder::TryBuildBinaryTreeOrMultiBrForSwitchInts
  547. /// Builds a range of integer cases into either a binary tree or jump table.
  548. ///
  549. ////////////////////////////////////////////////////////////////////////////////////////////
  550. void
  551. SwitchIRBuilder::TryBuildBinaryTreeOrMultiBrForSwitchInts(IR::MultiBranchInstr * &multiBranchInstr, uint32 fallthrOffset, int startjmpTableIndex, int endjmpTableIndex, int startBinaryTravIndex, uint32 defaultTargetOffset)
  552. {
  553. int endBinaryTravIndex = startjmpTableIndex;
  554. //Try Building Binary tree, if there are available case arms, as indicated by the boundary offsets
  555. if (endBinaryTravIndex != startBinaryTravIndex)
  556. {
  557. endBinaryTravIndex = startjmpTableIndex - 1;
  558. BuildBinaryTraverseInstr(startBinaryTravIndex, endBinaryTravIndex, fallthrOffset);
  559. //Fix up the fallthrOffset for the previous multiBrInstr, if one existed
  560. //Example => Binary tree immediately succeeds a MultiBr Instr
  561. if (multiBranchInstr)
  562. {
  563. FixUpMultiBrJumpTable(multiBranchInstr, multiBranchInstr->GetNextRealInstr()->GetByteCodeOffset());
  564. multiBranchInstr = nullptr;
  565. }
  566. }
  567. //Fix up the fallthrOffset for the previous multiBrInstr, if one existed
  568. //Example -> A multiBr can be followed by another multiBr
  569. if (multiBranchInstr)
  570. {
  571. FixUpMultiBrJumpTable(multiBranchInstr, fallthrOffset);
  572. multiBranchInstr = nullptr;
  573. }
  574. multiBranchInstr = BuildMultiBrCaseInstrForInts(startjmpTableIndex, endjmpTableIndex, defaultTargetOffset);
  575. //We currently assign the offset of the multiBr Instr same as the offset of the last instruction of the case arm selected for building the jump table
  576. //AssertMsg(m_lastInstr->GetByteCodeOffset() == fallthrOffset, "The fallthrough offset to the multi branch instruction is wrong");
  577. }
  578. ////////////////////////////////////////////////////////////////////////////////////////////
  579. ///
  580. ///SwitchIRBuilder::FixUpMultiBrJumpTable
  581. /// Creates Reloc Records for the branch instructions that are generated with the MultiBr Instr
  582. /// Also calls FixMultiBrDefaultTarget to fix the target offset in the MultiBr Instr
  583. ////////////////////////////////////////////////////////////////////////////////////////////
  584. void
  585. SwitchIRBuilder::FixUpMultiBrJumpTable(IR::MultiBranchInstr * multiBranchInstr, uint32 targetOffset)
  586. {
  587. multiBranchInstr->FixMultiBrDefaultTarget(targetOffset);
  588. uint32 offset = multiBranchInstr->GetByteCodeOffset();
  589. IR::Instr * subInstr = multiBranchInstr->GetPrevRealInstr();
  590. IR::Instr * upperBoundCheckInstr = subInstr->GetPrevRealInstr();
  591. IR::Instr * lowerBoundCheckInstr = upperBoundCheckInstr->GetPrevRealInstr();
  592. AssertMsg(subInstr->m_opcode == m_subOp, "Missing Offset Calculation instruction");
  593. AssertMsg(upperBoundCheckInstr->IsBranchInstr() && lowerBoundCheckInstr->IsBranchInstr(), "Invalid boundary check instructions");
  594. AssertMsg(upperBoundCheckInstr->m_opcode == m_gtOp && lowerBoundCheckInstr->m_opcode == m_ltOp, "Invalid boundary check instructions");
  595. m_adapter->CreateRelocRecord(upperBoundCheckInstr->AsBranchInstr(), offset, targetOffset, true);
  596. m_adapter->CreateRelocRecord(lowerBoundCheckInstr->AsBranchInstr(), offset, targetOffset, true);
  597. }
  598. ////////////////////////////////////////////////////////////////////////////////////////////
  599. ///
  600. ///SwitchIRBuilder::BuildBailOnNotInteger
  601. /// Creates the necessary bailout for a switch case that expected an integer expression
  602. /// but was not.
  603. ///
  604. ////////////////////////////////////////////////////////////////////////////////////////////
  605. void
  606. SwitchIRBuilder::BuildBailOnNotInteger()
  607. {
  608. if (!m_switchOptBuildBail)
  609. {
  610. return;
  611. }
  612. m_adapter->ConvertToBailOut(m_profiledSwitchInstr, IR::BailOutExpectingInteger);
  613. m_switchOptBuildBail = false; // falsify this to avoid generating extra BailOuts when optimization is done again on the same switch statement
  614. #if ENABLE_DEBUG_CONFIG_OPTIONS
  615. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  616. #endif
  617. PHASE_PRINT_TESTTRACE1(Js::SwitchOptPhase, L"Func %s, Switch %d:Optimized for Integers\n",
  618. m_profiledSwitchInstr->m_func->GetJnFunction()->GetDebugNumberSet(debugStringBuffer),
  619. m_profiledSwitchInstr->AsProfiledInstr()->u.profileId);
  620. }
  621. ////////////////////////////////////////////////////////////////////////////////////////////
  622. ///
  623. ///SwitchIRBuilder::BuildBailOnNotString
  624. /// Creates the necessary bailout for a switch case that expected an string expression
  625. /// but was not.
  626. ///
  627. ////////////////////////////////////////////////////////////////////////////////////////////
  628. void
  629. SwitchIRBuilder::BuildBailOnNotString()
  630. {
  631. if (!m_switchOptBuildBail)
  632. {
  633. return;
  634. }
  635. m_adapter->ConvertToBailOut(m_profiledSwitchInstr, IR::BailOutExpectingString);
  636. m_switchOptBuildBail = false; // falsify this to avoid generating extra BailOuts when optimization is done again on the same switch statement
  637. #if ENABLE_DEBUG_CONFIG_OPTIONS
  638. wchar_t debugStringBuffer[MAX_FUNCTION_BODY_DEBUG_STRING_SIZE];
  639. #endif
  640. PHASE_PRINT_TESTTRACE1(Js::SwitchOptPhase, L"Func %s, Switch %d:Optimized for Strings\n",
  641. m_profiledSwitchInstr->m_func->GetJnFunction()->GetDebugNumberSet(debugStringBuffer),
  642. m_profiledSwitchInstr->AsProfiledInstr()->u.profileId);
  643. }
  644. ///----------------------------------------------------------------------------
  645. ///
  646. /// SwitchIRBuilder::TestAndAddStringCaseConst
  647. ///
  648. /// Checks if strConstSwitchCases already has the string constant
  649. /// - if yes, then return true
  650. /// - if no, then add the string to the list 'strConstSwitchCases' and return false
  651. ///
  652. ///----------------------------------------------------------------------------
  653. bool
  654. SwitchIRBuilder::TestAndAddStringCaseConst(Js::JavascriptString * str)
  655. {
  656. Assert(m_strConstSwitchCases);
  657. if (m_strConstSwitchCases->Contains(str))
  658. {
  659. return true;
  660. }
  661. else
  662. {
  663. m_strConstSwitchCases->Add(str);
  664. return false;
  665. }
  666. }
  667. ///----------------------------------------------------------------------------
  668. ///
  669. /// SwitchIRBuilder::BuildMultiBrCaseInstrForStrings
  670. ///
  671. /// Build Multi Branch IR instr for a set of Case statements(String case arms).
  672. /// - Builds the multibranch target and adds the instruction
  673. ///
  674. ///----------------------------------------------------------------------------
  675. void
  676. SwitchIRBuilder::BuildMultiBrCaseInstrForStrings(uint32 targetOffset)
  677. {
  678. Assert(m_caseNodes && m_caseNodes->Count() && m_profiledSwitchInstr && !m_isAsmJs);
  679. if (m_caseNodes->Count() < CONFIG_FLAG(MaxLinearStringCaseCount))
  680. {
  681. int start = 0;
  682. int end = m_caseNodes->Count() - 1;
  683. BuildLinearTraverseInstr(start, end, targetOffset);
  684. ResetCaseNodes();
  685. return;
  686. }
  687. IR::Opnd * srcOpnd = m_caseNodes->Item(0)->GetCaseInstr()->GetSrc1(); // Src1 is same in all the caseNodes
  688. IR::MultiBranchInstr * multiBranchInstr = IR::MultiBranchInstr::New(Js::OpCode::MultiBr, srcOpnd, m_func);
  689. uint32 lastCaseOffset = m_caseNodes->Item(m_caseNodes->Count() - 1)->GetOffset();
  690. uint caseCount = m_caseNodes->Count();
  691. bool generateDictionary = true;
  692. wchar_t minChar = USHORT_MAX;
  693. wchar_t maxChar = 0;
  694. // Either the jump table is within the limit (<= 128) or it is dense (<= 2 * case Count)
  695. uint const maxJumpTableSize = max<uint>(CONFIG_FLAG(MaxSingleCharStrJumpTableSize), CONFIG_FLAG(MaxSingleCharStrJumpTableRatio) * caseCount);
  696. if (this->m_seenOnlySingleCharStrCaseNodes)
  697. {
  698. generateDictionary = false;
  699. for (uint i = 0; i < caseCount; i++)
  700. {
  701. Js::JavascriptString * str = m_caseNodes->Item(i)->GetSrc2StringConst();
  702. Assert(str->GetLength() == 1);
  703. wchar_t currChar = str->GetString()[0];
  704. minChar = min(minChar, currChar);
  705. maxChar = max(maxChar, currChar);
  706. if ((uint)(maxChar - minChar) > maxJumpTableSize)
  707. {
  708. generateDictionary = true;
  709. break;
  710. }
  711. }
  712. }
  713. if (generateDictionary)
  714. {
  715. multiBranchInstr->CreateBranchTargetsAndSetDefaultTarget(caseCount, IR::MultiBranchInstr::StrDictionary, targetOffset);
  716. //Adding normal cases to the instruction (except the default case, which we do it later)
  717. for (uint i = 0; i < caseCount; i++)
  718. {
  719. Js::JavascriptString * str = m_caseNodes->Item(i)->GetSrc2StringConst();
  720. uint32 caseTargetOffset = m_caseNodes->Item(i)->GetTargetOffset();
  721. multiBranchInstr->AddtoDictionary(caseTargetOffset, str);
  722. }
  723. }
  724. else
  725. {
  726. // If we are only going to save 16 entries, just start from 0 so we don't have to subtract
  727. if (minChar < 16)
  728. {
  729. minChar = 0;
  730. }
  731. multiBranchInstr->m_baseCaseValue = minChar;
  732. multiBranchInstr->m_lastCaseValue = maxChar;
  733. uint jumpTableSize = maxChar - minChar + 1;
  734. multiBranchInstr->CreateBranchTargetsAndSetDefaultTarget(jumpTableSize, IR::MultiBranchInstr::SingleCharStrJumpTable, targetOffset);
  735. for (uint i = 0; i < jumpTableSize; i++)
  736. {
  737. // Initialize all the entries to the default target first.
  738. multiBranchInstr->AddtoJumpTable(targetOffset, i);
  739. }
  740. //Adding normal cases to the instruction (except the default case, which we do it later)
  741. for (uint i = 0; i < caseCount; i++)
  742. {
  743. Js::JavascriptString * str = m_caseNodes->Item(i)->GetSrc2StringConst();
  744. Assert(str->GetLength() == 1);
  745. uint32 caseTargetOffset = m_caseNodes->Item(i)->GetTargetOffset();
  746. multiBranchInstr->AddtoJumpTable(caseTargetOffset, str->GetString()[0] - minChar);
  747. }
  748. }
  749. multiBranchInstr->m_isSwitchBr = true;
  750. m_adapter->CreateRelocRecord(multiBranchInstr, lastCaseOffset, targetOffset);
  751. m_adapter->AddInstr(multiBranchInstr, lastCaseOffset);
  752. BuildBailOnNotString();
  753. ResetCaseNodes();
  754. }
  755. ///----------------------------------------------------------------------------
  756. ///
  757. /// SwitchIRBuilder::BuildMultiBrCaseInstrForInts
  758. ///
  759. /// Build Multi Branch IR instr for a set of Case statements(Integer case arms).
  760. /// - Builds the multibranch target and adds the instruction
  761. /// - Add boundary checks for the jump table and calculates the offset in the jump table
  762. ///
  763. ///----------------------------------------------------------------------------
  764. IR::MultiBranchInstr *
  765. SwitchIRBuilder::BuildMultiBrCaseInstrForInts(uint32 start, uint32 end, uint32 targetOffset)
  766. {
  767. Assert(m_caseNodes && m_caseNodes->Count() && (m_profiledSwitchInstr || m_isAsmJs));
  768. IR::Opnd * srcOpnd = m_caseNodes->Item(start)->GetCaseInstr()->GetSrc1(); // Src1 is same in all the caseNodes
  769. IR::MultiBranchInstr * multiBranchInstr = IR::MultiBranchInstr::New(Js::OpCode::MultiBr, srcOpnd, m_func);
  770. uint32 lastCaseOffset = m_caseNodes->Item(end)->GetOffset();
  771. int32 baseCaseValue = m_caseNodes->Item(start)->GetLowerBound()->GetStackSym()->GetIntConstValue();
  772. int32 lastCaseValue = m_caseNodes->Item(end)->GetUpperBound()->GetStackSym()->GetIntConstValue();
  773. multiBranchInstr->m_baseCaseValue = baseCaseValue;
  774. multiBranchInstr->m_lastCaseValue = lastCaseValue;
  775. uint32 jmpTableSize = lastCaseValue - baseCaseValue + 1;
  776. multiBranchInstr->CreateBranchTargetsAndSetDefaultTarget(jmpTableSize, IR::MultiBranchInstr::IntJumpTable, targetOffset);
  777. int caseIndex = end;
  778. int lowerBoundCaseConstValue = 0;
  779. int upperBoundCaseConstValue = 0;
  780. uint32 caseTargetOffset = 0;
  781. for (int jmpIndex = jmpTableSize - 1; jmpIndex >= 0; jmpIndex--)
  782. {
  783. if (caseIndex >= 0 && jmpIndex == m_caseNodes->Item(caseIndex)->GetSrc2IntConst() - baseCaseValue)
  784. {
  785. lowerBoundCaseConstValue = m_caseNodes->Item(caseIndex)->GetLowerBound()->GetStackSym()->GetIntConstValue();
  786. upperBoundCaseConstValue = m_caseNodes->Item(caseIndex)->GetUpperBound()->GetStackSym()->GetIntConstValue();
  787. caseTargetOffset = m_caseNodes->Item(caseIndex--)->GetTargetOffset();
  788. multiBranchInstr->AddtoJumpTable(caseTargetOffset, jmpIndex);
  789. }
  790. else
  791. {
  792. if (jmpIndex >= lowerBoundCaseConstValue - baseCaseValue && jmpIndex <= upperBoundCaseConstValue - baseCaseValue)
  793. {
  794. multiBranchInstr->AddtoJumpTable(caseTargetOffset, jmpIndex);
  795. }
  796. else
  797. {
  798. multiBranchInstr->AddtoJumpTable(targetOffset, jmpIndex);
  799. }
  800. }
  801. }
  802. //Insert Boundary checks for the jump table - Reloc records are created later for these instructions (in FixUpMultiBrJumpTable())
  803. IR::BranchInstr* lowerBoundChk = IR::BranchInstr::New(m_ltOp, nullptr, srcOpnd, m_caseNodes->Item(start)->GetLowerBound(), m_func);
  804. lowerBoundChk->m_isSwitchBr = true;
  805. m_adapter->AddInstr(lowerBoundChk, lastCaseOffset);
  806. IR::BranchInstr* upperBoundChk = IR::BranchInstr::New(m_gtOp, nullptr, srcOpnd, m_caseNodes->Item(end)->GetUpperBound(), m_func);
  807. upperBoundChk->m_isSwitchBr = true;
  808. m_adapter->AddInstr(upperBoundChk, lastCaseOffset);
  809. //Calculate the offset inside the jump table using the switch operand value and the lowest case arm value (in the considered set of consecutive integers)
  810. IR::IntConstOpnd *baseCaseValueOpnd = IR::IntConstOpnd::New(multiBranchInstr->m_baseCaseValue, TyInt32, m_func);
  811. IR::RegOpnd * offset = IR::RegOpnd::New(TyVar, m_func);
  812. IR::Instr * subInstr = IR::Instr::New(m_subOp, offset, multiBranchInstr->GetSrc1(), baseCaseValueOpnd, m_func);
  813. //We are sure that the SUB operation will not overflow the int range - It will either bailout or will not optimize if it finds a number that is out of the int range.
  814. subInstr->ignoreIntOverflow = true;
  815. m_adapter->AddInstr(subInstr, lastCaseOffset);
  816. //Source of the multi branch instr will now have the calculated offset
  817. multiBranchInstr->UnlinkSrc1();
  818. multiBranchInstr->SetSrc1(offset);
  819. multiBranchInstr->m_isSwitchBr = true;
  820. m_adapter->AddInstr(multiBranchInstr, lastCaseOffset);
  821. m_adapter->CreateRelocRecord(multiBranchInstr, lastCaseOffset, targetOffset);
  822. return multiBranchInstr;
  823. }