@@ -41,51 +41,28 @@ struct OptimiserState
41
41
std::back_insert_iterator<AssemblyItems> out;
42
42
};
43
43
44
- template <class Method , size_t Arguments>
45
- struct ApplyRule
44
+ template <typename FunctionType>
45
+ struct FunctionParameterCount ;
46
+ template <typename R, typename ... Args>
47
+ struct FunctionParameterCount <R(Args...)>
46
48
{
49
+ static constexpr auto value = sizeof ...(Args);
47
50
};
51
+
48
52
template <class Method >
49
- struct ApplyRule <Method, 4 >
50
- {
51
- static bool applyRule (AssemblyItems::const_iterator _in, std::back_insert_iterator<AssemblyItems> _out)
52
- {
53
- return Method::applySimple (_in[0 ], _in[1 ], _in[2 ], _in[3 ], _out);
54
- }
55
- };
56
- template <class Method >
57
- struct ApplyRule <Method, 3 >
58
- {
59
- static bool applyRule (AssemblyItems::const_iterator _in, std::back_insert_iterator<AssemblyItems> _out)
60
- {
61
- return Method::applySimple (_in[0 ], _in[1 ], _in[2 ], _out);
62
- }
63
- };
64
- template <class Method >
65
- struct ApplyRule <Method, 2 >
66
- {
67
- static bool applyRule (AssemblyItems::const_iterator _in, std::back_insert_iterator<AssemblyItems> _out)
68
- {
69
- return Method::applySimple (_in[0 ], _in[1 ], _out);
70
- }
71
- };
72
- template <class Method >
73
- struct ApplyRule <Method, 1 >
53
+ struct SimplePeepholeOptimizerMethod
74
54
{
75
- static bool applyRule (AssemblyItems::const_iterator _in, std::back_insert_iterator<AssemblyItems> _out)
55
+ template <size_t ... Indices>
56
+ static bool applyRule (AssemblyItems::const_iterator _in, back_insert_iterator<AssemblyItems> _out, index_sequence<Indices...>)
76
57
{
77
- return Method::applySimple (_in[0 ] , _out);
58
+ return Method::applySimple (_in[Indices]... , _out);
78
59
}
79
- };
80
-
81
- template <class Method , size_t WindowSize>
82
- struct SimplePeepholeOptimizerMethod
83
- {
84
60
static bool apply (OptimiserState& _state)
85
61
{
62
+ static constexpr size_t WindowSize = FunctionParameterCount<decltype (Method::applySimple)>::value - 1 ;
86
63
if (
87
64
_state.i + WindowSize <= _state.items .size () &&
88
- ApplyRule<Method, WindowSize>:: applyRule (_state.items .begin () + static_cast <ptrdiff_t >(_state.i ), _state.out )
65
+ applyRule (_state.items .begin () + static_cast <ptrdiff_t >(_state.i ), _state.out , make_index_sequence<WindowSize>{} )
89
66
)
90
67
{
91
68
_state.i += WindowSize;
@@ -96,7 +73,7 @@ struct SimplePeepholeOptimizerMethod
96
73
}
97
74
};
98
75
99
- struct Identity : SimplePeepholeOptimizerMethod<Identity, 1 >
76
+ struct Identity : SimplePeepholeOptimizerMethod<Identity>
100
77
{
101
78
static bool applySimple (AssemblyItem const & _item, std::back_insert_iterator<AssemblyItems> _out)
102
79
{
@@ -105,7 +82,7 @@ struct Identity: SimplePeepholeOptimizerMethod<Identity, 1>
105
82
}
106
83
};
107
84
108
- struct PushPop : SimplePeepholeOptimizerMethod<PushPop, 2 >
85
+ struct PushPop : SimplePeepholeOptimizerMethod<PushPop>
109
86
{
110
87
static bool applySimple (AssemblyItem const & _push, AssemblyItem const & _pop, std::back_insert_iterator<AssemblyItems>)
111
88
{
@@ -118,7 +95,7 @@ struct PushPop: SimplePeepholeOptimizerMethod<PushPop, 2>
118
95
}
119
96
};
120
97
121
- struct OpPop : SimplePeepholeOptimizerMethod<OpPop, 2 >
98
+ struct OpPop : SimplePeepholeOptimizerMethod<OpPop>
122
99
{
123
100
static bool applySimple (
124
101
AssemblyItem const & _op,
@@ -140,15 +117,15 @@ struct OpPop: SimplePeepholeOptimizerMethod<OpPop, 2>
140
117
}
141
118
};
142
119
143
- struct DoubleSwap : SimplePeepholeOptimizerMethod<DoubleSwap, 2 >
120
+ struct DoubleSwap : SimplePeepholeOptimizerMethod<DoubleSwap>
144
121
{
145
122
static size_t applySimple (AssemblyItem const & _s1, AssemblyItem const & _s2, std::back_insert_iterator<AssemblyItems>)
146
123
{
147
124
return _s1 == _s2 && SemanticInformation::isSwapInstruction (_s1);
148
125
}
149
126
};
150
127
151
- struct DoublePush : SimplePeepholeOptimizerMethod<DoublePush, 2 >
128
+ struct DoublePush : SimplePeepholeOptimizerMethod<DoublePush>
152
129
{
153
130
static bool applySimple (AssemblyItem const & _push1, AssemblyItem const & _push2, std::back_insert_iterator<AssemblyItems> _out)
154
131
{
@@ -163,7 +140,7 @@ struct DoublePush: SimplePeepholeOptimizerMethod<DoublePush, 2>
163
140
}
164
141
};
165
142
166
- struct CommutativeSwap : SimplePeepholeOptimizerMethod<CommutativeSwap, 2 >
143
+ struct CommutativeSwap : SimplePeepholeOptimizerMethod<CommutativeSwap>
167
144
{
168
145
static bool applySimple (AssemblyItem const & _swap, AssemblyItem const & _op, std::back_insert_iterator<AssemblyItems> _out)
169
146
{
@@ -181,7 +158,7 @@ struct CommutativeSwap: SimplePeepholeOptimizerMethod<CommutativeSwap, 2>
181
158
}
182
159
};
183
160
184
- struct SwapComparison : SimplePeepholeOptimizerMethod<SwapComparison, 2 >
161
+ struct SwapComparison : SimplePeepholeOptimizerMethod<SwapComparison>
185
162
{
186
163
static bool applySimple (AssemblyItem const & _swap, AssemblyItem const & _op, std::back_insert_iterator<AssemblyItems> _out)
187
164
{
@@ -207,7 +184,7 @@ struct SwapComparison: SimplePeepholeOptimizerMethod<SwapComparison, 2>
207
184
};
208
185
209
186
// / Remove swapN after dupN
210
- struct DupSwap : SimplePeepholeOptimizerMethod<DupSwap, 2 >
187
+ struct DupSwap : SimplePeepholeOptimizerMethod<DupSwap>
211
188
{
212
189
static size_t applySimple (
213
190
AssemblyItem const & _dupN,
@@ -230,7 +207,7 @@ struct DupSwap: SimplePeepholeOptimizerMethod<DupSwap, 2>
230
207
};
231
208
232
209
233
- struct IsZeroIsZeroJumpI : SimplePeepholeOptimizerMethod<IsZeroIsZeroJumpI, 4 >
210
+ struct IsZeroIsZeroJumpI : SimplePeepholeOptimizerMethod<IsZeroIsZeroJumpI>
234
211
{
235
212
static size_t applySimple (
236
213
AssemblyItem const & _iszero1,
@@ -256,7 +233,7 @@ struct IsZeroIsZeroJumpI: SimplePeepholeOptimizerMethod<IsZeroIsZeroJumpI, 4>
256
233
}
257
234
};
258
235
259
- struct JumpToNext : SimplePeepholeOptimizerMethod<JumpToNext, 3 >
236
+ struct JumpToNext : SimplePeepholeOptimizerMethod<JumpToNext>
260
237
{
261
238
static size_t applySimple (
262
239
AssemblyItem const & _pushTag,
@@ -282,7 +259,7 @@ struct JumpToNext: SimplePeepholeOptimizerMethod<JumpToNext, 3>
282
259
}
283
260
};
284
261
285
- struct TagConjunctions : SimplePeepholeOptimizerMethod<TagConjunctions, 3 >
262
+ struct TagConjunctions : SimplePeepholeOptimizerMethod<TagConjunctions>
286
263
{
287
264
static bool applySimple (
288
265
AssemblyItem const & _pushTag,
@@ -317,7 +294,7 @@ struct TagConjunctions: SimplePeepholeOptimizerMethod<TagConjunctions, 3>
317
294
}
318
295
};
319
296
320
- struct TruthyAnd : SimplePeepholeOptimizerMethod<TruthyAnd, 3 >
297
+ struct TruthyAnd : SimplePeepholeOptimizerMethod<TruthyAnd>
321
298
{
322
299
static bool applySimple (
323
300
AssemblyItem const & _push,
0 commit comments