Skip to content

Commit c0e374d

Browse files
committed
Ensure consistency of representations when replacing integer ops with constants
during canonicalization. BUG=dartbug.com/25335 [email protected] Review URL: https://codereview.chromium.org/1557923003 .
1 parent 634e5a1 commit c0e374d

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

runtime/vm/intermediate_language.cc

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1735,6 +1735,19 @@ RawInteger* BinaryIntegerOpInstr::Evaluate(const Integer& left,
17351735
}
17361736

17371737

1738+
Definition* BinaryIntegerOpInstr::CreateConstantResult(FlowGraph* flow_graph,
1739+
const Integer& result) {
1740+
Definition* result_defn = flow_graph->GetConstant(result);
1741+
if (representation() != kTagged) {
1742+
result_defn = UnboxInstr::Create(representation(),
1743+
new Value(result_defn),
1744+
GetDeoptId());
1745+
flow_graph->InsertBefore(this, result_defn, env(), FlowGraph::kValue);
1746+
}
1747+
return result_defn;
1748+
}
1749+
1750+
17381751
Definition* BinaryIntegerOpInstr::Canonicalize(FlowGraph* flow_graph) {
17391752
// If both operands are constants evaluate this expression. Might
17401753
// occur due to load forwarding after constant propagation pass
@@ -1747,7 +1760,7 @@ Definition* BinaryIntegerOpInstr::Canonicalize(FlowGraph* flow_graph) {
17471760
Evaluate(Integer::Cast(left()->BoundConstant()),
17481761
Integer::Cast(right()->BoundConstant())));
17491762
if (!result.IsNull()) {
1750-
return flow_graph->GetConstant(result);
1763+
return CreateConstantResult(flow_graph, result);
17511764
}
17521765
}
17531766

@@ -1872,7 +1885,7 @@ Definition* BinaryIntegerOpInstr::Canonicalize(FlowGraph* flow_graph) {
18721885
DeoptimizeInstr* deopt =
18731886
new DeoptimizeInstr(ICData::kDeoptBinarySmiOp, GetDeoptId());
18741887
flow_graph->InsertBefore(this, deopt, env(), FlowGraph::kEffect);
1875-
return flow_graph->GetConstant(Smi::Handle(Smi::New(0)));
1888+
return CreateConstantResult(flow_graph, Integer::Handle(Smi::New(0)));
18761889
}
18771890
break;
18781891

@@ -1886,7 +1899,7 @@ Definition* BinaryIntegerOpInstr::Canonicalize(FlowGraph* flow_graph) {
18861899
new DeoptimizeInstr(ICData::kDeoptBinarySmiOp, GetDeoptId());
18871900
flow_graph->InsertBefore(this, deopt, env(), FlowGraph::kEffect);
18881901
}
1889-
return flow_graph->GetConstant(Smi::Handle(Smi::New(0)));
1902+
return CreateConstantResult(flow_graph, Integer::Handle(Smi::New(0)));
18901903
}
18911904
break;
18921905
}
@@ -2202,11 +2215,13 @@ Definition* UnboxIntegerInstr::Canonicalize(FlowGraph* flow_graph) {
22022215
// Fold away UnboxInteger<rep_to>(BoxInteger<rep_from>(v)).
22032216
BoxIntegerInstr* box_defn = value()->definition()->AsBoxInteger();
22042217
if (box_defn != NULL) {
2205-
if (box_defn->value()->definition()->representation() == representation()) {
2218+
Representation from_representation =
2219+
box_defn->value()->definition()->representation();
2220+
if (from_representation == representation()) {
22062221
return box_defn->value()->definition();
22072222
} else {
22082223
UnboxedIntConverterInstr* converter = new UnboxedIntConverterInstr(
2209-
box_defn->value()->definition()->representation(),
2224+
from_representation,
22102225
representation(),
22112226
box_defn->value()->CopyWithType(),
22122227
(representation() == kUnboxedInt32) ?

runtime/vm/intermediate_language.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6842,6 +6842,8 @@ class BinaryIntegerOpInstr : public TemplateDefinition<2, NoThrow, Pure> {
68426842
Range* range);
68436843

68446844
private:
6845+
Definition* CreateConstantResult(FlowGraph* graph, const Integer& result);
6846+
68456847
const Token::Kind op_kind_;
68466848

68476849
bool can_overflow_;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
//
5+
// Test that canonicalization inserts constants with correct representation.
6+
// VMOptions=--optimization-counter-threshold=10 --optimization-filter=bar
7+
8+
import 'dart:typed_data';
9+
10+
toSigned(v, width) {
11+
var signMask = 1 << (width - 1);
12+
return (v & (signMask - 1)) - (v & signMask);
13+
}
14+
15+
foo(value) {
16+
return value >> 32;
17+
}
18+
19+
bar(td) {
20+
return toSigned(foo(td[0]), 64);
21+
}
22+
23+
main() {
24+
toSigned(1 << 1, 32);
25+
toSigned(1 << 32, 32);
26+
27+
var l = new Int64List(1);
28+
l[0] = 0xf8f7f6f5f4f3f2f1;
29+
30+
for (var i = 0; i < 20; i++) {
31+
bar(l);
32+
}
33+
}

0 commit comments

Comments
 (0)