blob: a057217cc514f091ea9f71f2a0a17474cb56b0a5 [file] [log] [blame]
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001// Copyright 2012 the V8 project authors. All rights reserved.
rossberg@chromium.org34849642014-04-29 16:30:47 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004
machenbach@chromium.org196eb602014-06-04 00:06:13 +00005#include "src/v8.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +00006
machenbach@chromium.orge2a89372014-08-21 07:23:04 +00007#include "src/base/bits.h"
machenbach@chromium.org196eb602014-06-04 00:06:13 +00008#include "src/double.h"
9#include "src/factory.h"
10#include "src/hydrogen-infer-representation.h"
11#include "src/property-details-inl.h"
kasperl@chromium.orga5551262010-12-07 12:49:48 +000012
13#if V8_TARGET_ARCH_IA32
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +000014#include "src/ia32/lithium-ia32.h" // NOLINT
kasperl@chromium.orga5551262010-12-07 12:49:48 +000015#elif V8_TARGET_ARCH_X64
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +000016#include "src/x64/lithium-x64.h" // NOLINT
machenbach@chromium.orgfa0c3c62014-03-24 08:11:09 +000017#elif V8_TARGET_ARCH_ARM64
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +000018#include "src/arm64/lithium-arm64.h" // NOLINT
kasperl@chromium.orga5551262010-12-07 12:49:48 +000019#elif V8_TARGET_ARCH_ARM
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +000020#include "src/arm/lithium-arm.h" // NOLINT
lrn@chromium.org7516f052011-03-30 08:52:27 +000021#elif V8_TARGET_ARCH_MIPS
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +000022#include "src/mips/lithium-mips.h" // NOLINT
machenbach@chromium.org12e05e82014-07-10 00:04:42 +000023#elif V8_TARGET_ARCH_MIPS64
24#include "src/mips64/lithium-mips64.h" // NOLINT
machenbach@chromium.org864abd72014-05-26 06:35:16 +000025#elif V8_TARGET_ARCH_X87
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +000026#include "src/x87/lithium-x87.h" // NOLINT
kasperl@chromium.orga5551262010-12-07 12:49:48 +000027#else
28#error Unsupported target architecture.
29#endif
30
machenbach@chromium.org975b9402014-06-24 00:06:56 +000031#include "src/base/safe_math.h"
32
kasperl@chromium.orga5551262010-12-07 12:49:48 +000033namespace v8 {
34namespace internal {
35
36#define DEFINE_COMPILE(type) \
37 LInstruction* H##type::CompileToLithium(LChunkBuilder* builder) { \
38 return builder->Do##type(this); \
39 }
40HYDROGEN_CONCRETE_INSTRUCTION_LIST(DEFINE_COMPILE)
41#undef DEFINE_COMPILE
42
43
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000044Isolate* HValue::isolate() const {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +000045 DCHECK(block() != NULL);
ulan@chromium.org750145a2013-03-07 15:14:13 +000046 return block()->isolate();
ulan@chromium.org09d7ab52013-02-25 15:50:35 +000047}
48
49
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000050void HValue::AssumeRepresentation(Representation r) {
51 if (CheckFlag(kFlexibleRepresentation)) {
52 ChangeRepresentation(r);
53 // The representation of the value is dictated by type feedback and
54 // will not be changed later.
55 ClearFlag(kFlexibleRepresentation);
56 }
57}
58
59
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000060void HValue::InferRepresentation(HInferRepresentationPhase* h_infer) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +000061 DCHECK(CheckFlag(kFlexibleRepresentation));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000062 Representation new_rep = RepresentationFromInputs();
63 UpdateRepresentation(new_rep, h_infer, "inputs");
64 new_rep = RepresentationFromUses();
65 UpdateRepresentation(new_rep, h_infer, "uses");
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +000066 if (representation().IsSmi() && HasNonSmiUse()) {
67 UpdateRepresentation(
68 Representation::Integer32(), h_infer, "use requirements");
dslomov@chromium.orgb752d402013-06-18 11:54:54 +000069 }
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000070}
71
72
73Representation HValue::RepresentationFromUses() {
74 if (HasNoUses()) return Representation::None();
75
76 // Array of use counts for each representation.
77 int use_count[Representation::kNumRepresentations] = { 0 };
78
79 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
80 HValue* use = it.value();
81 Representation rep = use->observed_input_representation(it.index());
82 if (rep.IsNone()) continue;
83 if (FLAG_trace_representation) {
84 PrintF("#%d %s is used by #%d %s as %s%s\n",
85 id(), Mnemonic(), use->id(), use->Mnemonic(), rep.Mnemonic(),
86 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
87 }
machenbach@chromium.orge31286d2014-01-15 10:29:52 +000088 use_count[rep.kind()] += 1;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000089 }
90 if (IsPhi()) HPhi::cast(this)->AddIndirectUsesTo(&use_count[0]);
91 int tagged_count = use_count[Representation::kTagged];
92 int double_count = use_count[Representation::kDouble];
93 int int32_count = use_count[Representation::kInteger32];
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +000094 int smi_count = use_count[Representation::kSmi];
yangguo@chromium.orgfb377212012-11-16 14:43:43 +000095
96 if (tagged_count > 0) return Representation::Tagged();
97 if (double_count > 0) return Representation::Double();
98 if (int32_count > 0) return Representation::Integer32();
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +000099 if (smi_count > 0) return Representation::Smi();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000100
101 return Representation::None();
102}
103
104
105void HValue::UpdateRepresentation(Representation new_rep,
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000106 HInferRepresentationPhase* h_infer,
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000107 const char* reason) {
108 Representation r = representation();
109 if (new_rep.is_more_general_than(r)) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000110 if (CheckFlag(kCannotBeTagged) && new_rep.IsTagged()) return;
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000111 if (FLAG_trace_representation) {
112 PrintF("Changing #%d %s representation %s -> %s based on %s\n",
113 id(), Mnemonic(), r.Mnemonic(), new_rep.Mnemonic(), reason);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000114 }
115 ChangeRepresentation(new_rep);
116 AddDependantsToWorklist(h_infer);
117 }
118}
119
120
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000121void HValue::AddDependantsToWorklist(HInferRepresentationPhase* h_infer) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000122 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
123 h_infer->AddToWorklist(it.value());
124 }
125 for (int i = 0; i < OperandCount(); ++i) {
126 h_infer->AddToWorklist(OperandAt(i));
127 }
128}
129
130
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000131static int32_t ConvertAndSetOverflow(Representation r,
132 int64_t result,
133 bool* overflow) {
134 if (r.IsSmi()) {
135 if (result > Smi::kMaxValue) {
136 *overflow = true;
137 return Smi::kMaxValue;
138 }
139 if (result < Smi::kMinValue) {
140 *overflow = true;
141 return Smi::kMinValue;
142 }
143 } else {
144 if (result > kMaxInt) {
145 *overflow = true;
146 return kMaxInt;
147 }
148 if (result < kMinInt) {
149 *overflow = true;
150 return kMinInt;
151 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000152 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000153 return static_cast<int32_t>(result);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000154}
155
156
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000157static int32_t AddWithoutOverflow(Representation r,
158 int32_t a,
159 int32_t b,
160 bool* overflow) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000161 int64_t result = static_cast<int64_t>(a) + static_cast<int64_t>(b);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000162 return ConvertAndSetOverflow(r, result, overflow);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000163}
164
165
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000166static int32_t SubWithoutOverflow(Representation r,
167 int32_t a,
168 int32_t b,
169 bool* overflow) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000170 int64_t result = static_cast<int64_t>(a) - static_cast<int64_t>(b);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000171 return ConvertAndSetOverflow(r, result, overflow);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000172}
173
174
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000175static int32_t MulWithoutOverflow(const Representation& r,
176 int32_t a,
177 int32_t b,
178 bool* overflow) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000179 int64_t result = static_cast<int64_t>(a) * static_cast<int64_t>(b);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000180 return ConvertAndSetOverflow(r, result, overflow);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000181}
182
183
184int32_t Range::Mask() const {
185 if (lower_ == upper_) return lower_;
186 if (lower_ >= 0) {
187 int32_t res = 1;
188 while (res < upper_) {
189 res = (res << 1) | 1;
190 }
191 return res;
192 }
193 return 0xffffffff;
194}
195
196
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000197void Range::AddConstant(int32_t value) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000198 if (value == 0) return;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000199 bool may_overflow = false; // Overflow is ignored here.
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000200 Representation r = Representation::Integer32();
201 lower_ = AddWithoutOverflow(r, lower_, value, &may_overflow);
202 upper_ = AddWithoutOverflow(r, upper_, value, &may_overflow);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000203#ifdef DEBUG
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000204 Verify();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000205#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000206}
207
208
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000209void Range::Intersect(Range* other) {
210 upper_ = Min(upper_, other->upper_);
211 lower_ = Max(lower_, other->lower_);
212 bool b = CanBeMinusZero() && other->CanBeMinusZero();
213 set_can_be_minus_zero(b);
214}
215
216
217void Range::Union(Range* other) {
218 upper_ = Max(upper_, other->upper_);
219 lower_ = Min(lower_, other->lower_);
220 bool b = CanBeMinusZero() || other->CanBeMinusZero();
221 set_can_be_minus_zero(b);
222}
223
224
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +0000225void Range::CombinedMax(Range* other) {
226 upper_ = Max(upper_, other->upper_);
227 lower_ = Max(lower_, other->lower_);
228 set_can_be_minus_zero(CanBeMinusZero() || other->CanBeMinusZero());
229}
230
231
232void Range::CombinedMin(Range* other) {
233 upper_ = Min(upper_, other->upper_);
234 lower_ = Min(lower_, other->lower_);
235 set_can_be_minus_zero(CanBeMinusZero() || other->CanBeMinusZero());
236}
237
238
karlklose@chromium.org8f806e82011-03-07 14:06:08 +0000239void Range::Sar(int32_t value) {
240 int32_t bits = value & 0x1F;
241 lower_ = lower_ >> bits;
242 upper_ = upper_ >> bits;
243 set_can_be_minus_zero(false);
244}
245
246
247void Range::Shl(int32_t value) {
248 int32_t bits = value & 0x1F;
249 int old_lower = lower_;
250 int old_upper = upper_;
251 lower_ = lower_ << bits;
252 upper_ = upper_ << bits;
253 if (old_lower != lower_ >> bits || old_upper != upper_ >> bits) {
254 upper_ = kMaxInt;
255 lower_ = kMinInt;
256 }
257 set_can_be_minus_zero(false);
258}
259
260
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000261bool Range::AddAndCheckOverflow(const Representation& r, Range* other) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000262 bool may_overflow = false;
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000263 lower_ = AddWithoutOverflow(r, lower_, other->lower(), &may_overflow);
264 upper_ = AddWithoutOverflow(r, upper_, other->upper(), &may_overflow);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000265 KeepOrder();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000266#ifdef DEBUG
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000267 Verify();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000268#endif
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000269 return may_overflow;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000270}
271
272
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000273bool Range::SubAndCheckOverflow(const Representation& r, Range* other) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000274 bool may_overflow = false;
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000275 lower_ = SubWithoutOverflow(r, lower_, other->upper(), &may_overflow);
276 upper_ = SubWithoutOverflow(r, upper_, other->lower(), &may_overflow);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000277 KeepOrder();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000278#ifdef DEBUG
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000279 Verify();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000280#endif
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000281 return may_overflow;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000282}
283
284
285void Range::KeepOrder() {
286 if (lower_ > upper_) {
287 int32_t tmp = lower_;
288 lower_ = upper_;
289 upper_ = tmp;
290 }
291}
292
293
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000294#ifdef DEBUG
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000295void Range::Verify() const {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000296 DCHECK(lower_ <= upper_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000297}
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000298#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000299
300
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000301bool Range::MulAndCheckOverflow(const Representation& r, Range* other) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000302 bool may_overflow = false;
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +0000303 int v1 = MulWithoutOverflow(r, lower_, other->lower(), &may_overflow);
304 int v2 = MulWithoutOverflow(r, lower_, other->upper(), &may_overflow);
305 int v3 = MulWithoutOverflow(r, upper_, other->lower(), &may_overflow);
306 int v4 = MulWithoutOverflow(r, upper_, other->upper(), &may_overflow);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000307 lower_ = Min(Min(v1, v2), Min(v3, v4));
308 upper_ = Max(Max(v1, v2), Max(v3, v4));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000309#ifdef DEBUG
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000310 Verify();
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000311#endif
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000312 return may_overflow;
313}
314
315
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000316bool HValue::IsDefinedAfter(HBasicBlock* other) const {
317 return block()->block_id() > other->block_id();
318}
319
320
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000321HUseListNode* HUseListNode::tail() {
322 // Skip and remove dead items in the use list.
323 while (tail_ != NULL && tail_->value()->CheckFlag(HValue::kIsDead)) {
324 tail_ = tail_->tail_;
325 }
326 return tail_;
327}
328
329
danno@chromium.orgc00ec2b2013-08-14 17:13:49 +0000330bool HValue::CheckUsesForFlag(Flag f) const {
ulan@chromium.org812308e2012-02-29 15:58:45 +0000331 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +0000332 if (it.value()->IsSimulate()) continue;
ulan@chromium.org812308e2012-02-29 15:58:45 +0000333 if (!it.value()->CheckFlag(f)) return false;
334 }
335 return true;
336}
337
338
verwaest@chromium.org662436e2013-08-28 08:41:27 +0000339bool HValue::CheckUsesForFlag(Flag f, HValue** value) const {
340 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
341 if (it.value()->IsSimulate()) continue;
342 if (!it.value()->CheckFlag(f)) {
343 *value = it.value();
344 return false;
345 }
346 }
347 return true;
348}
349
350
danno@chromium.orgc00ec2b2013-08-14 17:13:49 +0000351bool HValue::HasAtLeastOneUseWithFlagAndNoneWithout(Flag f) const {
ulan@chromium.org837a67e2013-06-11 15:39:48 +0000352 bool return_value = false;
353 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
354 if (it.value()->IsSimulate()) continue;
355 if (!it.value()->CheckFlag(f)) return false;
356 return_value = true;
357 }
358 return return_value;
359}
360
361
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000362HUseIterator::HUseIterator(HUseListNode* head) : next_(head) {
363 Advance();
364}
365
366
367void HUseIterator::Advance() {
368 current_ = next_;
369 if (current_ != NULL) {
370 next_ = current_->tail();
371 value_ = current_->value();
372 index_ = current_->index();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000373 }
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000374}
375
376
377int HValue::UseCount() const {
378 int count = 0;
379 for (HUseIterator it(uses()); !it.Done(); it.Advance()) ++count;
380 return count;
381}
382
383
384HUseListNode* HValue::RemoveUse(HValue* value, int index) {
385 HUseListNode* previous = NULL;
386 HUseListNode* current = use_list_;
387 while (current != NULL) {
388 if (current->value() == value && current->index() == index) {
389 if (previous == NULL) {
390 use_list_ = current->tail();
391 } else {
392 previous->set_tail(current->tail());
393 }
394 break;
395 }
396
397 previous = current;
398 current = current->tail();
399 }
400
401#ifdef DEBUG
402 // Do not reuse use list nodes in debug mode, zap them.
403 if (current != NULL) {
404 HUseListNode* temp =
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000405 new(block()->zone())
406 HUseListNode(current->value(), current->index(), NULL);
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000407 current->Zap();
408 current = temp;
409 }
410#endif
411 return current;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000412}
413
414
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000415bool HValue::Equals(HValue* other) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000416 if (other->opcode() != opcode()) return false;
417 if (!other->representation().Equals(representation())) return false;
418 if (!other->type_.Equals(type_)) return false;
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000419 if (other->flags() != flags()) return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000420 if (OperandCount() != other->OperandCount()) return false;
421 for (int i = 0; i < OperandCount(); ++i) {
422 if (OperandAt(i)->id() != other->OperandAt(i)->id()) return false;
423 }
424 bool result = DataEquals(other);
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000425 DCHECK(!result || Hashcode() == other->Hashcode());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000426 return result;
427}
428
429
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000430intptr_t HValue::Hashcode() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000431 intptr_t result = opcode();
432 int count = OperandCount();
433 for (int i = 0; i < count; ++i) {
434 result = result * 19 + OperandAt(i)->id() + (result >> 7);
435 }
436 return result;
437}
438
439
ricow@chromium.orgdcebac02011-04-20 09:44:50 +0000440const char* HValue::Mnemonic() const {
441 switch (opcode()) {
442#define MAKE_CASE(type) case k##type: return #type;
443 HYDROGEN_CONCRETE_INSTRUCTION_LIST(MAKE_CASE)
444#undef MAKE_CASE
445 case kPhi: return "Phi";
446 default: return "";
447 }
448}
449
450
jkummerow@chromium.orgd8a3a142013-10-03 12:15:05 +0000451bool HValue::CanReplaceWithDummyUses() {
452 return FLAG_unreachable_code_elimination &&
453 !(block()->IsReachable() ||
454 IsBlockEntry() ||
455 IsControlInstruction() ||
machenbach@chromium.org2ebef182014-04-14 00:05:03 +0000456 IsArgumentsObject() ||
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000457 IsCapturedObject() ||
jkummerow@chromium.orgd8a3a142013-10-03 12:15:05 +0000458 IsSimulate() ||
459 IsEnterInlined() ||
460 IsLeaveInlined());
461}
462
463
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000464bool HValue::IsInteger32Constant() {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +0000465 return IsConstant() && HConstant::cast(this)->HasInteger32Value();
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000466}
467
468
469int32_t HValue::GetInteger32Constant() {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +0000470 return HConstant::cast(this)->Integer32Value();
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000471}
472
473
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +0000474bool HValue::EqualsInteger32Constant(int32_t value) {
475 return IsInteger32Constant() && GetInteger32Constant() == value;
476}
477
478
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000479void HValue::SetOperandAt(int index, HValue* value) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000480 RegisterUse(index, value);
481 InternalSetOperandAt(index, value);
482}
483
484
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000485void HValue::DeleteAndReplaceWith(HValue* other) {
486 // We replace all uses first, so Delete can assert that there are none.
487 if (other != NULL) ReplaceAllUsesWith(other);
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000488 Kill();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000489 DeleteFromGraph();
490}
491
492
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000493void HValue::ReplaceAllUsesWith(HValue* other) {
494 while (use_list_ != NULL) {
495 HUseListNode* list_node = use_list_;
496 HValue* value = list_node->value();
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000497 DCHECK(!value->block()->IsStartBlock());
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000498 value->InternalSetOperandAt(list_node->index(), other);
499 use_list_ = list_node->tail();
500 list_node->set_tail(other->use_list_);
501 other->use_list_ = list_node;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000502 }
503}
504
505
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000506void HValue::Kill() {
507 // Instead of going through the entire use list of each operand, we only
508 // check the first item in each use list and rely on the tail() method to
509 // skip dead items, removing them lazily next time we traverse the list.
510 SetFlag(kIsDead);
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000511 for (int i = 0; i < OperandCount(); ++i) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000512 HValue* operand = OperandAt(i);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000513 if (operand == NULL) continue;
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000514 HUseListNode* first = operand->use_list_;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000515 if (first != NULL && first->value()->CheckFlag(kIsDead)) {
yangguo@chromium.orgab30bb82012-02-24 14:41:46 +0000516 operand->use_list_ = first->tail();
517 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000518 }
519}
520
521
522void HValue::SetBlock(HBasicBlock* block) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000523 DCHECK(block_ == NULL || block == NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000524 block_ = block;
525 if (id_ == kNoNumber && block != NULL) {
526 id_ = block->graph()->GetNextValueID(this);
527 }
528}
529
530
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000531OStream& operator<<(OStream& os, const HValue& v) { return v.PrintTo(os); }
532
533
534OStream& operator<<(OStream& os, const TypeOf& t) {
535 if (t.value->representation().IsTagged() &&
536 !t.value->type().Equals(HType::Tagged()))
537 return os;
538 return os << " type:" << t.value->type();
machenbach@chromium.orgd0bddc62014-07-07 00:05:07 +0000539}
540
541
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000542OStream& operator<<(OStream& os, const ChangesOf& c) {
543 GVNFlagSet changes_flags = c.value->ChangesFlags();
544 if (changes_flags.IsEmpty()) return os;
545 os << " changes[";
546 if (changes_flags == c.value->AllSideEffectsFlagSet()) {
547 os << "*";
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000548 } else {
549 bool add_comma = false;
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000550#define PRINT_DO(Type) \
551 if (changes_flags.Contains(k##Type)) { \
552 if (add_comma) os << ","; \
553 add_comma = true; \
554 os << #Type; \
555 }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000556 GVN_TRACKED_FLAG_LIST(PRINT_DO);
557 GVN_UNTRACKED_FLAG_LIST(PRINT_DO);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000558#undef PRINT_DO
559 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000560 return os << "]";
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000561}
562
563
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000564bool HValue::HasMonomorphicJSObjectType() {
565 return !GetMonomorphicJSObjectMap().is_null();
566}
567
568
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000569bool HValue::UpdateInferredType() {
570 HType type = CalculateInferredType();
571 bool result = (!type.Equals(type_));
572 type_ = type;
573 return result;
574}
575
576
577void HValue::RegisterUse(int index, HValue* new_value) {
578 HValue* old_value = OperandAt(index);
579 if (old_value == new_value) return;
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000580
581 HUseListNode* removed = NULL;
582 if (old_value != NULL) {
583 removed = old_value->RemoveUse(this, index);
584 }
585
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000586 if (new_value != NULL) {
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000587 if (removed == NULL) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000588 new_value->use_list_ = new(new_value->block()->zone()) HUseListNode(
589 this, index, new_value->use_list_);
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +0000590 } else {
591 removed->set_tail(new_value->use_list_);
592 new_value->use_list_ = removed;
593 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000594 }
595}
596
597
ulan@chromium.org812308e2012-02-29 15:58:45 +0000598void HValue::AddNewRange(Range* r, Zone* zone) {
599 if (!HasRange()) ComputeInitialRange(zone);
600 if (!HasRange()) range_ = new(zone) Range();
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000601 DCHECK(HasRange());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000602 r->StackUpon(range_);
603 range_ = r;
604}
605
606
607void HValue::RemoveLastAddedRange() {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000608 DCHECK(HasRange());
609 DCHECK(range_->next() != NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000610 range_ = range_->next();
611}
612
613
ulan@chromium.org812308e2012-02-29 15:58:45 +0000614void HValue::ComputeInitialRange(Zone* zone) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000615 DCHECK(!HasRange());
ulan@chromium.org812308e2012-02-29 15:58:45 +0000616 range_ = InferRange(zone);
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000617 DCHECK(HasRange());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000618}
619
620
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000621OStream& operator<<(OStream& os, const HSourcePosition& p) {
622 if (p.IsUnknown()) {
623 return os << "<?>";
624 } else if (FLAG_hydrogen_track_positions) {
625 return os << "<" << p.inlining_id() << ":" << p.position() << ">";
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000626 } else {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000627 return os << "<0:" << p.raw() << ">";
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000628 }
629}
630
631
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000632OStream& HInstruction::PrintTo(OStream& os) const { // NOLINT
633 os << Mnemonic() << " ";
634 PrintDataTo(os) << ChangesOf(this) << TypeOf(this);
635 if (CheckFlag(HValue::kHasNoObservableSideEffects)) os << " [noOSE]";
636 if (CheckFlag(HValue::kIsDead)) os << " [dead]";
637 return os;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000638}
639
640
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000641OStream& HInstruction::PrintDataTo(OStream& os) const { // NOLINT
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000642 for (int i = 0; i < OperandCount(); ++i) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000643 if (i > 0) os << " ";
644 os << NameOf(OperandAt(i));
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000645 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000646 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000647}
648
649
650void HInstruction::Unlink() {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000651 DCHECK(IsLinked());
652 DCHECK(!IsControlInstruction()); // Must never move control instructions.
653 DCHECK(!IsBlockEntry()); // Doesn't make sense to delete these.
654 DCHECK(previous_ != NULL);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000655 previous_->next_ = next_;
656 if (next_ == NULL) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000657 DCHECK(block()->last() == this);
kmillikin@chromium.org49edbdf2011-02-16 12:32:18 +0000658 block()->set_last(previous_);
659 } else {
660 next_->previous_ = previous_;
661 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000662 clear_block();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000663}
664
665
666void HInstruction::InsertBefore(HInstruction* next) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000667 DCHECK(!IsLinked());
668 DCHECK(!next->IsBlockEntry());
669 DCHECK(!IsControlInstruction());
670 DCHECK(!next->block()->IsStartBlock());
671 DCHECK(next->previous_ != NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000672 HInstruction* prev = next->previous();
673 prev->next_ = this;
674 next->previous_ = this;
675 next_ = next;
676 previous_ = prev;
677 SetBlock(next->block());
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000678 if (!has_position() && next->has_position()) {
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000679 set_position(next->position());
680 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000681}
682
683
684void HInstruction::InsertAfter(HInstruction* previous) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000685 DCHECK(!IsLinked());
686 DCHECK(!previous->IsControlInstruction());
687 DCHECK(!IsControlInstruction() || previous->next_ == NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000688 HBasicBlock* block = previous->block();
689 // Never insert anything except constants into the start block after finishing
690 // it.
691 if (block->IsStartBlock() && block->IsFinished() && !IsConstant()) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000692 DCHECK(block->end()->SecondSuccessor() == NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000693 InsertAfter(block->end()->FirstSuccessor()->first());
694 return;
695 }
696
697 // If we're inserting after an instruction with side-effects that is
698 // followed by a simulate instruction, we need to insert after the
699 // simulate instruction instead.
700 HInstruction* next = previous->next_;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000701 if (previous->HasObservableSideEffects() && next != NULL) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000702 DCHECK(next->IsSimulate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000703 previous = next;
704 next = previous->next_;
705 }
706
707 previous_ = previous;
708 next_ = next;
709 SetBlock(block);
710 previous->next_ = this;
711 if (next != NULL) next->previous_ = this;
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000712 if (block->last() == previous) {
713 block->set_last(this);
714 }
titzer@chromium.orgf5a24542014-03-04 09:06:17 +0000715 if (!has_position() && previous->has_position()) {
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +0000716 set_position(previous->position());
717 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000718}
719
720
machenbach@chromium.org38de99a2014-06-05 00:04:53 +0000721bool HInstruction::Dominates(HInstruction* other) {
722 if (block() != other->block()) {
723 return block()->Dominates(other->block());
724 }
725 // Both instructions are in the same basic block. This instruction
726 // should precede the other one in order to dominate it.
727 for (HInstruction* instr = next(); instr != NULL; instr = instr->next()) {
728 if (instr == other) {
729 return true;
730 }
731 }
732 return false;
733}
734
735
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000736#ifdef DEBUG
ager@chromium.org378b34e2011-01-28 08:04:38 +0000737void HInstruction::Verify() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000738 // Verify that input operands are defined before use.
739 HBasicBlock* cur_block = block();
740 for (int i = 0; i < OperandCount(); ++i) {
741 HValue* other_operand = OperandAt(i);
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +0000742 if (other_operand == NULL) continue;
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000743 HBasicBlock* other_block = other_operand->block();
744 if (cur_block == other_block) {
745 if (!other_operand->IsPhi()) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000746 HInstruction* cur = this->previous();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000747 while (cur != NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000748 if (cur == other_operand) break;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000749 cur = cur->previous();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000750 }
751 // Must reach other operand in the same block!
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000752 DCHECK(cur == other_operand);
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000753 }
754 } else {
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000755 // If the following assert fires, you may have forgotten an
756 // AddInstruction.
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000757 DCHECK(other_block->Dominates(cur_block));
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000758 }
759 }
760
761 // Verify that instructions that may have side-effects are followed
762 // by a simulate instruction.
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +0000763 if (HasObservableSideEffects() && !IsOsrEntry()) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000764 DCHECK(next()->IsSimulate());
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000765 }
ager@chromium.org378b34e2011-01-28 08:04:38 +0000766
767 // Verify that instructions that can be eliminated by GVN have overridden
768 // HValue::DataEquals. The default implementation is UNREACHABLE. We
769 // don't actually care whether DataEquals returns true or false here.
770 if (CheckFlag(kUseGVN)) DataEquals(this);
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000771
772 // Verify that all uses are in the graph.
773 for (HUseIterator use = uses(); !use.Done(); use.Advance()) {
774 if (use.value()->IsInstruction()) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000775 DCHECK(HInstruction::cast(use.value())->IsLinked());
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000776 }
777 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000778}
779#endif
780
781
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000782bool HInstruction::CanDeoptimize() {
783 // TODO(titzer): make this a virtual method?
784 switch (opcode()) {
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000785 case HValue::kAbnormalExit:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000786 case HValue::kAccessArgumentsAt:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000787 case HValue::kAllocate:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000788 case HValue::kArgumentsElements:
789 case HValue::kArgumentsLength:
790 case HValue::kArgumentsObject:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000791 case HValue::kBlockEntry:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000792 case HValue::kBoundsCheckBaseIndexInformation:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000793 case HValue::kCallFunction:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000794 case HValue::kCallNew:
795 case HValue::kCallNewArray:
796 case HValue::kCallStub:
797 case HValue::kCallWithDescriptor:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000798 case HValue::kCapturedObject:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000799 case HValue::kClassOfTestAndBranch:
800 case HValue::kCompareGeneric:
801 case HValue::kCompareHoleAndBranch:
802 case HValue::kCompareMap:
803 case HValue::kCompareMinusZeroAndBranch:
804 case HValue::kCompareNumericAndBranch:
805 case HValue::kCompareObjectEqAndBranch:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000806 case HValue::kConstant:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000807 case HValue::kConstructDouble:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000808 case HValue::kContext:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000809 case HValue::kDebugBreak:
810 case HValue::kDeclareGlobals:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000811 case HValue::kDoubleBits:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000812 case HValue::kDummyUse:
813 case HValue::kEnterInlined:
814 case HValue::kEnvironmentMarker:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000815 case HValue::kForceRepresentation:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000816 case HValue::kGetCachedArrayIndex:
817 case HValue::kGoto:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000818 case HValue::kHasCachedArrayIndexAndBranch:
819 case HValue::kHasInstanceTypeAndBranch:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000820 case HValue::kInnerAllocatedObject:
821 case HValue::kInstanceOf:
822 case HValue::kInstanceOfKnownGlobal:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000823 case HValue::kIsConstructCallAndBranch:
824 case HValue::kIsObjectAndBranch:
825 case HValue::kIsSmiAndBranch:
826 case HValue::kIsStringAndBranch:
827 case HValue::kIsUndetectableAndBranch:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000828 case HValue::kLeaveInlined:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000829 case HValue::kLoadFieldByIndex:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000830 case HValue::kLoadGlobalGeneric:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000831 case HValue::kLoadNamedField:
832 case HValue::kLoadNamedGeneric:
833 case HValue::kLoadRoot:
834 case HValue::kMapEnumLength:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000835 case HValue::kMathMinMax:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000836 case HValue::kParameter:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000837 case HValue::kPhi:
machenbach@chromium.org011a81f2014-05-26 00:04:51 +0000838 case HValue::kPushArguments:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000839 case HValue::kRegExpLiteral:
840 case HValue::kReturn:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000841 case HValue::kSeqStringGetChar:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000842 case HValue::kStoreCodeEntry:
machenbach@chromium.org1e2d50c2014-06-06 00:04:56 +0000843 case HValue::kStoreFrameContext:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000844 case HValue::kStoreKeyed:
machenbach@chromium.orgd06b9262014-05-28 06:48:05 +0000845 case HValue::kStoreNamedField:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000846 case HValue::kStoreNamedGeneric:
847 case HValue::kStringCharCodeAt:
848 case HValue::kStringCharFromCode:
machenbach@chromium.orge20e19e2014-09-09 00:05:04 +0000849 case HValue::kTailCallThroughMegamorphicCache:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000850 case HValue::kThisFunction:
851 case HValue::kTypeofIsAndBranch:
852 case HValue::kUnknownOSRValue:
853 case HValue::kUseConst:
854 return false;
855
856 case HValue::kAdd:
machenbach@chromium.org1e2d50c2014-06-06 00:04:56 +0000857 case HValue::kAllocateBlockContext:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000858 case HValue::kApplyArguments:
859 case HValue::kBitwise:
860 case HValue::kBoundsCheck:
861 case HValue::kBranch:
machenbach@chromium.org8d8413c2014-06-03 00:04:55 +0000862 case HValue::kCallJSFunction:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000863 case HValue::kCallRuntime:
864 case HValue::kChange:
865 case HValue::kCheckHeapObject:
866 case HValue::kCheckInstanceType:
867 case HValue::kCheckMapValue:
868 case HValue::kCheckMaps:
869 case HValue::kCheckSmi:
870 case HValue::kCheckValue:
871 case HValue::kClampToUint8:
872 case HValue::kDateField:
873 case HValue::kDeoptimize:
874 case HValue::kDiv:
875 case HValue::kForInCacheArray:
876 case HValue::kForInPrepareMap:
877 case HValue::kFunctionLiteral:
878 case HValue::kInvokeFunction:
879 case HValue::kLoadContextSlot:
880 case HValue::kLoadFunctionPrototype:
881 case HValue::kLoadGlobalCell:
882 case HValue::kLoadKeyed:
883 case HValue::kLoadKeyedGeneric:
884 case HValue::kMathFloorOfDiv:
885 case HValue::kMod:
886 case HValue::kMul:
887 case HValue::kOsrEntry:
888 case HValue::kPower:
machenbach@chromium.orgeac65cd2014-06-02 00:04:30 +0000889 case HValue::kRor:
890 case HValue::kSar:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000891 case HValue::kSeqStringSetChar:
892 case HValue::kShl:
893 case HValue::kShr:
894 case HValue::kSimulate:
895 case HValue::kStackCheck:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000896 case HValue::kStoreContextSlot:
897 case HValue::kStoreGlobalCell:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000898 case HValue::kStoreKeyedGeneric:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000899 case HValue::kStringAdd:
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000900 case HValue::kStringCompareAndBranch:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000901 case HValue::kSub:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000902 case HValue::kToFastProperties:
903 case HValue::kTransitionElementsKind:
904 case HValue::kTrapAllocationMemento:
905 case HValue::kTypeof:
906 case HValue::kUnaryMathOperation:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000907 case HValue::kWrapReceiver:
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000908 return true;
909 }
machenbach@chromium.org255043f2014-04-04 00:04:59 +0000910 UNREACHABLE();
911 return true;
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +0000912}
913
914
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000915OStream& operator<<(OStream& os, const NameOf& v) {
916 return os << v.value->representation().Mnemonic() << v.value->id();
917}
918
919OStream& HDummyUse::PrintDataTo(OStream& os) const { // NOLINT
920 return os << NameOf(value());
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000921}
922
923
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000924OStream& HEnvironmentMarker::PrintDataTo(OStream& os) const { // NOLINT
925 return os << (kind() == BIND ? "bind" : "lookup") << " var[" << index()
926 << "]";
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +0000927}
928
929
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000930OStream& HUnaryCall::PrintDataTo(OStream& os) const { // NOLINT
931 return os << NameOf(value()) << " #" << argument_count();
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000932}
933
934
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000935OStream& HCallJSFunction::PrintDataTo(OStream& os) const { // NOLINT
936 return os << NameOf(function()) << " #" << argument_count();
machenbach@chromium.org26ca35c2014-01-16 08:22:55 +0000937}
938
939
940HCallJSFunction* HCallJSFunction::New(
941 Zone* zone,
942 HValue* context,
943 HValue* function,
944 int argument_count,
945 bool pass_argument_count) {
946 bool has_stack_check = false;
947 if (function->IsConstant()) {
948 HConstant* fun_const = HConstant::cast(function);
949 Handle<JSFunction> jsfun =
950 Handle<JSFunction>::cast(fun_const->handle(zone->isolate()));
951 has_stack_check = !jsfun.is_null() &&
952 (jsfun->code()->kind() == Code::FUNCTION ||
953 jsfun->code()->kind() == Code::OPTIMIZED_FUNCTION);
954 }
955
956 return new(zone) HCallJSFunction(
957 function, argument_count, pass_argument_count,
958 has_stack_check);
959}
960
961
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +0000962OStream& HBinaryCall::PrintDataTo(OStream& os) const { // NOLINT
963 return os << NameOf(first()) << " " << NameOf(second()) << " #"
964 << argument_count();
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +0000965}
966
967
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000968void HBoundsCheck::ApplyIndexChange() {
969 if (skip_check()) return;
970
971 DecompositionResult decomposition;
972 bool index_is_decomposable = index()->TryDecompose(&decomposition);
973 if (index_is_decomposable) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +0000974 DCHECK(decomposition.base() == base());
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000975 if (decomposition.offset() == offset() &&
976 decomposition.scale() == scale()) return;
977 } else {
978 return;
979 }
980
981 ReplaceAllUsesWith(index());
982
983 HValue* current_index = decomposition.base();
984 int actual_offset = decomposition.offset() + offset();
985 int actual_scale = decomposition.scale() + scale();
986
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000987 Zone* zone = block()->graph()->zone();
988 HValue* context = block()->graph()->GetInvalidContext();
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000989 if (actual_offset != 0) {
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000990 HConstant* add_offset = HConstant::New(zone, context, actual_offset);
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000991 add_offset->InsertBefore(this);
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000992 HInstruction* add = HAdd::New(zone, context,
993 current_index, add_offset);
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000994 add->InsertBefore(this);
995 add->AssumeRepresentation(index()->representation());
danno@chromium.org1fd77d52013-06-07 16:01:45 +0000996 add->ClearFlag(kCanOverflow);
svenpanne@chromium.org876cca82013-03-18 14:43:20 +0000997 current_index = add;
998 }
999
1000 if (actual_scale != 0) {
danno@chromium.orgd3c42102013-08-01 16:58:23 +00001001 HConstant* sar_scale = HConstant::New(zone, context, actual_scale);
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001002 sar_scale->InsertBefore(this);
danno@chromium.orgd3c42102013-08-01 16:58:23 +00001003 HInstruction* sar = HSar::New(zone, context,
1004 current_index, sar_scale);
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001005 sar->InsertBefore(this);
1006 sar->AssumeRepresentation(index()->representation());
1007 current_index = sar;
1008 }
1009
1010 SetOperandAt(0, current_index);
1011
1012 base_ = NULL;
1013 offset_ = 0;
1014 scale_ = 0;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +00001015}
1016
1017
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001018OStream& HBoundsCheck::PrintDataTo(OStream& os) const { // NOLINT
1019 os << NameOf(index()) << " " << NameOf(length());
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001020 if (base() != NULL && (offset() != 0 || scale() != 0)) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001021 os << " base: ((";
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001022 if (base() != index()) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001023 os << NameOf(index());
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001024 } else {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001025 os << "index";
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001026 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001027 os << " + " << offset() << ") >> " << scale() << ")";
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001028 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001029 if (skip_check()) os << " [DISABLED]";
1030 return os;
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001031}
1032
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001033
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001034void HBoundsCheck::InferRepresentation(HInferRepresentationPhase* h_infer) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00001035 DCHECK(CheckFlag(kFlexibleRepresentation));
ulan@chromium.org750145a2013-03-07 15:14:13 +00001036 HValue* actual_index = index()->ActualValue();
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001037 HValue* actual_length = length()->ActualValue();
1038 Representation index_rep = actual_index->representation();
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001039 Representation length_rep = actual_length->representation();
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001040 if (index_rep.IsTagged() && actual_index->type().IsSmi()) {
1041 index_rep = Representation::Smi();
1042 }
1043 if (length_rep.IsTagged() && actual_length->type().IsSmi()) {
1044 length_rep = Representation::Smi();
1045 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00001046 Representation r = index_rep.generalize(length_rep);
1047 if (r.is_more_general_than(Representation::Integer32())) {
mstarzinger@chromium.org068ea0a2013-01-30 09:39:44 +00001048 r = Representation::Integer32();
1049 }
1050 UpdateRepresentation(r, h_infer, "boundscheck");
1051}
1052
1053
machenbach@chromium.orgc86e8c22013-11-27 15:11:04 +00001054Range* HBoundsCheck::InferRange(Zone* zone) {
1055 Representation r = representation();
1056 if (r.IsSmiOrInteger32() && length()->HasRange()) {
1057 int upper = length()->range()->upper() - (allow_equality() ? 0 : 1);
1058 int lower = 0;
1059
1060 Range* result = new(zone) Range(lower, upper);
1061 if (index()->HasRange()) {
1062 result->Intersect(index()->range());
1063 }
1064
1065 // In case of Smi representation, clamp result to Smi::kMaxValue.
1066 if (r.IsSmi()) result->ClampToSmi();
1067 return result;
1068 }
1069 return HValue::InferRange(zone);
1070}
1071
1072
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001073OStream& HBoundsCheckBaseIndexInformation::PrintDataTo(
1074 OStream& os) const { // NOLINT
1075 // TODO(svenpanne) This 2nd base_index() looks wrong...
1076 return os << "base: " << NameOf(base_index())
1077 << ", check: " << NameOf(base_index());
svenpanne@chromium.org876cca82013-03-18 14:43:20 +00001078}
1079
1080
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001081OStream& HCallWithDescriptor::PrintDataTo(OStream& os) const { // NOLINT
machenbach@chromium.org26ca35c2014-01-16 08:22:55 +00001082 for (int i = 0; i < OperandCount(); i++) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001083 os << NameOf(OperandAt(i)) << " ";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001084 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001085 return os << "#" << argument_count();
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001086}
1087
1088
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001089OStream& HCallNewArray::PrintDataTo(OStream& os) const { // NOLINT
1090 os << ElementsKindToString(elements_kind()) << " ";
1091 return HBinaryCall::PrintDataTo(os);
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001092}
1093
1094
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001095OStream& HCallRuntime::PrintDataTo(OStream& os) const { // NOLINT
1096 os << name()->ToCString().get() << " ";
1097 if (save_doubles() == kSaveFPRegs) os << "[save doubles] ";
1098 return os << "#" << argument_count();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001099}
1100
1101
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001102OStream& HClassOfTestAndBranch::PrintDataTo(OStream& os) const { // NOLINT
1103 return os << "class_of_test(" << NameOf(value()) << ", \""
1104 << class_name()->ToCString().get() << "\")";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001105}
1106
1107
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001108OStream& HWrapReceiver::PrintDataTo(OStream& os) const { // NOLINT
1109 return os << NameOf(receiver()) << " " << NameOf(function());
yangguo@chromium.org39110192013-01-16 09:55:08 +00001110}
1111
1112
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001113OStream& HAccessArgumentsAt::PrintDataTo(OStream& os) const { // NOLINT
1114 return os << NameOf(arguments()) << "[" << NameOf(index()) << "], length "
1115 << NameOf(length());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001116}
1117
1118
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001119OStream& HAllocateBlockContext::PrintDataTo(OStream& os) const { // NOLINT
1120 return os << NameOf(context()) << " " << NameOf(function());
machenbach@chromium.org1e2d50c2014-06-06 00:04:56 +00001121}
1122
1123
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001124OStream& HControlInstruction::PrintDataTo(OStream& os) const { // NOLINT
1125 os << " goto (";
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001126 bool first_block = true;
1127 for (HSuccessorIterator it(this); !it.Done(); it.Advance()) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001128 if (!first_block) os << ", ";
1129 os << *it.Current();
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001130 first_block = false;
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001131 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001132 return os << ")";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001133}
1134
1135
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001136OStream& HUnaryControlInstruction::PrintDataTo(OStream& os) const { // NOLINT
1137 os << NameOf(value());
1138 return HControlInstruction::PrintDataTo(os);
vegorov@chromium.org0a4e9012011-01-24 12:33:13 +00001139}
1140
1141
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001142OStream& HReturn::PrintDataTo(OStream& os) const { // NOLINT
1143 return os << NameOf(value()) << " (pop " << NameOf(parameter_count())
1144 << " values)";
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +00001145}
1146
1147
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001148Representation HBranch::observed_input_representation(int index) {
1149 static const ToBooleanStub::Types tagged_types(
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001150 ToBooleanStub::NULL_TYPE |
1151 ToBooleanStub::SPEC_OBJECT |
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00001152 ToBooleanStub::STRING |
1153 ToBooleanStub::SYMBOL);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001154 if (expected_input_types_.ContainsAnyOf(tagged_types)) {
1155 return Representation::Tagged();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001156 }
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001157 if (expected_input_types_.Contains(ToBooleanStub::UNDEFINED)) {
1158 if (expected_input_types_.Contains(ToBooleanStub::HEAP_NUMBER)) {
1159 return Representation::Double();
1160 }
1161 return Representation::Tagged();
1162 }
1163 if (expected_input_types_.Contains(ToBooleanStub::HEAP_NUMBER)) {
1164 return Representation::Double();
1165 }
1166 if (expected_input_types_.Contains(ToBooleanStub::SMI)) {
1167 return Representation::Smi();
1168 }
1169 return Representation::None();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00001170}
1171
1172
jkummerow@chromium.orgd8a3a142013-10-03 12:15:05 +00001173bool HBranch::KnownSuccessorBlock(HBasicBlock** block) {
1174 HValue* value = this->value();
1175 if (value->EmitAtUses()) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00001176 DCHECK(value->IsConstant());
1177 DCHECK(!value->representation().IsDouble());
jkummerow@chromium.orgd8a3a142013-10-03 12:15:05 +00001178 *block = HConstant::cast(value)->BooleanValue()
1179 ? FirstSuccessor()
1180 : SecondSuccessor();
1181 return true;
1182 }
1183 *block = NULL;
1184 return false;
1185}
1186
1187
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001188OStream& HBranch::PrintDataTo(OStream& os) const { // NOLINT
1189 return HUnaryControlInstruction::PrintDataTo(os) << " "
1190 << expected_input_types();
machenbach@chromium.orga77ec9c2014-04-23 00:05:14 +00001191}
1192
1193
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001194OStream& HCompareMap::PrintDataTo(OStream& os) const { // NOLINT
1195 os << NameOf(value()) << " (" << *map().handle() << ")";
1196 HControlInstruction::PrintDataTo(os);
machenbach@chromium.org82975302014-02-06 01:06:18 +00001197 if (known_successor_index() == 0) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001198 os << " [true]";
machenbach@chromium.org82975302014-02-06 01:06:18 +00001199 } else if (known_successor_index() == 1) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001200 os << " [false]";
machenbach@chromium.org82975302014-02-06 01:06:18 +00001201 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001202 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001203}
1204
1205
1206const char* HUnaryMathOperation::OpName() const {
1207 switch (op()) {
machenbach@chromium.orgdc207d92014-07-30 00:05:07 +00001208 case kMathFloor:
1209 return "floor";
1210 case kMathFround:
1211 return "fround";
1212 case kMathRound:
1213 return "round";
1214 case kMathAbs:
1215 return "abs";
1216 case kMathLog:
1217 return "log";
1218 case kMathExp:
1219 return "exp";
1220 case kMathSqrt:
1221 return "sqrt";
1222 case kMathPowHalf:
1223 return "pow-half";
1224 case kMathClz32:
1225 return "clz32";
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001226 default:
1227 UNREACHABLE();
1228 return NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001229 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001230}
1231
1232
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001233Range* HUnaryMathOperation::InferRange(Zone* zone) {
1234 Representation r = representation();
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001235 if (op() == kMathClz32) return new(zone) Range(0, 32);
danno@chromium.org1fd77d52013-06-07 16:01:45 +00001236 if (r.IsSmiOrInteger32() && value()->HasRange()) {
1237 if (op() == kMathAbs) {
1238 int upper = value()->range()->upper();
1239 int lower = value()->range()->lower();
1240 bool spans_zero = value()->range()->CanBeZero();
1241 // Math.abs(kMinInt) overflows its representation, on which the
1242 // instruction deopts. Hence clamp it to kMaxInt.
1243 int abs_upper = upper == kMinInt ? kMaxInt : abs(upper);
1244 int abs_lower = lower == kMinInt ? kMaxInt : abs(lower);
1245 Range* result =
1246 new(zone) Range(spans_zero ? 0 : Min(abs_lower, abs_upper),
1247 Max(abs_lower, abs_upper));
1248 // In case of Smi representation, clamp Math.abs(Smi::kMinValue) to
1249 // Smi::kMaxValue.
1250 if (r.IsSmi()) result->ClampToSmi();
1251 return result;
1252 }
1253 }
1254 return HValue::InferRange(zone);
1255}
1256
1257
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001258OStream& HUnaryMathOperation::PrintDataTo(OStream& os) const { // NOLINT
1259 return os << OpName() << " " << NameOf(value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001260}
1261
1262
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001263OStream& HUnaryOperation::PrintDataTo(OStream& os) const { // NOLINT
1264 return os << NameOf(value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001265}
1266
1267
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001268OStream& HHasInstanceTypeAndBranch::PrintDataTo(OStream& os) const { // NOLINT
1269 os << NameOf(value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001270 switch (from_) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001271 case FIRST_JS_RECEIVER_TYPE:
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001272 if (to_ == LAST_TYPE) os << " spec_object";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001273 break;
1274 case JS_REGEXP_TYPE:
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001275 if (to_ == JS_REGEXP_TYPE) os << " reg_exp";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001276 break;
1277 case JS_ARRAY_TYPE:
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001278 if (to_ == JS_ARRAY_TYPE) os << " array";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001279 break;
1280 case JS_FUNCTION_TYPE:
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001281 if (to_ == JS_FUNCTION_TYPE) os << " function";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001282 break;
1283 default:
1284 break;
1285 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001286 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001287}
1288
1289
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001290OStream& HTypeofIsAndBranch::PrintDataTo(OStream& os) const { // NOLINT
1291 os << NameOf(value()) << " == " << type_literal()->ToCString().get();
1292 return HControlInstruction::PrintDataTo(os);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001293}
1294
1295
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00001296static String* TypeOfString(HConstant* constant, Isolate* isolate) {
1297 Heap* heap = isolate->heap();
1298 if (constant->HasNumberValue()) return heap->number_string();
1299 if (constant->IsUndetectable()) return heap->undefined_string();
1300 if (constant->HasStringValue()) return heap->string_string();
1301 switch (constant->GetInstanceType()) {
1302 case ODDBALL_TYPE: {
1303 Unique<Object> unique = constant->GetUnique();
1304 if (unique.IsKnownGlobal(heap->true_value()) ||
1305 unique.IsKnownGlobal(heap->false_value())) {
1306 return heap->boolean_string();
1307 }
1308 if (unique.IsKnownGlobal(heap->null_value())) {
machenbach@chromium.org9d1a7a82014-07-22 00:04:43 +00001309 return heap->object_string();
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00001310 }
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00001311 DCHECK(unique.IsKnownGlobal(heap->undefined_value()));
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00001312 return heap->undefined_string();
machenbach@chromium.orgaf9cfcb2013-11-19 11:05:18 +00001313 }
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00001314 case SYMBOL_TYPE:
1315 return heap->symbol_string();
1316 case JS_FUNCTION_TYPE:
1317 case JS_FUNCTION_PROXY_TYPE:
1318 return heap->function_string();
1319 default:
1320 return heap->object_string();
1321 }
1322}
1323
1324
1325bool HTypeofIsAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
1326 if (FLAG_fold_constants && value()->IsConstant()) {
1327 HConstant* constant = HConstant::cast(value());
1328 String* type_string = TypeOfString(constant, isolate());
1329 bool same_type = type_literal_.IsKnownGlobal(type_string);
1330 *block = same_type ? FirstSuccessor() : SecondSuccessor();
1331 return true;
1332 } else if (value()->representation().IsSpecialization()) {
1333 bool number_type =
1334 type_literal_.IsKnownGlobal(isolate()->heap()->number_string());
1335 *block = number_type ? FirstSuccessor() : SecondSuccessor();
machenbach@chromium.orgaf9cfcb2013-11-19 11:05:18 +00001336 return true;
1337 }
1338 *block = NULL;
1339 return false;
1340}
1341
1342
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001343OStream& HCheckMapValue::PrintDataTo(OStream& os) const { // NOLINT
1344 return os << NameOf(value()) << " " << NameOf(map());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001345}
1346
1347
machenbach@chromium.org29699e32014-05-07 00:04:40 +00001348HValue* HCheckMapValue::Canonicalize() {
1349 if (map()->IsConstant()) {
1350 HConstant* c_map = HConstant::cast(map());
1351 return HCheckMaps::CreateAndInsertAfter(
1352 block()->graph()->zone(), value(), c_map->MapValue(),
1353 c_map->HasStableMapValue(), this);
1354 }
1355 return this;
1356}
1357
1358
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001359OStream& HForInPrepareMap::PrintDataTo(OStream& os) const { // NOLINT
1360 return os << NameOf(enumerable());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001361}
1362
1363
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001364OStream& HForInCacheArray::PrintDataTo(OStream& os) const { // NOLINT
1365 return os << NameOf(enumerable()) << " " << NameOf(map()) << "[" << idx_
1366 << "]";
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001367}
1368
1369
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001370OStream& HLoadFieldByIndex::PrintDataTo(OStream& os) const { // NOLINT
1371 return os << NameOf(object()) << " " << NameOf(index());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00001372}
1373
1374
danno@chromium.org59400602013-08-13 17:09:37 +00001375static bool MatchLeftIsOnes(HValue* l, HValue* r, HValue** negated) {
1376 if (!l->EqualsInteger32Constant(~0)) return false;
1377 *negated = r;
1378 return true;
1379}
1380
1381
1382static bool MatchNegationViaXor(HValue* instr, HValue** negated) {
1383 if (!instr->IsBitwise()) return false;
1384 HBitwise* b = HBitwise::cast(instr);
1385 return (b->op() == Token::BIT_XOR) &&
1386 (MatchLeftIsOnes(b->left(), b->right(), negated) ||
1387 MatchLeftIsOnes(b->right(), b->left(), negated));
1388}
1389
1390
1391static bool MatchDoubleNegation(HValue* instr, HValue** arg) {
1392 HValue* negated;
1393 return MatchNegationViaXor(instr, &negated) &&
1394 MatchNegationViaXor(negated, arg);
1395}
1396
1397
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001398HValue* HBitwise::Canonicalize() {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001399 if (!representation().IsSmiOrInteger32()) return this;
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001400 // If x is an int32, then x & -1 == x, x | 0 == x and x ^ 0 == x.
1401 int32_t nop_constant = (op() == Token::BIT_AND) ? -1 : 0;
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001402 if (left()->EqualsInteger32Constant(nop_constant) &&
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001403 !right()->CheckFlag(kUint32)) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001404 return right();
1405 }
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001406 if (right()->EqualsInteger32Constant(nop_constant) &&
yangguo@chromium.org46839fb2012-08-28 09:06:19 +00001407 !left()->CheckFlag(kUint32)) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001408 return left();
1409 }
danno@chromium.org59400602013-08-13 17:09:37 +00001410 // Optimize double negation, a common pattern used for ToInt32(x).
1411 HValue* arg;
1412 if (MatchDoubleNegation(this, &arg) && !arg->CheckFlag(kUint32)) {
1413 return arg;
mstarzinger@chromium.org88d326b2012-04-23 12:57:22 +00001414 }
1415 return this;
1416}
1417
1418
machenbach@chromium.org37be4082013-11-26 13:50:38 +00001419Representation HAdd::RepresentationFromInputs() {
1420 Representation left_rep = left()->representation();
1421 if (left_rep.IsExternal()) {
1422 return Representation::External();
1423 }
1424 return HArithmeticBinaryOperation::RepresentationFromInputs();
1425}
1426
1427
1428Representation HAdd::RequiredInputRepresentation(int index) {
1429 if (index == 2) {
1430 Representation left_rep = left()->representation();
1431 if (left_rep.IsExternal()) {
1432 return Representation::Integer32();
1433 }
1434 }
1435 return HArithmeticBinaryOperation::RequiredInputRepresentation(index);
1436}
1437
1438
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001439static bool IsIdentityOperation(HValue* arg1, HValue* arg2, int32_t identity) {
1440 return arg1->representation().IsSpecialization() &&
1441 arg2->EqualsInteger32Constant(identity);
1442}
1443
1444
1445HValue* HAdd::Canonicalize() {
machenbach@chromium.orged29eb22013-10-31 13:30:00 +00001446 // Adding 0 is an identity operation except in case of -0: -0 + 0 = +0
1447 if (IsIdentityOperation(left(), right(), 0) &&
1448 !left()->representation().IsDouble()) { // Left could be -0.
1449 return left();
1450 }
1451 if (IsIdentityOperation(right(), left(), 0) &&
1452 !left()->representation().IsDouble()) { // Right could be -0.
1453 return right();
1454 }
ulan@chromium.org837a67e2013-06-11 15:39:48 +00001455 return this;
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001456}
1457
1458
ulan@chromium.org812308e2012-02-29 15:58:45 +00001459HValue* HSub::Canonicalize() {
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001460 if (IsIdentityOperation(left(), right(), 0)) return left();
ulan@chromium.org837a67e2013-06-11 15:39:48 +00001461 return this;
mstarzinger@chromium.orgb228be02013-04-18 14:56:59 +00001462}
1463
1464
1465HValue* HMul::Canonicalize() {
1466 if (IsIdentityOperation(left(), right(), 1)) return left();
1467 if (IsIdentityOperation(right(), left(), 1)) return right();
danno@chromium.orgca29dd82013-04-26 11:59:48 +00001468 return this;
ulan@chromium.org812308e2012-02-29 15:58:45 +00001469}
1470
1471
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001472bool HMul::MulMinusOne() {
1473 if (left()->EqualsInteger32Constant(-1) ||
1474 right()->EqualsInteger32Constant(-1)) {
1475 return true;
1476 }
1477
1478 return false;
1479}
1480
1481
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001482HValue* HMod::Canonicalize() {
1483 return this;
1484}
1485
1486
1487HValue* HDiv::Canonicalize() {
verwaest@chromium.org662436e2013-08-28 08:41:27 +00001488 if (IsIdentityOperation(left(), right(), 1)) return left();
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001489 return this;
1490}
1491
1492
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +00001493HValue* HChange::Canonicalize() {
1494 return (from().Equals(to())) ? value() : this;
1495}
1496
1497
yangguo@chromium.org154ff992012-03-13 08:09:54 +00001498HValue* HWrapReceiver::Canonicalize() {
1499 if (HasNoUses()) return NULL;
1500 if (receiver()->type().IsJSObject()) {
1501 return receiver();
1502 }
1503 return this;
1504}
1505
1506
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001507OStream& HTypeof::PrintDataTo(OStream& os) const { // NOLINT
1508 return os << NameOf(value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001509}
1510
1511
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00001512HInstruction* HForceRepresentation::New(Zone* zone, HValue* context,
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001513 HValue* value, Representation representation) {
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00001514 if (FLAG_fold_constants && value->IsConstant()) {
1515 HConstant* c = HConstant::cast(value);
machenbach@chromium.orgfa7f9142014-08-27 00:06:40 +00001516 c = c->CopyToRepresentation(representation, zone);
1517 if (c != NULL) return c;
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00001518 }
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001519 return new(zone) HForceRepresentation(value, representation);
machenbach@chromium.org9af454f2013-11-20 09:25:57 +00001520}
1521
1522
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001523OStream& HForceRepresentation::PrintDataTo(OStream& os) const { // NOLINT
1524 return os << representation().Mnemonic() << " " << NameOf(value());
yangguo@chromium.org4cd70b42013-01-04 08:57:54 +00001525}
1526
1527
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001528OStream& HChange::PrintDataTo(OStream& os) const { // NOLINT
1529 HUnaryOperation::PrintDataTo(os);
1530 os << " " << from().Mnemonic() << " to " << to().Mnemonic();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001531
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001532 if (CanTruncateToSmi()) os << " truncating-smi";
1533 if (CanTruncateToInt32()) os << " truncating-int32";
1534 if (CheckFlag(kBailoutOnMinusZero)) os << " -0?";
1535 if (CheckFlag(kAllowUndefinedAsNaN)) os << " allow-undefined-as-nan";
1536 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001537}
1538
1539
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001540HValue* HUnaryMathOperation::Canonicalize() {
danno@chromium.org59400602013-08-13 17:09:37 +00001541 if (op() == kMathRound || op() == kMathFloor) {
danno@chromium.orgad75d6f2013-08-12 16:57:59 +00001542 HValue* val = value();
1543 if (val->IsChange()) val = HChange::cast(val)->value();
danno@chromium.org387c3b02013-08-12 17:34:10 +00001544 if (val->representation().IsSmiOrInteger32()) {
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00001545 if (val->representation().Equals(representation())) return val;
1546 return Prepend(new(block()->zone()) HChange(
1547 val, representation(), false, false));
danno@chromium.org387c3b02013-08-12 17:34:10 +00001548 }
danno@chromium.orgad75d6f2013-08-12 16:57:59 +00001549 }
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +00001550 if (op() == kMathFloor && value()->IsDiv() && value()->HasOneUse()) {
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00001551 HDiv* hdiv = HDiv::cast(value());
danno@chromium.orgad75d6f2013-08-12 16:57:59 +00001552
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00001553 HValue* left = hdiv->left();
1554 if (left->representation().IsInteger32()) {
1555 // A value with an integer representation does not need to be transformed.
1556 } else if (left->IsChange() && HChange::cast(left)->from().IsInteger32()) {
1557 // A change from an integer32 can be replaced by the integer32 value.
1558 left = HChange::cast(left)->value();
1559 } else if (hdiv->observed_input_representation(1).IsSmiOrInteger32()) {
1560 left = Prepend(new(block()->zone()) HChange(
1561 left, Representation::Integer32(), false, false));
1562 } else {
1563 return this;
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001564 }
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00001565
1566 HValue* right = hdiv->right();
1567 if (right->IsInteger32Constant()) {
1568 right = Prepend(HConstant::cast(right)->CopyToRepresentation(
1569 Representation::Integer32(), right->block()->zone()));
1570 } else if (right->representation().IsInteger32()) {
1571 // A value with an integer representation does not need to be transformed.
1572 } else if (right->IsChange() &&
1573 HChange::cast(right)->from().IsInteger32()) {
1574 // A change from an integer32 can be replaced by the integer32 value.
1575 right = HChange::cast(right)->value();
1576 } else if (hdiv->observed_input_representation(2).IsSmiOrInteger32()) {
1577 right = Prepend(new(block()->zone()) HChange(
1578 right, Representation::Integer32(), false, false));
1579 } else {
1580 return this;
1581 }
1582
1583 return Prepend(HMathFloorOfDiv::New(
1584 block()->zone(), context(), left, right));
yangguo@chromium.orgefdb9d72012-04-26 08:21:05 +00001585 }
1586 return this;
1587}
1588
1589
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001590HValue* HCheckInstanceType::Canonicalize() {
machenbach@chromium.orgeac65cd2014-06-02 00:04:30 +00001591 if ((check_ == IS_SPEC_OBJECT && value()->type().IsJSObject()) ||
1592 (check_ == IS_JS_ARRAY && value()->type().IsJSArray()) ||
1593 (check_ == IS_STRING && value()->type().IsString())) {
danno@chromium.orgd3c42102013-08-01 16:58:23 +00001594 return value();
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001595 }
yangguo@chromium.org003650e2013-01-24 16:31:08 +00001596
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001597 if (check_ == IS_INTERNALIZED_STRING && value()->IsConstant()) {
danno@chromium.orgd3c42102013-08-01 16:58:23 +00001598 if (HConstant::cast(value())->HasInternalizedStringValue()) {
1599 return value();
1600 }
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001601 }
1602 return this;
1603}
1604
1605
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001606void HCheckInstanceType::GetCheckInterval(InstanceType* first,
1607 InstanceType* last) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00001608 DCHECK(is_interval_check());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001609 switch (check_) {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001610 case IS_SPEC_OBJECT:
1611 *first = FIRST_SPEC_OBJECT_TYPE;
1612 *last = LAST_SPEC_OBJECT_TYPE;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001613 return;
1614 case IS_JS_ARRAY:
1615 *first = *last = JS_ARRAY_TYPE;
1616 return;
1617 default:
1618 UNREACHABLE();
1619 }
1620}
1621
1622
1623void HCheckInstanceType::GetCheckMaskAndTag(uint8_t* mask, uint8_t* tag) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00001624 DCHECK(!is_interval_check());
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001625 switch (check_) {
1626 case IS_STRING:
1627 *mask = kIsNotStringMask;
1628 *tag = kStringTag;
1629 return;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001630 case IS_INTERNALIZED_STRING:
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00001631 *mask = kIsNotStringMask | kIsNotInternalizedMask;
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001632 *tag = kInternalizedTag;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001633 return;
1634 default:
1635 UNREACHABLE();
1636 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001637}
1638
1639
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001640OStream& HCheckMaps::PrintDataTo(OStream& os) const { // NOLINT
1641 os << NameOf(value()) << " [" << *maps()->at(0).handle();
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00001642 for (int i = 1; i < maps()->size(); ++i) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001643 os << "," << *maps()->at(i).handle();
yangguo@chromium.org78d1ad42012-02-09 13:53:47 +00001644 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001645 os << "]";
1646 if (IsStabilityCheck()) os << "(stability-check)";
1647 return os;
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00001648}
1649
1650
1651HValue* HCheckMaps::Canonicalize() {
1652 if (!IsStabilityCheck() && maps_are_stable() && value()->IsConstant()) {
1653 HConstant* c_value = HConstant::cast(value());
machenbach@chromium.org29699e32014-05-07 00:04:40 +00001654 if (c_value->HasObjectMap()) {
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00001655 for (int i = 0; i < maps()->size(); ++i) {
1656 if (c_value->ObjectMap() == maps()->at(i)) {
1657 if (maps()->size() > 1) {
1658 set_maps(new(block()->graph()->zone()) UniqueSet<Map>(
1659 maps()->at(i), block()->graph()->zone()));
1660 }
1661 MarkAsStabilityCheck();
1662 break;
1663 }
1664 }
1665 }
1666 }
1667 return this;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001668}
1669
1670
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001671OStream& HCheckValue::PrintDataTo(OStream& os) const { // NOLINT
1672 return os << NameOf(value()) << " " << Brief(*object().handle());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001673}
1674
1675
mstarzinger@chromium.org1f410f92013-08-29 08:13:16 +00001676HValue* HCheckValue::Canonicalize() {
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001677 return (value()->IsConstant() &&
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001678 HConstant::cast(value())->EqualsUnique(object_)) ? NULL : this;
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00001679}
1680
1681
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001682const char* HCheckInstanceType::GetCheckName() const {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001683 switch (check_) {
1684 case IS_SPEC_OBJECT: return "object";
1685 case IS_JS_ARRAY: return "array";
1686 case IS_STRING: return "string";
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +00001687 case IS_INTERNALIZED_STRING: return "internalized_string";
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001688 }
1689 UNREACHABLE();
1690 return "";
1691}
1692
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00001693
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001694OStream& HCheckInstanceType::PrintDataTo(OStream& os) const { // NOLINT
1695 os << GetCheckName() << " ";
1696 return HUnaryOperation::PrintDataTo(os);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001697}
1698
1699
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001700OStream& HCallStub::PrintDataTo(OStream& os) const { // NOLINT
1701 os << CodeStub::MajorName(major_key_, false) << " ";
1702 return HUnaryCall::PrintDataTo(os);
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00001703}
1704
1705
machenbach@chromium.orge20e19e2014-09-09 00:05:04 +00001706OStream& HTailCallThroughMegamorphicCache::PrintDataTo(
1707 OStream& os) const { // NOLINT
1708 for (int i = 0; i < OperandCount(); i++) {
1709 os << NameOf(OperandAt(i)) << " ";
1710 }
1711 return os << "flags: " << flags();
1712}
1713
1714
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001715OStream& HUnknownOSRValue::PrintDataTo(OStream& os) const { // NOLINT
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00001716 const char* type = "expression";
1717 if (environment_->is_local_index(index_)) type = "local";
1718 if (environment_->is_special_index(index_)) type = "special";
1719 if (environment_->is_parameter_index(index_)) type = "parameter";
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001720 return os << type << " @ " << index_;
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +00001721}
1722
1723
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00001724OStream& HInstanceOf::PrintDataTo(OStream& os) const { // NOLINT
1725 return os << NameOf(left()) << " " << NameOf(right()) << " "
1726 << NameOf(context());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001727}
1728
1729
ulan@chromium.org812308e2012-02-29 15:58:45 +00001730Range* HValue::InferRange(Zone* zone) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001731 Range* result;
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001732 if (representation().IsSmi() || type().IsSmi()) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001733 result = new(zone) Range(Smi::kMinValue, Smi::kMaxValue);
1734 result->set_can_be_minus_zero(false);
1735 } else {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001736 result = new(zone) Range();
danno@chromium.orgbee51992013-07-10 14:57:15 +00001737 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32));
1738 // TODO(jkummerow): The range cannot be minus zero when the upper type
1739 // bound is Integer32.
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00001740 }
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00001741 return result;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001742}
1743
1744
ulan@chromium.org812308e2012-02-29 15:58:45 +00001745Range* HChange::InferRange(Zone* zone) {
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001746 Range* input_range = value()->range();
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001747 if (from().IsInteger32() && !value()->CheckFlag(HInstruction::kUint32) &&
1748 (to().IsSmi() ||
1749 (to().IsTagged() &&
1750 input_range != NULL &&
1751 input_range->IsInSmiRange()))) {
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001752 set_type(HType::Smi());
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001753 ClearChangesFlag(kNewSpacePromotion);
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001754 }
machenbach@chromium.org381adef2014-03-14 03:04:56 +00001755 if (to().IsSmiOrTagged() &&
1756 input_range != NULL &&
1757 input_range->IsInSmiRange() &&
1758 (!SmiValuesAre32Bits() ||
1759 !value()->CheckFlag(HValue::kUint32) ||
1760 input_range->upper() != kMaxInt)) {
1761 // The Range class can't express upper bounds in the (kMaxInt, kMaxUint32]
1762 // interval, so we treat kMaxInt as a sentinel for this entire interval.
1763 ClearFlag(kCanOverflow);
1764 }
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001765 Range* result = (input_range != NULL)
ulan@chromium.org812308e2012-02-29 15:58:45 +00001766 ? input_range->Copy(zone)
1767 : HValue::InferRange(zone);
danno@chromium.orgbee51992013-07-10 14:57:15 +00001768 result->set_can_be_minus_zero(!to().IsSmiOrInteger32() ||
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001769 !(CheckFlag(kAllUsesTruncatingToInt32) ||
1770 CheckFlag(kAllUsesTruncatingToSmi)));
1771 if (to().IsSmi()) result->ClampToSmi();
ricow@chromium.orgddd545c2011-08-24 12:02:41 +00001772 return result;
1773}
1774
1775
ulan@chromium.org812308e2012-02-29 15:58:45 +00001776Range* HConstant::InferRange(Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001777 if (has_int32_value_) {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001778 Range* result = new(zone) Range(int32_value_, int32_value_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001779 result->set_can_be_minus_zero(false);
1780 return result;
1781 }
ulan@chromium.org812308e2012-02-29 15:58:45 +00001782 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001783}
1784
1785
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00001786HSourcePosition HPhi::position() const {
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +00001787 return block()->first()->position();
1788}
1789
1790
ulan@chromium.org812308e2012-02-29 15:58:45 +00001791Range* HPhi::InferRange(Zone* zone) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001792 Representation r = representation();
1793 if (r.IsSmiOrInteger32()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001794 if (block()->IsLoopHeader()) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00001795 Range* range = r.IsSmi()
1796 ? new(zone) Range(Smi::kMinValue, Smi::kMaxValue)
1797 : new(zone) Range(kMinInt, kMaxInt);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001798 return range;
1799 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001800 Range* range = OperandAt(0)->range()->Copy(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001801 for (int i = 1; i < OperandCount(); ++i) {
1802 range->Union(OperandAt(i)->range());
1803 }
1804 return range;
1805 }
1806 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001807 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001808 }
1809}
1810
1811
ulan@chromium.org812308e2012-02-29 15:58:45 +00001812Range* HAdd::InferRange(Zone* zone) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001813 Representation r = representation();
1814 if (r.IsSmiOrInteger32()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001815 Range* a = left()->range();
1816 Range* b = right()->range();
ulan@chromium.org812308e2012-02-29 15:58:45 +00001817 Range* res = a->Copy(zone);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001818 if (!res->AddAndCheckOverflow(r, b) ||
1819 (r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1820 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001821 ClearFlag(kCanOverflow);
1822 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001823 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1824 !CheckFlag(kAllUsesTruncatingToInt32) &&
danno@chromium.orgbee51992013-07-10 14:57:15 +00001825 a->CanBeMinusZero() && b->CanBeMinusZero());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001826 return res;
1827 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001828 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001829 }
1830}
1831
1832
ulan@chromium.org812308e2012-02-29 15:58:45 +00001833Range* HSub::InferRange(Zone* zone) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001834 Representation r = representation();
1835 if (r.IsSmiOrInteger32()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001836 Range* a = left()->range();
1837 Range* b = right()->range();
ulan@chromium.org812308e2012-02-29 15:58:45 +00001838 Range* res = a->Copy(zone);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001839 if (!res->SubAndCheckOverflow(r, b) ||
1840 (r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1841 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001842 ClearFlag(kCanOverflow);
1843 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001844 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1845 !CheckFlag(kAllUsesTruncatingToInt32) &&
danno@chromium.orgbee51992013-07-10 14:57:15 +00001846 a->CanBeMinusZero() && b->CanBeZero());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001847 return res;
1848 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001849 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001850 }
1851}
1852
1853
ulan@chromium.org812308e2012-02-29 15:58:45 +00001854Range* HMul::InferRange(Zone* zone) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001855 Representation r = representation();
1856 if (r.IsSmiOrInteger32()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001857 Range* a = left()->range();
1858 Range* b = right()->range();
ulan@chromium.org812308e2012-02-29 15:58:45 +00001859 Range* res = a->Copy(zone);
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00001860 if (!res->MulAndCheckOverflow(r, b) ||
1861 (((r.IsInteger32() && CheckFlag(kAllUsesTruncatingToInt32)) ||
1862 (r.IsSmi() && CheckFlag(kAllUsesTruncatingToSmi))) &&
1863 MulMinusOne())) {
1864 // Truncated int multiplication is too precise and therefore not the
1865 // same as converting to Double and back.
1866 // Handle truncated integer multiplication by -1 special.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001867 ClearFlag(kCanOverflow);
1868 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001869 res->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToSmi) &&
1870 !CheckFlag(kAllUsesTruncatingToInt32) &&
danno@chromium.orgbee51992013-07-10 14:57:15 +00001871 ((a->CanBeZero() && b->CanBeNegative()) ||
1872 (a->CanBeNegative() && b->CanBeZero())));
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001873 return res;
1874 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001875 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001876 }
1877}
1878
1879
machenbach@chromium.org381adef2014-03-14 03:04:56 +00001880Range* HDiv::InferRange(Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001881 if (representation().IsInteger32()) {
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001882 Range* a = left()->range();
1883 Range* b = right()->range();
ulan@chromium.org812308e2012-02-29 15:58:45 +00001884 Range* result = new(zone) Range();
danno@chromium.orgbee51992013-07-10 14:57:15 +00001885 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1886 (a->CanBeMinusZero() ||
1887 (a->CanBeZero() && b->CanBeNegative())));
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00001888 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001889 ClearFlag(kCanOverflow);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001890 }
1891
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001892 if (!b->CanBeZero()) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001893 ClearFlag(kCanBeDivByZero);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001894 }
1895 return result;
1896 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001897 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001898 }
1899}
1900
1901
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001902Range* HMathFloorOfDiv::InferRange(Zone* zone) {
machenbach@chromium.org381adef2014-03-14 03:04:56 +00001903 if (representation().IsInteger32()) {
1904 Range* a = left()->range();
1905 Range* b = right()->range();
1906 Range* result = new(zone) Range();
1907 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1908 (a->CanBeMinusZero() ||
1909 (a->CanBeZero() && b->CanBeNegative())));
1910 if (!a->Includes(kMinInt)) {
1911 ClearFlag(kLeftCanBeMinInt);
1912 }
1913
machenbach@chromium.org7010a2d2014-03-20 15:46:12 +00001914 if (!a->CanBeNegative()) {
1915 ClearFlag(HValue::kLeftCanBeNegative);
1916 }
1917
1918 if (!a->CanBePositive()) {
1919 ClearFlag(HValue::kLeftCanBePositive);
1920 }
1921
machenbach@chromium.org381adef2014-03-14 03:04:56 +00001922 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
1923 ClearFlag(kCanOverflow);
1924 }
1925
1926 if (!b->CanBeZero()) {
1927 ClearFlag(kCanBeDivByZero);
1928 }
1929 return result;
1930 } else {
1931 return HValue::InferRange(zone);
1932 }
dslomov@chromium.org486536d2014-03-12 13:09:18 +00001933}
1934
1935
machenbach@chromium.orgfda8f0c2014-07-14 00:04:59 +00001936// Returns the absolute value of its argument minus one, avoiding undefined
1937// behavior at kMinInt.
1938static int32_t AbsMinus1(int32_t a) { return a < 0 ? -(a + 1) : (a - 1); }
1939
1940
ulan@chromium.org812308e2012-02-29 15:58:45 +00001941Range* HMod::InferRange(Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001942 if (representation().IsInteger32()) {
1943 Range* a = left()->range();
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001944 Range* b = right()->range();
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001945
machenbach@chromium.orgfda8f0c2014-07-14 00:04:59 +00001946 // The magnitude of the modulus is bounded by the right operand.
1947 int32_t positive_bound = Max(AbsMinus1(b->lower()), AbsMinus1(b->upper()));
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00001948
1949 // The result of the modulo operation has the sign of its left operand.
1950 bool left_can_be_negative = a->CanBeMinusZero() || a->CanBeNegative();
1951 Range* result = new(zone) Range(left_can_be_negative ? -positive_bound : 0,
1952 a->CanBePositive() ? positive_bound : 0);
1953
danno@chromium.orgbee51992013-07-10 14:57:15 +00001954 result->set_can_be_minus_zero(!CheckFlag(kAllUsesTruncatingToInt32) &&
1955 left_can_be_negative);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001956
machenbach@chromium.org381adef2014-03-14 03:04:56 +00001957 if (!a->CanBeNegative()) {
1958 ClearFlag(HValue::kLeftCanBeNegative);
1959 }
1960
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001961 if (!a->Includes(kMinInt) || !b->Includes(-1)) {
1962 ClearFlag(HValue::kCanOverflow);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +00001963 }
1964
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00001965 if (!b->CanBeZero()) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001966 ClearFlag(HValue::kCanBeDivByZero);
1967 }
1968 return result;
1969 } else {
ulan@chromium.org812308e2012-02-29 15:58:45 +00001970 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001971 }
1972}
1973
1974
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00001975InductionVariableData* InductionVariableData::ExaminePhi(HPhi* phi) {
1976 if (phi->block()->loop_information() == NULL) return NULL;
1977 if (phi->OperandCount() != 2) return NULL;
1978 int32_t candidate_increment;
1979
1980 candidate_increment = ComputeIncrement(phi, phi->OperandAt(0));
1981 if (candidate_increment != 0) {
1982 return new(phi->block()->graph()->zone())
1983 InductionVariableData(phi, phi->OperandAt(1), candidate_increment);
1984 }
1985
1986 candidate_increment = ComputeIncrement(phi, phi->OperandAt(1));
1987 if (candidate_increment != 0) {
1988 return new(phi->block()->graph()->zone())
1989 InductionVariableData(phi, phi->OperandAt(0), candidate_increment);
1990 }
1991
1992 return NULL;
1993}
1994
1995
1996/*
1997 * This function tries to match the following patterns (and all the relevant
1998 * variants related to |, & and + being commutative):
1999 * base | constant_or_mask
2000 * base & constant_and_mask
2001 * (base + constant_offset) & constant_and_mask
2002 * (base - constant_offset) & constant_and_mask
2003 */
2004void InductionVariableData::DecomposeBitwise(
2005 HValue* value,
2006 BitwiseDecompositionResult* result) {
2007 HValue* base = IgnoreOsrValue(value);
2008 result->base = value;
2009
2010 if (!base->representation().IsInteger32()) return;
2011
2012 if (base->IsBitwise()) {
2013 bool allow_offset = false;
2014 int32_t mask = 0;
2015
2016 HBitwise* bitwise = HBitwise::cast(base);
2017 if (bitwise->right()->IsInteger32Constant()) {
2018 mask = bitwise->right()->GetInteger32Constant();
2019 base = bitwise->left();
2020 } else if (bitwise->left()->IsInteger32Constant()) {
2021 mask = bitwise->left()->GetInteger32Constant();
2022 base = bitwise->right();
2023 } else {
2024 return;
2025 }
2026 if (bitwise->op() == Token::BIT_AND) {
2027 result->and_mask = mask;
2028 allow_offset = true;
2029 } else if (bitwise->op() == Token::BIT_OR) {
2030 result->or_mask = mask;
2031 } else {
2032 return;
2033 }
2034
2035 result->context = bitwise->context();
2036
2037 if (allow_offset) {
2038 if (base->IsAdd()) {
2039 HAdd* add = HAdd::cast(base);
2040 if (add->right()->IsInteger32Constant()) {
2041 base = add->left();
2042 } else if (add->left()->IsInteger32Constant()) {
2043 base = add->right();
2044 }
2045 } else if (base->IsSub()) {
2046 HSub* sub = HSub::cast(base);
2047 if (sub->right()->IsInteger32Constant()) {
2048 base = sub->left();
2049 }
2050 }
2051 }
2052
2053 result->base = base;
2054 }
2055}
2056
2057
2058void InductionVariableData::AddCheck(HBoundsCheck* check,
2059 int32_t upper_limit) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002060 DCHECK(limit_validity() != NULL);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002061 if (limit_validity() != check->block() &&
2062 !limit_validity()->Dominates(check->block())) return;
2063 if (!phi()->block()->current_loop()->IsNestedInThisLoop(
2064 check->block()->current_loop())) return;
2065
2066 ChecksRelatedToLength* length_checks = checks();
2067 while (length_checks != NULL) {
2068 if (length_checks->length() == check->length()) break;
2069 length_checks = length_checks->next();
2070 }
2071 if (length_checks == NULL) {
2072 length_checks = new(check->block()->zone())
2073 ChecksRelatedToLength(check->length(), checks());
2074 checks_ = length_checks;
2075 }
2076
2077 length_checks->AddCheck(check, upper_limit);
2078}
2079
2080
2081void InductionVariableData::ChecksRelatedToLength::CloseCurrentBlock() {
2082 if (checks() != NULL) {
2083 InductionVariableCheck* c = checks();
2084 HBasicBlock* current_block = c->check()->block();
2085 while (c != NULL && c->check()->block() == current_block) {
2086 c->set_upper_limit(current_upper_limit_);
2087 c = c->next();
2088 }
2089 }
2090}
2091
2092
2093void InductionVariableData::ChecksRelatedToLength::UseNewIndexInCurrentBlock(
2094 Token::Value token,
2095 int32_t mask,
2096 HValue* index_base,
2097 HValue* context) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002098 DCHECK(first_check_in_block() != NULL);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002099 HValue* previous_index = first_check_in_block()->index();
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002100 DCHECK(context != NULL);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002101
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002102 Zone* zone = index_base->block()->graph()->zone();
2103 set_added_constant(HConstant::New(zone, context, mask));
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002104 if (added_index() != NULL) {
2105 added_constant()->InsertBefore(added_index());
2106 } else {
2107 added_constant()->InsertBefore(first_check_in_block());
2108 }
2109
2110 if (added_index() == NULL) {
2111 first_check_in_block()->ReplaceAllUsesWith(first_check_in_block()->index());
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002112 HInstruction* new_index = HBitwise::New(zone, context, token, index_base,
2113 added_constant());
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002114 DCHECK(new_index->IsBitwise());
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002115 new_index->ClearAllSideEffects();
2116 new_index->AssumeRepresentation(Representation::Integer32());
2117 set_added_index(HBitwise::cast(new_index));
2118 added_index()->InsertBefore(first_check_in_block());
2119 }
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002120 DCHECK(added_index()->op() == token);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002121
2122 added_index()->SetOperandAt(1, index_base);
2123 added_index()->SetOperandAt(2, added_constant());
2124 first_check_in_block()->SetOperandAt(0, added_index());
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +00002125 if (previous_index->HasNoUses()) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002126 previous_index->DeleteAndReplaceWith(NULL);
2127 }
2128}
2129
2130void InductionVariableData::ChecksRelatedToLength::AddCheck(
2131 HBoundsCheck* check,
2132 int32_t upper_limit) {
2133 BitwiseDecompositionResult decomposition;
2134 InductionVariableData::DecomposeBitwise(check->index(), &decomposition);
2135
2136 if (first_check_in_block() == NULL ||
2137 first_check_in_block()->block() != check->block()) {
2138 CloseCurrentBlock();
2139
2140 first_check_in_block_ = check;
2141 set_added_index(NULL);
2142 set_added_constant(NULL);
2143 current_and_mask_in_block_ = decomposition.and_mask;
2144 current_or_mask_in_block_ = decomposition.or_mask;
2145 current_upper_limit_ = upper_limit;
2146
2147 InductionVariableCheck* new_check = new(check->block()->graph()->zone())
2148 InductionVariableCheck(check, checks_, upper_limit);
2149 checks_ = new_check;
2150 return;
2151 }
2152
2153 if (upper_limit > current_upper_limit()) {
2154 current_upper_limit_ = upper_limit;
2155 }
2156
2157 if (decomposition.and_mask != 0 &&
2158 current_or_mask_in_block() == 0) {
2159 if (current_and_mask_in_block() == 0 ||
2160 decomposition.and_mask > current_and_mask_in_block()) {
2161 UseNewIndexInCurrentBlock(Token::BIT_AND,
2162 decomposition.and_mask,
2163 decomposition.base,
2164 decomposition.context);
2165 current_and_mask_in_block_ = decomposition.and_mask;
2166 }
2167 check->set_skip_check();
2168 }
2169 if (current_and_mask_in_block() == 0) {
2170 if (decomposition.or_mask > current_or_mask_in_block()) {
2171 UseNewIndexInCurrentBlock(Token::BIT_OR,
2172 decomposition.or_mask,
2173 decomposition.base,
2174 decomposition.context);
2175 current_or_mask_in_block_ = decomposition.or_mask;
2176 }
2177 check->set_skip_check();
2178 }
2179
2180 if (!check->skip_check()) {
2181 InductionVariableCheck* new_check = new(check->block()->graph()->zone())
2182 InductionVariableCheck(check, checks_, upper_limit);
2183 checks_ = new_check;
2184 }
2185}
2186
2187
2188/*
2189 * This method detects if phi is an induction variable, with phi_operand as
2190 * its "incremented" value (the other operand would be the "base" value).
2191 *
2192 * It cheks is phi_operand has the form "phi + constant".
2193 * If yes, the constant is the increment that the induction variable gets at
2194 * every loop iteration.
2195 * Otherwise it returns 0.
2196 */
2197int32_t InductionVariableData::ComputeIncrement(HPhi* phi,
2198 HValue* phi_operand) {
2199 if (!phi_operand->representation().IsInteger32()) return 0;
2200
2201 if (phi_operand->IsAdd()) {
2202 HAdd* operation = HAdd::cast(phi_operand);
2203 if (operation->left() == phi &&
2204 operation->right()->IsInteger32Constant()) {
2205 return operation->right()->GetInteger32Constant();
2206 } else if (operation->right() == phi &&
2207 operation->left()->IsInteger32Constant()) {
2208 return operation->left()->GetInteger32Constant();
2209 }
2210 } else if (phi_operand->IsSub()) {
2211 HSub* operation = HSub::cast(phi_operand);
2212 if (operation->left() == phi &&
2213 operation->right()->IsInteger32Constant()) {
2214 return -operation->right()->GetInteger32Constant();
2215 }
2216 }
2217
2218 return 0;
2219}
2220
2221
2222/*
2223 * Swaps the information in "update" with the one contained in "this".
2224 * The swapping is important because this method is used while doing a
2225 * dominator tree traversal, and "update" will retain the old data that
2226 * will be restored while backtracking.
2227 */
2228void InductionVariableData::UpdateAdditionalLimit(
2229 InductionVariableLimitUpdate* update) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002230 DCHECK(update->updated_variable == this);
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002231 if (update->limit_is_upper) {
2232 swap(&additional_upper_limit_, &update->limit);
2233 swap(&additional_upper_limit_is_included_, &update->limit_is_included);
2234 } else {
2235 swap(&additional_lower_limit_, &update->limit);
2236 swap(&additional_lower_limit_is_included_, &update->limit_is_included);
2237 }
2238}
2239
2240
2241int32_t InductionVariableData::ComputeUpperLimit(int32_t and_mask,
2242 int32_t or_mask) {
2243 // Should be Smi::kMaxValue but it must fit 32 bits; lower is safe anyway.
2244 const int32_t MAX_LIMIT = 1 << 30;
2245
2246 int32_t result = MAX_LIMIT;
2247
2248 if (limit() != NULL &&
2249 limit()->IsInteger32Constant()) {
2250 int32_t limit_value = limit()->GetInteger32Constant();
2251 if (!limit_included()) {
2252 limit_value--;
2253 }
2254 if (limit_value < result) result = limit_value;
2255 }
2256
2257 if (additional_upper_limit() != NULL &&
2258 additional_upper_limit()->IsInteger32Constant()) {
2259 int32_t limit_value = additional_upper_limit()->GetInteger32Constant();
2260 if (!additional_upper_limit_is_included()) {
2261 limit_value--;
2262 }
2263 if (limit_value < result) result = limit_value;
2264 }
2265
2266 if (and_mask > 0 && and_mask < MAX_LIMIT) {
2267 if (and_mask < result) result = and_mask;
2268 return result;
2269 }
2270
2271 // Add the effect of the or_mask.
2272 result |= or_mask;
2273
2274 return result >= MAX_LIMIT ? kNoLimit : result;
2275}
2276
2277
2278HValue* InductionVariableData::IgnoreOsrValue(HValue* v) {
2279 if (!v->IsPhi()) return v;
2280 HPhi* phi = HPhi::cast(v);
2281 if (phi->OperandCount() != 2) return v;
2282 if (phi->OperandAt(0)->block()->is_osr_entry()) {
2283 return phi->OperandAt(1);
2284 } else if (phi->OperandAt(1)->block()->is_osr_entry()) {
2285 return phi->OperandAt(0);
2286 } else {
2287 return v;
2288 }
2289}
2290
2291
2292InductionVariableData* InductionVariableData::GetInductionVariableData(
2293 HValue* v) {
2294 v = IgnoreOsrValue(v);
2295 if (v->IsPhi()) {
2296 return HPhi::cast(v)->induction_variable_data();
2297 }
2298 return NULL;
2299}
2300
2301
2302/*
2303 * Check if a conditional branch to "current_branch" with token "token" is
2304 * the branch that keeps the induction loop running (and, conversely, will
2305 * terminate it if the "other_branch" is taken).
2306 *
2307 * Three conditions must be met:
2308 * - "current_branch" must be in the induction loop.
2309 * - "other_branch" must be out of the induction loop.
2310 * - "token" and the induction increment must be "compatible": the token should
2311 * be a condition that keeps the execution inside the loop until the limit is
2312 * reached.
2313 */
2314bool InductionVariableData::CheckIfBranchIsLoopGuard(
2315 Token::Value token,
2316 HBasicBlock* current_branch,
2317 HBasicBlock* other_branch) {
2318 if (!phi()->block()->current_loop()->IsNestedInThisLoop(
2319 current_branch->current_loop())) {
2320 return false;
2321 }
2322
2323 if (phi()->block()->current_loop()->IsNestedInThisLoop(
2324 other_branch->current_loop())) {
2325 return false;
2326 }
2327
2328 if (increment() > 0 && (token == Token::LT || token == Token::LTE)) {
2329 return true;
2330 }
2331 if (increment() < 0 && (token == Token::GT || token == Token::GTE)) {
2332 return true;
2333 }
2334 if (Token::IsInequalityOp(token) && (increment() == 1 || increment() == -1)) {
2335 return true;
2336 }
2337
2338 return false;
2339}
2340
2341
2342void InductionVariableData::ComputeLimitFromPredecessorBlock(
2343 HBasicBlock* block,
2344 LimitFromPredecessorBlock* result) {
2345 if (block->predecessors()->length() != 1) return;
2346 HBasicBlock* predecessor = block->predecessors()->at(0);
2347 HInstruction* end = predecessor->last();
2348
2349 if (!end->IsCompareNumericAndBranch()) return;
2350 HCompareNumericAndBranch* branch = HCompareNumericAndBranch::cast(end);
2351
2352 Token::Value token = branch->token();
2353 if (!Token::IsArithmeticCompareOp(token)) return;
2354
2355 HBasicBlock* other_target;
2356 if (block == branch->SuccessorAt(0)) {
2357 other_target = branch->SuccessorAt(1);
2358 } else {
2359 other_target = branch->SuccessorAt(0);
2360 token = Token::NegateCompareOp(token);
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002361 DCHECK(block == branch->SuccessorAt(1));
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002362 }
2363
2364 InductionVariableData* data;
2365
2366 data = GetInductionVariableData(branch->left());
2367 HValue* limit = branch->right();
2368 if (data == NULL) {
2369 data = GetInductionVariableData(branch->right());
2370 token = Token::ReverseCompareOp(token);
2371 limit = branch->left();
2372 }
2373
2374 if (data != NULL) {
2375 result->variable = data;
2376 result->token = token;
2377 result->limit = limit;
2378 result->other_target = other_target;
2379 }
2380}
2381
2382
2383/*
2384 * Compute the limit that is imposed on an induction variable when entering
2385 * "block" (if any).
2386 * If the limit is the "proper" induction limit (the one that makes the loop
2387 * terminate when the induction variable reaches it) it is stored directly in
2388 * the induction variable data.
2389 * Otherwise the limit is written in "additional_limit" and the method
2390 * returns true.
2391 */
2392bool InductionVariableData::ComputeInductionVariableLimit(
2393 HBasicBlock* block,
2394 InductionVariableLimitUpdate* additional_limit) {
2395 LimitFromPredecessorBlock limit;
2396 ComputeLimitFromPredecessorBlock(block, &limit);
2397 if (!limit.LimitIsValid()) return false;
2398
2399 if (limit.variable->CheckIfBranchIsLoopGuard(limit.token,
2400 block,
2401 limit.other_target)) {
2402 limit.variable->limit_ = limit.limit;
2403 limit.variable->limit_included_ = limit.LimitIsIncluded();
2404 limit.variable->limit_validity_ = block;
2405 limit.variable->induction_exit_block_ = block->predecessors()->at(0);
2406 limit.variable->induction_exit_target_ = limit.other_target;
2407 return false;
2408 } else {
2409 additional_limit->updated_variable = limit.variable;
2410 additional_limit->limit = limit.limit;
2411 additional_limit->limit_is_upper = limit.LimitIsUpper();
2412 additional_limit->limit_is_included = limit.LimitIsIncluded();
2413 return true;
2414 }
2415}
2416
2417
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002418Range* HMathMinMax::InferRange(Zone* zone) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002419 if (representation().IsSmiOrInteger32()) {
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002420 Range* a = left()->range();
2421 Range* b = right()->range();
2422 Range* res = a->Copy(zone);
2423 if (operation_ == kMathMax) {
2424 res->CombinedMax(b);
2425 } else {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002426 DCHECK(operation_ == kMathMin);
mstarzinger@chromium.org471f2f12012-08-10 14:46:33 +00002427 res->CombinedMin(b);
2428 }
2429 return res;
2430 } else {
2431 return HValue::InferRange(zone);
2432 }
2433}
2434
2435
machenbach@chromium.org8d8413c2014-06-03 00:04:55 +00002436void HPushArguments::AddInput(HValue* value) {
2437 inputs_.Add(NULL, value->block()->zone());
2438 SetOperandAt(OperandCount() - 1, value);
2439}
2440
2441
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002442OStream& HPhi::PrintTo(OStream& os) const { // NOLINT
2443 os << "[";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002444 for (int i = 0; i < OperandCount(); ++i) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002445 os << " " << NameOf(OperandAt(i)) << " ";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002446 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002447 return os << " uses:" << UseCount() << "_"
2448 << smi_non_phi_uses() + smi_indirect_uses() << "s_"
2449 << int32_non_phi_uses() + int32_indirect_uses() << "i_"
2450 << double_non_phi_uses() + double_indirect_uses() << "d_"
2451 << tagged_non_phi_uses() + tagged_indirect_uses() << "t"
2452 << TypeOf(this) << "]";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002453}
2454
2455
2456void HPhi::AddInput(HValue* value) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002457 inputs_.Add(NULL, value->block()->zone());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002458 SetOperandAt(OperandCount() - 1, value);
2459 // Mark phis that may have 'arguments' directly or indirectly as an operand.
2460 if (!CheckFlag(kIsArguments) && value->CheckFlag(kIsArguments)) {
2461 SetFlag(kIsArguments);
2462 }
2463}
2464
2465
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00002466bool HPhi::HasRealUses() {
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00002467 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
2468 if (!it.value()->IsPhi()) return true;
danno@chromium.org4d3fe4e2011-03-10 10:14:28 +00002469 }
2470 return false;
2471}
2472
2473
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00002474HValue* HPhi::GetRedundantReplacement() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002475 HValue* candidate = NULL;
2476 int count = OperandCount();
2477 int position = 0;
2478 while (position < count && candidate == NULL) {
2479 HValue* current = OperandAt(position++);
2480 if (current != this) candidate = current;
2481 }
2482 while (position < count) {
2483 HValue* current = OperandAt(position++);
2484 if (current != this && current != candidate) return NULL;
2485 }
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002486 DCHECK(candidate != this);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002487 return candidate;
2488}
2489
2490
2491void HPhi::DeleteFromGraph() {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002492 DCHECK(block() != NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002493 block()->RemovePhi(this);
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002494 DCHECK(block() == NULL);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002495}
2496
2497
2498void HPhi::InitRealUses(int phi_id) {
2499 // Initialize real uses.
2500 phi_id_ = phi_id;
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002501 // Compute a conservative approximation of truncating uses before inferring
2502 // representations. The proper, exact computation will be done later, when
2503 // inserting representation changes.
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002504 SetFlag(kTruncatingToSmi);
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002505 SetFlag(kTruncatingToInt32);
erik.corry@gmail.com3847bd52011-04-27 10:38:56 +00002506 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
2507 HValue* value = it.value();
2508 if (!value->IsPhi()) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002509 Representation rep = value->observed_input_representation(it.index());
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00002510 non_phi_uses_[rep.kind()] += 1;
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002511 if (FLAG_trace_representation) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002512 PrintF("#%d Phi is used by real #%d %s as %s\n",
2513 id(), value->id(), value->Mnemonic(), rep.Mnemonic());
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002514 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002515 if (!value->IsSimulate()) {
2516 if (!value->CheckFlag(kTruncatingToSmi)) {
2517 ClearFlag(kTruncatingToSmi);
2518 }
2519 if (!value->CheckFlag(kTruncatingToInt32)) {
2520 ClearFlag(kTruncatingToInt32);
2521 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +00002522 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002523 }
2524 }
2525}
2526
2527
2528void HPhi::AddNonPhiUsesFrom(HPhi* other) {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002529 if (FLAG_trace_representation) {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002530 PrintF("adding to #%d Phi uses of #%d Phi: s%d i%d d%d t%d\n",
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002531 id(), other->id(),
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002532 other->non_phi_uses_[Representation::kSmi],
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002533 other->non_phi_uses_[Representation::kInteger32],
2534 other->non_phi_uses_[Representation::kDouble],
2535 other->non_phi_uses_[Representation::kTagged]);
2536 }
2537
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002538 for (int i = 0; i < Representation::kNumRepresentations; i++) {
2539 indirect_uses_[i] += other->non_phi_uses_[i];
2540 }
2541}
2542
2543
2544void HPhi::AddIndirectUsesTo(int* dest) {
2545 for (int i = 0; i < Representation::kNumRepresentations; i++) {
2546 dest[i] += indirect_uses_[i];
2547 }
2548}
2549
2550
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002551void HSimulate::MergeWith(ZoneList<HSimulate*>* list) {
2552 while (!list->is_empty()) {
2553 HSimulate* from = list->RemoveLast();
2554 ZoneList<HValue*>* from_values = &from->values_;
2555 for (int i = 0; i < from_values->length(); ++i) {
2556 if (from->HasAssignedIndexAt(i)) {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00002557 int index = from->GetAssignedIndexAt(i);
2558 if (HasValueForIndex(index)) continue;
2559 AddAssignedValue(index, from_values->at(i));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002560 } else {
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002561 if (pop_count_ > 0) {
2562 pop_count_--;
2563 } else {
2564 AddPushedValue(from_values->at(i));
2565 }
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002566 }
2567 }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002568 pop_count_ += from->pop_count_;
2569 from->DeleteAndReplaceWith(NULL);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002570 }
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002571}
2572
2573
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002574OStream& HSimulate::PrintDataTo(OStream& os) const { // NOLINT
2575 os << "id=" << ast_id().ToInt();
2576 if (pop_count_ > 0) os << " pop " << pop_count_;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002577 if (values_.length() > 0) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002578 if (pop_count_ > 0) os << " /";
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002579 for (int i = values_.length() - 1; i >= 0; --i) {
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002580 if (HasAssignedIndexAt(i)) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002581 os << " var[" << GetAssignedIndexAt(i) << "] = ";
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00002582 } else {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002583 os << " push ";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002584 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002585 os << NameOf(values_[i]);
2586 if (i > 0) os << ",";
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002587 }
2588 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002589 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002590}
2591
2592
verwaest@chromium.org662436e2013-08-28 08:41:27 +00002593void HSimulate::ReplayEnvironment(HEnvironment* env) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002594 if (done_with_replay_) return;
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002595 DCHECK(env != NULL);
verwaest@chromium.org662436e2013-08-28 08:41:27 +00002596 env->set_ast_id(ast_id());
2597 env->Drop(pop_count());
2598 for (int i = values()->length() - 1; i >= 0; --i) {
2599 HValue* value = values()->at(i);
2600 if (HasAssignedIndexAt(i)) {
2601 env->Bind(GetAssignedIndexAt(i), value);
2602 } else {
2603 env->Push(value);
2604 }
2605 }
dslomov@chromium.org486536d2014-03-12 13:09:18 +00002606 done_with_replay_ = true;
verwaest@chromium.org662436e2013-08-28 08:41:27 +00002607}
2608
2609
machenbach@chromium.orgcfdf67d2013-09-27 07:27:26 +00002610static void ReplayEnvironmentNested(const ZoneList<HValue*>* values,
2611 HCapturedObject* other) {
2612 for (int i = 0; i < values->length(); ++i) {
2613 HValue* value = values->at(i);
2614 if (value->IsCapturedObject()) {
2615 if (HCapturedObject::cast(value)->capture_id() == other->capture_id()) {
2616 values->at(i) = other;
2617 } else {
2618 ReplayEnvironmentNested(HCapturedObject::cast(value)->values(), other);
2619 }
2620 }
2621 }
2622}
2623
2624
jkummerow@chromium.org1e8da742013-08-26 17:13:35 +00002625// Replay captured objects by replacing all captured objects with the
2626// same capture id in the current and all outer environments.
2627void HCapturedObject::ReplayEnvironment(HEnvironment* env) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002628 DCHECK(env != NULL);
jkummerow@chromium.org1e8da742013-08-26 17:13:35 +00002629 while (env != NULL) {
machenbach@chromium.orgcfdf67d2013-09-27 07:27:26 +00002630 ReplayEnvironmentNested(env->values(), this);
jkummerow@chromium.org1e8da742013-08-26 17:13:35 +00002631 env = env->outer();
2632 }
2633}
2634
2635
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002636OStream& HCapturedObject::PrintDataTo(OStream& os) const { // NOLINT
2637 os << "#" << capture_id() << " ";
2638 return HDematerializedObject::PrintDataTo(os);
mstarzinger@chromium.org2ed0d022013-10-17 08:06:21 +00002639}
2640
2641
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002642void HEnterInlined::RegisterReturnTarget(HBasicBlock* return_target,
2643 Zone* zone) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002644 DCHECK(return_target->IsInlineReturnTarget());
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00002645 return_targets_.Add(return_target, zone);
2646}
2647
2648
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002649OStream& HEnterInlined::PrintDataTo(OStream& os) const { // NOLINT
2650 return os << function()->debug_name()->ToCString().get()
2651 << ", id=" << function()->id().ToInt();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002652}
2653
2654
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002655static bool IsInteger32(double value) {
2656 double roundtrip_value = static_cast<double>(static_cast<int32_t>(value));
machenbach@chromium.orge20e19e2014-09-09 00:05:04 +00002657 return bit_cast<int64_t>(roundtrip_value) == bit_cast<int64_t>(value);
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002658}
2659
2660
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002661HConstant::HConstant(Handle<Object> object, Representation r)
machenbach@chromium.orgeac65cd2014-06-02 00:04:30 +00002662 : HTemplateInstruction<0>(HType::FromValue(object)),
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002663 object_(Unique<Object>::CreateUninitialized(object)),
2664 object_map_(Handle<Map>::null()),
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002665 has_stable_map_value_(false),
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002666 has_smi_value_(false),
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002667 has_int32_value_(false),
2668 has_double_value_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002669 has_external_reference_value_(false),
danno@chromium.orgf005df62013-04-30 16:36:45 +00002670 is_not_in_new_space_(true),
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002671 boolean_value_(object->BooleanValue()),
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002672 is_undetectable_(false),
2673 instance_type_(kUnknownInstanceType) {
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002674 if (object->IsHeapObject()) {
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002675 Handle<HeapObject> heap_object = Handle<HeapObject>::cast(object);
2676 Isolate* isolate = heap_object->GetIsolate();
2677 Handle<Map> map(heap_object->map(), isolate);
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002678 is_not_in_new_space_ = !isolate->heap()->InNewSpace(*object);
2679 instance_type_ = map->instance_type();
2680 is_undetectable_ = map->is_undetectable();
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002681 if (map->is_stable()) object_map_ = Unique<Map>::CreateImmovable(map);
2682 has_stable_map_value_ = (instance_type_ == MAP_TYPE &&
2683 Handle<Map>::cast(heap_object)->is_stable());
danno@chromium.orgf005df62013-04-30 16:36:45 +00002684 }
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002685 if (object->IsNumber()) {
2686 double n = object->Number();
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002687 has_int32_value_ = IsInteger32(n);
2688 int32_value_ = DoubleToInt32(n);
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002689 has_smi_value_ = has_int32_value_ && Smi::IsValid(int32_value_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002690 double_value_ = n;
2691 has_double_value_ = true;
machenbach@chromium.orgc8cbc432014-01-21 09:01:57 +00002692 // TODO(titzer): if this heap number is new space, tenure a new one.
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002693 }
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002694
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002695 Initialize(r);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002696}
2697
2698
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002699HConstant::HConstant(Unique<Object> object,
2700 Unique<Map> object_map,
2701 bool has_stable_map_value,
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002702 Representation r,
2703 HType type,
danno@chromium.orgf005df62013-04-30 16:36:45 +00002704 bool is_not_in_new_space,
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002705 bool boolean_value,
2706 bool is_undetectable,
2707 InstanceType instance_type)
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002708 : HTemplateInstruction<0>(type),
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002709 object_(object),
2710 object_map_(object_map),
2711 has_stable_map_value_(has_stable_map_value),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002712 has_smi_value_(false),
2713 has_int32_value_(false),
2714 has_double_value_(false),
2715 has_external_reference_value_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002716 is_not_in_new_space_(is_not_in_new_space),
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002717 boolean_value_(boolean_value),
2718 is_undetectable_(is_undetectable),
2719 instance_type_(instance_type) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002720 DCHECK(!object.handle().is_null());
2721 DCHECK(!type.IsTaggedNumber() || type.IsNone());
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002722 Initialize(r);
2723}
2724
2725
2726HConstant::HConstant(int32_t integer_value,
2727 Representation r,
danno@chromium.orgf005df62013-04-30 16:36:45 +00002728 bool is_not_in_new_space,
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002729 Unique<Object> object)
2730 : object_(object),
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002731 object_map_(Handle<Map>::null()),
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002732 has_stable_map_value_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002733 has_smi_value_(Smi::IsValid(integer_value)),
2734 has_int32_value_(true),
2735 has_double_value_(true),
2736 has_external_reference_value_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002737 is_not_in_new_space_(is_not_in_new_space),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002738 boolean_value_(integer_value != 0),
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002739 is_undetectable_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002740 int32_value_(integer_value),
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002741 double_value_(FastI2D(integer_value)),
2742 instance_type_(kUnknownInstanceType) {
jkummerow@chromium.org50bb8682014-03-06 17:59:13 +00002743 // It's possible to create a constant with a value in Smi-range but stored
2744 // in a (pre-existing) HeapNumber. See crbug.com/349878.
2745 bool could_be_heapobject = r.IsTagged() && !object.handle().is_null();
2746 bool is_smi = has_smi_value_ && !could_be_heapobject;
2747 set_type(is_smi ? HType::Smi() : HType::TaggedNumber());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002748 Initialize(r);
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002749}
2750
2751
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002752HConstant::HConstant(double double_value,
2753 Representation r,
danno@chromium.orgf005df62013-04-30 16:36:45 +00002754 bool is_not_in_new_space,
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002755 Unique<Object> object)
2756 : object_(object),
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002757 object_map_(Handle<Map>::null()),
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002758 has_stable_map_value_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002759 has_int32_value_(IsInteger32(double_value)),
2760 has_double_value_(true),
2761 has_external_reference_value_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002762 is_not_in_new_space_(is_not_in_new_space),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002763 boolean_value_(double_value != 0 && !std::isnan(double_value)),
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002764 is_undetectable_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002765 int32_value_(DoubleToInt32(double_value)),
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002766 double_value_(double_value),
2767 instance_type_(kUnknownInstanceType) {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002768 has_smi_value_ = has_int32_value_ && Smi::IsValid(int32_value_);
jkummerow@chromium.org50bb8682014-03-06 17:59:13 +00002769 // It's possible to create a constant with a value in Smi-range but stored
2770 // in a (pre-existing) HeapNumber. See crbug.com/349878.
2771 bool could_be_heapobject = r.IsTagged() && !object.handle().is_null();
2772 bool is_smi = has_smi_value_ && !could_be_heapobject;
2773 set_type(is_smi ? HType::Smi() : HType::TaggedNumber());
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002774 Initialize(r);
2775}
2776
2777
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002778HConstant::HConstant(ExternalReference reference)
machenbach@chromium.orgeac65cd2014-06-02 00:04:30 +00002779 : HTemplateInstruction<0>(HType::Any()),
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002780 object_(Unique<Object>(Handle<Object>::null())),
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00002781 object_map_(Handle<Map>::null()),
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002782 has_stable_map_value_(false),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002783 has_smi_value_(false),
2784 has_int32_value_(false),
2785 has_double_value_(false),
2786 has_external_reference_value_(true),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002787 is_not_in_new_space_(true),
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002788 boolean_value_(true),
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002789 is_undetectable_(false),
2790 external_reference_value_(reference),
2791 instance_type_(kUnknownInstanceType) {
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002792 Initialize(Representation::External());
2793}
2794
2795
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002796void HConstant::Initialize(Representation r) {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002797 if (r.IsNone()) {
machenbach@chromium.orgca2f2042014-03-10 10:03:12 +00002798 if (has_smi_value_ && SmiValuesAre31Bits()) {
2799 r = Representation::Smi();
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00002800 } else if (has_int32_value_) {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002801 r = Representation::Integer32();
2802 } else if (has_double_value_) {
2803 r = Representation::Double();
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002804 } else if (has_external_reference_value_) {
2805 r = Representation::External();
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002806 } else {
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002807 Handle<Object> object = object_.handle();
2808 if (object->IsJSObject()) {
2809 // Try to eagerly migrate JSObjects that have deprecated maps.
2810 Handle<JSObject> js_object = Handle<JSObject>::cast(object);
2811 if (js_object->map()->is_deprecated()) {
2812 JSObject::TryMigrateInstance(js_object);
2813 }
2814 }
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002815 r = Representation::Tagged();
2816 }
2817 }
machenbach@chromium.orgb376fed2014-09-15 00:05:18 +00002818 if (r.IsSmi()) {
2819 // If we have an existing handle, zap it, because it might be a heap
2820 // number which we must not re-use when copying this HConstant to
2821 // Tagged representation later, because having Smi representation now
2822 // could cause heap object checks not to get emitted.
2823 object_ = Unique<Object>(Handle<Object>::null());
2824 }
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002825 set_representation(r);
2826 SetFlag(kUseGVN);
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002827}
2828
2829
machenbach@chromium.org57a54ac2014-01-31 14:01:53 +00002830bool HConstant::ImmortalImmovable() const {
2831 if (has_int32_value_) {
2832 return false;
2833 }
2834 if (has_double_value_) {
2835 if (IsSpecialDouble()) {
2836 return true;
2837 }
2838 return false;
2839 }
2840 if (has_external_reference_value_) {
2841 return false;
2842 }
2843
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002844 DCHECK(!object_.handle().is_null());
machenbach@chromium.org57a54ac2014-01-31 14:01:53 +00002845 Heap* heap = isolate()->heap();
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002846 DCHECK(!object_.IsKnownGlobal(heap->minus_zero_value()));
2847 DCHECK(!object_.IsKnownGlobal(heap->nan_value()));
machenbach@chromium.org57a54ac2014-01-31 14:01:53 +00002848 return
2849#define IMMORTAL_IMMOVABLE_ROOT(name) \
2850 object_.IsKnownGlobal(heap->name()) ||
2851 IMMORTAL_IMMOVABLE_ROOT_LIST(IMMORTAL_IMMOVABLE_ROOT)
2852#undef IMMORTAL_IMMOVABLE_ROOT
2853#define INTERNALIZED_STRING(name, value) \
2854 object_.IsKnownGlobal(heap->name()) ||
2855 INTERNALIZED_STRING_LIST(INTERNALIZED_STRING)
2856#undef INTERNALIZED_STRING
2857#define STRING_TYPE(NAME, size, name, Name) \
2858 object_.IsKnownGlobal(heap->name##_map()) ||
2859 STRING_TYPE_LIST(STRING_TYPE)
2860#undef STRING_TYPE
2861 false;
2862}
2863
2864
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002865bool HConstant::EmitAtUses() {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002866 DCHECK(IsLinked());
machenbach@chromium.org528ce022013-09-23 14:09:36 +00002867 if (block()->graph()->has_osr() &&
2868 block()->graph()->IsStandardConstant(this)) {
bmeurer@chromium.org71f9fca2013-10-22 08:00:09 +00002869 // TODO(titzer): this seems like a hack that should be fixed by custom OSR.
machenbach@chromium.org528ce022013-09-23 14:09:36 +00002870 return true;
hpayer@chromium.org8432c912013-02-28 15:55:26 +00002871 }
machenbach@chromium.org4b0feee2014-06-23 08:21:41 +00002872 if (HasNoUses()) return true;
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002873 if (IsCell()) return false;
2874 if (representation().IsDouble()) return false;
machenbach@chromium.orgd06b9262014-05-28 06:48:05 +00002875 if (representation().IsExternal()) return false;
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00002876 return true;
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002877}
2878
2879
mmassi@chromium.org7028c052012-06-13 11:51:58 +00002880HConstant* HConstant::CopyToRepresentation(Representation r, Zone* zone) const {
svenpanne@chromium.org53ad1752013-05-27 12:20:38 +00002881 if (r.IsSmi() && !has_smi_value_) return NULL;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002882 if (r.IsInteger32() && !has_int32_value_) return NULL;
2883 if (r.IsDouble() && !has_double_value_) return NULL;
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002884 if (r.IsExternal() && !has_external_reference_value_) return NULL;
danno@chromium.orgf005df62013-04-30 16:36:45 +00002885 if (has_int32_value_) {
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002886 return new(zone) HConstant(int32_value_, r, is_not_in_new_space_, object_);
danno@chromium.orgf005df62013-04-30 16:36:45 +00002887 }
2888 if (has_double_value_) {
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002889 return new(zone) HConstant(double_value_, r, is_not_in_new_space_, object_);
danno@chromium.orgf005df62013-04-30 16:36:45 +00002890 }
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002891 if (has_external_reference_value_) {
2892 return new(zone) HConstant(external_reference_value_);
2893 }
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002894 DCHECK(!object_.handle().is_null());
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002895 return new(zone) HConstant(object_,
machenbach@chromium.org29699e32014-05-07 00:04:40 +00002896 object_map_,
2897 has_stable_map_value_,
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +00002898 r,
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002899 type_,
danno@chromium.orgf005df62013-04-30 16:36:45 +00002900 is_not_in_new_space_,
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00002901 boolean_value_,
2902 is_undetectable_,
2903 instance_type_);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002904}
2905
2906
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00002907Maybe<HConstant*> HConstant::CopyToTruncatedInt32(Zone* zone) {
2908 HConstant* res = NULL;
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002909 if (has_int32_value_) {
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00002910 res = new(zone) HConstant(int32_value_,
2911 Representation::Integer32(),
2912 is_not_in_new_space_,
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002913 object_);
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00002914 } else if (has_double_value_) {
2915 res = new(zone) HConstant(DoubleToInt32(double_value_),
2916 Representation::Integer32(),
2917 is_not_in_new_space_,
machenbach@chromium.org3d079fe2013-09-25 08:19:55 +00002918 object_);
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002919 }
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00002920 return Maybe<HConstant*>(res != NULL, res);
2921}
2922
2923
2924Maybe<HConstant*> HConstant::CopyToTruncatedNumber(Zone* zone) {
2925 HConstant* res = NULL;
dslomov@chromium.org639bac02013-09-09 11:58:54 +00002926 Handle<Object> handle = this->handle(zone->isolate());
2927 if (handle->IsBoolean()) {
2928 res = handle->BooleanValue() ?
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00002929 new(zone) HConstant(1) : new(zone) HConstant(0);
dslomov@chromium.org639bac02013-09-09 11:58:54 +00002930 } else if (handle->IsUndefined()) {
yangguo@chromium.org5de00742014-07-01 11:58:10 +00002931 res = new(zone) HConstant(base::OS::nan_value());
dslomov@chromium.org639bac02013-09-09 11:58:54 +00002932 } else if (handle->IsNull()) {
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00002933 res = new(zone) HConstant(0);
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00002934 }
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00002935 return Maybe<HConstant*>(res != NULL, res);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002936}
2937
2938
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002939OStream& HConstant::PrintDataTo(OStream& os) const { // NOLINT
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002940 if (has_int32_value_) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002941 os << int32_value_ << " ";
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002942 } else if (has_double_value_) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002943 os << double_value_ << " ";
danno@chromium.orgd3c42102013-08-01 16:58:23 +00002944 } else if (has_external_reference_value_) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002945 os << reinterpret_cast<void*>(external_reference_value_.address()) << " ";
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002946 } else {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002947 // The handle() method is silently and lazily mutating the object.
2948 Handle<Object> h = const_cast<HConstant*>(this)->handle(Isolate::Current());
2949 os << Brief(*h) << " ";
2950 if (HasStableMapValue()) os << "[stable-map] ";
2951 if (HasObjectMap()) os << "[map " << *ObjectMap().handle() << "] ";
rossberg@chromium.org657d53b2012-07-12 11:06:03 +00002952 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002953 if (!is_not_in_new_space_) os << "[new space] ";
2954 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002955}
2956
2957
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00002958OStream& HBinaryOperation::PrintDataTo(OStream& os) const { // NOLINT
2959 os << NameOf(left()) << " " << NameOf(right());
2960 if (CheckFlag(kCanOverflow)) os << " !";
2961 if (CheckFlag(kBailoutOnMinusZero)) os << " -0?";
2962 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00002963}
2964
2965
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00002966void HBinaryOperation::InferRepresentation(HInferRepresentationPhase* h_infer) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00002967 DCHECK(CheckFlag(kFlexibleRepresentation));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002968 Representation new_rep = RepresentationFromInputs();
2969 UpdateRepresentation(new_rep, h_infer, "inputs");
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00002970
2971 if (representation().IsSmi() && HasNonSmiUse()) {
2972 UpdateRepresentation(
2973 Representation::Integer32(), h_infer, "use requirements");
2974 }
2975
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002976 if (observed_output_representation_.IsNone()) {
2977 new_rep = RepresentationFromUses();
2978 UpdateRepresentation(new_rep, h_infer, "uses");
2979 } else {
2980 new_rep = RepresentationFromOutput();
2981 UpdateRepresentation(new_rep, h_infer, "output");
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00002982 }
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00002983}
2984
2985
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002986Representation HBinaryOperation::RepresentationFromInputs() {
2987 // Determine the worst case of observed input representations and
2988 // the currently assumed output representation.
2989 Representation rep = representation();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002990 for (int i = 1; i <= 2; ++i) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002991 rep = rep.generalize(observed_input_representation(i));
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002992 }
2993 // If any of the actual input representation is more general than what we
2994 // have so far but not Tagged, use that representation instead.
2995 Representation left_rep = left()->representation();
2996 Representation right_rep = right()->representation();
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00002997 if (!left_rep.IsTagged()) rep = rep.generalize(left_rep);
2998 if (!right_rep.IsTagged()) rep = rep.generalize(right_rep);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00002999
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00003000 return rep;
3001}
3002
3003
3004bool HBinaryOperation::IgnoreObservedOutputRepresentation(
3005 Representation current_rep) {
3006 return ((current_rep.IsInteger32() && CheckUsesForFlag(kTruncatingToInt32)) ||
3007 (current_rep.IsSmi() && CheckUsesForFlag(kTruncatingToSmi))) &&
3008 // Mul in Integer32 mode would be too precise.
machenbach@chromium.org8e36b5b2013-09-26 07:36:30 +00003009 (!this->IsMul() || HMul::cast(this)->MulMinusOne());
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00003010}
3011
3012
3013Representation HBinaryOperation::RepresentationFromOutput() {
3014 Representation rep = representation();
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003015 // Consider observed output representation, but ignore it if it's Double,
3016 // this instruction is not a division, and all its uses are truncating
3017 // to Integer32.
3018 if (observed_output_representation_.is_more_general_than(rep) &&
3019 !IgnoreObservedOutputRepresentation(rep)) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00003020 return observed_output_representation_;
ulan@chromium.org32d7dba2013-04-24 10:59:06 +00003021 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00003022 return Representation::None();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003023}
3024
3025
3026void HBinaryOperation::AssumeRepresentation(Representation r) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +00003027 set_observed_input_representation(1, r);
3028 set_observed_input_representation(2, r);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003029 HValue::AssumeRepresentation(r);
3030}
3031
3032
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00003033void HMathMinMax::InferRepresentation(HInferRepresentationPhase* h_infer) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003034 DCHECK(CheckFlag(kFlexibleRepresentation));
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +00003035 Representation new_rep = RepresentationFromInputs();
3036 UpdateRepresentation(new_rep, h_infer, "inputs");
3037 // Do not care about uses.
3038}
3039
3040
ulan@chromium.org812308e2012-02-29 15:58:45 +00003041Range* HBitwise::InferRange(Zone* zone) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00003042 if (op() == Token::BIT_XOR) {
3043 if (left()->HasRange() && right()->HasRange()) {
3044 // The maximum value has the high bit, and all bits below, set:
3045 // (1 << high) - 1.
3046 // If the range can be negative, the minimum int is a negative number with
3047 // the high bit, and all bits below, unset:
3048 // -(1 << high).
3049 // If it cannot be negative, conservatively choose 0 as minimum int.
3050 int64_t left_upper = left()->range()->upper();
3051 int64_t left_lower = left()->range()->lower();
3052 int64_t right_upper = right()->range()->upper();
3053 int64_t right_lower = right()->range()->lower();
3054
3055 if (left_upper < 0) left_upper = ~left_upper;
3056 if (left_lower < 0) left_lower = ~left_lower;
3057 if (right_upper < 0) right_upper = ~right_upper;
3058 if (right_lower < 0) right_lower = ~right_lower;
3059
3060 int high = MostSignificantBit(
3061 static_cast<uint32_t>(
3062 left_upper | left_lower | right_upper | right_lower));
3063
3064 int64_t limit = 1;
3065 limit <<= high;
3066 int32_t min = (left()->range()->CanBeNegative() ||
3067 right()->range()->CanBeNegative())
3068 ? static_cast<int32_t>(-limit) : 0;
3069 return new(zone) Range(min, static_cast<int32_t>(limit - 1));
3070 }
danno@chromium.orgbee51992013-07-10 14:57:15 +00003071 Range* result = HValue::InferRange(zone);
3072 result->set_can_be_minus_zero(false);
3073 return result;
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00003074 }
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00003075 const int32_t kDefaultMask = static_cast<int32_t>(0xffffffff);
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00003076 int32_t left_mask = (left()->range() != NULL)
3077 ? left()->range()->Mask()
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00003078 : kDefaultMask;
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00003079 int32_t right_mask = (right()->range() != NULL)
3080 ? right()->range()->Mask()
erik.corry@gmail.combbceb572012-03-09 10:52:05 +00003081 : kDefaultMask;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00003082 int32_t result_mask = (op() == Token::BIT_AND)
3083 ? left_mask & right_mask
3084 : left_mask | right_mask;
danno@chromium.orgbee51992013-07-10 14:57:15 +00003085 if (result_mask >= 0) return new(zone) Range(0, result_mask);
3086
3087 Range* result = HValue::InferRange(zone);
3088 result->set_can_be_minus_zero(false);
3089 return result;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003090}
3091
3092
ulan@chromium.org812308e2012-02-29 15:58:45 +00003093Range* HSar::InferRange(Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003094 if (right()->IsConstant()) {
3095 HConstant* c = HConstant::cast(right());
3096 if (c->HasInteger32Value()) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00003097 Range* result = (left()->range() != NULL)
ulan@chromium.org812308e2012-02-29 15:58:45 +00003098 ? left()->range()->Copy(zone)
3099 : new(zone) Range();
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00003100 result->Sar(c->Integer32Value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003101 return result;
3102 }
3103 }
ulan@chromium.org812308e2012-02-29 15:58:45 +00003104 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003105}
3106
3107
ulan@chromium.org812308e2012-02-29 15:58:45 +00003108Range* HShr::InferRange(Zone* zone) {
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003109 if (right()->IsConstant()) {
3110 HConstant* c = HConstant::cast(right());
3111 if (c->HasInteger32Value()) {
3112 int shift_count = c->Integer32Value() & 0x1f;
3113 if (left()->range()->CanBeNegative()) {
3114 // Only compute bounds if the result always fits into an int32.
3115 return (shift_count >= 1)
ulan@chromium.org812308e2012-02-29 15:58:45 +00003116 ? new(zone) Range(0,
3117 static_cast<uint32_t>(0xffffffff) >> shift_count)
3118 : new(zone) Range();
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003119 } else {
3120 // For positive inputs we can use the >> operator.
3121 Range* result = (left()->range() != NULL)
ulan@chromium.org812308e2012-02-29 15:58:45 +00003122 ? left()->range()->Copy(zone)
3123 : new(zone) Range();
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003124 result->Sar(c->Integer32Value());
3125 return result;
3126 }
3127 }
3128 }
ulan@chromium.org812308e2012-02-29 15:58:45 +00003129 return HValue::InferRange(zone);
ricow@chromium.org2c99e282011-07-28 09:15:17 +00003130}
3131
3132
ulan@chromium.org812308e2012-02-29 15:58:45 +00003133Range* HShl::InferRange(Zone* zone) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003134 if (right()->IsConstant()) {
3135 HConstant* c = HConstant::cast(right());
3136 if (c->HasInteger32Value()) {
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00003137 Range* result = (left()->range() != NULL)
ulan@chromium.org812308e2012-02-29 15:58:45 +00003138 ? left()->range()->Copy(zone)
3139 : new(zone) Range();
karlklose@chromium.org8f806e82011-03-07 14:06:08 +00003140 result->Shl(c->Integer32Value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003141 return result;
3142 }
3143 }
ulan@chromium.org812308e2012-02-29 15:58:45 +00003144 return HValue::InferRange(zone);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003145}
3146
3147
danno@chromium.orgd3c42102013-08-01 16:58:23 +00003148Range* HLoadNamedField::InferRange(Zone* zone) {
machenbach@chromium.org935a7792013-11-12 09:05:18 +00003149 if (access().representation().IsInteger8()) {
3150 return new(zone) Range(kMinInt8, kMaxInt8);
3151 }
3152 if (access().representation().IsUInteger8()) {
3153 return new(zone) Range(kMinUInt8, kMaxUInt8);
3154 }
3155 if (access().representation().IsInteger16()) {
3156 return new(zone) Range(kMinInt16, kMaxInt16);
3157 }
3158 if (access().representation().IsUInteger16()) {
3159 return new(zone) Range(kMinUInt16, kMaxUInt16);
jkummerow@chromium.orgd8a3a142013-10-03 12:15:05 +00003160 }
danno@chromium.orgd3c42102013-08-01 16:58:23 +00003161 if (access().IsStringLength()) {
3162 return new(zone) Range(0, String::kMaxLength);
3163 }
3164 return HValue::InferRange(zone);
3165}
3166
3167
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003168Range* HLoadKeyed::InferRange(Zone* zone) {
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003169 switch (elements_kind()) {
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00003170 case EXTERNAL_INT8_ELEMENTS:
machenbach@chromium.org935a7792013-11-12 09:05:18 +00003171 return new(zone) Range(kMinInt8, kMaxInt8);
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00003172 case EXTERNAL_UINT8_ELEMENTS:
3173 case EXTERNAL_UINT8_CLAMPED_ELEMENTS:
machenbach@chromium.org935a7792013-11-12 09:05:18 +00003174 return new(zone) Range(kMinUInt8, kMaxUInt8);
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00003175 case EXTERNAL_INT16_ELEMENTS:
machenbach@chromium.org935a7792013-11-12 09:05:18 +00003176 return new(zone) Range(kMinInt16, kMaxInt16);
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00003177 case EXTERNAL_UINT16_ELEMENTS:
machenbach@chromium.org935a7792013-11-12 09:05:18 +00003178 return new(zone) Range(kMinUInt16, kMaxUInt16);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003179 default:
ulan@chromium.org812308e2012-02-29 15:58:45 +00003180 return HValue::InferRange(zone);
yangguo@chromium.org659ceec2012-01-26 07:37:54 +00003181 }
3182}
3183
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003184
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003185OStream& HCompareGeneric::PrintDataTo(OStream& os) const { // NOLINT
3186 os << Token::Name(token()) << " ";
3187 return HBinaryOperation::PrintDataTo(os);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003188}
3189
3190
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003191OStream& HStringCompareAndBranch::PrintDataTo(OStream& os) const { // NOLINT
3192 os << Token::Name(token()) << " ";
3193 return HControlInstruction::PrintDataTo(os);
erikcorry0ad885c2011-11-21 13:51:57 +00003194}
3195
3196
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003197OStream& HCompareNumericAndBranch::PrintDataTo(OStream& os) const { // NOLINT
3198 os << Token::Name(token()) << " " << NameOf(left()) << " " << NameOf(right());
3199 return HControlInstruction::PrintDataTo(os);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003200}
3201
3202
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003203OStream& HCompareObjectEqAndBranch::PrintDataTo(OStream& os) const { // NOLINT
3204 os << NameOf(left()) << " " << NameOf(right());
3205 return HControlInstruction::PrintDataTo(os);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003206}
3207
3208
jkummerow@chromium.orgd8a3a142013-10-03 12:15:05 +00003209bool HCompareObjectEqAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
machenbach@chromium.org8545d492014-03-17 09:28:03 +00003210 if (known_successor_index() != kNoKnownSuccessorIndex) {
3211 *block = SuccessorAt(known_successor_index());
3212 return true;
3213 }
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00003214 if (FLAG_fold_constants && left()->IsConstant() && right()->IsConstant()) {
dslomov@chromium.org486536d2014-03-12 13:09:18 +00003215 *block = HConstant::cast(left())->DataEquals(HConstant::cast(right()))
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00003216 ? FirstSuccessor() : SecondSuccessor();
3217 return true;
3218 }
3219 *block = NULL;
3220 return false;
3221}
3222
3223
3224bool ConstantIsObject(HConstant* constant, Isolate* isolate) {
3225 if (constant->HasNumberValue()) return false;
3226 if (constant->GetUnique().IsKnownGlobal(isolate->heap()->null_value())) {
3227 return true;
3228 }
3229 if (constant->IsUndetectable()) return false;
3230 InstanceType type = constant->GetInstanceType();
3231 return (FIRST_NONCALLABLE_SPEC_OBJECT_TYPE <= type) &&
3232 (type <= LAST_NONCALLABLE_SPEC_OBJECT_TYPE);
3233}
3234
3235
3236bool HIsObjectAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3237 if (FLAG_fold_constants && value()->IsConstant()) {
3238 *block = ConstantIsObject(HConstant::cast(value()), isolate())
3239 ? FirstSuccessor() : SecondSuccessor();
3240 return true;
3241 }
3242 *block = NULL;
3243 return false;
3244}
3245
3246
3247bool HIsStringAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
machenbach@chromium.org8d8413c2014-06-03 00:04:55 +00003248 if (known_successor_index() != kNoKnownSuccessorIndex) {
3249 *block = SuccessorAt(known_successor_index());
3250 return true;
3251 }
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00003252 if (FLAG_fold_constants && value()->IsConstant()) {
3253 *block = HConstant::cast(value())->HasStringValue()
3254 ? FirstSuccessor() : SecondSuccessor();
3255 return true;
3256 }
machenbach@chromium.org8d8413c2014-06-03 00:04:55 +00003257 if (value()->type().IsString()) {
3258 *block = FirstSuccessor();
3259 return true;
3260 }
3261 if (value()->type().IsSmi() ||
3262 value()->type().IsNull() ||
3263 value()->type().IsBoolean() ||
3264 value()->type().IsUndefined() ||
3265 value()->type().IsJSObject()) {
3266 *block = SecondSuccessor();
3267 return true;
3268 }
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00003269 *block = NULL;
3270 return false;
3271}
3272
3273
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00003274bool HIsUndetectableAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3275 if (FLAG_fold_constants && value()->IsConstant()) {
3276 *block = HConstant::cast(value())->IsUndetectable()
3277 ? FirstSuccessor() : SecondSuccessor();
3278 return true;
3279 }
3280 *block = NULL;
3281 return false;
3282}
3283
3284
3285bool HHasInstanceTypeAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3286 if (FLAG_fold_constants && value()->IsConstant()) {
3287 InstanceType type = HConstant::cast(value())->GetInstanceType();
3288 *block = (from_ <= type) && (type <= to_)
3289 ? FirstSuccessor() : SecondSuccessor();
jkummerow@chromium.orgd8a3a142013-10-03 12:15:05 +00003290 return true;
3291 }
3292 *block = NULL;
3293 return false;
3294}
3295
3296
danno@chromium.orgc00ec2b2013-08-14 17:13:49 +00003297void HCompareHoleAndBranch::InferRepresentation(
3298 HInferRepresentationPhase* h_infer) {
machenbach@chromium.org528ce022013-09-23 14:09:36 +00003299 ChangeRepresentation(value()->representation());
danno@chromium.orgc00ec2b2013-08-14 17:13:49 +00003300}
3301
3302
machenbach@chromium.orga77ec9c2014-04-23 00:05:14 +00003303bool HCompareNumericAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
3304 if (left() == right() &&
3305 left()->representation().IsSmiOrInteger32()) {
3306 *block = (token() == Token::EQ ||
3307 token() == Token::EQ_STRICT ||
3308 token() == Token::LTE ||
3309 token() == Token::GTE)
3310 ? FirstSuccessor() : SecondSuccessor();
3311 return true;
3312 }
3313 *block = NULL;
3314 return false;
3315}
3316
3317
machenbach@chromium.org0cc09502013-11-13 12:20:55 +00003318bool HCompareMinusZeroAndBranch::KnownSuccessorBlock(HBasicBlock** block) {
machenbach@chromium.orgbcc36722014-03-11 07:52:26 +00003319 if (FLAG_fold_constants && value()->IsConstant()) {
3320 HConstant* constant = HConstant::cast(value());
3321 if (constant->HasDoubleValue()) {
3322 *block = IsMinusZero(constant->DoubleValue())
3323 ? FirstSuccessor() : SecondSuccessor();
3324 return true;
3325 }
3326 }
machenbach@chromium.org0cc09502013-11-13 12:20:55 +00003327 if (value()->representation().IsSmiOrInteger32()) {
3328 // A Smi or Integer32 cannot contain minus zero.
3329 *block = SecondSuccessor();
3330 return true;
3331 }
3332 *block = NULL;
3333 return false;
3334}
3335
3336
3337void HCompareMinusZeroAndBranch::InferRepresentation(
3338 HInferRepresentationPhase* h_infer) {
3339 ChangeRepresentation(value()->representation());
3340}
3341
3342
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003343OStream& HGoto::PrintDataTo(OStream& os) const { // NOLINT
3344 return os << *SuccessorAt(0);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00003345}
3346
3347
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00003348void HCompareNumericAndBranch::InferRepresentation(
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00003349 HInferRepresentationPhase* h_infer) {
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003350 Representation left_rep = left()->representation();
3351 Representation right_rep = right()->representation();
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00003352 Representation observed_left = observed_input_representation(0);
3353 Representation observed_right = observed_input_representation(1);
3354
ulan@chromium.orgdfe53072013-06-06 14:14:51 +00003355 Representation rep = Representation::None();
3356 rep = rep.generalize(observed_left);
3357 rep = rep.generalize(observed_right);
3358 if (rep.IsNone() || rep.IsSmiOrInteger32()) {
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00003359 if (!left_rep.IsTagged()) rep = rep.generalize(left_rep);
3360 if (!right_rep.IsTagged()) rep = rep.generalize(right_rep);
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003361 } else {
3362 rep = Representation::Double();
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00003363 }
3364
3365 if (rep.IsDouble()) {
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00003366 // According to the ES5 spec (11.9.3, 11.8.5), Equality comparisons (==, ===
3367 // and !=) have special handling of undefined, e.g. undefined == undefined
3368 // is 'true'. Relational comparisons have a different semantic, first
3369 // calling ToPrimitive() on their arguments. The standard Crankshaft
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00003370 // tagged-to-double conversion to ensure the HCompareNumericAndBranch's
3371 // inputs are doubles caused 'undefined' to be converted to NaN. That's
3372 // compatible out-of-the box with ordered relational comparisons (<, >, <=,
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00003373 // >=). However, for equality comparisons (and for 'in' and 'instanceof'),
3374 // it is not consistent with the spec. For example, it would cause undefined
3375 // == undefined (should be true) to be evaluated as NaN == NaN
3376 // (false). Therefore, any comparisons other than ordered relational
3377 // comparisons must cause a deopt when one of their arguments is undefined.
3378 // See also v8:1434
rossberg@chromium.orgb99c7542013-05-31 11:40:45 +00003379 if (Token::IsOrderedRelationalCompareOp(token_)) {
3380 SetFlag(kAllowUndefinedAsNaN);
ulan@chromium.org9a21ec42012-03-06 08:42:24 +00003381 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003382 }
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00003383 ChangeRepresentation(rep);
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003384}
3385
3386
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003387OStream& HParameter::PrintDataTo(OStream& os) const { // NOLINT
3388 return os << index();
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003389}
3390
3391
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003392OStream& HLoadNamedField::PrintDataTo(OStream& os) const { // NOLINT
3393 os << NameOf(object()) << access_;
machenbach@chromium.org09cae8d2014-01-30 01:05:27 +00003394
machenbach@chromium.org59249172014-05-08 00:04:50 +00003395 if (maps() != NULL) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003396 os << " [" << *maps()->at(0).handle();
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00003397 for (int i = 1; i < maps()->size(); ++i) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003398 os << "," << *maps()->at(i).handle();
machenbach@chromium.org9fa61952014-04-17 00:05:12 +00003399 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003400 os << "]";
machenbach@chromium.orge9fd6582014-04-16 00:05:02 +00003401 }
3402
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003403 if (HasDependency()) os << " " << NameOf(dependency());
3404 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003405}
3406
3407
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003408OStream& HLoadNamedGeneric::PrintDataTo(OStream& os) const { // NOLINT
3409 Handle<String> n = Handle<String>::cast(name());
3410 return os << NameOf(object()) << "." << n->ToCString().get();
whesse@chromium.org4acdc2c2011-08-15 13:01:23 +00003411}
3412
3413
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003414OStream& HLoadKeyed::PrintDataTo(OStream& os) const { // NOLINT
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003415 if (!is_external()) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003416 os << NameOf(elements());
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003417 } else {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003418 DCHECK(elements_kind() >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003419 elements_kind() <= LAST_EXTERNAL_ARRAY_ELEMENTS_KIND);
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003420 os << NameOf(elements()) << "." << ElementsKindToString(elements_kind());
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003421 }
3422
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003423 os << "[" << NameOf(key());
3424 if (IsDehoisted()) os << " + " << base_offset();
3425 os << "]";
ulan@chromium.org8e8d8822012-11-23 14:36:46 +00003426
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003427 if (HasDependency()) os << " " << NameOf(dependency());
3428 if (RequiresHoleCheck()) os << " check_hole";
3429 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003430}
3431
3432
machenbach@chromium.org975b9402014-06-24 00:06:56 +00003433bool HLoadKeyed::TryIncreaseBaseOffset(uint32_t increase_by_value) {
3434 // The base offset is usually simply the size of the array header, except
3435 // with dehoisting adds an addition offset due to a array index key
3436 // manipulation, in which case it becomes (array header size +
3437 // constant-offset-from-key * kPointerSize)
3438 uint32_t base_offset = BaseOffsetField::decode(bit_field_);
3439 v8::base::internal::CheckedNumeric<uint32_t> addition_result = base_offset;
3440 addition_result += increase_by_value;
3441 if (!addition_result.IsValid()) return false;
3442 base_offset = addition_result.ValueOrDie();
3443 if (!BaseOffsetField::is_valid(base_offset)) return false;
3444 bit_field_ = BaseOffsetField::update(bit_field_, base_offset);
3445 return true;
3446}
3447
3448
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00003449bool HLoadKeyed::UsesMustHandleHole() const {
mmassi@chromium.org7028c052012-06-13 11:51:58 +00003450 if (IsFastPackedElementsKind(elements_kind())) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00003451 return false;
3452 }
3453
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003454 if (IsExternalArrayElementsKind(elements_kind())) {
3455 return false;
3456 }
3457
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00003458 if (hole_mode() == ALLOW_RETURN_HOLE) {
3459 if (IsFastDoubleElementsKind(elements_kind())) {
3460 return AllUsesCanTreatHoleAsNaN();
3461 }
3462 return true;
3463 }
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00003464
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003465 if (IsFastDoubleElementsKind(elements_kind())) {
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00003466 return false;
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003467 }
3468
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00003469 // Holes are only returned as tagged values.
3470 if (!representation().IsTagged()) {
3471 return false;
3472 }
3473
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003474 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
3475 HValue* use = it.value();
jkummerow@chromium.orgc1184022013-05-28 16:58:15 +00003476 if (!use->IsChange()) return false;
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003477 }
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00003478
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00003479 return true;
3480}
3481
3482
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00003483bool HLoadKeyed::AllUsesCanTreatHoleAsNaN() const {
danno@chromium.orgc00ec2b2013-08-14 17:13:49 +00003484 return IsFastDoubleElementsKind(elements_kind()) &&
3485 CheckUsesForFlag(HValue::kAllowUndefinedAsNaN);
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00003486}
3487
3488
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00003489bool HLoadKeyed::RequiresHoleCheck() const {
3490 if (IsFastPackedElementsKind(elements_kind())) {
3491 return false;
3492 }
3493
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00003494 if (IsExternalArrayElementsKind(elements_kind())) {
3495 return false;
3496 }
3497
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00003498 return !UsesMustHandleHole();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003499}
3500
3501
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003502OStream& HLoadKeyedGeneric::PrintDataTo(OStream& os) const { // NOLINT
3503 return os << NameOf(object()) << "[" << NameOf(key()) << "]";
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003504}
3505
3506
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00003507HValue* HLoadKeyedGeneric::Canonicalize() {
3508 // Recognize generic keyed loads that use property name generated
3509 // by for-in statement as a key and rewrite them into fast property load
3510 // by index.
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003511 if (key()->IsLoadKeyed()) {
3512 HLoadKeyed* key_load = HLoadKeyed::cast(key());
3513 if (key_load->elements()->IsForInCacheArray()) {
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00003514 HForInCacheArray* names_cache =
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003515 HForInCacheArray::cast(key_load->elements());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00003516
3517 if (names_cache->enumerable() == object()) {
3518 HForInCacheArray* index_cache =
3519 names_cache->index_cache();
3520 HCheckMapValue* map_check =
danno@chromium.orgd3c42102013-08-01 16:58:23 +00003521 HCheckMapValue::New(block()->graph()->zone(),
3522 block()->graph()->GetInvalidContext(),
3523 object(),
3524 names_cache->map());
3525 HInstruction* index = HLoadKeyed::New(
3526 block()->graph()->zone(),
3527 block()->graph()->GetInvalidContext(),
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00003528 index_cache,
yangguo@chromium.org304cc332012-07-24 07:59:48 +00003529 key_load->key(),
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003530 key_load->key(),
3531 key_load->elements_kind());
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00003532 map_check->InsertBefore(this);
3533 index->InsertBefore(this);
machenbach@chromium.orgaf4fba32014-01-27 01:05:32 +00003534 return Prepend(new(block()->zone()) HLoadFieldByIndex(
3535 object(), index));
kmillikin@chromium.orgbe6bd102012-02-23 08:45:21 +00003536 }
3537 }
3538 }
3539
3540 return this;
3541}
3542
3543
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003544OStream& HStoreNamedGeneric::PrintDataTo(OStream& os) const { // NOLINT
3545 Handle<String> n = Handle<String>::cast(name());
3546 return os << NameOf(object()) << "." << n->ToCString().get() << " = "
3547 << NameOf(value());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003548}
3549
3550
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003551OStream& HStoreNamedField::PrintDataTo(OStream& os) const { // NOLINT
3552 os << NameOf(object()) << access_ << " = " << NameOf(value());
3553 if (NeedsWriteBarrier()) os << " (write-barrier)";
3554 if (has_transition()) os << " (transition map " << *transition_map() << ")";
3555 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003556}
3557
3558
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003559OStream& HStoreKeyed::PrintDataTo(OStream& os) const { // NOLINT
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003560 if (!is_external()) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003561 os << NameOf(elements());
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003562 } else {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003563 DCHECK(elements_kind() >= FIRST_EXTERNAL_ARRAY_ELEMENTS_KIND &&
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003564 elements_kind() <= LAST_EXTERNAL_ARRAY_ELEMENTS_KIND);
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003565 os << NameOf(elements()) << "." << ElementsKindToString(elements_kind());
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00003566 }
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003567
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003568 os << "[" << NameOf(key());
3569 if (IsDehoisted()) os << " + " << base_offset();
3570 return os << "] = " << NameOf(value());
rossberg@chromium.org717967f2011-07-20 13:44:42 +00003571}
3572
3573
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003574OStream& HStoreKeyedGeneric::PrintDataTo(OStream& os) const { // NOLINT
3575 return os << NameOf(object()) << "[" << NameOf(key())
3576 << "] = " << NameOf(value());
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003577}
3578
3579
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003580OStream& HTransitionElementsKind::PrintDataTo(OStream& os) const { // NOLINT
3581 os << NameOf(object());
machenbach@chromium.org528ce022013-09-23 14:09:36 +00003582 ElementsKind from_kind = original_map().handle()->elements_kind();
3583 ElementsKind to_kind = transitioned_map().handle()->elements_kind();
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003584 os << " " << *original_map().handle() << " ["
3585 << ElementsAccessor::ForKind(from_kind)->name() << "] -> "
3586 << *transitioned_map().handle() << " ["
3587 << ElementsAccessor::ForKind(to_kind)->name() << "]";
3588 if (IsSimpleMapChangeTransition(from_kind, to_kind)) os << " (simple)";
3589 return os;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003590}
3591
3592
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003593OStream& HLoadGlobalCell::PrintDataTo(OStream& os) const { // NOLINT
3594 os << "[" << *cell().handle() << "]";
machenbach@chromium.orgf1a5a1d2014-08-22 00:04:38 +00003595 if (details_.IsConfigurable()) os << " (configurable)";
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003596 if (details_.IsReadOnly()) os << " (read-only)";
3597 return os;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003598}
3599
3600
jkummerow@chromium.orgc1956672012-10-11 15:57:38 +00003601bool HLoadGlobalCell::RequiresHoleCheck() const {
machenbach@chromium.orgf1a5a1d2014-08-22 00:04:38 +00003602 if (!details_.IsConfigurable()) return false;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003603 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
3604 HValue* use = it.value();
3605 if (!use->IsChange()) return true;
3606 }
3607 return false;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003608}
3609
3610
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003611OStream& HLoadGlobalGeneric::PrintDataTo(OStream& os) const { // NOLINT
3612 return os << name()->ToCString().get() << " ";
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00003613}
3614
3615
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003616OStream& HInnerAllocatedObject::PrintDataTo(OStream& os) const { // NOLINT
3617 os << NameOf(base_object()) << " offset ";
3618 return offset()->PrintTo(os);
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +00003619}
3620
3621
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003622OStream& HStoreGlobalCell::PrintDataTo(OStream& os) const { // NOLINT
3623 os << "[" << *cell().handle() << "] = " << NameOf(value());
machenbach@chromium.orgf1a5a1d2014-08-22 00:04:38 +00003624 if (details_.IsConfigurable()) os << " (configurable)";
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003625 if (details_.IsReadOnly()) os << " (read-only)";
3626 return os;
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003627}
3628
3629
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003630OStream& HLoadContextSlot::PrintDataTo(OStream& os) const { // NOLINT
3631 return os << NameOf(value()) << "[" << slot_index() << "]";
ricow@chromium.org83aa5492011-02-07 12:42:56 +00003632}
3633
3634
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003635OStream& HStoreContextSlot::PrintDataTo(OStream& os) const { // NOLINT
3636 return os << NameOf(context()) << "[" << slot_index()
3637 << "] = " << NameOf(value());
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +00003638}
3639
3640
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003641// Implementation of type inference and type conversions. Calculates
3642// the inferred type of this instruction based on the input operands.
3643
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003644HType HValue::CalculateInferredType() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003645 return type_;
3646}
3647
3648
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +00003649HType HPhi::CalculateInferredType() {
danno@chromium.orgd3c42102013-08-01 16:58:23 +00003650 if (OperandCount() == 0) return HType::Tagged();
3651 HType result = OperandAt(0)->type();
3652 for (int i = 1; i < OperandCount(); ++i) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00003653 HType current = OperandAt(i)->type();
3654 result = result.Combine(current);
3655 }
3656 return result;
3657}
3658
3659
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003660HType HChange::CalculateInferredType() {
3661 if (from().IsDouble() && to().IsTagged()) return HType::HeapNumber();
3662 return type();
3663}
3664
3665
danno@chromium.org1fd77d52013-06-07 16:01:45 +00003666Representation HUnaryMathOperation::RepresentationFromInputs() {
machenbach@chromium.orga86d4162014-05-01 00:05:11 +00003667 if (SupportsFlexibleFloorAndRound() &&
3668 (op_ == kMathFloor || op_ == kMathRound)) {
3669 // Floor and Round always take a double input. The integral result can be
3670 // used as an integer or a double. Infer the representation from the uses.
3671 return Representation::None();
3672 }
danno@chromium.org1fd77d52013-06-07 16:01:45 +00003673 Representation rep = representation();
3674 // If any of the actual input representation is more general than what we
3675 // have so far but not Tagged, use that representation instead.
3676 Representation input_rep = value()->representation();
danno@chromium.orgad75d6f2013-08-12 16:57:59 +00003677 if (!input_rep.IsTagged()) {
3678 rep = rep.generalize(input_rep);
danno@chromium.orgad75d6f2013-08-12 16:57:59 +00003679 }
danno@chromium.org1fd77d52013-06-07 16:01:45 +00003680 return rep;
3681}
3682
3683
machenbach@chromium.org82975302014-02-06 01:06:18 +00003684bool HAllocate::HandleSideEffectDominator(GVNFlag side_effect,
danno@chromium.org169691d2013-07-15 08:01:13 +00003685 HValue* dominator) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003686 DCHECK(side_effect == kNewSpacePromotion);
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003687 Zone* zone = block()->zone();
machenbach@chromium.org82975302014-02-06 01:06:18 +00003688 if (!FLAG_use_allocation_folding) return false;
jkummerow@chromium.org10480472013-07-17 08:22:15 +00003689
danno@chromium.org169691d2013-07-15 08:01:13 +00003690 // Try to fold allocations together with their dominating allocations.
jkummerow@chromium.org10480472013-07-17 08:22:15 +00003691 if (!dominator->IsAllocate()) {
3692 if (FLAG_trace_allocation_folding) {
3693 PrintF("#%d (%s) cannot fold into #%d (%s)\n",
3694 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3695 }
machenbach@chromium.org82975302014-02-06 01:06:18 +00003696 return false;
danno@chromium.org169691d2013-07-15 08:01:13 +00003697 }
jkummerow@chromium.org10480472013-07-17 08:22:15 +00003698
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00003699 // Check whether we are folding within the same block for local folding.
3700 if (FLAG_use_local_allocation_folding && dominator->block() != block()) {
3701 if (FLAG_trace_allocation_folding) {
3702 PrintF("#%d (%s) cannot fold into #%d (%s), crosses basic blocks\n",
3703 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3704 }
3705 return false;
3706 }
3707
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003708 HAllocate* dominator_allocate = HAllocate::cast(dominator);
3709 HValue* dominator_size = dominator_allocate->size();
danno@chromium.org169691d2013-07-15 08:01:13 +00003710 HValue* current_size = size();
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003711
danno@chromium.org169691d2013-07-15 08:01:13 +00003712 // TODO(hpayer): Add support for non-constant allocation in dominator.
machenbach@chromium.org38de99a2014-06-05 00:04:53 +00003713 if (!dominator_size->IsInteger32Constant()) {
jkummerow@chromium.org10480472013-07-17 08:22:15 +00003714 if (FLAG_trace_allocation_folding) {
machenbach@chromium.org38de99a2014-06-05 00:04:53 +00003715 PrintF("#%d (%s) cannot fold into #%d (%s), "
3716 "dynamic allocation size in dominator\n",
jkummerow@chromium.org10480472013-07-17 08:22:15 +00003717 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3718 }
machenbach@chromium.org82975302014-02-06 01:06:18 +00003719 return false;
danno@chromium.org169691d2013-07-15 08:01:13 +00003720 }
3721
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003722 dominator_allocate = GetFoldableDominator(dominator_allocate);
3723 if (dominator_allocate == NULL) {
machenbach@chromium.org82975302014-02-06 01:06:18 +00003724 return false;
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003725 }
3726
machenbach@chromium.org38de99a2014-06-05 00:04:53 +00003727 if (!has_size_upper_bound()) {
3728 if (FLAG_trace_allocation_folding) {
3729 PrintF("#%d (%s) cannot fold into #%d (%s), "
3730 "can't estimate total allocation size\n",
3731 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3732 }
3733 return false;
3734 }
3735
3736 if (!current_size->IsInteger32Constant()) {
3737 // If it's not constant then it is a size_in_bytes calculation graph
3738 // like this: (const_header_size + const_element_size * size).
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003739 DCHECK(current_size->IsInstruction());
machenbach@chromium.org38de99a2014-06-05 00:04:53 +00003740
3741 HInstruction* current_instr = HInstruction::cast(current_size);
3742 if (!current_instr->Dominates(dominator_allocate)) {
3743 if (FLAG_trace_allocation_folding) {
3744 PrintF("#%d (%s) cannot fold into #%d (%s), dynamic size "
3745 "value does not dominate target allocation\n",
3746 id(), Mnemonic(), dominator_allocate->id(),
3747 dominator_allocate->Mnemonic());
3748 }
3749 return false;
3750 }
3751 }
3752
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003753 DCHECK((IsNewSpaceAllocation() &&
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003754 dominator_allocate->IsNewSpaceAllocation()) ||
3755 (IsOldDataSpaceAllocation() &&
3756 dominator_allocate->IsOldDataSpaceAllocation()) ||
3757 (IsOldPointerSpaceAllocation() &&
3758 dominator_allocate->IsOldPointerSpaceAllocation()));
3759
danno@chromium.org169691d2013-07-15 08:01:13 +00003760 // First update the size of the dominator allocate instruction.
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003761 dominator_size = dominator_allocate->size();
verwaest@chromium.org662436e2013-08-28 08:41:27 +00003762 int32_t original_object_size =
danno@chromium.org169691d2013-07-15 08:01:13 +00003763 HConstant::cast(dominator_size)->GetInteger32Constant();
verwaest@chromium.org662436e2013-08-28 08:41:27 +00003764 int32_t dominator_size_constant = original_object_size;
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00003765
3766 if (MustAllocateDoubleAligned()) {
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00003767 if ((dominator_size_constant & kDoubleAlignmentMask) != 0) {
3768 dominator_size_constant += kDoubleSize / 2;
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00003769 }
3770 }
3771
machenbach@chromium.org38de99a2014-06-05 00:04:53 +00003772 int32_t current_size_max_value = size_upper_bound()->GetInteger32Constant();
3773 int32_t new_dominator_size = dominator_size_constant + current_size_max_value;
3774
machenbach@chromium.orgef9a2b92014-01-24 01:05:19 +00003775 // Since we clear the first word after folded memory, we cannot use the
3776 // whole Page::kMaxRegularHeapObjectSize memory.
3777 if (new_dominator_size > Page::kMaxRegularHeapObjectSize - kPointerSize) {
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00003778 if (FLAG_trace_allocation_folding) {
3779 PrintF("#%d (%s) cannot fold into #%d (%s) due to size: %d\n",
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003780 id(), Mnemonic(), dominator_allocate->id(),
3781 dominator_allocate->Mnemonic(), new_dominator_size);
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00003782 }
machenbach@chromium.org82975302014-02-06 01:06:18 +00003783 return false;
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00003784 }
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003785
machenbach@chromium.org38de99a2014-06-05 00:04:53 +00003786 HInstruction* new_dominator_size_value;
3787
3788 if (current_size->IsInteger32Constant()) {
3789 new_dominator_size_value =
3790 HConstant::CreateAndInsertBefore(zone,
3791 context(),
3792 new_dominator_size,
3793 Representation::None(),
3794 dominator_allocate);
3795 } else {
3796 HValue* new_dominator_size_constant =
3797 HConstant::CreateAndInsertBefore(zone,
3798 context(),
3799 dominator_size_constant,
3800 Representation::Integer32(),
3801 dominator_allocate);
3802
3803 // Add old and new size together and insert.
3804 current_size->ChangeRepresentation(Representation::Integer32());
3805
3806 new_dominator_size_value = HAdd::New(zone, context(),
3807 new_dominator_size_constant, current_size);
3808 new_dominator_size_value->ClearFlag(HValue::kCanOverflow);
3809 new_dominator_size_value->ChangeRepresentation(Representation::Integer32());
3810
3811 new_dominator_size_value->InsertBefore(dominator_allocate);
3812 }
3813
3814 dominator_allocate->UpdateSize(new_dominator_size_value);
3815
3816 if (MustAllocateDoubleAligned()) {
3817 if (!dominator_allocate->MustAllocateDoubleAligned()) {
3818 dominator_allocate->MakeDoubleAligned();
3819 }
3820 }
danno@chromium.org169691d2013-07-15 08:01:13 +00003821
machenbach@chromium.org3c3c8d72014-05-12 00:05:07 +00003822 bool keep_new_space_iterable = FLAG_log_gc || FLAG_heap_stats;
danno@chromium.org169691d2013-07-15 08:01:13 +00003823#ifdef VERIFY_HEAP
machenbach@chromium.org3c3c8d72014-05-12 00:05:07 +00003824 keep_new_space_iterable = keep_new_space_iterable || FLAG_verify_heap;
3825#endif
3826
3827 if (keep_new_space_iterable && dominator_allocate->IsNewSpaceAllocation()) {
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003828 dominator_allocate->MakePrefillWithFiller();
verwaest@chromium.org662436e2013-08-28 08:41:27 +00003829 } else {
3830 // TODO(hpayer): This is a short-term hack to make allocation mementos
3831 // work again in new space.
hpayer@chromium.org2311a912013-08-28 13:39:38 +00003832 dominator_allocate->ClearNextMapWord(original_object_size);
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +00003833 }
danno@chromium.org169691d2013-07-15 08:01:13 +00003834
ulan@chromium.org9ca30172014-01-02 12:10:54 +00003835 dominator_allocate->UpdateClearNextMapWord(MustClearNextMapWord());
verwaest@chromium.org662436e2013-08-28 08:41:27 +00003836
danno@chromium.org169691d2013-07-15 08:01:13 +00003837 // After that replace the dominated allocate instruction.
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003838 HInstruction* inner_offset = HConstant::CreateAndInsertBefore(
3839 zone,
3840 context(),
3841 dominator_size_constant,
3842 Representation::None(),
3843 this);
3844
danno@chromium.org169691d2013-07-15 08:01:13 +00003845 HInstruction* dominated_allocate_instr =
danno@chromium.orgd3c42102013-08-01 16:58:23 +00003846 HInnerAllocatedObject::New(zone,
3847 context(),
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003848 dominator_allocate,
hpayer@chromium.org4f99be92013-12-18 16:23:55 +00003849 inner_offset,
danno@chromium.orgd3c42102013-08-01 16:58:23 +00003850 type());
danno@chromium.org169691d2013-07-15 08:01:13 +00003851 dominated_allocate_instr->InsertBefore(this);
3852 DeleteAndReplaceWith(dominated_allocate_instr);
3853 if (FLAG_trace_allocation_folding) {
3854 PrintF("#%d (%s) folded into #%d (%s)\n",
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003855 id(), Mnemonic(), dominator_allocate->id(),
3856 dominator_allocate->Mnemonic());
danno@chromium.org169691d2013-07-15 08:01:13 +00003857 }
machenbach@chromium.org82975302014-02-06 01:06:18 +00003858 return true;
danno@chromium.org169691d2013-07-15 08:01:13 +00003859}
3860
3861
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003862HAllocate* HAllocate::GetFoldableDominator(HAllocate* dominator) {
3863 if (!IsFoldable(dominator)) {
3864 // We cannot hoist old space allocations over new space allocations.
3865 if (IsNewSpaceAllocation() || dominator->IsNewSpaceAllocation()) {
3866 if (FLAG_trace_allocation_folding) {
3867 PrintF("#%d (%s) cannot fold into #%d (%s), new space hoisting\n",
3868 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3869 }
3870 return NULL;
3871 }
3872
3873 HAllocate* dominator_dominator = dominator->dominating_allocate_;
3874
3875 // We can hoist old data space allocations over an old pointer space
3876 // allocation and vice versa. For that we have to check the dominator
3877 // of the dominator allocate instruction.
3878 if (dominator_dominator == NULL) {
3879 dominating_allocate_ = dominator;
3880 if (FLAG_trace_allocation_folding) {
3881 PrintF("#%d (%s) cannot fold into #%d (%s), different spaces\n",
3882 id(), Mnemonic(), dominator->id(), dominator->Mnemonic());
3883 }
3884 return NULL;
3885 }
3886
3887 // We can just fold old space allocations that are in the same basic block,
3888 // since it is not guaranteed that we fill up the whole allocated old
3889 // space memory.
3890 // TODO(hpayer): Remove this limitation and add filler maps for each each
3891 // allocation as soon as we have store elimination.
3892 if (block()->block_id() != dominator_dominator->block()->block_id()) {
3893 if (FLAG_trace_allocation_folding) {
3894 PrintF("#%d (%s) cannot fold into #%d (%s), different basic blocks\n",
3895 id(), Mnemonic(), dominator_dominator->id(),
3896 dominator_dominator->Mnemonic());
3897 }
3898 return NULL;
3899 }
3900
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003901 DCHECK((IsOldDataSpaceAllocation() &&
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003902 dominator_dominator->IsOldDataSpaceAllocation()) ||
3903 (IsOldPointerSpaceAllocation() &&
3904 dominator_dominator->IsOldPointerSpaceAllocation()));
3905
3906 int32_t current_size = HConstant::cast(size())->GetInteger32Constant();
3907 HStoreNamedField* dominator_free_space_size =
3908 dominator->filler_free_space_size_;
3909 if (dominator_free_space_size != NULL) {
3910 // We already hoisted one old space allocation, i.e., we already installed
3911 // a filler map. Hence, we just have to update the free space size.
3912 dominator->UpdateFreeSpaceFiller(current_size);
3913 } else {
3914 // This is the first old space allocation that gets hoisted. We have to
3915 // install a filler map since the follwing allocation may cause a GC.
3916 dominator->CreateFreeSpaceFiller(current_size);
3917 }
3918
3919 // We can hoist the old space allocation over the actual dominator.
3920 return dominator_dominator;
3921 }
3922 return dominator;
3923}
3924
3925
3926void HAllocate::UpdateFreeSpaceFiller(int32_t free_space_size) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003927 DCHECK(filler_free_space_size_ != NULL);
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003928 Zone* zone = block()->zone();
3929 // We must explicitly force Smi representation here because on x64 we
3930 // would otherwise automatically choose int32, but the actual store
3931 // requires a Smi-tagged value.
3932 HConstant* new_free_space_size = HConstant::CreateAndInsertBefore(
3933 zone,
3934 context(),
3935 filler_free_space_size_->value()->GetInteger32Constant() +
3936 free_space_size,
3937 Representation::Smi(),
3938 filler_free_space_size_);
3939 filler_free_space_size_->UpdateValue(new_free_space_size);
3940}
3941
3942
3943void HAllocate::CreateFreeSpaceFiller(int32_t free_space_size) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00003944 DCHECK(filler_free_space_size_ == NULL);
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003945 Zone* zone = block()->zone();
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003946 HInstruction* free_space_instr =
3947 HInnerAllocatedObject::New(zone, context(), dominating_allocate_,
machenbach@chromium.orgce9c5142013-12-03 08:00:39 +00003948 dominating_allocate_->size(), type());
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003949 free_space_instr->InsertBefore(this);
machenbach@chromium.orgaf6f6992014-05-06 00:04:47 +00003950 HConstant* filler_map = HConstant::CreateAndInsertAfter(
3951 zone, Unique<Map>::CreateImmovable(
machenbach@chromium.orga3b66332014-05-13 00:04:55 +00003952 isolate()->factory()->free_space_map()), true, free_space_instr);
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003953 HInstruction* store_map = HStoreNamedField::New(zone, context(),
machenbach@chromium.org0a730362014-02-05 03:04:56 +00003954 free_space_instr, HObjectAccess::ForMap(), filler_map);
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003955 store_map->SetFlag(HValue::kHasNoObservableSideEffects);
3956 store_map->InsertAfter(filler_map);
3957
3958 // We must explicitly force Smi representation here because on x64 we
3959 // would otherwise automatically choose int32, but the actual store
3960 // requires a Smi-tagged value.
3961 HConstant* filler_size = HConstant::CreateAndInsertAfter(
3962 zone, context(), free_space_size, Representation::Smi(), store_map);
3963 // Must force Smi representation for x64 (see comment above).
3964 HObjectAccess access =
machenbach@chromium.org0a730362014-02-05 03:04:56 +00003965 HObjectAccess::ForMapAndOffset(isolate()->factory()->free_space_map(),
3966 FreeSpace::kSizeOffset,
3967 Representation::Smi());
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003968 HStoreNamedField* store_size = HStoreNamedField::New(zone, context(),
machenbach@chromium.org0a730362014-02-05 03:04:56 +00003969 free_space_instr, access, filler_size);
hpayer@chromium.orgcc8e1772013-08-27 16:41:54 +00003970 store_size->SetFlag(HValue::kHasNoObservableSideEffects);
3971 store_size->InsertAfter(filler_size);
3972 filler_free_space_size_ = store_size;
3973}
3974
3975
verwaest@chromium.org662436e2013-08-28 08:41:27 +00003976void HAllocate::ClearNextMapWord(int offset) {
ulan@chromium.org9ca30172014-01-02 12:10:54 +00003977 if (MustClearNextMapWord()) {
verwaest@chromium.org662436e2013-08-28 08:41:27 +00003978 Zone* zone = block()->zone();
machenbach@chromium.org0a730362014-02-05 03:04:56 +00003979 HObjectAccess access =
3980 HObjectAccess::ForObservableJSObjectOffset(offset);
verwaest@chromium.org662436e2013-08-28 08:41:27 +00003981 HStoreNamedField* clear_next_map =
3982 HStoreNamedField::New(zone, context(), this, access,
machenbach@chromium.org0a730362014-02-05 03:04:56 +00003983 block()->graph()->GetConstant0());
verwaest@chromium.org662436e2013-08-28 08:41:27 +00003984 clear_next_map->ClearAllSideEffects();
3985 clear_next_map->InsertAfter(this);
3986 }
3987}
3988
3989
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00003990OStream& HAllocate::PrintDataTo(OStream& os) const { // NOLINT
3991 os << NameOf(size()) << " (";
3992 if (IsNewSpaceAllocation()) os << "N";
3993 if (IsOldPointerSpaceAllocation()) os << "P";
3994 if (IsOldDataSpaceAllocation()) os << "D";
3995 if (MustAllocateDoubleAligned()) os << "A";
3996 if (MustPrefillWithFiller()) os << "F";
3997 return os << ")";
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00003998}
3999
4000
machenbach@chromium.org975b9402014-06-24 00:06:56 +00004001bool HStoreKeyed::TryIncreaseBaseOffset(uint32_t increase_by_value) {
4002 // The base offset is usually simply the size of the array header, except
4003 // with dehoisting adds an addition offset due to a array index key
4004 // manipulation, in which case it becomes (array header size +
4005 // constant-offset-from-key * kPointerSize)
4006 v8::base::internal::CheckedNumeric<uint32_t> addition_result = base_offset_;
4007 addition_result += increase_by_value;
4008 if (!addition_result.IsValid()) return false;
4009 base_offset_ = addition_result.ValueOrDie();
4010 return true;
4011}
4012
4013
verwaest@chromium.orge4ee6de2012-11-06 12:13:00 +00004014bool HStoreKeyed::NeedsCanonicalization() {
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00004015 // If value is an integer or smi or comes from the result of a keyed load or
4016 // constant then it is either be a non-hole value or in the case of a constant
4017 // the hole is only being stored explicitly: no need for canonicalization.
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00004018 //
4019 // The exception to that is keyed loads from external float or double arrays:
4020 // these can load arbitrary representation of NaN.
4021
4022 if (value()->IsConstant()) {
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004023 return false;
4024 }
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00004025
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +00004026 if (value()->IsLoadKeyed()) {
4027 return IsExternalFloatOrDoubleElementsKind(
4028 HLoadKeyed::cast(value())->elements_kind());
4029 }
4030
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00004031 if (value()->IsChange()) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004032 if (HChange::cast(value())->from().IsSmiOrInteger32()) {
danno@chromium.org94b0d6f2013-02-04 13:33:20 +00004033 return false;
4034 }
4035 if (HChange::cast(value())->value()->type().IsSmi()) {
4036 return false;
4037 }
4038 }
jkummerow@chromium.org28faa982012-04-13 09:58:30 +00004039 return true;
4040}
4041
4042
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004043#define H_CONSTANT_INT(val) \
4044HConstant::New(zone, context, static_cast<int32_t>(val))
erikcorry0ad885c2011-11-21 13:51:57 +00004045#define H_CONSTANT_DOUBLE(val) \
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004046HConstant::New(zone, context, static_cast<double>(val))
erikcorry0ad885c2011-11-21 13:51:57 +00004047
4048#define DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HInstr, op) \
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004049HInstruction* HInstr::New( \
4050 Zone* zone, HValue* context, HValue* left, HValue* right) { \
4051 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) { \
erikcorry0ad885c2011-11-21 13:51:57 +00004052 HConstant* c_left = HConstant::cast(left); \
4053 HConstant* c_right = HConstant::cast(right); \
4054 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) { \
4055 double double_res = c_left->DoubleValue() op c_right->DoubleValue(); \
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +00004056 if (IsInt32Double(double_res)) { \
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004057 return H_CONSTANT_INT(double_res); \
erikcorry0ad885c2011-11-21 13:51:57 +00004058 } \
4059 return H_CONSTANT_DOUBLE(double_res); \
4060 } \
4061 } \
4062 return new(zone) HInstr(context, left, right); \
4063}
4064
4065
4066DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HAdd, +)
4067DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HMul, *)
4068DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR(HSub, -)
4069
4070#undef DEFINE_NEW_H_SIMPLE_ARITHMETIC_INSTR
4071
4072
jkummerow@chromium.orgba72ec82013-07-22 09:21:20 +00004073HInstruction* HStringAdd::New(Zone* zone,
4074 HValue* context,
4075 HValue* left,
4076 HValue* right,
ulan@chromium.org0f13e742014-01-03 15:51:11 +00004077 PretenureFlag pretenure_flag,
4078 StringAddFlags flags,
4079 Handle<AllocationSite> allocation_site) {
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004080 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4081 HConstant* c_right = HConstant::cast(right);
4082 HConstant* c_left = HConstant::cast(left);
4083 if (c_left->HasStringValue() && c_right->HasStringValue()) {
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +00004084 Handle<String> left_string = c_left->StringValue();
4085 Handle<String> right_string = c_right->StringValue();
4086 // Prevent possible exception by invalid string length.
4087 if (left_string->length() + right_string->length() < String::kMaxLength) {
machenbach@chromium.orgd6472082014-07-16 00:04:33 +00004088 MaybeHandle<String> concat = zone->isolate()->factory()->NewConsString(
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +00004089 c_left->StringValue(), c_right->StringValue());
machenbach@chromium.orgd6472082014-07-16 00:04:33 +00004090 return HConstant::New(zone, context, concat.ToHandleChecked());
machenbach@chromium.orgb5ed9302014-03-25 13:44:35 +00004091 }
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004092 }
4093 }
ulan@chromium.org0f13e742014-01-03 15:51:11 +00004094 return new(zone) HStringAdd(
4095 context, left, right, pretenure_flag, flags, allocation_site);
4096}
4097
4098
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004099OStream& HStringAdd::PrintDataTo(OStream& os) const { // NOLINT
ulan@chromium.org0f13e742014-01-03 15:51:11 +00004100 if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_BOTH) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004101 os << "_CheckBoth";
ulan@chromium.org0f13e742014-01-03 15:51:11 +00004102 } else if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_LEFT) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004103 os << "_CheckLeft";
ulan@chromium.org0f13e742014-01-03 15:51:11 +00004104 } else if ((flags() & STRING_ADD_CHECK_BOTH) == STRING_ADD_CHECK_RIGHT) {
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004105 os << "_CheckRight";
ulan@chromium.org0f13e742014-01-03 15:51:11 +00004106 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004107 HBinaryOperation::PrintDataTo(os);
4108 os << " (";
4109 if (pretenure_flag() == NOT_TENURED)
4110 os << "N";
4111 else if (pretenure_flag() == TENURED)
4112 os << "D";
4113 return os << ")";
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004114}
4115
4116
4117HInstruction* HStringCharFromCode::New(
4118 Zone* zone, HValue* context, HValue* char_code) {
4119 if (FLAG_fold_constants && char_code->IsConstant()) {
4120 HConstant* c_code = HConstant::cast(char_code);
jkummerow@chromium.org3d00d0a2013-09-04 13:57:32 +00004121 Isolate* isolate = zone->isolate();
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004122 if (c_code->HasNumberValue()) {
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00004123 if (std::isfinite(c_code->DoubleValue())) {
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004124 uint32_t code = c_code->NumberValueAsInteger32() & 0xffff;
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004125 return HConstant::New(zone, context,
machenbach@chromium.org9e41f9e2014-04-09 00:06:04 +00004126 isolate->factory()->LookupSingleCharacterStringFromCode(code));
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004127 }
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004128 return HConstant::New(zone, context, isolate->factory()->empty_string());
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004129 }
4130 }
4131 return new(zone) HStringCharFromCode(context, char_code);
4132}
4133
4134
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004135HInstruction* HUnaryMathOperation::New(
4136 Zone* zone, HValue* context, HValue* value, BuiltinFunctionId op) {
4137 do {
4138 if (!FLAG_fold_constants) break;
4139 if (!value->IsConstant()) break;
4140 HConstant* constant = HConstant::cast(value);
4141 if (!constant->HasNumberValue()) break;
4142 double d = constant->DoubleValue();
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00004143 if (std::isnan(d)) { // NaN poisons everything.
yangguo@chromium.org5de00742014-07-01 11:58:10 +00004144 return H_CONSTANT_DOUBLE(base::OS::nan_value());
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004145 }
ulan@chromium.org77ca49a2013-04-22 09:43:56 +00004146 if (std::isinf(d)) { // +Infinity and -Infinity.
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004147 switch (op) {
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004148 case kMathExp:
4149 return H_CONSTANT_DOUBLE((d > 0.0) ? d : 0.0);
4150 case kMathLog:
4151 case kMathSqrt:
yangguo@chromium.org5de00742014-07-01 11:58:10 +00004152 return H_CONSTANT_DOUBLE((d > 0.0) ? d : base::OS::nan_value());
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004153 case kMathPowHalf:
4154 case kMathAbs:
4155 return H_CONSTANT_DOUBLE((d > 0.0) ? d : -d);
4156 case kMathRound:
machenbach@chromium.orgdc207d92014-07-30 00:05:07 +00004157 case kMathFround:
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004158 case kMathFloor:
4159 return H_CONSTANT_DOUBLE(d);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004160 case kMathClz32:
4161 return H_CONSTANT_INT(32);
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004162 default:
4163 UNREACHABLE();
4164 break;
4165 }
4166 }
4167 switch (op) {
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004168 case kMathExp:
4169 return H_CONSTANT_DOUBLE(fast_exp(d));
4170 case kMathLog:
machenbach@chromium.orge31286d2014-01-15 10:29:52 +00004171 return H_CONSTANT_DOUBLE(std::log(d));
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004172 case kMathSqrt:
4173 return H_CONSTANT_DOUBLE(fast_sqrt(d));
4174 case kMathPowHalf:
4175 return H_CONSTANT_DOUBLE(power_double_double(d, 0.5));
4176 case kMathAbs:
4177 return H_CONSTANT_DOUBLE((d >= 0.0) ? d + 0.0 : -d);
4178 case kMathRound:
4179 // -0.5 .. -0.0 round to -0.0.
4180 if ((d >= -0.5 && Double(d).Sign() < 0)) return H_CONSTANT_DOUBLE(-0.0);
4181 // Doubles are represented as Significant * 2 ^ Exponent. If the
4182 // Exponent is not negative, the double value is already an integer.
4183 if (Double(d).Exponent() >= 0) return H_CONSTANT_DOUBLE(d);
machenbach@chromium.orgdc207d92014-07-30 00:05:07 +00004184 return H_CONSTANT_DOUBLE(Floor(d + 0.5));
4185 case kMathFround:
4186 return H_CONSTANT_DOUBLE(static_cast<double>(static_cast<float>(d)));
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004187 case kMathFloor:
machenbach@chromium.orgdc207d92014-07-30 00:05:07 +00004188 return H_CONSTANT_DOUBLE(Floor(d));
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004189 case kMathClz32: {
4190 uint32_t i = DoubleToUint32(d);
machenbach@chromium.orge2a89372014-08-21 07:23:04 +00004191 return H_CONSTANT_INT(base::bits::CountLeadingZeros32(i));
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004192 }
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004193 default:
4194 UNREACHABLE();
4195 break;
4196 }
4197 } while (false);
4198 return new(zone) HUnaryMathOperation(context, value, op);
4199}
4200
4201
machenbach@chromium.orga86d4162014-05-01 00:05:11 +00004202Representation HUnaryMathOperation::RepresentationFromUses() {
4203 if (op_ != kMathFloor && op_ != kMathRound) {
4204 return HValue::RepresentationFromUses();
4205 }
4206
4207 // The instruction can have an int32 or double output. Prefer a double
4208 // representation if there are double uses.
4209 bool use_double = false;
4210
4211 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4212 HValue* use = it.value();
4213 int use_index = it.index();
4214 Representation rep_observed = use->observed_input_representation(use_index);
4215 Representation rep_required = use->RequiredInputRepresentation(use_index);
4216 use_double |= (rep_observed.IsDouble() || rep_required.IsDouble());
4217 if (use_double && !FLAG_trace_representation) {
4218 // Having seen one double is enough.
4219 break;
4220 }
4221 if (FLAG_trace_representation) {
4222 if (!rep_required.IsDouble() || rep_observed.IsDouble()) {
4223 PrintF("#%d %s is used by #%d %s as %s%s\n",
4224 id(), Mnemonic(), use->id(),
4225 use->Mnemonic(), rep_observed.Mnemonic(),
4226 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
4227 } else {
4228 PrintF("#%d %s is required by #%d %s as %s%s\n",
4229 id(), Mnemonic(), use->id(),
4230 use->Mnemonic(), rep_required.Mnemonic(),
4231 (use->CheckFlag(kTruncatingToInt32) ? "-trunc" : ""));
4232 }
4233 }
4234 }
4235 return use_double ? Representation::Double() : Representation::Integer32();
4236}
4237
4238
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004239HInstruction* HPower::New(Zone* zone,
4240 HValue* context,
4241 HValue* left,
4242 HValue* right) {
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004243 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4244 HConstant* c_left = HConstant::cast(left);
4245 HConstant* c_right = HConstant::cast(right);
4246 if (c_left->HasNumberValue() && c_right->HasNumberValue()) {
4247 double result = power_helper(c_left->DoubleValue(),
4248 c_right->DoubleValue());
yangguo@chromium.org5de00742014-07-01 11:58:10 +00004249 return H_CONSTANT_DOUBLE(std::isnan(result) ? base::OS::nan_value()
4250 : result);
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004251 }
4252 }
4253 return new(zone) HPower(left, right);
4254}
4255
4256
4257HInstruction* HMathMinMax::New(
4258 Zone* zone, HValue* context, HValue* left, HValue* right, Operation op) {
4259 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
4260 HConstant* c_left = HConstant::cast(left);
4261 HConstant* c_right = HConstant::cast(right);
4262 if (c_left->HasNumberValue() && c_right->HasNumberValue()) {
4263 double d_left = c_left->DoubleValue();
4264 double d_right = c_right->DoubleValue();
4265 if (op == kMathMin) {
4266 if (d_left > d_right) return H_CONSTANT_DOUBLE(d_right);
4267 if (d_left < d_right) return H_CONSTANT_DOUBLE(d_left);
4268 if (d_left == d_right) {
4269 // Handle +0 and -0.
4270 return H_CONSTANT_DOUBLE((Double(d_left).Sign() == -1) ? d_left
4271 : d_right);
4272 }
4273 } else {
4274 if (d_left < d_right) return H_CONSTANT_DOUBLE(d_right);
4275 if (d_left > d_right) return H_CONSTANT_DOUBLE(d_left);
4276 if (d_left == d_right) {
4277 // Handle +0 and -0.
4278 return H_CONSTANT_DOUBLE((Double(d_left).Sign() == -1) ? d_right
4279 : d_left);
4280 }
4281 }
4282 // All comparisons failed, must be NaN.
yangguo@chromium.org5de00742014-07-01 11:58:10 +00004283 return H_CONSTANT_DOUBLE(base::OS::nan_value());
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004284 }
4285 }
4286 return new(zone) HMathMinMax(context, left, right, op);
4287}
4288
4289
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00004290HInstruction* HMod::New(Zone* zone,
4291 HValue* context,
4292 HValue* left,
machenbach@chromium.org7ff76072013-11-21 09:47:43 +00004293 HValue* right) {
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004294 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
erikcorry0ad885c2011-11-21 13:51:57 +00004295 HConstant* c_left = HConstant::cast(left);
4296 HConstant* c_right = HConstant::cast(right);
4297 if (c_left->HasInteger32Value() && c_right->HasInteger32Value()) {
4298 int32_t dividend = c_left->Integer32Value();
4299 int32_t divisor = c_right->Integer32Value();
ulan@chromium.org906e2fb2013-05-14 08:14:38 +00004300 if (dividend == kMinInt && divisor == -1) {
4301 return H_CONSTANT_DOUBLE(-0.0);
4302 }
erikcorry0ad885c2011-11-21 13:51:57 +00004303 if (divisor != 0) {
4304 int32_t res = dividend % divisor;
4305 if ((res == 0) && (dividend < 0)) {
4306 return H_CONSTANT_DOUBLE(-0.0);
4307 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004308 return H_CONSTANT_INT(res);
erikcorry0ad885c2011-11-21 13:51:57 +00004309 }
4310 }
4311 }
machenbach@chromium.org7ff76072013-11-21 09:47:43 +00004312 return new(zone) HMod(context, left, right);
erikcorry0ad885c2011-11-21 13:51:57 +00004313}
4314
4315
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004316HInstruction* HDiv::New(
4317 Zone* zone, HValue* context, HValue* left, HValue* right) {
erikcorry0ad885c2011-11-21 13:51:57 +00004318 // If left and right are constant values, try to return a constant value.
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004319 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
erikcorry0ad885c2011-11-21 13:51:57 +00004320 HConstant* c_left = HConstant::cast(left);
4321 HConstant* c_right = HConstant::cast(right);
4322 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4323 if (c_right->DoubleValue() != 0) {
4324 double double_res = c_left->DoubleValue() / c_right->DoubleValue();
hpayer@chromium.orgea9b8ba2013-12-20 19:22:39 +00004325 if (IsInt32Double(double_res)) {
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004326 return H_CONSTANT_INT(double_res);
erikcorry0ad885c2011-11-21 13:51:57 +00004327 }
4328 return H_CONSTANT_DOUBLE(double_res);
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004329 } else {
4330 int sign = Double(c_left->DoubleValue()).Sign() *
4331 Double(c_right->DoubleValue()).Sign(); // Right could be -0.
4332 return H_CONSTANT_DOUBLE(sign * V8_INFINITY);
erikcorry0ad885c2011-11-21 13:51:57 +00004333 }
4334 }
4335 }
4336 return new(zone) HDiv(context, left, right);
4337}
4338
4339
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004340HInstruction* HBitwise::New(
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004341 Zone* zone, HValue* context, Token::Value op, HValue* left, HValue* right) {
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004342 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
erikcorry0ad885c2011-11-21 13:51:57 +00004343 HConstant* c_left = HConstant::cast(left);
4344 HConstant* c_right = HConstant::cast(right);
4345 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4346 int32_t result;
4347 int32_t v_left = c_left->NumberValueAsInteger32();
4348 int32_t v_right = c_right->NumberValueAsInteger32();
4349 switch (op) {
4350 case Token::BIT_XOR:
4351 result = v_left ^ v_right;
4352 break;
4353 case Token::BIT_AND:
4354 result = v_left & v_right;
4355 break;
4356 case Token::BIT_OR:
4357 result = v_left | v_right;
4358 break;
4359 default:
4360 result = 0; // Please the compiler.
4361 UNREACHABLE();
4362 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004363 return H_CONSTANT_INT(result);
erikcorry0ad885c2011-11-21 13:51:57 +00004364 }
4365 }
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004366 return new(zone) HBitwise(context, op, left, right);
erikcorry0ad885c2011-11-21 13:51:57 +00004367}
4368
4369
4370#define DEFINE_NEW_H_BITWISE_INSTR(HInstr, result) \
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004371HInstruction* HInstr::New( \
4372 Zone* zone, HValue* context, HValue* left, HValue* right) { \
4373 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) { \
erikcorry0ad885c2011-11-21 13:51:57 +00004374 HConstant* c_left = HConstant::cast(left); \
4375 HConstant* c_right = HConstant::cast(right); \
4376 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) { \
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004377 return H_CONSTANT_INT(result); \
erikcorry0ad885c2011-11-21 13:51:57 +00004378 } \
4379 } \
4380 return new(zone) HInstr(context, left, right); \
4381}
4382
4383
4384DEFINE_NEW_H_BITWISE_INSTR(HSar,
4385c_left->NumberValueAsInteger32() >> (c_right->NumberValueAsInteger32() & 0x1f))
4386DEFINE_NEW_H_BITWISE_INSTR(HShl,
4387c_left->NumberValueAsInteger32() << (c_right->NumberValueAsInteger32() & 0x1f))
4388
4389#undef DEFINE_NEW_H_BITWISE_INSTR
4390
4391
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004392HInstruction* HShr::New(
4393 Zone* zone, HValue* context, HValue* left, HValue* right) {
4394 if (FLAG_fold_constants && left->IsConstant() && right->IsConstant()) {
erikcorry0ad885c2011-11-21 13:51:57 +00004395 HConstant* c_left = HConstant::cast(left);
4396 HConstant* c_right = HConstant::cast(right);
4397 if ((c_left->HasNumberValue() && c_right->HasNumberValue())) {
4398 int32_t left_val = c_left->NumberValueAsInteger32();
4399 int32_t right_val = c_right->NumberValueAsInteger32() & 0x1f;
4400 if ((right_val == 0) && (left_val < 0)) {
ulan@chromium.org2e04b582013-02-21 14:06:02 +00004401 return H_CONSTANT_DOUBLE(static_cast<uint32_t>(left_val));
erikcorry0ad885c2011-11-21 13:51:57 +00004402 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004403 return H_CONSTANT_INT(static_cast<uint32_t>(left_val) >> right_val);
erikcorry0ad885c2011-11-21 13:51:57 +00004404 }
4405 }
4406 return new(zone) HShr(context, left, right);
4407}
4408
4409
machenbach@chromium.orge8412be2013-11-08 10:23:52 +00004410HInstruction* HSeqStringGetChar::New(Zone* zone,
4411 HValue* context,
4412 String::Encoding encoding,
4413 HValue* string,
4414 HValue* index) {
4415 if (FLAG_fold_constants && string->IsConstant() && index->IsConstant()) {
4416 HConstant* c_string = HConstant::cast(string);
4417 HConstant* c_index = HConstant::cast(index);
4418 if (c_string->HasStringValue() && c_index->HasInteger32Value()) {
4419 Handle<String> s = c_string->StringValue();
4420 int32_t i = c_index->Integer32Value();
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004421 DCHECK_LE(0, i);
4422 DCHECK_LT(i, s->length());
machenbach@chromium.orge8412be2013-11-08 10:23:52 +00004423 return H_CONSTANT_INT(s->Get(i));
4424 }
4425 }
4426 return new(zone) HSeqStringGetChar(encoding, string, index);
4427}
4428
4429
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004430#undef H_CONSTANT_INT
erikcorry0ad885c2011-11-21 13:51:57 +00004431#undef H_CONSTANT_DOUBLE
4432
4433
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004434OStream& HBitwise::PrintDataTo(OStream& os) const { // NOLINT
4435 os << Token::Name(op_) << " ";
4436 return HBitwiseBinaryOperation::PrintDataTo(os);
erik.corry@gmail.comed49e962012-04-17 11:57:53 +00004437}
4438
4439
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004440void HPhi::SimplifyConstantInputs() {
4441 // Convert constant inputs to integers when all uses are truncating.
4442 // This must happen before representation inference takes place.
4443 if (!CheckUsesForFlag(kTruncatingToInt32)) return;
4444 for (int i = 0; i < OperandCount(); ++i) {
4445 if (!OperandAt(i)->IsConstant()) return;
4446 }
4447 HGraph* graph = block()->graph();
4448 for (int i = 0; i < OperandCount(); ++i) {
4449 HConstant* operand = HConstant::cast(OperandAt(i));
4450 if (operand->HasInteger32Value()) {
4451 continue;
4452 } else if (operand->HasDoubleValue()) {
4453 HConstant* integer_input =
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004454 HConstant::New(graph->zone(), graph->GetInvalidContext(),
4455 DoubleToInt32(operand->DoubleValue()));
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004456 integer_input->InsertAfter(operand);
4457 SetOperandAt(i, integer_input);
danno@chromium.org59400602013-08-13 17:09:37 +00004458 } else if (operand->HasBooleanValue()) {
4459 SetOperandAt(i, operand->BooleanValue() ? graph->GetConstant1()
4460 : graph->GetConstant0());
4461 } else if (operand->ImmortalImmovable()) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004462 SetOperandAt(i, graph->GetConstant0());
4463 }
4464 }
4465 // Overwrite observed input representations because they are likely Tagged.
4466 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4467 HValue* use = it.value();
4468 if (use->IsBinaryOperation()) {
4469 HBinaryOperation::cast(use)->set_observed_input_representation(
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004470 it.index(), Representation::Smi());
danno@chromium.orgca29dd82013-04-26 11:59:48 +00004471 }
4472 }
4473}
4474
4475
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00004476void HPhi::InferRepresentation(HInferRepresentationPhase* h_infer) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004477 DCHECK(CheckFlag(kFlexibleRepresentation));
ulan@chromium.org57ff8812013-05-10 08:16:55 +00004478 Representation new_rep = RepresentationFromInputs();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004479 UpdateRepresentation(new_rep, h_infer, "inputs");
4480 new_rep = RepresentationFromUses();
4481 UpdateRepresentation(new_rep, h_infer, "uses");
4482 new_rep = RepresentationFromUseRequirements();
4483 UpdateRepresentation(new_rep, h_infer, "use requirements");
4484}
4485
4486
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004487Representation HPhi::RepresentationFromInputs() {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00004488 Representation r = Representation::None();
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00004489 for (int i = 0; i < OperandCount(); ++i) {
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00004490 r = r.generalize(OperandAt(i)->KnownOptimalRepresentation());
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00004491 }
verwaest@chromium.orgd4be0f02013-06-05 13:39:03 +00004492 return r;
rossberg@chromium.org2c067b12012-03-19 11:01:52 +00004493}
4494
4495
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00004496// Returns a representation if all uses agree on the same representation.
4497// Integer32 is also returned when some uses are Smi but others are Integer32.
4498Representation HValue::RepresentationFromUseRequirements() {
4499 Representation rep = Representation::None();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004500 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00004501 // Ignore the use requirement from never run code
mvstanton@chromium.orgdd6d9ee2013-10-11 10:35:37 +00004502 if (it.value()->block()->IsUnreachable()) continue;
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +00004503
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004504 // We check for observed_input_representation elsewhere.
4505 Representation use_rep =
4506 it.value()->RequiredInputRepresentation(it.index());
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00004507 if (rep.IsNone()) {
4508 rep = use_rep;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004509 continue;
4510 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00004511 if (use_rep.IsNone() || rep.Equals(use_rep)) continue;
4512 if (rep.generalize(use_rep).IsInteger32()) {
4513 rep = Representation::Integer32();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004514 continue;
4515 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00004516 return Representation::None();
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004517 }
dslomov@chromium.orgb752d402013-06-18 11:54:54 +00004518 return rep;
yangguo@chromium.orgfb377212012-11-16 14:43:43 +00004519}
4520
4521
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004522bool HValue::HasNonSmiUse() {
4523 for (HUseIterator it(uses()); !it.Done(); it.Advance()) {
4524 // We check for observed_input_representation elsewhere.
4525 Representation use_rep =
4526 it.value()->RequiredInputRepresentation(it.index());
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004527 if (!use_rep.IsNone() &&
4528 !use_rep.IsSmi() &&
4529 !use_rep.IsTagged()) {
4530 return true;
4531 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004532 }
4533 return false;
4534}
4535
4536
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004537// Node-specific verification code is only included in debug mode.
4538#ifdef DEBUG
4539
ager@chromium.org378b34e2011-01-28 08:04:38 +00004540void HPhi::Verify() {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004541 DCHECK(OperandCount() == block()->predecessors()->length());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004542 for (int i = 0; i < OperandCount(); ++i) {
4543 HValue* value = OperandAt(i);
4544 HBasicBlock* defining_block = value->block();
4545 HBasicBlock* predecessor_block = block()->predecessors()->at(i);
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004546 DCHECK(defining_block == predecessor_block ||
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004547 defining_block->Dominates(predecessor_block));
4548 }
4549}
4550
4551
ager@chromium.org378b34e2011-01-28 08:04:38 +00004552void HSimulate::Verify() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004553 HInstruction::Verify();
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004554 DCHECK(HasAstId() || next()->IsEnterInlined());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004555}
4556
4557
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +00004558void HCheckHeapObject::Verify() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004559 HInstruction::Verify();
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004560 DCHECK(HasNoUses());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004561}
4562
4563
mstarzinger@chromium.org1f410f92013-08-29 08:13:16 +00004564void HCheckValue::Verify() {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004565 HInstruction::Verify();
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004566 DCHECK(HasNoUses());
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004567}
4568
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004569#endif
4570
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004571
4572HObjectAccess HObjectAccess::ForFixedArrayHeader(int offset) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004573 DCHECK(offset >= 0);
4574 DCHECK(offset < FixedArray::kHeaderSize);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004575 if (offset == FixedArray::kLengthOffset) return ForFixedArrayLength();
4576 return HObjectAccess(kInobject, offset);
4577}
4578
4579
machenbach@chromium.org0a730362014-02-05 03:04:56 +00004580HObjectAccess HObjectAccess::ForMapAndOffset(Handle<Map> map, int offset,
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004581 Representation representation) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004582 DCHECK(offset >= 0);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004583 Portion portion = kInobject;
4584
4585 if (offset == JSObject::kElementsOffset) {
4586 portion = kElementsPointer;
4587 } else if (offset == JSObject::kMapOffset) {
4588 portion = kMaps;
4589 }
machenbach@chromium.org0a730362014-02-05 03:04:56 +00004590 bool existing_inobject_property = true;
4591 if (!map.is_null()) {
4592 existing_inobject_property = (offset <
4593 map->instance_size() - map->unused_property_fields() * kPointerSize);
4594 }
4595 return HObjectAccess(portion, offset, representation, Handle<String>::null(),
4596 false, existing_inobject_property);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004597}
4598
4599
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00004600HObjectAccess HObjectAccess::ForAllocationSiteOffset(int offset) {
4601 switch (offset) {
4602 case AllocationSite::kTransitionInfoOffset:
4603 return HObjectAccess(kInobject, offset, Representation::Tagged());
4604 case AllocationSite::kNestedSiteOffset:
4605 return HObjectAccess(kInobject, offset, Representation::Tagged());
machenbach@chromium.org4ddd2f12014-01-14 08:13:44 +00004606 case AllocationSite::kPretenureDataOffset:
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00004607 return HObjectAccess(kInobject, offset, Representation::Smi());
machenbach@chromium.org4ddd2f12014-01-14 08:13:44 +00004608 case AllocationSite::kPretenureCreateCountOffset:
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00004609 return HObjectAccess(kInobject, offset, Representation::Smi());
4610 case AllocationSite::kDependentCodeOffset:
4611 return HObjectAccess(kInobject, offset, Representation::Tagged());
4612 case AllocationSite::kWeakNextOffset:
4613 return HObjectAccess(kInobject, offset, Representation::Tagged());
4614 default:
4615 UNREACHABLE();
4616 }
4617 return HObjectAccess(kInobject, offset);
4618}
4619
4620
verwaest@chromium.org662436e2013-08-28 08:41:27 +00004621HObjectAccess HObjectAccess::ForContextSlot(int index) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004622 DCHECK(index >= 0);
verwaest@chromium.org662436e2013-08-28 08:41:27 +00004623 Portion portion = kInobject;
4624 int offset = Context::kHeaderSize + index * kPointerSize;
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004625 DCHECK_EQ(offset, Context::SlotOffset(index) + kHeapObjectTag);
verwaest@chromium.org662436e2013-08-28 08:41:27 +00004626 return HObjectAccess(portion, offset, Representation::Tagged());
4627}
4628
4629
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004630HObjectAccess HObjectAccess::ForJSArrayOffset(int offset) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004631 DCHECK(offset >= 0);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004632 Portion portion = kInobject;
4633
4634 if (offset == JSObject::kElementsOffset) {
4635 portion = kElementsPointer;
4636 } else if (offset == JSArray::kLengthOffset) {
4637 portion = kArrayLengths;
4638 } else if (offset == JSObject::kMapOffset) {
4639 portion = kMaps;
4640 }
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004641 return HObjectAccess(portion, offset);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004642}
4643
4644
jkummerow@chromium.orgfb732b12013-07-26 10:27:09 +00004645HObjectAccess HObjectAccess::ForBackingStoreOffset(int offset,
4646 Representation representation) {
machenbach@chromium.orge3c177a2014-08-05 00:05:55 +00004647 DCHECK(offset >= 0);
machenbach@chromium.org0a730362014-02-05 03:04:56 +00004648 return HObjectAccess(kBackingStore, offset, representation,
4649 Handle<String>::null(), false, false);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004650}
4651
4652
machenbach@chromium.orge2a89372014-08-21 07:23:04 +00004653HObjectAccess HObjectAccess::ForField(Handle<Map> map, int index,
4654 Representation representation,
machenbach@chromium.org0a730362014-02-05 03:04:56 +00004655 Handle<String> name) {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004656 if (index < 0) {
4657 // Negative property indices are in-object properties, indexed
4658 // from the end of the fixed part of the object.
4659 int offset = (index * kPointerSize) + map->instance_size();
machenbach@chromium.org0a730362014-02-05 03:04:56 +00004660 return HObjectAccess(kInobject, offset, representation, name, false, true);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004661 } else {
4662 // Non-negative property indices are in the properties array.
4663 int offset = (index * kPointerSize) + FixedArray::kHeaderSize;
machenbach@chromium.org0a730362014-02-05 03:04:56 +00004664 return HObjectAccess(kBackingStore, offset, representation, name,
4665 false, false);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004666 }
4667}
4668
4669
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00004670HObjectAccess HObjectAccess::ForCellPayload(Isolate* isolate) {
machenbach@chromium.org5e570592014-08-20 00:06:26 +00004671 return HObjectAccess(kInobject, Cell::kValueOffset, Representation::Tagged(),
4672 isolate->factory()->cell_value_string());
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +00004673}
4674
4675
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004676void HObjectAccess::SetGVNFlags(HValue *instr, PropertyAccessType access_type) {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004677 // set the appropriate GVN flags for a given load or store instruction
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004678 if (access_type == STORE) {
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004679 // track dominating allocations in order to eliminate write barriers
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004680 instr->SetDependsOnFlag(::v8::internal::kNewSpacePromotion);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004681 instr->SetFlag(HValue::kTrackSideEffectDominators);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004682 } else {
4683 // try to GVN loads, but don't hoist above map changes
4684 instr->SetFlag(HValue::kUseGVN);
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004685 instr->SetDependsOnFlag(::v8::internal::kMaps);
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004686 }
4687
4688 switch (portion()) {
4689 case kArrayLengths:
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004690 if (access_type == STORE) {
4691 instr->SetChangesFlag(::v8::internal::kArrayLengths);
4692 } else {
4693 instr->SetDependsOnFlag(::v8::internal::kArrayLengths);
4694 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004695 break;
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004696 case kStringLengths:
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004697 if (access_type == STORE) {
4698 instr->SetChangesFlag(::v8::internal::kStringLengths);
4699 } else {
4700 instr->SetDependsOnFlag(::v8::internal::kStringLengths);
4701 }
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004702 break;
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004703 case kInobject:
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004704 if (access_type == STORE) {
4705 instr->SetChangesFlag(::v8::internal::kInobjectFields);
4706 } else {
4707 instr->SetDependsOnFlag(::v8::internal::kInobjectFields);
4708 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004709 break;
4710 case kDouble:
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004711 if (access_type == STORE) {
4712 instr->SetChangesFlag(::v8::internal::kDoubleFields);
4713 } else {
4714 instr->SetDependsOnFlag(::v8::internal::kDoubleFields);
4715 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004716 break;
4717 case kBackingStore:
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004718 if (access_type == STORE) {
4719 instr->SetChangesFlag(::v8::internal::kBackingStoreFields);
4720 } else {
4721 instr->SetDependsOnFlag(::v8::internal::kBackingStoreFields);
4722 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004723 break;
4724 case kElementsPointer:
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004725 if (access_type == STORE) {
4726 instr->SetChangesFlag(::v8::internal::kElementsPointer);
4727 } else {
4728 instr->SetDependsOnFlag(::v8::internal::kElementsPointer);
4729 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004730 break;
4731 case kMaps:
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004732 if (access_type == STORE) {
4733 instr->SetChangesFlag(::v8::internal::kMaps);
4734 } else {
4735 instr->SetDependsOnFlag(::v8::internal::kMaps);
4736 }
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004737 break;
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004738 case kExternalMemory:
titzer@chromium.orgf5a24542014-03-04 09:06:17 +00004739 if (access_type == STORE) {
4740 instr->SetChangesFlag(::v8::internal::kExternalMemory);
4741 } else {
4742 instr->SetDependsOnFlag(::v8::internal::kExternalMemory);
4743 }
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004744 break;
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004745 }
4746}
4747
4748
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004749OStream& operator<<(OStream& os, const HObjectAccess& access) {
4750 os << ".";
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004751
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004752 switch (access.portion()) {
4753 case HObjectAccess::kArrayLengths:
4754 case HObjectAccess::kStringLengths:
4755 os << "%length";
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004756 break;
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004757 case HObjectAccess::kElementsPointer:
4758 os << "%elements";
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004759 break;
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004760 case HObjectAccess::kMaps:
4761 os << "%map";
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004762 break;
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004763 case HObjectAccess::kDouble: // fall through
4764 case HObjectAccess::kInobject:
4765 if (!access.name().is_null()) {
4766 os << Handle<String>::cast(access.name())->ToCString().get();
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00004767 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004768 os << "[in-object]";
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004769 break;
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004770 case HObjectAccess::kBackingStore:
4771 if (!access.name().is_null()) {
4772 os << Handle<String>::cast(access.name())->ToCString().get();
machenbach@chromium.orgafbdadc2013-12-09 16:12:18 +00004773 }
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004774 os << "[backing-store]";
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004775 break;
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004776 case HObjectAccess::kExternalMemory:
4777 os << "[external-memory]";
danno@chromium.orgd3c42102013-08-01 16:58:23 +00004778 break;
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004779 }
4780
machenbach@chromium.orgf15d0cd2014-07-08 00:05:42 +00004781 return os << "@" << access.offset();
svenpanne@chromium.orga53e8e02013-05-24 12:35:50 +00004782}
4783
kasperl@chromium.orga5551262010-12-07 12:49:48 +00004784} } // namespace v8::internal