1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_COMPILER_SIMD_SCALAR_LOWERING_H_
6 #define V8_COMPILER_SIMD_SCALAR_LOWERING_H_
7 
8 #include "src/compiler/common-operator.h"
9 #include "src/compiler/graph.h"
10 #include "src/compiler/machine-graph.h"
11 #include "src/compiler/machine-operator.h"
12 #include "src/compiler/node-marker.h"
13 #include "src/zone/zone-containers.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 template <typename T>
19 class Signature;
20 
21 namespace compiler {
22 
23 class SimdScalarLowering {
24  public:
25   SimdScalarLowering(MachineGraph* mcgraph,
26                      Signature<MachineRepresentation>* signature);
27 
28   void LowerGraph();
29 
30   int GetParameterCountAfterLowering();
31 
32  private:
33   enum class State : uint8_t { kUnvisited, kOnStack, kVisited };
34 
35   enum class SimdType : uint8_t { kFloat32x4, kInt32x4, kInt16x8, kInt8x16 };
36 
37 #if defined(V8_TARGET_BIG_ENDIAN)
38   static constexpr int kLaneOffsets[16] = {15, 14, 13, 12, 11, 10, 9, 8,
39                                            7,  6,  5,  4,  3,  2,  1, 0};
40 #else
41   static constexpr int kLaneOffsets[16] = {0, 1, 2,  3,  4,  5,  6,  7,
42                                            8, 9, 10, 11, 12, 13, 14, 15};
43 #endif
44   struct Replacement {
45     Node** node = nullptr;
46     SimdType type;  // represents output type
47     int num_replacements = 0;
48   };
49 
50   struct NodeState {
51     Node* node;
52     int input_index;
53   };
54 
zone()55   Zone* zone() const { return mcgraph_->zone(); }
graph()56   Graph* graph() const { return mcgraph_->graph(); }
machine()57   MachineOperatorBuilder* machine() const { return mcgraph_->machine(); }
common()58   CommonOperatorBuilder* common() const { return mcgraph_->common(); }
signature()59   Signature<MachineRepresentation>* signature() const { return signature_; }
60 
61   void LowerNode(Node* node);
62   bool DefaultLowering(Node* node);
63 
64   int NumLanes(SimdType type);
65   void ReplaceNode(Node* old, Node** new_nodes, int count);
66   bool HasReplacement(size_t index, Node* node);
67   Node** GetReplacements(Node* node);
68   int ReplacementCount(Node* node);
69   void Float32ToInt32(Node** replacements, Node** result);
70   void Int32ToFloat32(Node** replacements, Node** result);
71   Node** GetReplacementsWithType(Node* node, SimdType type);
72   SimdType ReplacementType(Node* node);
73   void PreparePhiReplacement(Node* phi);
74   void SetLoweredType(Node* node, Node* output);
75   void GetIndexNodes(Node* index, Node** new_indices, SimdType type);
76   void LowerLoadOp(MachineRepresentation rep, Node* node,
77                    const Operator* load_op, SimdType type);
78   void LowerStoreOp(MachineRepresentation rep, Node* node,
79                     const Operator* store_op, SimdType rep_type);
80   void LowerBinaryOp(Node* node, SimdType input_rep_type, const Operator* op,
81                      bool not_horizontal = true);
82   void LowerCompareOp(Node* node, SimdType input_rep_type, const Operator* op,
83                       bool invert_inputs = false);
84   Node* FixUpperBits(Node* input, int32_t shift);
85   void LowerBinaryOpForSmallInt(Node* node, SimdType input_rep_type,
86                                 const Operator* op, bool not_horizontal = true);
87   Node* Mask(Node* input, int32_t mask);
88   void LowerSaturateBinaryOp(Node* node, SimdType input_rep_type,
89                              const Operator* op, bool is_signed);
90   void LowerUnaryOp(Node* node, SimdType input_rep_type, const Operator* op);
91   void LowerIntMinMax(Node* node, const Operator* op, bool is_max,
92                       SimdType type);
93   void LowerConvertFromFloat(Node* node, bool is_signed);
94   void LowerConvertFromInt(Node* node, SimdType input_rep_type,
95                            SimdType output_rep_type, bool is_signed);
96   void LowerPack(Node* node, SimdType input_rep_type, SimdType output_rep_type,
97                  bool is_signed);
98   void LowerShiftOp(Node* node, SimdType type);
99   Node* BuildF64Trunc(Node* input);
100   void LowerNotEqual(Node* node, SimdType input_rep_type, const Operator* op);
101   MachineType MachineTypeFrom(SimdType simdType);
102 
103   MachineGraph* const mcgraph_;
104   NodeMarker<State> state_;
105   ZoneDeque<NodeState> stack_;
106   Replacement* replacements_;
107   Signature<MachineRepresentation>* signature_;
108   Node* placeholder_;
109   int parameter_count_after_lowering_;
110 };
111 
112 }  // namespace compiler
113 }  // namespace internal
114 }  // namespace v8
115 
116 #endif  // V8_COMPILER_SIMD_SCALAR_LOWERING_H_
117