5 #include "src/compiler/constant-folding-reducer.h" 7 #include "src/compiler/js-graph.h" 8 #include "src/objects-inl.h" 14 ConstantFoldingReducer::ConstantFoldingReducer(Editor* editor, JSGraph* jsgraph,
16 : AdvancedReducer(editor), jsgraph_(jsgraph), broker_(broker) {}
18 ConstantFoldingReducer::~ConstantFoldingReducer() =
default;
20 Reduction ConstantFoldingReducer::Reduce(Node* node) {
21 DisallowHeapAccess no_heap_access;
24 if (!NodeProperties::IsConstant(node) && NodeProperties::IsTyped(node) &&
25 node->op()->HasProperty(Operator::kEliminatable)) {
29 if (node->opcode() == IrOpcode::kFinishRegion)
return NoChange();
34 Type upper = NodeProperties::GetType(node);
35 if (!upper.IsNone()) {
36 Node* replacement =
nullptr;
37 if (upper.IsHeapConstant()) {
38 replacement = jsgraph()->Constant(upper.AsHeapConstant()->Ref());
39 }
else if (upper.Is(Type::MinusZero())) {
40 Factory* factory = jsgraph()->isolate()->factory();
41 ObjectRef minus_zero(broker(), factory->minus_zero_value());
42 replacement = jsgraph()->Constant(minus_zero);
43 }
else if (upper.Is(Type::NaN())) {
44 replacement = jsgraph()->NaNConstant();
45 }
else if (upper.Is(Type::Null())) {
46 replacement = jsgraph()->NullConstant();
47 }
else if (upper.Is(Type::PlainNumber()) && upper.Min() == upper.Max()) {
48 replacement = jsgraph()->Constant(upper.Min());
49 }
else if (upper.Is(Type::Undefined())) {
50 replacement = jsgraph()->UndefinedConstant();
54 if (!NodeProperties::IsTyped(replacement)) {
55 NodeProperties::SetType(replacement, upper);
57 ReplaceWithValue(node, replacement);
58 return Changed(replacement);