simplified-operator-reducer.cc 9.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2014 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.

#include "src/compiler/simplified-operator-reducer.h"

#include "src/compiler/js-graph.h"
#include "src/compiler/machine-operator.h"
#include "src/compiler/node-matchers.h"
#include "src/compiler/operator-properties.h"
11
#include "src/compiler/simplified-operator.h"
12
#include "src/compiler/type-cache.h"
13
#include "src/numbers/conversions-inl.h"
14 15 16 17 18

namespace v8 {
namespace internal {
namespace compiler {

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
namespace {

Decision DecideObjectIsSmi(Node* const input) {
  NumberMatcher m(input);
  if (m.HasValue()) {
    return IsSmiDouble(m.Value()) ? Decision::kTrue : Decision::kFalse;
  }
  if (m.IsAllocate()) return Decision::kFalse;
  if (m.IsChangeBitToTagged()) return Decision::kFalse;
  if (m.IsChangeInt31ToTaggedSigned()) return Decision::kTrue;
  if (m.IsHeapConstant()) return Decision::kFalse;
  return Decision::kUnknown;
}

}  // namespace

35 36 37 38
SimplifiedOperatorReducer::SimplifiedOperatorReducer(Editor* editor,
                                                     JSGraph* jsgraph,
                                                     JSHeapBroker* broker)
    : AdvancedReducer(editor), jsgraph_(jsgraph), broker_(broker) {}
39

40
SimplifiedOperatorReducer::~SimplifiedOperatorReducer() = default;
41 42 43


Reduction SimplifiedOperatorReducer::Reduce(Node* node) {
44
  DisallowHeapAccess no_heap_access;
45 46
  switch (node->opcode()) {
    case IrOpcode::kBooleanNot: {
47
      // TODO(neis): Provide HeapObjectRefMatcher?
48
      HeapObjectMatcher m(node->InputAt(0));
49 50
      if (m.Is(factory()->true_value())) return ReplaceBoolean(false);
      if (m.Is(factory()->false_value())) return ReplaceBoolean(true);
51 52 53
      if (m.IsBooleanNot()) return Replace(m.InputAt(0));
      break;
    }
54
    case IrOpcode::kChangeBitToTagged: {
55 56 57
      Int32Matcher m(node->InputAt(0));
      if (m.Is(0)) return Replace(jsgraph()->FalseConstant());
      if (m.Is(1)) return Replace(jsgraph()->TrueConstant());
58
      if (m.IsChangeTaggedToBit()) return Replace(m.InputAt(0));
59 60
      break;
    }
61
    case IrOpcode::kChangeTaggedToBit: {
62
      HeapObjectMatcher m(node->InputAt(0));
63
      if (m.HasValue()) {
64
        return ReplaceInt32(m.Ref(broker()).BooleanValue());
65
      }
66
      if (m.IsChangeBitToTagged()) return Replace(m.InputAt(0));
67 68 69 70 71
      break;
    }
    case IrOpcode::kChangeFloat64ToTagged: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceNumber(m.Value());
72
      if (m.IsChangeTaggedToFloat64()) return Replace(m.node()->InputAt(0));
73 74
      break;
    }
75
    case IrOpcode::kChangeInt31ToTaggedSigned:
76 77 78
    case IrOpcode::kChangeInt32ToTagged: {
      Int32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceNumber(m.Value());
79 80 81
      if (m.IsChangeTaggedToInt32() || m.IsChangeTaggedSignedToInt32()) {
        return Replace(m.InputAt(0));
      }
82 83
      break;
    }
84 85
    case IrOpcode::kChangeTaggedToFloat64:
    case IrOpcode::kTruncateTaggedToFloat64: {
86 87
      NumberMatcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceFloat64(m.Value());
88 89 90
      if (m.IsChangeFloat64ToTagged() || m.IsChangeFloat64ToTaggedPointer()) {
        return Replace(m.node()->InputAt(0));
      }
91
      if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged()) {
92 93 94 95 96 97 98
        return Change(node, machine()->ChangeInt32ToFloat64(), m.InputAt(0));
      }
      if (m.IsChangeUint32ToTagged()) {
        return Change(node, machine()->ChangeUint32ToFloat64(), m.InputAt(0));
      }
      break;
    }
99
    case IrOpcode::kChangeTaggedSignedToInt32:
100 101 102
    case IrOpcode::kChangeTaggedToInt32: {
      NumberMatcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
103
      if (m.IsChangeFloat64ToTagged() || m.IsChangeFloat64ToTaggedPointer()) {
104 105
        return Change(node, machine()->ChangeFloat64ToInt32(), m.InputAt(0));
      }
106
      if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged()) {
107 108
        return Replace(m.InputAt(0));
      }
109 110 111 112 113
      break;
    }
    case IrOpcode::kChangeTaggedToUint32: {
      NumberMatcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceUint32(DoubleToUint32(m.Value()));
114
      if (m.IsChangeFloat64ToTagged() || m.IsChangeFloat64ToTaggedPointer()) {
115 116 117 118 119 120 121 122 123 124
        return Change(node, machine()->ChangeFloat64ToUint32(), m.InputAt(0));
      }
      if (m.IsChangeUint32ToTagged()) return Replace(m.InputAt(0));
      break;
    }
    case IrOpcode::kChangeUint32ToTagged: {
      Uint32Matcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceNumber(FastUI2D(m.Value()));
      break;
    }
125 126 127
    case IrOpcode::kTruncateTaggedToWord32: {
      NumberMatcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceInt32(DoubleToInt32(m.Value()));
128
      if (m.IsChangeInt31ToTaggedSigned() || m.IsChangeInt32ToTagged() ||
129 130 131
          m.IsChangeUint32ToTagged()) {
        return Replace(m.InputAt(0));
      }
132
      if (m.IsChangeFloat64ToTagged() || m.IsChangeFloat64ToTaggedPointer()) {
133 134 135 136
        return Change(node, machine()->TruncateFloat64ToWord32(), m.InputAt(0));
      }
      break;
    }
137 138 139 140 141 142 143 144 145
    case IrOpcode::kCheckedFloat64ToInt32: {
      Float64Matcher m(node->InputAt(0));
      if (m.HasValue() && IsInt32Double(m.Value())) {
        Node* value = jsgraph()->Int32Constant(static_cast<int32_t>(m.Value()));
        ReplaceWithValue(node, value);
        return Replace(value);
      }
      break;
    }
146
    case IrOpcode::kCheckedTaggedToArrayIndex:
147
    case IrOpcode::kCheckedTaggedToInt32:
148 149 150 151 152 153 154 155
    case IrOpcode::kCheckedTaggedSignedToInt32: {
      NodeMatcher m(node->InputAt(0));
      if (m.IsConvertTaggedHoleToUndefined()) {
        node->ReplaceInput(0, m.InputAt(0));
        return Changed(node);
      }
      break;
    }
156 157 158 159 160 161 162 163
    case IrOpcode::kCheckIf: {
      HeapObjectMatcher m(node->InputAt(0));
      if (m.Is(factory()->true_value())) {
        Node* const effect = NodeProperties::GetEffectInput(node);
        return Replace(effect);
      }
      break;
    }
164 165 166 167 168 169 170 171
    case IrOpcode::kCheckNumber: {
      NodeMatcher m(node->InputAt(0));
      if (m.IsConvertTaggedHoleToUndefined()) {
        node->ReplaceInput(0, m.InputAt(0));
        return Changed(node);
      }
      break;
    }
172
    case IrOpcode::kCheckHeapObject: {
173 174 175 176 177
      Node* const input = node->InputAt(0);
      if (DecideObjectIsSmi(input) == Decision::kFalse) {
        ReplaceWithValue(node, input);
        return Replace(input);
      }
178
      NodeMatcher m(input);
179
      if (m.IsCheckHeapObject()) {
180 181 182
        ReplaceWithValue(node, input);
        return Replace(input);
      }
183 184
      break;
    }
185
    case IrOpcode::kCheckSmi: {
186 187 188 189 190
      Node* const input = node->InputAt(0);
      if (DecideObjectIsSmi(input) == Decision::kTrue) {
        ReplaceWithValue(node, input);
        return Replace(input);
      }
191
      NodeMatcher m(input);
192
      if (m.IsCheckSmi()) {
193 194 195
        ReplaceWithValue(node, input);
        return Replace(input);
      } else if (m.IsConvertTaggedHoleToUndefined()) {
196 197 198
        node->ReplaceInput(0, m.InputAt(0));
        return Changed(node);
      }
199 200
      break;
    }
201
    case IrOpcode::kObjectIsSmi: {
202 203 204 205 206 207 208 209 210
      Node* const input = node->InputAt(0);
      switch (DecideObjectIsSmi(input)) {
        case Decision::kTrue:
          return ReplaceBoolean(true);
        case Decision::kFalse:
          return ReplaceBoolean(false);
        case Decision::kUnknown:
          break;
      }
211 212
      break;
    }
213 214 215 216 217
    case IrOpcode::kNumberAbs: {
      NumberMatcher m(node->InputAt(0));
      if (m.HasValue()) return ReplaceNumber(std::fabs(m.Value()));
      break;
    }
218 219 220
    case IrOpcode::kReferenceEqual: {
      HeapObjectBinopMatcher m(node);
      if (m.left().node() == m.right().node()) return ReplaceBoolean(true);
221 222
      break;
    }
223 224 225 226 227 228 229 230 231 232 233
    default:
      break;
  }
  return NoChange();
}

