DFGFixupPhase.cpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*
  2. * Copyright (C) 2012, 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "DFGFixupPhase.h"
  27. #if ENABLE(DFG_JIT)
  28. #include "DFGGraph.h"
  29. #include "DFGInsertionSet.h"
  30. #include "DFGPhase.h"
  31. #include "DFGPredictionPropagationPhase.h"
  32. #include "DFGVariableAccessDataDump.h"
  33. #include "Operations.h"
  34. namespace JSC { namespace DFG {
  35. class FixupPhase : public Phase {
  36. public:
  37. FixupPhase(Graph& graph)
  38. : Phase(graph, "fixup")
  39. , m_insertionSet(graph)
  40. {
  41. }
  42. bool run()
  43. {
  44. ASSERT(m_graph.m_fixpointState == BeforeFixpoint);
  45. ASSERT(m_graph.m_form == ThreadedCPS);
  46. m_profitabilityChanged = false;
  47. for (BlockIndex blockIndex = 0; blockIndex < m_graph.m_blocks.size(); ++blockIndex)
  48. fixupBlock(m_graph.m_blocks[blockIndex].get());
  49. while (m_profitabilityChanged) {
  50. m_profitabilityChanged = false;
  51. for (unsigned i = m_graph.m_argumentPositions.size(); i--;)
  52. m_graph.m_argumentPositions[i].mergeArgumentUnboxingAwareness();
  53. for (BlockIndex blockIndex = 0; blockIndex < m_graph.m_blocks.size(); ++blockIndex)
  54. fixupSetLocalsInBlock(m_graph.m_blocks[blockIndex].get());
  55. }
  56. return true;
  57. }
  58. private:
  59. void fixupBlock(BasicBlock* block)
  60. {
  61. if (!block)
  62. return;
  63. ASSERT(block->isReachable);
  64. m_block = block;
  65. for (m_indexInBlock = 0; m_indexInBlock < block->size(); ++m_indexInBlock) {
  66. m_currentNode = block->at(m_indexInBlock);
  67. fixupNode(m_currentNode);
  68. }
  69. m_insertionSet.execute(block);
  70. }
  71. void fixupNode(Node* node)
  72. {
  73. NodeType op = node->op();
  74. #if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
  75. dataLogF(" %s @%u: ", Graph::opName(op), node->index());
  76. #endif
  77. switch (op) {
  78. case SetLocal: {
  79. // This gets handled by fixupSetLocalsInBlock().
  80. break;
  81. }
  82. case BitAnd:
  83. case BitOr:
  84. case BitXor:
  85. case BitRShift:
  86. case BitLShift:
  87. case BitURShift:
  88. case ArithIMul: {
  89. fixIntEdge(node->child1());
  90. fixIntEdge(node->child2());
  91. break;
  92. }
  93. case UInt32ToNumber: {
  94. setUseKindAndUnboxIfProfitable<KnownInt32Use>(node->child1());
  95. break;
  96. }
  97. case DoubleAsInt32: {
  98. RELEASE_ASSERT_NOT_REACHED();
  99. break;
  100. }
  101. case ValueToInt32: {
  102. if (node->child1()->shouldSpeculateInteger()) {
  103. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  104. break;
  105. }
  106. if (node->child1()->shouldSpeculateNumber()) {
  107. setUseKindAndUnboxIfProfitable<NumberUse>(node->child1());
  108. break;
  109. }
  110. if (node->child1()->shouldSpeculateBoolean()) {
  111. setUseKindAndUnboxIfProfitable<BooleanUse>(node->child1());
  112. break;
  113. }
  114. setUseKindAndUnboxIfProfitable<NotCellUse>(node->child1());
  115. break;
  116. }
  117. case Int32ToDouble: {
  118. RELEASE_ASSERT_NOT_REACHED();
  119. break;
  120. }
  121. case ValueAdd: {
  122. if (attemptToMakeIntegerAdd(node))
  123. break;
  124. if (Node::shouldSpeculateNumberExpectingDefined(node->child1().node(), node->child2().node())) {
  125. fixDoubleEdge<NumberUse>(node->child1());
  126. fixDoubleEdge<NumberUse>(node->child2());
  127. break;
  128. }
  129. // FIXME: Optimize for the case where one of the operands is the
  130. // empty string. Also consider optimizing for the case where we don't
  131. // believe either side is the emtpy string. Both of these things should
  132. // be easy.
  133. if (node->child1()->shouldSpeculateString()
  134. && attemptToMakeFastStringAdd<StringUse>(node, node->child1(), node->child2()))
  135. break;
  136. if (node->child2()->shouldSpeculateString()
  137. && attemptToMakeFastStringAdd<StringUse>(node, node->child2(), node->child1()))
  138. break;
  139. if (node->child1()->shouldSpeculateStringObject()
  140. && attemptToMakeFastStringAdd<StringObjectUse>(node, node->child1(), node->child2()))
  141. break;
  142. if (node->child2()->shouldSpeculateStringObject()
  143. && attemptToMakeFastStringAdd<StringObjectUse>(node, node->child2(), node->child1()))
  144. break;
  145. if (node->child1()->shouldSpeculateStringOrStringObject()
  146. && attemptToMakeFastStringAdd<StringOrStringObjectUse>(node, node->child1(), node->child2()))
  147. break;
  148. if (node->child2()->shouldSpeculateStringOrStringObject()
  149. && attemptToMakeFastStringAdd<StringOrStringObjectUse>(node, node->child2(), node->child1()))
  150. break;
  151. break;
  152. }
  153. case MakeRope: {
  154. fixupMakeRope(node);
  155. break;
  156. }
  157. case ArithAdd:
  158. case ArithSub: {
  159. if (attemptToMakeIntegerAdd(node))
  160. break;
  161. fixDoubleEdge<NumberUse>(node->child1());
  162. fixDoubleEdge<NumberUse>(node->child2());
  163. break;
  164. }
  165. case ArithNegate: {
  166. if (m_graph.negateShouldSpeculateInteger(node)) {
  167. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  168. break;
  169. }
  170. fixDoubleEdge<NumberUse>(node->child1());
  171. break;
  172. }
  173. case ArithMul: {
  174. if (m_graph.mulShouldSpeculateInteger(node)) {
  175. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  176. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  177. break;
  178. }
  179. fixDoubleEdge<NumberUse>(node->child1());
  180. fixDoubleEdge<NumberUse>(node->child2());
  181. break;
  182. }
  183. case ArithDiv: {
  184. if (Node::shouldSpeculateIntegerForArithmetic(node->child1().node(), node->child2().node())
  185. && node->canSpeculateInteger()) {
  186. if (isX86() || isARMv7s()) {
  187. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  188. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  189. break;
  190. }
  191. injectInt32ToDoubleNode(node->child1());
  192. injectInt32ToDoubleNode(node->child2());
  193. // We don't need to do ref'ing on the children because we're stealing them from
  194. // the original division.
  195. Node* newDivision = m_insertionSet.insertNode(
  196. m_indexInBlock, SpecDouble, *node);
  197. node->setOp(DoubleAsInt32);
  198. node->children.initialize(Edge(newDivision, KnownNumberUse), Edge(), Edge());
  199. break;
  200. }
  201. fixDoubleEdge<NumberUse>(node->child1());
  202. fixDoubleEdge<NumberUse>(node->child2());
  203. break;
  204. }
  205. case ArithMin:
  206. case ArithMax:
  207. case ArithMod: {
  208. if (Node::shouldSpeculateIntegerForArithmetic(node->child1().node(), node->child2().node())
  209. && node->canSpeculateInteger()) {
  210. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  211. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  212. break;
  213. }
  214. fixDoubleEdge<NumberUse>(node->child1());
  215. fixDoubleEdge<NumberUse>(node->child2());
  216. break;
  217. }
  218. case ArithAbs: {
  219. if (node->child1()->shouldSpeculateIntegerForArithmetic()
  220. && node->canSpeculateInteger()) {
  221. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  222. break;
  223. }
  224. fixDoubleEdge<NumberUse>(node->child1());
  225. break;
  226. }
  227. case ArithSqrt: {
  228. fixDoubleEdge<NumberUse>(node->child1());
  229. break;
  230. }
  231. case LogicalNot: {
  232. if (node->child1()->shouldSpeculateBoolean())
  233. setUseKindAndUnboxIfProfitable<BooleanUse>(node->child1());
  234. else if (node->child1()->shouldSpeculateObjectOrOther())
  235. setUseKindAndUnboxIfProfitable<ObjectOrOtherUse>(node->child1());
  236. else if (node->child1()->shouldSpeculateInteger())
  237. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  238. else if (node->child1()->shouldSpeculateNumber())
  239. fixDoubleEdge<NumberUse>(node->child1());
  240. break;
  241. }
  242. case TypeOf: {
  243. if (node->child1()->shouldSpeculateString())
  244. setUseKindAndUnboxIfProfitable<StringUse>(node->child1());
  245. else if (node->child1()->shouldSpeculateCell())
  246. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  247. break;
  248. }
  249. case CompareEqConstant: {
  250. break;
  251. }
  252. case CompareEq:
  253. case CompareLess:
  254. case CompareLessEq:
  255. case CompareGreater:
  256. case CompareGreaterEq: {
  257. if (Node::shouldSpeculateInteger(node->child1().node(), node->child2().node())) {
  258. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  259. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  260. break;
  261. }
  262. if (Node::shouldSpeculateNumber(node->child1().node(), node->child2().node())) {
  263. fixDoubleEdge<NumberUse>(node->child1());
  264. fixDoubleEdge<NumberUse>(node->child2());
  265. break;
  266. }
  267. if (node->op() != CompareEq)
  268. break;
  269. if (Node::shouldSpeculateBoolean(node->child1().node(), node->child2().node())) {
  270. setUseKindAndUnboxIfProfitable<BooleanUse>(node->child1());
  271. setUseKindAndUnboxIfProfitable<BooleanUse>(node->child2());
  272. break;
  273. }
  274. if (node->child1()->shouldSpeculateString() && node->child2()->shouldSpeculateString() && GPRInfo::numberOfRegisters >= 7) {
  275. setUseKindAndUnboxIfProfitable<StringUse>(node->child1());
  276. setUseKindAndUnboxIfProfitable<StringUse>(node->child2());
  277. break;
  278. }
  279. if (node->child1()->shouldSpeculateObject() && node->child2()->shouldSpeculateObject()) {
  280. setUseKindAndUnboxIfProfitable<ObjectUse>(node->child1());
  281. setUseKindAndUnboxIfProfitable<ObjectUse>(node->child2());
  282. break;
  283. }
  284. if (node->child1()->shouldSpeculateObject() && node->child2()->shouldSpeculateObjectOrOther()) {
  285. setUseKindAndUnboxIfProfitable<ObjectUse>(node->child1());
  286. setUseKindAndUnboxIfProfitable<ObjectOrOtherUse>(node->child2());
  287. break;
  288. }
  289. if (node->child1()->shouldSpeculateObjectOrOther() && node->child2()->shouldSpeculateObject()) {
  290. setUseKindAndUnboxIfProfitable<ObjectOrOtherUse>(node->child1());
  291. setUseKindAndUnboxIfProfitable<ObjectUse>(node->child2());
  292. break;
  293. }
  294. break;
  295. }
  296. case CompareStrictEqConstant: {
  297. break;
  298. }
  299. case CompareStrictEq: {
  300. if (Node::shouldSpeculateBoolean(node->child1().node(), node->child2().node())) {
  301. setUseKindAndUnboxIfProfitable<BooleanUse>(node->child1());
  302. setUseKindAndUnboxIfProfitable<BooleanUse>(node->child2());
  303. break;
  304. }
  305. if (Node::shouldSpeculateInteger(node->child1().node(), node->child2().node())) {
  306. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  307. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  308. break;
  309. }
  310. if (Node::shouldSpeculateNumber(node->child1().node(), node->child2().node())) {
  311. fixDoubleEdge<NumberUse>(node->child1());
  312. fixDoubleEdge<NumberUse>(node->child2());
  313. break;
  314. }
  315. if (node->child1()->shouldSpeculateString() && node->child2()->shouldSpeculateString() && GPRInfo::numberOfRegisters >= 7) {
  316. setUseKindAndUnboxIfProfitable<StringUse>(node->child1());
  317. setUseKindAndUnboxIfProfitable<StringUse>(node->child2());
  318. break;
  319. }
  320. if (node->child1()->shouldSpeculateObject() && node->child2()->shouldSpeculateObject()) {
  321. setUseKindAndUnboxIfProfitable<ObjectUse>(node->child1());
  322. setUseKindAndUnboxIfProfitable<ObjectUse>(node->child2());
  323. break;
  324. }
  325. break;
  326. }
  327. case StringFromCharCode:
  328. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  329. break;
  330. case StringCharAt:
  331. case StringCharCodeAt: {
  332. // Currently we have no good way of refining these.
  333. ASSERT(node->arrayMode() == ArrayMode(Array::String));
  334. blessArrayOperation(node->child1(), node->child2(), node->child3());
  335. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child1());
  336. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  337. break;
  338. }
  339. case GetByVal: {
  340. node->setArrayMode(
  341. node->arrayMode().refine(
  342. node->child1()->prediction(),
  343. node->child2()->prediction(),
  344. SpecNone, node->flags()));
  345. blessArrayOperation(node->child1(), node->child2(), node->child3());
  346. ArrayMode arrayMode = node->arrayMode();
  347. if (arrayMode.type() == Array::Double
  348. && arrayMode.arrayClass() == Array::OriginalArray
  349. && arrayMode.speculation() == Array::InBounds
  350. && arrayMode.conversion() == Array::AsIs
  351. && m_graph.globalObjectFor(node->codeOrigin)->arrayPrototypeChainIsSane()
  352. && !(node->flags() & NodeUsedAsOther))
  353. node->setArrayMode(arrayMode.withSpeculation(Array::SaneChain));
  354. switch (node->arrayMode().type()) {
  355. case Array::SelectUsingPredictions:
  356. case Array::Unprofiled:
  357. case Array::Undecided:
  358. RELEASE_ASSERT_NOT_REACHED();
  359. break;
  360. case Array::Generic:
  361. #if USE(JSVALUE32_64)
  362. setUseKindAndUnboxIfProfitable<CellUse>(node->child1()); // Speculating cell due to register pressure on 32-bit.
  363. #endif
  364. break;
  365. case Array::ForceExit:
  366. break;
  367. default:
  368. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child1());
  369. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  370. break;
  371. }
  372. break;
  373. }
  374. case PutByVal:
  375. case PutByValAlias: {
  376. Edge& child1 = m_graph.varArgChild(node, 0);
  377. Edge& child2 = m_graph.varArgChild(node, 1);
  378. Edge& child3 = m_graph.varArgChild(node, 2);
  379. node->setArrayMode(
  380. node->arrayMode().refine(
  381. child1->prediction(),
  382. child2->prediction(),
  383. child3->prediction()));
  384. blessArrayOperation(child1, child2, m_graph.varArgChild(node, 3));
  385. switch (node->arrayMode().modeForPut().type()) {
  386. case Array::SelectUsingPredictions:
  387. case Array::Unprofiled:
  388. case Array::Undecided:
  389. RELEASE_ASSERT_NOT_REACHED();
  390. break;
  391. case Array::ForceExit:
  392. case Array::Generic:
  393. #if USE(JSVALUE32_64)
  394. // Due to register pressure on 32-bit, we speculate cell and
  395. // ignore the base-is-not-cell case entirely by letting the
  396. // baseline JIT handle it.
  397. setUseKindAndUnboxIfProfitable<CellUse>(child1);
  398. #endif
  399. break;
  400. case Array::Int32:
  401. setUseKindAndUnboxIfProfitable<KnownCellUse>(child1);
  402. setUseKindAndUnboxIfProfitable<Int32Use>(child2);
  403. setUseKindAndUnboxIfProfitable<Int32Use>(child3);
  404. break;
  405. case Array::Double:
  406. setUseKindAndUnboxIfProfitable<KnownCellUse>(child1);
  407. setUseKindAndUnboxIfProfitable<Int32Use>(child2);
  408. fixDoubleEdge<RealNumberUse>(child3);
  409. break;
  410. case Array::Int8Array:
  411. case Array::Int16Array:
  412. case Array::Int32Array:
  413. case Array::Uint8Array:
  414. case Array::Uint8ClampedArray:
  415. case Array::Uint16Array:
  416. case Array::Uint32Array:
  417. setUseKindAndUnboxIfProfitable<KnownCellUse>(child1);
  418. setUseKindAndUnboxIfProfitable<Int32Use>(child2);
  419. if (child3->shouldSpeculateInteger())
  420. setUseKindAndUnboxIfProfitable<Int32Use>(child3);
  421. else
  422. fixDoubleEdge<NumberUse>(child3);
  423. break;
  424. case Array::Float32Array:
  425. case Array::Float64Array:
  426. setUseKindAndUnboxIfProfitable<KnownCellUse>(child1);
  427. setUseKindAndUnboxIfProfitable<Int32Use>(child2);
  428. fixDoubleEdge<NumberUse>(child3);
  429. break;
  430. default:
  431. setUseKindAndUnboxIfProfitable<KnownCellUse>(child1);
  432. setUseKindAndUnboxIfProfitable<Int32Use>(child2);
  433. break;
  434. }
  435. break;
  436. }
  437. case ArrayPush: {
  438. // May need to refine the array mode in case the value prediction contravenes
  439. // the array prediction. For example, we may have evidence showing that the
  440. // array is in Int32 mode, but the value we're storing is likely to be a double.
  441. // Then we should turn this into a conversion to Double array followed by the
  442. // push. On the other hand, we absolutely don't want to refine based on the
  443. // base prediction. If it has non-cell garbage in it, then we want that to be
  444. // ignored. That's because ArrayPush can't handle any array modes that aren't
  445. // array-related - so if refine() turned this into a "Generic" ArrayPush then
  446. // that would break things.
  447. node->setArrayMode(
  448. node->arrayMode().refine(
  449. node->child1()->prediction() & SpecCell,
  450. SpecInt32,
  451. node->child2()->prediction()));
  452. blessArrayOperation(node->child1(), Edge(), node->child3());
  453. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child1());
  454. switch (node->arrayMode().type()) {
  455. case Array::Int32:
  456. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  457. break;
  458. case Array::Double:
  459. fixDoubleEdge<RealNumberUse>(node->child2());
  460. break;
  461. default:
  462. break;
  463. }
  464. break;
  465. }
  466. case ArrayPop: {
  467. blessArrayOperation(node->child1(), Edge(), node->child2());
  468. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child1());
  469. break;
  470. }
  471. case RegExpExec:
  472. case RegExpTest: {
  473. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  474. setUseKindAndUnboxIfProfitable<CellUse>(node->child2());
  475. break;
  476. }
  477. case Branch: {
  478. if (node->child1()->shouldSpeculateBoolean())
  479. setUseKindAndUnboxIfProfitable<BooleanUse>(node->child1());
  480. else if (node->child1()->shouldSpeculateObjectOrOther())
  481. setUseKindAndUnboxIfProfitable<ObjectOrOtherUse>(node->child1());
  482. else if (node->child1()->shouldSpeculateInteger())
  483. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  484. else if (node->child1()->shouldSpeculateNumber())
  485. fixDoubleEdge<NumberUse>(node->child1());
  486. Node* logicalNot = node->child1().node();
  487. if (logicalNot->op() == LogicalNot) {
  488. // Make sure that OSR exit can't observe the LogicalNot. If it can,
  489. // then we must compute it and cannot peephole around it.
  490. bool found = false;
  491. bool ok = true;
  492. for (unsigned i = m_indexInBlock; i--;) {
  493. Node* candidate = m_block->at(i);
  494. if (candidate == logicalNot) {
  495. found = true;
  496. break;
  497. }
  498. if (candidate->canExit()) {
  499. ok = false;
  500. found = true;
  501. break;
  502. }
  503. }
  504. ASSERT_UNUSED(found, found);
  505. if (ok) {
  506. Edge newChildEdge = logicalNot->child1();
  507. if (newChildEdge->hasBooleanResult()) {
  508. node->children.setChild1(newChildEdge);
  509. BlockIndex toBeTaken = node->notTakenBlockIndex();
  510. BlockIndex toBeNotTaken = node->takenBlockIndex();
  511. node->setTakenBlockIndex(toBeTaken);
  512. node->setNotTakenBlockIndex(toBeNotTaken);
  513. }
  514. }
  515. }
  516. break;
  517. }
  518. case ToPrimitive: {
  519. fixupToPrimitive(node);
  520. break;
  521. }
  522. case ToString: {
  523. fixupToString(node);
  524. break;
  525. }
  526. case NewStringObject: {
  527. setUseKindAndUnboxIfProfitable<KnownStringUse>(node->child1());
  528. break;
  529. }
  530. case NewArray: {
  531. for (unsigned i = m_graph.varArgNumChildren(node); i--;) {
  532. node->setIndexingType(
  533. leastUpperBoundOfIndexingTypeAndType(
  534. node->indexingType(), m_graph.varArgChild(node, i)->prediction()));
  535. }
  536. switch (node->indexingType()) {
  537. case ALL_BLANK_INDEXING_TYPES:
  538. CRASH();
  539. break;
  540. case ALL_UNDECIDED_INDEXING_TYPES:
  541. if (node->numChildren()) {
  542. // This will only happen if the children have no type predictions. We
  543. // would have already exited by now, but insert a forced exit just to
  544. // be safe.
  545. m_insertionSet.insertNode(
  546. m_indexInBlock, SpecNone, ForceOSRExit, node->codeOrigin);
  547. }
  548. break;
  549. case ALL_INT32_INDEXING_TYPES:
  550. for (unsigned operandIndex = 0; operandIndex < node->numChildren(); ++operandIndex)
  551. setUseKindAndUnboxIfProfitable<Int32Use>(m_graph.m_varArgChildren[node->firstChild() + operandIndex]);
  552. break;
  553. case ALL_DOUBLE_INDEXING_TYPES:
  554. for (unsigned operandIndex = 0; operandIndex < node->numChildren(); ++operandIndex)
  555. setUseKindAndUnboxIfProfitable<RealNumberUse>(m_graph.m_varArgChildren[node->firstChild() + operandIndex]);
  556. break;
  557. case ALL_CONTIGUOUS_INDEXING_TYPES:
  558. case ALL_ARRAY_STORAGE_INDEXING_TYPES:
  559. break;
  560. default:
  561. CRASH();
  562. break;
  563. }
  564. break;
  565. }
  566. case NewArrayWithSize: {
  567. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  568. break;
  569. }
  570. case ConvertThis: {
  571. if (isOtherSpeculation(node->child1()->prediction())) {
  572. m_insertionSet.insertNode(
  573. m_indexInBlock, SpecNone, Phantom, node->codeOrigin,
  574. Edge(node->child1().node(), OtherUse));
  575. observeUseKindOnNode<OtherUse>(node->child1().node());
  576. node->convertToWeakConstant(m_graph.globalThisObjectFor(node->codeOrigin));
  577. break;
  578. }
  579. if (isObjectSpeculation(node->child1()->prediction())) {
  580. setUseKindAndUnboxIfProfitable<ObjectUse>(node->child1());
  581. node->convertToIdentity();
  582. break;
  583. }
  584. break;
  585. }
  586. case CreateThis: {
  587. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  588. break;
  589. }
  590. case GetMyArgumentByVal:
  591. case GetMyArgumentByValSafe: {
  592. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  593. break;
  594. }
  595. case GetScopeRegisters:
  596. case PutScopedVar:
  597. case SkipTopScope:
  598. case SkipScope:
  599. case SetCallee:
  600. case SetMyScope:
  601. case PutStructure:
  602. case AllocatePropertyStorage:
  603. case ReallocatePropertyStorage:
  604. case GetScope:
  605. case GetButterfly: {
  606. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child1());
  607. break;
  608. }
  609. case GetById: {
  610. if (!node->child1()->shouldSpeculateCell())
  611. break;
  612. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  613. if (!isInt32Speculation(node->prediction()))
  614. break;
  615. if (codeBlock()->identifier(node->identifierNumber()) != vm().propertyNames->length)
  616. break;
  617. ArrayProfile* arrayProfile =
  618. m_graph.baselineCodeBlockFor(node->codeOrigin)->getArrayProfile(
  619. node->codeOrigin.bytecodeIndex);
  620. ArrayMode arrayMode = ArrayMode(Array::SelectUsingPredictions);
  621. if (arrayProfile) {
  622. arrayProfile->computeUpdatedPrediction(m_graph.baselineCodeBlockFor(node->codeOrigin));
  623. arrayMode = ArrayMode::fromObserved(arrayProfile, Array::Read, false);
  624. arrayMode = arrayMode.refine(
  625. node->child1()->prediction(), node->prediction());
  626. if (arrayMode.supportsLength() && arrayProfile->hasDefiniteStructure()) {
  627. m_insertionSet.insertNode(
  628. m_indexInBlock, SpecNone, CheckStructure, node->codeOrigin,
  629. OpInfo(m_graph.addStructureSet(arrayProfile->expectedStructure())),
  630. node->child1());
  631. }
  632. } else
  633. arrayMode = arrayMode.refine(node->child1()->prediction(), node->prediction());
  634. if (arrayMode.type() == Array::Generic) {
  635. // Check if the input is something that we can't get array length for, but for which we
  636. // could insert some conversions in order to transform it into something that we can do it
  637. // for.
  638. if (node->child1()->shouldSpeculateStringObject())
  639. attemptToForceStringArrayModeByToStringConversion<StringObjectUse>(arrayMode, node);
  640. else if (node->child1()->shouldSpeculateStringOrStringObject())
  641. attemptToForceStringArrayModeByToStringConversion<StringOrStringObjectUse>(arrayMode, node);
  642. }
  643. if (!arrayMode.supportsLength())
  644. break;
  645. node->setOp(GetArrayLength);
  646. ASSERT(node->flags() & NodeMustGenerate);
  647. node->clearFlags(NodeMustGenerate | NodeClobbersWorld);
  648. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child1());
  649. node->setArrayMode(arrayMode);
  650. Node* storage = checkArray(arrayMode, node->codeOrigin, node->child1().node(), 0, lengthNeedsStorage);
  651. if (!storage)
  652. break;
  653. node->child2() = Edge(storage);
  654. break;
  655. }
  656. case GetByIdFlush: {
  657. if (node->child1()->shouldSpeculateCell())
  658. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  659. break;
  660. }
  661. case CheckExecutable:
  662. case CheckStructure:
  663. case ForwardCheckStructure:
  664. case StructureTransitionWatchpoint:
  665. case ForwardStructureTransitionWatchpoint:
  666. case CheckFunction:
  667. case PutById:
  668. case PutByIdDirect:
  669. case CheckHasInstance: {
  670. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  671. break;
  672. }
  673. case CheckArray: {
  674. switch (node->arrayMode().type()) {
  675. case Array::String:
  676. setUseKindAndUnboxIfProfitable<StringUse>(node->child1());
  677. break;
  678. default:
  679. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  680. break;
  681. }
  682. break;
  683. }
  684. case Arrayify:
  685. case ArrayifyToStructure: {
  686. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  687. if (node->child2())
  688. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  689. break;
  690. }
  691. case GetByOffset: {
  692. if (!node->child1()->hasStorageResult())
  693. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child1());
  694. break;
  695. }
  696. case PutByOffset: {
  697. if (!node->child1()->hasStorageResult())
  698. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child1());
  699. setUseKindAndUnboxIfProfitable<KnownCellUse>(node->child2());
  700. break;
  701. }
  702. case InstanceOf: {
  703. // FIXME: This appears broken: CheckHasInstance already does an unconditional cell
  704. // check. https://bugs.webkit.org/show_bug.cgi?id=107479
  705. if (!(node->child1()->prediction() & ~SpecCell))
  706. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  707. setUseKindAndUnboxIfProfitable<CellUse>(node->child2());
  708. break;
  709. }
  710. case Phantom:
  711. case Identity: {
  712. switch (node->child1().useKind()) {
  713. case NumberUse:
  714. if (node->child1()->shouldSpeculateIntegerForArithmetic())
  715. node->child1().setUseKind(Int32Use);
  716. break;
  717. default:
  718. break;
  719. }
  720. observeUseKindOnEdge(node->child1());
  721. break;
  722. }
  723. case GetArrayLength:
  724. case Nop:
  725. case Phi:
  726. case ForwardInt32ToDouble:
  727. case PhantomPutStructure:
  728. case GetIndexedPropertyStorage:
  729. case LastNodeType:
  730. case MovHint:
  731. case MovHintAndCheck:
  732. case ZombieHint:
  733. RELEASE_ASSERT_NOT_REACHED();
  734. break;
  735. #if !ASSERT_DISABLED
  736. // Have these no-op cases here to ensure that nobody forgets to add handlers for new opcodes.
  737. case SetArgument:
  738. case JSConstant:
  739. case WeakJSConstant:
  740. case GetLocal:
  741. case GetCallee:
  742. case Flush:
  743. case PhantomLocal:
  744. case GetLocalUnlinked:
  745. case InlineStart:
  746. case GetMyScope:
  747. case GetScopedVar:
  748. case GetGlobalVar:
  749. case PutGlobalVar:
  750. case GlobalVarWatchpoint:
  751. case PutGlobalVarCheck:
  752. case AllocationProfileWatchpoint:
  753. case Call:
  754. case Construct:
  755. case NewObject:
  756. case NewArrayBuffer:
  757. case NewRegexp:
  758. case Resolve:
  759. case ResolveBase:
  760. case ResolveBaseStrictPut:
  761. case ResolveGlobal:
  762. case Breakpoint:
  763. case IsUndefined:
  764. case IsBoolean:
  765. case IsNumber:
  766. case IsString:
  767. case IsObject:
  768. case IsFunction:
  769. case CreateActivation:
  770. case TearOffActivation:
  771. case CreateArguments:
  772. case PhantomArguments:
  773. case TearOffArguments:
  774. case GetMyArgumentsLength:
  775. case GetMyArgumentsLengthSafe:
  776. case CheckArgumentsNotCreated:
  777. case NewFunction:
  778. case NewFunctionNoCheck:
  779. case NewFunctionExpression:
  780. case Jump:
  781. case Return:
  782. case Throw:
  783. case ThrowReferenceError:
  784. case GarbageValue:
  785. case CountExecution:
  786. case ForceOSRExit:
  787. case CheckWatchdogTimer:
  788. break;
  789. #else
  790. default:
  791. break;
  792. #endif
  793. }
  794. #if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
  795. if (!(node->flags() & NodeHasVarArgs)) {
  796. dataLogF("new children: ");
  797. node->dumpChildren(WTF::dataFile());
  798. }
  799. dataLogF("\n");
  800. #endif
  801. }
  802. template<UseKind useKind>
  803. void createToString(Node* node, Edge& edge)
  804. {
  805. edge.setNode(m_insertionSet.insertNode(
  806. m_indexInBlock, SpecString, ToString, node->codeOrigin,
  807. Edge(edge.node(), useKind)));
  808. }
  809. template<UseKind useKind>
  810. void attemptToForceStringArrayModeByToStringConversion(ArrayMode& arrayMode, Node* node)
  811. {
  812. ASSERT(arrayMode == ArrayMode(Array::Generic));
  813. if (!canOptimizeStringObjectAccess(node->codeOrigin))
  814. return;
  815. createToString<useKind>(node, node->child1());
  816. arrayMode = ArrayMode(Array::String);
  817. }
  818. template<UseKind useKind>
  819. bool isStringObjectUse()
  820. {
  821. switch (useKind) {
  822. case StringObjectUse:
  823. case StringOrStringObjectUse:
  824. return true;
  825. default:
  826. return false;
  827. }
  828. }
  829. template<UseKind useKind>
  830. void convertStringAddUse(Node* node, Edge& edge)
  831. {
  832. if (useKind == StringUse) {
  833. // This preserves the binaryUseKind() invariant ot ValueAdd: ValueAdd's
  834. // two edges will always have identical use kinds, which makes the
  835. // decision process much easier.
  836. observeUseKindOnNode<StringUse>(edge.node());
  837. m_insertionSet.insertNode(
  838. m_indexInBlock, SpecNone, Phantom, node->codeOrigin,
  839. Edge(edge.node(), StringUse));
  840. edge.setUseKind(KnownStringUse);
  841. return;
  842. }
  843. // FIXME: We ought to be able to have a ToPrimitiveToString node.
  844. observeUseKindOnNode<useKind>(edge.node());
  845. createToString<useKind>(node, edge);
  846. }
  847. void convertToMakeRope(Node* node)
  848. {
  849. node->setOpAndDefaultFlags(MakeRope);
  850. fixupMakeRope(node);
  851. }
  852. void fixupMakeRope(Node* node)
  853. {
  854. for (unsigned i = 0; i < AdjacencyList::Size; ++i) {
  855. Edge& edge = node->children.child(i);
  856. if (!edge)
  857. break;
  858. edge.setUseKind(KnownStringUse);
  859. if (!m_graph.isConstant(edge.node()))
  860. continue;
  861. JSString* string = jsCast<JSString*>(m_graph.valueOfJSConstant(edge.node()).asCell());
  862. if (string->length())
  863. continue;
  864. // Don't allow the MakeRope to have zero children.
  865. if (!i && !node->child2())
  866. break;
  867. node->children.removeEdge(i--);
  868. }
  869. if (!node->child2()) {
  870. ASSERT(!node->child3());
  871. node->convertToIdentity();
  872. }
  873. }
  874. void fixupToPrimitive(Node* node)
  875. {
  876. if (node->child1()->shouldSpeculateInteger()) {
  877. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  878. node->convertToIdentity();
  879. return;
  880. }
  881. if (node->child1()->shouldSpeculateString()) {
  882. setUseKindAndUnboxIfProfitable<StringUse>(node->child1());
  883. node->convertToIdentity();
  884. return;
  885. }
  886. if (node->child1()->shouldSpeculateStringObject()
  887. && canOptimizeStringObjectAccess(node->codeOrigin)) {
  888. setUseKindAndUnboxIfProfitable<StringObjectUse>(node->child1());
  889. node->convertToToString();
  890. return;
  891. }
  892. if (node->child1()->shouldSpeculateStringOrStringObject()
  893. && canOptimizeStringObjectAccess(node->codeOrigin)) {
  894. setUseKindAndUnboxIfProfitable<StringOrStringObjectUse>(node->child1());
  895. node->convertToToString();
  896. return;
  897. }
  898. }
  899. void fixupToString(Node* node)
  900. {
  901. if (node->child1()->shouldSpeculateString()) {
  902. setUseKindAndUnboxIfProfitable<StringUse>(node->child1());
  903. node->convertToIdentity();
  904. return;
  905. }
  906. if (node->child1()->shouldSpeculateStringObject()
  907. && canOptimizeStringObjectAccess(node->codeOrigin)) {
  908. setUseKindAndUnboxIfProfitable<StringObjectUse>(node->child1());
  909. return;
  910. }
  911. if (node->child1()->shouldSpeculateStringOrStringObject()
  912. && canOptimizeStringObjectAccess(node->codeOrigin)) {
  913. setUseKindAndUnboxIfProfitable<StringOrStringObjectUse>(node->child1());
  914. return;
  915. }
  916. if (node->child1()->shouldSpeculateCell()) {
  917. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  918. return;
  919. }
  920. }
  921. template<UseKind leftUseKind>
  922. bool attemptToMakeFastStringAdd(Node* node, Edge& left, Edge& right)
  923. {
  924. Node* originalLeft = left.node();
  925. Node* originalRight = right.node();
  926. ASSERT(leftUseKind == StringUse || leftUseKind == StringObjectUse || leftUseKind == StringOrStringObjectUse);
  927. if (isStringObjectUse<leftUseKind>() && !canOptimizeStringObjectAccess(node->codeOrigin))
  928. return false;
  929. convertStringAddUse<leftUseKind>(node, left);
  930. if (right->shouldSpeculateString())
  931. convertStringAddUse<StringUse>(node, right);
  932. else if (right->shouldSpeculateStringObject() && canOptimizeStringObjectAccess(node->codeOrigin))
  933. convertStringAddUse<StringObjectUse>(node, right);
  934. else if (right->shouldSpeculateStringOrStringObject() && canOptimizeStringObjectAccess(node->codeOrigin))
  935. convertStringAddUse<StringOrStringObjectUse>(node, right);
  936. else {
  937. // At this point we know that the other operand is something weird. The semantically correct
  938. // way of dealing with this is:
  939. //
  940. // MakeRope(@left, ToString(ToPrimitive(@right)))
  941. //
  942. // So that's what we emit. NB, we need to do all relevant type checks on @left before we do
  943. // anything to @right, since ToPrimitive may be effectful.
  944. Node* toPrimitive = m_insertionSet.insertNode(
  945. m_indexInBlock, resultOfToPrimitive(right->prediction()), ToPrimitive, node->codeOrigin,
  946. Edge(right.node()));
  947. Node* toString = m_insertionSet.insertNode(
  948. m_indexInBlock, SpecString, ToString, node->codeOrigin, Edge(toPrimitive));
  949. fixupToPrimitive(toPrimitive);
  950. fixupToString(toString);
  951. right.setNode(toString);
  952. }
  953. // We're doing checks up there, so we need to make sure that the
  954. // *original* inputs to the addition are live up to here.
  955. m_insertionSet.insertNode(
  956. m_indexInBlock, SpecNone, Phantom, node->codeOrigin,
  957. Edge(originalLeft), Edge(originalRight));
  958. convertToMakeRope(node);
  959. return true;
  960. }
  961. bool isStringPrototypeMethodSane(Structure* stringPrototypeStructure, const Identifier& ident)
  962. {
  963. unsigned attributesUnused;
  964. JSCell* specificValue;
  965. PropertyOffset offset = stringPrototypeStructure->get(
  966. vm(), ident, attributesUnused, specificValue);
  967. if (!isValidOffset(offset))
  968. return false;
  969. if (!specificValue)
  970. return false;
  971. if (!specificValue->inherits(&JSFunction::s_info))
  972. return false;
  973. JSFunction* function = jsCast<JSFunction*>(specificValue);
  974. if (function->executable()->intrinsicFor(CodeForCall) != StringPrototypeValueOfIntrinsic)
  975. return false;
  976. return true;
  977. }
  978. bool canOptimizeStringObjectAccess(const CodeOrigin& codeOrigin)
  979. {
  980. if (m_graph.hasExitSite(codeOrigin, NotStringObject))
  981. return false;
  982. Structure* stringObjectStructure = m_graph.globalObjectFor(codeOrigin)->stringObjectStructure();
  983. ASSERT(stringObjectStructure->storedPrototype().isObject());
  984. ASSERT(stringObjectStructure->storedPrototype().asCell()->classInfo() == &StringPrototype::s_info);
  985. JSObject* stringPrototypeObject = asObject(stringObjectStructure->storedPrototype());
  986. Structure* stringPrototypeStructure = stringPrototypeObject->structure();
  987. if (stringPrototypeStructure->transitionWatchpointSetHasBeenInvalidated())
  988. return false;
  989. if (stringPrototypeStructure->isDictionary())
  990. return false;
  991. // We're being conservative here. We want DFG's ToString on StringObject to be
  992. // used in both numeric contexts (that would call valueOf()) and string contexts
  993. // (that would call toString()). We don't want the DFG to have to distinguish
  994. // between the two, just because that seems like it would get confusing. So we
  995. // just require both methods to be sane.
  996. if (!isStringPrototypeMethodSane(stringPrototypeStructure, vm().propertyNames->valueOf))
  997. return false;
  998. if (!isStringPrototypeMethodSane(stringPrototypeStructure, vm().propertyNames->toString))
  999. return false;
  1000. return true;
  1001. }
  1002. void fixupSetLocalsInBlock(BasicBlock* block)
  1003. {
  1004. if (!block)
  1005. return;
  1006. ASSERT(block->isReachable);
  1007. m_block = block;
  1008. for (m_indexInBlock = 0; m_indexInBlock < block->size(); ++m_indexInBlock) {
  1009. Node* node = m_currentNode = block->at(m_indexInBlock);
  1010. if (node->op() != SetLocal)
  1011. continue;
  1012. VariableAccessData* variable = node->variableAccessData();
  1013. if (!variable->shouldUnboxIfPossible())
  1014. continue;
  1015. if (variable->shouldUseDoubleFormat()) {
  1016. fixDoubleEdge<NumberUse>(node->child1(), ForwardSpeculation);
  1017. continue;
  1018. }
  1019. SpeculatedType predictedType = variable->argumentAwarePrediction();
  1020. if (isInt32Speculation(predictedType))
  1021. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  1022. else if (isCellSpeculation(predictedType))
  1023. setUseKindAndUnboxIfProfitable<CellUse>(node->child1());
  1024. else if (isBooleanSpeculation(predictedType))
  1025. setUseKindAndUnboxIfProfitable<BooleanUse>(node->child1());
  1026. }
  1027. m_insertionSet.execute(block);
  1028. }
  1029. void findAndRemoveUnnecessaryStructureCheck(Node* array, const CodeOrigin& codeOrigin)
  1030. {
  1031. for (unsigned index = m_indexInBlock; index--;) {
  1032. Node* previousNode = m_block->at(index);
  1033. if (previousNode->codeOrigin != codeOrigin)
  1034. return;
  1035. if (previousNode->op() != CheckStructure)
  1036. continue;
  1037. if (previousNode->child1() != array)
  1038. continue;
  1039. previousNode->child1() = Edge();
  1040. previousNode->convertToPhantom();
  1041. return; // Assume we were smart enough to only insert one CheckStructure on the array.
  1042. }
  1043. }
  1044. Node* checkArray(ArrayMode arrayMode, const CodeOrigin& codeOrigin, Node* array, Node* index, bool (*storageCheck)(const ArrayMode&) = canCSEStorage)
  1045. {
  1046. ASSERT(arrayMode.isSpecific());
  1047. Structure* structure = arrayMode.originalArrayStructure(m_graph, codeOrigin);
  1048. Edge indexEdge = index ? Edge(index, Int32Use) : Edge();
  1049. if (arrayMode.doesConversion()) {
  1050. if (structure) {
  1051. if (m_indexInBlock > 0) {
  1052. // If the previous node was a CheckStructure inserted because of stuff
  1053. // that the array profile told us, then remove it, since we're going to be
  1054. // doing arrayification instead.
  1055. findAndRemoveUnnecessaryStructureCheck(array, codeOrigin);
  1056. }
  1057. m_insertionSet.insertNode(
  1058. m_indexInBlock, SpecNone, ArrayifyToStructure, codeOrigin,
  1059. OpInfo(structure), OpInfo(arrayMode.asWord()), Edge(array, CellUse), indexEdge);
  1060. } else {
  1061. m_insertionSet.insertNode(
  1062. m_indexInBlock, SpecNone, Arrayify, codeOrigin,
  1063. OpInfo(arrayMode.asWord()), Edge(array, CellUse), indexEdge);
  1064. }
  1065. } else {
  1066. if (structure) {
  1067. m_insertionSet.insertNode(
  1068. m_indexInBlock, SpecNone, CheckStructure, codeOrigin,
  1069. OpInfo(m_graph.addStructureSet(structure)), Edge(array, CellUse));
  1070. } else {
  1071. m_insertionSet.insertNode(
  1072. m_indexInBlock, SpecNone, CheckArray, codeOrigin,
  1073. OpInfo(arrayMode.asWord()), Edge(array, CellUse));
  1074. }
  1075. }
  1076. if (!storageCheck(arrayMode))
  1077. return 0;
  1078. if (arrayMode.usesButterfly()) {
  1079. return m_insertionSet.insertNode(
  1080. m_indexInBlock, SpecNone, GetButterfly, codeOrigin, Edge(array, KnownCellUse));
  1081. }
  1082. return m_insertionSet.insertNode(
  1083. m_indexInBlock, SpecNone, GetIndexedPropertyStorage, codeOrigin,
  1084. OpInfo(arrayMode.asWord()), Edge(array, KnownCellUse));
  1085. }
  1086. void blessArrayOperation(Edge base, Edge index, Edge& storageChild)
  1087. {
  1088. Node* node = m_currentNode;
  1089. switch (node->arrayMode().type()) {
  1090. case Array::ForceExit: {
  1091. m_insertionSet.insertNode(
  1092. m_indexInBlock, SpecNone, ForceOSRExit, node->codeOrigin);
  1093. return;
  1094. }
  1095. case Array::SelectUsingPredictions:
  1096. case Array::Unprofiled:
  1097. RELEASE_ASSERT_NOT_REACHED();
  1098. return;
  1099. case Array::Generic:
  1100. findAndRemoveUnnecessaryStructureCheck(base.node(), node->codeOrigin);
  1101. return;
  1102. default: {
  1103. Node* storage = checkArray(node->arrayMode(), node->codeOrigin, base.node(), index.node());
  1104. if (!storage)
  1105. return;
  1106. storageChild = Edge(storage);
  1107. return;
  1108. } }
  1109. }
  1110. bool alwaysUnboxSimplePrimitives()
  1111. {
  1112. #if USE(JSVALUE64)
  1113. return false;
  1114. #else
  1115. // Any boolean, int, or cell value is profitable to unbox on 32-bit because it
  1116. // reduces traffic.
  1117. return true;
  1118. #endif
  1119. }
  1120. template<UseKind useKind>
  1121. void observeUseKindOnNode(Node* node)
  1122. {
  1123. observeUseKindOnNode(node, useKind);
  1124. }
  1125. void observeUseKindOnEdge(Edge edge)
  1126. {
  1127. observeUseKindOnNode(edge.node(), edge.useKind());
  1128. }
  1129. void observeUseKindOnNode(Node* node, UseKind useKind)
  1130. {
  1131. if (node->op() != GetLocal)
  1132. return;
  1133. VariableAccessData* variable = node->variableAccessData();
  1134. switch (useKind) {
  1135. case Int32Use:
  1136. if (alwaysUnboxSimplePrimitives()
  1137. || isInt32Speculation(variable->prediction()))
  1138. m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
  1139. break;
  1140. case NumberUse:
  1141. case RealNumberUse:
  1142. if (variable->doubleFormatState() == UsingDoubleFormat)
  1143. m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
  1144. break;
  1145. case BooleanUse:
  1146. if (alwaysUnboxSimplePrimitives()
  1147. || isBooleanSpeculation(variable->prediction()))
  1148. m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
  1149. break;
  1150. case CellUse:
  1151. case ObjectUse:
  1152. case StringUse:
  1153. case KnownStringUse:
  1154. case StringObjectUse:
  1155. case StringOrStringObjectUse:
  1156. if (alwaysUnboxSimplePrimitives()
  1157. || isCellSpeculation(variable->prediction()))
  1158. m_profitabilityChanged |= variable->mergeIsProfitableToUnbox(true);
  1159. break;
  1160. default:
  1161. break;
  1162. }
  1163. }
  1164. // Set the use kind of the edge. In the future (https://bugs.webkit.org/show_bug.cgi?id=110433),
  1165. // this can be used to notify the GetLocal that the variable is profitable to unbox.
  1166. template<UseKind useKind>
  1167. void setUseKindAndUnboxIfProfitable(Edge& edge)
  1168. {
  1169. observeUseKindOnNode<useKind>(edge.node());
  1170. edge.setUseKind(useKind);
  1171. }
  1172. void fixIntEdge(Edge& edge)
  1173. {
  1174. Node* node = edge.node();
  1175. if (node->op() != ValueToInt32) {
  1176. setUseKindAndUnboxIfProfitable<KnownInt32Use>(edge);
  1177. return;
  1178. }
  1179. Edge newEdge = node->child1();
  1180. if (newEdge.useKind() != Int32Use) {
  1181. edge.setUseKind(KnownInt32Use);
  1182. return;
  1183. }
  1184. ASSERT(newEdge->shouldSpeculateInteger());
  1185. edge = newEdge;
  1186. }
  1187. template<UseKind useKind>
  1188. void fixDoubleEdge(Edge& edge, SpeculationDirection direction = BackwardSpeculation)
  1189. {
  1190. ASSERT(useKind == NumberUse || useKind == KnownNumberUse || useKind == RealNumberUse);
  1191. if (edge->prediction() & SpecDouble) {
  1192. setUseKindAndUnboxIfProfitable<useKind>(edge);
  1193. return;
  1194. }
  1195. injectInt32ToDoubleNode(edge, useKind, direction);
  1196. }
  1197. void injectInt32ToDoubleNode(Edge& edge, UseKind useKind = NumberUse, SpeculationDirection direction = BackwardSpeculation)
  1198. {
  1199. Node* result = m_insertionSet.insertNode(
  1200. m_indexInBlock, SpecDouble,
  1201. direction == BackwardSpeculation ? Int32ToDouble : ForwardInt32ToDouble,
  1202. m_currentNode->codeOrigin, Edge(edge.node(), NumberUse));
  1203. #if DFG_ENABLE(DEBUG_PROPAGATION_VERBOSE)
  1204. dataLogF(
  1205. "(replacing @%u->@%u with @%u->@%u) ",
  1206. m_currentNode->index(), edge->index(), m_currentNode->index(), result->index());
  1207. #endif
  1208. edge = Edge(result, useKind);
  1209. }
  1210. void truncateConstantToInt32(Edge& edge)
  1211. {
  1212. Node* oldNode = edge.node();
  1213. ASSERT(oldNode->hasConstant());
  1214. JSValue value = m_graph.valueOfJSConstant(oldNode);
  1215. if (value.isInt32())
  1216. return;
  1217. value = jsNumber(JSC::toInt32(value.asNumber()));
  1218. ASSERT(value.isInt32());
  1219. edge.setNode(m_insertionSet.insertNode(
  1220. m_indexInBlock, SpecInt32, JSConstant, m_currentNode->codeOrigin,
  1221. OpInfo(codeBlock()->addOrFindConstant(value))));
  1222. }
  1223. void truncateConstantsIfNecessary(Node* node, AddSpeculationMode mode)
  1224. {
  1225. if (mode != SpeculateIntegerAndTruncateConstants)
  1226. return;
  1227. ASSERT(node->child1()->hasConstant() || node->child2()->hasConstant());
  1228. if (node->child1()->hasConstant())
  1229. truncateConstantToInt32(node->child1());
  1230. else
  1231. truncateConstantToInt32(node->child2());
  1232. }
  1233. bool attemptToMakeIntegerAdd(Node* node)
  1234. {
  1235. AddSpeculationMode mode = m_graph.addSpeculationMode(node);
  1236. if (mode == DontSpeculateInteger)
  1237. return false;
  1238. truncateConstantsIfNecessary(node, mode);
  1239. setUseKindAndUnboxIfProfitable<Int32Use>(node->child1());
  1240. setUseKindAndUnboxIfProfitable<Int32Use>(node->child2());
  1241. return true;
  1242. }
  1243. BasicBlock* m_block;
  1244. unsigned m_indexInBlock;
  1245. Node* m_currentNode;
  1246. InsertionSet m_insertionSet;
  1247. bool m_profitabilityChanged;
  1248. };
  1249. bool performFixup(Graph& graph)
  1250. {
  1251. SamplingRegion samplingRegion("DFG Fixup Phase");
  1252. return runPhase<FixupPhase>(graph);
  1253. }
  1254. } } // namespace JSC::DFG
  1255. #endif // ENABLE(DFG_JIT)