simplified-operator-reducer.cc 8.79 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/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 35 36
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

SimplifiedOperatorReducer::SimplifiedOperatorReducer(Editor* editor,
                                                     JSGraph* jsgraph)
37
    : AdvancedReducer(editor), jsgraph_(jsgraph) {}
38 39 40 41 42 43 44 45

SimplifiedOperatorReducer::~SimplifiedOperatorReducer() {}


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

232 233 234
Reduction SimplifiedOperatorReducer::ReplaceBoolean(bool value) {
  return Replace(jsgraph()->BooleanConstant(value));
}
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254

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));
}

255 256 257
Factory* SimplifiedOperatorReducer::factory() const {
  return isolate()->factory();
}
258 259 260

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

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

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

}  // namespace compiler
}  // namespace internal
}  // namespace v8