Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "src/compiler/machine-operator-reducer.h" |
| 6 | |
| 7 | #include "src/base/bits.h" |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 8 | #include "src/base/division-by-constant.h" |
| 9 | #include "src/codegen.h" |
| 10 | #include "src/compiler/diamond.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 11 | #include "src/compiler/graph.h" |
| 12 | #include "src/compiler/js-graph.h" |
| 13 | #include "src/compiler/node-matchers.h" |
| 14 | |
| 15 | namespace v8 { |
| 16 | namespace internal { |
| 17 | namespace compiler { |
| 18 | |
| 19 | MachineOperatorReducer::MachineOperatorReducer(JSGraph* jsgraph) |
| 20 | : jsgraph_(jsgraph) {} |
| 21 | |
| 22 | |
| 23 | MachineOperatorReducer::~MachineOperatorReducer() {} |
| 24 | |
| 25 | |
| 26 | Node* MachineOperatorReducer::Float32Constant(volatile float value) { |
| 27 | return graph()->NewNode(common()->Float32Constant(value)); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | Node* MachineOperatorReducer::Float64Constant(volatile double value) { |
| 32 | return jsgraph()->Float64Constant(value); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | Node* MachineOperatorReducer::Int32Constant(int32_t value) { |
| 37 | return jsgraph()->Int32Constant(value); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | Node* MachineOperatorReducer::Int64Constant(int64_t value) { |
| 42 | return graph()->NewNode(common()->Int64Constant(value)); |
| 43 | } |
| 44 | |
| 45 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 46 | Node* MachineOperatorReducer::Word32And(Node* lhs, Node* rhs) { |
| 47 | Node* const node = graph()->NewNode(machine()->Word32And(), lhs, rhs); |
| 48 | Reduction const reduction = ReduceWord32And(node); |
| 49 | return reduction.Changed() ? reduction.replacement() : node; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | Node* MachineOperatorReducer::Word32Sar(Node* lhs, uint32_t rhs) { |
| 54 | if (rhs == 0) return lhs; |
| 55 | return graph()->NewNode(machine()->Word32Sar(), lhs, Uint32Constant(rhs)); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | Node* MachineOperatorReducer::Word32Shr(Node* lhs, uint32_t rhs) { |
| 60 | if (rhs == 0) return lhs; |
| 61 | return graph()->NewNode(machine()->Word32Shr(), lhs, Uint32Constant(rhs)); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | Node* MachineOperatorReducer::Word32Equal(Node* lhs, Node* rhs) { |
| 66 | return graph()->NewNode(machine()->Word32Equal(), lhs, rhs); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) { |
| 71 | Node* const node = graph()->NewNode(machine()->Int32Add(), lhs, rhs); |
| 72 | Reduction const reduction = ReduceInt32Add(node); |
| 73 | return reduction.Changed() ? reduction.replacement() : node; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) { |
| 78 | return graph()->NewNode(machine()->Int32Sub(), lhs, rhs); |
| 79 | } |
| 80 | |
| 81 | |
| 82 | Node* MachineOperatorReducer::Int32Mul(Node* lhs, Node* rhs) { |
| 83 | return graph()->NewNode(machine()->Int32Mul(), lhs, rhs); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | Node* MachineOperatorReducer::Int32Div(Node* dividend, int32_t divisor) { |
| 88 | DCHECK_NE(0, divisor); |
| 89 | DCHECK_NE(std::numeric_limits<int32_t>::min(), divisor); |
| 90 | base::MagicNumbersForDivision<uint32_t> const mag = |
| 91 | base::SignedDivisionByConstant(bit_cast<uint32_t>(divisor)); |
| 92 | Node* quotient = graph()->NewNode(machine()->Int32MulHigh(), dividend, |
| 93 | Uint32Constant(mag.multiplier)); |
| 94 | if (divisor > 0 && bit_cast<int32_t>(mag.multiplier) < 0) { |
| 95 | quotient = Int32Add(quotient, dividend); |
| 96 | } else if (divisor < 0 && bit_cast<int32_t>(mag.multiplier) > 0) { |
| 97 | quotient = Int32Sub(quotient, dividend); |
| 98 | } |
| 99 | return Int32Add(Word32Sar(quotient, mag.shift), Word32Shr(dividend, 31)); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | Node* MachineOperatorReducer::Uint32Div(Node* dividend, uint32_t divisor) { |
| 104 | DCHECK_LT(0, divisor); |
| 105 | base::MagicNumbersForDivision<uint32_t> const mag = |
| 106 | base::UnsignedDivisionByConstant(bit_cast<uint32_t>(divisor)); |
| 107 | Node* quotient = graph()->NewNode(machine()->Uint32MulHigh(), dividend, |
| 108 | Uint32Constant(mag.multiplier)); |
| 109 | if (mag.add) { |
| 110 | DCHECK_LE(1, mag.shift); |
| 111 | quotient = Word32Shr( |
| 112 | Int32Add(Word32Shr(Int32Sub(dividend, quotient), 1), quotient), |
| 113 | mag.shift - 1); |
| 114 | } else { |
| 115 | quotient = Word32Shr(quotient, mag.shift); |
| 116 | } |
| 117 | return quotient; |
| 118 | } |
| 119 | |
| 120 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | // Perform constant folding and strength reduction on machine operators. |
| 122 | Reduction MachineOperatorReducer::Reduce(Node* node) { |
| 123 | switch (node->opcode()) { |
| 124 | case IrOpcode::kProjection: |
| 125 | return ReduceProjection(OpParameter<size_t>(node), node->InputAt(0)); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 126 | case IrOpcode::kWord32And: |
| 127 | return ReduceWord32And(node); |
| 128 | case IrOpcode::kWord32Or: |
| 129 | return ReduceWord32Or(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 130 | case IrOpcode::kWord32Xor: { |
| 131 | Int32BinopMatcher m(node); |
| 132 | if (m.right().Is(0)) return Replace(m.left().node()); // x ^ 0 => x |
| 133 | if (m.IsFoldable()) { // K ^ K => K |
| 134 | return ReplaceInt32(m.left().Value() ^ m.right().Value()); |
| 135 | } |
| 136 | if (m.LeftEqualsRight()) return ReplaceInt32(0); // x ^ x => 0 |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 137 | if (m.left().IsWord32Xor() && m.right().Is(-1)) { |
| 138 | Int32BinopMatcher mleft(m.left().node()); |
| 139 | if (mleft.right().Is(-1)) { // (x ^ -1) ^ -1 => x |
| 140 | return Replace(mleft.left().node()); |
| 141 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 142 | } |
| 143 | break; |
| 144 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 145 | case IrOpcode::kWord32Shl: |
| 146 | return ReduceWord32Shl(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 147 | case IrOpcode::kWord32Shr: { |
| 148 | Uint32BinopMatcher m(node); |
| 149 | if (m.right().Is(0)) return Replace(m.left().node()); // x >>> 0 => x |
| 150 | if (m.IsFoldable()) { // K >>> K => K |
| 151 | return ReplaceInt32(m.left().Value() >> m.right().Value()); |
| 152 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 153 | return ReduceWord32Shifts(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 154 | } |
| 155 | case IrOpcode::kWord32Sar: { |
| 156 | Int32BinopMatcher m(node); |
| 157 | if (m.right().Is(0)) return Replace(m.left().node()); // x >> 0 => x |
| 158 | if (m.IsFoldable()) { // K >> K => K |
| 159 | return ReplaceInt32(m.left().Value() >> m.right().Value()); |
| 160 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 161 | if (m.left().IsWord32Shl()) { |
| 162 | Int32BinopMatcher mleft(m.left().node()); |
| 163 | if (mleft.left().IsLoad()) { |
| 164 | LoadRepresentation const rep = |
| 165 | OpParameter<LoadRepresentation>(mleft.left().node()); |
| 166 | if (m.right().Is(24) && mleft.right().Is(24) && rep == kMachInt8) { |
| 167 | // Load[kMachInt8] << 24 >> 24 => Load[kMachInt8] |
| 168 | return Replace(mleft.left().node()); |
| 169 | } |
| 170 | if (m.right().Is(16) && mleft.right().Is(16) && rep == kMachInt16) { |
| 171 | // Load[kMachInt16] << 16 >> 16 => Load[kMachInt8] |
| 172 | return Replace(mleft.left().node()); |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | return ReduceWord32Shifts(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | } |
| 178 | case IrOpcode::kWord32Ror: { |
| 179 | Int32BinopMatcher m(node); |
| 180 | if (m.right().Is(0)) return Replace(m.left().node()); // x ror 0 => x |
| 181 | if (m.IsFoldable()) { // K ror K => K |
| 182 | return ReplaceInt32( |
| 183 | base::bits::RotateRight32(m.left().Value(), m.right().Value())); |
| 184 | } |
| 185 | break; |
| 186 | } |
| 187 | case IrOpcode::kWord32Equal: { |
| 188 | Int32BinopMatcher m(node); |
| 189 | if (m.IsFoldable()) { // K == K => K |
| 190 | return ReplaceBool(m.left().Value() == m.right().Value()); |
| 191 | } |
| 192 | if (m.left().IsInt32Sub() && m.right().Is(0)) { // x - y == 0 => x == y |
| 193 | Int32BinopMatcher msub(m.left().node()); |
| 194 | node->ReplaceInput(0, msub.left().node()); |
| 195 | node->ReplaceInput(1, msub.right().node()); |
| 196 | return Changed(node); |
| 197 | } |
| 198 | // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares |
| 199 | if (m.LeftEqualsRight()) return ReplaceBool(true); // x == x => true |
| 200 | break; |
| 201 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 202 | case IrOpcode::kWord64Equal: { |
| 203 | Int64BinopMatcher m(node); |
| 204 | if (m.IsFoldable()) { // K == K => K |
| 205 | return ReplaceBool(m.left().Value() == m.right().Value()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 206 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 207 | if (m.left().IsInt64Sub() && m.right().Is(0)) { // x - y == 0 => x == y |
| 208 | Int64BinopMatcher msub(m.left().node()); |
| 209 | node->ReplaceInput(0, msub.left().node()); |
| 210 | node->ReplaceInput(1, msub.right().node()); |
| 211 | return Changed(node); |
| 212 | } |
| 213 | // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares |
| 214 | if (m.LeftEqualsRight()) return ReplaceBool(true); // x == x => true |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 215 | break; |
| 216 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 217 | case IrOpcode::kInt32Add: |
| 218 | return ReduceInt32Add(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 219 | case IrOpcode::kInt32Sub: { |
| 220 | Int32BinopMatcher m(node); |
| 221 | if (m.right().Is(0)) return Replace(m.left().node()); // x - 0 => x |
| 222 | if (m.IsFoldable()) { // K - K => K |
| 223 | return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) - |
| 224 | static_cast<uint32_t>(m.right().Value())); |
| 225 | } |
| 226 | if (m.LeftEqualsRight()) return ReplaceInt32(0); // x - x => 0 |
| 227 | break; |
| 228 | } |
| 229 | case IrOpcode::kInt32Mul: { |
| 230 | Int32BinopMatcher m(node); |
| 231 | if (m.right().Is(0)) return Replace(m.right().node()); // x * 0 => 0 |
| 232 | if (m.right().Is(1)) return Replace(m.left().node()); // x * 1 => x |
| 233 | if (m.IsFoldable()) { // K * K => K |
| 234 | return ReplaceInt32(m.left().Value() * m.right().Value()); |
| 235 | } |
| 236 | if (m.right().Is(-1)) { // x * -1 => 0 - x |
| 237 | node->set_op(machine()->Int32Sub()); |
| 238 | node->ReplaceInput(0, Int32Constant(0)); |
| 239 | node->ReplaceInput(1, m.left().node()); |
| 240 | return Changed(node); |
| 241 | } |
| 242 | if (m.right().IsPowerOf2()) { // x * 2^n => x << n |
| 243 | node->set_op(machine()->Word32Shl()); |
| 244 | node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value()))); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 245 | Reduction reduction = ReduceWord32Shl(node); |
| 246 | return reduction.Changed() ? reduction : Changed(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 247 | } |
| 248 | break; |
| 249 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 250 | case IrOpcode::kInt32Div: |
| 251 | return ReduceInt32Div(node); |
| 252 | case IrOpcode::kUint32Div: |
| 253 | return ReduceUint32Div(node); |
| 254 | case IrOpcode::kInt32Mod: |
| 255 | return ReduceInt32Mod(node); |
| 256 | case IrOpcode::kUint32Mod: |
| 257 | return ReduceUint32Mod(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 258 | case IrOpcode::kInt32LessThan: { |
| 259 | Int32BinopMatcher m(node); |
| 260 | if (m.IsFoldable()) { // K < K => K |
| 261 | return ReplaceBool(m.left().Value() < m.right().Value()); |
| 262 | } |
| 263 | if (m.left().IsInt32Sub() && m.right().Is(0)) { // x - y < 0 => x < y |
| 264 | Int32BinopMatcher msub(m.left().node()); |
| 265 | node->ReplaceInput(0, msub.left().node()); |
| 266 | node->ReplaceInput(1, msub.right().node()); |
| 267 | return Changed(node); |
| 268 | } |
| 269 | if (m.left().Is(0) && m.right().IsInt32Sub()) { // 0 < x - y => y < x |
| 270 | Int32BinopMatcher msub(m.right().node()); |
| 271 | node->ReplaceInput(0, msub.right().node()); |
| 272 | node->ReplaceInput(1, msub.left().node()); |
| 273 | return Changed(node); |
| 274 | } |
| 275 | if (m.LeftEqualsRight()) return ReplaceBool(false); // x < x => false |
| 276 | break; |
| 277 | } |
| 278 | case IrOpcode::kInt32LessThanOrEqual: { |
| 279 | Int32BinopMatcher m(node); |
| 280 | if (m.IsFoldable()) { // K <= K => K |
| 281 | return ReplaceBool(m.left().Value() <= m.right().Value()); |
| 282 | } |
| 283 | if (m.left().IsInt32Sub() && m.right().Is(0)) { // x - y <= 0 => x <= y |
| 284 | Int32BinopMatcher msub(m.left().node()); |
| 285 | node->ReplaceInput(0, msub.left().node()); |
| 286 | node->ReplaceInput(1, msub.right().node()); |
| 287 | return Changed(node); |
| 288 | } |
| 289 | if (m.left().Is(0) && m.right().IsInt32Sub()) { // 0 <= x - y => y <= x |
| 290 | Int32BinopMatcher msub(m.right().node()); |
| 291 | node->ReplaceInput(0, msub.right().node()); |
| 292 | node->ReplaceInput(1, msub.left().node()); |
| 293 | return Changed(node); |
| 294 | } |
| 295 | if (m.LeftEqualsRight()) return ReplaceBool(true); // x <= x => true |
| 296 | break; |
| 297 | } |
| 298 | case IrOpcode::kUint32LessThan: { |
| 299 | Uint32BinopMatcher m(node); |
| 300 | if (m.left().Is(kMaxUInt32)) return ReplaceBool(false); // M < x => false |
| 301 | if (m.right().Is(0)) return ReplaceBool(false); // x < 0 => false |
| 302 | if (m.IsFoldable()) { // K < K => K |
| 303 | return ReplaceBool(m.left().Value() < m.right().Value()); |
| 304 | } |
| 305 | if (m.LeftEqualsRight()) return ReplaceBool(false); // x < x => false |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 306 | if (m.left().IsWord32Sar() && m.right().HasValue()) { |
| 307 | Int32BinopMatcher mleft(m.left().node()); |
| 308 | if (mleft.right().HasValue()) { |
| 309 | // (x >> K) < C => x < (C << K) |
| 310 | // when C < (M >> K) |
| 311 | const uint32_t c = m.right().Value(); |
| 312 | const uint32_t k = mleft.right().Value() & 0x1f; |
| 313 | if (c < static_cast<uint32_t>(kMaxInt >> k)) { |
| 314 | node->ReplaceInput(0, mleft.left().node()); |
| 315 | node->ReplaceInput(1, Uint32Constant(c << k)); |
| 316 | return Changed(node); |
| 317 | } |
| 318 | // TODO(turbofan): else the comparison is always true. |
| 319 | } |
| 320 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 321 | break; |
| 322 | } |
| 323 | case IrOpcode::kUint32LessThanOrEqual: { |
| 324 | Uint32BinopMatcher m(node); |
| 325 | if (m.left().Is(0)) return ReplaceBool(true); // 0 <= x => true |
| 326 | if (m.right().Is(kMaxUInt32)) return ReplaceBool(true); // x <= M => true |
| 327 | if (m.IsFoldable()) { // K <= K => K |
| 328 | return ReplaceBool(m.left().Value() <= m.right().Value()); |
| 329 | } |
| 330 | if (m.LeftEqualsRight()) return ReplaceBool(true); // x <= x => true |
| 331 | break; |
| 332 | } |
| 333 | case IrOpcode::kFloat64Add: { |
| 334 | Float64BinopMatcher m(node); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 335 | if (m.right().IsNaN()) { // x + NaN => NaN |
| 336 | return Replace(m.right().node()); |
| 337 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 338 | if (m.IsFoldable()) { // K + K => K |
| 339 | return ReplaceFloat64(m.left().Value() + m.right().Value()); |
| 340 | } |
| 341 | break; |
| 342 | } |
| 343 | case IrOpcode::kFloat64Sub: { |
| 344 | Float64BinopMatcher m(node); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 345 | if (m.right().Is(0) && (Double(m.right().Value()).Sign() > 0)) { |
| 346 | return Replace(m.left().node()); // x - 0 => x |
| 347 | } |
| 348 | if (m.right().IsNaN()) { // x - NaN => NaN |
| 349 | return Replace(m.right().node()); |
| 350 | } |
| 351 | if (m.left().IsNaN()) { // NaN - x => NaN |
| 352 | return Replace(m.left().node()); |
| 353 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 354 | if (m.IsFoldable()) { // K - K => K |
| 355 | return ReplaceFloat64(m.left().Value() - m.right().Value()); |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | case IrOpcode::kFloat64Mul: { |
| 360 | Float64BinopMatcher m(node); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 361 | if (m.right().Is(-1)) { // x * -1.0 => -0.0 - x |
| 362 | node->set_op(machine()->Float64Sub()); |
| 363 | node->ReplaceInput(0, Float64Constant(-0.0)); |
| 364 | node->ReplaceInput(1, m.left().node()); |
| 365 | return Changed(node); |
| 366 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 367 | if (m.right().Is(1)) return Replace(m.left().node()); // x * 1.0 => x |
| 368 | if (m.right().IsNaN()) { // x * NaN => NaN |
| 369 | return Replace(m.right().node()); |
| 370 | } |
| 371 | if (m.IsFoldable()) { // K * K => K |
| 372 | return ReplaceFloat64(m.left().Value() * m.right().Value()); |
| 373 | } |
| 374 | break; |
| 375 | } |
| 376 | case IrOpcode::kFloat64Div: { |
| 377 | Float64BinopMatcher m(node); |
| 378 | if (m.right().Is(1)) return Replace(m.left().node()); // x / 1.0 => x |
| 379 | if (m.right().IsNaN()) { // x / NaN => NaN |
| 380 | return Replace(m.right().node()); |
| 381 | } |
| 382 | if (m.left().IsNaN()) { // NaN / x => NaN |
| 383 | return Replace(m.left().node()); |
| 384 | } |
| 385 | if (m.IsFoldable()) { // K / K => K |
| 386 | return ReplaceFloat64(m.left().Value() / m.right().Value()); |
| 387 | } |
| 388 | break; |
| 389 | } |
| 390 | case IrOpcode::kFloat64Mod: { |
| 391 | Float64BinopMatcher m(node); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 392 | if (m.right().Is(0)) { // x % 0 => NaN |
| 393 | return ReplaceFloat64(base::OS::nan_value()); |
| 394 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 395 | if (m.right().IsNaN()) { // x % NaN => NaN |
| 396 | return Replace(m.right().node()); |
| 397 | } |
| 398 | if (m.left().IsNaN()) { // NaN % x => NaN |
| 399 | return Replace(m.left().node()); |
| 400 | } |
| 401 | if (m.IsFoldable()) { // K % K => K |
| 402 | return ReplaceFloat64(modulo(m.left().Value(), m.right().Value())); |
| 403 | } |
| 404 | break; |
| 405 | } |
| 406 | case IrOpcode::kChangeFloat32ToFloat64: { |
| 407 | Float32Matcher m(node->InputAt(0)); |
| 408 | if (m.HasValue()) return ReplaceFloat64(m.Value()); |
| 409 | break; |
| 410 | } |
| 411 | case IrOpcode::kChangeFloat64ToInt32: { |
| 412 | Float64Matcher m(node->InputAt(0)); |
| 413 | if (m.HasValue()) return ReplaceInt32(FastD2I(m.Value())); |
| 414 | if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0)); |
| 415 | break; |
| 416 | } |
| 417 | case IrOpcode::kChangeFloat64ToUint32: { |
| 418 | Float64Matcher m(node->InputAt(0)); |
| 419 | if (m.HasValue()) return ReplaceInt32(FastD2UI(m.Value())); |
| 420 | if (m.IsChangeUint32ToFloat64()) return Replace(m.node()->InputAt(0)); |
| 421 | break; |
| 422 | } |
| 423 | case IrOpcode::kChangeInt32ToFloat64: { |
| 424 | Int32Matcher m(node->InputAt(0)); |
| 425 | if (m.HasValue()) return ReplaceFloat64(FastI2D(m.Value())); |
| 426 | break; |
| 427 | } |
| 428 | case IrOpcode::kChangeInt32ToInt64: { |
| 429 | Int32Matcher m(node->InputAt(0)); |
| 430 | if (m.HasValue()) return ReplaceInt64(m.Value()); |
| 431 | break; |
| 432 | } |
| 433 | case IrOpcode::kChangeUint32ToFloat64: { |
| 434 | Uint32Matcher m(node->InputAt(0)); |
| 435 | if (m.HasValue()) return ReplaceFloat64(FastUI2D(m.Value())); |
| 436 | break; |
| 437 | } |
| 438 | case IrOpcode::kChangeUint32ToUint64: { |
| 439 | Uint32Matcher m(node->InputAt(0)); |
| 440 | if (m.HasValue()) return ReplaceInt64(static_cast<uint64_t>(m.Value())); |
| 441 | break; |
| 442 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 443 | case IrOpcode::kTruncateFloat64ToInt32: |
| 444 | return ReduceTruncateFloat64ToInt32(node); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 445 | case IrOpcode::kTruncateInt64ToInt32: { |
| 446 | Int64Matcher m(node->InputAt(0)); |
| 447 | if (m.HasValue()) return ReplaceInt32(static_cast<int32_t>(m.Value())); |
| 448 | if (m.IsChangeInt32ToInt64()) return Replace(m.node()->InputAt(0)); |
| 449 | break; |
| 450 | } |
| 451 | case IrOpcode::kTruncateFloat64ToFloat32: { |
| 452 | Float64Matcher m(node->InputAt(0)); |
| 453 | if (m.HasValue()) return ReplaceFloat32(DoubleToFloat32(m.Value())); |
| 454 | if (m.IsChangeFloat32ToFloat64()) return Replace(m.node()->InputAt(0)); |
| 455 | break; |
| 456 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 457 | case IrOpcode::kStore: |
| 458 | return ReduceStore(node); |
| 459 | default: |
| 460 | break; |
| 461 | } |
| 462 | return NoChange(); |
| 463 | } |
| 464 | |
| 465 | |
| 466 | Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) { |
| 467 | DCHECK_EQ(IrOpcode::kInt32Add, node->opcode()); |
| 468 | Int32BinopMatcher m(node); |
| 469 | if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => x |
| 470 | if (m.IsFoldable()) { // K + K => K |
| 471 | return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) + |
| 472 | bit_cast<uint32_t>(m.right().Value())); |
| 473 | } |
| 474 | return NoChange(); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) { |
| 479 | Int32BinopMatcher m(node); |
| 480 | if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0 |
| 481 | if (m.right().Is(0)) return Replace(m.right().node()); // x / 0 => 0 |
| 482 | if (m.right().Is(1)) return Replace(m.left().node()); // x / 1 => x |
| 483 | if (m.IsFoldable()) { // K / K => K |
| 484 | return ReplaceInt32( |
| 485 | base::bits::SignedDiv32(m.left().Value(), m.right().Value())); |
| 486 | } |
| 487 | if (m.LeftEqualsRight()) { // x / x => x != 0 |
| 488 | Node* const zero = Int32Constant(0); |
| 489 | return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero)); |
| 490 | } |
| 491 | if (m.right().Is(-1)) { // x / -1 => 0 - x |
| 492 | node->set_op(machine()->Int32Sub()); |
| 493 | node->ReplaceInput(0, Int32Constant(0)); |
| 494 | node->ReplaceInput(1, m.left().node()); |
| 495 | node->TrimInputCount(2); |
| 496 | return Changed(node); |
| 497 | } |
| 498 | if (m.right().HasValue()) { |
| 499 | int32_t const divisor = m.right().Value(); |
| 500 | Node* const dividend = m.left().node(); |
| 501 | Node* quotient = dividend; |
| 502 | if (base::bits::IsPowerOfTwo32(Abs(divisor))) { |
| 503 | uint32_t const shift = WhichPowerOf2Abs(divisor); |
| 504 | DCHECK_NE(0, shift); |
| 505 | if (shift > 1) { |
| 506 | quotient = Word32Sar(quotient, 31); |
| 507 | } |
| 508 | quotient = Int32Add(Word32Shr(quotient, 32u - shift), dividend); |
| 509 | quotient = Word32Sar(quotient, shift); |
| 510 | } else { |
| 511 | quotient = Int32Div(quotient, Abs(divisor)); |
| 512 | } |
| 513 | if (divisor < 0) { |
| 514 | node->set_op(machine()->Int32Sub()); |
| 515 | node->ReplaceInput(0, Int32Constant(0)); |
| 516 | node->ReplaceInput(1, quotient); |
| 517 | node->TrimInputCount(2); |
| 518 | return Changed(node); |
| 519 | } |
| 520 | return Replace(quotient); |
| 521 | } |
| 522 | return NoChange(); |
| 523 | } |
| 524 | |
| 525 | |
| 526 | Reduction MachineOperatorReducer::ReduceUint32Div(Node* node) { |
| 527 | Uint32BinopMatcher m(node); |
| 528 | if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0 |
| 529 | if (m.right().Is(0)) return Replace(m.right().node()); // x / 0 => 0 |
| 530 | if (m.right().Is(1)) return Replace(m.left().node()); // x / 1 => x |
| 531 | if (m.IsFoldable()) { // K / K => K |
| 532 | return ReplaceUint32( |
| 533 | base::bits::UnsignedDiv32(m.left().Value(), m.right().Value())); |
| 534 | } |
| 535 | if (m.LeftEqualsRight()) { // x / x => x != 0 |
| 536 | Node* const zero = Int32Constant(0); |
| 537 | return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero)); |
| 538 | } |
| 539 | if (m.right().HasValue()) { |
| 540 | Node* const dividend = m.left().node(); |
| 541 | uint32_t const divisor = m.right().Value(); |
| 542 | if (base::bits::IsPowerOfTwo32(divisor)) { // x / 2^n => x >> n |
| 543 | node->set_op(machine()->Word32Shr()); |
| 544 | node->ReplaceInput(1, Uint32Constant(WhichPowerOf2(m.right().Value()))); |
| 545 | node->TrimInputCount(2); |
| 546 | return Changed(node); |
| 547 | } else { |
| 548 | return Replace(Uint32Div(dividend, divisor)); |
| 549 | } |
| 550 | } |
| 551 | return NoChange(); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | Reduction MachineOperatorReducer::ReduceInt32Mod(Node* node) { |
| 556 | Int32BinopMatcher m(node); |
| 557 | if (m.left().Is(0)) return Replace(m.left().node()); // 0 % x => 0 |
| 558 | if (m.right().Is(0)) return Replace(m.right().node()); // x % 0 => 0 |
| 559 | if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0 |
| 560 | if (m.right().Is(-1)) return ReplaceInt32(0); // x % -1 => 0 |
| 561 | if (m.LeftEqualsRight()) return ReplaceInt32(0); // x % x => 0 |
| 562 | if (m.IsFoldable()) { // K % K => K |
| 563 | return ReplaceInt32( |
| 564 | base::bits::SignedMod32(m.left().Value(), m.right().Value())); |
| 565 | } |
| 566 | if (m.right().HasValue()) { |
| 567 | Node* const dividend = m.left().node(); |
| 568 | int32_t const divisor = Abs(m.right().Value()); |
| 569 | if (base::bits::IsPowerOfTwo32(divisor)) { |
| 570 | uint32_t const mask = divisor - 1; |
| 571 | Node* const zero = Int32Constant(0); |
| 572 | node->set_op(common()->Select(kMachInt32, BranchHint::kFalse)); |
| 573 | node->ReplaceInput( |
| 574 | 0, graph()->NewNode(machine()->Int32LessThan(), dividend, zero)); |
| 575 | node->ReplaceInput( |
| 576 | 1, Int32Sub(zero, Word32And(Int32Sub(zero, dividend), mask))); |
| 577 | node->ReplaceInput(2, Word32And(dividend, mask)); |
| 578 | } else { |
| 579 | Node* quotient = Int32Div(dividend, divisor); |
| 580 | node->set_op(machine()->Int32Sub()); |
| 581 | DCHECK_EQ(dividend, node->InputAt(0)); |
| 582 | node->ReplaceInput(1, Int32Mul(quotient, Int32Constant(divisor))); |
| 583 | node->TrimInputCount(2); |
| 584 | } |
| 585 | return Changed(node); |
| 586 | } |
| 587 | return NoChange(); |
| 588 | } |
| 589 | |
| 590 | |
| 591 | Reduction MachineOperatorReducer::ReduceUint32Mod(Node* node) { |
| 592 | Uint32BinopMatcher m(node); |
| 593 | if (m.left().Is(0)) return Replace(m.left().node()); // 0 % x => 0 |
| 594 | if (m.right().Is(0)) return Replace(m.right().node()); // x % 0 => 0 |
| 595 | if (m.right().Is(1)) return ReplaceUint32(0); // x % 1 => 0 |
| 596 | if (m.LeftEqualsRight()) return ReplaceInt32(0); // x % x => 0 |
| 597 | if (m.IsFoldable()) { // K % K => K |
| 598 | return ReplaceUint32( |
| 599 | base::bits::UnsignedMod32(m.left().Value(), m.right().Value())); |
| 600 | } |
| 601 | if (m.right().HasValue()) { |
| 602 | Node* const dividend = m.left().node(); |
| 603 | uint32_t const divisor = m.right().Value(); |
| 604 | if (base::bits::IsPowerOfTwo32(divisor)) { // x % 2^n => x & 2^n-1 |
| 605 | node->set_op(machine()->Word32And()); |
| 606 | node->ReplaceInput(1, Uint32Constant(m.right().Value() - 1)); |
| 607 | } else { |
| 608 | Node* quotient = Uint32Div(dividend, divisor); |
| 609 | node->set_op(machine()->Int32Sub()); |
| 610 | DCHECK_EQ(dividend, node->InputAt(0)); |
| 611 | node->ReplaceInput(1, Int32Mul(quotient, Uint32Constant(divisor))); |
| 612 | } |
| 613 | node->TrimInputCount(2); |
| 614 | return Changed(node); |
| 615 | } |
| 616 | return NoChange(); |
| 617 | } |
| 618 | |
| 619 | |
| 620 | Reduction MachineOperatorReducer::ReduceTruncateFloat64ToInt32(Node* node) { |
| 621 | Float64Matcher m(node->InputAt(0)); |
| 622 | if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value())); |
| 623 | if (m.IsChangeInt32ToFloat64()) return Replace(m.node()->InputAt(0)); |
| 624 | if (m.IsPhi()) { |
| 625 | Node* const phi = m.node(); |
| 626 | DCHECK_EQ(kRepFloat64, RepresentationOf(OpParameter<MachineType>(phi))); |
| 627 | if (phi->OwnedBy(node)) { |
| 628 | // TruncateFloat64ToInt32(Phi[Float64](x1,...,xn)) |
| 629 | // => Phi[Int32](TruncateFloat64ToInt32(x1), |
| 630 | // ..., |
| 631 | // TruncateFloat64ToInt32(xn)) |
| 632 | const int value_input_count = phi->InputCount() - 1; |
| 633 | for (int i = 0; i < value_input_count; ++i) { |
| 634 | Node* input = graph()->NewNode(machine()->TruncateFloat64ToInt32(), |
| 635 | phi->InputAt(i)); |
| 636 | // TODO(bmeurer): Reschedule input for reduction once we have Revisit() |
| 637 | // instead of recursing into ReduceTruncateFloat64ToInt32() here. |
| 638 | Reduction reduction = ReduceTruncateFloat64ToInt32(input); |
| 639 | if (reduction.Changed()) input = reduction.replacement(); |
| 640 | phi->ReplaceInput(i, input); |
| 641 | } |
| 642 | phi->set_op(common()->Phi(kMachInt32, value_input_count)); |
| 643 | return Replace(phi); |
| 644 | } |
| 645 | } |
| 646 | return NoChange(); |
| 647 | } |
| 648 | |
| 649 | |
| 650 | Reduction MachineOperatorReducer::ReduceStore(Node* node) { |
| 651 | MachineType const rep = |
| 652 | RepresentationOf(StoreRepresentationOf(node->op()).machine_type()); |
| 653 | Node* const value = node->InputAt(2); |
| 654 | switch (value->opcode()) { |
| 655 | case IrOpcode::kWord32And: { |
| 656 | Uint32BinopMatcher m(value); |
| 657 | if (m.right().HasValue() && |
| 658 | ((rep == kRepWord8 && (m.right().Value() & 0xff) == 0xff) || |
| 659 | (rep == kRepWord16 && (m.right().Value() & 0xffff) == 0xffff))) { |
| 660 | node->ReplaceInput(2, m.left().node()); |
| 661 | return Changed(node); |
| 662 | } |
| 663 | break; |
| 664 | } |
| 665 | case IrOpcode::kWord32Sar: { |
| 666 | Int32BinopMatcher m(value); |
| 667 | if (m.left().IsWord32Shl() && |
| 668 | ((rep == kRepWord8 && m.right().IsInRange(1, 24)) || |
| 669 | (rep == kRepWord16 && m.right().IsInRange(1, 16)))) { |
| 670 | Int32BinopMatcher mleft(m.left().node()); |
| 671 | if (mleft.right().Is(m.right().Value())) { |
| 672 | node->ReplaceInput(2, mleft.left().node()); |
| 673 | return Changed(node); |
| 674 | } |
| 675 | } |
| 676 | break; |
| 677 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 678 | default: |
| 679 | break; |
| 680 | } |
| 681 | return NoChange(); |
| 682 | } |
| 683 | |
| 684 | |
| 685 | Reduction MachineOperatorReducer::ReduceProjection(size_t index, Node* node) { |
| 686 | switch (node->opcode()) { |
| 687 | case IrOpcode::kInt32AddWithOverflow: { |
| 688 | DCHECK(index == 0 || index == 1); |
| 689 | Int32BinopMatcher m(node); |
| 690 | if (m.IsFoldable()) { |
| 691 | int32_t val; |
| 692 | bool ovf = base::bits::SignedAddOverflow32(m.left().Value(), |
| 693 | m.right().Value(), &val); |
| 694 | return ReplaceInt32((index == 0) ? val : ovf); |
| 695 | } |
| 696 | if (m.right().Is(0)) { |
| 697 | return (index == 0) ? Replace(m.left().node()) : ReplaceInt32(0); |
| 698 | } |
| 699 | break; |
| 700 | } |
| 701 | case IrOpcode::kInt32SubWithOverflow: { |
| 702 | DCHECK(index == 0 || index == 1); |
| 703 | Int32BinopMatcher m(node); |
| 704 | if (m.IsFoldable()) { |
| 705 | int32_t val; |
| 706 | bool ovf = base::bits::SignedSubOverflow32(m.left().Value(), |
| 707 | m.right().Value(), &val); |
| 708 | return ReplaceInt32((index == 0) ? val : ovf); |
| 709 | } |
| 710 | if (m.right().Is(0)) { |
| 711 | return (index == 0) ? Replace(m.left().node()) : ReplaceInt32(0); |
| 712 | } |
| 713 | break; |
| 714 | } |
| 715 | default: |
| 716 | break; |
| 717 | } |
| 718 | return NoChange(); |
| 719 | } |
| 720 | |
| 721 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 722 | Reduction MachineOperatorReducer::ReduceWord32Shifts(Node* node) { |
| 723 | DCHECK((node->opcode() == IrOpcode::kWord32Shl) || |
| 724 | (node->opcode() == IrOpcode::kWord32Shr) || |
| 725 | (node->opcode() == IrOpcode::kWord32Sar)); |
| 726 | if (machine()->Word32ShiftIsSafe()) { |
| 727 | // Remove the explicit 'and' with 0x1f if the shift provided by the machine |
| 728 | // instruction matches that required by JavaScript. |
| 729 | Int32BinopMatcher m(node); |
| 730 | if (m.right().IsWord32And()) { |
| 731 | Int32BinopMatcher mright(m.right().node()); |
| 732 | if (mright.right().Is(0x1f)) { |
| 733 | node->ReplaceInput(1, mright.left().node()); |
| 734 | return Changed(node); |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | return NoChange(); |
| 739 | } |
| 740 | |
| 741 | |
| 742 | Reduction MachineOperatorReducer::ReduceWord32Shl(Node* node) { |
| 743 | DCHECK_EQ(IrOpcode::kWord32Shl, node->opcode()); |
| 744 | Int32BinopMatcher m(node); |
| 745 | if (m.right().Is(0)) return Replace(m.left().node()); // x << 0 => x |
| 746 | if (m.IsFoldable()) { // K << K => K |
| 747 | return ReplaceInt32(m.left().Value() << m.right().Value()); |
| 748 | } |
| 749 | if (m.right().IsInRange(1, 31)) { |
| 750 | // (x >>> K) << K => x & ~(2^K - 1) |
| 751 | // (x >> K) << K => x & ~(2^K - 1) |
| 752 | if (m.left().IsWord32Sar() || m.left().IsWord32Shr()) { |
| 753 | Int32BinopMatcher mleft(m.left().node()); |
| 754 | if (mleft.right().Is(m.right().Value())) { |
| 755 | node->set_op(machine()->Word32And()); |
| 756 | node->ReplaceInput(0, mleft.left().node()); |
| 757 | node->ReplaceInput(1, |
| 758 | Uint32Constant(~((1U << m.right().Value()) - 1U))); |
| 759 | Reduction reduction = ReduceWord32And(node); |
| 760 | return reduction.Changed() ? reduction : Changed(node); |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | return ReduceWord32Shifts(node); |
| 765 | } |
| 766 | |
| 767 | |
| 768 | Reduction MachineOperatorReducer::ReduceWord32And(Node* node) { |
| 769 | DCHECK_EQ(IrOpcode::kWord32And, node->opcode()); |
| 770 | Int32BinopMatcher m(node); |
| 771 | if (m.right().Is(0)) return Replace(m.right().node()); // x & 0 => 0 |
| 772 | if (m.right().Is(-1)) return Replace(m.left().node()); // x & -1 => x |
| 773 | if (m.IsFoldable()) { // K & K => K |
| 774 | return ReplaceInt32(m.left().Value() & m.right().Value()); |
| 775 | } |
| 776 | if (m.LeftEqualsRight()) return Replace(m.left().node()); // x & x => x |
| 777 | if (m.left().IsWord32And() && m.right().HasValue()) { |
| 778 | Int32BinopMatcher mleft(m.left().node()); |
| 779 | if (mleft.right().HasValue()) { // (x & K) & K => x & K |
| 780 | node->ReplaceInput(0, mleft.left().node()); |
| 781 | node->ReplaceInput( |
| 782 | 1, Int32Constant(m.right().Value() & mleft.right().Value())); |
| 783 | Reduction const reduction = ReduceWord32And(node); |
| 784 | return reduction.Changed() ? reduction : Changed(node); |
| 785 | } |
| 786 | } |
| 787 | if (m.left().IsInt32Add() && m.right().IsNegativePowerOf2()) { |
| 788 | Int32BinopMatcher mleft(m.left().node()); |
| 789 | if (mleft.right().HasValue() && |
| 790 | (mleft.right().Value() & m.right().Value()) == mleft.right().Value()) { |
| 791 | // (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L) |
| 792 | node->set_op(machine()->Int32Add()); |
| 793 | node->ReplaceInput(0, Word32And(mleft.left().node(), m.right().node())); |
| 794 | node->ReplaceInput(1, mleft.right().node()); |
| 795 | Reduction const reduction = ReduceInt32Add(node); |
| 796 | return reduction.Changed() ? reduction : Changed(node); |
| 797 | } |
| 798 | if (mleft.left().IsInt32Mul()) { |
| 799 | Int32BinopMatcher mleftleft(mleft.left().node()); |
| 800 | if (mleftleft.right().IsMultipleOf(-m.right().Value())) { |
| 801 | // (y * (K << L) + x) & (-1 << L) => (x & (-1 << L)) + y * (K << L) |
| 802 | node->set_op(machine()->Int32Add()); |
| 803 | node->ReplaceInput(0, |
| 804 | Word32And(mleft.right().node(), m.right().node())); |
| 805 | node->ReplaceInput(1, mleftleft.node()); |
| 806 | Reduction const reduction = ReduceInt32Add(node); |
| 807 | return reduction.Changed() ? reduction : Changed(node); |
| 808 | } |
| 809 | } |
| 810 | if (mleft.right().IsInt32Mul()) { |
| 811 | Int32BinopMatcher mleftright(mleft.right().node()); |
| 812 | if (mleftright.right().IsMultipleOf(-m.right().Value())) { |
| 813 | // (x + y * (K << L)) & (-1 << L) => (x & (-1 << L)) + y * (K << L) |
| 814 | node->set_op(machine()->Int32Add()); |
| 815 | node->ReplaceInput(0, Word32And(mleft.left().node(), m.right().node())); |
| 816 | node->ReplaceInput(1, mleftright.node()); |
| 817 | Reduction const reduction = ReduceInt32Add(node); |
| 818 | return reduction.Changed() ? reduction : Changed(node); |
| 819 | } |
| 820 | } |
| 821 | if (mleft.left().IsWord32Shl()) { |
| 822 | Int32BinopMatcher mleftleft(mleft.left().node()); |
| 823 | if (mleftleft.right().Is( |
| 824 | base::bits::CountTrailingZeros32(m.right().Value()))) { |
| 825 | // (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L |
| 826 | node->set_op(machine()->Int32Add()); |
| 827 | node->ReplaceInput(0, |
| 828 | Word32And(mleft.right().node(), m.right().node())); |
| 829 | node->ReplaceInput(1, mleftleft.node()); |
| 830 | Reduction const reduction = ReduceInt32Add(node); |
| 831 | return reduction.Changed() ? reduction : Changed(node); |
| 832 | } |
| 833 | } |
| 834 | if (mleft.right().IsWord32Shl()) { |
| 835 | Int32BinopMatcher mleftright(mleft.right().node()); |
| 836 | if (mleftright.right().Is( |
| 837 | base::bits::CountTrailingZeros32(m.right().Value()))) { |
| 838 | // (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L |
| 839 | node->set_op(machine()->Int32Add()); |
| 840 | node->ReplaceInput(0, Word32And(mleft.left().node(), m.right().node())); |
| 841 | node->ReplaceInput(1, mleftright.node()); |
| 842 | Reduction const reduction = ReduceInt32Add(node); |
| 843 | return reduction.Changed() ? reduction : Changed(node); |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | return NoChange(); |
| 848 | } |
| 849 | |
| 850 | |
| 851 | Reduction MachineOperatorReducer::ReduceWord32Or(Node* node) { |
| 852 | DCHECK_EQ(IrOpcode::kWord32Or, node->opcode()); |
| 853 | Int32BinopMatcher m(node); |
| 854 | if (m.right().Is(0)) return Replace(m.left().node()); // x | 0 => x |
| 855 | if (m.right().Is(-1)) return Replace(m.right().node()); // x | -1 => -1 |
| 856 | if (m.IsFoldable()) { // K | K => K |
| 857 | return ReplaceInt32(m.left().Value() | m.right().Value()); |
| 858 | } |
| 859 | if (m.LeftEqualsRight()) return Replace(m.left().node()); // x | x => x |
| 860 | |
| 861 | Node* shl = NULL; |
| 862 | Node* shr = NULL; |
| 863 | // Recognize rotation, we are matching either: |
| 864 | // * x << y | x >>> (32 - y) => x ror (32 - y), i.e x rol y |
| 865 | // * x << (32 - y) | x >>> y => x ror y |
| 866 | // as well as their commuted form. |
| 867 | if (m.left().IsWord32Shl() && m.right().IsWord32Shr()) { |
| 868 | shl = m.left().node(); |
| 869 | shr = m.right().node(); |
| 870 | } else if (m.left().IsWord32Shr() && m.right().IsWord32Shl()) { |
| 871 | shl = m.right().node(); |
| 872 | shr = m.left().node(); |
| 873 | } else { |
| 874 | return NoChange(); |
| 875 | } |
| 876 | |
| 877 | Int32BinopMatcher mshl(shl); |
| 878 | Int32BinopMatcher mshr(shr); |
| 879 | if (mshl.left().node() != mshr.left().node()) return NoChange(); |
| 880 | |
| 881 | if (mshl.right().HasValue() && mshr.right().HasValue()) { |
| 882 | // Case where y is a constant. |
| 883 | if (mshl.right().Value() + mshr.right().Value() != 32) return NoChange(); |
| 884 | } else { |
| 885 | Node* sub = NULL; |
| 886 | Node* y = NULL; |
| 887 | if (mshl.right().IsInt32Sub()) { |
| 888 | sub = mshl.right().node(); |
| 889 | y = mshr.right().node(); |
| 890 | } else if (mshr.right().IsInt32Sub()) { |
| 891 | sub = mshr.right().node(); |
| 892 | y = mshl.right().node(); |
| 893 | } else { |
| 894 | return NoChange(); |
| 895 | } |
| 896 | |
| 897 | Int32BinopMatcher msub(sub); |
| 898 | if (!msub.left().Is(32) || msub.right().node() != y) return NoChange(); |
| 899 | } |
| 900 | |
| 901 | node->set_op(machine()->Word32Ror()); |
| 902 | node->ReplaceInput(0, mshl.left().node()); |
| 903 | node->ReplaceInput(1, mshr.right().node()); |
| 904 | return Changed(node); |
| 905 | } |
| 906 | |
| 907 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 908 | CommonOperatorBuilder* MachineOperatorReducer::common() const { |
| 909 | return jsgraph()->common(); |
| 910 | } |
| 911 | |
| 912 | |
| 913 | MachineOperatorBuilder* MachineOperatorReducer::machine() const { |
| 914 | return jsgraph()->machine(); |
| 915 | } |
| 916 | |
| 917 | |
| 918 | Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); } |
| 919 | |
| 920 | } // namespace compiler |
| 921 | } // namespace internal |
| 922 | } // namespace v8 |