DFGArrayMode.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright (C) 2012 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. #ifndef DFGArrayMode_h
  26. #define DFGArrayMode_h
  27. #include <wtf/Platform.h>
  28. #if ENABLE(DFG_JIT)
  29. #include "ArrayProfile.h"
  30. #include "DFGNodeFlags.h"
  31. #include "SpeculatedType.h"
  32. namespace JSC {
  33. struct CodeOrigin;
  34. namespace DFG {
  35. class Graph;
  36. struct AbstractValue;
  37. struct Node;
  38. // Use a namespace + enum instead of enum alone to avoid the namespace collision
  39. // that would otherwise occur, since we say things like "Int8Array" and "JSArray"
  40. // in lots of other places, to mean subtly different things.
  41. namespace Array {
  42. enum Action {
  43. Read,
  44. Write
  45. };
  46. enum Type {
  47. SelectUsingPredictions, // Implies that we need predictions to decide. We will never get to the backend in this mode.
  48. Unprofiled, // Implies that array profiling didn't see anything. But that could be because the operands didn't comply with basic type assumptions (base is cell, property is int). This either becomes Generic or ForceExit depending on value profiling.
  49. ForceExit, // Implies that we have no idea how to execute this operation, so we should just give up.
  50. Generic,
  51. String,
  52. Undecided,
  53. Int32,
  54. Double,
  55. Contiguous,
  56. ArrayStorage,
  57. SlowPutArrayStorage,
  58. Arguments,
  59. Int8Array,
  60. Int16Array,
  61. Int32Array,
  62. Uint8Array,
  63. Uint8ClampedArray,
  64. Uint16Array,
  65. Uint32Array,
  66. Float32Array,
  67. Float64Array
  68. };
  69. enum Class {
  70. NonArray, // Definitely some object that is not a JSArray.
  71. Array, // Definitely a JSArray, and may or may not have custom properties or have undergone some other bizarre transitions.
  72. OriginalArray, // Definitely a JSArray, and still has one of the primordial JSArray structures for the global object that this code block (possibly inlined code block) belongs to.
  73. PossiblyArray // Some object that may or may not be a JSArray.
  74. };
  75. enum Speculation {
  76. SaneChain, // In bounds and the array prototype chain is still intact, i.e. loading a hole doesn't require special treatment.
  77. InBounds, // In bounds and not loading a hole.
  78. ToHole, // Potentially storing to a hole.
  79. OutOfBounds // Out-of-bounds access and anything can happen.
  80. };
  81. enum Conversion {
  82. AsIs,
  83. Convert,
  84. RageConvert
  85. };
  86. } // namespace Array
  87. const char* arrayTypeToString(Array::Type);
  88. const char* arrayClassToString(Array::Class);
  89. const char* arraySpeculationToString(Array::Speculation);
  90. const char* arrayConversionToString(Array::Conversion);
  91. class ArrayMode {
  92. public:
  93. ArrayMode()
  94. {
  95. u.asBytes.type = Array::SelectUsingPredictions;
  96. u.asBytes.arrayClass = Array::NonArray;
  97. u.asBytes.speculation = Array::InBounds;
  98. u.asBytes.conversion = Array::AsIs;
  99. }
  100. explicit ArrayMode(Array::Type type)
  101. {
  102. u.asBytes.type = type;
  103. u.asBytes.arrayClass = Array::NonArray;
  104. u.asBytes.speculation = Array::InBounds;
  105. u.asBytes.conversion = Array::AsIs;
  106. }
  107. ArrayMode(Array::Type type, Array::Class arrayClass, Array::Speculation speculation, Array::Conversion conversion)
  108. {
  109. u.asBytes.type = type;
  110. u.asBytes.arrayClass = arrayClass;
  111. u.asBytes.speculation = speculation;
  112. u.asBytes.conversion = conversion;
  113. }
  114. ArrayMode(Array::Type type, Array::Class arrayClass, Array::Conversion conversion)
  115. {
  116. u.asBytes.type = type;
  117. u.asBytes.arrayClass = arrayClass;
  118. u.asBytes.speculation = Array::InBounds;
  119. u.asBytes.conversion = conversion;
  120. }
  121. Array::Type type() const { return static_cast<Array::Type>(u.asBytes.type); }
  122. Array::Class arrayClass() const { return static_cast<Array::Class>(u.asBytes.arrayClass); }
  123. Array::Speculation speculation() const { return static_cast<Array::Speculation>(u.asBytes.speculation); }
  124. Array::Conversion conversion() const { return static_cast<Array::Conversion>(u.asBytes.conversion); }
  125. unsigned asWord() const { return u.asWord; }
  126. static ArrayMode fromWord(unsigned word)
  127. {
  128. return ArrayMode(word);
  129. }
  130. static ArrayMode fromObserved(ArrayProfile*, Array::Action, bool makeSafe);
  131. ArrayMode withSpeculation(Array::Speculation speculation) const
  132. {
  133. return ArrayMode(type(), arrayClass(), speculation, conversion());
  134. }
  135. ArrayMode withProfile(ArrayProfile* profile, bool makeSafe) const
  136. {
  137. Array::Speculation mySpeculation;
  138. Array::Class myArrayClass;
  139. if (makeSafe)
  140. mySpeculation = Array::OutOfBounds;
  141. else if (profile->mayStoreToHole())
  142. mySpeculation = Array::ToHole;
  143. else
  144. mySpeculation = Array::InBounds;
  145. if (isJSArray()) {
  146. if (profile->usesOriginalArrayStructures() && benefitsFromOriginalArray())
  147. myArrayClass = Array::OriginalArray;
  148. else
  149. myArrayClass = Array::Array;
  150. } else
  151. myArrayClass = arrayClass();
  152. return ArrayMode(type(), myArrayClass, mySpeculation, conversion());
  153. }
  154. ArrayMode withType(Array::Type type) const
  155. {
  156. return ArrayMode(type, arrayClass(), speculation(), conversion());
  157. }
  158. ArrayMode withConversion(Array::Conversion conversion) const
  159. {
  160. return ArrayMode(type(), arrayClass(), speculation(), conversion);
  161. }
  162. ArrayMode withTypeAndConversion(Array::Type type, Array::Conversion conversion) const
  163. {
  164. return ArrayMode(type, arrayClass(), speculation(), conversion);
  165. }
  166. ArrayMode refine(SpeculatedType base, SpeculatedType index, SpeculatedType value = SpecNone, NodeFlags = 0) const;
  167. bool alreadyChecked(Graph&, Node*, AbstractValue&) const;
  168. void dump(PrintStream&) const;
  169. bool usesButterfly() const
  170. {
  171. switch (type()) {
  172. case Array::Int32:
  173. case Array::Double:
  174. case Array::Contiguous:
  175. case Array::ArrayStorage:
  176. case Array::SlowPutArrayStorage:
  177. return true;
  178. default:
  179. return false;
  180. }
  181. }
  182. bool isJSArray() const
  183. {
  184. switch (arrayClass()) {
  185. case Array::Array:
  186. case Array::OriginalArray:
  187. return true;
  188. default:
  189. return false;
  190. }
  191. }
  192. bool isJSArrayWithOriginalStructure() const
  193. {
  194. return arrayClass() == Array::OriginalArray;
  195. }
  196. bool isSaneChain() const
  197. {
  198. return speculation() == Array::SaneChain;
  199. }
  200. bool isInBounds() const
  201. {
  202. switch (speculation()) {
  203. case Array::SaneChain:
  204. case Array::InBounds:
  205. return true;
  206. default:
  207. return false;
  208. }
  209. }
  210. bool mayStoreToHole() const
  211. {
  212. return !isInBounds();
  213. }
  214. bool isOutOfBounds() const
  215. {
  216. return speculation() == Array::OutOfBounds;
  217. }
  218. bool isSlowPut() const
  219. {
  220. return type() == Array::SlowPutArrayStorage;
  221. }
  222. bool canCSEStorage() const
  223. {
  224. switch (type()) {
  225. case Array::SelectUsingPredictions:
  226. case Array::Unprofiled:
  227. case Array::ForceExit:
  228. case Array::Generic:
  229. case Array::Arguments:
  230. return false;
  231. default:
  232. return true;
  233. }
  234. }
  235. bool lengthNeedsStorage() const
  236. {
  237. return isJSArray();
  238. }
  239. ArrayMode modeForPut() const
  240. {
  241. switch (type()) {
  242. case Array::String:
  243. return ArrayMode(Array::Generic);
  244. #if USE(JSVALUE32_64)
  245. case Array::Arguments:
  246. return ArrayMode(Array::Generic);
  247. #endif
  248. default:
  249. return *this;
  250. }
  251. }
  252. bool isSpecific() const
  253. {
  254. switch (type()) {
  255. case Array::SelectUsingPredictions:
  256. case Array::Unprofiled:
  257. case Array::ForceExit:
  258. case Array::Generic:
  259. case Array::Undecided:
  260. return false;
  261. default:
  262. return true;
  263. }
  264. }
  265. bool supportsLength() const
  266. {
  267. switch (type()) {
  268. case Array::SelectUsingPredictions:
  269. case Array::Unprofiled:
  270. case Array::ForceExit:
  271. case Array::Generic:
  272. return false;
  273. case Array::Int32:
  274. case Array::Double:
  275. case Array::Contiguous:
  276. case Array::ArrayStorage:
  277. case Array::SlowPutArrayStorage:
  278. return isJSArray();
  279. default:
  280. return true;
  281. }
  282. }
  283. bool benefitsFromOriginalArray() const
  284. {
  285. switch (type()) {
  286. case Array::Int32:
  287. case Array::Double:
  288. case Array::Contiguous:
  289. case Array::ArrayStorage:
  290. return true;
  291. default:
  292. return false;
  293. }
  294. }
  295. // Returns 0 if this is not OriginalArray.
  296. Structure* originalArrayStructure(Graph&, const CodeOrigin&) const;
  297. Structure* originalArrayStructure(Graph&, Node*) const;
  298. bool benefitsFromStructureCheck() const
  299. {
  300. switch (type()) {
  301. case Array::SelectUsingPredictions:
  302. // It might benefit from structure checks! If it ends up not benefiting, we can just
  303. // remove it. The FixupPhase does this: if it finds a CheckStructure just before an
  304. // array op and it had turned that array op into either generic or conversion mode,
  305. // it will remove the CheckStructure.
  306. return true;
  307. case Array::Unprofiled:
  308. case Array::ForceExit:
  309. case Array::Generic:
  310. return false;
  311. default:
  312. return conversion() == Array::AsIs;
  313. }
  314. }
  315. bool doesConversion() const
  316. {
  317. return conversion() != Array::AsIs;
  318. }
  319. ArrayModes arrayModesThatPassFiltering() const
  320. {
  321. switch (type()) {
  322. case Array::Generic:
  323. return ALL_ARRAY_MODES;
  324. case Array::Int32:
  325. return arrayModesWithIndexingShape(Int32Shape);
  326. case Array::Double:
  327. return arrayModesWithIndexingShape(DoubleShape);
  328. case Array::Contiguous:
  329. return arrayModesWithIndexingShape(ContiguousShape);
  330. case Array::ArrayStorage:
  331. return arrayModesWithIndexingShape(ArrayStorageShape);
  332. case Array::SlowPutArrayStorage:
  333. return arrayModesWithIndexingShape(SlowPutArrayStorageShape);
  334. default:
  335. return asArrayModes(NonArray);
  336. }
  337. }
  338. bool getIndexedPropertyStorageMayTriggerGC() const
  339. {
  340. return type() == Array::String;
  341. }
  342. bool operator==(const ArrayMode& other) const
  343. {
  344. return type() == other.type()
  345. && arrayClass() == other.arrayClass()
  346. && speculation() == other.speculation()
  347. && conversion() == other.conversion();
  348. }
  349. bool operator!=(const ArrayMode& other) const
  350. {
  351. return !(*this == other);
  352. }
  353. private:
  354. explicit ArrayMode(unsigned word)
  355. {
  356. u.asWord = word;
  357. }
  358. ArrayModes arrayModesWithIndexingShape(IndexingType shape) const
  359. {
  360. switch (arrayClass()) {
  361. case Array::NonArray:
  362. return asArrayModes(shape);
  363. case Array::Array:
  364. case Array::OriginalArray:
  365. return asArrayModes(shape | IsArray);
  366. case Array::PossiblyArray:
  367. return asArrayModes(shape) | asArrayModes(shape | IsArray);
  368. default:
  369. // This is only necessary for C++ compilers that don't understand enums.
  370. return 0;
  371. }
  372. }
  373. bool alreadyChecked(Graph&, Node*, AbstractValue&, IndexingType shape) const;
  374. union {
  375. struct {
  376. uint8_t type;
  377. uint8_t arrayClass;
  378. uint8_t speculation;
  379. uint8_t conversion;
  380. } asBytes;
  381. unsigned asWord;
  382. } u;
  383. };
  384. static inline bool canCSEStorage(const ArrayMode& arrayMode)
  385. {
  386. return arrayMode.canCSEStorage();
  387. }
  388. static inline bool lengthNeedsStorage(const ArrayMode& arrayMode)
  389. {
  390. return arrayMode.lengthNeedsStorage();
  391. }
  392. } } // namespace JSC::DFG
  393. namespace WTF {
  394. class PrintStream;
  395. void printInternal(PrintStream&, JSC::DFG::Array::Type);
  396. void printInternal(PrintStream&, JSC::DFG::Array::Class);
  397. void printInternal(PrintStream&, JSC::DFG::Array::Speculation);
  398. void printInternal(PrintStream&, JSC::DFG::Array::Conversion);
  399. } // namespace WTF
  400. #endif // ENABLE(DFG_JIT)
  401. #endif // DFGArrayMode_h