summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/compiler/number-divide.js
blob: 1c7710c1f890b2f813b164c29891b7ea4751454e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax --opt --noalways-opt

// Test that NumberDivide with Number feedback works if only in the
// end SimplifiedLowering figures out that the inputs to this operation
// are actually Unsigned32.
(function() {
  // We need a separately polluted % with NumberOrOddball feedback.
  function bar(x) { return x / 2; }
  bar(undefined);  // The % feedback is now NumberOrOddball.

  // Now just use the gadget above in a way that only after RETYPE
  // in SimplifiedLowering we find out that the `x` is actually in
  // Unsigned32 range (based on taking the SignedSmall feedback on
  // the + operator).
  function foo(x) {
    x = (x >>> 0) + 1;
    return bar(x) | 0;
  }

  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo(1));
  assertEquals(1, foo(2));
  assertEquals(2, foo(3));
  assertEquals(2, foo(4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(1));
  assertEquals(1, foo(2));
  assertEquals(2, foo(3));
  assertEquals(2, foo(4));
  assertOptimized(foo);
})();

// Test that NumberDivide with Number feedback works if only in the
// end SimplifiedLowering figures out that the inputs to this operation
// are actually Signed32.
(function() {
  // We need a separately polluted % with NumberOrOddball feedback.
  function bar(x) { return x / 2; }
  bar(undefined);  // The % feedback is now NumberOrOddball.

  // Now just use the gadget above in a way that only after RETYPE
  // in SimplifiedLowering we find out that the `x` is actually in
  // Signed32 range (based on taking the SignedSmall feedback on
  // the + operator).
  function foo(x) {
    x = (x | 0) + 1;
    return bar(x) | 0;
  }

  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo(1));
  assertEquals(1, foo(2));
  assertEquals(2, foo(3));
  assertEquals(2, foo(4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(1));
  assertEquals(1, foo(2));
  assertEquals(2, foo(3));
  assertEquals(2, foo(4));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedInt32Div, and
// that the "known power of two divisor" optimization works correctly.
(function() {
  function foo(x) { return (x | 0) / 2; }

  // Warmup with proper int32 divisions.
  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo(2));
  assertEquals(2, foo(4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo(6));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(0.5, foo(1));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
  %PrepareFunctionForOptimization(foo);
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(4, foo(8));
  assertOptimized(foo);
  assertEquals(0.5, foo(1));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedInt32Div, and
// that the optimized code properly bails out on "division by zero".
(function() {
  function foo(x, y) { return x / y; }

  // Warmup with proper int32 divisions.
  %PrepareFunctionForOptimization(foo);
  assertEquals(2, foo(4, 2));
  assertEquals(2, foo(8, 4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(2, 2));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(Infinity, foo(1, 0));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
  %PrepareFunctionForOptimization(foo);
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo(2, 1));
  assertOptimized(foo);
  assertEquals(Infinity, foo(1, 0));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedInt32Div, and
// that the optimized code properly bails out on minus zero.
(function() {
  function foo(x, y) { return x / y; }

  // Warmup with proper int32 divisions.
  %PrepareFunctionForOptimization(foo);
  assertEquals(2, foo(4, 2));
  assertEquals(2, foo(8, 4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(2, 2));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(-0, foo(0, -1));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
  %PrepareFunctionForOptimization(foo);
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo(2, 1));
  assertOptimized(foo);
  assertEquals(-0, foo(0, -1));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedInt32Div, and
// that the optimized code properly bails out if result is -kMinInt.
(function() {
  function foo(x, y) { return x / y; }

  // Warmup with proper int32 divisions.
  %PrepareFunctionForOptimization(foo);
  assertEquals(2, foo(4, 2));
  assertEquals(2, foo(8, 4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(2, 2));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(2147483648, foo(-2147483648, -1));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
  %PrepareFunctionForOptimization(foo);
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo(2, 1));
  assertOptimized(foo);
  assertEquals(2147483648, foo(-2147483648, -1));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedUint32Div, and
// that the "known power of two divisor" optimization works correctly.
(function() {
  function foo(s) { return s.length / 2; }

  // Warmup with proper uint32 divisions.
  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo("ab".repeat(1)));
  assertEquals(2, foo("ab".repeat(2)));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(3, foo("ab".repeat(3)));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(0.5, foo("a"));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
  %PrepareFunctionForOptimization(foo);
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(4, foo("ab".repeat(4)));
  assertOptimized(foo);
  assertEquals(0.5, foo("a"));
  assertOptimized(foo);
})();

// Test that SpeculativeNumberDivide turns into CheckedUint32Div, and
// that the optimized code properly bails out on "division by zero".
(function() {
  function foo(x, y) { return (x >>> 0) / (y >>> 0); }

  // Warmup with proper uint32 divisions.
  %PrepareFunctionForOptimization(foo);
  assertEquals(2, foo(4, 2));
  assertEquals(2, foo(8, 4));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo(2, 2));
  assertOptimized(foo);

  // Make optimized code fail.
  assertEquals(Infinity, foo(1, 0));
  assertUnoptimized(foo);

  // Try again with the new feedback, and now it should stay optimized.
  %PrepareFunctionForOptimization(foo);
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo(2, 1));
  assertOptimized(foo);
  assertEquals(Infinity, foo(1, 0));
  assertOptimized(foo);
})();