Reduction SimplifiedOperatorReducer::Change(Node* node, const Operator* op,
                                            Node* a) {
  DCHECK_EQ(node->InputCount(), OperatorProperties::GetTotalInputCount(op));
  DCHECK_LE(1, node->InputCount());
  node->ReplaceInput(0, a);
234
  NodeProperties::ChangeOp(node, op);
235 236 237
  return Changed(node);
}

238 239 240
Reduction SimplifiedOperatorReducer::ReplaceBoolean(bool value) {
  return Replace(jsgraph()->BooleanConstant(value));
}
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

Reduction SimplifiedOperatorReducer::ReplaceFloat64(double value) {
  return Replace(jsgraph()->Float64Constant(value));
}


Reduction SimplifiedOperatorReducer::ReplaceInt32(int32_t value) {
  return Replace(jsgraph()->Int32Constant(value));
}


Reduction SimplifiedOperatorReducer::ReplaceNumber(double value) {
  return Replace(jsgraph()->Constant(value));
}


Reduction SimplifiedOperatorReducer::ReplaceNumber(int32_t value) {
  return Replace(jsgraph()->Constant(value));
}

261
Factory* SimplifiedOperatorReducer::factory() const {
262
  return jsgraph()->isolate()->factory();
263
}
264 265 266 267 268 269 270

Graph* SimplifiedOperatorReducer::graph() const { return jsgraph()->graph(); }

MachineOperatorBuilder* SimplifiedOperatorReducer::machine() const {
  return jsgraph()->machine();
}

271 272 273 274
SimplifiedOperatorBuilder* SimplifiedOperatorReducer::simplified() const {
  return jsgraph()->simplified();
}

275 276 277
}  // namespace compiler
}  // namespace internal
}  // namespace v8