decompression-elimination.cc 8.74 KB
Newer Older
1 2 3 4 5
// Copyright 2019 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/decompression-elimination.h"
6
#include "src/compiler/node-properties.h"
7 8 9 10 11

namespace v8 {
namespace internal {
namespace compiler {

12
DecompressionElimination::DecompressionElimination(
13 14 15 16 17 18 19
    Editor* editor, Graph* graph, MachineOperatorBuilder* machine,
    CommonOperatorBuilder* common)
    : AdvancedReducer(editor),
      graph_(graph),
      machine_(machine),
      common_(common) {}

20
bool DecompressionElimination::IsReducibleConstantOpcode(
21 22 23
    IrOpcode::Value opcode) {
  switch (opcode) {
    case IrOpcode::kInt64Constant:
24
    case IrOpcode::kHeapConstant:
25
      return true;
26 27 28 29
    default:
      return false;
  }
}
30 31 32 33 34 35 36 37

bool DecompressionElimination::IsValidDecompress(
    IrOpcode::Value compressOpcode, IrOpcode::Value decompressOpcode) {
  switch (compressOpcode) {
    case IrOpcode::kChangeTaggedToCompressed:
      return IrOpcode::IsDecompressOpcode(decompressOpcode);
    case IrOpcode::kChangeTaggedSignedToCompressedSigned:
      return decompressOpcode ==
38 39
                 IrOpcode::kChangeCompressedSignedToTaggedSigned ||
             decompressOpcode == IrOpcode::kChangeCompressedToTagged;
40 41 42 43 44
    case IrOpcode::kChangeTaggedPointerToCompressedPointer:
      return decompressOpcode ==
                 IrOpcode::kChangeCompressedPointerToTaggedPointer ||
             decompressOpcode == IrOpcode::kChangeCompressedToTagged;
    default:
45
      UNREACHABLE();
46 47 48
  }
}

49 50 51 52 53 54 55
Node* DecompressionElimination::GetCompressedConstant(Node* constant) {
  switch (constant->opcode()) {
    case IrOpcode::kInt64Constant:
      return graph()->NewNode(common()->Int32Constant(
          static_cast<int32_t>(OpParameter<int64_t>(constant->op()))));
      break;
    case IrOpcode::kHeapConstant:
56 57
      return graph()->NewNode(
          common()->CompressedHeapConstant(HeapConstantOf(constant->op())));
58 59 60 61 62
    default:
      UNREACHABLE();
  }
}

63 64 65 66 67
Reduction DecompressionElimination::ReduceCompress(Node* node) {
  DCHECK(IrOpcode::IsCompressOpcode(node->opcode()));

  DCHECK_EQ(node->InputCount(), 1);
  Node* input_node = node->InputAt(0);
68 69 70
  IrOpcode::Value input_opcode = input_node->opcode();
  if (IrOpcode::IsDecompressOpcode(input_opcode)) {
    DCHECK(IsValidDecompress(node->opcode(), input_opcode));
71 72
    DCHECK_EQ(input_node->InputCount(), 1);
    return Replace(input_node->InputAt(0));
73 74
  } else if (IsReducibleConstantOpcode(input_opcode)) {
    return Replace(GetCompressedConstant(input_node));
75 76 77 78 79
  } else {
    return NoChange();
  }
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
Reduction DecompressionElimination::ReduceDecompress(Node* node) {
  DCHECK(IrOpcode::IsDecompressOpcode(node->opcode()));

  DCHECK_EQ(node->InputCount(), 1);
  Node* input_node = node->InputAt(0);
  IrOpcode::Value input_opcode = input_node->opcode();
  if (IrOpcode::IsCompressOpcode(input_opcode)) {
    DCHECK(IsValidDecompress(input_opcode, node->opcode()));
    DCHECK_EQ(input_node->InputCount(), 1);
    return Replace(input_node->InputAt(0));
  } else {
    return NoChange();
  }
}

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
Reduction DecompressionElimination::ReducePhi(Node* node) {
  DCHECK_EQ(node->opcode(), IrOpcode::kPhi);

  const int value_input_count = node->op()->ValueInputCount();

  // Check if all input are decompress nodes, and if all are the same.
  bool same_decompresses = true;
  IrOpcode::Value first_opcode = node->InputAt(0)->opcode();
  for (int i = 0; i < value_input_count; ++i) {
    Node* input = node->InputAt(i);
    if (IrOpcode::IsDecompressOpcode(input->opcode())) {
      same_decompresses &= first_opcode == input->opcode();
    } else {
      return NoChange();
    }
  }

  // By now, we know that all inputs are decompress nodes. If all are the same,
  // we can grab the first one to be used after the Phi. If we have different
  // Decompress nodes as inputs, we need to use a conservative decompression
  // after the Phi.
  const Operator* op;
  if (same_decompresses) {
    op = node->InputAt(0)->op();
  } else {
    op = machine()->ChangeCompressedToTagged();
  }

  // Rewire phi's inputs to be the compressed inputs.
  for (int i = 0; i < value_input_count; ++i) {
    Node* input = node->InputAt(i);
    DCHECK_EQ(input->InputCount(), 1);
    node->ReplaceInput(i, input->InputAt(0));
  }

  // Update the MachineRepresentation on the Phi.
  MachineRepresentation rep;
  switch (op->opcode()) {
    case IrOpcode::kChangeCompressedToTagged:
      rep = MachineRepresentation::kCompressed;
      break;
    case IrOpcode::kChangeCompressedSignedToTaggedSigned:
      rep = MachineRepresentation::kCompressedSigned;
      break;
    case IrOpcode::kChangeCompressedPointerToTaggedPointer:
      rep = MachineRepresentation::kCompressedPointer;
      break;
    default:
      UNREACHABLE();
  }
  NodeProperties::ChangeOp(node, common()->Phi(rep, value_input_count));

  // Add a decompress after the Phi. To do this, we need to replace the Phi with
  // "Phi <- Decompress".
149 150 151 152
  Node* decompress = graph()->NewNode(op, node);
  ReplaceWithValue(node, decompress);
  decompress->ReplaceInput(0, node);
  return Changed(node);
153 154
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
Reduction DecompressionElimination::ReduceTypedStateValues(Node* node) {
  DCHECK_EQ(node->opcode(), IrOpcode::kTypedStateValues);

  bool any_change = false;
  for (int i = 0; i < node->InputCount(); ++i) {
    Node* input = node->InputAt(i);
    if (IrOpcode::IsDecompressOpcode(input->opcode())) {
      DCHECK_EQ(input->InputCount(), 1);
      node->ReplaceInput(i, input->InputAt(0));
      any_change = true;
    }
  }
  return any_change ? Changed(node) : NoChange();
}

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
Reduction DecompressionElimination::ReduceWord32Equal(Node* node) {
  DCHECK_EQ(node->opcode(), IrOpcode::kWord32Equal);

  DCHECK_EQ(node->InputCount(), 2);
  Node* lhs = node->InputAt(0);
  Node* rhs = node->InputAt(1);

  if (!IrOpcode::IsCompressOpcode(lhs->opcode()) ||
      !IrOpcode::IsCompressOpcode(rhs->opcode())) {
    return NoChange();
  }
  // Input nodes for compress operation.
  lhs = lhs->InputAt(0);
  rhs = rhs->InputAt(0);

  bool changed = false;

  if (lhs->opcode() == IrOpcode::kBitcastWordToTaggedSigned) {
    Node* input = lhs->InputAt(0);
    if (IsReducibleConstantOpcode(input->opcode())) {
      node->ReplaceInput(0, GetCompressedConstant(input));
      changed = true;
    }
  }

  if (rhs->opcode() == IrOpcode::kBitcastWordToTaggedSigned) {
    Node* input = rhs->InputAt(0);
    if (IsReducibleConstantOpcode(input->opcode())) {
      node->ReplaceInput(1, GetCompressedConstant(input));
      changed = true;
    }
  }

  return changed ? Changed(node) : NoChange();
}

206 207 208 209 210 211 212
Reduction DecompressionElimination::ReduceWord64Equal(Node* node) {
  DCHECK_EQ(node->opcode(), IrOpcode::kWord64Equal);

  DCHECK_EQ(node->InputCount(), 2);
  Node* lhs = node->InputAt(0);
  Node* rhs = node->InputAt(1);

213 214 215 216 217
  bool lhs_is_decompress = IrOpcode::IsDecompressOpcode(lhs->opcode());
  bool rhs_is_decompress = IrOpcode::IsDecompressOpcode(rhs->opcode());

  // Case where both of its inputs are Decompress nodes.
  if (lhs_is_decompress && rhs_is_decompress) {
218 219 220 221 222 223 224 225
    DCHECK_EQ(lhs->InputCount(), 1);
    node->ReplaceInput(0, lhs->InputAt(0));
    DCHECK_EQ(rhs->InputCount(), 1);
    node->ReplaceInput(1, rhs->InputAt(0));
    NodeProperties::ChangeOp(node, machine()->Word32Equal());
    return Changed(node);
  }

226 227
  bool lhs_is_constant = IsReducibleConstantOpcode(lhs->opcode());
  bool rhs_is_constant = IsReducibleConstantOpcode(rhs->opcode());
228 229 230 231 232 233 234 235 236 237 238 239

  // Case where one input is a Decompress node and the other a constant.
  if ((lhs_is_decompress && rhs_is_constant) ||
      (lhs_is_constant && rhs_is_decompress)) {
    node->ReplaceInput(
        0, lhs_is_decompress ? lhs->InputAt(0) : GetCompressedConstant(lhs));
    node->ReplaceInput(
        1, lhs_is_decompress ? GetCompressedConstant(rhs) : rhs->InputAt(0));
    NodeProperties::ChangeOp(node, machine()->Word32Equal());
    return Changed(node);
  }

240 241 242
  return NoChange();
}

243 244 245 246 247 248 249 250
Reduction DecompressionElimination::Reduce(Node* node) {
  DisallowHeapAccess no_heap_access;

  switch (node->opcode()) {
    case IrOpcode::kChangeTaggedToCompressed:
    case IrOpcode::kChangeTaggedSignedToCompressedSigned:
    case IrOpcode::kChangeTaggedPointerToCompressedPointer:
      return ReduceCompress(node);
251 252 253 254
    case IrOpcode::kChangeCompressedToTagged:
    case IrOpcode::kChangeCompressedSignedToTaggedSigned:
    case IrOpcode::kChangeCompressedPointerToTaggedPointer:
      return ReduceDecompress(node);
255 256
    case IrOpcode::kPhi:
      return ReducePhi(node);
257 258
    case IrOpcode::kTypedStateValues:
      return ReduceTypedStateValues(node);
259 260
    case IrOpcode::kWord32Equal:
      return ReduceWord32Equal(node);
261 262
    case IrOpcode::kWord64Equal:
      return ReduceWord64Equal(node);
263 264 265
    default:
      break;
  }
266

267 268 269 270 271 272
  return NoChange();
}

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