1 // Copyright 2014 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_MACHINE_OPERATOR_REDUCER_H_ 6 #define V8_COMPILER_MACHINE_OPERATOR_REDUCER_H_ 7 8 #include "src/base/compiler-specific.h" 9 #include "src/common/globals.h" 10 #include "src/compiler/graph-reducer.h" 11 #include "src/compiler/machine-operator.h" 12 13 namespace v8 { 14 namespace internal { 15 namespace compiler { 16 17 // Forward declarations. 18 class CommonOperatorBuilder; 19 class MachineGraph; 20 class Word32Adapter; 21 class Word64Adapter; 22 23 // Performs constant folding and strength reduction on nodes that have 24 // machine operators. 25 class V8_EXPORT_PRIVATE MachineOperatorReducer final NON_EXPORTED_BASE(AdvancedReducer)26 : public NON_EXPORTED_BASE(AdvancedReducer) { 27 public: 28 explicit MachineOperatorReducer(Editor* editor, MachineGraph* mcgraph, 29 bool allow_signalling_nan = true); 30 ~MachineOperatorReducer() override; 31 32 const char* reducer_name() const override { return "MachineOperatorReducer"; } 33 34 Reduction Reduce(Node* node) override; 35 36 private: 37 friend class Word32Adapter; 38 friend class Word64Adapter; 39 40 Node* Float32Constant(volatile float value); 41 Node* Float64Constant(volatile double value); 42 Node* Int32Constant(int32_t value); 43 Node* Int64Constant(int64_t value); 44 Node* Uint32Constant(uint32_t value) { 45 return Int32Constant(bit_cast<int32_t>(value)); 46 } 47 Node* Uint64Constant(uint64_t value) { 48 return Int64Constant(bit_cast<int64_t>(value)); 49 } 50 Node* Float64Mul(Node* lhs, Node* rhs); 51 Node* Float64PowHalf(Node* value); 52 Node* Word32And(Node* lhs, Node* rhs); 53 Node* Word32And(Node* lhs, uint32_t rhs) { 54 return Word32And(lhs, Uint32Constant(rhs)); 55 } 56 Node* Word32Sar(Node* lhs, uint32_t rhs); 57 Node* Word32Shr(Node* lhs, uint32_t rhs); 58 Node* Word32Equal(Node* lhs, Node* rhs); 59 Node* Word64And(Node* lhs, Node* rhs); 60 Node* Int32Add(Node* lhs, Node* rhs); 61 Node* Int32Sub(Node* lhs, Node* rhs); 62 Node* Int32Mul(Node* lhs, Node* rhs); 63 Node* Int32Div(Node* dividend, int32_t divisor); 64 Node* Uint32Div(Node* dividend, uint32_t divisor); 65 Node* TruncateInt64ToInt32(Node* value); 66 67 Reduction ReplaceBool(bool value) { return ReplaceInt32(value ? 1 : 0); } 68 Reduction ReplaceFloat32(volatile float value) { 69 return Replace(Float32Constant(value)); 70 } 71 Reduction ReplaceFloat64(volatile double value) { 72 return Replace(Float64Constant(value)); 73 } 74 Reduction ReplaceInt32(int32_t value) { 75 return Replace(Int32Constant(value)); 76 } 77 Reduction ReplaceUint32(uint32_t value) { 78 return Replace(Uint32Constant(value)); 79 } 80 Reduction ReplaceInt64(int64_t value) { 81 return Replace(Int64Constant(value)); 82 } 83 84 Reduction ReduceInt32Add(Node* node); 85 Reduction ReduceInt64Add(Node* node); 86 Reduction ReduceInt32Sub(Node* node); 87 Reduction ReduceInt64Sub(Node* node); 88 Reduction ReduceInt64Mul(Node* node); 89 Reduction ReduceInt32Div(Node* node); 90 Reduction ReduceUint32Div(Node* node); 91 Reduction ReduceInt32Mod(Node* node); 92 Reduction ReduceUint32Mod(Node* node); 93 Reduction ReduceStore(Node* node); 94 Reduction ReduceProjection(size_t index, Node* node); 95 const Operator* Map64To32Comparison(const Operator* op, bool sign_extended); 96 Reduction ReduceWord32Comparisons(Node* node); 97 Reduction ReduceWord64Comparisons(Node* node); 98 Reduction ReduceWord32Shifts(Node* node); 99 Reduction ReduceWord32Shl(Node* node); 100 Reduction ReduceWord64Shl(Node* node); 101 Reduction ReduceWord32Shr(Node* node); 102 Reduction ReduceWord64Shr(Node* node); 103 Reduction ReduceWord32Sar(Node* node); 104 Reduction ReduceWord64Sar(Node* node); 105 Reduction ReduceWord32And(Node* node); 106 Reduction ReduceWord64And(Node* node); 107 Reduction TryMatchWord32Ror(Node* node); 108 Reduction ReduceWord32Or(Node* node); 109 Reduction ReduceWord64Or(Node* node); 110 Reduction ReduceWord32Xor(Node* node); 111 Reduction ReduceWord64Xor(Node* node); 112 Reduction ReduceWord32Equal(Node* node); 113 Reduction ReduceFloat64InsertLowWord32(Node* node); 114 Reduction ReduceFloat64InsertHighWord32(Node* node); 115 Reduction ReduceFloat64Compare(Node* node); 116 Reduction ReduceFloat64RoundDown(Node* node); 117 Reduction ReduceTruncateInt64ToInt32(Node* node); 118 Reduction ReduceConditional(Node* node); 119 120 Graph* graph() const; 121 MachineGraph* mcgraph() const { return mcgraph_; } 122 CommonOperatorBuilder* common() const; 123 MachineOperatorBuilder* machine() const; 124 125 // These reductions can be applied to operations of different word sizes. 126 // Use Word32Adapter or Word64Adapter to specialize for a particular one. 127 template <typename WordNAdapter> 128 Reduction ReduceWordNAnd(Node* node); 129 template <typename WordNAdapter> 130 Reduction ReduceWordNOr(Node* node); 131 template <typename WordNAdapter> 132 Reduction ReduceWordNXor(Node* node); 133 134 // Tries to simplify "if(x == 0)" by removing the "== 0" and inverting 135 // branches. 136 Reduction SimplifyBranch(Node* node); 137 // Helper for SimplifyBranch; swaps the if/else of a branch. 138 void SwapBranches(Node* node); 139 140 // Helper for ReduceConditional. Does not perform the actual reduction; just 141 // returns a new Node that could be used as the input to the condition. 142 template <typename WordNAdapter> 143 base::Optional<Node*> ReduceConditionalN(Node* node); 144 145 // Helper for finding a reduced equality condition. Does not perform the 146 // actual reduction; just returns a new pair that could be compared for the 147 // same outcome. 148 template <typename WordNAdapter> 149 base::Optional<std::pair<Node*, uint32_t>> ReduceWord32EqualForConstantRhs( 150 Node* lhs, uint32_t rhs); 151 152 MachineGraph* mcgraph_; 153 bool allow_signalling_nan_; 154 }; 155 156 } // namespace compiler 157 } // namespace internal 158 } // namespace v8 159 160 #endif // V8_COMPILER_MACHINE_OPERATOR_REDUCER_H_ 161