GlobOptIntBounds.cpp 128 KB

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