summaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/turboshaft/tag-untag-lowering-reducer.h
blob: 65b756aea77a7062598d41a1277744d9dd3f3b95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2023 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.

#ifndef V8_COMPILER_TURBOSHAFT_TAG_UNTAG_LOWERING_REDUCER_H_
#define V8_COMPILER_TURBOSHAFT_TAG_UNTAG_LOWERING_REDUCER_H_

#include "src/compiler/turboshaft/assembler.h"
#include "src/compiler/turboshaft/graph.h"
#include "src/compiler/turboshaft/operations.h"
#include "src/compiler/turboshaft/representations.h"

namespace v8::internal::compiler::turboshaft {

#include "src/compiler/turboshaft/define-assembler-macros.inc"

template <class Next>
class TagUntagLoweringReducer : public Next {
  static constexpr int kSmiShiftBits = kSmiShiftSize + kSmiTagSize;

 public:
  TURBOSHAFT_REDUCER_BOILERPLATE()

  template <class... Args>
  explicit TagUntagLoweringReducer(const std::tuple<Args...>& args)
      : Next(args) {}

  OpIndex ReduceTag(OpIndex input, TagKind kind) {
    DCHECK_EQ(kind, TagKind::kSmiTag);
    // Do shift on 32bit values if Smis are stored in the lower word.
    if constexpr (Is64() && SmiValuesAre31Bits()) {
      return ChangeTaggedInt32ToSmi(__ Word32ShiftLeft(input, kSmiShiftBits));
    } else {
      return V<Tagged>::Cast(
          __ WordPtrShiftLeft(__ ChangeInt32ToIntPtr(input), kSmiShiftBits));
    }
  }

  OpIndex ReduceUntag(OpIndex input, TagKind kind, RegisterRepresentation rep) {
    DCHECK_EQ(kind, TagKind::kSmiTag);
    DCHECK_EQ(rep, RegisterRepresentation::Word32());
    if constexpr (Is64() && SmiValuesAre31Bits()) {
      return __ Word32ShiftRightArithmeticShiftOutZeros(input, kSmiShiftBits);
    }
    return V<Word32>::Cast(
        __ WordPtrShiftRightArithmeticShiftOutZeros(input, kSmiShiftBits));
  }

 private:
  V<Tagged> ChangeTaggedInt32ToSmi(V<Word32> input) {
    DCHECK(SmiValuesAre31Bits());
    // In pointer compression, we smi-corrupt. Then, the upper bits are not
    // important.
    return COMPRESS_POINTERS_BOOL
               ? V<Tagged>::Cast(__ BitcastWord32ToWord64(input))
               : V<Tagged>::Cast(__ ChangeInt32ToIntPtr(input));
  }
};

#include "src/compiler/turboshaft/undef-assembler-macros.inc"

}  // namespace v8::internal::compiler::turboshaft

#endif  // V8_COMPILER_TURBOSHAFT_TAG_UNTAG_LOWERING_REDUCER_H_