GlobOptIntBounds.cpp 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142
  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. #if ENABLE_DEBUG_CONFIG_OPTIONS && DBG_DUMP
  7. #define TRACE_PHASE_VERBOSE(phase, indent, ...) \
  8. if(PHASE_VERBOSE_TRACE(phase, this->func)) \
  9. { \
  10. for(int i = 0; i < static_cast<int>(indent); ++i) \
  11. { \
  12. Output::Print(_u(" ")); \
  13. } \
  14. Output::Print(__VA_ARGS__); \
  15. Output::Flush(); \
  16. }
  17. #else
  18. #define TRACE_PHASE_VERBOSE(phase, indent, ...)
  19. #endif
  20. void GlobOpt::AddSubConstantInfo::Set(
  21. StackSym *const srcSym,
  22. Value *const srcValue,
  23. const bool srcValueIsLikelyConstant,
  24. const int32 offset)
  25. {
  26. Assert(srcSym);
  27. Assert(!srcSym->IsTypeSpec());
  28. Assert(srcValue);
  29. Assert(srcValue->GetValueInfo()->IsLikelyInt());
  30. this->srcSym = srcSym;
  31. this->srcValue = srcValue;
  32. this->srcValueIsLikelyConstant = srcValueIsLikelyConstant;
  33. this->offset = offset;
  34. }
  35. void GlobOpt::ArrayLowerBoundCheckHoistInfo::SetCompatibleBoundCheck(
  36. BasicBlock *const compatibleBoundCheckBlock,
  37. StackSym *const indexSym,
  38. const int offset,
  39. const ValueNumber indexValueNumber)
  40. {
  41. Assert(!Loop());
  42. Assert(compatibleBoundCheckBlock);
  43. Assert(indexSym);
  44. Assert(!indexSym->IsTypeSpec());
  45. Assert(indexValueNumber != InvalidValueNumber);
  46. this->compatibleBoundCheckBlock = compatibleBoundCheckBlock;
  47. this->indexSym = indexSym;
  48. this->offset = offset;
  49. this->indexValueNumber = indexValueNumber;
  50. }
  51. void GlobOpt::ArrayLowerBoundCheckHoistInfo::SetLoop(
  52. ::Loop *const loop,
  53. const int indexConstantValue,
  54. const bool isLoopCountBasedBound)
  55. {
  56. Assert(!CompatibleBoundCheckBlock());
  57. Assert(loop);
  58. this->loop = loop;
  59. indexSym = nullptr;
  60. offset = 0;
  61. indexValue = nullptr;
  62. indexConstantBounds = IntConstantBounds(indexConstantValue, indexConstantValue);
  63. this->isLoopCountBasedBound = isLoopCountBasedBound;
  64. loopCount = nullptr;
  65. }
  66. void GlobOpt::ArrayLowerBoundCheckHoistInfo::SetLoop(
  67. ::Loop *const loop,
  68. StackSym *const indexSym,
  69. const int offset,
  70. Value *const indexValue,
  71. const IntConstantBounds &indexConstantBounds,
  72. const bool isLoopCountBasedBound)
  73. {
  74. Assert(!CompatibleBoundCheckBlock());
  75. Assert(loop);
  76. Assert(indexSym);
  77. Assert(!indexSym->IsTypeSpec());
  78. Assert(indexValue);
  79. this->loop = loop;
  80. this->indexSym = indexSym;
  81. this->offset = offset;
  82. this->indexValueNumber = indexValue->GetValueNumber();
  83. this->indexValue = indexValue;
  84. this->indexConstantBounds = indexConstantBounds;
  85. this->isLoopCountBasedBound = isLoopCountBasedBound;
  86. loopCount = nullptr;
  87. }
  88. void GlobOpt::ArrayLowerBoundCheckHoistInfo::SetLoopCount(::LoopCount *const loopCount, const int maxMagnitudeChange)
  89. {
  90. Assert(Loop());
  91. Assert(loopCount);
  92. Assert(maxMagnitudeChange != 0);
  93. this->loopCount = loopCount;
  94. this->maxMagnitudeChange = maxMagnitudeChange;
  95. }
  96. void GlobOpt::ArrayUpperBoundCheckHoistInfo::SetCompatibleBoundCheck(
  97. BasicBlock *const compatibleBoundCheckBlock,
  98. const int indexConstantValue)
  99. {
  100. Assert(!Loop());
  101. Assert(compatibleBoundCheckBlock);
  102. this->compatibleBoundCheckBlock = compatibleBoundCheckBlock;
  103. indexSym = nullptr;
  104. offset = -1; // simulate < instead of <=
  105. indexConstantBounds = IntConstantBounds(indexConstantValue, indexConstantValue);
  106. }
  107. void GlobOpt::ArrayUpperBoundCheckHoistInfo::SetLoop(
  108. ::Loop *const loop,
  109. const int indexConstantValue,
  110. Value *const headSegmentLengthValue,
  111. const IntConstantBounds &headSegmentLengthConstantBounds,
  112. const bool isLoopCountBasedBound)
  113. {
  114. Assert(!CompatibleBoundCheckBlock());
  115. Assert(loop);
  116. Assert(headSegmentLengthValue);
  117. SetLoop(loop, indexConstantValue, isLoopCountBasedBound);
  118. offset = -1; // simulate < instead of <=
  119. this->headSegmentLengthValue = headSegmentLengthValue;
  120. this->headSegmentLengthConstantBounds = headSegmentLengthConstantBounds;
  121. }
  122. void GlobOpt::ArrayUpperBoundCheckHoistInfo::SetLoop(
  123. ::Loop *const loop,
  124. StackSym *const indexSym,
  125. const int offset,
  126. Value *const indexValue,
  127. const IntConstantBounds &indexConstantBounds,
  128. Value *const headSegmentLengthValue,
  129. const IntConstantBounds &headSegmentLengthConstantBounds,
  130. const bool isLoopCountBasedBound)
  131. {
  132. Assert(headSegmentLengthValue);
  133. SetLoop(loop, indexSym, offset, indexValue, indexConstantBounds, isLoopCountBasedBound);
  134. this->headSegmentLengthValue = headSegmentLengthValue;
  135. this->headSegmentLengthConstantBounds = headSegmentLengthConstantBounds;
  136. }
  137. void GlobOpt::UpdateIntBoundsForEqualBranch(
  138. Value *const src1Value,
  139. Value *const src2Value,
  140. const int32 src2ConstantValue)
  141. {
  142. Assert(src1Value);
  143. if(!DoPathDependentValues() || (src2Value && src1Value->GetValueNumber() == src2Value->GetValueNumber()))
  144. {
  145. return;
  146. }
  147. #if DBG
  148. if(!IsLoopPrePass() && DoAggressiveIntTypeSpec() && DoConstFold())
  149. {
  150. IntConstantBounds src1ConstantBounds, src2ConstantBounds;
  151. AssertVerify(src1Value->GetValueInfo()->TryGetIntConstantBounds(&src1ConstantBounds, true));
  152. if(src2Value)
  153. {
  154. AssertVerify(src2Value->GetValueInfo()->TryGetIntConstantBounds(&src2ConstantBounds, true));
  155. }
  156. else
  157. {
  158. src2ConstantBounds = IntConstantBounds(src2ConstantValue, src2ConstantValue);
  159. }
  160. Assert(
  161. !ValueInfo::IsEqualTo(
  162. src1Value,
  163. src1ConstantBounds.LowerBound(),
  164. src1ConstantBounds.UpperBound(),
  165. src2Value,
  166. src2ConstantBounds.LowerBound(),
  167. src2ConstantBounds.UpperBound()));
  168. Assert(
  169. !ValueInfo::IsNotEqualTo(
  170. src1Value,
  171. src1ConstantBounds.LowerBound(),
  172. src1ConstantBounds.UpperBound(),
  173. src2Value,
  174. src2ConstantBounds.LowerBound(),
  175. src2ConstantBounds.UpperBound()));
  176. }
  177. #endif
  178. SetPathDependentInfo(
  179. true,
  180. PathDependentInfo(PathDependentRelationship::Equal, src1Value, src2Value, src2ConstantValue));
  181. SetPathDependentInfo(
  182. false,
  183. PathDependentInfo(PathDependentRelationship::NotEqual, src1Value, src2Value, src2ConstantValue));
  184. }
  185. void GlobOpt::UpdateIntBoundsForNotEqualBranch(
  186. Value *const src1Value,
  187. Value *const src2Value,
  188. const int32 src2ConstantValue)
  189. {
  190. Assert(src1Value);
  191. if(!DoPathDependentValues() || (src2Value && src1Value->GetValueNumber() == src2Value->GetValueNumber()))
  192. {
  193. return;
  194. }
  195. #if DBG
  196. if(!IsLoopPrePass() && DoAggressiveIntTypeSpec() && DoConstFold())
  197. {
  198. IntConstantBounds src1ConstantBounds, src2ConstantBounds;
  199. AssertVerify(src1Value->GetValueInfo()->TryGetIntConstantBounds(&src1ConstantBounds, true));
  200. if(src2Value)
  201. {
  202. AssertVerify(src2Value->GetValueInfo()->TryGetIntConstantBounds(&src2ConstantBounds, true));
  203. }
  204. else
  205. {
  206. src2ConstantBounds = IntConstantBounds(src2ConstantValue, src2ConstantValue);
  207. }
  208. Assert(
  209. !ValueInfo::IsEqualTo(
  210. src1Value,
  211. src1ConstantBounds.LowerBound(),
  212. src1ConstantBounds.UpperBound(),
  213. src2Value,
  214. src2ConstantBounds.LowerBound(),
  215. src2ConstantBounds.UpperBound()));
  216. Assert(
  217. !ValueInfo::IsNotEqualTo(
  218. src1Value,
  219. src1ConstantBounds.LowerBound(),
  220. src1ConstantBounds.UpperBound(),
  221. src2Value,
  222. src2ConstantBounds.LowerBound(),
  223. src2ConstantBounds.UpperBound()));
  224. }
  225. #endif
  226. SetPathDependentInfo(
  227. true, PathDependentInfo(PathDependentRelationship::NotEqual, src1Value, src2Value, src2ConstantValue));
  228. SetPathDependentInfo(
  229. false, PathDependentInfo(PathDependentRelationship::Equal, src1Value, src2Value, src2ConstantValue));
  230. }
  231. void GlobOpt::UpdateIntBoundsForGreaterThanOrEqualBranch(Value *const src1Value, Value *const src2Value)
  232. {
  233. Assert(src1Value);
  234. Assert(src2Value);
  235. if(!DoPathDependentValues() || src1Value->GetValueNumber() == src2Value->GetValueNumber())
  236. {
  237. return;
  238. }
  239. #if DBG
  240. if(!IsLoopPrePass() && DoAggressiveIntTypeSpec() && DoConstFold())
  241. {
  242. IntConstantBounds src1ConstantBounds, src2ConstantBounds;
  243. AssertVerify(src1Value->GetValueInfo()->TryGetIntConstantBounds(&src1ConstantBounds, true));
  244. AssertVerify(src2Value->GetValueInfo()->TryGetIntConstantBounds(&src2ConstantBounds, true));
  245. Assert(
  246. !ValueInfo::IsGreaterThanOrEqualTo(
  247. src1Value,
  248. src1ConstantBounds.LowerBound(),
  249. src1ConstantBounds.UpperBound(),
  250. src2Value,
  251. src2ConstantBounds.LowerBound(),
  252. src2ConstantBounds.UpperBound()));
  253. Assert(
  254. !ValueInfo::IsLessThan(
  255. src1Value,
  256. src1ConstantBounds.LowerBound(),
  257. src1ConstantBounds.UpperBound(),
  258. src2Value,
  259. src2ConstantBounds.LowerBound(),
  260. src2ConstantBounds.UpperBound()));
  261. }
  262. #endif
  263. SetPathDependentInfo(true, PathDependentInfo(PathDependentRelationship::GreaterThanOrEqual, src1Value, src2Value));
  264. SetPathDependentInfo(false, PathDependentInfo(PathDependentRelationship::LessThan, src1Value, src2Value));
  265. }
  266. void GlobOpt::UpdateIntBoundsForGreaterThanBranch(Value *const src1Value, Value *const src2Value)
  267. {
  268. return UpdateIntBoundsForLessThanBranch(src2Value, src1Value);
  269. }
  270. void GlobOpt::UpdateIntBoundsForLessThanOrEqualBranch(Value *const src1Value, Value *const src2Value)
  271. {
  272. return UpdateIntBoundsForGreaterThanOrEqualBranch(src2Value, src1Value);
  273. }
  274. void GlobOpt::UpdateIntBoundsForLessThanBranch(Value *const src1Value, Value *const src2Value)
  275. {
  276. Assert(src1Value);
  277. Assert(src2Value);
  278. if(!DoPathDependentValues() || src1Value->GetValueNumber() == src2Value->GetValueNumber())
  279. {
  280. return;
  281. }
  282. #if DBG
  283. if(!IsLoopPrePass() && DoAggressiveIntTypeSpec() && DoConstFold())
  284. {
  285. IntConstantBounds src1ConstantBounds, src2ConstantBounds;
  286. AssertVerify(src1Value->GetValueInfo()->TryGetIntConstantBounds(&src1ConstantBounds, true));
  287. AssertVerify(src2Value->GetValueInfo()->TryGetIntConstantBounds(&src2ConstantBounds, true));
  288. Assert(
  289. !ValueInfo::IsGreaterThanOrEqualTo(
  290. src1Value,
  291. src1ConstantBounds.LowerBound(),
  292. src1ConstantBounds.UpperBound(),
  293. src2Value,
  294. src2ConstantBounds.LowerBound(),
  295. src2ConstantBounds.UpperBound()));
  296. Assert(
  297. !ValueInfo::IsLessThan(
  298. src1Value,
  299. src1ConstantBounds.LowerBound(),
  300. src1ConstantBounds.UpperBound(),
  301. src2Value,
  302. src2ConstantBounds.LowerBound(),
  303. src2ConstantBounds.UpperBound()));
  304. }
  305. #endif
  306. SetPathDependentInfo(true, PathDependentInfo(PathDependentRelationship::LessThan, src1Value, src2Value));
  307. SetPathDependentInfo(false, PathDependentInfo(PathDependentRelationship::GreaterThanOrEqual, src1Value, src2Value));
  308. }
  309. IntBounds *GlobOpt::GetIntBoundsToUpdate(
  310. const ValueInfo *const valueInfo,
  311. const IntConstantBounds &constantBounds,
  312. const bool isSettingNewBound,
  313. const bool isBoundConstant,
  314. const bool isSettingUpperBound,
  315. const bool isExplicit)
  316. {
  317. Assert(valueInfo);
  318. Assert(valueInfo->IsLikelyInt());
  319. if(!DoTrackRelativeIntBounds())
  320. {
  321. return nullptr;
  322. }
  323. if(valueInfo->IsIntBounded())
  324. {
  325. const IntBounds *const bounds = valueInfo->AsIntBounded()->Bounds();
  326. if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
  327. {
  328. return bounds->Clone();
  329. }
  330. }
  331. if(valueInfo->IsInt())
  332. {
  333. if(constantBounds.IsConstant())
  334. {
  335. // Don't start tracking relative bounds for int constant values, just retain existing relative bounds. Will use
  336. // IntConstantValueInfo instead.
  337. return nullptr;
  338. }
  339. if(isBoundConstant)
  340. {
  341. // There are no relative bounds to track
  342. if(!(isSettingUpperBound && isExplicit))
  343. {
  344. // We are not setting a constant upper bound that is established explicitly, will use IntRangeValueInfo instead
  345. return nullptr;
  346. }
  347. }
  348. else if(!isSettingNewBound)
  349. {
  350. // New relative bounds are not being set, will use IntRangeValueInfo instead
  351. return nullptr;
  352. }
  353. return IntBounds::New(constantBounds, false, alloc);
  354. }
  355. return nullptr;
  356. }
  357. ValueInfo *GlobOpt::UpdateIntBoundsForEqual(
  358. Value *const value,
  359. const IntConstantBounds &constantBounds,
  360. Value *const boundValue,
  361. const IntConstantBounds &boundConstantBounds,
  362. const bool isExplicit)
  363. {
  364. Assert(value || constantBounds.IsConstant());
  365. Assert(boundValue || boundConstantBounds.IsConstant());
  366. if(!value)
  367. {
  368. return nullptr;
  369. }
  370. Assert(!boundValue || value->GetValueNumber() != boundValue->GetValueNumber());
  371. ValueInfo *const valueInfo = value->GetValueInfo();
  372. IntBounds *const bounds =
  373. GetIntBoundsToUpdate(valueInfo, constantBounds, true, boundConstantBounds.IsConstant(), true, isExplicit);
  374. if(bounds)
  375. {
  376. if(boundValue)
  377. {
  378. const ValueNumber valueNumber = value->GetValueNumber();
  379. bounds->SetLowerBound(valueNumber, boundValue, isExplicit);
  380. bounds->SetUpperBound(valueNumber, boundValue, isExplicit);
  381. }
  382. else
  383. {
  384. bounds->SetLowerBound(boundConstantBounds.LowerBound());
  385. bounds->SetUpperBound(boundConstantBounds.LowerBound(), isExplicit);
  386. }
  387. if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
  388. {
  389. return NewIntBoundedValueInfo(valueInfo, bounds);
  390. }
  391. bounds->Delete();
  392. }
  393. if(!valueInfo->IsInt())
  394. {
  395. return nullptr;
  396. }
  397. const int32 newMin = max(constantBounds.LowerBound(), boundConstantBounds.LowerBound());
  398. const int32 newMax = min(constantBounds.UpperBound(), boundConstantBounds.UpperBound());
  399. return newMin <= newMax ? NewIntRangeValueInfo(valueInfo, newMin, newMax) : nullptr;
  400. }
  401. ValueInfo *GlobOpt::UpdateIntBoundsForNotEqual(
  402. Value *const value,
  403. const IntConstantBounds &constantBounds,
  404. Value *const boundValue,
  405. const IntConstantBounds &boundConstantBounds,
  406. const bool isExplicit)
  407. {
  408. Assert(value || constantBounds.IsConstant());
  409. Assert(boundValue || boundConstantBounds.IsConstant());
  410. if(!value)
  411. {
  412. return nullptr;
  413. }
  414. Assert(!boundValue || value->GetValueNumber() != boundValue->GetValueNumber());
  415. ValueInfo *const valueInfo = value->GetValueInfo();
  416. IntBounds *const bounds =
  417. GetIntBoundsToUpdate(
  418. valueInfo,
  419. constantBounds,
  420. false,
  421. boundConstantBounds.IsConstant(),
  422. boundConstantBounds.IsConstant() && boundConstantBounds.LowerBound() == constantBounds.UpperBound(),
  423. isExplicit);
  424. if(bounds)
  425. {
  426. if(boundValue
  427. ? bounds->SetIsNot(boundValue, isExplicit)
  428. : bounds->SetIsNot(boundConstantBounds.LowerBound(), isExplicit))
  429. {
  430. if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
  431. {
  432. return NewIntBoundedValueInfo(valueInfo, bounds);
  433. }
  434. }
  435. else
  436. {
  437. bounds->Delete();
  438. return nullptr;
  439. }
  440. bounds->Delete();
  441. }
  442. if(!valueInfo->IsInt() || !boundConstantBounds.IsConstant())
  443. {
  444. return nullptr;
  445. }
  446. const int32 constantBound = boundConstantBounds.LowerBound();
  447. // The value is not equal to a constant, so narrow the range if the constant is equal to the value's lower or upper bound
  448. int32 newMin = constantBounds.LowerBound(), newMax = constantBounds.UpperBound();
  449. if(constantBound == newMin)
  450. {
  451. Assert(newMin <= newMax);
  452. if(newMin == newMax)
  453. {
  454. return nullptr;
  455. }
  456. ++newMin;
  457. }
  458. else if(constantBound == newMax)
  459. {
  460. Assert(newMin <= newMax);
  461. if(newMin == newMax)
  462. {
  463. return nullptr;
  464. }
  465. --newMax;
  466. }
  467. else
  468. {
  469. return nullptr;
  470. }
  471. return NewIntRangeValueInfo(valueInfo, newMin, newMax);
  472. }
  473. ValueInfo *GlobOpt::UpdateIntBoundsForGreaterThanOrEqual(
  474. Value *const value,
  475. const IntConstantBounds &constantBounds,
  476. Value *const boundValue,
  477. const IntConstantBounds &boundConstantBounds,
  478. const bool isExplicit)
  479. {
  480. return UpdateIntBoundsForGreaterThanOrEqual(value, constantBounds, boundValue, boundConstantBounds, 0, isExplicit);
  481. }
  482. ValueInfo *GlobOpt::UpdateIntBoundsForGreaterThanOrEqual(
  483. Value *const value,
  484. const IntConstantBounds &constantBounds,
  485. Value *const boundValue,
  486. const IntConstantBounds &boundConstantBounds,
  487. const int boundOffset,
  488. const bool isExplicit)
  489. {
  490. Assert(value || constantBounds.IsConstant());
  491. Assert(boundValue || boundConstantBounds.IsConstant());
  492. if(!value)
  493. {
  494. return nullptr;
  495. }
  496. Assert(!boundValue || value->GetValueNumber() != boundValue->GetValueNumber());
  497. ValueInfo *const valueInfo = value->GetValueInfo();
  498. IntBounds *const bounds =
  499. GetIntBoundsToUpdate(valueInfo, constantBounds, true, boundConstantBounds.IsConstant(), false, isExplicit);
  500. if(bounds)
  501. {
  502. if(boundValue)
  503. {
  504. bounds->SetLowerBound(value->GetValueNumber(), boundValue, boundOffset, isExplicit);
  505. }
  506. else
  507. {
  508. bounds->SetLowerBound(boundConstantBounds.LowerBound(), boundOffset);
  509. }
  510. if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
  511. {
  512. return NewIntBoundedValueInfo(valueInfo, bounds);
  513. }
  514. bounds->Delete();
  515. }
  516. if(!valueInfo->IsInt())
  517. {
  518. return nullptr;
  519. }
  520. int32 adjustedBoundMin;
  521. if(boundOffset == 0)
  522. {
  523. adjustedBoundMin = boundConstantBounds.LowerBound();
  524. }
  525. else if(boundOffset == 1)
  526. {
  527. if(boundConstantBounds.LowerBound() + 1 <= boundConstantBounds.LowerBound())
  528. {
  529. return nullptr;
  530. }
  531. adjustedBoundMin = boundConstantBounds.LowerBound() + 1;
  532. }
  533. else if(Int32Math::Add(boundConstantBounds.LowerBound(), boundOffset, &adjustedBoundMin))
  534. {
  535. return nullptr;
  536. }
  537. const int32 newMin = max(constantBounds.LowerBound(), adjustedBoundMin);
  538. return
  539. newMin <= constantBounds.UpperBound()
  540. ? NewIntRangeValueInfo(valueInfo, newMin, constantBounds.UpperBound())
  541. : nullptr;
  542. }
  543. ValueInfo *GlobOpt::UpdateIntBoundsForGreaterThan(
  544. Value *const value,
  545. const IntConstantBounds &constantBounds,
  546. Value *const boundValue,
  547. const IntConstantBounds &boundConstantBounds,
  548. const bool isExplicit)
  549. {
  550. return UpdateIntBoundsForGreaterThanOrEqual(value, constantBounds, boundValue, boundConstantBounds, 1, isExplicit);
  551. }
  552. ValueInfo *GlobOpt::UpdateIntBoundsForLessThanOrEqual(
  553. Value *const value,
  554. const IntConstantBounds &constantBounds,
  555. Value *const boundValue,
  556. const IntConstantBounds &boundConstantBounds,
  557. const bool isExplicit)
  558. {
  559. return UpdateIntBoundsForLessThanOrEqual(value, constantBounds, boundValue, boundConstantBounds, 0, isExplicit);
  560. }
  561. ValueInfo *GlobOpt::UpdateIntBoundsForLessThanOrEqual(
  562. Value *const value,
  563. const IntConstantBounds &constantBounds,
  564. Value *const boundValue,
  565. const IntConstantBounds &boundConstantBounds,
  566. const int boundOffset,
  567. const bool isExplicit)
  568. {
  569. Assert(value || constantBounds.IsConstant());
  570. Assert(boundValue || boundConstantBounds.IsConstant());
  571. if(!value)
  572. {
  573. return nullptr;
  574. }
  575. Assert(!boundValue || value->GetValueNumber() != boundValue->GetValueNumber());
  576. ValueInfo *const valueInfo = value->GetValueInfo();
  577. IntBounds *const bounds =
  578. GetIntBoundsToUpdate(valueInfo, constantBounds, true, boundConstantBounds.IsConstant(), true, isExplicit);
  579. if(bounds)
  580. {
  581. if(boundValue)
  582. {
  583. bounds->SetUpperBound(value->GetValueNumber(), boundValue, boundOffset, isExplicit);
  584. }
  585. else
  586. {
  587. bounds->SetUpperBound(boundConstantBounds.LowerBound(), boundOffset, isExplicit);
  588. }
  589. if(bounds->RequiresIntBoundedValueInfo(valueInfo->Type()))
  590. {
  591. return NewIntBoundedValueInfo(valueInfo, bounds);
  592. }
  593. bounds->Delete();
  594. }
  595. if(!valueInfo->IsInt())
  596. {
  597. return nullptr;
  598. }
  599. int32 adjustedBoundMax;
  600. if(boundOffset == 0)
  601. {
  602. adjustedBoundMax = boundConstantBounds.UpperBound();
  603. }
  604. else if(boundOffset == -1)
  605. {
  606. if(boundConstantBounds.UpperBound() - 1 >= boundConstantBounds.UpperBound())
  607. {
  608. return nullptr;
  609. }
  610. adjustedBoundMax = boundConstantBounds.UpperBound() - 1;
  611. }
  612. else if(Int32Math::Add(boundConstantBounds.UpperBound(), boundOffset, &adjustedBoundMax))
  613. {
  614. return nullptr;
  615. }
  616. const int32 newMax = min(constantBounds.UpperBound(), adjustedBoundMax);
  617. return
  618. newMax >= constantBounds.LowerBound()
  619. ? NewIntRangeValueInfo(valueInfo, constantBounds.LowerBound(), newMax)
  620. : nullptr;
  621. }
  622. ValueInfo *GlobOpt::UpdateIntBoundsForLessThan(
  623. Value *const value,
  624. const IntConstantBounds &constantBounds,
  625. Value *const boundValue,
  626. const IntConstantBounds &boundConstantBounds,
  627. const bool isExplicit)
  628. {
  629. return UpdateIntBoundsForLessThanOrEqual(value, constantBounds, boundValue, boundConstantBounds, -1, isExplicit);
  630. }
  631. void GlobOpt::TrackIntSpecializedAddSubConstant(
  632. IR::Instr *const instr,
  633. const AddSubConstantInfo *const addSubConstantInfo,
  634. Value *const dstValue,
  635. const bool updateSourceBounds)
  636. {
  637. Assert(instr);
  638. Assert(dstValue);
  639. if(addSubConstantInfo)
  640. {
  641. Assert(addSubConstantInfo->HasInfo());
  642. Assert(!ignoredIntOverflowForCurrentInstr);
  643. do
  644. {
  645. if(!IsLoopPrePass() || !DoBoundCheckHoist())
  646. {
  647. break;
  648. }
  649. Assert(
  650. instr->m_opcode == Js::OpCode::Incr_A ||
  651. instr->m_opcode == Js::OpCode::Decr_A ||
  652. instr->m_opcode == Js::OpCode::Add_A ||
  653. instr->m_opcode == Js::OpCode::Sub_A);
  654. StackSym *sym = instr->GetDst()->AsRegOpnd()->m_sym;
  655. bool isPostfixIncDecPattern = false;
  656. if(addSubConstantInfo->SrcSym() != sym)
  657. {
  658. // Check for the following patterns.
  659. //
  660. // This pattern is used for postfix inc/dec operators:
  661. // s2 = Conv_Num s1
  662. // s1 = Inc s2
  663. //
  664. // This pattern is used for prefix inc/dec operators:
  665. // s2 = Inc s1
  666. // s1 = Ld s2
  667. IR::Instr *const prevInstr = instr->m_prev;
  668. Assert(prevInstr);
  669. if(prevInstr->m_opcode == Js::OpCode::Conv_Num &&
  670. prevInstr->GetSrc1()->IsRegOpnd() &&
  671. prevInstr->GetSrc1()->AsRegOpnd()->m_sym == sym &&
  672. prevInstr->GetDst()->AsRegOpnd()->m_sym == addSubConstantInfo->SrcSym())
  673. {
  674. // s2 will get a new value number, since Conv_Num cannot transfer in the prepass. For the purposes of
  675. // induction variable tracking however, it doesn't matter, so record this case and use s1's value in the
  676. // current block.
  677. isPostfixIncDecPattern = true;
  678. }
  679. else
  680. {
  681. IR::Instr *const nextInstr = instr->m_next;
  682. Assert(nextInstr);
  683. if(nextInstr->m_opcode != Js::OpCode::Ld_A ||
  684. !nextInstr->GetSrc1()->IsRegOpnd() ||
  685. nextInstr->GetSrc1()->AsRegOpnd()->m_sym != sym)
  686. {
  687. break;
  688. }
  689. sym = addSubConstantInfo->SrcSym();
  690. if(nextInstr->GetDst()->AsRegOpnd()->m_sym != sym)
  691. {
  692. break;
  693. }
  694. // In the prefix inc/dec pattern, the result of Ld currently gets a new value number, which will cause the
  695. // induction variable info to become indeterminate. Indicate that the value number should be updated in the
  696. // induction variable info.
  697. // Consider: Remove this once loop prepass value transfer scheme is fixed
  698. updateInductionVariableValueNumber = true;
  699. }
  700. }
  701. // Track induction variable info
  702. ValueNumber srcValueNumber;
  703. if(isPostfixIncDecPattern)
  704. {
  705. Value *const value = this->currentBlock->globOptData.FindValue(sym);
  706. Assert(value);
  707. srcValueNumber = value->GetValueNumber();
  708. }
  709. else
  710. {
  711. srcValueNumber = addSubConstantInfo->SrcValue()->GetValueNumber();
  712. }
  713. InductionVariableSet *const inductionVariables = currentBlock->globOptData.inductionVariables;
  714. Assert(inductionVariables);
  715. InductionVariable *inductionVariable;
  716. if(!inductionVariables->TryGetReference(sym->m_id, &inductionVariable))
  717. {
  718. // Only track changes in the current loop's prepass. In subsequent prepasses, the info is only being propagated
  719. // for use by the parent loop, so changes in the current loop have already been tracked.
  720. if(prePassLoop != currentBlock->loop)
  721. {
  722. updateInductionVariableValueNumber = false;
  723. break;
  724. }
  725. // Ensure that the sym is live in the landing pad, and that its value has not changed in an unknown way yet
  726. Value *const landingPadValue = currentBlock->loop->landingPad->globOptData.FindValue(sym);
  727. if(!landingPadValue || srcValueNumber != landingPadValue->GetValueNumber())
  728. {
  729. updateInductionVariableValueNumber = false;
  730. break;
  731. }
  732. inductionVariables->Add(
  733. InductionVariable(sym, dstValue->GetValueNumber(), addSubConstantInfo->Offset()));
  734. break;
  735. }
  736. if(!inductionVariable->IsChangeDeterminate())
  737. {
  738. updateInductionVariableValueNumber = false;
  739. break;
  740. }
  741. if(srcValueNumber != inductionVariable->SymValueNumber())
  742. {
  743. // The sym's value has changed since the last time induction variable info was recorded for it. Due to the
  744. // unknown change, mark the info as indeterminate.
  745. inductionVariable->SetChangeIsIndeterminate();
  746. updateInductionVariableValueNumber = false;
  747. break;
  748. }
  749. // Only track changes in the current loop's prepass. In subsequent prepasses, the info is only being propagated for
  750. // use by the parent loop, so changes in the current loop have already been tracked. Induction variable value
  751. // numbers are updated as changes occur, but their change bounds are preserved from the first prepass over the loop.
  752. inductionVariable->SetSymValueNumber(dstValue->GetValueNumber());
  753. if(prePassLoop != currentBlock->loop)
  754. {
  755. break;
  756. }
  757. if(!inductionVariable->Add(addSubConstantInfo->Offset()))
  758. {
  759. updateInductionVariableValueNumber = false;
  760. }
  761. } while(false);
  762. if(updateSourceBounds && addSubConstantInfo->Offset() != IntConstMin)
  763. {
  764. // Track bounds for add or sub with a constant. For instance, consider (b = a + 2). The value of 'b' should track
  765. // that it is equal to (the value of 'a') + 2. That part has been done above. Similarly, the value of 'a' should
  766. // also track that it is equal to (the value of 'b') - 2.
  767. Value *const value = addSubConstantInfo->SrcValue();
  768. const ValueInfo *const valueInfo = value->GetValueInfo();
  769. Assert(valueInfo->IsInt());
  770. IntConstantBounds constantBounds;
  771. AssertVerify(valueInfo->TryGetIntConstantBounds(&constantBounds));
  772. IntBounds *const bounds =
  773. GetIntBoundsToUpdate(
  774. valueInfo,
  775. constantBounds,
  776. true,
  777. dstValue->GetValueInfo()->HasIntConstantValue(),
  778. true,
  779. true);
  780. if(bounds)
  781. {
  782. const ValueNumber valueNumber = value->GetValueNumber();
  783. const int32 dstOffset = -addSubConstantInfo->Offset();
  784. bounds->SetLowerBound(valueNumber, dstValue, dstOffset, true);
  785. bounds->SetUpperBound(valueNumber, dstValue, dstOffset, true);
  786. ChangeValueInfo(nullptr, value, NewIntBoundedValueInfo(valueInfo, bounds));
  787. }
  788. }
  789. return;
  790. }
  791. if(!updateInductionVariableValueNumber)
  792. {
  793. return;
  794. }
  795. // See comment above where this is set to true
  796. // Consider: Remove this once loop prepass value transfer scheme is fixed
  797. updateInductionVariableValueNumber = false;
  798. Assert(IsLoopPrePass());
  799. Assert(instr->m_opcode == Js::OpCode::Ld_A);
  800. Assert(
  801. instr->m_prev->m_opcode == Js::OpCode::Incr_A ||
  802. instr->m_prev->m_opcode == Js::OpCode::Decr_A ||
  803. instr->m_prev->m_opcode == Js::OpCode::Add_A ||
  804. instr->m_prev->m_opcode == Js::OpCode::Sub_A);
  805. Assert(instr->m_prev->GetDst()->AsRegOpnd()->m_sym == instr->GetSrc1()->AsRegOpnd()->m_sym);
  806. InductionVariable *inductionVariable;
  807. AssertVerify(currentBlock->globOptData.inductionVariables->TryGetReference(instr->GetDst()->AsRegOpnd()->m_sym->m_id, &inductionVariable));
  808. inductionVariable->SetSymValueNumber(dstValue->GetValueNumber());
  809. }
  810. void GlobOpt::CloneBoundCheckHoistBlockData(
  811. BasicBlock *const toBlock,
  812. GlobOptBlockData *const toData,
  813. BasicBlock *const fromBlock,
  814. GlobOptBlockData *const fromData)
  815. {
  816. Assert(DoBoundCheckHoist());
  817. Assert(toBlock);
  818. Assert(toData);
  819. //Assert(toData == &toBlock->globOptData || toData == &currentBlock->globOptData);
  820. Assert(fromBlock);
  821. Assert(fromData);
  822. Assert(fromData == &fromBlock->globOptData);
  823. Assert(fromData->availableIntBoundChecks);
  824. toData->availableIntBoundChecks = fromData->availableIntBoundChecks->Clone();
  825. if(toBlock->isLoopHeader)
  826. {
  827. Assert(fromBlock == toBlock->loop->landingPad);
  828. if(prePassLoop == toBlock->loop)
  829. {
  830. // When the current prepass loop is the current loop, the loop header's induction variable set needs to start off
  831. // empty to track changes in the current loop
  832. toData->inductionVariables = JitAnew(alloc, InductionVariableSet, alloc);
  833. return;
  834. }
  835. if(!IsLoopPrePass())
  836. {
  837. return;
  838. }
  839. // After the prepass on this loop, if we're still in a prepass, this must be an inner loop. Merge the landing pad info
  840. // for use by the parent loop.
  841. Assert(fromBlock->loop);
  842. Assert(fromData->inductionVariables);
  843. toData->inductionVariables = fromData->inductionVariables->Clone();
  844. return;
  845. }
  846. if(!toBlock->loop || !IsLoopPrePass())
  847. {
  848. return;
  849. }
  850. Assert(fromBlock->loop);
  851. Assert(toBlock->loop->IsDescendentOrSelf(fromBlock->loop));
  852. Assert(fromData->inductionVariables);
  853. toData->inductionVariables = fromData->inductionVariables->Clone();
  854. }
  855. void GlobOpt::MergeBoundCheckHoistBlockData(
  856. BasicBlock *const toBlock,
  857. GlobOptBlockData *const toData,
  858. BasicBlock *const fromBlock,
  859. GlobOptBlockData *const fromData)
  860. {
  861. Assert(DoBoundCheckHoist());
  862. Assert(toBlock);
  863. Assert(toData);
  864. //Assert(toData == &toBlock->globOptData || toData == &currentBlock->globOptData);
  865. Assert(fromBlock);
  866. Assert(fromData);
  867. Assert(fromData == &fromBlock->globOptData);
  868. Assert(toData->availableIntBoundChecks);
  869. for(auto it = toData->availableIntBoundChecks->GetIteratorWithRemovalSupport(); it.IsValid(); it.MoveNext())
  870. {
  871. const IntBoundCheck &toDataIntBoundCheck = it.CurrentValue();
  872. const IntBoundCheck *fromDataIntBoundCheck;
  873. if(!fromData->availableIntBoundChecks->TryGetReference(
  874. toDataIntBoundCheck.CompatibilityId(),
  875. &fromDataIntBoundCheck) ||
  876. fromDataIntBoundCheck->Instr() != toDataIntBoundCheck.Instr())
  877. {
  878. it.RemoveCurrent();
  879. }
  880. }
  881. InductionVariableSet *mergeInductionVariablesInto;
  882. if(toBlock->isLoopHeader)
  883. {
  884. Assert(fromBlock->loop == toBlock->loop); // The flow is such that you cannot have back-edges from an inner loop
  885. if(IsLoopPrePass())
  886. {
  887. // Collect info for the parent loop. Any changes to induction variables in this inner loop need to be expanded in
  888. // the same direction for the parent loop, so merge expanded info from back-edges. Info about induction variables
  889. // that changed before the loop but not inside the loop, can be kept intact because the landing pad dominates the
  890. // loop.
  891. Assert(prePassLoop != toBlock->loop);
  892. Assert(fromData->inductionVariables);
  893. Assert(toData->inductionVariables);
  894. InductionVariableSet *const mergedInductionVariables = toData->inductionVariables;
  895. for(auto it = fromData->inductionVariables->GetIterator(); it.IsValid(); it.MoveNext())
  896. {
  897. InductionVariable backEdgeInductionVariable = it.CurrentValue();
  898. backEdgeInductionVariable.ExpandInnerLoopChange();
  899. StackSym *const sym = backEdgeInductionVariable.Sym();
  900. InductionVariable *mergedInductionVariable;
  901. if(mergedInductionVariables->TryGetReference(sym->m_id, &mergedInductionVariable))
  902. {
  903. mergedInductionVariable->Merge(backEdgeInductionVariable);
  904. continue;
  905. }
  906. // Ensure that the sym is live in the parent loop's landing pad, and that its value has not changed in an
  907. // unknown way between the parent loop's landing pad and the current loop's landing pad.
  908. Value *const parentLandingPadValue = currentBlock->loop->parent->landingPad->globOptData.FindValue(sym);
  909. if(!parentLandingPadValue)
  910. {
  911. continue;
  912. }
  913. Value *const landingPadValue = currentBlock->loop->landingPad->globOptData.FindValue(sym);
  914. Assert(landingPadValue);
  915. if(landingPadValue->GetValueNumber() == parentLandingPadValue->GetValueNumber())
  916. {
  917. mergedInductionVariables->Add(backEdgeInductionVariable);
  918. }
  919. }
  920. const InductionVariableSet *const fromDataInductionVariables = fromData->inductionVariables;
  921. for (auto it = mergedInductionVariables->GetIterator(); it.IsValid(); it.MoveNext())
  922. {
  923. InductionVariable &mergedInductionVariable = it.CurrentValueReference();
  924. if (!mergedInductionVariable.IsChangeDeterminate())
  925. {
  926. continue;
  927. }
  928. StackSym *const sym = mergedInductionVariable.Sym();
  929. const InductionVariable *fromDataInductionVariable;
  930. if (fromDataInductionVariables->TryGetReference(sym->m_id, &fromDataInductionVariable))
  931. {
  932. continue;
  933. }
  934. // Process the set of symbols that are induction variables due to prior loops that share the same parent loop, but are not induction variables in the current loop
  935. // If the current loop is initializing such carried over induction variables, then their value numbers will differ from the current loop's landing pad
  936. // Such induction variables should be marked as indeterminate going forward, such the induction variable analysis accurately flows to the parent loop.
  937. Value *const fromDataValue = fromData->FindValue(sym);
  938. if (fromDataValue)
  939. {
  940. Value *const landingPadValue = toBlock->loop->landingPad->globOptData.FindValue(sym);
  941. if (landingPadValue && fromDataValue->GetValueNumber() != landingPadValue->GetValueNumber())
  942. {
  943. mergedInductionVariable.SetChangeIsIndeterminate();
  944. }
  945. }
  946. }
  947. return;
  948. }
  949. // Collect info for the current loop. We want to merge only the back-edge info without the landing pad info, such that
  950. // the loop's induction variable set reflects changes made inside this loop.
  951. Assert(fromData->inductionVariables);
  952. InductionVariableSet *&loopInductionVariables = toBlock->loop->inductionVariables;
  953. if(!loopInductionVariables)
  954. {
  955. loopInductionVariables = fromData->inductionVariables->Clone();
  956. return;
  957. }
  958. mergeInductionVariablesInto = loopInductionVariables;
  959. }
  960. else if(toBlock->loop && IsLoopPrePass())
  961. {
  962. Assert(fromBlock->loop);
  963. Assert(toBlock->loop->IsDescendentOrSelf(fromBlock->loop));
  964. mergeInductionVariablesInto = toData->inductionVariables;
  965. }
  966. else
  967. {
  968. return;
  969. }
  970. const InductionVariableSet *const fromDataInductionVariables = fromData->inductionVariables;
  971. InductionVariableSet *const mergedInductionVariables = mergeInductionVariablesInto;
  972. Assert(fromDataInductionVariables);
  973. Assert(mergedInductionVariables);
  974. for(auto it = mergedInductionVariables->GetIterator(); it.IsValid(); it.MoveNext())
  975. {
  976. InductionVariable &mergedInductionVariable = it.CurrentValueReference();
  977. if(!mergedInductionVariable.IsChangeDeterminate())
  978. {
  979. continue;
  980. }
  981. StackSym *const sym = mergedInductionVariable.Sym();
  982. const InductionVariable *fromDataInductionVariable;
  983. if(fromDataInductionVariables->TryGetReference(sym->m_id, &fromDataInductionVariable))
  984. {
  985. mergedInductionVariable.Merge(*fromDataInductionVariable);
  986. continue;
  987. }
  988. // Ensure that the sym is live in the landing pad, and that its value has not changed in an unknown way yet on the path
  989. // where the sym is not already marked as an induction variable.
  990. Value *const fromDataValue = fromData->FindValue(sym);
  991. if(fromDataValue)
  992. {
  993. Value *const landingPadValue = toBlock->loop->landingPad->globOptData.FindValue(sym);
  994. if(landingPadValue && fromDataValue->GetValueNumber() == landingPadValue->GetValueNumber())
  995. {
  996. mergedInductionVariable.Merge(InductionVariable(sym, ZeroValueNumber, 0));
  997. continue;
  998. }
  999. }
  1000. mergedInductionVariable.SetChangeIsIndeterminate();
  1001. }
  1002. for(auto it = fromDataInductionVariables->GetIterator(); it.IsValid(); it.MoveNext())
  1003. {
  1004. const InductionVariable &fromDataInductionVariable = it.CurrentValue();
  1005. StackSym *const sym = fromDataInductionVariable.Sym();
  1006. if(mergedInductionVariables->ContainsKey(sym->m_id))
  1007. {
  1008. continue;
  1009. }
  1010. // Ensure that the sym is live in the landing pad, and that its value has not changed in an unknown way yet on the path
  1011. // where the sym is not already marked as an induction variable.
  1012. bool indeterminate = true;
  1013. Value *const toDataValue = toData->FindValue(sym);
  1014. if(toDataValue)
  1015. {
  1016. Value *const landingPadValue = toBlock->loop->landingPad->globOptData.FindValue(sym);
  1017. if(landingPadValue && toDataValue->GetValueNumber() == landingPadValue->GetValueNumber())
  1018. {
  1019. indeterminate = false;
  1020. }
  1021. }
  1022. InductionVariable mergedInductionVariable(sym, ZeroValueNumber, 0);
  1023. if(indeterminate)
  1024. {
  1025. mergedInductionVariable.SetChangeIsIndeterminate();
  1026. }
  1027. else
  1028. {
  1029. mergedInductionVariable.Merge(fromDataInductionVariable);
  1030. }
  1031. mergedInductionVariables->Add(mergedInductionVariable);
  1032. }
  1033. }
  1034. void GlobOpt::DetectUnknownChangesToInductionVariables(GlobOptBlockData *const blockData)
  1035. {
  1036. Assert(DoBoundCheckHoist());
  1037. Assert(IsLoopPrePass());
  1038. Assert(blockData);
  1039. Assert(blockData->inductionVariables);
  1040. // Check induction variable value numbers, and mark those that changed in an unknown way as indeterminate. They must remain
  1041. // in the set though, for merging purposes.
  1042. for(auto it = blockData->inductionVariables->GetIterator(); it.IsValid(); it.MoveNext())
  1043. {
  1044. InductionVariable &inductionVariable = it.CurrentValueReference();
  1045. if(!inductionVariable.IsChangeDeterminate())
  1046. {
  1047. continue;
  1048. }
  1049. Value *const value = blockData->FindValue(inductionVariable.Sym());
  1050. if(!value || value->GetValueNumber() != inductionVariable.SymValueNumber())
  1051. {
  1052. inductionVariable.SetChangeIsIndeterminate();
  1053. }
  1054. }
  1055. }
  1056. void GlobOpt::SetInductionVariableValueNumbers(GlobOptBlockData *const blockData)
  1057. {
  1058. Assert(DoBoundCheckHoist());
  1059. Assert(IsLoopPrePass());
  1060. //Assert(blockData == &this->currentBlock->globOptData);
  1061. Assert(blockData->inductionVariables);
  1062. // Now that all values have been merged, update value numbers in the induction variable info.
  1063. for(auto it = blockData->inductionVariables->GetIterator(); it.IsValid(); it.MoveNext())
  1064. {
  1065. InductionVariable &inductionVariable = it.CurrentValueReference();
  1066. if(!inductionVariable.IsChangeDeterminate())
  1067. {
  1068. continue;
  1069. }
  1070. Value *const value = blockData->FindValue(inductionVariable.Sym());
  1071. if(value)
  1072. {
  1073. inductionVariable.SetSymValueNumber(value->GetValueNumber());
  1074. }
  1075. else
  1076. {
  1077. inductionVariable.SetChangeIsIndeterminate();
  1078. }
  1079. }
  1080. }
  1081. void GlobOpt::FinalizeInductionVariables(Loop *const loop, GlobOptBlockData *const headerData)
  1082. {
  1083. Assert(DoBoundCheckHoist());
  1084. Assert(!IsLoopPrePass());
  1085. Assert(loop);
  1086. Assert(loop->GetHeadBlock() == currentBlock);
  1087. Assert(loop->inductionVariables);
  1088. Assert(currentBlock->isLoopHeader);
  1089. //Assert(headerData == &this->currentBlock->globOptData);
  1090. // Clean up induction variables and for each, install a relationship between its values inside and outside the loop.
  1091. GlobOptBlockData &landingPadBlockData = loop->landingPad->globOptData;
  1092. for(auto it = loop->inductionVariables->GetIterator(); it.IsValid(); it.MoveNext())
  1093. {
  1094. InductionVariable &inductionVariable = it.CurrentValueReference();
  1095. if(!inductionVariable.IsChangeDeterminate())
  1096. {
  1097. continue;
  1098. }
  1099. if(!inductionVariable.IsChangeUnidirectional())
  1100. {
  1101. inductionVariable.SetChangeIsIndeterminate();
  1102. continue;
  1103. }
  1104. StackSym *const sym = inductionVariable.Sym();
  1105. if(!headerData->IsInt32TypeSpecialized(sym))
  1106. {
  1107. inductionVariable.SetChangeIsIndeterminate();
  1108. continue;
  1109. }
  1110. Assert(landingPadBlockData.IsInt32TypeSpecialized(sym));
  1111. Value *const value = headerData->FindValue(sym);
  1112. if(!value)
  1113. {
  1114. inductionVariable.SetChangeIsIndeterminate();
  1115. continue;
  1116. }
  1117. Value *const landingPadValue = landingPadBlockData.FindValue(sym);
  1118. Assert(landingPadValue);
  1119. IntConstantBounds constantBounds, landingPadConstantBounds;
  1120. AssertVerify(value->GetValueInfo()->TryGetIntConstantBounds(&constantBounds));
  1121. AssertVerify(landingPadValue->GetValueInfo()->TryGetIntConstantBounds(&landingPadConstantBounds, true));
  1122. // For an induction variable i, update the value of i inside the loop to indicate that it is bounded by the value of i
  1123. // just before the loop.
  1124. if(inductionVariable.ChangeBounds().LowerBound() >= 0)
  1125. {
  1126. ValueInfo *const newValueInfo =
  1127. UpdateIntBoundsForGreaterThanOrEqual(value, constantBounds, landingPadValue, landingPadConstantBounds, true);
  1128. ChangeValueInfo(nullptr, value, newValueInfo);
  1129. if(inductionVariable.ChangeBounds().UpperBound() == 0)
  1130. {
  1131. AssertVerify(newValueInfo->TryGetIntConstantBounds(&constantBounds, true));
  1132. }
  1133. }
  1134. if(inductionVariable.ChangeBounds().UpperBound() <= 0)
  1135. {
  1136. ValueInfo *const newValueInfo =
  1137. UpdateIntBoundsForLessThanOrEqual(value, constantBounds, landingPadValue, landingPadConstantBounds, true);
  1138. ChangeValueInfo(nullptr, value, newValueInfo);
  1139. }
  1140. }
  1141. }
  1142. GlobOpt::SymBoundType GlobOpt::DetermineSymBoundOffsetOrValueRelativeToLandingPad(
  1143. StackSym *const sym,
  1144. const bool landingPadValueIsLowerBound,
  1145. ValueInfo *const valueInfo,
  1146. const IntBounds *const bounds,
  1147. GlobOptBlockData *const landingPadGlobOptBlockData,
  1148. int *const boundOffsetOrValueRef)
  1149. {
  1150. Assert(sym);
  1151. Assert(!sym->IsTypeSpec());
  1152. Assert(valueInfo);
  1153. Assert(landingPadGlobOptBlockData);
  1154. Assert(boundOffsetOrValueRef);
  1155. Assert(valueInfo->IsInt());
  1156. int constantValue;
  1157. if(valueInfo->TryGetIntConstantValue(&constantValue))
  1158. {
  1159. // The sym's constant value is the constant bound value, so just return that. This is possible in loops such as
  1160. // for(; i === 1; ++i){...}, where 'i' is an induction variable but has a constant value inside the loop, or in blocks
  1161. // inside the loop such as if(i === 1){...}
  1162. *boundOffsetOrValueRef = constantValue;
  1163. return SymBoundType::VALUE;
  1164. }
  1165. if (bounds)
  1166. {
  1167. Value *const landingPadValue = landingPadGlobOptBlockData->FindValue(sym);
  1168. Assert(landingPadValue);
  1169. Assert(landingPadValue->GetValueInfo()->IsInt());
  1170. int landingPadConstantValue;
  1171. const ValueRelativeOffset* bound = nullptr;
  1172. const RelativeIntBoundSet& boundSet = landingPadValueIsLowerBound ? bounds->RelativeLowerBounds() : bounds->RelativeUpperBounds();
  1173. if (landingPadValue->GetValueInfo()->TryGetIntConstantValue(&landingPadConstantValue))
  1174. {
  1175. // The sym's bound already takes the landing pad constant value into consideration, unless the landing pad value was
  1176. // updated to have a more aggressive range (and hence, now a constant value) as part of hoisting a bound check or some
  1177. // other hoisting operation. The sym's bound also takes into consideration the change to the sym so far inside the loop,
  1178. // and the landing pad constant value does not, so use the sym's bound by default.
  1179. int constantBound = landingPadValueIsLowerBound ? bounds->ConstantLowerBound() : bounds->ConstantUpperBound();
  1180. if(landingPadValueIsLowerBound ? landingPadConstantValue > constantBound : landingPadConstantValue < constantBound)
  1181. {
  1182. // The landing pad value became a constant value as part of a hoisting operation. The landing pad constant value is
  1183. // a more aggressive bound, so use that instead, and take into consideration the change to the sym so far inside the
  1184. // loop, using the relative bound to the landing pad value.
  1185. if (!boundSet.TryGetReference(landingPadValue->GetValueNumber(), &bound))
  1186. {
  1187. return SymBoundType::UNKNOWN;
  1188. }
  1189. constantBound = landingPadConstantValue + bound->Offset();
  1190. }
  1191. *boundOffsetOrValueRef = constantBound;
  1192. return SymBoundType::VALUE;
  1193. }
  1194. if (!boundSet.TryGetReference(landingPadValue->GetValueNumber(), &bound))
  1195. {
  1196. return SymBoundType::UNKNOWN;
  1197. }
  1198. *boundOffsetOrValueRef = bound->Offset();
  1199. return SymBoundType::OFFSET;
  1200. }
  1201. AssertVerify(
  1202. landingPadValueIsLowerBound
  1203. ? valueInfo->TryGetIntConstantLowerBound(boundOffsetOrValueRef)
  1204. : valueInfo->TryGetIntConstantUpperBound(boundOffsetOrValueRef));
  1205. return SymBoundType::VALUE;
  1206. }
  1207. void GlobOpt::DetermineDominatingLoopCountableBlock(Loop *const loop, BasicBlock *const headerBlock)
  1208. {
  1209. Assert(DoLoopCountBasedBoundCheckHoist());
  1210. Assert(!IsLoopPrePass());
  1211. Assert(loop);
  1212. Assert(headerBlock);
  1213. Assert(headerBlock->isLoopHeader);
  1214. Assert(headerBlock->loop == loop);
  1215. // Determine if the loop header has a unique successor that is inside the loop. If so, then all other paths out of the loop
  1216. // header exit the loop, allowing a loop count to be established and used from the unique in-loop successor block.
  1217. Assert(!loop->dominatingLoopCountableBlock);
  1218. FOREACH_SUCCESSOR_BLOCK(successor, headerBlock)
  1219. {
  1220. if(successor->loop != loop)
  1221. {
  1222. Assert(!successor->loop || successor->loop->IsDescendentOrSelf(loop->parent));
  1223. continue;
  1224. }
  1225. if(loop->dominatingLoopCountableBlock)
  1226. {
  1227. // Found a second successor inside the loop
  1228. loop->dominatingLoopCountableBlock = nullptr;
  1229. break;
  1230. }
  1231. loop->dominatingLoopCountableBlock = successor;
  1232. } NEXT_SUCCESSOR_BLOCK;
  1233. }
  1234. void GlobOpt::DetermineLoopCount(Loop *const loop)
  1235. {
  1236. Assert(DoLoopCountBasedBoundCheckHoist());
  1237. Assert(loop);
  1238. GlobOptBlockData &landingPadBlockData = loop->landingPad->globOptData;
  1239. const InductionVariableSet *const inductionVariables = loop->inductionVariables;
  1240. Assert(inductionVariables);
  1241. for(auto inductionVariablesIterator = inductionVariables->GetIterator(); inductionVariablesIterator.IsValid(); inductionVariablesIterator.MoveNext())
  1242. {
  1243. InductionVariable &inductionVariable = inductionVariablesIterator.CurrentValueReference();
  1244. if(!inductionVariable.IsChangeDeterminate())
  1245. {
  1246. continue;
  1247. }
  1248. // Determine the minimum-magnitude change per iteration, and verify that the change is nonzero and finite
  1249. Assert(inductionVariable.IsChangeUnidirectional());
  1250. int minMagnitudeChange = inductionVariable.ChangeBounds().LowerBound();
  1251. if(minMagnitudeChange >= 0)
  1252. {
  1253. if(minMagnitudeChange == 0 || minMagnitudeChange == IntConstMax)
  1254. {
  1255. continue;
  1256. }
  1257. }
  1258. else
  1259. {
  1260. minMagnitudeChange = inductionVariable.ChangeBounds().UpperBound();
  1261. Assert(minMagnitudeChange <= 0);
  1262. if(minMagnitudeChange == 0 || minMagnitudeChange == IntConstMin)
  1263. {
  1264. continue;
  1265. }
  1266. }
  1267. StackSym *const inductionVariableVarSym = inductionVariable.Sym();
  1268. if(!this->currentBlock->globOptData.IsInt32TypeSpecialized(inductionVariableVarSym))
  1269. {
  1270. inductionVariable.SetChangeIsIndeterminate();
  1271. continue;
  1272. }
  1273. Assert(landingPadBlockData.IsInt32TypeSpecialized(inductionVariableVarSym));
  1274. Value *const inductionVariableValue = this->currentBlock->globOptData.FindValue(inductionVariableVarSym);
  1275. if(!inductionVariableValue)
  1276. {
  1277. inductionVariable.SetChangeIsIndeterminate();
  1278. continue;
  1279. }
  1280. ValueInfo *const inductionVariableValueInfo = inductionVariableValue->GetValueInfo();
  1281. const IntBounds *const inductionVariableBounds =
  1282. inductionVariableValueInfo->IsIntBounded() ? inductionVariableValueInfo->AsIntBounded()->Bounds() : nullptr;
  1283. // Look for an invariant bound in the direction of change
  1284. StackSym *boundBaseVarSym = nullptr;
  1285. int boundOffset = 0;
  1286. {
  1287. bool foundBound = false;
  1288. if(inductionVariableBounds)
  1289. {
  1290. // Look for a relative bound
  1291. for(auto it =
  1292. (
  1293. minMagnitudeChange >= 0
  1294. ? inductionVariableBounds->RelativeUpperBounds()
  1295. : inductionVariableBounds->RelativeLowerBounds()
  1296. ).GetIterator();
  1297. it.IsValid();
  1298. it.MoveNext())
  1299. {
  1300. const ValueRelativeOffset &bound = it.CurrentValue();
  1301. StackSym *currentBoundBaseVarSym = bound.BaseSym();
  1302. if(!currentBoundBaseVarSym || !landingPadBlockData.IsInt32TypeSpecialized(currentBoundBaseVarSym))
  1303. {
  1304. continue;
  1305. }
  1306. Value *const boundBaseValue = this->currentBlock->globOptData.FindValue(currentBoundBaseVarSym);
  1307. const ValueNumber boundBaseValueNumber = bound.BaseValueNumber();
  1308. if(!boundBaseValue || boundBaseValue->GetValueNumber() != boundBaseValueNumber)
  1309. {
  1310. continue;
  1311. }
  1312. Value *const landingPadBoundBaseValue = landingPadBlockData.FindValue(currentBoundBaseVarSym);
  1313. if(!landingPadBoundBaseValue || landingPadBoundBaseValue->GetValueNumber() != boundBaseValueNumber)
  1314. {
  1315. continue;
  1316. }
  1317. if (foundBound)
  1318. {
  1319. // We used to pick the first usable bound we saw in this list, but the list contains both
  1320. // the loop counter's bound *and* relative bounds of the primary bound. These secondary bounds
  1321. // are not guaranteed to be correct, so if the bound we found on a previous iteration is itself
  1322. // a bound for the current bound, then choose the current bound.
  1323. if (!boundBaseValue->GetValueInfo()->IsIntBounded())
  1324. {
  1325. continue;
  1326. }
  1327. // currentBoundBaseVarSym has relative bounds of its own. If we find the saved boundBaseVarSym
  1328. // in currentBoundBaseVarSym's relative bounds list, let currentBoundBaseVarSym be the
  1329. // chosen bound.
  1330. const IntBounds *const currentBounds = boundBaseValue->GetValueInfo()->AsIntBounded()->Bounds();
  1331. bool foundSecondaryBound = false;
  1332. for (auto it2 =
  1333. (
  1334. minMagnitudeChange >= 0
  1335. ? currentBounds->RelativeUpperBounds()
  1336. : currentBounds->RelativeLowerBounds()
  1337. ).GetIterator();
  1338. it2.IsValid();
  1339. it2.MoveNext())
  1340. {
  1341. const ValueRelativeOffset &bound2 = it2.CurrentValue();
  1342. if (bound2.BaseSym() == boundBaseVarSym)
  1343. {
  1344. // boundBaseVarSym is a secondary bound. Use currentBoundBaseVarSym instead.
  1345. foundSecondaryBound = true;
  1346. break;
  1347. }
  1348. }
  1349. if (!foundSecondaryBound)
  1350. {
  1351. // boundBaseVarSym is not a relative bound of currentBoundBaseVarSym, so continue
  1352. // to use boundBaseVarSym.
  1353. continue;
  1354. }
  1355. }
  1356. boundBaseVarSym = bound.BaseSym();
  1357. boundOffset = bound.Offset();
  1358. foundBound = true;
  1359. }
  1360. }
  1361. if(!foundBound)
  1362. {
  1363. // No useful relative bound found; look for a constant bound. Exclude large constant bounds established implicitly by
  1364. // <, <=, >, and >=. For example, for a loop condition (i < n), if 'n' is not invariant and hence can't be used,
  1365. // 'i' will still have a constant upper bound of (int32 max - 1) that should be excluded as it's too large. Any
  1366. // other constant bounds must have been established explicitly by the loop condition, and are safe to use.
  1367. boundBaseVarSym = nullptr;
  1368. if(minMagnitudeChange >= 0)
  1369. {
  1370. if(inductionVariableBounds)
  1371. {
  1372. boundOffset = inductionVariableBounds->ConstantUpperBound();
  1373. }
  1374. else
  1375. {
  1376. AssertVerify(inductionVariableValueInfo->TryGetIntConstantUpperBound(&boundOffset));
  1377. }
  1378. if(boundOffset >= IntConstMax - 1)
  1379. {
  1380. continue;
  1381. }
  1382. }
  1383. else
  1384. {
  1385. if(inductionVariableBounds)
  1386. {
  1387. boundOffset = inductionVariableBounds->ConstantLowerBound();
  1388. }
  1389. else
  1390. {
  1391. AssertVerify(inductionVariableValueInfo->TryGetIntConstantLowerBound(&boundOffset));
  1392. }
  1393. if(boundOffset <= IntConstMin + 1)
  1394. {
  1395. continue;
  1396. }
  1397. }
  1398. }
  1399. }
  1400. // Determine if the induction variable already changed in the loop, and by how much
  1401. int inductionVariableOffset = 0;
  1402. StackSym *inductionVariableSymToAdd = nullptr;
  1403. SymBoundType symBoundType = DetermineSymBoundOffsetOrValueRelativeToLandingPad(
  1404. inductionVariableVarSym,
  1405. minMagnitudeChange >= 0,
  1406. inductionVariableValueInfo,
  1407. inductionVariableBounds,
  1408. &landingPadBlockData,
  1409. &inductionVariableOffset);
  1410. if (symBoundType == SymBoundType::VALUE)
  1411. {
  1412. // The bound value is constant
  1413. inductionVariableSymToAdd = nullptr;
  1414. }
  1415. else if (symBoundType == SymBoundType::OFFSET)
  1416. {
  1417. // The bound value is not constant, the offset needs to be added to the induction variable in the landing pad
  1418. inductionVariableSymToAdd = inductionVariableVarSym->GetInt32EquivSym(nullptr);
  1419. Assert(inductionVariableSymToAdd);
  1420. }
  1421. else
  1422. {
  1423. Assert(symBoundType == SymBoundType::UNKNOWN);
  1424. // We were unable to determine the sym bound offset or value
  1425. continue;
  1426. }
  1427. // Int operands are required
  1428. StackSym *boundBaseSym;
  1429. if(boundBaseVarSym)
  1430. {
  1431. boundBaseSym = boundBaseVarSym->IsVar() ? boundBaseVarSym->GetInt32EquivSym(nullptr) : boundBaseVarSym;
  1432. Assert(boundBaseSym);
  1433. Assert(boundBaseSym->GetType() == TyInt32 || boundBaseSym->GetType() == TyUint32);
  1434. }
  1435. else
  1436. {
  1437. boundBaseSym = nullptr;
  1438. }
  1439. // The loop count is computed as follows. We're actually computing the loop count minus one, because the value is used
  1440. // to determine the bound of a secondary induction variable in its direction of change, and at that point the secondary
  1441. // induction variable's information already accounts for changes in the first loop iteration.
  1442. //
  1443. // If the induction variable increases in the loop:
  1444. // loopCountMinusOne = (upperBound - inductionVariable) / abs(minMagnitudeChange)
  1445. // Or more precisely:
  1446. // loopCountMinusOne =
  1447. // ((boundBase - inductionVariable) + (boundOffset - inductionVariableOffset)) / abs(minMagnitudeChange)
  1448. //
  1449. // If the induction variable decreases in the loop, the subtract operands are just reversed to yield a nonnegative
  1450. // number, and the rest is similar. The two offsets are also constant and can be folded. So in general:
  1451. // loopCountMinusOne = (left - right + offset) / abs(minMagnitudeChange)
  1452. // Determine the left and right information
  1453. StackSym *leftSym, *rightSym;
  1454. int leftOffset, rightOffset;
  1455. if(minMagnitudeChange >= 0)
  1456. {
  1457. leftSym = boundBaseSym;
  1458. leftOffset = boundOffset;
  1459. rightSym = inductionVariableSymToAdd;
  1460. rightOffset = inductionVariableOffset;
  1461. }
  1462. else
  1463. {
  1464. minMagnitudeChange = -minMagnitudeChange;
  1465. leftSym = inductionVariableSymToAdd;
  1466. leftOffset = inductionVariableOffset;
  1467. rightSym = boundBaseSym;
  1468. rightOffset = boundOffset;
  1469. }
  1470. // Determine the combined offset, and save the info necessary to generate the loop count
  1471. int offset;
  1472. if(Int32Math::Sub(leftOffset, rightOffset, &offset))
  1473. {
  1474. continue;
  1475. }
  1476. void *const loopCountBuffer = JitAnewArray(this->func->GetTopFunc()->m_fg->alloc, byte, sizeof(LoopCount));
  1477. if(!rightSym)
  1478. {
  1479. if(!leftSym)
  1480. {
  1481. loop->loopCount = new(loopCountBuffer) LoopCount(offset / minMagnitudeChange);
  1482. break;
  1483. }
  1484. if(offset == 0 && minMagnitudeChange == 1)
  1485. {
  1486. loop->loopCount = new(loopCountBuffer) LoopCount(leftSym);
  1487. break;
  1488. }
  1489. }
  1490. loop->loopCount = new(loopCountBuffer) LoopCount(leftSym, rightSym, offset, minMagnitudeChange);
  1491. break;
  1492. }
  1493. }
  1494. void GlobOpt::GenerateLoopCount(Loop *const loop, LoopCount *const loopCount)
  1495. {
  1496. Assert(DoLoopCountBasedBoundCheckHoist());
  1497. Assert(loop);
  1498. Assert(loopCount);
  1499. Assert(loopCount == loop->loopCount);
  1500. Assert(!loopCount->HasBeenGenerated());
  1501. // loopCountMinusOne = (left - right + offset) / minMagnitudeChange
  1502. // Prepare the landing pad for bailouts and instruction insertion
  1503. BailOutInfo *const bailOutInfo = loop->bailOutInfo;
  1504. Assert(bailOutInfo);
  1505. const IR::BailOutKind bailOutKind = IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck;
  1506. IR::Instr *const insertBeforeInstr = bailOutInfo->bailOutInstr;
  1507. Assert(insertBeforeInstr);
  1508. Func *const func = bailOutInfo->bailOutFunc;
  1509. // intermediateValue = left - right
  1510. IR::IntConstOpnd *offset =
  1511. loopCount->Offset() == 0 ? nullptr : IR::IntConstOpnd::New(loopCount->Offset(), TyInt32, func, true);
  1512. StackSym *const rightSym = loopCount->RightSym();
  1513. StackSym *intermediateValueSym;
  1514. if(rightSym)
  1515. {
  1516. IR::BailOutInstr *const instr = IR::BailOutInstr::New(Js::OpCode::Sub_I4, bailOutKind, bailOutInfo, func);
  1517. instr->SetSrc2(IR::RegOpnd::New(rightSym, rightSym->GetType(), func));
  1518. instr->GetSrc2()->SetIsJITOptimizedReg(true);
  1519. StackSym *const leftSym = loopCount->LeftSym();
  1520. if(leftSym)
  1521. {
  1522. // intermediateValue = left - right
  1523. instr->SetSrc1(IR::RegOpnd::New(leftSym, leftSym->GetType(), func));
  1524. instr->GetSrc1()->SetIsJITOptimizedReg(true);
  1525. }
  1526. else if(offset)
  1527. {
  1528. // intermediateValue = offset - right
  1529. instr->SetSrc1(offset);
  1530. offset = nullptr;
  1531. }
  1532. else
  1533. {
  1534. // intermediateValue = -right
  1535. instr->m_opcode = Js::OpCode::Neg_I4;
  1536. instr->SetSrc1(instr->UnlinkSrc2());
  1537. }
  1538. intermediateValueSym = StackSym::New(TyInt32, func);
  1539. instr->SetDst(IR::RegOpnd::New(intermediateValueSym, intermediateValueSym->GetType(), func));
  1540. instr->GetDst()->SetIsJITOptimizedReg(true);
  1541. instr->SetByteCodeOffset(insertBeforeInstr);
  1542. insertBeforeInstr->InsertBefore(instr);
  1543. }
  1544. else
  1545. {
  1546. // intermediateValue = left
  1547. Assert(loopCount->LeftSym());
  1548. intermediateValueSym = loopCount->LeftSym();
  1549. }
  1550. // intermediateValue += offset
  1551. if(offset)
  1552. {
  1553. IR::BailOutInstr *const instr = IR::BailOutInstr::New(Js::OpCode::Add_I4, bailOutKind, bailOutInfo, func);
  1554. instr->SetSrc1(IR::RegOpnd::New(intermediateValueSym, intermediateValueSym->GetType(), func));
  1555. instr->GetSrc1()->SetIsJITOptimizedReg(true);
  1556. if(offset->GetValue() < 0 && offset->GetValue() != IntConstMin)
  1557. {
  1558. instr->m_opcode = Js::OpCode::Sub_I4;
  1559. offset->SetValue(-offset->GetValue());
  1560. }
  1561. instr->SetSrc2(offset);
  1562. if(intermediateValueSym == loopCount->LeftSym())
  1563. {
  1564. intermediateValueSym = StackSym::New(TyInt32, func);
  1565. }
  1566. instr->SetDst(IR::RegOpnd::New(intermediateValueSym, intermediateValueSym->GetType(), func));
  1567. instr->GetDst()->SetIsJITOptimizedReg(true);
  1568. instr->SetByteCodeOffset(insertBeforeInstr);
  1569. insertBeforeInstr->InsertBefore(instr);
  1570. }
  1571. // intermediateValue /= minMagnitudeChange
  1572. const int minMagnitudeChange = loopCount->MinMagnitudeChange();
  1573. if(minMagnitudeChange != 1)
  1574. {
  1575. IR::Instr *const instr = IR::Instr::New(Js::OpCode::Div_I4, func);
  1576. instr->SetSrc1(IR::RegOpnd::New(intermediateValueSym, intermediateValueSym->GetType(), func));
  1577. instr->GetSrc1()->SetIsJITOptimizedReg(true);
  1578. Assert(minMagnitudeChange != 0); // bailout is not needed
  1579. instr->SetSrc2(IR::IntConstOpnd::New(minMagnitudeChange, TyInt32, func, true));
  1580. if(intermediateValueSym == loopCount->LeftSym())
  1581. {
  1582. intermediateValueSym = StackSym::New(TyInt32, func);
  1583. }
  1584. instr->SetDst(IR::RegOpnd::New(intermediateValueSym, intermediateValueSym->GetType(), func));
  1585. instr->GetDst()->SetIsJITOptimizedReg(true);
  1586. instr->SetByteCodeOffset(insertBeforeInstr);
  1587. insertBeforeInstr->InsertBefore(instr);
  1588. }
  1589. else
  1590. {
  1591. Assert(intermediateValueSym != loopCount->LeftSym());
  1592. }
  1593. // loopCountMinusOne = intermediateValue
  1594. loopCount->SetLoopCountMinusOneSym(intermediateValueSym);
  1595. }
  1596. void GlobOpt::GenerateLoopCountPlusOne(Loop *const loop, LoopCount *const loopCount)
  1597. {
  1598. Assert(loop);
  1599. Assert(loopCount);
  1600. Assert(loopCount == loop->loopCount);
  1601. if (loopCount->HasGeneratedLoopCountSym())
  1602. {
  1603. return;
  1604. }
  1605. if (!loopCount->HasBeenGenerated())
  1606. {
  1607. GenerateLoopCount(loop, loopCount);
  1608. }
  1609. Assert(loopCount->HasBeenGenerated());
  1610. // If this is null then the loop count is a constant and there is nothing more to do here
  1611. if (loopCount->LoopCountMinusOneSym())
  1612. {
  1613. // Prepare the landing pad for bailouts and instruction insertion
  1614. BailOutInfo *const bailOutInfo = loop->bailOutInfo;
  1615. Assert(bailOutInfo);
  1616. IR::Instr *const insertBeforeInstr = bailOutInfo->bailOutInstr;
  1617. Assert(insertBeforeInstr);
  1618. Func *const func = bailOutInfo->bailOutFunc;
  1619. IRType type = loopCount->LoopCountMinusOneSym()->GetType();
  1620. // loop count is off by one, so add one
  1621. IR::RegOpnd *loopCountOpnd = IR::RegOpnd::New(type, func);
  1622. IR::RegOpnd *minusOneOpnd = IR::RegOpnd::New(loopCount->LoopCountMinusOneSym(), type, func);
  1623. minusOneOpnd->SetIsJITOptimizedReg(true);
  1624. insertBeforeInstr->InsertBefore(IR::Instr::New(Js::OpCode::Add_I4,
  1625. loopCountOpnd,
  1626. minusOneOpnd,
  1627. IR::IntConstOpnd::New(1, type, func, true),
  1628. func));
  1629. loopCount->SetLoopCountSym(loopCountOpnd->GetStackSym());
  1630. }
  1631. }
  1632. void GlobOpt::GenerateSecondaryInductionVariableBound(
  1633. Loop *const loop,
  1634. StackSym *const inductionVariableSym,
  1635. const LoopCount *const loopCount,
  1636. const int maxMagnitudeChange,
  1637. StackSym *const boundSym)
  1638. {
  1639. Assert(loop);
  1640. Assert(inductionVariableSym);
  1641. Assert(inductionVariableSym->GetType() == TyInt32 || inductionVariableSym->GetType() == TyUint32);
  1642. Assert(loopCount);
  1643. Assert(loopCount == loop->loopCount);
  1644. Assert(loopCount->LoopCountMinusOneSym());
  1645. Assert(maxMagnitudeChange != 0);
  1646. Assert(maxMagnitudeChange >= -InductionVariable::ChangeMagnitudeLimitForLoopCountBasedHoisting);
  1647. Assert(maxMagnitudeChange <= InductionVariable::ChangeMagnitudeLimitForLoopCountBasedHoisting);
  1648. Assert(boundSym);
  1649. Assert(boundSym->IsInt32());
  1650. // bound = inductionVariable + loopCountMinusOne * maxMagnitudeChange
  1651. // Prepare the landing pad for bailouts and instruction insertion
  1652. BailOutInfo *const bailOutInfo = loop->bailOutInfo;
  1653. Assert(bailOutInfo);
  1654. const IR::BailOutKind bailOutKind = IR::BailOutOnFailedHoistedLoopCountBasedBoundCheck;
  1655. IR::Instr *const insertBeforeInstr = bailOutInfo->bailOutInstr;
  1656. Assert(insertBeforeInstr);
  1657. Func *const func = bailOutInfo->bailOutFunc;
  1658. // intermediateValue = loopCount * maxMagnitudeChange
  1659. StackSym *intermediateValueSym;
  1660. if(maxMagnitudeChange == 1 || maxMagnitudeChange == -1)
  1661. {
  1662. intermediateValueSym = loopCount->LoopCountMinusOneSym();
  1663. }
  1664. else
  1665. {
  1666. IR::BailOutInstr *const instr = IR::BailOutInstr::New(Js::OpCode::Mul_I4, bailOutKind, bailOutInfo, func);
  1667. instr->SetSrc1(
  1668. IR::RegOpnd::New(loopCount->LoopCountMinusOneSym(), loopCount->LoopCountMinusOneSym()->GetType(), func));
  1669. instr->GetSrc1()->SetIsJITOptimizedReg(true);
  1670. instr->SetSrc2(IR::IntConstOpnd::New(maxMagnitudeChange, TyInt32, func, true));
  1671. intermediateValueSym = boundSym;
  1672. instr->SetDst(IR::RegOpnd::New(intermediateValueSym, intermediateValueSym->GetType(), func));
  1673. instr->GetDst()->SetIsJITOptimizedReg(true);
  1674. instr->SetByteCodeOffset(insertBeforeInstr);
  1675. insertBeforeInstr->InsertBefore(instr);
  1676. }
  1677. // bound = intermediateValue + inductionVariable
  1678. {
  1679. IR::BailOutInstr *const instr = IR::BailOutInstr::New(Js::OpCode::Add_I4, bailOutKind, bailOutInfo, func);
  1680. instr->SetSrc1(IR::RegOpnd::New(intermediateValueSym, intermediateValueSym->GetType(), func));
  1681. instr->GetSrc1()->SetIsJITOptimizedReg(true);
  1682. instr->SetSrc2(IR::RegOpnd::New(inductionVariableSym, inductionVariableSym->GetType(), func));
  1683. instr->GetSrc2()->SetIsJITOptimizedReg(true);
  1684. if(maxMagnitudeChange == -1)
  1685. {
  1686. // bound = inductionVariable - intermediateValue[loopCount]
  1687. instr->m_opcode = Js::OpCode::Sub_I4;
  1688. instr->SwapOpnds();
  1689. }
  1690. instr->SetDst(IR::RegOpnd::New(boundSym, boundSym->GetType(), func));
  1691. instr->GetDst()->SetIsJITOptimizedReg(true);
  1692. instr->SetByteCodeOffset(insertBeforeInstr);
  1693. insertBeforeInstr->InsertBefore(instr);
  1694. }
  1695. }
  1696. void GlobOpt::DetermineArrayBoundCheckHoistability(
  1697. bool needLowerBoundCheck,
  1698. bool needUpperBoundCheck,
  1699. ArrayLowerBoundCheckHoistInfo &lowerHoistInfo,
  1700. ArrayUpperBoundCheckHoistInfo &upperHoistInfo,
  1701. const bool isJsArray,
  1702. StackSym *const indexSym,
  1703. Value *const indexValue,
  1704. const IntConstantBounds &indexConstantBounds,
  1705. StackSym *const headSegmentLengthSym,
  1706. Value *const headSegmentLengthValue,
  1707. const IntConstantBounds &headSegmentLengthConstantBounds,
  1708. Loop *const headSegmentLengthInvariantLoop,
  1709. bool &failedToUpdateCompatibleLowerBoundCheck,
  1710. bool &failedToUpdateCompatibleUpperBoundCheck)
  1711. {
  1712. Assert(DoBoundCheckHoist());
  1713. Assert(needLowerBoundCheck || needUpperBoundCheck);
  1714. Assert(!lowerHoistInfo.HasAnyInfo());
  1715. Assert(!upperHoistInfo.HasAnyInfo());
  1716. Assert(!indexSym == !indexValue);
  1717. Assert(!needUpperBoundCheck || headSegmentLengthSym);
  1718. Assert(!headSegmentLengthSym == !headSegmentLengthValue);
  1719. Assert(!failedToUpdateCompatibleLowerBoundCheck);
  1720. Assert(!failedToUpdateCompatibleUpperBoundCheck);
  1721. Loop *const currentLoop = currentBlock->loop;
  1722. if(!indexValue)
  1723. {
  1724. Assert(!needLowerBoundCheck);
  1725. Assert(needUpperBoundCheck);
  1726. Assert(indexConstantBounds.IsConstant());
  1727. // The index is a constant value, so a bound check on it can be hoisted as far as desired. Just find a compatible bound
  1728. // check that is already available, or the loop in which the head segment length is invariant.
  1729. TRACE_PHASE_VERBOSE(
  1730. Js::Phase::BoundCheckHoistPhase,
  1731. 2,
  1732. _u("Index is constant, looking for a compatible upper bound check\n"));
  1733. const int indexConstantValue = indexConstantBounds.LowerBound();
  1734. Assert(indexConstantValue != IntConstMax);
  1735. const IntBoundCheck *compatibleBoundCheck;
  1736. if(currentBlock->globOptData.availableIntBoundChecks->TryGetReference(
  1737. IntBoundCheckCompatibilityId(ZeroValueNumber, headSegmentLengthValue->GetValueNumber()),
  1738. &compatibleBoundCheck))
  1739. {
  1740. // We need:
  1741. // index < headSegmentLength
  1742. // Normalize the offset such that:
  1743. // 0 <= headSegmentLength + compatibleBoundCheckOffset
  1744. // Where (compatibleBoundCheckOffset = -1 - index), and -1 is to simulate < instead of <=.
  1745. const int compatibleBoundCheckOffset = -1 - indexConstantValue;
  1746. if(compatibleBoundCheck->SetBoundOffset(compatibleBoundCheckOffset))
  1747. {
  1748. TRACE_PHASE_VERBOSE(
  1749. Js::Phase::BoundCheckHoistPhase,
  1750. 3,
  1751. _u("Found in block %u\n"),
  1752. compatibleBoundCheck->Block()->GetBlockNum());
  1753. upperHoistInfo.SetCompatibleBoundCheck(compatibleBoundCheck->Block(), indexConstantValue);
  1754. return;
  1755. }
  1756. failedToUpdateCompatibleUpperBoundCheck = true;
  1757. }
  1758. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Not found\n"));
  1759. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 2, _u("Looking for invariant head segment length\n"));
  1760. Loop *invariantLoop;
  1761. Value *landingPadHeadSegmentLengthValue = nullptr;
  1762. if(headSegmentLengthInvariantLoop)
  1763. {
  1764. invariantLoop = headSegmentLengthInvariantLoop;
  1765. landingPadHeadSegmentLengthValue =
  1766. invariantLoop->landingPad->globOptData.FindValue(headSegmentLengthSym);
  1767. }
  1768. else if(currentLoop)
  1769. {
  1770. invariantLoop = nullptr;
  1771. for(Loop *loop = currentLoop; loop; loop = loop->parent)
  1772. {
  1773. GlobOptBlockData &landingPadBlockData = loop->landingPad->globOptData;
  1774. Value *const value = landingPadBlockData.FindValue(headSegmentLengthSym);
  1775. if(!value)
  1776. {
  1777. break;
  1778. }
  1779. invariantLoop = loop;
  1780. landingPadHeadSegmentLengthValue = value;
  1781. }
  1782. if(!invariantLoop)
  1783. {
  1784. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Not found\n"));
  1785. return;
  1786. }
  1787. }
  1788. else
  1789. {
  1790. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Not found, block is not in a loop\n"));
  1791. return;
  1792. }
  1793. TRACE_PHASE_VERBOSE(
  1794. Js::Phase::BoundCheckHoistPhase,
  1795. 3,
  1796. _u("Found in loop %u landing pad block %u\n"),
  1797. invariantLoop->GetLoopNumber(),
  1798. invariantLoop->landingPad->GetBlockNum());
  1799. IntConstantBounds landingPadHeadSegmentLengthConstantBounds;
  1800. AssertVerify(
  1801. landingPadHeadSegmentLengthValue
  1802. ->GetValueInfo()
  1803. ->TryGetIntConstantBounds(&landingPadHeadSegmentLengthConstantBounds));
  1804. if(isJsArray)
  1805. {
  1806. // index >= headSegmentLength is currently not possible for JS arrays (except when index == int32 max, which is
  1807. // covered above).
  1808. Assert(
  1809. !ValueInfo::IsGreaterThanOrEqualTo(
  1810. nullptr,
  1811. indexConstantValue,
  1812. indexConstantValue,
  1813. landingPadHeadSegmentLengthValue,
  1814. landingPadHeadSegmentLengthConstantBounds.LowerBound(),
  1815. landingPadHeadSegmentLengthConstantBounds.UpperBound()));
  1816. }
  1817. else if(
  1818. ValueInfo::IsGreaterThanOrEqualTo(
  1819. nullptr,
  1820. indexConstantValue,
  1821. indexConstantValue,
  1822. landingPadHeadSegmentLengthValue,
  1823. landingPadHeadSegmentLengthConstantBounds.LowerBound(),
  1824. landingPadHeadSegmentLengthConstantBounds.UpperBound()))
  1825. {
  1826. // index >= headSegmentLength in the landing pad, can't use the index sym. This is possible for typed arrays through
  1827. // conditions on array.length in user code.
  1828. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 2, _u("Index >= head segment length\n"));
  1829. return;
  1830. }
  1831. upperHoistInfo.SetLoop(
  1832. invariantLoop,
  1833. indexConstantValue,
  1834. landingPadHeadSegmentLengthValue,
  1835. landingPadHeadSegmentLengthConstantBounds);
  1836. return;
  1837. }
  1838. Assert(!indexConstantBounds.IsConstant());
  1839. ValueInfo *const indexValueInfo = indexValue->GetValueInfo();
  1840. const IntBounds *const indexBounds = indexValueInfo->IsIntBounded() ? indexValueInfo->AsIntBounded()->Bounds() : nullptr;
  1841. {
  1842. // See if a compatible bound check is already available
  1843. TRACE_PHASE_VERBOSE(
  1844. Js::Phase::BoundCheckHoistPhase,
  1845. 2,
  1846. _u("Looking for compatible bound checks for index bounds\n"));
  1847. bool searchingLower = needLowerBoundCheck;
  1848. bool searchingUpper = needUpperBoundCheck;
  1849. Assert(searchingLower || searchingUpper);
  1850. bool foundLowerBoundCheck = false;
  1851. const IntBoundCheck *lowerBoundCheck = nullptr;
  1852. ValueNumber lowerHoistBlockIndexValueNumber = InvalidValueNumber;
  1853. int lowerBoundOffset = 0;
  1854. if(searchingLower &&
  1855. currentBlock->globOptData.availableIntBoundChecks->TryGetReference(
  1856. IntBoundCheckCompatibilityId(ZeroValueNumber, indexValue->GetValueNumber()),
  1857. &lowerBoundCheck))
  1858. {
  1859. if(lowerBoundCheck->SetBoundOffset(0))
  1860. {
  1861. foundLowerBoundCheck = true;
  1862. lowerHoistBlockIndexValueNumber = indexValue->GetValueNumber();
  1863. lowerBoundOffset = 0;
  1864. searchingLower = false;
  1865. }
  1866. else
  1867. {
  1868. failedToUpdateCompatibleLowerBoundCheck = true;
  1869. }
  1870. }
  1871. bool foundUpperBoundCheck = false;
  1872. const IntBoundCheck *upperBoundCheck = nullptr;
  1873. ValueNumber upperHoistBlockIndexValueNumber = InvalidValueNumber;
  1874. int upperBoundOffset = 0;
  1875. if(searchingUpper &&
  1876. currentBlock->globOptData.availableIntBoundChecks->TryGetReference(
  1877. IntBoundCheckCompatibilityId(indexValue->GetValueNumber(), headSegmentLengthValue->GetValueNumber()),
  1878. &upperBoundCheck))
  1879. {
  1880. if(upperBoundCheck->SetBoundOffset(-1)) // -1 is to simulate < instead of <=
  1881. {
  1882. foundUpperBoundCheck = true;
  1883. upperHoistBlockIndexValueNumber = indexValue->GetValueNumber();
  1884. upperBoundOffset = 0;
  1885. searchingUpper = false;
  1886. }
  1887. else
  1888. {
  1889. failedToUpdateCompatibleUpperBoundCheck = true;
  1890. }
  1891. }
  1892. if(indexBounds)
  1893. {
  1894. searchingLower = searchingLower && indexBounds->RelativeLowerBounds().Count() != 0;
  1895. searchingUpper = searchingUpper && indexBounds->RelativeUpperBounds().Count() != 0;
  1896. if(searchingLower || searchingUpper)
  1897. {
  1898. for(auto it = currentBlock->globOptData.availableIntBoundChecks->GetIterator(); it.IsValid(); it.MoveNext())
  1899. {
  1900. const IntBoundCheck &boundCheck = it.CurrentValue();
  1901. if(searchingLower && boundCheck.LeftValueNumber() == ZeroValueNumber)
  1902. {
  1903. lowerHoistBlockIndexValueNumber = boundCheck.RightValueNumber();
  1904. const ValueRelativeOffset *bound;
  1905. if(indexBounds->RelativeLowerBounds().TryGetReference(lowerHoistBlockIndexValueNumber, &bound))
  1906. {
  1907. // We need:
  1908. // 0 <= boundBase + boundOffset
  1909. const int offset = bound->Offset();
  1910. if(boundCheck.SetBoundOffset(offset))
  1911. {
  1912. foundLowerBoundCheck = true;
  1913. lowerBoundCheck = &boundCheck;
  1914. lowerBoundOffset = offset;
  1915. searchingLower = false;
  1916. if(!searchingUpper)
  1917. {
  1918. break;
  1919. }
  1920. }
  1921. else
  1922. {
  1923. failedToUpdateCompatibleLowerBoundCheck = true;
  1924. }
  1925. }
  1926. }
  1927. if(searchingUpper && boundCheck.RightValueNumber() == headSegmentLengthValue->GetValueNumber())
  1928. {
  1929. upperHoistBlockIndexValueNumber = boundCheck.LeftValueNumber();
  1930. const ValueRelativeOffset *bound;
  1931. if(indexBounds->RelativeUpperBounds().TryGetReference(upperHoistBlockIndexValueNumber, &bound))
  1932. {
  1933. // We need:
  1934. // boundBase + boundOffset < headSegmentLength
  1935. // Normalize the offset such that:
  1936. // boundBase <= headSegmentLength + compatibleBoundCheckOffset
  1937. // Where (compatibleBoundCheckOffset = -1 - boundOffset), and -1 is to simulate < instead of <=.
  1938. const int offset = -1 - bound->Offset();
  1939. if(boundCheck.SetBoundOffset(offset))
  1940. {
  1941. foundUpperBoundCheck = true;
  1942. upperBoundCheck = &boundCheck;
  1943. upperBoundOffset = bound->Offset();
  1944. searchingUpper = false;
  1945. if(!searchingLower)
  1946. {
  1947. break;
  1948. }
  1949. }
  1950. else
  1951. {
  1952. failedToUpdateCompatibleUpperBoundCheck = true;
  1953. }
  1954. }
  1955. }
  1956. }
  1957. }
  1958. }
  1959. if(foundLowerBoundCheck)
  1960. {
  1961. // A bound check takes the form src1 <= src2 + dst
  1962. Assert(lowerBoundCheck->Instr()->GetSrc2());
  1963. Assert(
  1964. lowerBoundCheck->Instr()->GetSrc2()->AsRegOpnd()->m_sym->GetType() == TyInt32 ||
  1965. lowerBoundCheck->Instr()->GetSrc2()->AsRegOpnd()->m_sym->GetType() == TyUint32);
  1966. StackSym *boundCheckIndexSym = lowerBoundCheck->Instr()->GetSrc2()->AsRegOpnd()->m_sym;
  1967. if(boundCheckIndexSym->IsTypeSpec())
  1968. {
  1969. boundCheckIndexSym = boundCheckIndexSym->GetVarEquivSym(nullptr);
  1970. Assert(boundCheckIndexSym);
  1971. }
  1972. TRACE_PHASE_VERBOSE(
  1973. Js::Phase::BoundCheckHoistPhase,
  1974. 3,
  1975. _u("Found lower bound (s%u + %d) in block %u\n"),
  1976. boundCheckIndexSym->m_id,
  1977. lowerBoundOffset,
  1978. lowerBoundCheck->Block()->GetBlockNum());
  1979. lowerHoistInfo.SetCompatibleBoundCheck(
  1980. lowerBoundCheck->Block(),
  1981. boundCheckIndexSym,
  1982. lowerBoundOffset,
  1983. lowerHoistBlockIndexValueNumber);
  1984. Assert(!searchingLower);
  1985. needLowerBoundCheck = false;
  1986. if(!needUpperBoundCheck)
  1987. {
  1988. return;
  1989. }
  1990. }
  1991. if(foundUpperBoundCheck)
  1992. {
  1993. // A bound check takes the form src1 <= src2 + dst
  1994. Assert(upperBoundCheck->Instr()->GetSrc1());
  1995. Assert(
  1996. upperBoundCheck->Instr()->GetSrc1()->AsRegOpnd()->m_sym->GetType() == TyInt32 ||
  1997. upperBoundCheck->Instr()->GetSrc1()->AsRegOpnd()->m_sym->GetType() == TyUint32);
  1998. StackSym *boundCheckIndexSym = upperBoundCheck->Instr()->GetSrc1()->AsRegOpnd()->m_sym;
  1999. if(boundCheckIndexSym->IsTypeSpec())
  2000. {
  2001. boundCheckIndexSym = boundCheckIndexSym->GetVarEquivSym(nullptr);
  2002. Assert(boundCheckIndexSym);
  2003. }
  2004. TRACE_PHASE_VERBOSE(
  2005. Js::Phase::BoundCheckHoistPhase,
  2006. 3,
  2007. _u("Found upper bound (s%u + %d) in block %u\n"),
  2008. boundCheckIndexSym->m_id,
  2009. upperBoundOffset,
  2010. upperBoundCheck->Block()->GetBlockNum());
  2011. upperHoistInfo.SetCompatibleBoundCheck(
  2012. upperBoundCheck->Block(),
  2013. boundCheckIndexSym,
  2014. -1 - upperBoundOffset,
  2015. upperHoistBlockIndexValueNumber);
  2016. Assert(!searchingUpper);
  2017. needUpperBoundCheck = false;
  2018. if(!needLowerBoundCheck)
  2019. {
  2020. return;
  2021. }
  2022. }
  2023. Assert(needLowerBoundCheck || needUpperBoundCheck);
  2024. Assert(!needLowerBoundCheck || !lowerHoistInfo.CompatibleBoundCheckBlock());
  2025. Assert(!needUpperBoundCheck || !upperHoistInfo.CompatibleBoundCheckBlock());
  2026. }
  2027. if(!currentLoop)
  2028. {
  2029. return;
  2030. }
  2031. // Check if the index sym is invariant in the loop, or if the index value in the landing pad is a lower/upper bound of the
  2032. // index value in the current block
  2033. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 2, _u("Looking for invariant index or index bounded by itself\n"));
  2034. bool searchingLower = needLowerBoundCheck, searchingUpper = needUpperBoundCheck;
  2035. for(Loop *loop = currentLoop; loop; loop = loop->parent)
  2036. {
  2037. GlobOptBlockData &landingPadBlockData = loop->landingPad->globOptData;
  2038. TRACE_PHASE_VERBOSE(
  2039. Js::Phase::BoundCheckHoistPhase,
  2040. 3,
  2041. _u("Trying loop %u landing pad block %u\n"),
  2042. loop->GetLoopNumber(),
  2043. loop->landingPad->GetBlockNum());
  2044. Value *const landingPadIndexValue = landingPadBlockData.FindValue(indexSym);
  2045. if(!landingPadIndexValue)
  2046. {
  2047. break;
  2048. }
  2049. IntConstantBounds landingPadIndexConstantBounds;
  2050. const bool landingPadIndexValueIsLikelyInt =
  2051. landingPadIndexValue->GetValueInfo()->TryGetIntConstantBounds(&landingPadIndexConstantBounds, true);
  2052. int lowerOffset = 0, upperOffset = 0;
  2053. if(indexValue->GetValueNumber() == landingPadIndexValue->GetValueNumber())
  2054. {
  2055. Assert(landingPadIndexValueIsLikelyInt);
  2056. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Index is invariant\n"));
  2057. }
  2058. else
  2059. {
  2060. if(!landingPadIndexValueIsLikelyInt)
  2061. {
  2062. break;
  2063. }
  2064. if(searchingLower)
  2065. {
  2066. if(lowerHoistInfo.Loop() && indexValue->GetValueNumber() == lowerHoistInfo.IndexValueNumber())
  2067. {
  2068. // Prefer using the invariant sym
  2069. needLowerBoundCheck = searchingLower = false;
  2070. if(!needUpperBoundCheck)
  2071. {
  2072. return;
  2073. }
  2074. if(!searchingUpper)
  2075. {
  2076. break;
  2077. }
  2078. }
  2079. else
  2080. {
  2081. bool foundBound = false;
  2082. if(indexBounds)
  2083. {
  2084. const ValueRelativeOffset *bound;
  2085. if(indexBounds->RelativeLowerBounds().TryGetReference(landingPadIndexValue->GetValueNumber(), &bound))
  2086. {
  2087. foundBound = true;
  2088. lowerOffset = bound->Offset();
  2089. TRACE_PHASE_VERBOSE(
  2090. Js::Phase::BoundCheckHoistPhase,
  2091. 4,
  2092. _u("Found lower bound (index + %d)\n"),
  2093. lowerOffset);
  2094. }
  2095. }
  2096. if(!foundBound)
  2097. {
  2098. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Lower bound was not found\n"));
  2099. searchingLower = false;
  2100. if(!searchingUpper)
  2101. {
  2102. break;
  2103. }
  2104. }
  2105. }
  2106. }
  2107. if(searchingUpper)
  2108. {
  2109. if(upperHoistInfo.Loop() && indexValue->GetValueNumber() == upperHoistInfo.IndexValueNumber())
  2110. {
  2111. // Prefer using the invariant sym
  2112. needUpperBoundCheck = searchingUpper = false;
  2113. if(!needLowerBoundCheck)
  2114. {
  2115. return;
  2116. }
  2117. if(!searchingLower)
  2118. {
  2119. break;
  2120. }
  2121. }
  2122. else
  2123. {
  2124. bool foundBound = false;
  2125. if(indexBounds)
  2126. {
  2127. const ValueRelativeOffset *bound;
  2128. if(indexBounds->RelativeUpperBounds().TryGetReference(landingPadIndexValue->GetValueNumber(), &bound))
  2129. {
  2130. foundBound = true;
  2131. upperOffset = bound->Offset();
  2132. TRACE_PHASE_VERBOSE(
  2133. Js::Phase::BoundCheckHoistPhase,
  2134. 4,
  2135. _u("Found upper bound (index + %d)\n"),
  2136. upperOffset);
  2137. }
  2138. }
  2139. if(!foundBound)
  2140. {
  2141. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Upper bound was not found\n"));
  2142. searchingUpper = false;
  2143. if(!searchingLower)
  2144. {
  2145. break;
  2146. }
  2147. }
  2148. }
  2149. }
  2150. }
  2151. if(searchingLower)
  2152. {
  2153. if(ValueInfo::IsLessThan(
  2154. landingPadIndexValue,
  2155. landingPadIndexConstantBounds.LowerBound(),
  2156. landingPadIndexConstantBounds.UpperBound(),
  2157. nullptr,
  2158. 0,
  2159. 0))
  2160. {
  2161. // index < 0 in the landing pad; can't use the index sym
  2162. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 5, _u("Index < 0\n"));
  2163. searchingLower = false;
  2164. if(!searchingUpper)
  2165. {
  2166. break;
  2167. }
  2168. }
  2169. else
  2170. {
  2171. lowerHoistInfo.SetLoop(
  2172. loop,
  2173. indexSym,
  2174. lowerOffset,
  2175. landingPadIndexValue,
  2176. landingPadIndexConstantBounds);
  2177. }
  2178. }
  2179. if(!searchingUpper)
  2180. {
  2181. continue;
  2182. }
  2183. // Check if the head segment length sym is available in the landing pad
  2184. Value *const landingPadHeadSegmentLengthValue = landingPadBlockData.FindValue(headSegmentLengthSym);
  2185. if(!landingPadHeadSegmentLengthValue)
  2186. {
  2187. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 5, _u("Head segment length is not invariant\n"));
  2188. searchingUpper = false;
  2189. if(!searchingLower)
  2190. {
  2191. break;
  2192. }
  2193. continue;
  2194. }
  2195. IntConstantBounds landingPadHeadSegmentLengthConstantBounds;
  2196. AssertVerify(
  2197. landingPadHeadSegmentLengthValue
  2198. ->GetValueInfo()
  2199. ->TryGetIntConstantBounds(&landingPadHeadSegmentLengthConstantBounds));
  2200. if(ValueInfo::IsGreaterThanOrEqualTo(
  2201. landingPadIndexValue,
  2202. landingPadIndexConstantBounds.LowerBound(),
  2203. landingPadIndexConstantBounds.UpperBound(),
  2204. landingPadHeadSegmentLengthValue,
  2205. landingPadHeadSegmentLengthConstantBounds.LowerBound(),
  2206. landingPadHeadSegmentLengthConstantBounds.UpperBound()))
  2207. {
  2208. // index >= headSegmentLength in the landing pad; can't use the index sym
  2209. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 5, _u("Index >= head segment length\n"));
  2210. searchingUpper = false;
  2211. if(!searchingLower)
  2212. {
  2213. break;
  2214. }
  2215. continue;
  2216. }
  2217. // We need:
  2218. // boundBase + boundOffset < headSegmentLength
  2219. // Normalize the offset such that:
  2220. // boundBase <= headSegmentLength + offset
  2221. // Where (offset = -1 - boundOffset), and -1 is to simulate < instead of <=.
  2222. upperOffset = -1 - upperOffset;
  2223. upperHoistInfo.SetLoop(
  2224. loop,
  2225. indexSym,
  2226. upperOffset,
  2227. landingPadIndexValue,
  2228. landingPadIndexConstantBounds,
  2229. landingPadHeadSegmentLengthValue,
  2230. landingPadHeadSegmentLengthConstantBounds);
  2231. }
  2232. if(needLowerBoundCheck && lowerHoistInfo.Loop())
  2233. {
  2234. needLowerBoundCheck = false;
  2235. if(!needUpperBoundCheck)
  2236. {
  2237. return;
  2238. }
  2239. }
  2240. if(needUpperBoundCheck && upperHoistInfo.Loop())
  2241. {
  2242. needUpperBoundCheck = false;
  2243. if(!needLowerBoundCheck)
  2244. {
  2245. return;
  2246. }
  2247. }
  2248. // Find an invariant lower/upper bound of the index that can be used for hoisting the bound checks
  2249. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 2, _u("Looking for invariant index bounds\n"));
  2250. searchingLower = needLowerBoundCheck;
  2251. searchingUpper = needUpperBoundCheck;
  2252. for(Loop *loop = currentLoop; loop; loop = loop->parent)
  2253. {
  2254. GlobOptBlockData &landingPadBlockData = loop->landingPad->globOptData;
  2255. TRACE_PHASE_VERBOSE(
  2256. Js::Phase::BoundCheckHoistPhase,
  2257. 3,
  2258. _u("Trying loop %u landing pad block %u\n"),
  2259. loop->GetLoopNumber(),
  2260. loop->landingPad->GetBlockNum());
  2261. Value *landingPadHeadSegmentLengthValue = nullptr;
  2262. IntConstantBounds landingPadHeadSegmentLengthConstantBounds;
  2263. if(searchingUpper)
  2264. {
  2265. // Check if the head segment length sym is available in the landing pad
  2266. landingPadHeadSegmentLengthValue = landingPadBlockData.FindValue(headSegmentLengthSym);
  2267. if(landingPadHeadSegmentLengthValue)
  2268. {
  2269. AssertVerify(
  2270. landingPadHeadSegmentLengthValue
  2271. ->GetValueInfo()
  2272. ->TryGetIntConstantBounds(&landingPadHeadSegmentLengthConstantBounds));
  2273. }
  2274. else
  2275. {
  2276. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Head segment length is not invariant\n"));
  2277. searchingUpper = false;
  2278. if(!searchingLower)
  2279. {
  2280. break;
  2281. }
  2282. }
  2283. }
  2284. // Look for a relative bound
  2285. if(indexBounds)
  2286. {
  2287. for(int j = 0; j < 2; ++j)
  2288. {
  2289. const bool searchingRelativeLowerBounds = j == 0;
  2290. if(!(searchingRelativeLowerBounds ? searchingLower : searchingUpper))
  2291. {
  2292. continue;
  2293. }
  2294. for(auto it =
  2295. (
  2296. searchingRelativeLowerBounds
  2297. ? indexBounds->RelativeLowerBounds()
  2298. : indexBounds->RelativeUpperBounds()
  2299. ).GetIterator();
  2300. it.IsValid();
  2301. it.MoveNext())
  2302. {
  2303. const ValueRelativeOffset &indexBound = it.CurrentValue();
  2304. StackSym *const indexBoundBaseSym = indexBound.BaseSym();
  2305. if(!indexBoundBaseSym)
  2306. {
  2307. continue;
  2308. }
  2309. TRACE_PHASE_VERBOSE(
  2310. Js::Phase::BoundCheckHoistPhase,
  2311. 4,
  2312. _u("Found %S bound (s%u + %d)\n"),
  2313. searchingRelativeLowerBounds ? "lower" : "upper",
  2314. indexBoundBaseSym->m_id,
  2315. indexBound.Offset());
  2316. if(!indexBound.WasEstablishedExplicitly())
  2317. {
  2318. // Don't use a bound that was not established explicitly, as it may be too aggressive. For instance, an
  2319. // index sym used in an array will obtain an upper bound of the array's head segment length - 1. That
  2320. // bound is not established explicitly because the bound assertion is not enforced by the source code.
  2321. // Rather, it is assumed and enforced by the JIT using bailout check. Incrementing the index and using
  2322. // it in a different array may otherwise cause it to use the first array's head segment length as the
  2323. // upper bound on which to do the bound check against the second array, and that bound check would
  2324. // always fail when the arrays are the same size.
  2325. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 5, _u("Bound was established implicitly\n"));
  2326. continue;
  2327. }
  2328. Value *const landingPadIndexBoundBaseValue = landingPadBlockData.FindValue(indexBoundBaseSym);
  2329. if(!landingPadIndexBoundBaseValue ||
  2330. landingPadIndexBoundBaseValue->GetValueNumber() != indexBound.BaseValueNumber())
  2331. {
  2332. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 5, _u("Bound is not invariant\n"));
  2333. continue;
  2334. }
  2335. IntConstantBounds landingPadIndexBoundBaseConstantBounds;
  2336. AssertVerify(
  2337. landingPadIndexBoundBaseValue
  2338. ->GetValueInfo()
  2339. ->TryGetIntConstantBounds(&landingPadIndexBoundBaseConstantBounds, true));
  2340. int offset = indexBound.Offset();
  2341. if(searchingRelativeLowerBounds)
  2342. {
  2343. if(offset == IntConstMin ||
  2344. ValueInfo::IsLessThan(
  2345. landingPadIndexBoundBaseValue,
  2346. landingPadIndexBoundBaseConstantBounds.LowerBound(),
  2347. landingPadIndexBoundBaseConstantBounds.UpperBound(),
  2348. nullptr,
  2349. -offset,
  2350. -offset))
  2351. {
  2352. // indexBoundBase + indexBoundOffset < 0; can't use this bound
  2353. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 5, _u("Bound < 0\n"));
  2354. continue;
  2355. }
  2356. lowerHoistInfo.SetLoop(
  2357. loop,
  2358. indexBoundBaseSym,
  2359. offset,
  2360. landingPadIndexBoundBaseValue,
  2361. landingPadIndexBoundBaseConstantBounds);
  2362. break;
  2363. }
  2364. if(ValueInfo::IsLessThanOrEqualTo(
  2365. landingPadHeadSegmentLengthValue,
  2366. landingPadHeadSegmentLengthConstantBounds.LowerBound(),
  2367. landingPadHeadSegmentLengthConstantBounds.UpperBound(),
  2368. landingPadIndexBoundBaseValue,
  2369. landingPadIndexBoundBaseConstantBounds.LowerBound(),
  2370. landingPadIndexBoundBaseConstantBounds.UpperBound(),
  2371. offset))
  2372. {
  2373. // indexBoundBase + indexBoundOffset >= headSegmentLength; can't use this bound
  2374. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 5, _u("Bound >= head segment length\n"));
  2375. continue;
  2376. }
  2377. // We need:
  2378. // boundBase + boundOffset < headSegmentLength
  2379. // Normalize the offset such that:
  2380. // boundBase <= headSegmentLength + offset
  2381. // Where (offset = -1 - boundOffset), and -1 is to simulate < instead of <=.
  2382. offset = -1 - offset;
  2383. upperHoistInfo.SetLoop(
  2384. loop,
  2385. indexBoundBaseSym,
  2386. offset,
  2387. landingPadIndexBoundBaseValue,
  2388. landingPadIndexBoundBaseConstantBounds,
  2389. landingPadHeadSegmentLengthValue,
  2390. landingPadHeadSegmentLengthConstantBounds);
  2391. break;
  2392. }
  2393. }
  2394. }
  2395. if(searchingLower && lowerHoistInfo.Loop() != loop)
  2396. {
  2397. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Lower bound was not found\n"));
  2398. searchingLower = false;
  2399. if(!searchingUpper)
  2400. {
  2401. break;
  2402. }
  2403. }
  2404. if(searchingUpper && upperHoistInfo.Loop() != loop)
  2405. {
  2406. // No useful relative bound found; look for a constant bound if the index is an induction variable. Exclude constant
  2407. // bounds of non-induction variables because those bounds may have been established through means other than a loop
  2408. // exit condition, such as math or bitwise operations. Exclude constant bounds established implicitly by <,
  2409. // <=, >, and >=. For example, for a loop condition (i < n - 1), if 'n' is not invariant and hence can't be used,
  2410. // 'i' will still have a constant upper bound of (int32 max - 2) that should be excluded.
  2411. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Relative upper bound was not found\n"));
  2412. const InductionVariable *indexInductionVariable;
  2413. if(!upperHoistInfo.Loop() &&
  2414. currentLoop->inductionVariables &&
  2415. currentLoop->inductionVariables->TryGetReference(indexSym->m_id, &indexInductionVariable) &&
  2416. indexInductionVariable->IsChangeDeterminate())
  2417. {
  2418. if(!(indexBounds && indexBounds->WasConstantUpperBoundEstablishedExplicitly()))
  2419. {
  2420. TRACE_PHASE_VERBOSE(
  2421. Js::Phase::BoundCheckHoistPhase,
  2422. 4,
  2423. _u("Constant upper bound was established implicitly\n"));
  2424. }
  2425. else
  2426. {
  2427. // See if a compatible bound check is already available
  2428. const int indexConstantBound = indexBounds->ConstantUpperBound();
  2429. TRACE_PHASE_VERBOSE(
  2430. Js::Phase::BoundCheckHoistPhase,
  2431. 4,
  2432. _u("Found constant upper bound %d, looking for a compatible bound check\n"),
  2433. indexConstantBound);
  2434. const IntBoundCheck *boundCheck;
  2435. if(currentBlock->globOptData.availableIntBoundChecks->TryGetReference(
  2436. IntBoundCheckCompatibilityId(ZeroValueNumber, headSegmentLengthValue->GetValueNumber()),
  2437. &boundCheck))
  2438. {
  2439. // We need:
  2440. // indexConstantBound < headSegmentLength
  2441. // Normalize the offset such that:
  2442. // 0 <= headSegmentLength + compatibleBoundCheckOffset
  2443. // Where (compatibleBoundCheckOffset = -1 - indexConstantBound), and -1 is to simulate < instead of <=.
  2444. const int compatibleBoundCheckOffset = -1 - indexConstantBound;
  2445. if(boundCheck->SetBoundOffset(compatibleBoundCheckOffset))
  2446. {
  2447. TRACE_PHASE_VERBOSE(
  2448. Js::Phase::BoundCheckHoistPhase,
  2449. 5,
  2450. _u("Found in block %u\n"),
  2451. boundCheck->Block()->GetBlockNum());
  2452. upperHoistInfo.SetCompatibleBoundCheck(boundCheck->Block(), indexConstantBound);
  2453. needUpperBoundCheck = searchingUpper = false;
  2454. if(!needLowerBoundCheck)
  2455. {
  2456. return;
  2457. }
  2458. if(!searchingLower)
  2459. {
  2460. break;
  2461. }
  2462. }
  2463. else
  2464. {
  2465. failedToUpdateCompatibleUpperBoundCheck = true;
  2466. }
  2467. }
  2468. if(searchingUpper)
  2469. {
  2470. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 5, _u("Not found\n"));
  2471. upperHoistInfo.SetLoop(
  2472. loop,
  2473. indexConstantBound,
  2474. landingPadHeadSegmentLengthValue,
  2475. landingPadHeadSegmentLengthConstantBounds);
  2476. }
  2477. }
  2478. }
  2479. else if(!upperHoistInfo.Loop())
  2480. {
  2481. TRACE_PHASE_VERBOSE(
  2482. Js::Phase::BoundCheckHoistPhase,
  2483. 4,
  2484. _u("Index is not an induction variable, not using constant upper bound\n"));
  2485. }
  2486. if(searchingUpper && upperHoistInfo.Loop() != loop)
  2487. {
  2488. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Upper bound was not found\n"));
  2489. searchingUpper = false;
  2490. if(!searchingLower)
  2491. {
  2492. break;
  2493. }
  2494. }
  2495. }
  2496. }
  2497. if(needLowerBoundCheck && lowerHoistInfo.Loop())
  2498. {
  2499. needLowerBoundCheck = false;
  2500. if(!needUpperBoundCheck)
  2501. {
  2502. return;
  2503. }
  2504. }
  2505. if(needUpperBoundCheck && upperHoistInfo.Loop())
  2506. {
  2507. needUpperBoundCheck = false;
  2508. if(!needLowerBoundCheck)
  2509. {
  2510. return;
  2511. }
  2512. }
  2513. // Try to use the loop count to calculate a missing lower/upper bound that in turn can be used for hoisting a bound check
  2514. TRACE_PHASE_VERBOSE(
  2515. Js::Phase::BoundCheckHoistPhase,
  2516. 2,
  2517. _u("Looking for loop count based bound for loop %u landing pad block %u\n"),
  2518. currentLoop->GetLoopNumber(),
  2519. currentLoop->landingPad->GetBlockNum());
  2520. LoopCount *const loopCount = currentLoop->loopCount;
  2521. if(!loopCount)
  2522. {
  2523. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Loop was not counted\n"));
  2524. return;
  2525. }
  2526. const InductionVariable *indexInductionVariable;
  2527. if(!currentLoop->inductionVariables ||
  2528. !currentLoop->inductionVariables->TryGetReference(indexSym->m_id, &indexInductionVariable) ||
  2529. !indexInductionVariable->IsChangeDeterminate())
  2530. {
  2531. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Index is not an induction variable\n"));
  2532. return;
  2533. }
  2534. // Determine the maximum-magnitude change per iteration, and verify that the change is reasonably finite
  2535. Assert(indexInductionVariable->IsChangeUnidirectional());
  2536. GlobOptBlockData &landingPadBlockData = currentLoop->landingPad->globOptData;
  2537. int maxMagnitudeChange = indexInductionVariable->ChangeBounds().UpperBound();
  2538. Value *landingPadHeadSegmentLengthValue;
  2539. IntConstantBounds landingPadHeadSegmentLengthConstantBounds;
  2540. if(maxMagnitudeChange > 0)
  2541. {
  2542. TRACE_PHASE_VERBOSE(
  2543. Js::Phase::BoundCheckHoistPhase,
  2544. 3,
  2545. _u("Index's maximum-magnitude change per iteration is %d\n"),
  2546. maxMagnitudeChange);
  2547. if(!needUpperBoundCheck || maxMagnitudeChange > InductionVariable::ChangeMagnitudeLimitForLoopCountBasedHoisting)
  2548. {
  2549. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Change magnitude is too large\n"));
  2550. return;
  2551. }
  2552. // Check whether the head segment length is available in the landing pad
  2553. landingPadHeadSegmentLengthValue = landingPadBlockData.FindValue(headSegmentLengthSym);
  2554. Assert(!headSegmentLengthInvariantLoop || landingPadHeadSegmentLengthValue);
  2555. if(!landingPadHeadSegmentLengthValue)
  2556. {
  2557. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Head segment length is not invariant\n"));
  2558. return;
  2559. }
  2560. AssertVerify(
  2561. landingPadHeadSegmentLengthValue
  2562. ->GetValueInfo()
  2563. ->TryGetIntConstantBounds(&landingPadHeadSegmentLengthConstantBounds));
  2564. }
  2565. else
  2566. {
  2567. maxMagnitudeChange = indexInductionVariable->ChangeBounds().LowerBound();
  2568. Assert(maxMagnitudeChange < 0);
  2569. TRACE_PHASE_VERBOSE(
  2570. Js::Phase::BoundCheckHoistPhase,
  2571. 3,
  2572. _u("Index's maximum-magnitude change per iteration is %d\n"),
  2573. maxMagnitudeChange);
  2574. if(!needLowerBoundCheck || maxMagnitudeChange < -InductionVariable::ChangeMagnitudeLimitForLoopCountBasedHoisting)
  2575. {
  2576. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Change magnitude is too large\n"));
  2577. return;
  2578. }
  2579. landingPadHeadSegmentLengthValue = nullptr;
  2580. }
  2581. // Determine if the index already changed in the loop, and by how much
  2582. int indexOffset = 0;
  2583. StackSym *indexSymToAdd = nullptr;
  2584. SymBoundType symBoundType = DetermineSymBoundOffsetOrValueRelativeToLandingPad(
  2585. indexSym,
  2586. maxMagnitudeChange >= 0,
  2587. indexValueInfo,
  2588. indexBounds,
  2589. &currentLoop->landingPad->globOptData,
  2590. &indexOffset);
  2591. if (symBoundType == SymBoundType::VALUE)
  2592. {
  2593. // The bound value is constant
  2594. indexSymToAdd = nullptr;
  2595. }
  2596. else if (symBoundType == SymBoundType::OFFSET)
  2597. {
  2598. // The bound value is not constant, the offset needs to be added to the index sym in the landing pad
  2599. indexSymToAdd = indexSym;
  2600. }
  2601. else
  2602. {
  2603. Assert(symBoundType == SymBoundType::UNKNOWN);
  2604. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Unable to determine the sym bound offset or value\n"));
  2605. return;
  2606. }
  2607. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Index's offset from landing pad is %d\n"), indexOffset);
  2608. // The secondary induction variable bound is computed as follows:
  2609. // bound = index + indexOffset + loopCountMinusOne * maxMagnitudeChange
  2610. //
  2611. // If the loop count is constant, (inductionVariableOffset + loopCount * maxMagnitudeChange) can be folded into an offset:
  2612. // bound = index + offset
  2613. int offset;
  2614. StackSym *indexLoopCountBasedBoundBaseSym = nullptr;
  2615. Value *indexLoopCountBasedBoundBaseValue = nullptr;
  2616. IntConstantBounds indexLoopCountBasedBoundBaseConstantBounds;
  2617. bool generateLoopCountBasedIndexBound;
  2618. if(!loopCount->HasBeenGenerated() || loopCount->LoopCountMinusOneSym())
  2619. {
  2620. if(loopCount->HasBeenGenerated())
  2621. {
  2622. TRACE_PHASE_VERBOSE(
  2623. Js::Phase::BoundCheckHoistPhase,
  2624. 3,
  2625. _u("Loop count is assigned to s%u\n"),
  2626. loopCount->LoopCountMinusOneSym()->m_id);
  2627. }
  2628. else
  2629. {
  2630. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Loop count has not been generated yet\n"));
  2631. }
  2632. offset = indexOffset;
  2633. // Check if there is already a loop count based bound sym for the index. If not, generate it.
  2634. do
  2635. {
  2636. const SymID indexSymId = indexSym->m_id;
  2637. SymIdToStackSymMap *&loopCountBasedBoundBaseSyms = currentLoop->loopCountBasedBoundBaseSyms;
  2638. if(!loopCountBasedBoundBaseSyms)
  2639. {
  2640. loopCountBasedBoundBaseSyms = JitAnew(alloc, SymIdToStackSymMap, alloc);
  2641. }
  2642. else if(loopCountBasedBoundBaseSyms->TryGetValue(indexSymId, &indexLoopCountBasedBoundBaseSym))
  2643. {
  2644. TRACE_PHASE_VERBOSE(
  2645. Js::Phase::BoundCheckHoistPhase,
  2646. 3,
  2647. _u("Loop count based bound is assigned to s%u\n"),
  2648. indexLoopCountBasedBoundBaseSym->m_id);
  2649. indexLoopCountBasedBoundBaseValue = landingPadBlockData.FindValue(indexLoopCountBasedBoundBaseSym);
  2650. Assert(indexLoopCountBasedBoundBaseValue);
  2651. AssertVerify(
  2652. indexLoopCountBasedBoundBaseValue
  2653. ->GetValueInfo()
  2654. ->TryGetIntConstantBounds(&indexLoopCountBasedBoundBaseConstantBounds));
  2655. generateLoopCountBasedIndexBound = false;
  2656. break;
  2657. }
  2658. indexLoopCountBasedBoundBaseSym = StackSym::New(TyInt32, func);
  2659. TRACE_PHASE_VERBOSE(
  2660. Js::Phase::BoundCheckHoistPhase,
  2661. 3,
  2662. _u("Assigning s%u to the loop count based bound\n"),
  2663. indexLoopCountBasedBoundBaseSym->m_id);
  2664. loopCountBasedBoundBaseSyms->Add(indexSymId, indexLoopCountBasedBoundBaseSym);
  2665. indexLoopCountBasedBoundBaseValue = NewValue(ValueInfo::New(alloc, ValueType::GetInt(true)));
  2666. landingPadBlockData.SetValue(indexLoopCountBasedBoundBaseValue, indexLoopCountBasedBoundBaseSym);
  2667. indexLoopCountBasedBoundBaseConstantBounds = IntConstantBounds(IntConstMin, IntConstMax);
  2668. generateLoopCountBasedIndexBound = true;
  2669. } while(false);
  2670. }
  2671. else
  2672. {
  2673. // The loop count is constant, fold (indexOffset + loopCountMinusOne * maxMagnitudeChange)
  2674. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Loop count is constant, folding\n"));
  2675. if(Int32Math::Mul(loopCount->LoopCountMinusOneConstantValue(), maxMagnitudeChange, &offset) ||
  2676. Int32Math::Add(offset, indexOffset, &offset))
  2677. {
  2678. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Folding failed\n"));
  2679. return;
  2680. }
  2681. if(!indexSymToAdd)
  2682. {
  2683. // The loop count based bound is constant
  2684. const int loopCountBasedConstantBound = offset;
  2685. TRACE_PHASE_VERBOSE(
  2686. Js::Phase::BoundCheckHoistPhase,
  2687. 3,
  2688. _u("Loop count based bound is constant: %d\n"),
  2689. loopCountBasedConstantBound);
  2690. if(maxMagnitudeChange < 0)
  2691. {
  2692. if(loopCountBasedConstantBound < 0)
  2693. {
  2694. // Can't use this bound
  2695. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Bound < 0\n"));
  2696. return;
  2697. }
  2698. lowerHoistInfo.SetLoop(currentLoop, loopCountBasedConstantBound, true);
  2699. return;
  2700. }
  2701. // loopCountBasedConstantBound >= headSegmentLength is currently not possible, except when
  2702. // loopCountBasedConstantBound == int32 max
  2703. Assert(
  2704. loopCountBasedConstantBound == IntConstMax ||
  2705. !ValueInfo::IsGreaterThanOrEqualTo(
  2706. nullptr,
  2707. loopCountBasedConstantBound,
  2708. loopCountBasedConstantBound,
  2709. landingPadHeadSegmentLengthValue,
  2710. landingPadHeadSegmentLengthConstantBounds.LowerBound(),
  2711. landingPadHeadSegmentLengthConstantBounds.UpperBound()));
  2712. // See if a compatible bound check is already available
  2713. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Looking for a compatible bound check\n"));
  2714. const IntBoundCheck *boundCheck;
  2715. if(currentBlock->globOptData.availableIntBoundChecks->TryGetReference(
  2716. IntBoundCheckCompatibilityId(ZeroValueNumber, headSegmentLengthValue->GetValueNumber()),
  2717. &boundCheck))
  2718. {
  2719. // We need:
  2720. // loopCountBasedConstantBound < headSegmentLength
  2721. // Normalize the offset such that:
  2722. // 0 <= headSegmentLength + compatibleBoundCheckOffset
  2723. // Where (compatibleBoundCheckOffset = -1 - loopCountBasedConstantBound), and -1 is to simulate < instead of <=.
  2724. const int compatibleBoundCheckOffset = -1 - loopCountBasedConstantBound;
  2725. if(boundCheck->SetBoundOffset(compatibleBoundCheckOffset, true))
  2726. {
  2727. TRACE_PHASE_VERBOSE(
  2728. Js::Phase::BoundCheckHoistPhase,
  2729. 4,
  2730. _u("Found in block %u\n"),
  2731. boundCheck->Block()->GetBlockNum());
  2732. upperHoistInfo.SetCompatibleBoundCheck(boundCheck->Block(), loopCountBasedConstantBound);
  2733. return;
  2734. }
  2735. failedToUpdateCompatibleUpperBoundCheck = true;
  2736. }
  2737. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Not found\n"));
  2738. upperHoistInfo.SetLoop(
  2739. currentLoop,
  2740. loopCountBasedConstantBound,
  2741. landingPadHeadSegmentLengthValue,
  2742. landingPadHeadSegmentLengthConstantBounds,
  2743. true);
  2744. return;
  2745. }
  2746. // The loop count based bound is not constant; we need to add the offset of the index sym in the landing pad. Instead
  2747. // of adding though, we will treat the index sym as the loop count based bound base sym and adjust the offset that will
  2748. // be used in the bound check itself.
  2749. indexLoopCountBasedBoundBaseSym = indexSymToAdd;
  2750. indexLoopCountBasedBoundBaseValue = landingPadBlockData.FindValue(indexSymToAdd);
  2751. Assert(indexLoopCountBasedBoundBaseValue);
  2752. AssertVerify(
  2753. indexLoopCountBasedBoundBaseValue
  2754. ->GetValueInfo()
  2755. ->TryGetIntConstantBounds(&indexLoopCountBasedBoundBaseConstantBounds));
  2756. generateLoopCountBasedIndexBound = false;
  2757. }
  2758. if(maxMagnitudeChange >= 0)
  2759. {
  2760. // We need:
  2761. // indexLoopCountBasedBoundBase + indexOffset < headSegmentLength
  2762. // Normalize the offset such that:
  2763. // indexLoopCountBasedBoundBase <= headSegmentLength + offset
  2764. // Where (offset = -1 - indexOffset), and -1 is to simulate < instead of <=.
  2765. offset = -1 - offset;
  2766. }
  2767. if(!generateLoopCountBasedIndexBound)
  2768. {
  2769. if(maxMagnitudeChange < 0)
  2770. {
  2771. if(offset != IntConstMax &&
  2772. ValueInfo::IsGreaterThanOrEqualTo(
  2773. nullptr,
  2774. 0,
  2775. 0,
  2776. indexLoopCountBasedBoundBaseValue,
  2777. indexLoopCountBasedBoundBaseConstantBounds.LowerBound(),
  2778. indexLoopCountBasedBoundBaseConstantBounds.UpperBound(),
  2779. offset + 1)) // + 1 to simulate > instead of >=
  2780. {
  2781. // loopCountBasedBound < 0, can't use this bound
  2782. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Bound < 0\n"));
  2783. return;
  2784. }
  2785. }
  2786. else if(
  2787. ValueInfo::IsGreaterThanOrEqualTo(
  2788. indexLoopCountBasedBoundBaseValue,
  2789. indexLoopCountBasedBoundBaseConstantBounds.LowerBound(),
  2790. indexLoopCountBasedBoundBaseConstantBounds.UpperBound(),
  2791. landingPadHeadSegmentLengthValue,
  2792. landingPadHeadSegmentLengthConstantBounds.LowerBound(),
  2793. landingPadHeadSegmentLengthConstantBounds.UpperBound(),
  2794. offset))
  2795. {
  2796. // loopCountBasedBound >= headSegmentLength, can't use this bound
  2797. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Bound >= head segment length\n"));
  2798. return;
  2799. }
  2800. // See if a compatible bound check is already available
  2801. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 3, _u("Looking for a compatible bound check\n"));
  2802. const ValueNumber indexLoopCountBasedBoundBaseValueNumber = indexLoopCountBasedBoundBaseValue->GetValueNumber();
  2803. const IntBoundCheck *boundCheck;
  2804. if(currentBlock->globOptData.availableIntBoundChecks->TryGetReference(
  2805. maxMagnitudeChange < 0
  2806. ? IntBoundCheckCompatibilityId(
  2807. ZeroValueNumber,
  2808. indexLoopCountBasedBoundBaseValueNumber)
  2809. : IntBoundCheckCompatibilityId(
  2810. indexLoopCountBasedBoundBaseValueNumber,
  2811. headSegmentLengthValue->GetValueNumber()),
  2812. &boundCheck))
  2813. {
  2814. if(boundCheck->SetBoundOffset(offset, true))
  2815. {
  2816. TRACE_PHASE_VERBOSE(
  2817. Js::Phase::BoundCheckHoistPhase,
  2818. 4,
  2819. _u("Found in block %u\n"),
  2820. boundCheck->Block()->GetBlockNum());
  2821. if(maxMagnitudeChange < 0)
  2822. {
  2823. lowerHoistInfo.SetCompatibleBoundCheck(
  2824. boundCheck->Block(),
  2825. indexLoopCountBasedBoundBaseSym,
  2826. offset,
  2827. indexLoopCountBasedBoundBaseValueNumber);
  2828. }
  2829. else
  2830. {
  2831. upperHoistInfo.SetCompatibleBoundCheck(
  2832. boundCheck->Block(),
  2833. indexLoopCountBasedBoundBaseSym,
  2834. offset,
  2835. indexLoopCountBasedBoundBaseValueNumber);
  2836. }
  2837. return;
  2838. }
  2839. (maxMagnitudeChange < 0 ? failedToUpdateCompatibleLowerBoundCheck : failedToUpdateCompatibleUpperBoundCheck) = true;
  2840. }
  2841. TRACE_PHASE_VERBOSE(Js::Phase::BoundCheckHoistPhase, 4, _u("Not found\n"));
  2842. }
  2843. if(maxMagnitudeChange < 0)
  2844. {
  2845. lowerHoistInfo.SetLoop(
  2846. currentLoop,
  2847. indexLoopCountBasedBoundBaseSym,
  2848. offset,
  2849. indexLoopCountBasedBoundBaseValue,
  2850. indexLoopCountBasedBoundBaseConstantBounds,
  2851. true);
  2852. if(generateLoopCountBasedIndexBound)
  2853. {
  2854. lowerHoistInfo.SetLoopCount(loopCount, maxMagnitudeChange);
  2855. }
  2856. return;
  2857. }
  2858. upperHoistInfo.SetLoop(
  2859. currentLoop,
  2860. indexLoopCountBasedBoundBaseSym,
  2861. offset,
  2862. indexLoopCountBasedBoundBaseValue,
  2863. indexLoopCountBasedBoundBaseConstantBounds,
  2864. landingPadHeadSegmentLengthValue,
  2865. landingPadHeadSegmentLengthConstantBounds,
  2866. true);
  2867. if(generateLoopCountBasedIndexBound)
  2868. {
  2869. upperHoistInfo.SetLoopCount(loopCount, maxMagnitudeChange);
  2870. }
  2871. }