Debian Codegraph @

test/opt/fold_test.cpp

raw
1 // Copyright (c) 2016 Google Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 #include "source/opt/fold.h"
15
16 #include <limits>
17 #include <memory>
18 #include <string>
19 #include <vector>
20
21 #include "effcee/effcee.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "source/opt/build_module.h"
25 #include "source/opt/def_use_manager.h"
26 #include "source/opt/ir_context.h"
27 #include "source/opt/module.h"
28 #include "spirv-tools/libspirv.hpp"
29
30 namespace spvtools {
31 namespace opt {
32 namespace {
33
34 using ::testing::Contains;
35
36 std::string Disassemble(const std::string& original, IRContext* context,
37 uint32_t disassemble_options = 0) {
38 std::vector<uint32_t> optimized_bin;
39 context->module()->ToBinary(&optimized_bin, true);
40 spv_target_env target_env = SPV_ENV_UNIVERSAL_1_2;
41 SpirvTools tools(target_env);
42 std::string optimized_asm;
43 EXPECT_TRUE(
44 tools.Disassemble(optimized_bin, &optimized_asm, disassemble_options))
45 << "Disassembling failed for shader:\n"
46 << original << std::endl;
47 return optimized_asm;
48 }
49
50 void Match(const std::string& original, IRContext* context,
51 uint32_t disassemble_options = 0) {
52 std::string disassembly = Disassemble(original, context, disassemble_options);
53 auto match_result = effcee::Match(disassembly, original);
54 EXPECT_EQ(effcee::Result::Status::Ok, match_result.status())
55 << match_result.message() << "\nChecking result:\n"
56 << disassembly;
57 }
58
59 template <class ResultType>
60 struct InstructionFoldingCase {
61 InstructionFoldingCase(const std::string& tb, uint32_t id, ResultType result)
62 : test_body(tb), id_to_fold(id), expected_result(result) {}
63
64 std::string test_body;
65 uint32_t id_to_fold;
66 ResultType expected_result;
67 };
68
69 std::tuple<std::unique_ptr<IRContext>, Instruction*> GetInstructionToFold(
70 const std::string test_body, const uint32_t id_to_fold,
71 spv_target_env spv_env) {
72 // Build module.
73 std::unique_ptr<IRContext> context =
74 BuildModule(spv_env, nullptr, test_body,
75 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
76 EXPECT_NE(nullptr, context);
77 if (context == nullptr) {
78 return {nullptr, nullptr};
79 }
80
81 // Fold the instruction to test.
82 if (id_to_fold != 0) {
83 analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
84 Instruction* inst = def_use_mgr->GetDef(id_to_fold);
85 return {std::move(context), inst};
86 }
87
88 // If there is not ID, we get the instruction just before a terminator
89 // instruction. That could be a return or abort. This is used for cases where
90 // the instruction we want to fold does not have a result id.
91 Function* func = &*context->module()->begin();
92 for (auto& bb : *func) {
93 Instruction* terminator = bb.terminator();
94 if (terminator->IsReturnOrAbort()) {
95 return {std::move(context), terminator->PreviousNode()};
96 }
97 }
98 return {nullptr, nullptr};
99 }
100
101 std::tuple<std::unique_ptr<IRContext>, Instruction*> FoldInstruction(
102 const std::string test_body, const uint32_t id_to_fold,
103 spv_target_env spv_env) {
104 // Build module.
105 std::unique_ptr<IRContext> context;
106 Instruction* inst = nullptr;
107 std::tie(context, inst) =
108 GetInstructionToFold(test_body, id_to_fold, spv_env);
109
110 if (context == nullptr) {
111 return {nullptr, nullptr};
112 }
113
114 std::unique_ptr<Instruction> original_inst(inst->Clone(context.get()));
115 bool succeeded = context->get_instruction_folder().FoldInstruction(inst);
116 EXPECT_EQ(inst->result_id(), original_inst->result_id());
117 EXPECT_EQ(inst->type_id(), original_inst->type_id());
118
119 if (!succeeded && inst != nullptr) {
120 EXPECT_EQ(inst->NumInOperands(), original_inst->NumInOperands());
121 for (uint32_t i = 0; i < inst->NumInOperands(); ++i) {
122 EXPECT_EQ(inst->GetOperand(i), original_inst->GetOperand(i));
123 }
124 }
125
126 return {std::move(context), succeeded ? inst : nullptr};
127 }
128
129 template <class ElementType, class Function>
130 void CheckForExpectedScalarConstant(Instruction* inst,
131 ElementType expected_result,
132 Function GetValue) {
133 ASSERT_TRUE(inst);
134
135 IRContext* context = inst->context();
136 analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
137 while (inst->opcode() == spv::Op::OpCopyObject) {
138 inst = def_use_mgr->GetDef(inst->GetSingleWordInOperand(0));
139 }
140
141 // Make sure we have a constant.
142 analysis::ConstantManager* const_mrg = context->get_constant_mgr();
143 const analysis::Constant* constant = const_mrg->GetConstantFromInst(inst);
144 ASSERT_TRUE(constant);
145
146 // Make sure the constant is a scalar.
147 const analysis::ScalarConstant* result = constant->AsScalarConstant();
148 ASSERT_TRUE(result);
149
150 // Check if the result matches the expected value.
151 // If ExpectedType is not a float type, it should cast the value to a double
152 // and never get a nan.
153 if (!std::isnan(static_cast<double>(expected_result))) {
154 EXPECT_EQ(expected_result, GetValue(result));
155 } else {
156 EXPECT_TRUE(std::isnan(static_cast<double>(GetValue(result))));
157 }
158 }
159
160 template <class ElementType, class Function>
161 void CheckForExpectedVectorConstant(Instruction* inst,
162 std::vector<ElementType> expected_result,
163 Function GetValue) {
164 ASSERT_TRUE(inst);
165
166 IRContext* context = inst->context();
167 EXPECT_EQ(inst->opcode(), spv::Op::OpCopyObject);
168 analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
169 inst = def_use_mgr->GetDef(inst->GetSingleWordInOperand(0));
170 std::vector<spv::Op> opcodes = {spv::Op::OpConstantComposite};
171 EXPECT_THAT(opcodes, Contains(inst->opcode()));
172 analysis::ConstantManager* const_mrg = context->get_constant_mgr();
173 const analysis::Constant* result = const_mrg->GetConstantFromInst(inst);
174 EXPECT_NE(result, nullptr);
175 if (result != nullptr) {
176 const std::vector<const analysis::Constant*>& componenets =
177 result->AsVectorConstant()->GetComponents();
178 EXPECT_EQ(componenets.size(), expected_result.size());
179 for (size_t i = 0; i < componenets.size(); i++) {
180 EXPECT_EQ(expected_result[i], GetValue(componenets[i]));
181 }
182 }
183 }
184
185 using IntegerInstructionFoldingTest =
186 ::testing::TestWithParam<InstructionFoldingCase<uint32_t>>;
187
188 TEST_P(IntegerInstructionFoldingTest, Case) {
189 const auto& tc = GetParam();
190
191 std::unique_ptr<IRContext> context;
192 Instruction* inst;
193 std::tie(context, inst) =
194 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
195 CheckForExpectedScalarConstant(
196 inst, tc.expected_result, [](const analysis::Constant* c) {
197 return c->AsScalarConstant()->GetU32BitValue();
198 });
199 }
200
201 // Returns a common SPIR-V header for all of the test that follow.
202 #define INT_0_ID 100
203 #define TRUE_ID 101
204 #define VEC2_0_ID 102
205 #define INT_7_ID 103
206 #define FLOAT_0_ID 104
207 #define DOUBLE_0_ID 105
208 #define VEC4_0_ID 106
209 #define DVEC4_0_ID 106
210 #define HALF_0_ID 108
211 #define UINT_0_ID 109
212 #define INT_NULL_ID 110
213 #define UINT_NULL_ID 111
214 #define HALF_3_ID 112
215 #define FLOAT_NULL_ID 113
216 const std::string& Header() {
217 static const std::string header = R"(OpCapability Shader
218 OpCapability Float16
219 OpCapability Float64
220 OpCapability Int8
221 OpCapability Int16
222 OpCapability Int64
223 OpCapability CooperativeMatrixKHR
224 OpExtension "SPV_KHR_cooperative_matrix"
225 %1 = OpExtInstImport "GLSL.std.450"
226 OpMemoryModel Logical GLSL450
227 OpEntryPoint Fragment %main "main"
228 OpExecutionMode %main OriginUpperLeft
229 OpSource GLSL 140
230 OpName %main "main"
231 %void = OpTypeVoid
232 %void_func = OpTypeFunction %void
233 %bool = OpTypeBool
234 %float = OpTypeFloat 32
235 %double = OpTypeFloat 64
236 %half = OpTypeFloat 16
237 %101 = OpConstantTrue %bool ; Need a def with an numerical id to define id maps.
238 %true = OpConstantTrue %bool
239 %false = OpConstantFalse %bool
240 %bool_null = OpConstantNull %bool
241 %short = OpTypeInt 16 1
242 %ushort = OpTypeInt 16 0
243 %byte = OpTypeInt 8 1
244 %ubyte = OpTypeInt 8 0
245 %int = OpTypeInt 32 1
246 %long = OpTypeInt 64 1
247 %uint = OpTypeInt 32 0
248 %ulong = OpTypeInt 64 0
249 %v2int = OpTypeVector %int 2
250 %v4int = OpTypeVector %int 4
251 %v2ubyte = OpTypeVector %ubyte 2
252 %v4ubyte = OpTypeVector %ubyte 4
253 %v4byte = OpTypeVector %byte 4
254 %v2ushort = OpTypeVector %ushort 2
255 %v4ushort = OpTypeVector %ushort 4
256 %v2short = OpTypeVector %short 2
257 %v2long = OpTypeVector %long 2
258 %v4long = OpTypeVector %long 4
259 %v4float = OpTypeVector %float 4
260 %v4double = OpTypeVector %double 4
261 %v4uint = OpTypeVector %uint 4
262 %v2uint = OpTypeVector %uint 2
263 %v2ulong = OpTypeVector %ulong 2
264 %v2float = OpTypeVector %float 2
265 %v2double = OpTypeVector %double 2
266 %v2half = OpTypeVector %half 2
267 %v4half = OpTypeVector %half 4
268 %v2bool = OpTypeVector %bool 2
269 %m2x2int = OpTypeMatrix %v2int 2
270 %mat4v2float = OpTypeMatrix %v2float 4
271 %mat2v4float = OpTypeMatrix %v4float 2
272 %mat4v4float = OpTypeMatrix %v4float 4
273 %mat4v4double = OpTypeMatrix %v4double 4
274 %struct_v2int_int_int = OpTypeStruct %v2int %int %int
275 %_ptr_int = OpTypePointer Function %int
276 %_ptr_uint = OpTypePointer Function %uint
277 %_ptr_bool = OpTypePointer Function %bool
278 %_ptr_float = OpTypePointer Function %float
279 %_ptr_double = OpTypePointer Function %double
280 %_ptr_half = OpTypePointer Function %half
281 %_ptr_long = OpTypePointer Function %long
282 %_ptr_ulong = OpTypePointer Function %ulong
283 %_ptr_v2int = OpTypePointer Function %v2int
284 %_ptr_v4int = OpTypePointer Function %v4int
285 %_ptr_v4float = OpTypePointer Function %v4float
286 %_ptr_v4double = OpTypePointer Function %v4double
287 %_ptr_v4half = OpTypePointer Function %v4half
288 %_ptr_struct_v2int_int_int = OpTypePointer Function %struct_v2int_int_int
289 %_ptr_v2float = OpTypePointer Function %v2float
290 %_ptr_v2double = OpTypePointer Function %v2double
291 %int_2 = OpConstant %int 2
292 %int_arr_2 = OpTypeArray %int %int_2
293 %short_n1 = OpConstant %short -1
294 %short_0 = OpConstant %short 0
295 %short_2 = OpConstant %short 2
296 %short_3 = OpConstant %short 3
297 %short_n5 = OpConstant %short -5
298 %ubyte_1 = OpConstant %ubyte 1
299 %ubyte_a = OpConstant %ubyte 10
300 %ubyte_b = OpConstant %ubyte 11
301 %ubyte_c = OpConstant %ubyte 12
302 %ubyte_d = OpConstant %ubyte 13
303 %byte_n1 = OpConstant %byte -1
304 %byte_0 = OpConstant %byte 0
305 %ushort_0 = OpConstant %ushort 0
306 %ushort_1 = OpConstant %ushort 1
307 %ushort_2 = OpConstant %ushort 2
308 %ushort_3 = OpConstant %ushort 3
309 %ushort_null = OpConstantNull %ushort
310 %100 = OpConstant %int 0 ; Need a def with an numerical id to define id maps.
311 %110 = OpConstantNull %int ; Need a def with an numerical id to define id maps.
312 %103 = OpConstant %int 7 ; Need a def with an numerical id to define id maps.
313 %int_0 = OpConstant %int 0
314 %int_1 = OpConstant %int 1
315 %int_3 = OpConstant %int 3
316 %int_4 = OpConstant %int 4
317 %int_10 = OpConstant %int 10
318 %int_1073741824 = OpConstant %int 1073741824
319 %int_n1 = OpConstant %int -1
320 %int_n24 = OpConstant %int -24
321 %int_n858993459 = OpConstant %int -858993459
322 %int_min = OpConstant %int -2147483648
323 %int_max = OpConstant %int 2147483647
324 %long_0 = OpConstant %long 0
325 %long_1 = OpConstant %long 1
326 %long_2 = OpConstant %long 2
327 %long_3 = OpConstant %long 3
328 %long_n3 = OpConstant %long -3
329 %long_7 = OpConstant %long 7
330 %long_n7 = OpConstant %long -7
331 %long_10 = OpConstant %long 10
332 %long_32768 = OpConstant %long 32768
333 %long_n57344 = OpConstant %long -57344
334 %long_n4611686018427387904 = OpConstant %long -4611686018427387904
335 %long_4611686018427387904 = OpConstant %long 4611686018427387904
336 %long_n1 = OpConstant %long -1
337 %long_n3689348814741910323 = OpConstant %long -3689348814741910323
338 %long_min = OpConstant %long -9223372036854775808
339 %long_max = OpConstant %long 9223372036854775807
340 %ulong_7 = OpConstant %ulong 7
341 %ulong_4611686018427387904 = OpConstant %ulong 4611686018427387904
342 %109 = OpConstant %uint 0 ; Need a def with an numerical id to define id maps.
343 %111 = OpConstantNull %uint ; Need a def with an numerical id to define id maps.
344 %uint_0 = OpConstant %uint 0
345 %uint_1 = OpConstant %uint 1
346 %uint_2 = OpConstant %uint 2
347 %uint_3 = OpConstant %uint 3
348 %uint_4 = OpConstant %uint 4
349 %uint_32 = OpConstant %uint 32
350 %uint_42 = OpConstant %uint 42
351 %uint_2147483649 = OpConstant %uint 2147483649
352 %uint_max = OpConstant %uint 4294967295
353 %uint_0x0000ffff = OpConstant %uint 65535
354 %ulong_0 = OpConstant %ulong 0
355 %ulong_1 = OpConstant %ulong 1
356 %ulong_2 = OpConstant %ulong 2
357 %ulong_9223372036854775809 = OpConstant %ulong 9223372036854775809
358 %ulong_max = OpConstant %ulong 18446744073709551615
359 %v2int_undef = OpUndef %v2int
360 %v2int_0_0 = OpConstantComposite %v2int %int_0 %int_0
361 %v2int_1_0 = OpConstantComposite %v2int %int_1 %int_0
362 %v2int_2_2 = OpConstantComposite %v2int %int_2 %int_2
363 %v2int_2_3 = OpConstantComposite %v2int %int_2 %int_3
364 %v2int_3_2 = OpConstantComposite %v2int %int_3 %int_2
365 %v2int_n1_n24 = OpConstantComposite %v2int %int_n1 %int_n24
366 %v2int_4_4 = OpConstantComposite %v2int %int_4 %int_4
367 %v2int_min_max = OpConstantComposite %v2int %int_min %int_max
368 %v2short_2_n5 = OpConstantComposite %v2short %short_2 %short_n5
369 %v2short_n1_0 = OpConstantComposite %v2short %short_n1 %short_0
370 %v2short_null = OpConstantNull %v2short
371 %v2long_2_2 = OpConstantComposite %v2long %long_2 %long_2
372 %v2long_2_3 = OpConstantComposite %v2long %long_2 %long_3
373 %v2bool_null = OpConstantNull %v2bool
374 %v2bool_true_false = OpConstantComposite %v2bool %true %false
375 %v2bool_false_true = OpConstantComposite %v2bool %false %true
376 %struct_v2int_int_int_null = OpConstantNull %struct_v2int_int_int
377 %v2int_null = OpConstantNull %v2int
378 %102 = OpConstantComposite %v2int %103 %103
379 %v4int_undef = OpUndef %v4int
380 %v4int_0_0_0_0 = OpConstantComposite %v4int %int_0 %int_0 %int_0 %int_0
381 %m2x2int_undef = OpUndef %m2x2int
382 %struct_undef_0_0 = OpConstantComposite %struct_v2int_int_int %v2int_undef %int_0 %int_0
383 %float_n1 = OpConstant %float -1
384 %104 = OpConstant %float 0 ; Need a def with an numerical id to define id maps.
385 %113 = OpConstantNull %float ; Need a def with an numerical id to define id maps.
386 %float_null = OpConstantNull %float
387 %float_0 = OpConstant %float 0
388 %float_n0 = OpConstant %float -0.0
389 %float_1 = OpConstant %float 1
390 %float_2 = OpConstant %float 2
391 %float_3 = OpConstant %float 3
392 %float_4 = OpConstant %float 4
393 %float_2049 = OpConstant %float 2049
394 %float_n2049 = OpConstant %float -2049
395 %float_0p5 = OpConstant %float 0.5
396 %float_0p2 = OpConstant %float 0.2
397 %float_pi = OpConstant %float 1.5555
398 %float_1e16 = OpConstant %float 1e16
399 %float_n1e16 = OpConstant %float -1e16
400 %float_1en16 = OpConstant %float 1e-16
401 %float_n1en16 = OpConstant %float -1e-16
402 %v2float_0_0 = OpConstantComposite %v2float %float_0 %float_0
403 %v2float_2_2 = OpConstantComposite %v2float %float_2 %float_2
404 %v2float_2_3 = OpConstantComposite %v2float %float_2 %float_3
405 %v2float_3_2 = OpConstantComposite %v2float %float_3 %float_2
406 %v2float_4_4 = OpConstantComposite %v2float %float_4 %float_4
407 %v2float_2_0p5 = OpConstantComposite %v2float %float_2 %float_0p5
408 %v2float_0p2_0p5 = OpConstantComposite %v2float %float_0p2 %float_0p5
409 %v2float_null = OpConstantNull %v2float
410 %double_n1 = OpConstant %double -1
411 %105 = OpConstant %double 0 ; Need a def with an numerical id to define id maps.
412 %double_null = OpConstantNull %double
413 %double_0 = OpConstant %double 0
414 %double_n0 = OpConstant %double -0.0
415 %double_1 = OpConstant %double 1
416 %double_2 = OpConstant %double 2
417 %double_3 = OpConstant %double 3
418 %double_4 = OpConstant %double 4
419 %double_5 = OpConstant %double 5
420 %double_0p5 = OpConstant %double 0.5
421 %double_0p2 = OpConstant %double 0.2
422 %v2double_0_0 = OpConstantComposite %v2double %double_0 %double_0
423 %v2double_2_2 = OpConstantComposite %v2double %double_2 %double_2
424 %v2double_2_3 = OpConstantComposite %v2double %double_2 %double_3
425 %v2double_3_2 = OpConstantComposite %v2double %double_3 %double_2
426 %v2double_4_4 = OpConstantComposite %v2double %double_4 %double_4
427 %v2double_2_0p5 = OpConstantComposite %v2double %double_2 %double_0p5
428 %v2double_null = OpConstantNull %v2double
429 %108 = OpConstant %half 0
430 %half_0p5 = OpConstant %half 0.5
431 %half_1 = OpConstant %half 1
432 %half_2 = OpConstant %half 2
433 %112 = OpConstant %half 3
434 %half_null = OpConstantNull %half
435 %half_0_1 = OpConstantComposite %v2half %108 %half_1
436 %v4half_0_1_0_0 = OpConstantComposite %v4half %108 %half_1 %108 %108
437 %106 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
438 %v4float_0_0_0_0 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
439 %v4float_0_0_0_1 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_1
440 %v4float_0_1_0_0 = OpConstantComposite %v4float %float_0 %float_1 %float_null %float_0
441 %v4float_1_1_1_1 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
442 %v4float_1_2_3_4 = OpConstantComposite %v4float %float_1 %float_2 %float_3 %float_4
443 %v4float_null = OpConstantNull %v4float
444 %mat2v4float_null = OpConstantNull %mat2v4float
445 %mat4v4float_null = OpConstantNull %mat4v4float
446 %mat4v4float_1_2_3_4 = OpConstantComposite %mat4v4float %v4float_1_2_3_4 %v4float_1_2_3_4 %v4float_1_2_3_4 %v4float_1_2_3_4
447 %mat4v4float_1_2_3_4_null = OpConstantComposite %mat4v4float %v4float_1_2_3_4 %v4float_null %v4float_1_2_3_4 %v4float_null
448 %107 = OpConstantComposite %v4double %double_0 %double_0 %double_0 %double_0
449 %v4double_0_0_0_0 = OpConstantComposite %v4double %double_0 %double_0 %double_0 %double_0
450 %v4double_0_0_0_1 = OpConstantComposite %v4double %double_0 %double_0 %double_0 %double_1
451 %v4double_0_1_0_0 = OpConstantComposite %v4double %double_0 %double_1 %double_null %double_0
452 %v4double_1_1_1_1 = OpConstantComposite %v4double %double_1 %double_1 %double_1 %double_1
453 %v4double_1_2_3_4 = OpConstantComposite %v4double %double_1 %double_2 %double_3 %double_4
454 %v4double_1_1_1_0p5 = OpConstantComposite %v4double %double_1 %double_1 %double_1 %double_0p5
455 %v4double_null = OpConstantNull %v4double
456 %mat4v4double_null = OpConstantNull %mat4v4double
457 %mat4v4double_1_2_3_4 = OpConstantComposite %mat4v4double %v4double_1_2_3_4 %v4double_1_2_3_4 %v4double_1_2_3_4 %v4double_1_2_3_4
458 %mat4v4double_1_2_3_4_null = OpConstantComposite %mat4v4double %v4double_1_2_3_4 %v4double_null %v4double_1_2_3_4 %v4double_null
459 %v4float_n1_2_1_3 = OpConstantComposite %v4float %float_n1 %float_2 %float_1 %float_3
460 %uint_0x3f800000 = OpConstant %uint 0x3f800000
461 %uint_0xbf800000 = OpConstant %uint 0xbf800000
462 %v2uint_0x3f800000_0xbf800000 = OpConstantComposite %v2uint %uint_0x3f800000 %uint_0xbf800000
463 %v4uint_1_0x0000ffff_uint_0_uint_max = OpConstantComposite %v4uint %uint_1 %uint_0x0000ffff %uint_0 %uint_max
464 %v2uint_1_null = OpConstantComposite %v2uint %uint_1 %111
465 %v2uint_null = OpConstantNull %v2uint
466 %v2ushort_1_null = OpConstantComposite %v2ushort %ushort_1 %ushort_null
467 %v4ushort_0_1_2_3 = OpConstantComposite %v4ushort %ushort_0 %ushort_1 %ushort_2 %ushort_3
468 %v2ubyte_a_b = OpConstantComposite %v2ubyte %ubyte_a %ubyte_b
469 %v4ubyte_a_b_c_d = OpConstantComposite %v4ubyte %ubyte_a %ubyte_b %ubyte_c %ubyte_d
470 %v4byte_n1_0_0_0 = OpConstantComposite %v4byte %byte_n1 %byte_0 %byte_0 %byte_0
471 %long_0xbf8000003f800000 = OpConstant %long 0xbf8000003f800000
472 %int_0x3FF00000 = OpConstant %int 0x3FF00000
473 %int_0x00000000 = OpConstant %int 0x00000000
474 %int_0xC05FD666 = OpConstant %int 0xC05FD666
475 %int_0x66666666 = OpConstant %int 0x66666666
476 %v4int_0x3FF00000_0x00000000_0xC05FD666_0x66666666 = OpConstantComposite %v4int %int_0x00000000 %int_0x3FF00000 %int_0x66666666 %int_0xC05FD666
477 %ushort_0x4400 = OpConstant %ushort 0x4400
478 %short_0x4400 = OpConstant %short 0x4400
479 %ushort_0xBC00 = OpConstant %ushort 0xBC00
480 %short_0xBC00 = OpConstant %short 0xBC00
481 %int_arr_2_undef = OpUndef %int_arr_2
482 %int_coop_matrix = OpTypeCooperativeMatrixKHR %int %uint_3 %uint_3 %uint_32 %uint_0
483 %undef_int_coop_matrix = OpUndef %int_coop_matrix
484 %uint_coop_matrix = OpTypeCooperativeMatrixKHR %uint %uint_3 %uint_3 %uint_32 %uint_0
485 %undef_uint_coop_matrix = OpUndef %uint_coop_matrix
486 %float_coop_matrix = OpTypeCooperativeMatrixKHR %float %uint_3 %uint_3 %uint_32 %uint_0
487 %undef_float_coop_matrix = OpUndef %float_coop_matrix
488 )";
489
490 return header;
491 }
492
493 // Returns the header with definitions of float NaN and double NaN. Since FC
494 // "; CHECK: [[double_n0:%\\w+]] = OpConstant [[double]] -0\n" finds
495 // %double_nan = OpConstant %double -0x1.8p+1024 instead of
496 // %double_n0 = OpConstant %double -0,
497 // we separates those definitions from Header().
498 const std::string& HeaderWithNaN() {
499 static const std::string headerWithNaN =
500 Header() +
501 R"(%float_nan = OpConstant %float -0x1.8p+128
502 %double_nan = OpConstant %double -0x1.8p+1024
503 )";
504
505 return headerWithNaN;
506 }
507
508 // clang-format off
509 INSTANTIATE_TEST_SUITE_P(TestCase, IntegerInstructionFoldingTest,
510 ::testing::Values(
511 // Test case 0: fold 0*n
512 InstructionFoldingCase<uint32_t>(
513 Header() + "%main = OpFunction %void None %void_func\n" +
514 "%main_lab = OpLabel\n" +
515 "%n = OpVariable %_ptr_int Function\n" +
516 "%load = OpLoad %int %n\n" +
517 "%2 = OpIMul %int %int_0 %load\n" +
518 "OpReturn\n" +
519 "OpFunctionEnd",
520 2, 0),
521 // Test case 1: fold n*0
522 InstructionFoldingCase<uint32_t>(
523 Header() + "%main = OpFunction %void None %void_func\n" +
524 "%main_lab = OpLabel\n" +
525 "%n = OpVariable %_ptr_int Function\n" +
526 "%load = OpLoad %int %n\n" +
527 "%2 = OpIMul %int %load %int_0\n" +
528 "OpReturn\n" +
529 "OpFunctionEnd",
530 2, 0),
531 // Test case 2: fold 0/n (signed)
532 InstructionFoldingCase<uint32_t>(
533 Header() + "%main = OpFunction %void None %void_func\n" +
534 "%main_lab = OpLabel\n" +
535 "%n = OpVariable %_ptr_int Function\n" +
536 "%load = OpLoad %int %n\n" +
537 "%2 = OpSDiv %int %int_0 %load\n" +
538 "OpReturn\n" +
539 "OpFunctionEnd",
540 2, 0),
541 // Test case 3: fold n/0 (signed)
542 InstructionFoldingCase<uint32_t>(
543 Header() + "%main = OpFunction %void None %void_func\n" +
544 "%main_lab = OpLabel\n" +
545 "%n = OpVariable %_ptr_int Function\n" +
546 "%load = OpLoad %int %n\n" +
547 "%2 = OpSDiv %int %load %int_0\n" +
548 "OpReturn\n" +
549 "OpFunctionEnd",
550 2, 0),
551 // Test case 4: fold 0/n (unsigned)
552 InstructionFoldingCase<uint32_t>(
553 Header() + "%main = OpFunction %void None %void_func\n" +
554 "%main_lab = OpLabel\n" +
555 "%n = OpVariable %_ptr_uint Function\n" +
556 "%load = OpLoad %uint %n\n" +
557 "%2 = OpUDiv %uint %uint_0 %load\n" +
558 "OpReturn\n" +
559 "OpFunctionEnd",
560 2, 0),
561 // Test case 5: fold n/0 (unsigned)
562 InstructionFoldingCase<uint32_t>(
563 Header() + "%main = OpFunction %void None %void_func\n" +
564 "%main_lab = OpLabel\n" +
565 "%n = OpVariable %_ptr_int Function\n" +
566 "%load = OpLoad %int %n\n" +
567 "%2 = OpSDiv %int %load %int_0\n" +
568 "OpReturn\n" +
569 "OpFunctionEnd",
570 2, 0),
571 // Test case 6: fold 0 remainder n
572 InstructionFoldingCase<uint32_t>(
573 Header() + "%main = OpFunction %void None %void_func\n" +
574 "%main_lab = OpLabel\n" +
575 "%n = OpVariable %_ptr_int Function\n" +
576 "%load = OpLoad %int %n\n" +
577 "%2 = OpSRem %int %int_0 %load\n" +
578 "OpReturn\n" +
579 "OpFunctionEnd",
580 2, 0),
581 // Test case 7: fold n remainder 0
582 InstructionFoldingCase<uint32_t>(
583 Header() + "%main = OpFunction %void None %void_func\n" +
584 "%main_lab = OpLabel\n" +
585 "%n = OpVariable %_ptr_int Function\n" +
586 "%load = OpLoad %int %n\n" +
587 "%2 = OpSRem %int %load %int_0\n" +
588 "OpReturn\n" +
589 "OpFunctionEnd",
590 2, 0),
591 // Test case 8: fold 0%n (signed)
592 InstructionFoldingCase<uint32_t>(
593 Header() + "%main = OpFunction %void None %void_func\n" +
594 "%main_lab = OpLabel\n" +
595 "%n = OpVariable %_ptr_int Function\n" +
596 "%load = OpLoad %int %n\n" +
597 "%2 = OpSMod %int %int_0 %load\n" +
598 "OpReturn\n" +
599 "OpFunctionEnd",
600 2, 0),
601 // Test case 9: fold n%0 (signed)
602 InstructionFoldingCase<uint32_t>(
603 Header() + "%main = OpFunction %void None %void_func\n" +
604 "%main_lab = OpLabel\n" +
605 "%n = OpVariable %_ptr_int Function\n" +
606 "%load = OpLoad %int %n\n" +
607 "%2 = OpSMod %int %load %int_0\n" +
608 "OpReturn\n" +
609 "OpFunctionEnd",
610 2, 0),
611 // Test case 10: fold 0%n (unsigned)
612 InstructionFoldingCase<uint32_t>(
613 Header() + "%main = OpFunction %void None %void_func\n" +
614 "%main_lab = OpLabel\n" +
615 "%n = OpVariable %_ptr_uint Function\n" +
616 "%load = OpLoad %uint %n\n" +
617 "%2 = OpUMod %uint %uint_0 %load\n" +
618 "OpReturn\n" +
619 "OpFunctionEnd",
620 2, 0),
621 // Test case 11: fold n%0 (unsigned)
622 InstructionFoldingCase<uint32_t>(
623 Header() + "%main = OpFunction %void None %void_func\n" +
624 "%main_lab = OpLabel\n" +
625 "%n = OpVariable %_ptr_uint Function\n" +
626 "%load = OpLoad %uint %n\n" +
627 "%2 = OpUMod %uint %load %uint_0\n" +
628 "OpReturn\n" +
629 "OpFunctionEnd",
630 2, 0),
631 // Test case 12: fold n << 32
632 InstructionFoldingCase<uint32_t>(
633 Header() + "%main = OpFunction %void None %void_func\n" +
634 "%main_lab = OpLabel\n" +
635 "%n = OpVariable %_ptr_uint Function\n" +
636 "%load = OpLoad %uint %n\n" +
637 "%2 = OpShiftLeftLogical %uint %load %uint_32\n" +
638 "OpReturn\n" +
639 "OpFunctionEnd",
640 2, 0),
641 // Test case 13: fold n >> 32
642 InstructionFoldingCase<uint32_t>(
643 Header() + "%main = OpFunction %void None %void_func\n" +
644 "%main_lab = OpLabel\n" +
645 "%n = OpVariable %_ptr_uint Function\n" +
646 "%load = OpLoad %uint %n\n" +
647 "%2 = OpShiftRightLogical %uint %load %uint_32\n" +
648 "OpReturn\n" +
649 "OpFunctionEnd",
650 2, 0),
651 // Test case 14: fold n | 0xFFFFFFFF
652 InstructionFoldingCase<uint32_t>(
653 Header() + "%main = OpFunction %void None %void_func\n" +
654 "%main_lab = OpLabel\n" +
655 "%n = OpVariable %_ptr_uint Function\n" +
656 "%load = OpLoad %uint %n\n" +
657 "%2 = OpBitwiseOr %uint %load %uint_max\n" +
658 "OpReturn\n" +
659 "OpFunctionEnd",
660 2, 0xFFFFFFFF),
661 // Test case 15: fold 0xFFFFFFFF | n
662 InstructionFoldingCase<uint32_t>(
663 Header() + "%main = OpFunction %void None %void_func\n" +
664 "%main_lab = OpLabel\n" +
665 "%n = OpVariable %_ptr_uint Function\n" +
666 "%load = OpLoad %uint %n\n" +
667 "%2 = OpBitwiseOr %uint %uint_max %load\n" +
668 "OpReturn\n" +
669 "OpFunctionEnd",
670 2, 0xFFFFFFFF),
671 // Test case 16: fold n & 0
672 InstructionFoldingCase<uint32_t>(
673 Header() + "%main = OpFunction %void None %void_func\n" +
674 "%main_lab = OpLabel\n" +
675 "%n = OpVariable %_ptr_uint Function\n" +
676 "%load = OpLoad %uint %n\n" +
677 "%2 = OpBitwiseAnd %uint %load %uint_0\n" +
678 "OpReturn\n" +
679 "OpFunctionEnd",
680 2, 0),
681 // Test case 17: fold 1/0 (signed)
682 InstructionFoldingCase<uint32_t>(
683 Header() + "%main = OpFunction %void None %void_func\n" +
684 "%main_lab = OpLabel\n" +
685 "%2 = OpSDiv %int %int_1 %int_0\n" +
686 "OpReturn\n" +
687 "OpFunctionEnd",
688 2, 0),
689 // Test case 18: fold 1/0 (unsigned)
690 InstructionFoldingCase<uint32_t>(
691 Header() + "%main = OpFunction %void None %void_func\n" +
692 "%main_lab = OpLabel\n" +
693 "%2 = OpUDiv %uint %uint_1 %uint_0\n" +
694 "OpReturn\n" +
695 "OpFunctionEnd",
696 2, 0),
697 // Test case 19: fold OpSRem 1 0 (signed)
698 InstructionFoldingCase<uint32_t>(
699 Header() + "%main = OpFunction %void None %void_func\n" +
700 "%main_lab = OpLabel\n" +
701 "%2 = OpSRem %int %int_1 %int_0\n" +
702 "OpReturn\n" +
703 "OpFunctionEnd",
704 2, 0),
705 // Test case 20: fold 1%0 (signed)
706 InstructionFoldingCase<uint32_t>(
707 Header() + "%main = OpFunction %void None %void_func\n" +
708 "%main_lab = OpLabel\n" +
709 "%2 = OpSMod %int %int_1 %int_0\n" +
710 "OpReturn\n" +
711 "OpFunctionEnd",
712 2, 0),
713 // Test case 21: fold 1%0 (unsigned)
714 InstructionFoldingCase<uint32_t>(
715 Header() + "%main = OpFunction %void None %void_func\n" +
716 "%main_lab = OpLabel\n" +
717 "%2 = OpUMod %uint %uint_1 %uint_0\n" +
718 "OpReturn\n" +
719 "OpFunctionEnd",
720 2, 0),
721 // Test case 22: fold unsigned n >> 42 (undefined, so set to zero).
722 InstructionFoldingCase<uint32_t>(
723 Header() + "%main = OpFunction %void None %void_func\n" +
724 "%main_lab = OpLabel\n" +
725 "%n = OpVariable %_ptr_uint Function\n" +
726 "%load = OpLoad %uint %n\n" +
727 "%2 = OpShiftRightLogical %uint %load %uint_42\n" +
728 "OpReturn\n" +
729 "OpFunctionEnd",
730 2, 0),
731 // Test case 23: fold signed n >> 42 (undefined, so set to zero).
732 InstructionFoldingCase<uint32_t>(
733 Header() + "%main = OpFunction %void None %void_func\n" +
734 "%main_lab = OpLabel\n" +
735 "%n = OpVariable %_ptr_int Function\n" +
736 "%load = OpLoad %int %n\n" +
737 "%2 = OpShiftRightLogical %int %load %uint_42\n" +
738 "OpReturn\n" +
739 "OpFunctionEnd",
740 2, 0),
741 // Test case 24: fold n << 42 (undefined, so set to zero).
742 InstructionFoldingCase<uint32_t>(
743 Header() + "%main = OpFunction %void None %void_func\n" +
744 "%main_lab = OpLabel\n" +
745 "%n = OpVariable %_ptr_int Function\n" +
746 "%load = OpLoad %int %n\n" +
747 "%2 = OpShiftLeftLogical %int %load %uint_42\n" +
748 "OpReturn\n" +
749 "OpFunctionEnd",
750 2, 0),
751 // Test case 25: fold -24 >> 32 (defined as -1)
752 InstructionFoldingCase<uint32_t>(
753 Header() + "%main = OpFunction %void None %void_func\n" +
754 "%main_lab = OpLabel\n" +
755 "%2 = OpShiftRightArithmetic %int %int_n24 %uint_32\n" +
756 "OpReturn\n" +
757 "OpFunctionEnd",
758 2, -1),
759 // Test case 26: fold 2 >> 32 (signed)
760 InstructionFoldingCase<uint32_t>(
761 Header() + "%main = OpFunction %void None %void_func\n" +
762 "%main_lab = OpLabel\n" +
763 "%2 = OpShiftRightArithmetic %int %int_2 %uint_32\n" +
764 "OpReturn\n" +
765 "OpFunctionEnd",
766 2, 0),
767 // Test case 27: fold 2 >> 32 (unsigned)
768 InstructionFoldingCase<uint32_t>(
769 Header() + "%main = OpFunction %void None %void_func\n" +
770 "%main_lab = OpLabel\n" +
771 "%2 = OpShiftRightLogical %int %int_2 %uint_32\n" +
772 "OpReturn\n" +
773 "OpFunctionEnd",
774 2, 0),
775 // Test case 28: fold 2 << 32
776 InstructionFoldingCase<uint32_t>(
777 Header() + "%main = OpFunction %void None %void_func\n" +
778 "%main_lab = OpLabel\n" +
779 "%2 = OpShiftLeftLogical %int %int_2 %uint_32\n" +
780 "OpReturn\n" +
781 "OpFunctionEnd",
782 2, 0),
783 // Test case 29: fold -INT_MIN
784 InstructionFoldingCase<uint32_t>(
785 Header() + "%main = OpFunction %void None %void_func\n" +
786 "%main_lab = OpLabel\n" +
787 "%2 = OpSNegate %int %int_min\n" +
788 "OpReturn\n" +
789 "OpFunctionEnd",
790 2, std::numeric_limits<int32_t>::min()),
791 // Test case 30: fold UMin 3 4
792 InstructionFoldingCase<uint32_t>(
793 Header() + "%main = OpFunction %void None %void_func\n" +
794 "%main_lab = OpLabel\n" +
795 "%2 = OpExtInst %uint %1 UMin %uint_3 %uint_4\n" +
796 "OpReturn\n" +
797 "OpFunctionEnd",
798 2, 3),
799 // Test case 31: fold UMin 4 2
800 InstructionFoldingCase<uint32_t>(
801 Header() + "%main = OpFunction %void None %void_func\n" +
802 "%main_lab = OpLabel\n" +
803 "%2 = OpExtInst %uint %1 UMin %uint_4 %uint_2\n" +
804 "OpReturn\n" +
805 "OpFunctionEnd",
806 2, 2),
807 // Test case 32: fold SMin 3 4
808 InstructionFoldingCase<uint32_t>(
809 Header() + "%main = OpFunction %void None %void_func\n" +
810 "%main_lab = OpLabel\n" +
811 "%2 = OpExtInst %int %1 SMin %int_3 %int_4\n" +
812 "OpReturn\n" +
813 "OpFunctionEnd",
814 2, 3),
815 // Test case 33: fold SMin 4 2
816 InstructionFoldingCase<uint32_t>(
817 Header() + "%main = OpFunction %void None %void_func\n" +
818 "%main_lab = OpLabel\n" +
819 "%2 = OpExtInst %int %1 SMin %int_4 %int_2\n" +
820 "OpReturn\n" +
821 "OpFunctionEnd",
822 2, 2),
823 // Test case 34: fold SMin short -5 2
824 InstructionFoldingCase<uint32_t>(
825 Header() + "%main = OpFunction %void None %void_func\n" +
826 "%main_lab = OpLabel\n" +
827 "%2 = OpExtInst %short %1 SMin %short_n5 %short_2\n" +
828 "OpReturn\n" +
829 "OpFunctionEnd",
830 2, -5),
831 // Test case 35: fold UMin ushort 2 0
832 InstructionFoldingCase<uint32_t>(
833 Header() + "%main = OpFunction %void None %void_func\n" +
834 "%main_lab = OpLabel\n" +
835 "%2 = OpExtInst %ushort %1 UMin %ushort_2 %ushort_0\n" +
836 "OpReturn\n" +
837 "OpFunctionEnd",
838 2, 0),
839 // Test case 36: fold SMin int OpConstNull -1
840 InstructionFoldingCase<uint32_t>(
841 Header() + "%main = OpFunction %void None %void_func\n" +
842 "%main_lab = OpLabel\n" +
843 "%2 = OpExtInst %int %1 SMin %110 %int_n1\n" +
844 "OpReturn\n" +
845 "OpFunctionEnd",
846 2, -1),
847 // Test case 37: fold SMin int -1 OpConstNull
848 InstructionFoldingCase<uint32_t>(
849 Header() + "%main = OpFunction %void None %void_func\n" +
850 "%main_lab = OpLabel\n" +
851 "%2 = OpExtInst %int %1 SMin %int_n1 %110\n" +
852 "OpReturn\n" +
853 "OpFunctionEnd",
854 2, -1),
855 // Test case 38: fold UMax 3 4
856 InstructionFoldingCase<uint32_t>(
857 Header() + "%main = OpFunction %void None %void_func\n" +
858 "%main_lab = OpLabel\n" +
859 "%2 = OpExtInst %uint %1 UMax %uint_3 %uint_4\n" +
860 "OpReturn\n" +
861 "OpFunctionEnd",
862 2, 4),
863 // Test case 39: fold UMax 3 2
864 InstructionFoldingCase<uint32_t>(
865 Header() + "%main = OpFunction %void None %void_func\n" +
866 "%main_lab = OpLabel\n" +
867 "%2 = OpExtInst %uint %1 UMax %uint_3 %uint_2\n" +
868 "OpReturn\n" +
869 "OpFunctionEnd",
870 2, 3),
871 // Test case 40: fold SMax 3 4
872 InstructionFoldingCase<uint32_t>(
873 Header() + "%main = OpFunction %void None %void_func\n" +
874 "%main_lab = OpLabel\n" +
875 "%2 = OpExtInst %int %1 SMax %int_3 %int_4\n" +
876 "OpReturn\n" +
877 "OpFunctionEnd",
878 2, 4),
879 // Test case 41: fold SMax 3 2
880 InstructionFoldingCase<uint32_t>(
881 Header() + "%main = OpFunction %void None %void_func\n" +
882 "%main_lab = OpLabel\n" +
883 "%2 = OpExtInst %int %1 SMax %int_3 %int_2\n" +
884 "OpReturn\n" +
885 "OpFunctionEnd",
886 2, 3),
887 // Test case 42: fold SMax short -5 2
888 InstructionFoldingCase<uint32_t>(
889 Header() + "%main = OpFunction %void None %void_func\n" +
890 "%main_lab = OpLabel\n" +
891 "%2 = OpExtInst %short %1 SMax %short_n5 %short_2\n" +
892 "OpReturn\n" +
893 "OpFunctionEnd",
894 2, 2),
895 // Test case 43: fold UMax ushort 2 0
896 InstructionFoldingCase<uint32_t>(
897 Header() + "%main = OpFunction %void None %void_func\n" +
898 "%main_lab = OpLabel\n" +
899 "%2 = OpExtInst %ushort %1 UMax %ushort_2 %ushort_0\n" +
900 "OpReturn\n" +
901 "OpFunctionEnd",
902 2, 2),
903 // Test case 44: fold SMax int OpConstNull 1
904 InstructionFoldingCase<uint32_t>(
905 Header() + "%main = OpFunction %void None %void_func\n" +
906 "%main_lab = OpLabel\n" +
907 "%2 = OpExtInst %int %1 SMax %110 %int_1\n" +
908 "OpReturn\n" +
909 "OpFunctionEnd",
910 2, 1),
911 // Test case 45: fold UClamp 2 3 4
912 InstructionFoldingCase<uint32_t>(
913 Header() + "%main = OpFunction %void None %void_func\n" +
914 "%main_lab = OpLabel\n" +
915 "%2 = OpExtInst %uint %1 UClamp %uint_2 %uint_3 %uint_4\n" +
916 "OpReturn\n" +
917 "OpFunctionEnd",
918 2, 3),
919 // Test case 46: fold UClamp 2 0 4
920 InstructionFoldingCase<uint32_t>(
921 Header() + "%main = OpFunction %void None %void_func\n" +
922 "%main_lab = OpLabel\n" +
923 "%2 = OpExtInst %uint %1 UClamp %uint_2 %uint_0 %uint_4\n" +
924 "OpReturn\n" +
925 "OpFunctionEnd",
926 2, 2),
927 // Test case 47: fold UClamp 2 0 1
928 InstructionFoldingCase<uint32_t>(
929 Header() + "%main = OpFunction %void None %void_func\n" +
930 "%main_lab = OpLabel\n" +
931 "%2 = OpExtInst %uint %1 UClamp %uint_2 %uint_0 %uint_1\n" +
932 "OpReturn\n" +
933 "OpFunctionEnd",
934 2, 1),
935 // Test case 48: fold UClamp short 2 0 1
936 InstructionFoldingCase<uint32_t>(
937 Header() + "%main = OpFunction %void None %void_func\n" +
938 "%main_lab = OpLabel\n" +
939 "%2 = OpExtInst %ushort %1 UClamp %ushort_2 %ushort_0 %ushort_1\n" +
940 "OpReturn\n" +
941 "OpFunctionEnd",
942 2, 1),
943 // Test case 49: fold SClamp 2 3 4
944 InstructionFoldingCase<uint32_t>(
945 Header() + "%main = OpFunction %void None %void_func\n" +
946 "%main_lab = OpLabel\n" +
947 "%2 = OpExtInst %int %1 SClamp %int_2 %int_3 %int_4\n" +
948 "OpReturn\n" +
949 "OpFunctionEnd",
950 2, 3),
951 // Test case 50: fold SClamp 2 0 4
952 InstructionFoldingCase<uint32_t>(
953 Header() + "%main = OpFunction %void None %void_func\n" +
954 "%main_lab = OpLabel\n" +
955 "%2 = OpExtInst %int %1 SClamp %int_2 %int_0 %int_4\n" +
956 "OpReturn\n" +
957 "OpFunctionEnd",
958 2, 2),
959 // Test case 51: fold SClamp 2 0 1
960 InstructionFoldingCase<uint32_t>(
961 Header() + "%main = OpFunction %void None %void_func\n" +
962 "%main_lab = OpLabel\n" +
963 "%2 = OpExtInst %int %1 SClamp %int_2 %int_0 %int_1\n" +
964 "OpReturn\n" +
965 "OpFunctionEnd",
966 2, 1),
967 // Test case 52: SClamp 1 2 x
968 InstructionFoldingCase<uint32_t>(
969 Header() + "%main = OpFunction %void None %void_func\n" +
970 "%main_lab = OpLabel\n" +
971 "%undef = OpUndef %int\n" +
972 "%2 = OpExtInst %int %1 SClamp %int_1 %int_2 %undef\n" +
973 "OpReturn\n" +
974 "OpFunctionEnd",
975 2, 2),
976 // Test case 53: SClamp 2 x 1
977 InstructionFoldingCase<uint32_t>(
978 Header() + "%main = OpFunction %void None %void_func\n" +
979 "%main_lab = OpLabel\n" +
980 "%undef = OpUndef %int\n" +
981 "%2 = OpExtInst %int %1 SClamp %int_2 %undef %int_1\n" +
982 "OpReturn\n" +
983 "OpFunctionEnd",
984 2, 1),
985 // Test case 54: UClamp 1 2 x
986 InstructionFoldingCase<uint32_t>(
987 Header() + "%main = OpFunction %void None %void_func\n" +
988 "%main_lab = OpLabel\n" +
989 "%undef = OpUndef %uint\n" +
990 "%2 = OpExtInst %uint %1 UClamp %uint_1 %uint_2 %undef\n" +
991 "OpReturn\n" +
992 "OpFunctionEnd",
993 2, 2),
994 // Test case 55: UClamp 2 x 1
995 InstructionFoldingCase<uint32_t>(
996 Header() + "%main = OpFunction %void None %void_func\n" +
997 "%main_lab = OpLabel\n" +
998 "%undef = OpUndef %uint\n" +
999 "%2 = OpExtInst %uint %1 UClamp %uint_2 %undef %uint_1\n" +
1000 "OpReturn\n" +
1001 "OpFunctionEnd",
1002 2, 1),
1003 // Test case 56: Bit-cast int 0 to unsigned int
1004 InstructionFoldingCase<uint32_t>(
1005 Header() + "%main = OpFunction %void None %void_func\n" +
1006 "%main_lab = OpLabel\n" +
1007 "%2 = OpBitcast %uint %int_0\n" +
1008 "OpReturn\n" +
1009 "OpFunctionEnd",
1010 2, 0),
1011 // Test case 57: Bit-cast int -24 to unsigned int
1012 InstructionFoldingCase<uint32_t>(
1013 Header() + "%main = OpFunction %void None %void_func\n" +
1014 "%main_lab = OpLabel\n" +
1015 "%2 = OpBitcast %uint %int_n24\n" +
1016 "OpReturn\n" +
1017 "OpFunctionEnd",
1018 2, static_cast<uint32_t>(-24)),
1019 // Test case 58: Bit-cast int const null to unsigned int
1020 InstructionFoldingCase<uint32_t>(
1021 Header() + "%main = OpFunction %void None %void_func\n" +
1022 "%main_lab = OpLabel\n" +
1023 "%2 = OpBitcast %uint %110\n" +
1024 "OpReturn\n" +
1025 "OpFunctionEnd",
1026 2, 0),
1027 // Test case 59: Bit-cast float 1.0f to unsigned int
1028 InstructionFoldingCase<uint32_t>(
1029 Header() + "%main = OpFunction %void None %void_func\n" +
1030 "%main_lab = OpLabel\n" +
1031 "%2 = OpBitcast %uint %float_1\n" +
1032 "OpReturn\n" +
1033 "OpFunctionEnd",
1034 2, static_cast<uint32_t>(0x3f800000)),
1035 // Test case 60: Bit-cast float constant null to unsigned int
1036 InstructionFoldingCase<uint32_t>(
1037 Header() + "%main = OpFunction %void None %void_func\n" +
1038 "%main_lab = OpLabel\n" +
1039 "%2 = OpBitcast %uint %float_null\n" +
1040 "OpReturn\n" +
1041 "OpFunctionEnd",
1042 2, 0),
1043 // Test case 61: Bit-cast ushort 0xBC00 to ushort
1044 InstructionFoldingCase<uint32_t>(
1045 Header() + "%main = OpFunction %void None %void_func\n" +
1046 "%main_lab = OpLabel\n" +
1047 "%2 = OpBitcast %ushort %ushort_0xBC00\n" +
1048 "OpReturn\n" +
1049 "OpFunctionEnd",
1050 2, 0xBC00),
1051 // Test case 62: Bit-cast short 0xBC00 to ushort
1052 InstructionFoldingCase<uint32_t>(
1053 Header() + "%main = OpFunction %void None %void_func\n" +
1054 "%main_lab = OpLabel\n" +
1055 "%2 = OpBitcast %ushort %short_0xBC00\n" +
1056 "OpReturn\n" +
1057 "OpFunctionEnd",
1058 2, 0xBC00),
1059 // Test case 63: Bit-cast half 1 to ushort
1060 InstructionFoldingCase<uint32_t>(
1061 Header() + "%main = OpFunction %void None %void_func\n" +
1062 "%main_lab = OpLabel\n" +
1063 "%2 = OpBitcast %ushort %half_1\n" +
1064 "OpReturn\n" +
1065 "OpFunctionEnd",
1066 2, 0x3C00),
1067 // Test case 64: Bit-cast half const null to ushort
1068 InstructionFoldingCase<uint32_t>(
1069 Header() + "%main = OpFunction %void None %void_func\n" +
1070 "%main_lab = OpLabel\n" +
1071 "%2 = OpBitcast %ushort %half_null\n" +
1072 "OpReturn\n" +
1073 "OpFunctionEnd",
1074 2, 0x0000),
1075 // Test case 65: Bit-cast ushort 0xBC00 to short
1076 InstructionFoldingCase<uint32_t>(
1077 Header() + "%main = OpFunction %void None %void_func\n" +
1078 "%main_lab = OpLabel\n" +
1079 "%2 = OpBitcast %short %ushort_0xBC00\n" +
1080 "OpReturn\n" +
1081 "OpFunctionEnd",
1082 2, 0xFFFFBC00),
1083 // Test case 66: Bit-cast short 0xBC00 to short
1084 InstructionFoldingCase<uint32_t>(
1085 Header() + "%main = OpFunction %void None %void_func\n" +
1086 "%main_lab = OpLabel\n" +
1087 "%2 = OpBitcast %short %short_0xBC00\n" +
1088 "OpReturn\n" +
1089 "OpFunctionEnd",
1090 2, 0xFFFFBC00),
1091 // Test case 67: Bit-cast half 1 to short
1092 InstructionFoldingCase<uint32_t>(
1093 Header() + "%main = OpFunction %void None %void_func\n" +
1094 "%main_lab = OpLabel\n" +
1095 "%2 = OpBitcast %short %half_1\n" +
1096 "OpReturn\n" +
1097 "OpFunctionEnd",
1098 2, 0x3C00),
1099 // Test case 68: Bit-cast ushort 0xBC00 to half
1100 InstructionFoldingCase<uint32_t>(
1101 Header() + "%main = OpFunction %void None %void_func\n" +
1102 "%main_lab = OpLabel\n" +
1103 "%2 = OpBitcast %half %ushort_0xBC00\n" +
1104 "OpReturn\n" +
1105 "OpFunctionEnd",
1106 2, 0xBC00),
1107 // Test case 69: Bit-cast short 0xBC00 to half
1108 InstructionFoldingCase<uint32_t>(
1109 Header() + "%main = OpFunction %void None %void_func\n" +
1110 "%main_lab = OpLabel\n" +
1111 "%2 = OpBitcast %half %short_0xBC00\n" +
1112 "OpReturn\n" +
1113 "OpFunctionEnd",
1114 2, 0xFFFFBC00),
1115 // Test case 70: Bit-cast half 1 to half
1116 InstructionFoldingCase<uint32_t>(
1117 Header() + "%main = OpFunction %void None %void_func\n" +
1118 "%main_lab = OpLabel\n" +
1119 "%2 = OpBitcast %half %half_1\n" +
1120 "OpReturn\n" +
1121 "OpFunctionEnd",
1122 2, 0x3C00),
1123 // Test case 71: Bit-cast ubyte 1 to byte
1124 InstructionFoldingCase<uint32_t>(
1125 Header() + "%main = OpFunction %void None %void_func\n" +
1126 "%main_lab = OpLabel\n" +
1127 "%2 = OpBitcast %byte %ubyte_1\n" +
1128 "OpReturn\n" +
1129 "OpFunctionEnd",
1130 2, 1),
1131 // Test case 72: Bit-cast byte -1 to ubyte
1132 InstructionFoldingCase<uint32_t>(
1133 Header() + "%main = OpFunction %void None %void_func\n" +
1134 "%main_lab = OpLabel\n" +
1135 "%2 = OpBitcast %ubyte %byte_n1\n" +
1136 "OpReturn\n" +
1137 "OpFunctionEnd",
1138 2, 0xFF),
1139 // Test case 73: Bit-cast v2shorts (-5,2) to uint
1140 InstructionFoldingCase<uint32_t>(
1141 Header() + "%main = OpFunction %void None %void_func\n" +
1142 "%main_lab = OpLabel\n" +
1143 "%2 = OpBitcast %uint %v2short_2_n5\n" +
1144 "OpReturn\n" +
1145 "OpFunctionEnd",
1146 2, 0xFFFB0002),
1147 // Test case 74: Bit-cast v4ubytes (0xA, 0xB, 0xC, 0xD) to int
1148 InstructionFoldingCase<uint32_t>(
1149 Header() + "%main = OpFunction %void None %void_func\n" +
1150 "%main_lab = OpLabel\n" +
1151 "%2 = OpBitcast %int %v4ubyte_a_b_c_d\n" +
1152 "OpReturn\n" +
1153 "OpFunctionEnd",
1154 2, 0x0D0C0B0A),
1155 // Test case 75: Bit-cast v2ushort (1, OpConstNull) to int
1156 InstructionFoldingCase<uint32_t>(
1157 Header() + "%main = OpFunction %void None %void_func\n" +
1158 "%main_lab = OpLabel\n" +
1159 "%2 = OpBitcast %int %v2ushort_1_null\n" +
1160 "OpReturn\n" +
1161 "OpFunctionEnd",
1162 2, 0x00000001),
1163 // Test case 76: Bit-cast v2short OpConstNull to int
1164 InstructionFoldingCase<uint32_t>(
1165 Header() + "%main = OpFunction %void None %void_func\n" +
1166 "%main_lab = OpLabel\n" +
1167 "%2 = OpBitcast %int %v2short_null\n" +
1168 "OpReturn\n" +
1169 "OpFunctionEnd",
1170 2, 0),
1171 // Test case 77: Bit-cast v2ubytes (0xA, 0xB) to ushort
1172 InstructionFoldingCase<uint32_t>(
1173 Header() + "%main = OpFunction %void None %void_func\n" +
1174 "%main_lab = OpLabel\n" +
1175 "%2 = OpBitcast %int %v2ubyte_a_b\n" +
1176 "OpReturn\n" +
1177 "OpFunctionEnd",
1178 2, 0x0B0A),
1179 // Test case 78: Bit-cast v2short (-1, 0) to uint
1180 InstructionFoldingCase<uint32_t>(
1181 Header() + "%main = OpFunction %void None %void_func\n" +
1182 "%main_lab = OpLabel\n" +
1183 "%2 = OpBitcast %uint %v2short_n1_0\n" +
1184 "OpReturn\n" +
1185 "OpFunctionEnd",
1186 2, 0x0000FFFF),
1187 // Test case 79: Bit-cast v4bytes (-1, 0, 0, 0) to uint
1188 InstructionFoldingCase<uint32_t>(
1189 Header() + "%main = OpFunction %void None %void_func\n" +
1190 "%main_lab = OpLabel\n" +
1191 "%2 = OpBitcast %uint %v4byte_n1_0_0_0\n" +
1192 "OpReturn\n" +
1193 "OpFunctionEnd",
1194 2, 0x000000FF),
1195 // Test case 80: Negate 2.
1196 InstructionFoldingCase<uint32_t>(
1197 Header() + "%main = OpFunction %void None %void_func\n" +
1198 "%main_lab = OpLabel\n" +
1199 "%2 = OpSNegate %int %int_2\n" +
1200 "OpReturn\n" +
1201 "OpFunctionEnd",
1202 2, -2),
1203 // Test case 81: Negate negative short.
1204 InstructionFoldingCase<uint32_t>(
1205 Header() + "%main = OpFunction %void None %void_func\n" +
1206 "%main_lab = OpLabel\n" +
1207 "%2 = OpSNegate %short %short_0xBC00\n" +
1208 "OpReturn\n" +
1209 "OpFunctionEnd",
1210 2, 0x4400 /* expected to be sign extended. */),
1211 // Test case 82: Negate positive short.
1212 InstructionFoldingCase<uint32_t>(
1213 Header() + "%main = OpFunction %void None %void_func\n" +
1214 "%main_lab = OpLabel\n" +
1215 "%2 = OpSNegate %short %short_0x4400\n" +
1216 "OpReturn\n" +
1217 "OpFunctionEnd",
1218 2, 0xFFFFBC00 /* expected to be sign extended. */),
1219 // Test case 83: Negate a negative short.
1220 InstructionFoldingCase<uint32_t>(
1221 Header() + "%main = OpFunction %void None %void_func\n" +
1222 "%main_lab = OpLabel\n" +
1223 "%2 = OpSNegate %ushort %ushort_0xBC00\n" +
1224 "OpReturn\n" +
1225 "OpFunctionEnd",
1226 2, 0x4400 /* expected to be zero extended. */),
1227 // Test case 84: Negate positive short.
1228 InstructionFoldingCase<uint32_t>(
1229 Header() + "%main = OpFunction %void None %void_func\n" +
1230 "%main_lab = OpLabel\n" +
1231 "%2 = OpSNegate %ushort %ushort_0x4400\n" +
1232 "OpReturn\n" +
1233 "OpFunctionEnd",
1234 2, 0xBC00 /* expected to be zero extended. */),
1235 // Test case 85: Fold 2 + 3 (short)
1236 InstructionFoldingCase<uint32_t>(
1237 Header() + "%main = OpFunction %void None %void_func\n" +
1238 "%main_lab = OpLabel\n" +
1239 "%2 = OpIAdd %short %short_2 %short_3\n" +
1240 "OpReturn\n" +
1241 "OpFunctionEnd",
1242 2, 5),
1243 // Test case 86: Fold 2 + -5 (short)
1244 InstructionFoldingCase<uint32_t>(
1245 Header() + "%main = OpFunction %void None %void_func\n" +
1246 "%main_lab = OpLabel\n" +
1247 "%2 = OpIAdd %short %short_2 %short_n5\n" +
1248 "OpReturn\n" +
1249 "OpFunctionEnd",
1250 2, -3),
1251 // Test case 87: Fold int(3ll)
1252 InstructionFoldingCase<uint32_t>(
1253 Header() + "%main = OpFunction %void None %void_func\n" +
1254 "%main_lab = OpLabel\n" +
1255 "%2 = OpSConvert %int %long_3\n" +
1256 "OpReturn\n" +
1257 "OpFunctionEnd",
1258 2, 3),
1259 // Test case 88: Fold short(-3ll)
1260 InstructionFoldingCase<uint32_t>(
1261 Header() + "%main = OpFunction %void None %void_func\n" +
1262 "%main_lab = OpLabel\n" +
1263 "%2 = OpSConvert %short %long_n3\n" +
1264 "OpReturn\n" +
1265 "OpFunctionEnd",
1266 2, -3),
1267 // Test case 89: Fold short(32768ll) - This should do a sign extend when
1268 // converting to short.
1269 InstructionFoldingCase<uint32_t>(
1270 Header() + "%main = OpFunction %void None %void_func\n" +
1271 "%main_lab = OpLabel\n" +
1272 "%2 = OpSConvert %short %long_32768\n" +
1273 "OpReturn\n" +
1274 "OpFunctionEnd",
1275 2, -32768),
1276 // Test case 90: Fold short(-57344) - This should do a sign extend when
1277 // converting to short making the upper bits 0.
1278 InstructionFoldingCase<uint32_t>(
1279 Header() + "%main = OpFunction %void None %void_func\n" +
1280 "%main_lab = OpLabel\n" +
1281 "%2 = OpSConvert %short %long_n57344\n" +
1282 "OpReturn\n" +
1283 "OpFunctionEnd",
1284 2, 8192),
1285 // Test case 91: Fold int(-5(short)). The -5 should be interpreted as an unsigned value, and be zero extended to 32-bits.
1286 InstructionFoldingCase<uint32_t>(
1287 Header() + "%main = OpFunction %void None %void_func\n" +
1288 "%main_lab = OpLabel\n" +
1289 "%2 = OpUConvert %uint %short_n5\n" +
1290 "OpReturn\n" +
1291 "OpFunctionEnd",
1292 2, 65531),
1293 // Test case 92: Fold short(-24(int)). The upper bits should be cleared. So 0xFFFFFFE8 should become 0x0000FFE8.
1294 InstructionFoldingCase<uint32_t>(
1295 Header() + "%main = OpFunction %void None %void_func\n" +
1296 "%main_lab = OpLabel\n" +
1297 "%2 = OpUConvert %ushort %int_n24\n" +
1298 "OpReturn\n" +
1299 "OpFunctionEnd",
1300 2, 65512),
1301 // Test case 93: Fold BitReverse of 1
1302 InstructionFoldingCase<uint32_t>(
1303 Header() + "%main = OpFunction %void None %void_func\n" +
1304 "%main_lab = OpLabel\n" +
1305 "%2 = OpBitReverse %uint %uint_1\n" +
1306 "OpReturn\n" +
1307 "OpFunctionEnd",
1308 2, 0x80000000),
1309 // Test case 94: Fold BitReverse of 0
1310 InstructionFoldingCase<uint32_t>(
1311 Header() + "%main = OpFunction %void None %void_func\n" +
1312 "%main_lab = OpLabel\n" +
1313 "%2 = OpBitReverse %uint %uint_0\n" +
1314 "OpReturn\n" +
1315 "OpFunctionEnd",
1316 2, 0),
1317 // Test case 95: Fold BitReverse of uint max
1318 InstructionFoldingCase<uint32_t>(
1319 Header() + "%main = OpFunction %void None %void_func\n" +
1320 "%main_lab = OpLabel\n" +
1321 "%2 = OpBitReverse %uint %uint_max\n" +
1322 "OpReturn\n" +
1323 "OpFunctionEnd",
1324 2, 0xffffffff),
1325 // Test case 96: Fold BitReverse of 0x0000FFFF
1326 InstructionFoldingCase<uint32_t>(
1327 Header() + "%main = OpFunction %void None %void_func\n" +
1328 "%main_lab = OpLabel\n" +
1329 "%2 = OpBitReverse %uint %uint_0x0000ffff\n" +
1330 "OpReturn\n" + "OpFunctionEnd",
1331 2, 0xffff0000),
1332 // Test case 97: Fold BitReverse of OpConstNull
1333 InstructionFoldingCase<uint32_t>(
1334 Header() + "%main = OpFunction %void None %void_func\n" +
1335 "%main_lab = OpLabel\n" +
1336 "%2 = OpBitReverse %uint %111\n" +
1337 "OpReturn\n" +
1338 "OpFunctionEnd",
1339 2, 0)
1340 ));
1341 // clang-format on
1342
1343 using LongIntegerInstructionFoldingTest =
1344 ::testing::TestWithParam<InstructionFoldingCase<uint64_t>>;
1345
1346 TEST_P(LongIntegerInstructionFoldingTest, Case) {
1347 const auto& tc = GetParam();
1348
1349 std::unique_ptr<IRContext> context;
1350 Instruction* inst;
1351 std::tie(context, inst) =
1352 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
1353 CheckForExpectedScalarConstant(
1354 inst, tc.expected_result, [](const analysis::Constant* c) {
1355 return c->AsScalarConstant()->GetU64BitValue();
1356 });
1357 }
1358
1359 // clang-format off
1360 INSTANTIATE_TEST_SUITE_P(
1361 TestCase, LongIntegerInstructionFoldingTest,
1362 ::testing::Values(
1363 // Test case 0: fold 1+4611686018427387904
1364 InstructionFoldingCase<uint64_t>(
1365 Header() + "%main = OpFunction %void None %void_func\n" +
1366 "%main_lab = OpLabel\n" +
1367 "%n = OpVariable %_ptr_int Function\n" +
1368 "%load = OpLoad %int %n\n" +
1369 "%2 = OpIAdd %long %long_1 %long_4611686018427387904\n" +
1370 "OpReturn\n" + "OpFunctionEnd",
1371 2, 1 + 4611686018427387904),
1372 // Test case 1: fold 1-4611686018427387904
1373 InstructionFoldingCase<uint64_t>(
1374 Header() + "%main = OpFunction %void None %void_func\n" +
1375 "%main_lab = OpLabel\n" +
1376 "%n = OpVariable %_ptr_int Function\n" +
1377 "%load = OpLoad %int %n\n" +
1378 "%2 = OpISub %long %long_1 %long_4611686018427387904\n" +
1379 "OpReturn\n" + "OpFunctionEnd",
1380 2, 1 - 4611686018427387904),
1381 // Test case 2: fold 2*4611686018427387904
1382 InstructionFoldingCase<uint64_t>(
1383 Header() + "%main = OpFunction %void None %void_func\n" +
1384 "%main_lab = OpLabel\n" +
1385 "%n = OpVariable %_ptr_int Function\n" +
1386 "%load = OpLoad %int %n\n" +
1387 "%2 = OpIMul %long %long_2 %long_4611686018427387904\n" +
1388 "OpReturn\n" + "OpFunctionEnd",
1389 2, 9223372036854775808ull),
1390 // Test case 3: fold 4611686018427387904/2 (unsigned)
1391 InstructionFoldingCase<uint64_t>(
1392 Header() + "%main = OpFunction %void None %void_func\n" +
1393 "%main_lab = OpLabel\n" +
1394 "%n = OpVariable %_ptr_int Function\n" +
1395 "%load = OpLoad %int %n\n" +
1396 "%2 = OpUDiv %ulong %ulong_4611686018427387904 %ulong_2\n" +
1397 "OpReturn\n" + "OpFunctionEnd",
1398 2, 4611686018427387904 / 2),
1399 // Test case 4: fold 4611686018427387904/2 (signed)
1400 InstructionFoldingCase<uint64_t>(
1401 Header() + "%main = OpFunction %void None %void_func\n" +
1402 "%main_lab = OpLabel\n" +
1403 "%n = OpVariable %_ptr_int Function\n" +
1404 "%load = OpLoad %int %n\n" +
1405 "%2 = OpSDiv %long %long_4611686018427387904 %long_2\n" +
1406 "OpReturn\n" + "OpFunctionEnd",
1407 2, 4611686018427387904 / 2),
1408 // Test case 5: fold -4611686018427387904/2 (signed)
1409 InstructionFoldingCase<uint64_t>(
1410 Header() + "%main = OpFunction %void None %void_func\n" +
1411 "%main_lab = OpLabel\n" +
1412 "%n = OpVariable %_ptr_int Function\n" +
1413 "%load = OpLoad %int %n\n" +
1414 "%2 = OpSDiv %long %long_n4611686018427387904 %long_2\n" +
1415 "OpReturn\n" + "OpFunctionEnd",
1416 2, -4611686018427387904 / 2),
1417 // Test case 6: fold 4611686018427387904 mod 7 (unsigned)
1418 InstructionFoldingCase<uint64_t>(
1419 Header() + "%main = OpFunction %void None %void_func\n" +
1420 "%main_lab = OpLabel\n" +
1421 "%n = OpVariable %_ptr_int Function\n" +
1422 "%load = OpLoad %int %n\n" +
1423 "%2 = OpUMod %ulong %ulong_4611686018427387904 %ulong_7\n" +
1424 "OpReturn\n" + "OpFunctionEnd",
1425 2, 4611686018427387904ull % 7ull),
1426 // Test case 7: fold 7 mod 3 (signed)
1427 InstructionFoldingCase<uint64_t>(
1428 Header() + "%main = OpFunction %void None %void_func\n" +
1429 "%main_lab = OpLabel\n" +
1430 "%n = OpVariable %_ptr_int Function\n" +
1431 "%load = OpLoad %int %n\n" +
1432 "%2 = OpSMod %long %long_7 %long_3\n" + "OpReturn\n" +
1433 "OpFunctionEnd",
1434 2, 1ull),
1435 // Test case 8: fold 7 rem 3 (signed)
1436 InstructionFoldingCase<uint64_t>(
1437 Header() + "%main = OpFunction %void None %void_func\n" +
1438 "%main_lab = OpLabel\n" +
1439 "%n = OpVariable %_ptr_int Function\n" +
1440 "%load = OpLoad %int %n\n" +
1441 "%2 = OpSRem %long %long_7 %long_3\n" + "OpReturn\n" +
1442 "OpFunctionEnd",
1443 2, 1ull),
1444 // Test case 9: fold 7 mod -3 (signed)
1445 InstructionFoldingCase<uint64_t>(
1446 Header() + "%main = OpFunction %void None %void_func\n" +
1447 "%main_lab = OpLabel\n" +
1448 "%n = OpVariable %_ptr_int Function\n" +
1449 "%load = OpLoad %int %n\n" +
1450 "%2 = OpSMod %long %long_7 %long_n3\n" + "OpReturn\n" +
1451 "OpFunctionEnd",
1452 2, -2ll),
1453 // Test case 10: fold 7 rem 3 (signed)
1454 InstructionFoldingCase<uint64_t>(
1455 Header() + "%main = OpFunction %void None %void_func\n" +
1456 "%main_lab = OpLabel\n" +
1457 "%n = OpVariable %_ptr_int Function\n" +
1458 "%load = OpLoad %int %n\n" +
1459 "%2 = OpSRem %long %long_7 %long_n3\n" + "OpReturn\n" +
1460 "OpFunctionEnd",
1461 2, 1ll),
1462 // Test case 11: fold -7 mod 3 (signed)
1463 InstructionFoldingCase<uint64_t>(
1464 Header() + "%main = OpFunction %void None %void_func\n" +
1465 "%main_lab = OpLabel\n" +
1466 "%n = OpVariable %_ptr_int Function\n" +
1467 "%load = OpLoad %int %n\n" +
1468 "%2 = OpSMod %long %long_n7 %long_3\n" + "OpReturn\n" +
1469 "OpFunctionEnd",
1470 2, 2ll),
1471 // Test case 12: fold -7 rem 3 (signed)
1472 InstructionFoldingCase<uint64_t>(
1473 Header() + "%main = OpFunction %void None %void_func\n" +
1474 "%main_lab = OpLabel\n" +
1475 "%n = OpVariable %_ptr_int Function\n" +
1476 "%load = OpLoad %int %n\n" +
1477 "%2 = OpSRem %long %long_n7 %long_3\n" + "OpReturn\n" +
1478 "OpFunctionEnd",
1479 2, -1ll),
1480 // Test case 13: fold long(-24)
1481 InstructionFoldingCase<uint64_t>(
1482 Header() + "%main = OpFunction %void None %void_func\n" +
1483 "%main_lab = OpLabel\n" +
1484 "%n = OpVariable %_ptr_int Function\n" +
1485 "%load = OpLoad %int %n\n" +
1486 "%2 = OpSConvert %long %int_n24\n" + "OpReturn\n" +
1487 "OpFunctionEnd",
1488 2, -24ll),
1489 // Test case 14: fold long(-24)
1490 InstructionFoldingCase<uint64_t>(
1491 Header() + "%main = OpFunction %void None %void_func\n" +
1492 "%main_lab = OpLabel\n" +
1493 "%n = OpVariable %_ptr_int Function\n" +
1494 "%load = OpLoad %int %n\n" + "%2 = OpSConvert %long %int_10\n" +
1495 "OpReturn\n" + "OpFunctionEnd",
1496 2, 10ll),
1497 // Test case 15: fold long(-24(short)).
1498 // The upper bits should be cleared. So 0xFFFFFFE8 should become
1499 // 0x000000000000FFE8.
1500 InstructionFoldingCase<uint64_t>(
1501 Header() + "%main = OpFunction %void None %void_func\n" +
1502 "%main_lab = OpLabel\n" + "%2 = OpUConvert %ulong %short_n5\n" +
1503 "OpReturn\n" + "OpFunctionEnd",
1504 2, 65531ull),
1505 // Test case 16: fold SMin long 0 -1.
1506 InstructionFoldingCase<uint64_t>(
1507 Header() + "%main = OpFunction %void None %void_func\n" +
1508 "%main_lab = OpLabel\n" +
1509 "%2 = OpExtInst %long %1 SMin %long_0 %long_n1\n" +
1510 "OpReturn\n" +
1511 "OpFunctionEnd",
1512 2, -1ll),
1513 // Test case 17: fold UMin ulong 9223372036854775809 max.
1514 InstructionFoldingCase<uint64_t>(
1515 Header() + "%main = OpFunction %void None %void_func\n" +
1516 "%main_lab = OpLabel\n" +
1517 "%2 = OpExtInst %ulong %1 UMin %ulong_9223372036854775809 %ulong_max\n" +
1518 "OpReturn\n" +
1519 "OpFunctionEnd",
1520 2, 9223372036854775809ull),
1521 // Test case 18: fold SMax long 0 -1.
1522 InstructionFoldingCase<uint64_t>(
1523 Header() + "%main = OpFunction %void None %void_func\n" +
1524 "%main_lab = OpLabel\n" +
1525 "%2 = OpExtInst %long %1 SMax %long_0 %long_n1\n" +
1526 "OpReturn\n" +
1527 "OpFunctionEnd",
1528 2, 0ll),
1529 // Test case 19: fold UMax ulong 9223372036854775809 max.
1530 InstructionFoldingCase<uint64_t>(
1531 Header() + "%main = OpFunction %void None %void_func\n" +
1532 "%main_lab = OpLabel\n" +
1533 "%2 = OpExtInst %ulong %1 UMax %ulong_9223372036854775809 %ulong_max\n" +
1534 "OpReturn\n" +
1535 "OpFunctionEnd",
1536 2, 18446744073709551615ull)
1537 ));
1538 // clang-format on
1539
1540 using UIntVectorInstructionFoldingTest =
1541 ::testing::TestWithParam<InstructionFoldingCase<std::vector<uint32_t>>>;
1542
1543 TEST_P(UIntVectorInstructionFoldingTest, Case) {
1544 const auto& tc = GetParam();
1545
1546 std::unique_ptr<IRContext> context;
1547 Instruction* inst;
1548 std::tie(context, inst) =
1549 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
1550
1551 CheckForExpectedVectorConstant(
1552 inst, tc.expected_result,
1553 [](const analysis::Constant* c) { return c->GetU32(); });
1554 }
1555
1556 // clang-format off
1557 INSTANTIATE_TEST_SUITE_P(TestCase, UIntVectorInstructionFoldingTest,
1558 ::testing::Values(
1559 // Test case 0: fold 0*n
1560 InstructionFoldingCase<std::vector<uint32_t>>(
1561 Header() + "%main = OpFunction %void None %void_func\n" +
1562 "%main_lab = OpLabel\n" +
1563 "%2 = OpVectorShuffle %v2int %v2int_2_2 %v2int_2_3 0 3\n" +
1564 "OpReturn\n" +
1565 "OpFunctionEnd",
1566 2, {2,3}),
1567 // Test case 1: fold vectorshuffle of nullconstant of type vec2int
1568 InstructionFoldingCase<std::vector<uint32_t>>(
1569 Header() + "%main = OpFunction %void None %void_func\n" +
1570 "%main_lab = OpLabel\n" +
1571 "%2 = OpVectorShuffle %v2int %v2int_null %v2int_2_3 0 3\n" +
1572 "OpReturn\n" +
1573 "OpFunctionEnd",
1574 2, {0,3}),
1575 // Test case 2: fold bit-cast int -24 to unsigned int
1576 InstructionFoldingCase<std::vector<uint32_t>>(
1577 Header() + "%main = OpFunction %void None %void_func\n" +
1578 "%main_lab = OpLabel\n" +
1579 "%2 = OpBitcast %v2uint %v2int_min_max\n" +
1580 "OpReturn\n" +
1581 "OpFunctionEnd",
1582 2, {2147483648, 2147483647}),
1583 // Test case 3: fold bit-cast nullconstant of type vec2uint to int2
1584 InstructionFoldingCase<std::vector<uint32_t>>(
1585 Header() + "%main = OpFunction %void None %void_func\n" +
1586 "%main_lab = OpLabel\n" +
1587 "%2 = OpBitcast %v2int %v2uint_null\n" +
1588 "OpReturn\n" +
1589 "OpFunctionEnd",
1590 2, {0, 0}),
1591 // Test case 4: fold bit-cast if any component is nullconstant
1592 InstructionFoldingCase<std::vector<uint32_t>>(
1593 Header() + "%main = OpFunction %void None %void_func\n" +
1594 "%main_lab = OpLabel\n" +
1595 "%2 = OpBitcast %v2int %v2uint_1_null\n" +
1596 "OpReturn\n" +
1597 "OpFunctionEnd",
1598 2, {1, 0}),
1599 // Test case 5: fold bit-cast v4ushort (0, 1, 2, 3) to v2uint
1600 InstructionFoldingCase<std::vector<uint32_t>>(
1601 Header() + "%main = OpFunction %void None %void_func\n" +
1602 "%main_lab = OpLabel\n" +
1603 "%2 = OpBitcast %v2uint %v4ushort_0_1_2_3\n" +
1604 "OpReturn\n" +
1605 "OpFunctionEnd",
1606 2, {0x00010000, 0x00030002}),
1607 // Test case 6: fold SNegate vector of uint
1608 InstructionFoldingCase<std::vector<uint32_t>>(
1609 Header() + "%main = OpFunction %void None %void_func\n" +
1610 "%main_lab = OpLabel\n" +
1611 "%2 = OpSNegate %v2uint %v2uint_0x3f800000_0xbf800000\n" +
1612 "OpReturn\n" +
1613 "OpFunctionEnd",
1614 2, {static_cast<uint32_t>(-0x3f800000), static_cast<uint32_t>(-0xbf800000ll)}),
1615 // Test case 7: fold vector components of uint (including integer overflow)
1616 InstructionFoldingCase<std::vector<uint32_t>>(
1617 Header() + "%main = OpFunction %void None %void_func\n" +
1618 "%main_lab = OpLabel\n" +
1619 "%2 = OpIAdd %v2uint %v2uint_0x3f800000_0xbf800000 %v2uint_0x3f800000_0xbf800000\n" +
1620 "OpReturn\n" +
1621 "OpFunctionEnd",
1622 2, {0x7f000000u, 0x7f000000u}),
1623 // Test case 8: fold vector components of uint
1624 InstructionFoldingCase<std::vector<uint32_t>>(
1625 Header() + "%main = OpFunction %void None %void_func\n" +
1626 "%main_lab = OpLabel\n" +
1627 "%2 = OpSConvert %v2int %v2short_2_n5\n" +
1628 "OpReturn\n" +
1629 "OpFunctionEnd",
1630 2, {2,static_cast<uint32_t>(-5)}),
1631 // Test case 9: fold vector components of uint (incuding integer overflow)
1632 InstructionFoldingCase<std::vector<uint32_t>>(
1633 Header() + "%main = OpFunction %void None %void_func\n" +
1634 "%main_lab = OpLabel\n" +
1635 "%2 = OpUConvert %v2uint %v2short_2_n5\n" +
1636 "OpReturn\n" +
1637 "OpFunctionEnd",
1638 2, {2,65531}),
1639 // Test case 10: fold bitreverse of a vector of uint
1640 InstructionFoldingCase<std::vector<uint32_t>>(
1641 Header() + "%main = OpFunction %void None %void_func\n" +
1642 "%main_lab = OpLabel\n" +
1643 "%2 = OpBitReverse %v4uint %v4uint_1_0x0000ffff_uint_0_uint_max\n" +
1644 "OpReturn\n" +
1645 "OpFunctionEnd",
1646 2, {0x80000000, 0xffff0000, 0, 0xffffffff}),
1647 // Test case 11: Fold bitreverse of ConstantNull of type v2uint
1648 InstructionFoldingCase<std::vector<uint32_t>>(
1649 Header() + "%main = OpFunction %void None %void_func\n" +
1650 "%main_lab = OpLabel\n" +
1651 "%2 = OpBitReverse %v2uint %v2uint_null\n" +
1652 "OpReturn\n" +
1653 "OpFunctionEnd",
1654 2, {0, 0}),
1655 // Test case 12: Fold bitreverse if any component is NullConstant
1656 InstructionFoldingCase<std::vector<uint32_t>>(
1657 Header() + "%main = OpFunction %void None %void_func\n" +
1658 "%main_lab = OpLabel\n" +
1659 "%2 = OpBitReverse %v2uint %v2uint_1_null\n" +
1660 "OpReturn\n" +
1661 "OpFunctionEnd",
1662 2, {0x80000000, 0})
1663 ));
1664 // clang-format on
1665
1666 using IntVectorInstructionFoldingTest =
1667 ::testing::TestWithParam<InstructionFoldingCase<std::vector<int32_t>>>;
1668
1669 TEST_P(IntVectorInstructionFoldingTest, Case) {
1670 const auto& tc = GetParam();
1671
1672 std::unique_ptr<IRContext> context;
1673 Instruction* inst;
1674 std::tie(context, inst) =
1675 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
1676
1677 CheckForExpectedVectorConstant(
1678 inst, tc.expected_result,
1679 [](const analysis::Constant* c) { return c->GetS32(); });
1680 }
1681
1682 // clang-format off
1683 INSTANTIATE_TEST_SUITE_P(TestCase, IntVectorInstructionFoldingTest,
1684 ::testing::Values(
1685 // Test case 0: fold negate of a vector
1686 InstructionFoldingCase<std::vector<int32_t>>(
1687 Header() + "%main = OpFunction %void None %void_func\n" +
1688 "%main_lab = OpLabel\n" +
1689 "%2 = OpSNegate %v2int %v2int_2_3\n" +
1690 "OpReturn\n" +
1691 "OpFunctionEnd",
1692 2, {-2, -3}),
1693 // Test case 1: fold negate of a vector containing negative values.
1694 InstructionFoldingCase<std::vector<int32_t>>(
1695 Header() + "%main = OpFunction %void None %void_func\n" +
1696 "%main_lab = OpLabel\n" +
1697 "%2 = OpSNegate %v2int %v2int_n1_n24\n" +
1698 "OpReturn\n" +
1699 "OpFunctionEnd",
1700 2, {1, 24}),
1701 // Test case 2: fold negate of a vector at the limits
1702 InstructionFoldingCase<std::vector<int32_t>>(
1703 Header() + "%main = OpFunction %void None %void_func\n" +
1704 "%main_lab = OpLabel\n" +
1705 "%2 = OpSNegate %v2int %v2int_min_max\n" +
1706 "OpReturn\n" +
1707 "OpFunctionEnd",
1708 2, {INT_MIN, -INT_MAX}),
1709 // Test case 3: fold vector components of int
1710 InstructionFoldingCase<std::vector<int32_t>>(
1711 Header() + "%main = OpFunction %void None %void_func\n" +
1712 "%main_lab = OpLabel\n" +
1713 "%2 = OpIMul %v2int %v2int_2_3 %v2int_2_3\n" +
1714 "OpReturn\n" +
1715 "OpFunctionEnd",
1716 2, {4,9})
1717 ));
1718 // clang-format on
1719
1720 using LongIntVectorInstructionFoldingTest =
1721 ::testing::TestWithParam<InstructionFoldingCase<std::vector<uint64_t>>>;
1722
1723 TEST_P(LongIntVectorInstructionFoldingTest, Case) {
1724 const auto& tc = GetParam();
1725
1726 std::unique_ptr<IRContext> context;
1727 Instruction* inst;
1728 std::tie(context, inst) =
1729 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
1730 CheckForExpectedVectorConstant(
1731 inst, tc.expected_result,
1732 [](const analysis::Constant* c) { return c->GetU64(); });
1733 }
1734
1735 // clang-format off
1736 INSTANTIATE_TEST_SUITE_P(TestCase, LongIntVectorInstructionFoldingTest,
1737 ::testing::Values(
1738 // Test case 0: fold {2,2} + {2,3} (Testing that the vector logic works
1739 // correctly. Scalar tests will check that the 64-bit values are correctly
1740 // folded.)
1741 InstructionFoldingCase<std::vector<uint64_t>>(
1742 Header() + "%main = OpFunction %void None %void_func\n" +
1743 "%main_lab = OpLabel\n" +
1744 "%n = OpVariable %_ptr_int Function\n" +
1745 "%load = OpLoad %int %n\n" +
1746 "%2 = OpIAdd %v2long %v2long_2_2 %v2long_2_3\n" +
1747 "OpReturn\n" +
1748 "OpFunctionEnd",
1749 2, {4,5}),
1750 // Test case 0: fold {2,2} / {2,3} (Testing that the vector logic works
1751 // correctly. Scalar tests will check that the 64-bit values are correctly
1752 // folded.)
1753 InstructionFoldingCase<std::vector<uint64_t>>(
1754 Header() + "%main = OpFunction %void None %void_func\n" +
1755 "%main_lab = OpLabel\n" +
1756 "%n = OpVariable %_ptr_int Function\n" +
1757 "%load = OpLoad %int %n\n" +
1758 "%2 = OpSDiv %v2long %v2long_2_2 %v2long_2_3\n" +
1759 "OpReturn\n" +
1760 "OpFunctionEnd",
1761 2, {1,0})
1762 ));
1763 // clang-format on
1764
1765 using DoubleVectorInstructionFoldingTest =
1766 ::testing::TestWithParam<InstructionFoldingCase<std::vector<double>>>;
1767
1768 TEST_P(DoubleVectorInstructionFoldingTest, Case) {
1769 const auto& tc = GetParam();
1770
1771 std::unique_ptr<IRContext> context;
1772 Instruction* inst;
1773 std::tie(context, inst) =
1774 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
1775 CheckForExpectedVectorConstant(
1776 inst, tc.expected_result,
1777 [](const analysis::Constant* c) { return c->GetDouble(); });
1778 }
1779
1780 // clang-format off
1781 INSTANTIATE_TEST_SUITE_P(TestCase, DoubleVectorInstructionFoldingTest,
1782 ::testing::Values(
1783 // Test case 0: bit-cast int {0x3FF00000,0x00000000,0xC05FD666,0x66666666}
1784 // to double vector
1785 InstructionFoldingCase<std::vector<double>>(
1786 Header() + "%main = OpFunction %void None %void_func\n" +
1787 "%main_lab = OpLabel\n" +
1788 "%2 = OpBitcast %v2double %v4int_0x3FF00000_0x00000000_0xC05FD666_0x66666666\n" +
1789 "OpReturn\n" +
1790 "OpFunctionEnd",
1791 2, {1.0,-127.35}),
1792 // Test case 1: OpVectorTimesMatrix Non-Zero Zero {{0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}} {1.0, 2.0, 3.0, 4.0} {0.0, 0.0, 0.0, 0.0}
1793 InstructionFoldingCase<std::vector<double>>(
1794 Header() +
1795 "%main = OpFunction %void None %void_func\n" +
1796 "%main_lab = OpLabel\n" +
1797 "%2 = OpVectorTimesMatrix %v4double %v4double_1_2_3_4 %mat4v4double_null\n" +
1798 "OpReturn\n" +
1799 "OpFunctionEnd",
1800 2, {0.0,0.0,0.0,0.0}),
1801 // Test case 2: OpVectorTimesMatrix Zero Non-Zero {{1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}} {0.0, 0.0, 0.0, 0.0} {0.0, 0.0, 0.0, 0.0}
1802 InstructionFoldingCase<std::vector<double>>(
1803 Header() +
1804 "%main = OpFunction %void None %void_func\n" +
1805 "%main_lab = OpLabel\n" +
1806 "%2 = OpVectorTimesMatrix %v4double %v4double_null %mat4v4double_1_2_3_4\n" +
1807 "OpReturn\n" +
1808 "OpFunctionEnd",
1809 2, {0.0,0.0,0.0,0.0}),
1810 // Test case 3: OpVectorTimesMatrix Non-Zero Non-Zero {{1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}} {1.0, 2.0, 3.0, 4.0} {30.0, 30.0, 30.0, 30.0}
1811 InstructionFoldingCase<std::vector<double>>(
1812 Header() +
1813 "%main = OpFunction %void None %void_func\n" +
1814 "%main_lab = OpLabel\n" +
1815 "%2 = OpVectorTimesMatrix %v4double %v4double_1_2_3_4 %mat4v4double_1_2_3_4\n" +
1816 "OpReturn\n" +
1817 "OpFunctionEnd",
1818 2, {30.0,30.0,30.0,30.0}),
1819 // Test case 4: OpVectorTimesMatrix Non-Zero Non-Zero {{1.0, 2.0, 3.0, 4.0}, Null, {1.0, 2.0, 3.0, 4.0}, Null} {1.0, 2.0, 3.0, 4.0} {30.0, 0.0, 30.0, 0.0}
1820 InstructionFoldingCase<std::vector<double>>(
1821 Header() +
1822 "%main = OpFunction %void None %void_func\n" +
1823 "%main_lab = OpLabel\n" +
1824 "%2 = OpVectorTimesMatrix %v4double %v4double_1_2_3_4 %mat4v4double_1_2_3_4_null\n" +
1825 "OpReturn\n" +
1826 "OpFunctionEnd",
1827 2, {30.0,0.0,30.0,0.0}),
1828 // Test case 5: OpMatrixTimesVector Zero Non-Zero {1.0, 2.0, 3.0, 4.0} {{0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}} {0.0, 0.0, 0.0, 0.0}
1829 InstructionFoldingCase<std::vector<double>>(
1830 Header() +
1831 "%main = OpFunction %void None %void_func\n" +
1832 "%main_lab = OpLabel\n" +
1833 "%2 = OpMatrixTimesVector %v4double %mat4v4double_null %v4double_1_2_3_4\n" +
1834 "OpReturn\n" +
1835 "OpFunctionEnd",
1836 2, {0.0,0.0,0.0,0.0}),
1837 // Test case 6: OpMatrixTimesVector Non-Zero Zero {{1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}} {0.0, 0.0, 0.0, 0.0} {0.0, 0.0, 0.0, 0.0}
1838 InstructionFoldingCase<std::vector<double>>(
1839 Header() +
1840 "%main = OpFunction %void None %void_func\n" +
1841 "%main_lab = OpLabel\n" +
1842 "%2 = OpMatrixTimesVector %v4double %mat4v4double_1_2_3_4 %v4double_null\n" +
1843 "OpReturn\n" +
1844 "OpFunctionEnd",
1845 2, {0.0,0.0,0.0,0.0}),
1846 // Test case 7: OpMatrixTimesVector Non-Zero Non-Zero {1.0, 2.0, 3.0, 4.0} {{1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}} {10.0, 20.0, 30.0, 40.0}
1847 InstructionFoldingCase<std::vector<double>>(
1848 Header() +
1849 "%main = OpFunction %void None %void_func\n" +
1850 "%main_lab = OpLabel\n" +
1851 "%2 = OpMatrixTimesVector %v4double %mat4v4double_1_2_3_4 %v4double_1_2_3_4\n" +
1852 "OpReturn\n" +
1853 "OpFunctionEnd",
1854 2, {10.0,20.0,30.0,40.0}),
1855 // Test case 8: OpMatrixTimesVector Non-Zero Non-Zero {1.0, 2.0, 3.0, 4.0} {{1.0, 2.0, 3.0, 4.0}, Null, {1.0, 2.0, 3.0, 4.0}, Null} {10.0, 20.0, 30.0, 40.0}
1856 InstructionFoldingCase<std::vector<double>>(
1857 Header() +
1858 "%main = OpFunction %void None %void_func\n" +
1859 "%main_lab = OpLabel\n" +
1860 "%2 = OpMatrixTimesVector %v4double %mat4v4double_1_2_3_4_null %v4double_1_2_3_4\n" +
1861 "OpReturn\n" +
1862 "OpFunctionEnd",
1863 2, {4.0,8.0,12.0,16.0})
1864 ));
1865
1866 using FloatVectorInstructionFoldingTest =
1867 ::testing::TestWithParam<InstructionFoldingCase<std::vector<float>>>;
1868
1869 TEST_P(FloatVectorInstructionFoldingTest, Case) {
1870 const auto& tc = GetParam();
1871
1872 std::unique_ptr<IRContext> context;
1873 Instruction* inst;
1874 std::tie(context, inst) = FoldInstruction(tc.test_body, tc.id_to_fold,SPV_ENV_UNIVERSAL_1_1);
1875 CheckForExpectedVectorConstant(inst, tc.expected_result, [](const analysis::Constant* c){ return c->GetFloat();});
1876 }
1877
1878 // clang-format off
1879 INSTANTIATE_TEST_SUITE_P(TestCase, FloatVectorInstructionFoldingTest,
1880 ::testing::Values(
1881 // Test case 0: FMix {2.0, 3.0}, {0.0, 0.0} {0.2,0.5}
1882 InstructionFoldingCase<std::vector<float>>(
1883 Header() + "%main = OpFunction %void None %void_func\n" +
1884 "%main_lab = OpLabel\n" +
1885 "%2 = OpExtInst %v2float %1 FMix %v2float_2_3 %v2float_0_0 %v2float_0p2_0p5\n" +
1886 "OpReturn\n" +
1887 "OpFunctionEnd",
1888 2, {1.6f,1.5f}),
1889 // Test case 1: bit-cast unsigned int vector {0x3f800000, 0xbf800000} to
1890 // float vector
1891 InstructionFoldingCase<std::vector<float>>(
1892 Header() + "%main = OpFunction %void None %void_func\n" +
1893 "%main_lab = OpLabel\n" +
1894 "%2 = OpBitcast %v2float %v2uint_0x3f800000_0xbf800000\n" +
1895 "OpReturn\n" +
1896 "OpFunctionEnd",
1897 2, {1.0f,-1.0f}),
1898 // Test case 2: bit-cast long int 0xbf8000003f800000 to float vector
1899 InstructionFoldingCase<std::vector<float>>(
1900 Header() + "%main = OpFunction %void None %void_func\n" +
1901 "%main_lab = OpLabel\n" +
1902 "%2 = OpBitcast %v2float %long_0xbf8000003f800000\n" +
1903 "OpReturn\n" +
1904 "OpFunctionEnd",
1905 2, {1.0f,-1.0f}),
1906 // Test case 3: OpVectorTimesMatrix Non-Zero Zero {{0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}} {1.0, 2.0, 3.0, 4.0} {0.0, 0.0, 0.0, 0.0}
1907 InstructionFoldingCase<std::vector<float>>(
1908 Header() +
1909 "%main = OpFunction %void None %void_func\n" +
1910 "%main_lab = OpLabel\n" +
1911 "%2 = OpVectorTimesMatrix %v4float %v4float_1_2_3_4 %mat4v4float_null\n" +
1912 "OpReturn\n" +
1913 "OpFunctionEnd",
1914 2, {0.0f,0.0f,0.0f,0.0f}),
1915 // Test case 4: OpVectorTimesMatrix Non-Zero Non-Zero {{1.0, 2.0, 3.0, 4.0}, Null, {1.0, 2.0, 3.0, 4.0}, Null} {1.0, 2.0, 3.0, 4.0} {30.0, 0.0, 30.0, 0.0}
1916 InstructionFoldingCase<std::vector<float>>(
1917 Header() +
1918 "%main = OpFunction %void None %void_func\n" +
1919 "%main_lab = OpLabel\n" +
1920 "%2 = OpVectorTimesMatrix %v4float %v4float_1_2_3_4 %mat4v4float_1_2_3_4_null\n" +
1921 "OpReturn\n" +
1922 "OpFunctionEnd",
1923 2, {30.0,0.0,30.0,0.0}),
1924 // Test case 5: OpVectorTimesMatrix Zero Non-Zero {{1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}} {0.0, 0.0, 0.0, 0.0} {0.0, 0.0, 0.0, 0.0}
1925 InstructionFoldingCase<std::vector<float>>(
1926 Header() +
1927 "%main = OpFunction %void None %void_func\n" +
1928 "%main_lab = OpLabel\n" +
1929 "%2 = OpVectorTimesMatrix %v4float %v4float_null %mat4v4float_1_2_3_4\n" +
1930 "OpReturn\n" +
1931 "OpFunctionEnd",
1932 2, {0.0f,0.0f,0.0f,0.0f}),
1933 // Test case 6: OpVectorTimesMatrix Non-Zero Non-Zero {{1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}} {1.0, 2.0, 3.0, 4.0} {30.0, 30.0, 30.0, 30.0}
1934 InstructionFoldingCase<std::vector<float>>(
1935 Header() +
1936 "%main = OpFunction %void None %void_func\n" +
1937 "%main_lab = OpLabel\n" +
1938 "%2 = OpVectorTimesMatrix %v4float %v4float_1_2_3_4 %mat4v4float_1_2_3_4\n" +
1939 "OpReturn\n" +
1940 "OpFunctionEnd",
1941 2, {30.0f,30.0f,30.0f,30.0f}),
1942 // Test case 7: OpMatrixTimesVector Zero Non-Zero {1.0, 2.0, 3.0, 4.0} {{0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}, {0.0, 0.0, 0.0, 0.0}} {0.0, 0.0, 0.0, 0.0}
1943 InstructionFoldingCase<std::vector<float>>(
1944 Header() +
1945 "%main = OpFunction %void None %void_func\n" +
1946 "%main_lab = OpLabel\n" +
1947 "%2 = OpMatrixTimesVector %v4float %mat4v4float_null %v4float_1_2_3_4\n" +
1948 "OpReturn\n" +
1949 "OpFunctionEnd",
1950 2, {0.0f,0.0f,0.0f,0.0f}),
1951 // Test case 8: OpMatrixTimesVector Non-Zero Zero {{1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}} {0.0, 0.0, 0.0, 0.0} {0.0, 0.0, 0.0, 0.0}
1952 InstructionFoldingCase<std::vector<float>>(
1953 Header() +
1954 "%main = OpFunction %void None %void_func\n" +
1955 "%main_lab = OpLabel\n" +
1956 "%2 = OpMatrixTimesVector %v4float %mat4v4float_1_2_3_4 %v4float_null\n" +
1957 "OpReturn\n" +
1958 "OpFunctionEnd",
1959 2, {0.0f,0.0f,0.0f,0.0f}),
1960 // Test case 9: OpMatrixTimesVector Non-Zero Non-Zero {1.0, 2.0, 3.0, 4.0} {{1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}, {1.0, 2.0, 3.0, 4.0}} {10.0, 20.0, 30.0, 40.0}
1961 InstructionFoldingCase<std::vector<float>>(
1962 Header() +
1963 "%main = OpFunction %void None %void_func\n" +
1964 "%main_lab = OpLabel\n" +
1965 "%2 = OpMatrixTimesVector %v4float %mat4v4float_1_2_3_4 %v4float_1_2_3_4\n" +
1966 "OpReturn\n" +
1967 "OpFunctionEnd",
1968 2, {10.0f,20.0f,30.0f,40.0f}),
1969 // Test case 10: OpMatrixTimesVector Non-Zero Non-Zero {1.0, 2.0, 3.0, 4.0} {{1.0, 2.0, 3.0, 4.0}, Null, {1.0, 2.0, 3.0, 4.0}, Null} {10.0, 20.0, 30.0, 40.0}
1970 InstructionFoldingCase<std::vector<float>>(
1971 Header() +
1972 "%main = OpFunction %void None %void_func\n" +
1973 "%main_lab = OpLabel\n" +
1974 "%2 = OpMatrixTimesVector %v4float %mat4v4float_1_2_3_4_null %v4float_1_2_3_4\n" +
1975 "OpReturn\n" +
1976 "OpFunctionEnd",
1977 2, {4.0,8.0,12.0,16.0})
1978 ));
1979 // clang-format on
1980
1981 using FloatMatrixInstructionFoldingTest = ::testing::TestWithParam<
1982 InstructionFoldingCase<std::vector<std::vector<float>>>>;
1983
1984 TEST_P(FloatMatrixInstructionFoldingTest, Case) {
1985 const auto& tc = GetParam();
1986
1987 std::unique_ptr<IRContext> context;
1988 Instruction* inst;
1989 std::tie(context, inst) =
1990 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
1991
1992 EXPECT_EQ(inst->opcode(), spv::Op::OpCopyObject);
1993 if (inst->opcode() == spv::Op::OpCopyObject) {
1994 analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
1995 inst = def_use_mgr->GetDef(inst->GetSingleWordInOperand(0));
1996 analysis::ConstantManager* const_mgr = context->get_constant_mgr();
1997 const analysis::Constant* result = const_mgr->GetConstantFromInst(inst);
1998 EXPECT_NE(result, nullptr);
1999 if (result != nullptr) {
2000 std::vector<const analysis::Constant*> matrix =
2001 result->AsMatrixConstant()->GetComponents();
2002 EXPECT_EQ(matrix.size(), tc.expected_result.size());
2003 for (size_t c = 0; c < matrix.size(); c++) {
2004 if (matrix[c]->AsNullConstant() != nullptr) {
2005 matrix[c] = const_mgr->GetNullCompositeConstant(matrix[c]->type());
2006 }
2007 const analysis::VectorConstant* column_const =
2008 matrix[c]->AsVectorConstant();
2009 ASSERT_NE(column_const, nullptr);
2010 const std::vector<const analysis::Constant*>& column =
2011 column_const->GetComponents();
2012 EXPECT_EQ(column.size(), tc.expected_result[c].size());
2013 for (size_t r = 0; r < column.size(); r++) {
2014 EXPECT_EQ(tc.expected_result[c][r], column[r]->GetFloat());
2015 }
2016 }
2017 }
2018 }
2019 }
2020
2021 // clang-format off
2022 INSTANTIATE_TEST_SUITE_P(TestCase, FloatMatrixInstructionFoldingTest,
2023 ::testing::Values(
2024 // Test case 0: OpTranspose square null matrix
2025 InstructionFoldingCase<std::vector<std::vector<float>>>(
2026 Header() + "%main = OpFunction %void None %void_func\n" +
2027 "%main_lab = OpLabel\n" +
2028 "%2 = OpTranspose %mat4v4float %mat4v4float_null\n" +
2029 "OpReturn\n" +
2030 "OpFunctionEnd",
2031 2, {{0.0f, 0.0f, 0.0f, 0.0f},{0.0f, 0.0f, 0.0f, 0.0f},{0.0f, 0.0f, 0.0f, 0.0f},{0.0f, 0.0f, 0.0f, 0.0f}}),
2032 // Test case 1: OpTranspose rectangular null matrix
2033 InstructionFoldingCase<std::vector<std::vector<float>>>(
2034 Header() + "%main = OpFunction %void None %void_func\n" +
2035 "%main_lab = OpLabel\n" +
2036 "%2 = OpTranspose %mat4v2float %mat2v4float_null\n" +
2037 "OpReturn\n" +
2038 "OpFunctionEnd",
2039 2, {{0.0f, 0.0f},{0.0f, 0.0f},{0.0f, 0.0f},{0.0f, 0.0f}}),
2040 InstructionFoldingCase<std::vector<std::vector<float>>>(
2041 Header() + "%main = OpFunction %void None %void_func\n" +
2042 "%main_lab = OpLabel\n" +
2043 "%2 = OpTranspose %mat4v4float %mat4v4float_1_2_3_4\n" +
2044 "OpReturn\n" +
2045 "OpFunctionEnd",
2046 2, {{1.0f, 1.0f, 1.0f, 1.0f},{2.0f, 2.0f, 2.0f, 2.0f},{3.0f, 3.0f, 3.0f, 3.0f},{4.0f, 4.0f, 4.0f, 4.0f}})
2047 ));
2048 // clang-format on
2049
2050 using BooleanInstructionFoldingTest =
2051 ::testing::TestWithParam<InstructionFoldingCase<bool>>;
2052
2053 TEST_P(BooleanInstructionFoldingTest, Case) {
2054 const auto& tc = GetParam();
2055
2056 std::unique_ptr<IRContext> context;
2057 Instruction* inst;
2058 std::tie(context, inst) =
2059 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
2060 CheckForExpectedScalarConstant(
2061 inst, tc.expected_result,
2062 [](const analysis::Constant* c) { return c->AsBoolConstant()->value(); });
2063 }
2064
2065 // clang-format off
2066 INSTANTIATE_TEST_SUITE_P(TestCase, BooleanInstructionFoldingTest,
2067 ::testing::Values(
2068 // Test case 0: fold true || n
2069 InstructionFoldingCase<bool>(
2070 Header() + "%main = OpFunction %void None %void_func\n" +
2071 "%main_lab = OpLabel\n" +
2072 "%n = OpVariable %_ptr_bool Function\n" +
2073 "%load = OpLoad %bool %n\n" +
2074 "%2 = OpLogicalOr %bool %true %load\n" +
2075 "OpReturn\n" +
2076 "OpFunctionEnd",
2077 2, true),
2078 // Test case 1: fold n || true
2079 InstructionFoldingCase<bool>(
2080 Header() + "%main = OpFunction %void None %void_func\n" +
2081 "%main_lab = OpLabel\n" +
2082 "%n = OpVariable %_ptr_bool Function\n" +
2083 "%load = OpLoad %bool %n\n" +
2084 "%2 = OpLogicalOr %bool %load %true\n" +
2085 "OpReturn\n" +
2086 "OpFunctionEnd",
2087 2, true),
2088 // Test case 2: fold false && n
2089 InstructionFoldingCase<bool>(
2090 Header() + "%main = OpFunction %void None %void_func\n" +
2091 "%main_lab = OpLabel\n" +
2092 "%n = OpVariable %_ptr_bool Function\n" +
2093 "%load = OpLoad %bool %n\n" +
2094 "%2 = OpLogicalAnd %bool %false %load\n" +
2095 "OpReturn\n" +
2096 "OpFunctionEnd",
2097 2, false),
2098 // Test case 3: fold n && false
2099 InstructionFoldingCase<bool>(
2100 Header() + "%main = OpFunction %void None %void_func\n" +
2101 "%main_lab = OpLabel\n" +
2102 "%n = OpVariable %_ptr_bool Function\n" +
2103 "%load = OpLoad %bool %n\n" +
2104 "%2 = OpLogicalAnd %bool %load %false\n" +
2105 "OpReturn\n" +
2106 "OpFunctionEnd",
2107 2, false),
2108 // Test case 4: fold n < 0 (unsigned)
2109 InstructionFoldingCase<bool>(
2110 Header() + "%main = OpFunction %void None %void_func\n" +
2111 "%main_lab = OpLabel\n" +
2112 "%n = OpVariable %_ptr_uint Function\n" +
2113 "%load = OpLoad %uint %n\n" +
2114 "%2 = OpULessThan %bool %load %uint_0\n" +
2115 "OpReturn\n" +
2116 "OpFunctionEnd",
2117 2, false),
2118 // Test case 5: fold UINT_MAX < n (unsigned)
2119 InstructionFoldingCase<bool>(
2120 Header() + "%main = OpFunction %void None %void_func\n" +
2121 "%main_lab = OpLabel\n" +
2122 "%n = OpVariable %_ptr_uint Function\n" +
2123 "%load = OpLoad %uint %n\n" +
2124 "%2 = OpULessThan %bool %uint_max %load\n" +
2125 "OpReturn\n" +
2126 "OpFunctionEnd",
2127 2, false),
2128 // Test case 6: fold INT_MAX < n (signed)
2129 InstructionFoldingCase<bool>(
2130 Header() + "%main = OpFunction %void None %void_func\n" +
2131 "%main_lab = OpLabel\n" +
2132 "%n = OpVariable %_ptr_int Function\n" +
2133 "%load = OpLoad %int %n\n" +
2134 "%2 = OpSLessThan %bool %int_max %load\n" +
2135 "OpReturn\n" +
2136 "OpFunctionEnd",
2137 2, false),
2138 // Test case 7: fold n < INT_MIN (signed)
2139 InstructionFoldingCase<bool>(
2140 Header() + "%main = OpFunction %void None %void_func\n" +
2141 "%main_lab = OpLabel\n" +
2142 "%n = OpVariable %_ptr_int Function\n" +
2143 "%load = OpLoad %int %n\n" +
2144 "%2 = OpSLessThan %bool %load %int_min\n" +
2145 "OpReturn\n" +
2146 "OpFunctionEnd",
2147 2, false),
2148 // Test case 8: fold 0 > n (unsigned)
2149 InstructionFoldingCase<bool>(
2150 Header() + "%main = OpFunction %void None %void_func\n" +
2151 "%main_lab = OpLabel\n" +
2152 "%n = OpVariable %_ptr_uint Function\n" +
2153 "%load = OpLoad %uint %n\n" +
2154 "%2 = OpUGreaterThan %bool %uint_0 %load\n" +
2155 "OpReturn\n" +
2156 "OpFunctionEnd",
2157 2, false),
2158 // Test case 9: fold n > UINT_MAX (unsigned)
2159 InstructionFoldingCase<bool>(
2160 Header() + "%main = OpFunction %void None %void_func\n" +
2161 "%main_lab = OpLabel\n" +
2162 "%n = OpVariable %_ptr_uint Function\n" +
2163 "%load = OpLoad %uint %n\n" +
2164 "%2 = OpUGreaterThan %bool %load %uint_max\n" +
2165 "OpReturn\n" +
2166 "OpFunctionEnd",
2167 2, false),
2168 // Test case 10: fold n > INT_MAX (signed)
2169 InstructionFoldingCase<bool>(
2170 Header() + "%main = OpFunction %void None %void_func\n" +
2171 "%main_lab = OpLabel\n" +
2172 "%n = OpVariable %_ptr_int Function\n" +
2173 "%load = OpLoad %int %n\n" +
2174 "%2 = OpSGreaterThan %bool %load %int_max\n" +
2175 "OpReturn\n" +
2176 "OpFunctionEnd",
2177 2, false),
2178 // Test case 11: fold INT_MIN > n (signed)
2179 InstructionFoldingCase<bool>(
2180 Header() + "%main = OpFunction %void None %void_func\n" +
2181 "%main_lab = OpLabel\n" +
2182 "%n = OpVariable %_ptr_uint Function\n" +
2183 "%load = OpLoad %uint %n\n" +
2184 "%2 = OpSGreaterThan %bool %int_min %load\n" +
2185 "OpReturn\n" +
2186 "OpFunctionEnd",
2187 2, false),
2188 // Test case 12: fold 0 <= n (unsigned)
2189 InstructionFoldingCase<bool>(
2190 Header() + "%main = OpFunction %void None %void_func\n" +
2191 "%main_lab = OpLabel\n" +
2192 "%n = OpVariable %_ptr_uint Function\n" +
2193 "%load = OpLoad %uint %n\n" +
2194 "%2 = OpULessThanEqual %bool %uint_0 %load\n" +
2195 "OpReturn\n" +
2196 "OpFunctionEnd",
2197 2, true),
2198 // Test case 13: fold n <= UINT_MAX (unsigned)
2199 InstructionFoldingCase<bool>(
2200 Header() + "%main = OpFunction %void None %void_func\n" +
2201 "%main_lab = OpLabel\n" +
2202 "%n = OpVariable %_ptr_uint Function\n" +
2203 "%load = OpLoad %uint %n\n" +
2204 "%2 = OpULessThanEqual %bool %load %uint_max\n" +
2205 "OpReturn\n" +
2206 "OpFunctionEnd",
2207 2, true),
2208 // Test case 14: fold INT_MIN <= n (signed)
2209 InstructionFoldingCase<bool>(
2210 Header() + "%main = OpFunction %void None %void_func\n" +
2211 "%main_lab = OpLabel\n" +
2212 "%n = OpVariable %_ptr_int Function\n" +
2213 "%load = OpLoad %int %n\n" +
2214 "%2 = OpSLessThanEqual %bool %int_min %load\n" +
2215 "OpReturn\n" +
2216 "OpFunctionEnd",
2217 2, true),
2218 // Test case 15: fold n <= INT_MAX (signed)
2219 InstructionFoldingCase<bool>(
2220 Header() + "%main = OpFunction %void None %void_func\n" +
2221 "%main_lab = OpLabel\n" +
2222 "%n = OpVariable %_ptr_int Function\n" +
2223 "%load = OpLoad %int %n\n" +
2224 "%2 = OpSLessThanEqual %bool %load %int_max\n" +
2225 "OpReturn\n" +
2226 "OpFunctionEnd",
2227 2, true),
2228 // Test case 16: fold n >= 0 (unsigned)
2229 InstructionFoldingCase<bool>(
2230 Header() + "%main = OpFunction %void None %void_func\n" +
2231 "%main_lab = OpLabel\n" +
2232 "%n = OpVariable %_ptr_uint Function\n" +
2233 "%load = OpLoad %uint %n\n" +
2234 "%2 = OpUGreaterThanEqual %bool %load %uint_0\n" +
2235 "OpReturn\n" +
2236 "OpFunctionEnd",
2237 2, true),
2238 // Test case 17: fold UINT_MAX >= n (unsigned)
2239 InstructionFoldingCase<bool>(
2240 Header() + "%main = OpFunction %void None %void_func\n" +
2241 "%main_lab = OpLabel\n" +
2242 "%n = OpVariable %_ptr_uint Function\n" +
2243 "%load = OpLoad %uint %n\n" +
2244 "%2 = OpUGreaterThanEqual %bool %uint_max %load\n" +
2245 "OpReturn\n" +
2246 "OpFunctionEnd",
2247 2, true),
2248 // Test case 18: fold n >= INT_MIN (signed)
2249 InstructionFoldingCase<bool>(
2250 Header() + "%main = OpFunction %void None %void_func\n" +
2251 "%main_lab = OpLabel\n" +
2252 "%n = OpVariable %_ptr_int Function\n" +
2253 "%load = OpLoad %int %n\n" +
2254 "%2 = OpSGreaterThanEqual %bool %load %int_min\n" +
2255 "OpReturn\n" +
2256 "OpFunctionEnd",
2257 2, true),
2258 // Test case 19: fold INT_MAX >= n (signed)
2259 InstructionFoldingCase<bool>(
2260 Header() + "%main = OpFunction %void None %void_func\n" +
2261 "%main_lab = OpLabel\n" +
2262 "%n = OpVariable %_ptr_int Function\n" +
2263 "%load = OpLoad %int %n\n" +
2264 "%2 = OpSGreaterThanEqual %bool %int_max %load\n" +
2265 "OpReturn\n" +
2266 "OpFunctionEnd",
2267 2, true)
2268 ));
2269
2270 INSTANTIATE_TEST_SUITE_P(FClampAndCmpLHS, BooleanInstructionFoldingTest,
2271 ::testing::Values(
2272 // Test case 0: fold 0.0 > clamp(n, 0.0, 1.0)
2273 InstructionFoldingCase<bool>(
2274 Header() + "%main = OpFunction %void None %void_func\n" +
2275 "%main_lab = OpLabel\n" +
2276 "%n = OpVariable %_ptr_float Function\n" +
2277 "%ld = OpLoad %float %n\n" +
2278 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
2279 "%2 = OpFOrdGreaterThan %bool %float_0 %clamp\n" +
2280 "OpReturn\n" +
2281 "OpFunctionEnd",
2282 2, false),
2283 // Test case 1: fold 0.0 > clamp(n, -1.0, -1.0)
2284 InstructionFoldingCase<bool>(
2285 Header() + "%main = OpFunction %void None %void_func\n" +
2286 "%main_lab = OpLabel\n" +
2287 "%n = OpVariable %_ptr_float Function\n" +
2288 "%ld = OpLoad %float %n\n" +
2289 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_n1\n" +
2290 "%2 = OpFOrdGreaterThan %bool %float_0 %clamp\n" +
2291 "OpReturn\n" +
2292 "OpFunctionEnd",
2293 2, true),
2294 // Test case 2: fold 0.0 >= clamp(n, 1, 2)
2295 InstructionFoldingCase<bool>(
2296 Header() + "%main = OpFunction %void None %void_func\n" +
2297 "%main_lab = OpLabel\n" +
2298 "%n = OpVariable %_ptr_float Function\n" +
2299 "%ld = OpLoad %float %n\n" +
2300 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2301 "%2 = OpFOrdGreaterThanEqual %bool %float_0 %clamp\n" +
2302 "OpReturn\n" +
2303 "OpFunctionEnd",
2304 2, false),
2305 // Test case 3: fold 0.0 >= clamp(n, -1.0, 0.0)
2306 InstructionFoldingCase<bool>(
2307 Header() + "%main = OpFunction %void None %void_func\n" +
2308 "%main_lab = OpLabel\n" +
2309 "%n = OpVariable %_ptr_float Function\n" +
2310 "%ld = OpLoad %float %n\n" +
2311 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
2312 "%2 = OpFOrdGreaterThanEqual %bool %float_0 %clamp\n" +
2313 "OpReturn\n" +
2314 "OpFunctionEnd",
2315 2, true),
2316 // Test case 4: fold 0.0 <= clamp(n, 0.0, 1.0)
2317 InstructionFoldingCase<bool>(
2318 Header() + "%main = OpFunction %void None %void_func\n" +
2319 "%main_lab = OpLabel\n" +
2320 "%n = OpVariable %_ptr_float Function\n" +
2321 "%ld = OpLoad %float %n\n" +
2322 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
2323 "%2 = OpFOrdLessThanEqual %bool %float_0 %clamp\n" +
2324 "OpReturn\n" +
2325 "OpFunctionEnd",
2326 2, true),
2327 // Test case 5: fold 0.0 <= clamp(n, -1.0, -1.0)
2328 InstructionFoldingCase<bool>(
2329 Header() + "%main = OpFunction %void None %void_func\n" +
2330 "%main_lab = OpLabel\n" +
2331 "%n = OpVariable %_ptr_float Function\n" +
2332 "%ld = OpLoad %float %n\n" +
2333 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_n1\n" +
2334 "%2 = OpFOrdLessThanEqual %bool %float_0 %clamp\n" +
2335 "OpReturn\n" +
2336 "OpFunctionEnd",
2337 2, false),
2338 // Test case 6: fold 0.0 < clamp(n, 1, 2)
2339 InstructionFoldingCase<bool>(
2340 Header() + "%main = OpFunction %void None %void_func\n" +
2341 "%main_lab = OpLabel\n" +
2342 "%n = OpVariable %_ptr_float Function\n" +
2343 "%ld = OpLoad %float %n\n" +
2344 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2345 "%2 = OpFOrdLessThan %bool %float_0 %clamp\n" +
2346 "OpReturn\n" +
2347 "OpFunctionEnd",
2348 2, true),
2349 // Test case 7: fold 0.0 < clamp(n, -1.0, 0.0)
2350 InstructionFoldingCase<bool>(
2351 Header() + "%main = OpFunction %void None %void_func\n" +
2352 "%main_lab = OpLabel\n" +
2353 "%n = OpVariable %_ptr_float Function\n" +
2354 "%ld = OpLoad %float %n\n" +
2355 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
2356 "%2 = OpFOrdLessThan %bool %float_0 %clamp\n" +
2357 "OpReturn\n" +
2358 "OpFunctionEnd",
2359 2, false),
2360 // Test case 8: fold 0.0 > clamp(n, 0.0, 1.0)
2361 InstructionFoldingCase<bool>(
2362 Header() + "%main = OpFunction %void None %void_func\n" +
2363 "%main_lab = OpLabel\n" +
2364 "%n = OpVariable %_ptr_float Function\n" +
2365 "%ld = OpLoad %float %n\n" +
2366 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
2367 "%2 = OpFUnordGreaterThan %bool %float_0 %clamp\n" +
2368 "OpReturn\n" +
2369 "OpFunctionEnd",
2370 2, false),
2371 // Test case 9: fold 0.0 > clamp(n, -1.0, -1.0)
2372 InstructionFoldingCase<bool>(
2373 Header() + "%main = OpFunction %void None %void_func\n" +
2374 "%main_lab = OpLabel\n" +
2375 "%n = OpVariable %_ptr_float Function\n" +
2376 "%ld = OpLoad %float %n\n" +
2377 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_n1\n" +
2378 "%2 = OpFUnordGreaterThan %bool %float_0 %clamp\n" +
2379 "OpReturn\n" +
2380 "OpFunctionEnd",
2381 2, true),
2382 // Test case 10: fold 0.0 >= clamp(n, 1, 2)
2383 InstructionFoldingCase<bool>(
2384 Header() + "%main = OpFunction %void None %void_func\n" +
2385 "%main_lab = OpLabel\n" +
2386 "%n = OpVariable %_ptr_float Function\n" +
2387 "%ld = OpLoad %float %n\n" +
2388 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2389 "%2 = OpFUnordGreaterThanEqual %bool %float_0 %clamp\n" +
2390 "OpReturn\n" +
2391 "OpFunctionEnd",
2392 2, false),
2393 // Test case 11: fold 0.0 >= clamp(n, -1.0, 0.0)
2394 InstructionFoldingCase<bool>(
2395 Header() + "%main = OpFunction %void None %void_func\n" +
2396 "%main_lab = OpLabel\n" +
2397 "%n = OpVariable %_ptr_float Function\n" +
2398 "%ld = OpLoad %float %n\n" +
2399 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
2400 "%2 = OpFUnordGreaterThanEqual %bool %float_0 %clamp\n" +
2401 "OpReturn\n" +
2402 "OpFunctionEnd",
2403 2, true),
2404 // Test case 12: fold 0.0 <= clamp(n, 0.0, 1.0)
2405 InstructionFoldingCase<bool>(
2406 Header() + "%main = OpFunction %void None %void_func\n" +
2407 "%main_lab = OpLabel\n" +
2408 "%n = OpVariable %_ptr_float Function\n" +
2409 "%ld = OpLoad %float %n\n" +
2410 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
2411 "%2 = OpFUnordLessThanEqual %bool %float_0 %clamp\n" +
2412 "OpReturn\n" +
2413 "OpFunctionEnd",
2414 2, true),
2415 // Test case 13: fold 0.0 <= clamp(n, -1.0, -1.0)
2416 InstructionFoldingCase<bool>(
2417 Header() + "%main = OpFunction %void None %void_func\n" +
2418 "%main_lab = OpLabel\n" +
2419 "%n = OpVariable %_ptr_float Function\n" +
2420 "%ld = OpLoad %float %n\n" +
2421 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_n1\n" +
2422 "%2 = OpFUnordLessThanEqual %bool %float_0 %clamp\n" +
2423 "OpReturn\n" +
2424 "OpFunctionEnd",
2425 2, false),
2426 // Test case 14: fold 0.0 < clamp(n, 1, 2)
2427 InstructionFoldingCase<bool>(
2428 Header() + "%main = OpFunction %void None %void_func\n" +
2429 "%main_lab = OpLabel\n" +
2430 "%n = OpVariable %_ptr_float Function\n" +
2431 "%ld = OpLoad %float %n\n" +
2432 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2433 "%2 = OpFUnordLessThan %bool %float_0 %clamp\n" +
2434 "OpReturn\n" +
2435 "OpFunctionEnd",
2436 2, true),
2437 // Test case 15: fold 0.0 < clamp(n, -1.0, 0.0)
2438 InstructionFoldingCase<bool>(
2439 Header() + "%main = OpFunction %void None %void_func\n" +
2440 "%main_lab = OpLabel\n" +
2441 "%n = OpVariable %_ptr_float Function\n" +
2442 "%ld = OpLoad %float %n\n" +
2443 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
2444 "%2 = OpFUnordLessThan %bool %float_0 %clamp\n" +
2445 "OpReturn\n" +
2446 "OpFunctionEnd",
2447 2, false)
2448 ));
2449
2450 INSTANTIATE_TEST_SUITE_P(FClampAndCmpRHS, BooleanInstructionFoldingTest,
2451 ::testing::Values(
2452 // Test case 0: fold clamp(n, 0.0, 1.0) > 1.0
2453 InstructionFoldingCase<bool>(
2454 Header() + "%main = OpFunction %void None %void_func\n" +
2455 "%main_lab = OpLabel\n" +
2456 "%n = OpVariable %_ptr_float Function\n" +
2457 "%ld = OpLoad %float %n\n" +
2458 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
2459 "%2 = OpFOrdGreaterThan %bool %clamp %float_1\n" +
2460 "OpReturn\n" +
2461 "OpFunctionEnd",
2462 2, false),
2463 // Test case 1: fold clamp(n, 1.0, 1.0) > 0.0
2464 InstructionFoldingCase<bool>(
2465 Header() + "%main = OpFunction %void None %void_func\n" +
2466 "%main_lab = OpLabel\n" +
2467 "%n = OpVariable %_ptr_float Function\n" +
2468 "%ld = OpLoad %float %n\n" +
2469 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_1\n" +
2470 "%2 = OpFOrdGreaterThan %bool %clamp %float_0\n" +
2471 "OpReturn\n" +
2472 "OpFunctionEnd",
2473 2, true),
2474 // Test case 2: fold clamp(n, 1, 2) >= 0.0
2475 InstructionFoldingCase<bool>(
2476 Header() + "%main = OpFunction %void None %void_func\n" +
2477 "%main_lab = OpLabel\n" +
2478 "%n = OpVariable %_ptr_float Function\n" +
2479 "%ld = OpLoad %float %n\n" +
2480 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2481 "%2 = OpFOrdGreaterThanEqual %bool %clamp %float_0\n" +
2482 "OpReturn\n" +
2483 "OpFunctionEnd",
2484 2, true),
2485 // Test case 3: fold clamp(n, 1.0, 2.0) >= 3.0
2486 InstructionFoldingCase<bool>(
2487 Header() + "%main = OpFunction %void None %void_func\n" +
2488 "%main_lab = OpLabel\n" +
2489 "%n = OpVariable %_ptr_float Function\n" +
2490 "%ld = OpLoad %float %n\n" +
2491 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2492 "%2 = OpFOrdGreaterThanEqual %bool %clamp %float_3\n" +
2493 "OpReturn\n" +
2494 "OpFunctionEnd",
2495 2, false),
2496 // Test case 4: fold clamp(n, 0.0, 1.0) <= 1.0
2497 InstructionFoldingCase<bool>(
2498 Header() + "%main = OpFunction %void None %void_func\n" +
2499 "%main_lab = OpLabel\n" +
2500 "%n = OpVariable %_ptr_float Function\n" +
2501 "%ld = OpLoad %float %n\n" +
2502 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
2503 "%2 = OpFOrdLessThanEqual %bool %clamp %float_1\n" +
2504 "OpReturn\n" +
2505 "OpFunctionEnd",
2506 2, true),
2507 // Test case 5: fold clamp(n, 1.0, 2.0) <= 0.0
2508 InstructionFoldingCase<bool>(
2509 Header() + "%main = OpFunction %void None %void_func\n" +
2510 "%main_lab = OpLabel\n" +
2511 "%n = OpVariable %_ptr_float Function\n" +
2512 "%ld = OpLoad %float %n\n" +
2513 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2514 "%2 = OpFOrdLessThanEqual %bool %clamp %float_0\n" +
2515 "OpReturn\n" +
2516 "OpFunctionEnd",
2517 2, false),
2518 // Test case 6: fold clamp(n, 1, 2) < 3
2519 InstructionFoldingCase<bool>(
2520 Header() + "%main = OpFunction %void None %void_func\n" +
2521 "%main_lab = OpLabel\n" +
2522 "%n = OpVariable %_ptr_float Function\n" +
2523 "%ld = OpLoad %float %n\n" +
2524 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2525 "%2 = OpFOrdLessThan %bool %clamp %float_3\n" +
2526 "OpReturn\n" +
2527 "OpFunctionEnd",
2528 2, true),
2529 // Test case 7: fold clamp(n, -1.0, 0.0) < -1.0
2530 InstructionFoldingCase<bool>(
2531 Header() + "%main = OpFunction %void None %void_func\n" +
2532 "%main_lab = OpLabel\n" +
2533 "%n = OpVariable %_ptr_float Function\n" +
2534 "%ld = OpLoad %float %n\n" +
2535 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
2536 "%2 = OpFOrdLessThan %bool %clamp %float_n1\n" +
2537 "OpReturn\n" +
2538 "OpFunctionEnd",
2539 2, false),
2540 // Test case 8: fold clamp(n, 0.0, 1.0) > 1.0
2541 InstructionFoldingCase<bool>(
2542 Header() + "%main = OpFunction %void None %void_func\n" +
2543 "%main_lab = OpLabel\n" +
2544 "%n = OpVariable %_ptr_float Function\n" +
2545 "%ld = OpLoad %float %n\n" +
2546 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
2547 "%2 = OpFUnordGreaterThan %bool %clamp %float_1\n" +
2548 "OpReturn\n" +
2549 "OpFunctionEnd",
2550 2, false),
2551 // Test case 9: fold clamp(n, 1.0, 2.0) > 0.0
2552 InstructionFoldingCase<bool>(
2553 Header() + "%main = OpFunction %void None %void_func\n" +
2554 "%main_lab = OpLabel\n" +
2555 "%n = OpVariable %_ptr_float Function\n" +
2556 "%ld = OpLoad %float %n\n" +
2557 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2558 "%2 = OpFUnordGreaterThan %bool %clamp %float_0\n" +
2559 "OpReturn\n" +
2560 "OpFunctionEnd",
2561 2, true),
2562 // Test case 10: fold clamp(n, 1, 2) >= 3.0
2563 InstructionFoldingCase<bool>(
2564 Header() + "%main = OpFunction %void None %void_func\n" +
2565 "%main_lab = OpLabel\n" +
2566 "%n = OpVariable %_ptr_float Function\n" +
2567 "%ld = OpLoad %float %n\n" +
2568 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2569 "%2 = OpFUnordGreaterThanEqual %bool %clamp %float_3\n" +
2570 "OpReturn\n" +
2571 "OpFunctionEnd",
2572 2, false),
2573 // Test case 11: fold clamp(n, -1.0, 0.0) >= -1.0
2574 InstructionFoldingCase<bool>(
2575 Header() + "%main = OpFunction %void None %void_func\n" +
2576 "%main_lab = OpLabel\n" +
2577 "%n = OpVariable %_ptr_float Function\n" +
2578 "%ld = OpLoad %float %n\n" +
2579 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
2580 "%2 = OpFUnordGreaterThanEqual %bool %clamp %float_n1\n" +
2581 "OpReturn\n" +
2582 "OpFunctionEnd",
2583 2, true),
2584 // Test case 12: fold clamp(n, 0.0, 1.0) <= 1.0
2585 InstructionFoldingCase<bool>(
2586 Header() + "%main = OpFunction %void None %void_func\n" +
2587 "%main_lab = OpLabel\n" +
2588 "%n = OpVariable %_ptr_float Function\n" +
2589 "%ld = OpLoad %float %n\n" +
2590 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
2591 "%2 = OpFUnordLessThanEqual %bool %clamp %float_1\n" +
2592 "OpReturn\n" +
2593 "OpFunctionEnd",
2594 2, true),
2595 // Test case 13: fold clamp(n, 1.0, 1.0) <= 0.0
2596 InstructionFoldingCase<bool>(
2597 Header() + "%main = OpFunction %void None %void_func\n" +
2598 "%main_lab = OpLabel\n" +
2599 "%n = OpVariable %_ptr_float Function\n" +
2600 "%ld = OpLoad %float %n\n" +
2601 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_1\n" +
2602 "%2 = OpFUnordLessThanEqual %bool %clamp %float_0\n" +
2603 "OpReturn\n" +
2604 "OpFunctionEnd",
2605 2, false),
2606 // Test case 14: fold clamp(n, 1, 2) < 3
2607 InstructionFoldingCase<bool>(
2608 Header() + "%main = OpFunction %void None %void_func\n" +
2609 "%main_lab = OpLabel\n" +
2610 "%n = OpVariable %_ptr_float Function\n" +
2611 "%ld = OpLoad %float %n\n" +
2612 "%clamp = OpExtInst %float %1 FClamp %ld %float_1 %float_2\n" +
2613 "%2 = OpFUnordLessThan %bool %clamp %float_3\n" +
2614 "OpReturn\n" +
2615 "OpFunctionEnd",
2616 2, true),
2617 // Test case 15: fold clamp(n, -1.0, 0.0) < -1.0
2618 InstructionFoldingCase<bool>(
2619 Header() + "%main = OpFunction %void None %void_func\n" +
2620 "%main_lab = OpLabel\n" +
2621 "%n = OpVariable %_ptr_float Function\n" +
2622 "%ld = OpLoad %float %n\n" +
2623 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
2624 "%2 = OpFUnordLessThan %bool %clamp %float_n1\n" +
2625 "OpReturn\n" +
2626 "OpFunctionEnd",
2627 2, false),
2628 // Test case 16: fold clamp(n, -1.0, 0.0) < -1.0 (one test for double)
2629 InstructionFoldingCase<bool>(
2630 Header() + "%main = OpFunction %void None %void_func\n" +
2631 "%main_lab = OpLabel\n" +
2632 "%n = OpVariable %_ptr_double Function\n" +
2633 "%ld = OpLoad %double %n\n" +
2634 "%clamp = OpExtInst %double %1 FClamp %ld %double_n1 %double_0\n" +
2635 "%2 = OpFUnordLessThan %bool %clamp %double_n1\n" +
2636 "OpReturn\n" +
2637 "OpFunctionEnd",
2638 2, false)
2639 ));
2640 // clang-format on
2641
2642 using FloatInstructionFoldingTest =
2643 ::testing::TestWithParam<InstructionFoldingCase<float>>;
2644
2645 TEST_P(FloatInstructionFoldingTest, Case) {
2646 const auto& tc = GetParam();
2647
2648 std::unique_ptr<IRContext> context;
2649 Instruction* inst;
2650 std::tie(context, inst) =
2651 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
2652
2653 CheckForExpectedScalarConstant(inst, tc.expected_result,
2654 [](const analysis::Constant* c) {
2655 return c->AsFloatConstant()->GetFloatValue();
2656 });
2657 }
2658
2659 using FloatBitsInstructionFoldingTest =
2660 ::testing::TestWithParam<InstructionFoldingCase<uint32_t>>;
2661
2662 TEST_P(FloatBitsInstructionFoldingTest, Case) {
2663 const auto& tc = GetParam();
2664
2665 std::unique_ptr<IRContext> context;
2666 Instruction* inst;
2667 std::tie(context, inst) =
2668 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
2669
2670 CheckForExpectedScalarConstant(
2671 inst, tc.expected_result, [](const analysis::Constant* c) {
2672 float f = c->AsFloatConstant()->GetFloatValue();
2673 uint32_t fbits{};
2674 static_assert(sizeof(float) == sizeof(uint32_t));
2675 std::memcpy(&fbits, &f, sizeof(uint32_t));
2676 return fbits;
2677 });
2678 }
2679
2680 // Not testing NaNs because there are no expectations concerning NaNs according
2681 // to the "Precision and Operation of SPIR-V Instructions" section of the Vulkan
2682 // specification.
2683
2684 // clang-format off
2685 INSTANTIATE_TEST_SUITE_P(FloatConstantFoldingTest, FloatInstructionFoldingTest,
2686 ::testing::Values(
2687 // Test case 0: Fold 2.0 - 1.0
2688 InstructionFoldingCase<float>(
2689 Header() + "%main = OpFunction %void None %void_func\n" +
2690 "%main_lab = OpLabel\n" +
2691 "%2 = OpFSub %float %float_2 %float_1\n" +
2692 "OpReturn\n" +
2693 "OpFunctionEnd",
2694 2, 1.0),
2695 // Test case 1: Fold 2.0 + 1.0
2696 InstructionFoldingCase<float>(
2697 Header() + "%main = OpFunction %void None %void_func\n" +
2698 "%main_lab = OpLabel\n" +
2699 "%2 = OpFAdd %float %float_2 %float_1\n" +
2700 "OpReturn\n" +
2701 "OpFunctionEnd",
2702 2, 3.0),
2703 // Test case 2: Fold 3.0 * 2.0
2704 InstructionFoldingCase<float>(
2705 Header() + "%main = OpFunction %void None %void_func\n" +
2706 "%main_lab = OpLabel\n" +
2707 "%2 = OpFMul %float %float_3 %float_2\n" +
2708 "OpReturn\n" +
2709 "OpFunctionEnd",
2710 2, 6.0),
2711 // Test case 3: Fold 1.0 / 2.0
2712 InstructionFoldingCase<float>(
2713 Header() + "%main = OpFunction %void None %void_func\n" +
2714 "%main_lab = OpLabel\n" +
2715 "%2 = OpFDiv %float %float_1 %float_2\n" +
2716 "OpReturn\n" +
2717 "OpFunctionEnd",
2718 2, 0.5),
2719 // Test case 4: Fold 1.0 / 0.0
2720 InstructionFoldingCase<float>(
2721 Header() + "%main = OpFunction %void None %void_func\n" +
2722 "%main_lab = OpLabel\n" +
2723 "%2 = OpFDiv %float %float_1 %float_0\n" +
2724 "OpReturn\n" +
2725 "OpFunctionEnd",
2726 2, std::numeric_limits<float>::infinity()),
2727 // Test case 5: Fold -1.0 / 0.0
2728 InstructionFoldingCase<float>(
2729 Header() + "%main = OpFunction %void None %void_func\n" +
2730 "%main_lab = OpLabel\n" +
2731 "%2 = OpFDiv %float %float_n1 %float_0\n" +
2732 "OpReturn\n" +
2733 "OpFunctionEnd",
2734 2, -std::numeric_limits<float>::infinity()),
2735 // Test case 6: Fold (2.0, 3.0) dot (2.0, 0.5)
2736 InstructionFoldingCase<float>(
2737 Header() + "%main = OpFunction %void None %void_func\n" +
2738 "%main_lab = OpLabel\n" +
2739 "%2 = OpDot %float %v2float_2_3 %v2float_2_0p5\n" +
2740 "OpReturn\n" +
2741 "OpFunctionEnd",
2742 2, 5.5f),
2743 // Test case 7: Fold (0.0, 0.0) dot v
2744 InstructionFoldingCase<float>(
2745 Header() + "%main = OpFunction %void None %void_func\n" +
2746 "%main_lab = OpLabel\n" +
2747 "%v = OpVariable %_ptr_v2float Function\n" +
2748 "%2 = OpLoad %v2float %v\n" +
2749 "%3 = OpDot %float %v2float_0_0 %2\n" +
2750 "OpReturn\n" +
2751 "OpFunctionEnd",
2752 3, 0.0f),
2753 // Test case 8: Fold v dot (0.0, 0.0)
2754 InstructionFoldingCase<float>(
2755 Header() + "%main = OpFunction %void None %void_func\n" +
2756 "%main_lab = OpLabel\n" +
2757 "%v = OpVariable %_ptr_v2float Function\n" +
2758 "%2 = OpLoad %v2float %v\n" +
2759 "%3 = OpDot %float %2 %v2float_0_0\n" +
2760 "OpReturn\n" +
2761 "OpFunctionEnd",
2762 3, 0.0f),
2763 // Test case 9: Fold Null dot v
2764 InstructionFoldingCase<float>(
2765 Header() + "%main = OpFunction %void None %void_func\n" +
2766 "%main_lab = OpLabel\n" +
2767 "%v = OpVariable %_ptr_v2float Function\n" +
2768 "%2 = OpLoad %v2float %v\n" +
2769 "%3 = OpDot %float %v2float_null %2\n" +
2770 "OpReturn\n" +
2771 "OpFunctionEnd",
2772 3, 0.0f),
2773 // Test case 10: Fold v dot Null
2774 InstructionFoldingCase<float>(
2775 Header() + "%main = OpFunction %void None %void_func\n" +
2776 "%main_lab = OpLabel\n" +
2777 "%v = OpVariable %_ptr_v2float Function\n" +
2778 "%2 = OpLoad %v2float %v\n" +
2779 "%3 = OpDot %float %2 %v2float_null\n" +
2780 "OpReturn\n" +
2781 "OpFunctionEnd",
2782 3, 0.0f),
2783 // Test case 11: Fold -2.0
2784 InstructionFoldingCase<float>(
2785 Header() + "%main = OpFunction %void None %void_func\n" +
2786 "%main_lab = OpLabel\n" +
2787 "%2 = OpFNegate %float %float_2\n" +
2788 "OpReturn\n" +
2789 "OpFunctionEnd",
2790 2, -2),
2791 // Test case 12: QuantizeToF16 1.0
2792 InstructionFoldingCase<float>(
2793 Header() + "%main = OpFunction %void None %void_func\n" +
2794 "%main_lab = OpLabel\n" +
2795 "%2 = OpQuantizeToF16 %float %float_1\n" +
2796 "OpReturn\n" +
2797 "OpFunctionEnd",
2798 2, 1.0),
2799 // Test case 13: QuantizeToF16 positive non exact
2800 InstructionFoldingCase<float>(
2801 Header() + "%main = OpFunction %void None %void_func\n" +
2802 "%main_lab = OpLabel\n" +
2803 "%2 = OpQuantizeToF16 %float %float_2049\n" +
2804 "OpReturn\n" +
2805 "OpFunctionEnd",
2806 2, 2048),
2807 // Test case 14: QuantizeToF16 negative non exact
2808 InstructionFoldingCase<float>(
2809 Header() + "%main = OpFunction %void None %void_func\n" +
2810 "%main_lab = OpLabel\n" +
2811 "%2 = OpQuantizeToF16 %float %float_n2049\n" +
2812 "OpReturn\n" +
2813 "OpFunctionEnd",
2814 2, -2048),
2815 // Test case 15: QuantizeToF16 large positive
2816 InstructionFoldingCase<float>(
2817 Header() + "%main = OpFunction %void None %void_func\n" +
2818 "%main_lab = OpLabel\n" +
2819 "%2 = OpQuantizeToF16 %float %float_1e16\n" +
2820 "OpReturn\n" +
2821 "OpFunctionEnd",
2822 2, std::numeric_limits<float>::infinity()),
2823 // Test case 16: QuantizeToF16 large negative
2824 InstructionFoldingCase<float>(
2825 Header() + "%main = OpFunction %void None %void_func\n" +
2826 "%main_lab = OpLabel\n" +
2827 "%2 = OpQuantizeToF16 %float %float_n1e16\n" +
2828 "OpReturn\n" +
2829 "OpFunctionEnd",
2830 2, -std::numeric_limits<float>::infinity()),
2831 // Test case 17: QuantizeToF16 small positive
2832 InstructionFoldingCase<float>(
2833 Header() + "%main = OpFunction %void None %void_func\n" +
2834 "%main_lab = OpLabel\n" +
2835 "%2 = OpQuantizeToF16 %float %float_1en16\n" +
2836 "OpReturn\n" +
2837 "OpFunctionEnd",
2838 2, 0.0),
2839 // Test case 18: QuantizeToF16 small negative
2840 InstructionFoldingCase<float>(
2841 Header() + "%main = OpFunction %void None %void_func\n" +
2842 "%main_lab = OpLabel\n" +
2843 "%2 = OpQuantizeToF16 %float %float_n1en16\n" +
2844 "OpReturn\n" +
2845 "OpFunctionEnd",
2846 2, 0.0),
2847 // Test case 19: QuantizeToF16 nan
2848 InstructionFoldingCase<float>(
2849 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
2850 "%main_lab = OpLabel\n" +
2851 "%2 = OpQuantizeToF16 %float %float_nan\n" +
2852 "OpReturn\n" +
2853 "OpFunctionEnd",
2854 2, std::numeric_limits<float>::quiet_NaN()),
2855 // Test case 20: FMix 1.0 4.0 0.2
2856 InstructionFoldingCase<float>(
2857 Header() + "%main = OpFunction %void None %void_func\n" +
2858 "%main_lab = OpLabel\n" +
2859 "%2 = OpExtInst %float %1 FMix %float_1 %float_4 %float_0p2\n" +
2860 "OpReturn\n" +
2861 "OpFunctionEnd",
2862 2, 1.6f),
2863 // Test case 21: FMin 1.0 4.0
2864 InstructionFoldingCase<float>(
2865 Header() + "%main = OpFunction %void None %void_func\n" +
2866 "%main_lab = OpLabel\n" +
2867 "%2 = OpExtInst %float %1 FMin %float_1 %float_4\n" +
2868 "OpReturn\n" +
2869 "OpFunctionEnd",
2870 2, 1.0f),
2871 // Test case 22: FMin 4.0 0.2
2872 InstructionFoldingCase<float>(
2873 Header() + "%main = OpFunction %void None %void_func\n" +
2874 "%main_lab = OpLabel\n" +
2875 "%2 = OpExtInst %float %1 FMin %float_4 %float_0p2\n" +
2876 "OpReturn\n" +
2877 "OpFunctionEnd",
2878 2, 0.2f),
2879 // Test case 23: FMax 1.0 4.0
2880 InstructionFoldingCase<float>(
2881 Header() + "%main = OpFunction %void None %void_func\n" +
2882 "%main_lab = OpLabel\n" +
2883 "%2 = OpExtInst %float %1 FMax %float_1 %float_4\n" +
2884 "OpReturn\n" +
2885 "OpFunctionEnd",
2886 2, 4.0f),
2887 // Test case 24: FMax 1.0 0.2
2888 InstructionFoldingCase<float>(
2889 Header() + "%main = OpFunction %void None %void_func\n" +
2890 "%main_lab = OpLabel\n" +
2891 "%2 = OpExtInst %float %1 FMax %float_1 %float_0p2\n" +
2892 "OpReturn\n" +
2893 "OpFunctionEnd",
2894 2, 1.0f),
2895 // Test case 25: FClamp 1.0 0.2 4.0
2896 InstructionFoldingCase<float>(
2897 Header() + "%main = OpFunction %void None %void_func\n" +
2898 "%main_lab = OpLabel\n" +
2899 "%2 = OpExtInst %float %1 FClamp %float_1 %float_0p2 %float_4\n" +
2900 "OpReturn\n" +
2901 "OpFunctionEnd",
2902 2, 1.0f),
2903 // Test case 26: FClamp 0.2 2.0 4.0
2904 InstructionFoldingCase<float>(
2905 Header() + "%main = OpFunction %void None %void_func\n" +
2906 "%main_lab = OpLabel\n" +
2907 "%2 = OpExtInst %float %1 FClamp %float_0p2 %float_2 %float_4\n" +
2908 "OpReturn\n" +
2909 "OpFunctionEnd",
2910 2, 2.0f),
2911 // Test case 27: FClamp 2049.0 2.0 4.0
2912 InstructionFoldingCase<float>(
2913 Header() + "%main = OpFunction %void None %void_func\n" +
2914 "%main_lab = OpLabel\n" +
2915 "%2 = OpExtInst %float %1 FClamp %float_2049 %float_2 %float_4\n" +
2916 "OpReturn\n" +
2917 "OpFunctionEnd",
2918 2, 4.0f),
2919 // Test case 28: FClamp 1.0 2.0 x
2920 InstructionFoldingCase<float>(
2921 Header() + "%main = OpFunction %void None %void_func\n" +
2922 "%main_lab = OpLabel\n" +
2923 "%undef = OpUndef %float\n" +
2924 "%2 = OpExtInst %float %1 FClamp %float_1 %float_2 %undef\n" +
2925 "OpReturn\n" +
2926 "OpFunctionEnd",
2927 2, 2.0),
2928 // Test case 29: FClamp 1.0 x 0.5
2929 InstructionFoldingCase<float>(
2930 Header() + "%main = OpFunction %void None %void_func\n" +
2931 "%main_lab = OpLabel\n" +
2932 "%undef = OpUndef %float\n" +
2933 "%2 = OpExtInst %float %1 FClamp %float_1 %undef %float_0p5\n" +
2934 "OpReturn\n" +
2935 "OpFunctionEnd",
2936 2, 0.5),
2937 // Test case 30: Sin 0.0
2938 InstructionFoldingCase<float>(
2939 Header() + "%main = OpFunction %void None %void_func\n" +
2940 "%main_lab = OpLabel\n" +
2941 "%2 = OpExtInst %float %1 Sin %float_0\n" +
2942 "OpReturn\n" +
2943 "OpFunctionEnd",
2944 2, 0.0),
2945 // Test case 31: Cos 0.0
2946 InstructionFoldingCase<float>(
2947 Header() + "%main = OpFunction %void None %void_func\n" +
2948 "%main_lab = OpLabel\n" +
2949 "%2 = OpExtInst %float %1 Cos %float_0\n" +
2950 "OpReturn\n" +
2951 "OpFunctionEnd",
2952 2, 1.0),
2953 // Test case 32: Tan 0.0
2954 InstructionFoldingCase<float>(
2955 Header() + "%main = OpFunction %void None %void_func\n" +
2956 "%main_lab = OpLabel\n" +
2957 "%2 = OpExtInst %float %1 Tan %float_0\n" +
2958 "OpReturn\n" +
2959 "OpFunctionEnd",
2960 2, 0.0),
2961 // Test case 33: Asin 0.0
2962 InstructionFoldingCase<float>(
2963 Header() + "%main = OpFunction %void None %void_func\n" +
2964 "%main_lab = OpLabel\n" +
2965 "%2 = OpExtInst %float %1 Asin %float_0\n" +
2966 "OpReturn\n" +
2967 "OpFunctionEnd",
2968 2, 0.0),
2969 // Test case 34: Acos 1.0
2970 InstructionFoldingCase<float>(
2971 Header() + "%main = OpFunction %void None %void_func\n" +
2972 "%main_lab = OpLabel\n" +
2973 "%2 = OpExtInst %float %1 Acos %float_1\n" +
2974 "OpReturn\n" +
2975 "OpFunctionEnd",
2976 2, 0.0),
2977 // Test case 35: Atan 0.0
2978 InstructionFoldingCase<float>(
2979 Header() + "%main = OpFunction %void None %void_func\n" +
2980 "%main_lab = OpLabel\n" +
2981 "%2 = OpExtInst %float %1 Atan %float_0\n" +
2982 "OpReturn\n" +
2983 "OpFunctionEnd",
2984 2, 0.0),
2985 // Test case 36: Exp 0.0
2986 InstructionFoldingCase<float>(
2987 Header() + "%main = OpFunction %void None %void_func\n" +
2988 "%main_lab = OpLabel\n" +
2989 "%2 = OpExtInst %float %1 Exp %float_0\n" +
2990 "OpReturn\n" +
2991 "OpFunctionEnd",
2992 2, 1.0),
2993 // Test case 37: Log 1.0
2994 InstructionFoldingCase<float>(
2995 Header() + "%main = OpFunction %void None %void_func\n" +
2996 "%main_lab = OpLabel\n" +
2997 "%2 = OpExtInst %float %1 Log %float_1\n" +
2998 "OpReturn\n" +
2999 "OpFunctionEnd",
3000 2, 0.0),
3001 // Test case 38: Exp2 2.0
3002 InstructionFoldingCase<float>(
3003 Header() + "%main = OpFunction %void None %void_func\n" +
3004 "%main_lab = OpLabel\n" +
3005 "%2 = OpExtInst %float %1 Exp2 %float_2\n" +
3006 "OpReturn\n" +
3007 "OpFunctionEnd",
3008 2, 4.0),
3009 // Test case 39: Log2 4.0
3010 InstructionFoldingCase<float>(
3011 Header() + "%main = OpFunction %void None %void_func\n" +
3012 "%main_lab = OpLabel\n" +
3013 "%2 = OpExtInst %float %1 Log2 %float_4\n" +
3014 "OpReturn\n" +
3015 "OpFunctionEnd",
3016 2, 2.0),
3017 // Test case 40: Sqrt 4.0
3018 InstructionFoldingCase<float>(
3019 Header() + "%main = OpFunction %void None %void_func\n" +
3020 "%main_lab = OpLabel\n" +
3021 "%2 = OpExtInst %float %1 Sqrt %float_4\n" +
3022 "OpReturn\n" +
3023 "OpFunctionEnd",
3024 2, 2.0),
3025 // Test case 41: Atan2 0.0 1.0
3026 InstructionFoldingCase<float>(
3027 Header() + "%main = OpFunction %void None %void_func\n" +
3028 "%main_lab = OpLabel\n" +
3029 "%2 = OpExtInst %float %1 Atan2 %float_0 %float_1\n" +
3030 "OpReturn\n" +
3031 "OpFunctionEnd",
3032 2, 0.0),
3033 // Test case 42: Pow 2.0 3.0
3034 InstructionFoldingCase<float>(
3035 Header() + "%main = OpFunction %void None %void_func\n" +
3036 "%main_lab = OpLabel\n" +
3037 "%2 = OpExtInst %float %1 Pow %float_2 %float_3\n" +
3038 "OpReturn\n" +
3039 "OpFunctionEnd",
3040 2, 8.0),
3041 // Test case 43: Fold 1.0 / -0.0.
3042 InstructionFoldingCase<float>(
3043 Header() + "%main = OpFunction %void None %void_func\n" +
3044 "%main_lab = OpLabel\n" +
3045 "%2 = OpFDiv %float %float_1 %float_n0\n" +
3046 "OpReturn\n" +
3047 "OpFunctionEnd",
3048 2, -std::numeric_limits<float>::infinity()),
3049 // Test case 44: Fold -1.0 / -0.0
3050 InstructionFoldingCase<float>(
3051 Header() + "%main = OpFunction %void None %void_func\n" +
3052 "%main_lab = OpLabel\n" +
3053 "%2 = OpFDiv %float %float_n1 %float_n0\n" +
3054 "OpReturn\n" +
3055 "OpFunctionEnd",
3056 2, std::numeric_limits<float>::infinity()),
3057 // Test case 45: Fold 0.0 / 0.0
3058 InstructionFoldingCase<float>(
3059 Header() + "%main = OpFunction %void None %void_func\n" +
3060 "%main_lab = OpLabel\n" +
3061 "%2 = OpFDiv %float %float_0 %float_0\n" +
3062 "OpReturn\n" +
3063 "OpFunctionEnd",
3064 2, std::numeric_limits<float>::quiet_NaN()),
3065 // Test case 46: Fold 0.0 / -0.0
3066 InstructionFoldingCase<float>(
3067 Header() + "%main = OpFunction %void None %void_func\n" +
3068 "%main_lab = OpLabel\n" +
3069 "%2 = OpFDiv %float %float_0 %float_n0\n" +
3070 "OpReturn\n" +
3071 "OpFunctionEnd",
3072 2, std::numeric_limits<float>::quiet_NaN()),
3073 // Test case 47: NMin 1.0 4.0
3074 InstructionFoldingCase<float>(
3075 Header() + "%main = OpFunction %void None %void_func\n" +
3076 "%main_lab = OpLabel\n" +
3077 "%2 = OpExtInst %float %1 NMin %float_1 %float_4\n" +
3078 "OpReturn\n" +
3079 "OpFunctionEnd",
3080 2, 1.0f),
3081 // Test case 48: NMin 4.0 0.2
3082 InstructionFoldingCase<float>(
3083 Header() + "%main = OpFunction %void None %void_func\n" +
3084 "%main_lab = OpLabel\n" +
3085 "%2 = OpExtInst %float %1 NMin %float_4 %float_0p2\n" +
3086 "OpReturn\n" +
3087 "OpFunctionEnd",
3088 2, 0.2f),
3089 // Test case 49: NMin NaN 4.0
3090 InstructionFoldingCase<float>(
3091 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3092 "%main_lab = OpLabel\n" +
3093 "%2 = OpExtInst %float %1 NMin %float_nan %float_4\n" +
3094 "OpReturn\n" +
3095 "OpFunctionEnd",
3096 2, 4.0f),
3097 // Test case 50: NMin 2.0 NaN
3098 InstructionFoldingCase<float>(
3099 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3100 "%main_lab = OpLabel\n" +
3101 "%2 = OpExtInst %float %1 NMin %float_2 %float_nan\n" +
3102 "OpReturn\n" +
3103 "OpFunctionEnd",
3104 2, 2.0f),
3105 // Test case 51: NMin NaN NaN
3106 InstructionFoldingCase<float>(
3107 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3108 "%main_lab = OpLabel\n" +
3109 "%2 = OpExtInst %float %1 NMin %float_nan %float_nan\n" +
3110 "OpReturn\n" +
3111 "OpFunctionEnd",
3112 2, std::numeric_limits<float>::quiet_NaN()),
3113 // Test case 52: NMax 1.0 4.0
3114 InstructionFoldingCase<float>(
3115 Header() + "%main = OpFunction %void None %void_func\n" +
3116 "%main_lab = OpLabel\n" +
3117 "%2 = OpExtInst %float %1 NMax %float_1 %float_4\n" +
3118 "OpReturn\n" +
3119 "OpFunctionEnd",
3120 2, 4.0f),
3121 // Test case 53: NMax 1.0 0.2
3122 InstructionFoldingCase<float>(
3123 Header() + "%main = OpFunction %void None %void_func\n" +
3124 "%main_lab = OpLabel\n" +
3125 "%2 = OpExtInst %float %1 NMax %float_1 %float_0p2\n" +
3126 "OpReturn\n" +
3127 "OpFunctionEnd",
3128 2, 1.0f),
3129 // Test case 54: NMax 1.0 NaN
3130 InstructionFoldingCase<float>(
3131 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3132 "%main_lab = OpLabel\n" +
3133 "%2 = OpExtInst %float %1 NMax %float_1 %float_nan\n" +
3134 "OpReturn\n" +
3135 "OpFunctionEnd",
3136 2, 1.0f),
3137 // Test case 55: NMax NaN 0.5
3138 InstructionFoldingCase<float>(
3139 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3140 "%main_lab = OpLabel\n" +
3141 "%2 = OpExtInst %float %1 NMax %float_nan %float_0p5\n" +
3142 "OpReturn\n" +
3143 "OpFunctionEnd",
3144 2, 0.5f),
3145 // Test case 56: NMax NaN NaN
3146 InstructionFoldingCase<float>(
3147 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3148 "%main_lab = OpLabel\n" +
3149 "%2 = OpExtInst %float %1 NMax %float_nan %float_nan\n" +
3150 "OpReturn\n" +
3151 "OpFunctionEnd",
3152 2, std::numeric_limits<float>::quiet_NaN()),
3153 // Test case 57: NClamp 1.0 0.2 4.0
3154 InstructionFoldingCase<float>(
3155 Header() + "%main = OpFunction %void None %void_func\n" +
3156 "%main_lab = OpLabel\n" +
3157 "%2 = OpExtInst %float %1 NClamp %float_1 %float_0p2 %float_4\n" +
3158 "OpReturn\n" +
3159 "OpFunctionEnd",
3160 2, 1.0f),
3161 // Test case 58: NClamp 0.2 2.0 4.0
3162 InstructionFoldingCase<float>(
3163 Header() + "%main = OpFunction %void None %void_func\n" +
3164 "%main_lab = OpLabel\n" +
3165 "%2 = OpExtInst %float %1 NClamp %float_0p2 %float_2 %float_4\n" +
3166 "OpReturn\n" +
3167 "OpFunctionEnd",
3168 2, 2.0f),
3169 // Test case 59: NClamp 2049.0 2.0 4.0
3170 InstructionFoldingCase<float>(
3171 Header() + "%main = OpFunction %void None %void_func\n" +
3172 "%main_lab = OpLabel\n" +
3173 "%2 = OpExtInst %float %1 NClamp %float_2049 %float_2 %float_4\n" +
3174 "OpReturn\n" +
3175 "OpFunctionEnd",
3176 2, 4.0f),
3177 // Test case 60: NClamp 1.0 2.0 x
3178 InstructionFoldingCase<float>(
3179 Header() + "%main = OpFunction %void None %void_func\n" +
3180 "%main_lab = OpLabel\n" +
3181 "%undef = OpUndef %float\n" +
3182 "%2 = OpExtInst %float %1 NClamp %float_1 %float_2 %undef\n" +
3183 "OpReturn\n" +
3184 "OpFunctionEnd",
3185 2, 2.0),
3186 // Test case 61: NClamp 1.0 x 0.5
3187 InstructionFoldingCase<float>(
3188 Header() + "%main = OpFunction %void None %void_func\n" +
3189 "%main_lab = OpLabel\n" +
3190 "%undef = OpUndef %float\n" +
3191 "%2 = OpExtInst %float %1 NClamp %float_1 %undef %float_0p5\n" +
3192 "OpReturn\n" +
3193 "OpFunctionEnd",
3194 2, 0.5),
3195 // Test case 62: NClamp NaN 2.0 4.0
3196 InstructionFoldingCase<float>(
3197 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3198 "%main_lab = OpLabel\n" +
3199 "%2 = OpExtInst %float %1 NClamp %float_nan %float_2 %float_4\n" +
3200 "OpReturn\n" +
3201 "OpFunctionEnd",
3202 2, 2.0f),
3203 // Test case 63: NClamp 2049.0 NaN 4.0
3204 InstructionFoldingCase<float>(
3205 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3206 "%main_lab = OpLabel\n" +
3207 "%2 = OpExtInst %float %1 NClamp %float_2049 %float_nan %float_4\n" +
3208 "OpReturn\n" +
3209 "OpFunctionEnd",
3210 2, 4.0f),
3211 // Test case 64: NClamp 1.0 2.0 NaN
3212 InstructionFoldingCase<float>(
3213 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3214 "%main_lab = OpLabel\n" +
3215 "%2 = OpExtInst %float %1 NClamp %float_1 %float_2 %float_nan\n" +
3216 "OpReturn\n" +
3217 "OpFunctionEnd",
3218 2, 2.0f),
3219 // Test case 65: NClamp 4.0 NaN NaN
3220 InstructionFoldingCase<float>(
3221 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3222 "%main_lab = OpLabel\n" +
3223 "%2 = OpExtInst %float %1 NClamp %float_4 %float_nan %float_nan\n" +
3224 "OpReturn\n" +
3225 "OpFunctionEnd",
3226 2, 4.0f),
3227 // Test case 66: NClamp NaN NaN NaN
3228 InstructionFoldingCase<float>(
3229 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3230 "%main_lab = OpLabel\n" +
3231 "%2 = OpExtInst %float %1 NClamp %float_nan %float_nan %float_nan\n" +
3232 "OpReturn\n" +
3233 "OpFunctionEnd",
3234 2, std::numeric_limits<float>::quiet_NaN())
3235 ));
3236
3237 INSTANTIATE_TEST_SUITE_P(MinMaxZeroFoldingTest, FloatBitsInstructionFoldingTest,
3238 ::testing::Values(
3239 // Test case 0: Fold FMin 0.0 -0.0
3240 InstructionFoldingCase<uint32_t>(
3241 Header() + "%main = OpFunction %void None %void_func\n" +
3242 "%main_lab = OpLabel\n" +
3243 "%2 = OpExtInst %float %1 FMin %float_0 %float_n0\n" +
3244 "OpReturn\n" +
3245 "OpFunctionEnd",
3246 2, 0x80000000u),
3247 // Test case 1: Fold FMin -0.0 0.0
3248 InstructionFoldingCase<uint32_t>(
3249 Header() + "%main = OpFunction %void None %void_func\n" +
3250 "%main_lab = OpLabel\n" +
3251 "%2 = OpExtInst %float %1 FMin %float_n0 %float_0\n" +
3252 "OpReturn\n" +
3253 "OpFunctionEnd",
3254 2, 0x80000000u),
3255 // Test case 2: Fold FMax 0.0 -0.0
3256 InstructionFoldingCase<uint32_t>(
3257 Header() + "%main = OpFunction %void None %void_func\n" +
3258 "%main_lab = OpLabel\n" +
3259 "%2 = OpExtInst %float %1 FMax %float_0 %float_n0\n" +
3260 "OpReturn\n" +
3261 "OpFunctionEnd",
3262 2, 0x00000000u),
3263 // Test case 3: Fold FMax -0.0 0.0
3264 InstructionFoldingCase<uint32_t>(
3265 Header() + "%main = OpFunction %void None %void_func\n" +
3266 "%main_lab = OpLabel\n" +
3267 "%2 = OpExtInst %float %1 FMax %float_n0 %float_0\n" +
3268 "OpReturn\n" +
3269 "OpFunctionEnd",
3270 2, 0x00000000u),
3271 // Test case 4: Fold FClamp -0.0 0.0 1.0
3272 InstructionFoldingCase<uint32_t>(
3273 Header() + "%main = OpFunction %void None %void_func\n" +
3274 "%main_lab = OpLabel\n" +
3275 "%2 = OpExtInst %float %1 FClamp %float_n0 %float_0 %float_1\n" +
3276 "OpReturn\n" +
3277 "OpFunctionEnd",
3278 2, 0x00000000u),
3279 // Test case 5: Fold FClamp 0.0 -0.0 1.0
3280 InstructionFoldingCase<uint32_t>(
3281 Header() + "%main = OpFunction %void None %void_func\n" +
3282 "%main_lab = OpLabel\n" +
3283 "%2 = OpExtInst %float %1 FClamp %float_0 %float_n0 %float_1\n" +
3284 "OpReturn\n" +
3285 "OpFunctionEnd",
3286 2, 0x00000000u),
3287 // Test case 6: Fold FClamp 1.0 -0.0 0.0
3288 InstructionFoldingCase<uint32_t>(
3289 Header() + "%main = OpFunction %void None %void_func\n" +
3290 "%main_lab = OpLabel\n" +
3291 "%2 = OpExtInst %float %1 FClamp %float_1 %float_n0 %float_0\n" +
3292 "OpReturn\n" +
3293 "OpFunctionEnd",
3294 2, 0x00000000u),
3295 // Test case 7: Fold FClamp 0.0 -1.0 -0.0
3296 InstructionFoldingCase<uint32_t>(
3297 Header() + "%main = OpFunction %void None %void_func\n" +
3298 "%main_lab = OpLabel\n" +
3299 "%2 = OpExtInst %float %1 FClamp %float_0 %float_n1 %float_n0\n" +
3300 "OpReturn\n" +
3301 "OpFunctionEnd",
3302 2, 0x80000000u),
3303 // Test case 8: Fold FClamp -0.0 -1.0 0.0
3304 InstructionFoldingCase<uint32_t>(
3305 Header() + "%main = OpFunction %void None %void_func\n" +
3306 "%main_lab = OpLabel\n" +
3307 "%2 = OpExtInst %float %1 FClamp %float_n0 %float_n1 %float_0\n" +
3308 "OpReturn\n" +
3309 "OpFunctionEnd",
3310 2, 0x80000000u),
3311 // Test case 9: Fold FClamp -1.0 -0.0 0.0
3312 InstructionFoldingCase<uint32_t>(
3313 Header() + "%main = OpFunction %void None %void_func\n" +
3314 "%main_lab = OpLabel\n" +
3315 "%2 = OpExtInst %float %1 FClamp %float_n1 %float_n0 %float_0\n" +
3316 "OpReturn\n" +
3317 "OpFunctionEnd",
3318 2, 0x80000000u),
3319 // Test case 10: Fold NMin 0.0 -0.0
3320 InstructionFoldingCase<uint32_t>(
3321 Header() + "%main = OpFunction %void None %void_func\n" +
3322 "%main_lab = OpLabel\n" +
3323 "%2 = OpExtInst %float %1 NMin %float_0 %float_n0\n" +
3324 "OpReturn\n" +
3325 "OpFunctionEnd",
3326 2, 0x80000000u),
3327 // Test case 11: Fold NMin -0.0 0.0
3328 InstructionFoldingCase<uint32_t>(
3329 Header() + "%main = OpFunction %void None %void_func\n" +
3330 "%main_lab = OpLabel\n" +
3331 "%2 = OpExtInst %float %1 NMin %float_n0 %float_0\n" +
3332 "OpReturn\n" +
3333 "OpFunctionEnd",
3334 2, 0x80000000u),
3335 // Test case 12: Fold NMax 0.0 -0.0
3336 InstructionFoldingCase<uint32_t>(
3337 Header() + "%main = OpFunction %void None %void_func\n" +
3338 "%main_lab = OpLabel\n" +
3339 "%2 = OpExtInst %float %1 NMax %float_0 %float_n0\n" +
3340 "OpReturn\n" +
3341 "OpFunctionEnd",
3342 2, 0x00000000u),
3343 // Test case 13: Fold NMax -0.0 0.0
3344 InstructionFoldingCase<uint32_t>(
3345 Header() + "%main = OpFunction %void None %void_func\n" +
3346 "%main_lab = OpLabel\n" +
3347 "%2 = OpExtInst %float %1 NMax %float_n0 %float_0\n" +
3348 "OpReturn\n" +
3349 "OpFunctionEnd",
3350 2, 0x00000000u),
3351 // Test case 14: Fold NClamp -0.0 0.0 1.0
3352 InstructionFoldingCase<uint32_t>(
3353 Header() + "%main = OpFunction %void None %void_func\n" +
3354 "%main_lab = OpLabel\n" +
3355 "%2 = OpExtInst %float %1 NClamp %float_n0 %float_0 %float_1\n" +
3356 "OpReturn\n" +
3357 "OpFunctionEnd",
3358 2, 0x00000000u),
3359 // Test case 15: Fold NClamp 0.0 -0.0 1.0
3360 InstructionFoldingCase<uint32_t>(
3361 Header() + "%main = OpFunction %void None %void_func\n" +
3362 "%main_lab = OpLabel\n" +
3363 "%2 = OpExtInst %float %1 NClamp %float_0 %float_n0 %float_1\n" +
3364 "OpReturn\n" +
3365 "OpFunctionEnd",
3366 2, 0x00000000u),
3367 // Test case 16: Fold NClamp 1.0 -0.0 0.0
3368 InstructionFoldingCase<uint32_t>(
3369 Header() + "%main = OpFunction %void None %void_func\n" +
3370 "%main_lab = OpLabel\n" +
3371 "%2 = OpExtInst %float %1 NClamp %float_1 %float_n0 %float_0\n" +
3372 "OpReturn\n" +
3373 "OpFunctionEnd",
3374 2, 0x00000000u),
3375 // Test case 17: Fold NClamp 0.0 -1.0 -0.0
3376 InstructionFoldingCase<uint32_t>(
3377 Header() + "%main = OpFunction %void None %void_func\n" +
3378 "%main_lab = OpLabel\n" +
3379 "%2 = OpExtInst %float %1 NClamp %float_0 %float_n1 %float_n0\n" +
3380 "OpReturn\n" +
3381 "OpFunctionEnd",
3382 2, 0x80000000u),
3383 // Test case 18: Fold NClamp -0.0 -1.0 0.0
3384 InstructionFoldingCase<uint32_t>(
3385 Header() + "%main = OpFunction %void None %void_func\n" +
3386 "%main_lab = OpLabel\n" +
3387 "%2 = OpExtInst %float %1 NClamp %float_n0 %float_n1 %float_0\n" +
3388 "OpReturn\n" +
3389 "OpFunctionEnd",
3390 2, 0x80000000u),
3391 // Test case 19: Fold NClamp -1.0 -0.0 0.0
3392 InstructionFoldingCase<uint32_t>(
3393 Header() + "%main = OpFunction %void None %void_func\n" +
3394 "%main_lab = OpLabel\n" +
3395 "%2 = OpExtInst %float %1 NClamp %float_n1 %float_n0 %float_0\n" +
3396 "OpReturn\n" +
3397 "OpFunctionEnd",
3398 2, 0x80000000u)
3399 ));
3400
3401 INSTANTIATE_TEST_SUITE_P(RedundantDivFloatTest, FloatInstructionFoldingTest,
3402 ::testing::Values(
3403 // Test case 0: Fold x / x
3404 InstructionFoldingCase<float>(
3405 Header() + "%main = OpFunction %void None %void_func\n" +
3406 "%main_lab = OpLabel\n" +
3407 "%n = OpVariable %_ptr_float Function\n" +
3408 "%3 = OpLoad %float %n\n" +
3409 "%2 = OpFDiv %float %3 %3\n" +
3410 "OpReturn\n" +
3411 "OpFunctionEnd",
3412 2, 1.0),
3413 // Test case 1: Fold -x / x
3414 InstructionFoldingCase<float>(
3415 Header() + "%main = OpFunction %void None %void_func\n" +
3416 "%main_lab = OpLabel\n" +
3417 "%n = OpVariable %_ptr_float Function\n" +
3418 "%3 = OpLoad %float %n\n" +
3419 "%4 = OpFNegate %float %3\n" +
3420 "%2 = OpFDiv %float %4 %3\n" +
3421 "OpReturn\n" +
3422 "OpFunctionEnd",
3423 2, -1.0),
3424 // Test case 2: Fold x / -x
3425 InstructionFoldingCase<float>(
3426 Header() + "%main = OpFunction %void None %void_func\n" +
3427 "%main_lab = OpLabel\n" +
3428 "%n = OpVariable %_ptr_float Function\n" +
3429 "%3 = OpLoad %float %n\n" +
3430 "%4 = OpFNegate %float %3\n" +
3431 "%2 = OpFDiv %float %3 %4\n" +
3432 "OpReturn\n" +
3433 "OpFunctionEnd",
3434 2, -1.0)
3435 ));
3436
3437 INSTANTIATE_TEST_SUITE_P(RedundantDivIntTest, IntegerInstructionFoldingTest,
3438 ::testing::Values(
3439 // Test case 0: Fold x / x
3440 InstructionFoldingCase<uint32_t>(
3441 Header() + "%main = OpFunction %void None %void_func\n" +
3442 "%main_lab = OpLabel\n" +
3443 "%n = OpVariable %_ptr_int Function\n" +
3444 "%3 = OpLoad %int %n\n" +
3445 "%2 = OpSDiv %int %3 %3\n" +
3446 "OpReturn\n" +
3447 "OpFunctionEnd",
3448 2, 1),
3449 // Test case 1: Fold x / x
3450 InstructionFoldingCase<uint32_t>(
3451 Header() + "%main = OpFunction %void None %void_func\n" +
3452 "%main_lab = OpLabel\n" +
3453 "%n = OpVariable %_ptr_uint Function\n" +
3454 "%3 = OpLoad %uint %n\n" +
3455 "%2 = OpUDiv %uint %3 %3\n" +
3456 "OpReturn\n" +
3457 "OpFunctionEnd",
3458 2, 1),
3459 // Test case 2: Fold -x / x
3460 InstructionFoldingCase<uint32_t>(
3461 Header() + "%main = OpFunction %void None %void_func\n" +
3462 "%main_lab = OpLabel\n" +
3463 "%n = OpVariable %_ptr_int Function\n" +
3464 "%3 = OpLoad %int %n\n" +
3465 "%4 = OpSNegate %int %3\n" +
3466 "%2 = OpSDiv %int %4 %3\n" +
3467 "OpReturn\n" +
3468 "OpFunctionEnd",
3469 2, UINT32_MAX),
3470 // Test case 3: Fold x / -x
3471 InstructionFoldingCase<uint32_t>(
3472 Header() + "%main = OpFunction %void None %void_func\n" +
3473 "%main_lab = OpLabel\n" +
3474 "%n = OpVariable %_ptr_int Function\n" +
3475 "%3 = OpLoad %int %n\n" +
3476 "%4 = OpSNegate %int %3\n" +
3477 "%2 = OpSDiv %int %3 %4\n" +
3478 "OpReturn\n" +
3479 "OpFunctionEnd",
3480 2, UINT32_MAX)
3481 ));
3482
3483 // clang-format on
3484
3485 using DoubleInstructionFoldingTest =
3486 ::testing::TestWithParam<InstructionFoldingCase<double>>;
3487
3488 TEST_P(DoubleInstructionFoldingTest, Case) {
3489 const auto& tc = GetParam();
3490
3491 std::unique_ptr<IRContext> context;
3492 Instruction* inst;
3493 std::tie(context, inst) =
3494 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
3495 CheckForExpectedScalarConstant(
3496 inst, tc.expected_result, [](const analysis::Constant* c) {
3497 return c->AsFloatConstant()->GetDoubleValue();
3498 });
3499 }
3500
3501 // clang-format off
3502 INSTANTIATE_TEST_SUITE_P(DoubleConstantFoldingTest, DoubleInstructionFoldingTest,
3503 ::testing::Values(
3504 // Test case 0: Fold 2.0 - 1.0
3505 InstructionFoldingCase<double>(
3506 Header() + "%main = OpFunction %void None %void_func\n" +
3507 "%main_lab = OpLabel\n" +
3508 "%2 = OpFSub %double %double_2 %double_1\n" +
3509 "OpReturn\n" +
3510 "OpFunctionEnd",
3511 2, 1.0),
3512 // Test case 1: Fold 2.0 + 1.0
3513 InstructionFoldingCase<double>(
3514 Header() + "%main = OpFunction %void None %void_func\n" +
3515 "%main_lab = OpLabel\n" +
3516 "%2 = OpFAdd %double %double_2 %double_1\n" +
3517 "OpReturn\n" +
3518 "OpFunctionEnd",
3519 2, 3.0),
3520 // Test case 2: Fold 3.0 * 2.0
3521 InstructionFoldingCase<double>(
3522 Header() + "%main = OpFunction %void None %void_func\n" +
3523 "%main_lab = OpLabel\n" +
3524 "%2 = OpFMul %double %double_3 %double_2\n" +
3525 "OpReturn\n" +
3526 "OpFunctionEnd",
3527 2, 6.0),
3528 // Test case 3: Fold 1.0 / 2.0
3529 InstructionFoldingCase<double>(
3530 Header() + "%main = OpFunction %void None %void_func\n" +
3531 "%main_lab = OpLabel\n" +
3532 "%2 = OpFDiv %double %double_1 %double_2\n" +
3533 "OpReturn\n" +
3534 "OpFunctionEnd",
3535 2, 0.5),
3536 // Test case 4: Fold 1.0 / 0.0
3537 InstructionFoldingCase<double>(
3538 Header() + "%main = OpFunction %void None %void_func\n" +
3539 "%main_lab = OpLabel\n" +
3540 "%2 = OpFDiv %double %double_1 %double_0\n" +
3541 "OpReturn\n" +
3542 "OpFunctionEnd",
3543 2, std::numeric_limits<double>::infinity()),
3544 // Test case 5: Fold -1.0 / 0.0
3545 InstructionFoldingCase<double>(
3546 Header() + "%main = OpFunction %void None %void_func\n" +
3547 "%main_lab = OpLabel\n" +
3548 "%2 = OpFDiv %double %double_n1 %double_0\n" +
3549 "OpReturn\n" +
3550 "OpFunctionEnd",
3551 2, -std::numeric_limits<double>::infinity()),
3552 // Test case 6: Fold (2.0, 3.0) dot (2.0, 0.5)
3553 InstructionFoldingCase<double>(
3554 Header() + "%main = OpFunction %void None %void_func\n" +
3555 "%main_lab = OpLabel\n" +
3556 "%2 = OpDot %double %v2double_2_3 %v2double_2_0p5\n" +
3557 "OpReturn\n" +
3558 "OpFunctionEnd",
3559 2, 5.5f),
3560 // Test case 7: Fold (0.0, 0.0) dot v
3561 InstructionFoldingCase<double>(
3562 Header() + "%main = OpFunction %void None %void_func\n" +
3563 "%main_lab = OpLabel\n" +
3564 "%v = OpVariable %_ptr_v2double Function\n" +
3565 "%2 = OpLoad %v2double %v\n" +
3566 "%3 = OpDot %double %v2double_0_0 %2\n" +
3567 "OpReturn\n" +
3568 "OpFunctionEnd",
3569 3, 0.0f),
3570 // Test case 8: Fold v dot (0.0, 0.0)
3571 InstructionFoldingCase<double>(
3572 Header() + "%main = OpFunction %void None %void_func\n" +
3573 "%main_lab = OpLabel\n" +
3574 "%v = OpVariable %_ptr_v2double Function\n" +
3575 "%2 = OpLoad %v2double %v\n" +
3576 "%3 = OpDot %double %2 %v2double_0_0\n" +
3577 "OpReturn\n" +
3578 "OpFunctionEnd",
3579 3, 0.0f),
3580 // Test case 9: Fold Null dot v
3581 InstructionFoldingCase<double>(
3582 Header() + "%main = OpFunction %void None %void_func\n" +
3583 "%main_lab = OpLabel\n" +
3584 "%v = OpVariable %_ptr_v2double Function\n" +
3585 "%2 = OpLoad %v2double %v\n" +
3586 "%3 = OpDot %double %v2double_null %2\n" +
3587 "OpReturn\n" +
3588 "OpFunctionEnd",
3589 3, 0.0f),
3590 // Test case 10: Fold v dot Null
3591 InstructionFoldingCase<double>(
3592 Header() + "%main = OpFunction %void None %void_func\n" +
3593 "%main_lab = OpLabel\n" +
3594 "%v = OpVariable %_ptr_v2double Function\n" +
3595 "%2 = OpLoad %v2double %v\n" +
3596 "%3 = OpDot %double %2 %v2double_null\n" +
3597 "OpReturn\n" +
3598 "OpFunctionEnd",
3599 3, 0.0f),
3600 // Test case 11: Fold -2.0
3601 InstructionFoldingCase<double>(
3602 Header() + "%main = OpFunction %void None %void_func\n" +
3603 "%main_lab = OpLabel\n" +
3604 "%2 = OpFNegate %double %double_2\n" +
3605 "OpReturn\n" +
3606 "OpFunctionEnd",
3607 2, -2),
3608 // Test case 12: FMin 1.0 4.0
3609 InstructionFoldingCase<double>(
3610 Header() + "%main = OpFunction %void None %void_func\n" +
3611 "%main_lab = OpLabel\n" +
3612 "%2 = OpExtInst %double %1 FMin %double_1 %double_4\n" +
3613 "OpReturn\n" +
3614 "OpFunctionEnd",
3615 2, 1.0),
3616 // Test case 13: FMin 4.0 0.2
3617 InstructionFoldingCase<double>(
3618 Header() + "%main = OpFunction %void None %void_func\n" +
3619 "%main_lab = OpLabel\n" +
3620 "%2 = OpExtInst %double %1 FMin %double_4 %double_0p2\n" +
3621 "OpReturn\n" +
3622 "OpFunctionEnd",
3623 2, 0.2),
3624 // Test case 14: FMax 1.0 4.0
3625 InstructionFoldingCase<double>(
3626 Header() + "%main = OpFunction %void None %void_func\n" +
3627 "%main_lab = OpLabel\n" +
3628 "%2 = OpExtInst %double %1 FMax %double_1 %double_4\n" +
3629 "OpReturn\n" +
3630 "OpFunctionEnd",
3631 2, 4.0),
3632 // Test case 15: FMax 1.0 0.2
3633 InstructionFoldingCase<double>(
3634 Header() + "%main = OpFunction %void None %void_func\n" +
3635 "%main_lab = OpLabel\n" +
3636 "%2 = OpExtInst %double %1 FMax %double_1 %double_0p2\n" +
3637 "OpReturn\n" +
3638 "OpFunctionEnd",
3639 2, 1.0),
3640 // Test case 16: FClamp 1.0 0.2 4.0
3641 InstructionFoldingCase<double>(
3642 Header() + "%main = OpFunction %void None %void_func\n" +
3643 "%main_lab = OpLabel\n" +
3644 "%2 = OpExtInst %double %1 FClamp %double_1 %double_0p2 %double_4\n" +
3645 "OpReturn\n" +
3646 "OpFunctionEnd",
3647 2, 1.0),
3648 // Test case 17: FClamp 0.2 2.0 4.0
3649 InstructionFoldingCase<double>(
3650 Header() + "%main = OpFunction %void None %void_func\n" +
3651 "%main_lab = OpLabel\n" +
3652 "%2 = OpExtInst %double %1 FClamp %double_0p2 %double_2 %double_4\n" +
3653 "OpReturn\n" +
3654 "OpFunctionEnd",
3655 2, 2.0),
3656 // Test case 18: FClamp 5.0 2.0 4.0
3657 InstructionFoldingCase<double>(
3658 Header() + "%main = OpFunction %void None %void_func\n" +
3659 "%main_lab = OpLabel\n" +
3660 "%2 = OpExtInst %double %1 FClamp %double_5 %double_2 %double_4\n" +
3661 "OpReturn\n" +
3662 "OpFunctionEnd",
3663 2, 4.0),
3664 // Test case 19: FClamp 1.0 2.0 x
3665 InstructionFoldingCase<double>(
3666 Header() + "%main = OpFunction %void None %void_func\n" +
3667 "%main_lab = OpLabel\n" +
3668 "%undef = OpUndef %double\n" +
3669 "%2 = OpExtInst %double %1 FClamp %double_1 %double_2 %undef\n" +
3670 "OpReturn\n" +
3671 "OpFunctionEnd",
3672 2, 2.0),
3673 // Test case 20: FClamp 1.0 x 0.5
3674 InstructionFoldingCase<double>(
3675 Header() + "%main = OpFunction %void None %void_func\n" +
3676 "%main_lab = OpLabel\n" +
3677 "%undef = OpUndef %double\n" +
3678 "%2 = OpExtInst %double %1 FClamp %double_1 %undef %double_0p5\n" +
3679 "OpReturn\n" +
3680 "OpFunctionEnd",
3681 2, 0.5),
3682 // Test case 21: Sqrt 4.0
3683 InstructionFoldingCase<double>(
3684 Header() + "%main = OpFunction %void None %void_func\n" +
3685 "%main_lab = OpLabel\n" +
3686 "%undef = OpUndef %double\n" +
3687 "%2 = OpExtInst %double %1 Sqrt %double_4\n" +
3688 "OpReturn\n" +
3689 "OpFunctionEnd",
3690 2, 2.0),
3691 // Test case 22: Pow 2.0 3.0
3692 InstructionFoldingCase<double>(
3693 Header() + "%main = OpFunction %void None %void_func\n" +
3694 "%main_lab = OpLabel\n" +
3695 "%undef = OpUndef %double\n" +
3696 "%2 = OpExtInst %double %1 Pow %double_2 %double_3\n" +
3697 "OpReturn\n" +
3698 "OpFunctionEnd",
3699 2, 8.0),
3700 // Test case 23: Fold 1.0 / -0.0.
3701 InstructionFoldingCase<double>(
3702 Header() + "%main = OpFunction %void None %void_func\n" +
3703 "%main_lab = OpLabel\n" +
3704 "%2 = OpFDiv %double %double_1 %double_n0\n" +
3705 "OpReturn\n" +
3706 "OpFunctionEnd",
3707 2, -std::numeric_limits<double>::infinity()),
3708 // Test case 24: Fold -1.0 / -0.0
3709 InstructionFoldingCase<double>(
3710 Header() + "%main = OpFunction %void None %void_func\n" +
3711 "%main_lab = OpLabel\n" +
3712 "%2 = OpFDiv %double %double_n1 %double_n0\n" +
3713 "OpReturn\n" +
3714 "OpFunctionEnd",
3715 2, std::numeric_limits<double>::infinity()),
3716 // Test case 25: Fold 0.0 / 0.0
3717 InstructionFoldingCase<double>(
3718 Header() + "%main = OpFunction %void None %void_func\n" +
3719 "%main_lab = OpLabel\n" +
3720 "%2 = OpFDiv %double %double_0 %double_0\n" +
3721 "OpReturn\n" +
3722 "OpFunctionEnd",
3723 2, std::numeric_limits<double>::quiet_NaN()),
3724 // Test case 26: Fold 0.0 / -0.0
3725 InstructionFoldingCase<double>(
3726 Header() + "%main = OpFunction %void None %void_func\n" +
3727 "%main_lab = OpLabel\n" +
3728 "%2 = OpFDiv %double %double_0 %double_n0\n" +
3729 "OpReturn\n" +
3730 "OpFunctionEnd",
3731 2, std::numeric_limits<double>::quiet_NaN()),
3732 // Test case 27: NMin 1.0 4.0
3733 InstructionFoldingCase<double>(
3734 Header() + "%main = OpFunction %void None %void_func\n" +
3735 "%main_lab = OpLabel\n" +
3736 "%2 = OpExtInst %double %1 NMin %double_1 %double_4\n" +
3737 "OpReturn\n" +
3738 "OpFunctionEnd",
3739 2, 1.0),
3740 // Test case 28: NMin 4.0 0.2
3741 InstructionFoldingCase<double>(
3742 Header() + "%main = OpFunction %void None %void_func\n" +
3743 "%main_lab = OpLabel\n" +
3744 "%2 = OpExtInst %double %1 NMin %double_4 %double_0p2\n" +
3745 "OpReturn\n" +
3746 "OpFunctionEnd",
3747 2, 0.2),
3748 // Test case 29: NMax 1.0 4.0
3749 InstructionFoldingCase<double>(
3750 Header() + "%main = OpFunction %void None %void_func\n" +
3751 "%main_lab = OpLabel\n" +
3752 "%2 = OpExtInst %double %1 NMax %double_1 %double_4\n" +
3753 "OpReturn\n" +
3754 "OpFunctionEnd",
3755 2, 4.0),
3756 // Test case 30: NMin NaN 4.0
3757 InstructionFoldingCase<double>(
3758 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3759 "%main_lab = OpLabel\n" +
3760 "%2 = OpExtInst %double %1 NMin %double_nan %double_4\n" +
3761 "OpReturn\n" +
3762 "OpFunctionEnd",
3763 2, 4.0),
3764 // Test case 31: NMin 2.0 NaN
3765 InstructionFoldingCase<double>(
3766 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3767 "%main_lab = OpLabel\n" +
3768 "%2 = OpExtInst %double %1 NMin %double_2 %double_nan\n" +
3769 "OpReturn\n" +
3770 "OpFunctionEnd",
3771 2, 2.0),
3772 // Test case 32: NMin NaN NaN
3773 InstructionFoldingCase<double>(
3774 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3775 "%main_lab = OpLabel\n" +
3776 "%2 = OpExtInst %double %1 NMin %double_nan %double_nan\n" +
3777 "OpReturn\n" +
3778 "OpFunctionEnd",
3779 2, std::numeric_limits<double>::quiet_NaN()),
3780 // Test case 33: NMax 1.0 0.2
3781 InstructionFoldingCase<double>(
3782 Header() + "%main = OpFunction %void None %void_func\n" +
3783 "%main_lab = OpLabel\n" +
3784 "%2 = OpExtInst %double %1 NMax %double_1 %double_0p2\n" +
3785 "OpReturn\n" +
3786 "OpFunctionEnd",
3787 2, 1.0),
3788 // Test case 34: NMax 1.0 NaN
3789 InstructionFoldingCase<double>(
3790 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3791 "%main_lab = OpLabel\n" +
3792 "%2 = OpExtInst %double %1 NMax %double_1 %double_nan\n" +
3793 "OpReturn\n" +
3794 "OpFunctionEnd",
3795 2, 1.0),
3796 // Test case 35: NMax NaN 0.5
3797 InstructionFoldingCase<double>(
3798 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3799 "%main_lab = OpLabel\n" +
3800 "%2 = OpExtInst %double %1 NMax %double_nan %double_0p5\n" +
3801 "OpReturn\n" +
3802 "OpFunctionEnd",
3803 2, 0.5),
3804 // Test case 36: NMax NaN NaN
3805 InstructionFoldingCase<double>(
3806 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3807 "%main_lab = OpLabel\n" +
3808 "%2 = OpExtInst %double %1 NMax %double_nan %double_nan\n" +
3809 "OpReturn\n" +
3810 "OpFunctionEnd",
3811 2, std::numeric_limits<double>::quiet_NaN()),
3812 // Test case 37: NClamp 1.0 0.2 4.0
3813 InstructionFoldingCase<double>(
3814 Header() + "%main = OpFunction %void None %void_func\n" +
3815 "%main_lab = OpLabel\n" +
3816 "%2 = OpExtInst %double %1 NClamp %double_1 %double_0p2 %double_4\n" +
3817 "OpReturn\n" +
3818 "OpFunctionEnd",
3819 2, 1.0),
3820 // Test case 38: NClamp 0.2 2.0 4.0
3821 InstructionFoldingCase<double>(
3822 Header() + "%main = OpFunction %void None %void_func\n" +
3823 "%main_lab = OpLabel\n" +
3824 "%2 = OpExtInst %double %1 NClamp %double_0p2 %double_2 %double_4\n" +
3825 "OpReturn\n" +
3826 "OpFunctionEnd",
3827 2, 2.0),
3828 // Test case 39: NClamp 5.0 2.0 4.0
3829 InstructionFoldingCase<double>(
3830 Header() + "%main = OpFunction %void None %void_func\n" +
3831 "%main_lab = OpLabel\n" +
3832 "%2 = OpExtInst %double %1 NClamp %double_5 %double_2 %double_4\n" +
3833 "OpReturn\n" +
3834 "OpFunctionEnd",
3835 2, 4.0),
3836 // Test case 40: NClamp 1.0 2.0 x
3837 InstructionFoldingCase<double>(
3838 Header() + "%main = OpFunction %void None %void_func\n" +
3839 "%main_lab = OpLabel\n" +
3840 "%undef = OpUndef %double\n" +
3841 "%2 = OpExtInst %double %1 NClamp %double_1 %double_2 %undef\n" +
3842 "OpReturn\n" +
3843 "OpFunctionEnd",
3844 2, 2.0),
3845 // Test case 41: NClamp 1.0 x 0.5
3846 InstructionFoldingCase<double>(
3847 Header() + "%main = OpFunction %void None %void_func\n" +
3848 "%main_lab = OpLabel\n" +
3849 "%undef = OpUndef %double\n" +
3850 "%2 = OpExtInst %double %1 NClamp %double_1 %undef %double_0p5\n" +
3851 "OpReturn\n" +
3852 "OpFunctionEnd",
3853 2, 0.5),
3854 // Test case 42: NClamp NaN 2.0 4.0
3855 InstructionFoldingCase<double>(
3856 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3857 "%main_lab = OpLabel\n" +
3858 "%2 = OpExtInst %double %1 NClamp %double_nan %double_2 %double_4\n" +
3859 "OpReturn\n" +
3860 "OpFunctionEnd",
3861 2, 2.0),
3862 // Test case 43: NClamp 5.0 NaN 4.0
3863 InstructionFoldingCase<double>(
3864 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3865 "%main_lab = OpLabel\n" +
3866 "%2 = OpExtInst %double %1 NClamp %double_5 %double_nan %double_4\n" +
3867 "OpReturn\n" +
3868 "OpFunctionEnd",
3869 2, 4.0),
3870 // Test case 44: NClamp 1.0 2.0 NaN
3871 InstructionFoldingCase<double>(
3872 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3873 "%main_lab = OpLabel\n" +
3874 "%2 = OpExtInst %double %1 NClamp %double_1 %double_2 %double_nan\n" +
3875 "OpReturn\n" +
3876 "OpFunctionEnd",
3877 2, 2.0),
3878 // Test case 45: NClamp 4.0 NaN NaN
3879 InstructionFoldingCase<double>(
3880 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3881 "%main_lab = OpLabel\n" +
3882 "%2 = OpExtInst %double %1 NClamp %double_4 %double_nan %double_nan\n" +
3883 "OpReturn\n" +
3884 "OpFunctionEnd",
3885 2, 4.0),
3886 // Test case 46: NClamp NaN NaN NaN
3887 InstructionFoldingCase<double>(
3888 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
3889 "%main_lab = OpLabel\n" +
3890 "%2 = OpExtInst %double %1 NClamp %double_nan %double_nan %double_nan\n" +
3891 "OpReturn\n" +
3892 "OpFunctionEnd",
3893 2, std::numeric_limits<double>::quiet_NaN())
3894 ));
3895 // clang-format on
3896
3897 // clang-format off
3898 INSTANTIATE_TEST_SUITE_P(DoubleOrderedCompareConstantFoldingTest, BooleanInstructionFoldingTest,
3899 ::testing::Values(
3900 // Test case 0: fold 1.0 == 2.0
3901 InstructionFoldingCase<bool>(
3902 Header() + "%main = OpFunction %void None %void_func\n" +
3903 "%main_lab = OpLabel\n" +
3904 "%2 = OpFOrdEqual %bool %double_1 %double_2\n" +
3905 "OpReturn\n" +
3906 "OpFunctionEnd",
3907 2, false),
3908 // Test case 1: fold 1.0 != 2.0
3909 InstructionFoldingCase<bool>(
3910 Header() + "%main = OpFunction %void None %void_func\n" +
3911 "%main_lab = OpLabel\n" +
3912 "%2 = OpFOrdNotEqual %bool %double_1 %double_2\n" +
3913 "OpReturn\n" +
3914 "OpFunctionEnd",
3915 2, true),
3916 // Test case 2: fold 1.0 < 2.0
3917 InstructionFoldingCase<bool>(
3918 Header() + "%main = OpFunction %void None %void_func\n" +
3919 "%main_lab = OpLabel\n" +
3920 "%2 = OpFOrdLessThan %bool %double_1 %double_2\n" +
3921 "OpReturn\n" +
3922 "OpFunctionEnd",
3923 2, true),
3924 // Test case 3: fold 1.0 > 2.0
3925 InstructionFoldingCase<bool>(
3926 Header() + "%main = OpFunction %void None %void_func\n" +
3927 "%main_lab = OpLabel\n" +
3928 "%2 = OpFOrdGreaterThan %bool %double_1 %double_2\n" +
3929 "OpReturn\n" +
3930 "OpFunctionEnd",
3931 2, false),
3932 // Test case 4: fold 1.0 <= 2.0
3933 InstructionFoldingCase<bool>(
3934 Header() + "%main = OpFunction %void None %void_func\n" +
3935 "%main_lab = OpLabel\n" +
3936 "%2 = OpFOrdLessThanEqual %bool %double_1 %double_2\n" +
3937 "OpReturn\n" +
3938 "OpFunctionEnd",
3939 2, true),
3940 // Test case 5: fold 1.0 >= 2.0
3941 InstructionFoldingCase<bool>(
3942 Header() + "%main = OpFunction %void None %void_func\n" +
3943 "%main_lab = OpLabel\n" +
3944 "%2 = OpFOrdGreaterThanEqual %bool %double_1 %double_2\n" +
3945 "OpReturn\n" +
3946 "OpFunctionEnd",
3947 2, false),
3948 // Test case 6: fold 1.0 == 1.0
3949 InstructionFoldingCase<bool>(
3950 Header() + "%main = OpFunction %void None %void_func\n" +
3951 "%main_lab = OpLabel\n" +
3952 "%2 = OpFOrdEqual %bool %double_1 %double_1\n" +
3953 "OpReturn\n" +
3954 "OpFunctionEnd",
3955 2, true),
3956 // Test case 7: fold 1.0 != 1.0
3957 InstructionFoldingCase<bool>(
3958 Header() + "%main = OpFunction %void None %void_func\n" +
3959 "%main_lab = OpLabel\n" +
3960 "%2 = OpFOrdNotEqual %bool %double_1 %double_1\n" +
3961 "OpReturn\n" +
3962 "OpFunctionEnd",
3963 2, false),
3964 // Test case 8: fold 1.0 < 1.0
3965 InstructionFoldingCase<bool>(
3966 Header() + "%main = OpFunction %void None %void_func\n" +
3967 "%main_lab = OpLabel\n" +
3968 "%2 = OpFOrdLessThan %bool %double_1 %double_1\n" +
3969 "OpReturn\n" +
3970 "OpFunctionEnd",
3971 2, false),
3972 // Test case 9: fold 1.0 > 1.0
3973 InstructionFoldingCase<bool>(
3974 Header() + "%main = OpFunction %void None %void_func\n" +
3975 "%main_lab = OpLabel\n" +
3976 "%2 = OpFOrdGreaterThan %bool %double_1 %double_1\n" +
3977 "OpReturn\n" +
3978 "OpFunctionEnd",
3979 2, false),
3980 // Test case 10: fold 1.0 <= 1.0
3981 InstructionFoldingCase<bool>(
3982 Header() + "%main = OpFunction %void None %void_func\n" +
3983 "%main_lab = OpLabel\n" +
3984 "%2 = OpFOrdLessThanEqual %bool %double_1 %double_1\n" +
3985 "OpReturn\n" +
3986 "OpFunctionEnd",
3987 2, true),
3988 // Test case 11: fold 1.0 >= 1.0
3989 InstructionFoldingCase<bool>(
3990 Header() + "%main = OpFunction %void None %void_func\n" +
3991 "%main_lab = OpLabel\n" +
3992 "%2 = OpFOrdGreaterThanEqual %bool %double_1 %double_1\n" +
3993 "OpReturn\n" +
3994 "OpFunctionEnd",
3995 2, true),
3996 // Test case 12: fold 2.0 < 1.0
3997 InstructionFoldingCase<bool>(
3998 Header() + "%main = OpFunction %void None %void_func\n" +
3999 "%main_lab = OpLabel\n" +
4000 "%2 = OpFOrdLessThan %bool %double_2 %double_1\n" +
4001 "OpReturn\n" +
4002 "OpFunctionEnd",
4003 2, false),
4004 // Test case 13: fold 2.0 > 1.0
4005 InstructionFoldingCase<bool>(
4006 Header() + "%main = OpFunction %void None %void_func\n" +
4007 "%main_lab = OpLabel\n" +
4008 "%2 = OpFOrdGreaterThan %bool %double_2 %double_1\n" +
4009 "OpReturn\n" +
4010 "OpFunctionEnd",
4011 2, true),
4012 // Test case 14: fold 2.0 <= 1.0
4013 InstructionFoldingCase<bool>(
4014 Header() + "%main = OpFunction %void None %void_func\n" +
4015 "%main_lab = OpLabel\n" +
4016 "%2 = OpFOrdLessThanEqual %bool %double_2 %double_1\n" +
4017 "OpReturn\n" +
4018 "OpFunctionEnd",
4019 2, false),
4020 // Test case 15: fold 2.0 >= 1.0
4021 InstructionFoldingCase<bool>(
4022 Header() + "%main = OpFunction %void None %void_func\n" +
4023 "%main_lab = OpLabel\n" +
4024 "%2 = OpFOrdGreaterThanEqual %bool %double_2 %double_1\n" +
4025 "OpReturn\n" +
4026 "OpFunctionEnd",
4027 2, true)
4028 ));
4029
4030 INSTANTIATE_TEST_SUITE_P(DoubleUnorderedCompareConstantFoldingTest, BooleanInstructionFoldingTest,
4031 ::testing::Values(
4032 // Test case 0: fold 1.0 == 2.0
4033 InstructionFoldingCase<bool>(
4034 Header() + "%main = OpFunction %void None %void_func\n" +
4035 "%main_lab = OpLabel\n" +
4036 "%2 = OpFUnordEqual %bool %double_1 %double_2\n" +
4037 "OpReturn\n" +
4038 "OpFunctionEnd",
4039 2, false),
4040 // Test case 1: fold 1.0 != 2.0
4041 InstructionFoldingCase<bool>(
4042 Header() + "%main = OpFunction %void None %void_func\n" +
4043 "%main_lab = OpLabel\n" +
4044 "%2 = OpFUnordNotEqual %bool %double_1 %double_2\n" +
4045 "OpReturn\n" +
4046 "OpFunctionEnd",
4047 2, true),
4048 // Test case 2: fold 1.0 < 2.0
4049 InstructionFoldingCase<bool>(
4050 Header() + "%main = OpFunction %void None %void_func\n" +
4051 "%main_lab = OpLabel\n" +
4052 "%2 = OpFUnordLessThan %bool %double_1 %double_2\n" +
4053 "OpReturn\n" +
4054 "OpFunctionEnd",
4055 2, true),
4056 // Test case 3: fold 1.0 > 2.0
4057 InstructionFoldingCase<bool>(
4058 Header() + "%main = OpFunction %void None %void_func\n" +
4059 "%main_lab = OpLabel\n" +
4060 "%2 = OpFUnordGreaterThan %bool %double_1 %double_2\n" +
4061 "OpReturn\n" +
4062 "OpFunctionEnd",
4063 2, false),
4064 // Test case 4: fold 1.0 <= 2.0
4065 InstructionFoldingCase<bool>(
4066 Header() + "%main = OpFunction %void None %void_func\n" +
4067 "%main_lab = OpLabel\n" +
4068 "%2 = OpFUnordLessThanEqual %bool %double_1 %double_2\n" +
4069 "OpReturn\n" +
4070 "OpFunctionEnd",
4071 2, true),
4072 // Test case 5: fold 1.0 >= 2.0
4073 InstructionFoldingCase<bool>(
4074 Header() + "%main = OpFunction %void None %void_func\n" +
4075 "%main_lab = OpLabel\n" +
4076 "%2 = OpFUnordGreaterThanEqual %bool %double_1 %double_2\n" +
4077 "OpReturn\n" +
4078 "OpFunctionEnd",
4079 2, false),
4080 // Test case 6: fold 1.0 == 1.0
4081 InstructionFoldingCase<bool>(
4082 Header() + "%main = OpFunction %void None %void_func\n" +
4083 "%main_lab = OpLabel\n" +
4084 "%2 = OpFUnordEqual %bool %double_1 %double_1\n" +
4085 "OpReturn\n" +
4086 "OpFunctionEnd",
4087 2, true),
4088 // Test case 7: fold 1.0 != 1.0
4089 InstructionFoldingCase<bool>(
4090 Header() + "%main = OpFunction %void None %void_func\n" +
4091 "%main_lab = OpLabel\n" +
4092 "%2 = OpFUnordNotEqual %bool %double_1 %double_1\n" +
4093 "OpReturn\n" +
4094 "OpFunctionEnd",
4095 2, false),
4096 // Test case 8: fold 1.0 < 1.0
4097 InstructionFoldingCase<bool>(
4098 Header() + "%main = OpFunction %void None %void_func\n" +
4099 "%main_lab = OpLabel\n" +
4100 "%2 = OpFUnordLessThan %bool %double_1 %double_1\n" +
4101 "OpReturn\n" +
4102 "OpFunctionEnd",
4103 2, false),
4104 // Test case 9: fold 1.0 > 1.0
4105 InstructionFoldingCase<bool>(
4106 Header() + "%main = OpFunction %void None %void_func\n" +
4107 "%main_lab = OpLabel\n" +
4108 "%2 = OpFUnordGreaterThan %bool %double_1 %double_1\n" +
4109 "OpReturn\n" +
4110 "OpFunctionEnd",
4111 2, false),
4112 // Test case 10: fold 1.0 <= 1.0
4113 InstructionFoldingCase<bool>(
4114 Header() + "%main = OpFunction %void None %void_func\n" +
4115 "%main_lab = OpLabel\n" +
4116 "%2 = OpFUnordLessThanEqual %bool %double_1 %double_1\n" +
4117 "OpReturn\n" +
4118 "OpFunctionEnd",
4119 2, true),
4120 // Test case 11: fold 1.0 >= 1.0
4121 InstructionFoldingCase<bool>(
4122 Header() + "%main = OpFunction %void None %void_func\n" +
4123 "%main_lab = OpLabel\n" +
4124 "%2 = OpFUnordGreaterThanEqual %bool %double_1 %double_1\n" +
4125 "OpReturn\n" +
4126 "OpFunctionEnd",
4127 2, true),
4128 // Test case 12: fold 2.0 < 1.0
4129 InstructionFoldingCase<bool>(
4130 Header() + "%main = OpFunction %void None %void_func\n" +
4131 "%main_lab = OpLabel\n" +
4132 "%2 = OpFUnordLessThan %bool %double_2 %double_1\n" +
4133 "OpReturn\n" +
4134 "OpFunctionEnd",
4135 2, false),
4136 // Test case 13: fold 2.0 > 1.0
4137 InstructionFoldingCase<bool>(
4138 Header() + "%main = OpFunction %void None %void_func\n" +
4139 "%main_lab = OpLabel\n" +
4140 "%2 = OpFUnordGreaterThan %bool %double_2 %double_1\n" +
4141 "OpReturn\n" +
4142 "OpFunctionEnd",
4143 2, true),
4144 // Test case 14: fold 2.0 <= 1.0
4145 InstructionFoldingCase<bool>(
4146 Header() + "%main = OpFunction %void None %void_func\n" +
4147 "%main_lab = OpLabel\n" +
4148 "%2 = OpFUnordLessThanEqual %bool %double_2 %double_1\n" +
4149 "OpReturn\n" +
4150 "OpFunctionEnd",
4151 2, false),
4152 // Test case 15: fold 2.0 >= 1.0
4153 InstructionFoldingCase<bool>(
4154 Header() + "%main = OpFunction %void None %void_func\n" +
4155 "%main_lab = OpLabel\n" +
4156 "%2 = OpFUnordGreaterThanEqual %bool %double_2 %double_1\n" +
4157 "OpReturn\n" +
4158 "OpFunctionEnd",
4159 2, true)
4160 ));
4161
4162 INSTANTIATE_TEST_SUITE_P(FloatOrderedCompareConstantFoldingTest, BooleanInstructionFoldingTest,
4163 ::testing::Values(
4164 // Test case 0: fold 1.0 == 2.0
4165 InstructionFoldingCase<bool>(
4166 Header() + "%main = OpFunction %void None %void_func\n" +
4167 "%main_lab = OpLabel\n" +
4168 "%2 = OpFOrdEqual %bool %float_1 %float_2\n" +
4169 "OpReturn\n" +
4170 "OpFunctionEnd",
4171 2, false),
4172 // Test case 1: fold 1.0 != 2.0
4173 InstructionFoldingCase<bool>(
4174 Header() + "%main = OpFunction %void None %void_func\n" +
4175 "%main_lab = OpLabel\n" +
4176 "%2 = OpFOrdNotEqual %bool %float_1 %float_2\n" +
4177 "OpReturn\n" +
4178 "OpFunctionEnd",
4179 2, true),
4180 // Test case 2: fold 1.0 < 2.0
4181 InstructionFoldingCase<bool>(
4182 Header() + "%main = OpFunction %void None %void_func\n" +
4183 "%main_lab = OpLabel\n" +
4184 "%2 = OpFOrdLessThan %bool %float_1 %float_2\n" +
4185 "OpReturn\n" +
4186 "OpFunctionEnd",
4187 2, true),
4188 // Test case 3: fold 1.0 > 2.0
4189 InstructionFoldingCase<bool>(
4190 Header() + "%main = OpFunction %void None %void_func\n" +
4191 "%main_lab = OpLabel\n" +
4192 "%2 = OpFOrdGreaterThan %bool %float_1 %float_2\n" +
4193 "OpReturn\n" +
4194 "OpFunctionEnd",
4195 2, false),
4196 // Test case 4: fold 1.0 <= 2.0
4197 InstructionFoldingCase<bool>(
4198 Header() + "%main = OpFunction %void None %void_func\n" +
4199 "%main_lab = OpLabel\n" +
4200 "%2 = OpFOrdLessThanEqual %bool %float_1 %float_2\n" +
4201 "OpReturn\n" +
4202 "OpFunctionEnd",
4203 2, true),
4204 // Test case 5: fold 1.0 >= 2.0
4205 InstructionFoldingCase<bool>(
4206 Header() + "%main = OpFunction %void None %void_func\n" +
4207 "%main_lab = OpLabel\n" +
4208 "%2 = OpFOrdGreaterThanEqual %bool %float_1 %float_2\n" +
4209 "OpReturn\n" +
4210 "OpFunctionEnd",
4211 2, false),
4212 // Test case 6: fold 1.0 == 1.0
4213 InstructionFoldingCase<bool>(
4214 Header() + "%main = OpFunction %void None %void_func\n" +
4215 "%main_lab = OpLabel\n" +
4216 "%2 = OpFOrdEqual %bool %float_1 %float_1\n" +
4217 "OpReturn\n" +
4218 "OpFunctionEnd",
4219 2, true),
4220 // Test case 7: fold 1.0 != 1.0
4221 InstructionFoldingCase<bool>(
4222 Header() + "%main = OpFunction %void None %void_func\n" +
4223 "%main_lab = OpLabel\n" +
4224 "%2 = OpFOrdNotEqual %bool %float_1 %float_1\n" +
4225 "OpReturn\n" +
4226 "OpFunctionEnd",
4227 2, false),
4228 // Test case 8: fold 1.0 < 1.0
4229 InstructionFoldingCase<bool>(
4230 Header() + "%main = OpFunction %void None %void_func\n" +
4231 "%main_lab = OpLabel\n" +
4232 "%2 = OpFOrdLessThan %bool %float_1 %float_1\n" +
4233 "OpReturn\n" +
4234 "OpFunctionEnd",
4235 2, false),
4236 // Test case 9: fold 1.0 > 1.0
4237 InstructionFoldingCase<bool>(
4238 Header() + "%main = OpFunction %void None %void_func\n" +
4239 "%main_lab = OpLabel\n" +
4240 "%2 = OpFOrdGreaterThan %bool %float_1 %float_1\n" +
4241 "OpReturn\n" +
4242 "OpFunctionEnd",
4243 2, false),
4244 // Test case 10: fold 1.0 <= 1.0
4245 InstructionFoldingCase<bool>(
4246 Header() + "%main = OpFunction %void None %void_func\n" +
4247 "%main_lab = OpLabel\n" +
4248 "%2 = OpFOrdLessThanEqual %bool %float_1 %float_1\n" +
4249 "OpReturn\n" +
4250 "OpFunctionEnd",
4251 2, true),
4252 // Test case 11: fold 1.0 >= 1.0
4253 InstructionFoldingCase<bool>(
4254 Header() + "%main = OpFunction %void None %void_func\n" +
4255 "%main_lab = OpLabel\n" +
4256 "%2 = OpFOrdGreaterThanEqual %bool %float_1 %float_1\n" +
4257 "OpReturn\n" +
4258 "OpFunctionEnd",
4259 2, true),
4260 // Test case 12: fold 2.0 < 1.0
4261 InstructionFoldingCase<bool>(
4262 Header() + "%main = OpFunction %void None %void_func\n" +
4263 "%main_lab = OpLabel\n" +
4264 "%2 = OpFOrdLessThan %bool %float_2 %float_1\n" +
4265 "OpReturn\n" +
4266 "OpFunctionEnd",
4267 2, false),
4268 // Test case 13: fold 2.0 > 1.0
4269 InstructionFoldingCase<bool>(
4270 Header() + "%main = OpFunction %void None %void_func\n" +
4271 "%main_lab = OpLabel\n" +
4272 "%2 = OpFOrdGreaterThan %bool %float_2 %float_1\n" +
4273 "OpReturn\n" +
4274 "OpFunctionEnd",
4275 2, true),
4276 // Test case 14: fold 2.0 <= 1.0
4277 InstructionFoldingCase<bool>(
4278 Header() + "%main = OpFunction %void None %void_func\n" +
4279 "%main_lab = OpLabel\n" +
4280 "%2 = OpFOrdLessThanEqual %bool %float_2 %float_1\n" +
4281 "OpReturn\n" +
4282 "OpFunctionEnd",
4283 2, false),
4284 // Test case 15: fold 2.0 >= 1.0
4285 InstructionFoldingCase<bool>(
4286 Header() + "%main = OpFunction %void None %void_func\n" +
4287 "%main_lab = OpLabel\n" +
4288 "%2 = OpFOrdGreaterThanEqual %bool %float_2 %float_1\n" +
4289 "OpReturn\n" +
4290 "OpFunctionEnd",
4291 2, true)
4292 ));
4293
4294 INSTANTIATE_TEST_SUITE_P(FloatUnorderedCompareConstantFoldingTest, BooleanInstructionFoldingTest,
4295 ::testing::Values(
4296 // Test case 0: fold 1.0 == 2.0
4297 InstructionFoldingCase<bool>(
4298 Header() + "%main = OpFunction %void None %void_func\n" +
4299 "%main_lab = OpLabel\n" +
4300 "%2 = OpFUnordEqual %bool %float_1 %float_2\n" +
4301 "OpReturn\n" +
4302 "OpFunctionEnd",
4303 2, false),
4304 // Test case 1: fold 1.0 != 2.0
4305 InstructionFoldingCase<bool>(
4306 Header() + "%main = OpFunction %void None %void_func\n" +
4307 "%main_lab = OpLabel\n" +
4308 "%2 = OpFUnordNotEqual %bool %float_1 %float_2\n" +
4309 "OpReturn\n" +
4310 "OpFunctionEnd",
4311 2, true),
4312 // Test case 2: fold 1.0 < 2.0
4313 InstructionFoldingCase<bool>(
4314 Header() + "%main = OpFunction %void None %void_func\n" +
4315 "%main_lab = OpLabel\n" +
4316 "%2 = OpFUnordLessThan %bool %float_1 %float_2\n" +
4317 "OpReturn\n" +
4318 "OpFunctionEnd",
4319 2, true),
4320 // Test case 3: fold 1.0 > 2.0
4321 InstructionFoldingCase<bool>(
4322 Header() + "%main = OpFunction %void None %void_func\n" +
4323 "%main_lab = OpLabel\n" +
4324 "%2 = OpFUnordGreaterThan %bool %float_1 %float_2\n" +
4325 "OpReturn\n" +
4326 "OpFunctionEnd",
4327 2, false),
4328 // Test case 4: fold 1.0 <= 2.0
4329 InstructionFoldingCase<bool>(
4330 Header() + "%main = OpFunction %void None %void_func\n" +
4331 "%main_lab = OpLabel\n" +
4332 "%2 = OpFUnordLessThanEqual %bool %float_1 %float_2\n" +
4333 "OpReturn\n" +
4334 "OpFunctionEnd",
4335 2, true),
4336 // Test case 5: fold 1.0 >= 2.0
4337 InstructionFoldingCase<bool>(
4338 Header() + "%main = OpFunction %void None %void_func\n" +
4339 "%main_lab = OpLabel\n" +
4340 "%2 = OpFUnordGreaterThanEqual %bool %float_1 %float_2\n" +
4341 "OpReturn\n" +
4342 "OpFunctionEnd",
4343 2, false),
4344 // Test case 6: fold 1.0 == 1.0
4345 InstructionFoldingCase<bool>(
4346 Header() + "%main = OpFunction %void None %void_func\n" +
4347 "%main_lab = OpLabel\n" +
4348 "%2 = OpFUnordEqual %bool %float_1 %float_1\n" +
4349 "OpReturn\n" +
4350 "OpFunctionEnd",
4351 2, true),
4352 // Test case 7: fold 1.0 != 1.0
4353 InstructionFoldingCase<bool>(
4354 Header() + "%main = OpFunction %void None %void_func\n" +
4355 "%main_lab = OpLabel\n" +
4356 "%2 = OpFUnordNotEqual %bool %float_1 %float_1\n" +
4357 "OpReturn\n" +
4358 "OpFunctionEnd",
4359 2, false),
4360 // Test case 8: fold 1.0 < 1.0
4361 InstructionFoldingCase<bool>(
4362 Header() + "%main = OpFunction %void None %void_func\n" +
4363 "%main_lab = OpLabel\n" +
4364 "%2 = OpFUnordLessThan %bool %float_1 %float_1\n" +
4365 "OpReturn\n" +
4366 "OpFunctionEnd",
4367 2, false),
4368 // Test case 9: fold 1.0 > 1.0
4369 InstructionFoldingCase<bool>(
4370 Header() + "%main = OpFunction %void None %void_func\n" +
4371 "%main_lab = OpLabel\n" +
4372 "%2 = OpFUnordGreaterThan %bool %float_1 %float_1\n" +
4373 "OpReturn\n" +
4374 "OpFunctionEnd",
4375 2, false),
4376 // Test case 10: fold 1.0 <= 1.0
4377 InstructionFoldingCase<bool>(
4378 Header() + "%main = OpFunction %void None %void_func\n" +
4379 "%main_lab = OpLabel\n" +
4380 "%2 = OpFUnordLessThanEqual %bool %float_1 %float_1\n" +
4381 "OpReturn\n" +
4382 "OpFunctionEnd",
4383 2, true),
4384 // Test case 11: fold 1.0 >= 1.0
4385 InstructionFoldingCase<bool>(
4386 Header() + "%main = OpFunction %void None %void_func\n" +
4387 "%main_lab = OpLabel\n" +
4388 "%2 = OpFUnordGreaterThanEqual %bool %float_1 %float_1\n" +
4389 "OpReturn\n" +
4390 "OpFunctionEnd",
4391 2, true),
4392 // Test case 12: fold 2.0 < 1.0
4393 InstructionFoldingCase<bool>(
4394 Header() + "%main = OpFunction %void None %void_func\n" +
4395 "%main_lab = OpLabel\n" +
4396 "%2 = OpFUnordLessThan %bool %float_2 %float_1\n" +
4397 "OpReturn\n" +
4398 "OpFunctionEnd",
4399 2, false),
4400 // Test case 13: fold 2.0 > 1.0
4401 InstructionFoldingCase<bool>(
4402 Header() + "%main = OpFunction %void None %void_func\n" +
4403 "%main_lab = OpLabel\n" +
4404 "%2 = OpFUnordGreaterThan %bool %float_2 %float_1\n" +
4405 "OpReturn\n" +
4406 "OpFunctionEnd",
4407 2, true),
4408 // Test case 14: fold 2.0 <= 1.0
4409 InstructionFoldingCase<bool>(
4410 Header() + "%main = OpFunction %void None %void_func\n" +
4411 "%main_lab = OpLabel\n" +
4412 "%2 = OpFUnordLessThanEqual %bool %float_2 %float_1\n" +
4413 "OpReturn\n" +
4414 "OpFunctionEnd",
4415 2, false),
4416 // Test case 15: fold 2.0 >= 1.0
4417 InstructionFoldingCase<bool>(
4418 Header() + "%main = OpFunction %void None %void_func\n" +
4419 "%main_lab = OpLabel\n" +
4420 "%2 = OpFUnordGreaterThanEqual %bool %float_2 %float_1\n" +
4421 "OpReturn\n" +
4422 "OpFunctionEnd",
4423 2, true)
4424 ));
4425
4426 INSTANTIATE_TEST_SUITE_P(DoubleNaNCompareConstantFoldingTest, BooleanInstructionFoldingTest,
4427 ::testing::Values(
4428 // Test case 0: fold NaN == 0 (ord)
4429 InstructionFoldingCase<bool>(
4430 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
4431 "%main_lab = OpLabel\n" +
4432 "%2 = OpFOrdEqual %bool %double_nan %double_0\n" +
4433 "OpReturn\n" +
4434 "OpFunctionEnd",
4435 2, false),
4436 // Test case 1: fold NaN == NaN (unord)
4437 InstructionFoldingCase<bool>(
4438 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
4439 "%main_lab = OpLabel\n" +
4440 "%2 = OpFUnordEqual %bool %double_nan %double_0\n" +
4441 "OpReturn\n" +
4442 "OpFunctionEnd",
4443 2, true),
4444 // Test case 2: fold NaN != NaN (ord)
4445 InstructionFoldingCase<bool>(
4446 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
4447 "%main_lab = OpLabel\n" +
4448 "%2 = OpFOrdNotEqual %bool %double_nan %double_0\n" +
4449 "OpReturn\n" +
4450 "OpFunctionEnd",
4451 2, false),
4452 // Test case 3: fold NaN != NaN (unord)
4453 InstructionFoldingCase<bool>(
4454 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
4455 "%main_lab = OpLabel\n" +
4456 "%2 = OpFUnordNotEqual %bool %double_nan %double_0\n" +
4457 "OpReturn\n" +
4458 "OpFunctionEnd",
4459 2, true)
4460 ));
4461
4462 INSTANTIATE_TEST_SUITE_P(FloatNaNCompareConstantFoldingTest, BooleanInstructionFoldingTest,
4463 ::testing::Values(
4464 // Test case 0: fold NaN == 0 (ord)
4465 InstructionFoldingCase<bool>(
4466 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
4467 "%main_lab = OpLabel\n" +
4468 "%2 = OpFOrdEqual %bool %float_nan %float_0\n" +
4469 "OpReturn\n" +
4470 "OpFunctionEnd",
4471 2, false),
4472 // Test case 1: fold NaN == NaN (unord)
4473 InstructionFoldingCase<bool>(
4474 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
4475 "%main_lab = OpLabel\n" +
4476 "%2 = OpFUnordEqual %bool %float_nan %float_0\n" +
4477 "OpReturn\n" +
4478 "OpFunctionEnd",
4479 2, true),
4480 // Test case 2: fold NaN != NaN (ord)
4481 InstructionFoldingCase<bool>(
4482 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
4483 "%main_lab = OpLabel\n" +
4484 "%2 = OpFOrdNotEqual %bool %float_nan %float_0\n" +
4485 "OpReturn\n" +
4486 "OpFunctionEnd",
4487 2, false),
4488 // Test case 3: fold NaN != NaN (unord)
4489 InstructionFoldingCase<bool>(
4490 HeaderWithNaN() + "%main = OpFunction %void None %void_func\n" +
4491 "%main_lab = OpLabel\n" +
4492 "%2 = OpFUnordNotEqual %bool %float_nan %float_0\n" +
4493 "OpReturn\n" +
4494 "OpFunctionEnd",
4495 2, true)
4496 ));
4497 // clang-format on
4498
4499 template <class ResultType>
4500 struct InstructionFoldingCaseWithMap {
4501 InstructionFoldingCaseWithMap(const std::string& tb, uint32_t id,
4502 ResultType result,
4503 std::function<uint32_t(uint32_t)> map)
4504 : test_body(tb), id_to_fold(id), expected_result(result), id_map(map) {}
4505
4506 std::string test_body;
4507 uint32_t id_to_fold;
4508 ResultType expected_result;
4509 std::function<uint32_t(uint32_t)> id_map;
4510 };
4511
4512 using IntegerInstructionFoldingTestWithMap =
4513 ::testing::TestWithParam<InstructionFoldingCaseWithMap<uint32_t>>;
4514
4515 TEST_P(IntegerInstructionFoldingTestWithMap, Case) {
4516 const auto& tc = GetParam();
4517
4518 std::unique_ptr<IRContext> context;
4519 Instruction* inst;
4520 std::tie(context, inst) =
4521 GetInstructionToFold(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_5);
4522
4523 inst = context->get_instruction_folder().FoldInstructionToConstant(inst,
4524 tc.id_map);
4525 EXPECT_NE(inst, nullptr);
4526
4527 CheckForExpectedScalarConstant(inst, tc.expected_result,
4528 [](const analysis::Constant* c) {
4529 return c->AsIntConstant()->GetU32BitValue();
4530 });
4531 }
4532 // clang-format off
4533
4534 INSTANTIATE_TEST_SUITE_P(TestCase, IntegerInstructionFoldingTestWithMap,
4535 ::testing::Values(
4536 // Test case 0: fold %3 = 0; %3 * n
4537 InstructionFoldingCaseWithMap<uint32_t>(
4538 Header() + "%main = OpFunction %void None %void_func\n" +
4539 "%main_lab = OpLabel\n" +
4540 "%n = OpVariable %_ptr_int Function\n" +
4541 "%load = OpLoad %int %n\n" +
4542 "%3 = OpCopyObject %int %int_0\n"
4543 "%2 = OpIMul %int %3 %load\n" +
4544 "OpReturn\n" +
4545 "OpFunctionEnd",
4546 2, 0, [](uint32_t id) {return (id == 3 ? INT_0_ID : id);})
4547 ));
4548 // clang-format on
4549
4550 using BooleanInstructionFoldingTestWithMap =
4551 ::testing::TestWithParam<InstructionFoldingCaseWithMap<bool>>;
4552
4553 TEST_P(BooleanInstructionFoldingTestWithMap, Case) {
4554 const auto& tc = GetParam();
4555
4556 std::unique_ptr<IRContext> context;
4557 Instruction* inst;
4558 std::tie(context, inst) =
4559 GetInstructionToFold(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_5);
4560 inst = context->get_instruction_folder().FoldInstructionToConstant(inst,
4561 tc.id_map);
4562 ASSERT_NE(inst, nullptr);
4563 CheckForExpectedScalarConstant(
4564 inst, tc.expected_result,
4565 [](const analysis::Constant* c) { return c->AsBoolConstant()->value(); });
4566 }
4567
4568 // clang-format off
4569 INSTANTIATE_TEST_SUITE_P(TestCase, BooleanInstructionFoldingTestWithMap,
4570 ::testing::Values(
4571 // Test case 0: fold %3 = true; %3 || n
4572 InstructionFoldingCaseWithMap<bool>(
4573 Header() + "%main = OpFunction %void None %void_func\n" +
4574 "%main_lab = OpLabel\n" +
4575 "%n = OpVariable %_ptr_bool Function\n" +
4576 "%load = OpLoad %bool %n\n" +
4577 "%3 = OpCopyObject %bool %true\n" +
4578 "%2 = OpLogicalOr %bool %3 %load\n" +
4579 "OpReturn\n" +
4580 "OpFunctionEnd",
4581 2, true, [](uint32_t id) {return (id == 3 ? TRUE_ID : id);})
4582 ));
4583 // clang-format on
4584
4585 using GeneralInstructionFoldingTest =
4586 ::testing::TestWithParam<InstructionFoldingCase<uint32_t>>;
4587
4588 TEST_P(GeneralInstructionFoldingTest, Case) {
4589 const auto& tc = GetParam();
4590
4591 std::unique_ptr<IRContext> context;
4592 Instruction* inst;
4593 std::tie(context, inst) =
4594 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
4595
4596 EXPECT_TRUE((inst == nullptr) == (tc.expected_result == 0));
4597 if (inst != nullptr) {
4598 EXPECT_EQ(inst->opcode(), spv::Op::OpCopyObject);
4599 EXPECT_EQ(inst->GetSingleWordInOperand(0), tc.expected_result);
4600 }
4601 }
4602
4603 // clang-format off
4604 INSTANTIATE_TEST_SUITE_P(IntegerArithmeticTestCases, GeneralInstructionFoldingTest,
4605 ::testing::Values(
4606 // Test case 0: Don't fold n * m
4607 InstructionFoldingCase<uint32_t>(
4608 Header() + "%main = OpFunction %void None %void_func\n" +
4609 "%main_lab = OpLabel\n" +
4610 "%n = OpVariable %_ptr_int Function\n" +
4611 "%m = OpVariable %_ptr_int Function\n" +
4612 "%load_n = OpLoad %int %n\n" +
4613 "%load_m = OpLoad %int %m\n" +
4614 "%2 = OpIMul %int %load_n %load_m\n" +
4615 "OpReturn\n" +
4616 "OpFunctionEnd",
4617 2, 0),
4618 // Test case 1: Don't fold n / m (unsigned)
4619 InstructionFoldingCase<uint32_t>(
4620 Header() + "%main = OpFunction %void None %void_func\n" +
4621 "%main_lab = OpLabel\n" +
4622 "%n = OpVariable %_ptr_uint Function\n" +
4623 "%m = OpVariable %_ptr_uint Function\n" +
4624 "%load_n = OpLoad %uint %n\n" +
4625 "%load_m = OpLoad %uint %m\n" +
4626 "%2 = OpUDiv %uint %load_n %load_m\n" +
4627 "OpReturn\n" +
4628 "OpFunctionEnd",
4629 2, 0),
4630 // Test case 2: Don't fold n / m (signed)
4631 InstructionFoldingCase<uint32_t>(
4632 Header() + "%main = OpFunction %void None %void_func\n" +
4633 "%main_lab = OpLabel\n" +
4634 "%n = OpVariable %_ptr_int Function\n" +
4635 "%m = OpVariable %_ptr_int Function\n" +
4636 "%load_n = OpLoad %int %n\n" +
4637 "%load_m = OpLoad %int %m\n" +
4638 "%2 = OpSDiv %int %load_n %load_m\n" +
4639 "OpReturn\n" +
4640 "OpFunctionEnd",
4641 2, 0),
4642 // Test case 3: Don't fold n remainder m
4643 InstructionFoldingCase<uint32_t>(
4644 Header() + "%main = OpFunction %void None %void_func\n" +
4645 "%main_lab = OpLabel\n" +
4646 "%n = OpVariable %_ptr_int Function\n" +
4647 "%m = OpVariable %_ptr_int Function\n" +
4648 "%load_n = OpLoad %int %n\n" +
4649 "%load_m = OpLoad %int %m\n" +
4650 "%2 = OpSRem %int %load_n %load_m\n" +
4651 "OpReturn\n" +
4652 "OpFunctionEnd",
4653 2, 0),
4654 // Test case 4: Don't fold n % m (signed)
4655 InstructionFoldingCase<uint32_t>(
4656 Header() + "%main = OpFunction %void None %void_func\n" +
4657 "%main_lab = OpLabel\n" +
4658 "%n = OpVariable %_ptr_int Function\n" +
4659 "%m = OpVariable %_ptr_int Function\n" +
4660 "%load_n = OpLoad %int %n\n" +
4661 "%load_m = OpLoad %int %m\n" +
4662 "%2 = OpSMod %int %load_n %load_m\n" +
4663 "OpReturn\n" +
4664 "OpFunctionEnd",
4665 2, 0),
4666 // Test case 5: Don't fold n % m (unsigned)
4667 InstructionFoldingCase<uint32_t>(
4668 Header() + "%main = OpFunction %void None %void_func\n" +
4669 "%main_lab = OpLabel\n" +
4670 "%n = OpVariable %_ptr_uint Function\n" +
4671 "%m = OpVariable %_ptr_uint Function\n" +
4672 "%load_n = OpLoad %uint %n\n" +
4673 "%load_m = OpLoad %uint %m\n" +
4674 "%2 = OpUMod %int %load_n %load_m\n" +
4675 "OpReturn\n" +
4676 "OpFunctionEnd",
4677 2, 0),
4678 // Test case 6: Don't fold n << m
4679 InstructionFoldingCase<uint32_t>(
4680 Header() + "%main = OpFunction %void None %void_func\n" +
4681 "%main_lab = OpLabel\n" +
4682 "%n = OpVariable %_ptr_uint Function\n" +
4683 "%m = OpVariable %_ptr_uint Function\n" +
4684 "%load_n = OpLoad %uint %n\n" +
4685 "%load_m = OpLoad %uint %m\n" +
4686 "%2 = OpShiftRightLogical %int %load_n %load_m\n" +
4687 "OpReturn\n" +
4688 "OpFunctionEnd",
4689 2, 0),
4690 // Test case 7: Don't fold n >> m
4691 InstructionFoldingCase<uint32_t>(
4692 Header() + "%main = OpFunction %void None %void_func\n" +
4693 "%main_lab = OpLabel\n" +
4694 "%n = OpVariable %_ptr_uint Function\n" +
4695 "%m = OpVariable %_ptr_uint Function\n" +
4696 "%load_n = OpLoad %uint %n\n" +
4697 "%load_m = OpLoad %uint %m\n" +
4698 "%2 = OpShiftLeftLogical %int %load_n %load_m\n" +
4699 "OpReturn\n" +
4700 "OpFunctionEnd",
4701 2, 0),
4702 // Test case 8: Don't fold n | m
4703 InstructionFoldingCase<uint32_t>(
4704 Header() + "%main = OpFunction %void None %void_func\n" +
4705 "%main_lab = OpLabel\n" +
4706 "%n = OpVariable %_ptr_uint Function\n" +
4707 "%m = OpVariable %_ptr_uint Function\n" +
4708 "%load_n = OpLoad %uint %n\n" +
4709 "%load_m = OpLoad %uint %m\n" +
4710 "%2 = OpBitwiseOr %int %load_n %load_m\n" +
4711 "OpReturn\n" +
4712 "OpFunctionEnd",
4713 2, 0),
4714 // Test case 9: Don't fold n & m
4715 InstructionFoldingCase<uint32_t>(
4716 Header() + "%main = OpFunction %void None %void_func\n" +
4717 "%main_lab = OpLabel\n" +
4718 "%n = OpVariable %_ptr_uint Function\n" +
4719 "%m = OpVariable %_ptr_uint Function\n" +
4720 "%load_n = OpLoad %uint %n\n" +
4721 "%load_m = OpLoad %uint %m\n" +
4722 "%2 = OpBitwiseAnd %int %load_n %load_m\n" +
4723 "OpReturn\n" +
4724 "OpFunctionEnd",
4725 2, 0),
4726 // Test case 10: Don't fold n < m (unsigned)
4727 InstructionFoldingCase<uint32_t>(
4728 Header() + "%main = OpFunction %void None %void_func\n" +
4729 "%main_lab = OpLabel\n" +
4730 "%n = OpVariable %_ptr_uint Function\n" +
4731 "%m = OpVariable %_ptr_uint Function\n" +
4732 "%load_n = OpLoad %uint %n\n" +
4733 "%load_m = OpLoad %uint %m\n" +
4734 "%2 = OpULessThan %bool %load_n %load_m\n" +
4735 "OpReturn\n" +
4736 "OpFunctionEnd",
4737 2, 0),
4738 // Test case 11: Don't fold n > m (unsigned)
4739 InstructionFoldingCase<uint32_t>(
4740 Header() + "%main = OpFunction %void None %void_func\n" +
4741 "%main_lab = OpLabel\n" +
4742 "%n = OpVariable %_ptr_uint Function\n" +
4743 "%m = OpVariable %_ptr_uint Function\n" +
4744 "%load_n = OpLoad %uint %n\n" +
4745 "%load_m = OpLoad %uint %m\n" +
4746 "%2 = OpUGreaterThan %bool %load_n %load_m\n" +
4747 "OpReturn\n" +
4748 "OpFunctionEnd",
4749 2, 0),
4750 // Test case 12: Don't fold n <= m (unsigned)
4751 InstructionFoldingCase<uint32_t>(
4752 Header() + "%main = OpFunction %void None %void_func\n" +
4753 "%main_lab = OpLabel\n" +
4754 "%n = OpVariable %_ptr_uint Function\n" +
4755 "%m = OpVariable %_ptr_uint Function\n" +
4756 "%load_n = OpLoad %uint %n\n" +
4757 "%load_m = OpLoad %uint %m\n" +
4758 "%2 = OpULessThanEqual %bool %load_n %load_m\n" +
4759 "OpReturn\n" +
4760 "OpFunctionEnd",
4761 2, 0),
4762 // Test case 13: Don't fold n >= m (unsigned)
4763 InstructionFoldingCase<uint32_t>(
4764 Header() + "%main = OpFunction %void None %void_func\n" +
4765 "%main_lab = OpLabel\n" +
4766 "%n = OpVariable %_ptr_uint Function\n" +
4767 "%m = OpVariable %_ptr_uint Function\n" +
4768 "%load_n = OpLoad %uint %n\n" +
4769 "%load_m = OpLoad %uint %m\n" +
4770 "%2 = OpUGreaterThanEqual %bool %load_n %load_m\n" +
4771 "OpReturn\n" +
4772 "OpFunctionEnd",
4773 2, 0),
4774 // Test case 14: Don't fold n < m (signed)
4775 InstructionFoldingCase<uint32_t>(
4776 Header() + "%main = OpFunction %void None %void_func\n" +
4777 "%main_lab = OpLabel\n" +
4778 "%n = OpVariable %_ptr_int Function\n" +
4779 "%m = OpVariable %_ptr_int Function\n" +
4780 "%load_n = OpLoad %int %n\n" +
4781 "%load_m = OpLoad %int %m\n" +
4782 "%2 = OpULessThan %bool %load_n %load_m\n" +
4783 "OpReturn\n" +
4784 "OpFunctionEnd",
4785 2, 0),
4786 // Test case 15: Don't fold n > m (signed)
4787 InstructionFoldingCase<uint32_t>(
4788 Header() + "%main = OpFunction %void None %void_func\n" +
4789 "%main_lab = OpLabel\n" +
4790 "%n = OpVariable %_ptr_int Function\n" +
4791 "%m = OpVariable %_ptr_int Function\n" +
4792 "%load_n = OpLoad %int %n\n" +
4793 "%load_m = OpLoad %int %m\n" +
4794 "%2 = OpUGreaterThan %bool %load_n %load_m\n" +
4795 "OpReturn\n" +
4796 "OpFunctionEnd",
4797 2, 0),
4798 // Test case 16: Don't fold n <= m (signed)
4799 InstructionFoldingCase<uint32_t>(
4800 Header() + "%main = OpFunction %void None %void_func\n" +
4801 "%main_lab = OpLabel\n" +
4802 "%n = OpVariable %_ptr_int Function\n" +
4803 "%m = OpVariable %_ptr_int Function\n" +
4804 "%load_n = OpLoad %int %n\n" +
4805 "%load_m = OpLoad %int %m\n" +
4806 "%2 = OpULessThanEqual %bool %load_n %load_m\n" +
4807 "OpReturn\n" +
4808 "OpFunctionEnd",
4809 2, 0),
4810 // Test case 17: Don't fold n >= m (signed)
4811 InstructionFoldingCase<uint32_t>(
4812 Header() + "%main = OpFunction %void None %void_func\n" +
4813 "%main_lab = OpLabel\n" +
4814 "%n = OpVariable %_ptr_int Function\n" +
4815 "%m = OpVariable %_ptr_int Function\n" +
4816 "%load_n = OpLoad %int %n\n" +
4817 "%load_m = OpLoad %int %m\n" +
4818 "%2 = OpUGreaterThanEqual %bool %load_n %load_m\n" +
4819 "OpReturn\n" +
4820 "OpFunctionEnd",
4821 2, 0),
4822 // Test case 18: Don't fold n || m
4823 InstructionFoldingCase<uint32_t>(
4824 Header() + "%main = OpFunction %void None %void_func\n" +
4825 "%main_lab = OpLabel\n" +
4826 "%n = OpVariable %_ptr_bool Function\n" +
4827 "%m = OpVariable %_ptr_bool Function\n" +
4828 "%load_n = OpLoad %bool %n\n" +
4829 "%load_m = OpLoad %bool %m\n" +
4830 "%2 = OpLogicalOr %bool %load_n %load_m\n" +
4831 "OpReturn\n" +
4832 "OpFunctionEnd",
4833 2, 0),
4834 // Test case 19: Don't fold n && m
4835 InstructionFoldingCase<uint32_t>(
4836 Header() + "%main = OpFunction %void None %void_func\n" +
4837 "%main_lab = OpLabel\n" +
4838 "%n = OpVariable %_ptr_bool Function\n" +
4839 "%m = OpVariable %_ptr_bool Function\n" +
4840 "%load_n = OpLoad %bool %n\n" +
4841 "%load_m = OpLoad %bool %m\n" +
4842 "%2 = OpLogicalAnd %bool %load_n %load_m\n" +
4843 "OpReturn\n" +
4844 "OpFunctionEnd",
4845 2, 0),
4846 // Test case 20: Don't fold n * 3
4847 InstructionFoldingCase<uint32_t>(
4848 Header() + "%main = OpFunction %void None %void_func\n" +
4849 "%main_lab = OpLabel\n" +
4850 "%n = OpVariable %_ptr_int Function\n" +
4851 "%load_n = OpLoad %int %n\n" +
4852 "%2 = OpIMul %int %load_n %int_3\n" +
4853 "OpReturn\n" +
4854 "OpFunctionEnd",
4855 2, 0),
4856 // Test case 21: Don't fold n / 3 (unsigned)
4857 InstructionFoldingCase<uint32_t>(
4858 Header() + "%main = OpFunction %void None %void_func\n" +
4859 "%main_lab = OpLabel\n" +
4860 "%n = OpVariable %_ptr_uint Function\n" +
4861 "%load_n = OpLoad %uint %n\n" +
4862 "%2 = OpUDiv %uint %load_n %uint_3\n" +
4863 "OpReturn\n" +
4864 "OpFunctionEnd",
4865 2, 0),
4866 // Test case 22: Don't fold n / 3 (signed)
4867 InstructionFoldingCase<uint32_t>(
4868 Header() + "%main = OpFunction %void None %void_func\n" +
4869 "%main_lab = OpLabel\n" +
4870 "%n = OpVariable %_ptr_int Function\n" +
4871 "%load_n = OpLoad %int %n\n" +
4872 "%2 = OpSDiv %int %load_n %int_3\n" +
4873 "OpReturn\n" +
4874 "OpFunctionEnd",
4875 2, 0),
4876 // Test case 23: Don't fold n remainder 3
4877 InstructionFoldingCase<uint32_t>(
4878 Header() + "%main = OpFunction %void None %void_func\n" +
4879 "%main_lab = OpLabel\n" +
4880 "%n = OpVariable %_ptr_int Function\n" +
4881 "%load_n = OpLoad %int %n\n" +
4882 "%2 = OpSRem %int %load_n %int_3\n" +
4883 "OpReturn\n" +
4884 "OpFunctionEnd",
4885 2, 0),
4886 // Test case 24: Don't fold n % 3 (signed)
4887 InstructionFoldingCase<uint32_t>(
4888 Header() + "%main = OpFunction %void None %void_func\n" +
4889 "%main_lab = OpLabel\n" +
4890 "%n = OpVariable %_ptr_int Function\n" +
4891 "%load_n = OpLoad %int %n\n" +
4892 "%2 = OpSMod %int %load_n %int_3\n" +
4893 "OpReturn\n" +
4894 "OpFunctionEnd",
4895 2, 0),
4896 // Test case 25: Don't fold n % 3 (unsigned)
4897 InstructionFoldingCase<uint32_t>(
4898 Header() + "%main = OpFunction %void None %void_func\n" +
4899 "%main_lab = OpLabel\n" +
4900 "%n = OpVariable %_ptr_uint Function\n" +
4901 "%load_n = OpLoad %uint %n\n" +
4902 "%2 = OpUMod %int %load_n %int_3\n" +
4903 "OpReturn\n" +
4904 "OpFunctionEnd",
4905 2, 0),
4906 // Test case 26: Don't fold n << 3
4907 InstructionFoldingCase<uint32_t>(
4908 Header() + "%main = OpFunction %void None %void_func\n" +
4909 "%main_lab = OpLabel\n" +
4910 "%n = OpVariable %_ptr_uint Function\n" +
4911 "%load_n = OpLoad %uint %n\n" +
4912 "%2 = OpShiftRightLogical %int %load_n %int_3\n" +
4913 "OpReturn\n" +
4914 "OpFunctionEnd",
4915 2, 0),
4916 // Test case 27: Don't fold n >> 3
4917 InstructionFoldingCase<uint32_t>(
4918 Header() + "%main = OpFunction %void None %void_func\n" +
4919 "%main_lab = OpLabel\n" +
4920 "%n = OpVariable %_ptr_uint Function\n" +
4921 "%load_n = OpLoad %uint %n\n" +
4922 "%2 = OpShiftLeftLogical %int %load_n %int_3\n" +
4923 "OpReturn\n" +
4924 "OpFunctionEnd",
4925 2, 0),
4926 // Test case 28: Don't fold n | 3
4927 InstructionFoldingCase<uint32_t>(
4928 Header() + "%main = OpFunction %void None %void_func\n" +
4929 "%main_lab = OpLabel\n" +
4930 "%n = OpVariable %_ptr_uint Function\n" +
4931 "%load_n = OpLoad %uint %n\n" +
4932 "%2 = OpBitwiseOr %int %load_n %int_3\n" +
4933 "OpReturn\n" +
4934 "OpFunctionEnd",
4935 2, 0),
4936 // Test case 29: Don't fold n & 3
4937 InstructionFoldingCase<uint32_t>(
4938 Header() + "%main = OpFunction %void None %void_func\n" +
4939 "%main_lab = OpLabel\n" +
4940 "%n = OpVariable %_ptr_uint Function\n" +
4941 "%load_n = OpLoad %uint %n\n" +
4942 "%2 = OpBitwiseAnd %uint %load_n %uint_3\n" +
4943 "OpReturn\n" +
4944 "OpFunctionEnd",
4945 2, 0),
4946 // Test case 30: Don't fold n < 3 (unsigned)
4947 InstructionFoldingCase<uint32_t>(
4948 Header() + "%main = OpFunction %void None %void_func\n" +
4949 "%main_lab = OpLabel\n" +
4950 "%n = OpVariable %_ptr_uint Function\n" +
4951 "%load_n = OpLoad %uint %n\n" +
4952 "%2 = OpULessThan %bool %load_n %uint_3\n" +
4953 "OpReturn\n" +
4954 "OpFunctionEnd",
4955 2, 0),
4956 // Test case 31: Don't fold n > 3 (unsigned)
4957 InstructionFoldingCase<uint32_t>(
4958 Header() + "%main = OpFunction %void None %void_func\n" +
4959 "%main_lab = OpLabel\n" +
4960 "%n = OpVariable %_ptr_uint Function\n" +
4961 "%load_n = OpLoad %uint %n\n" +
4962 "%2 = OpUGreaterThan %bool %load_n %uint_3\n" +
4963 "OpReturn\n" +
4964 "OpFunctionEnd",
4965 2, 0),
4966 // Test case 32: Don't fold n <= 3 (unsigned)
4967 InstructionFoldingCase<uint32_t>(
4968 Header() + "%main = OpFunction %void None %void_func\n" +
4969 "%main_lab = OpLabel\n" +
4970 "%n = OpVariable %_ptr_uint Function\n" +
4971 "%load_n = OpLoad %uint %n\n" +
4972 "%2 = OpULessThanEqual %bool %load_n %uint_3\n" +
4973 "OpReturn\n" +
4974 "OpFunctionEnd",
4975 2, 0),
4976 // Test case 33: Don't fold n >= 3 (unsigned)
4977 InstructionFoldingCase<uint32_t>(
4978 Header() + "%main = OpFunction %void None %void_func\n" +
4979 "%main_lab = OpLabel\n" +
4980 "%n = OpVariable %_ptr_uint Function\n" +
4981 "%load_n = OpLoad %uint %n\n" +
4982 "%2 = OpUGreaterThanEqual %bool %load_n %uint_3\n" +
4983 "OpReturn\n" +
4984 "OpFunctionEnd",
4985 2, 0),
4986 // Test case 34: Don't fold n < 3 (signed)
4987 InstructionFoldingCase<uint32_t>(
4988 Header() + "%main = OpFunction %void None %void_func\n" +
4989 "%main_lab = OpLabel\n" +
4990 "%n = OpVariable %_ptr_int Function\n" +
4991 "%load_n = OpLoad %int %n\n" +
4992 "%2 = OpULessThan %bool %load_n %int_3\n" +
4993 "OpReturn\n" +
4994 "OpFunctionEnd",
4995 2, 0),
4996 // Test case 35: Don't fold n > 3 (signed)
4997 InstructionFoldingCase<uint32_t>(
4998 Header() + "%main = OpFunction %void None %void_func\n" +
4999 "%main_lab = OpLabel\n" +
5000 "%n = OpVariable %_ptr_int Function\n" +
5001 "%load_n = OpLoad %int %n\n" +
5002 "%2 = OpUGreaterThan %bool %load_n %int_3\n" +
5003 "OpReturn\n" +
5004 "OpFunctionEnd",
5005 2, 0),
5006 // Test case 36: Don't fold n <= 3 (signed)
5007 InstructionFoldingCase<uint32_t>(
5008 Header() + "%main = OpFunction %void None %void_func\n" +
5009 "%main_lab = OpLabel\n" +
5010 "%n = OpVariable %_ptr_int Function\n" +
5011 "%load_n = OpLoad %int %n\n" +
5012 "%2 = OpULessThanEqual %bool %load_n %int_3\n" +
5013 "OpReturn\n" +
5014 "OpFunctionEnd",
5015 2, 0),
5016 // Test case 37: Don't fold n >= 3 (signed)
5017 InstructionFoldingCase<uint32_t>(
5018 Header() + "%main = OpFunction %void None %void_func\n" +
5019 "%main_lab = OpLabel\n" +
5020 "%n = OpVariable %_ptr_int Function\n" +
5021 "%load_n = OpLoad %int %n\n" +
5022 "%2 = OpUGreaterThanEqual %bool %load_n %int_3\n" +
5023 "OpReturn\n" +
5024 "OpFunctionEnd",
5025 2, 0),
5026 // Test case 38: fold 1*n
5027 InstructionFoldingCase<uint32_t>(
5028 Header() + "%main = OpFunction %void None %void_func\n" +
5029 "%main_lab = OpLabel\n" +
5030 "%n = OpVariable %_ptr_int Function\n" +
5031 "%3 = OpLoad %int %n\n" +
5032 "%2 = OpIMul %int %int_1 %3\n" +
5033 "OpReturn\n" +
5034 "OpFunctionEnd",
5035 2, 3),
5036 // Test case 39: fold n*1
5037 InstructionFoldingCase<uint32_t>(
5038 Header() + "%main = OpFunction %void None %void_func\n" +
5039 "%main_lab = OpLabel\n" +
5040 "%n = OpVariable %_ptr_int Function\n" +
5041 "%3 = OpLoad %int %n\n" +
5042 "%2 = OpIMul %int %3 %int_1\n" +
5043 "OpReturn\n" +
5044 "OpFunctionEnd",
5045 2, 3),
5046 // Test case 40: Don't fold comparisons of 64-bit types
5047 // (https://github.com/KhronosGroup/SPIRV-Tools/issues/3343).
5048 InstructionFoldingCase<uint32_t>(
5049 Header() + "%main = OpFunction %void None %void_func\n" +
5050 "%main_lab = OpLabel\n" +
5051 "%2 = OpSLessThan %bool %long_0 %long_2\n" +
5052 "OpReturn\n" +
5053 "OpFunctionEnd",
5054 2, 0),
5055 // Test case 41: Don't fold OpSNegate for cooperative matrices.
5056 InstructionFoldingCase<uint32_t>(
5057 Header() + "%main = OpFunction %void None %void_func\n" +
5058 "%main_lab = OpLabel\n" +
5059 "%2 = OpSNegate %int_coop_matrix %undef_int_coop_matrix\n" +
5060 "OpReturn\n" +
5061 "OpFunctionEnd",
5062 2, 0),
5063 // Test case 42: Don't fold OpIAdd for cooperative matrices.
5064 InstructionFoldingCase<uint32_t>(
5065 Header() + "%main = OpFunction %void None %void_func\n" +
5066 "%main_lab = OpLabel\n" +
5067 "%2 = OpIAdd %int_coop_matrix %undef_int_coop_matrix %undef_int_coop_matrix\n" +
5068 "OpReturn\n" +
5069 "OpFunctionEnd",
5070 2, 0),
5071 // Test case 43: Don't fold OpISub for cooperative matrices.
5072 InstructionFoldingCase<uint32_t>(
5073 Header() + "%main = OpFunction %void None %void_func\n" +
5074 "%main_lab = OpLabel\n" +
5075 "%2 = OpISub %int_coop_matrix %undef_int_coop_matrix %undef_int_coop_matrix\n" +
5076 "OpReturn\n" +
5077 "OpFunctionEnd",
5078 2, 0),
5079 // Test case 44: Don't fold OpIMul for cooperative matrices.
5080 InstructionFoldingCase<uint32_t>(
5081 Header() + "%main = OpFunction %void None %void_func\n" +
5082 "%main_lab = OpLabel\n" +
5083 "%2 = OpIMul %int_coop_matrix %undef_int_coop_matrix %undef_int_coop_matrix\n" +
5084 "OpReturn\n" +
5085 "OpFunctionEnd",
5086 2, 0),
5087 // Test case 45: Don't fold OpSDiv for cooperative matrices.
5088 InstructionFoldingCase<uint32_t>(
5089 Header() + "%main = OpFunction %void None %void_func\n" +
5090 "%main_lab = OpLabel\n" +
5091 "%2 = OpSDiv %int_coop_matrix %undef_int_coop_matrix %undef_int_coop_matrix\n" +
5092 "OpReturn\n" +
5093 "OpFunctionEnd",
5094 2, 0),
5095 // Test case 46: Don't fold OpUDiv for cooperative matrices.
5096 InstructionFoldingCase<uint32_t>(
5097 Header() + "%main = OpFunction %void None %void_func\n" +
5098 "%main_lab = OpLabel\n" +
5099 "%2 = OpUDiv %uint_coop_matrix %undef_uint_coop_matrix %undef_uint_coop_matrix\n" +
5100 "OpReturn\n" +
5101 "OpFunctionEnd",
5102 2, 0),
5103 // Test case 47: Don't fold OpMatrixTimesScalar for cooperative matrices.
5104 InstructionFoldingCase<uint32_t>(
5105 Header() + "%main = OpFunction %void None %void_func\n" +
5106 "%main_lab = OpLabel\n" +
5107 "%2 = OpMatrixTimesScalar %uint_coop_matrix %undef_uint_coop_matrix %uint_3\n" +
5108 "OpReturn\n" +
5109 "OpFunctionEnd",
5110 2, 0)
5111 ));
5112
5113 INSTANTIATE_TEST_SUITE_P(CompositeExtractFoldingTest, GeneralInstructionFoldingTest,
5114 ::testing::Values(
5115 // Test case 0: fold Insert feeding extract
5116 InstructionFoldingCase<uint32_t>(
5117 Header() + "%main = OpFunction %void None %void_func\n" +
5118 "%main_lab = OpLabel\n" +
5119 "%n = OpVariable %_ptr_int Function\n" +
5120 "%2 = OpLoad %int %n\n" +
5121 "%3 = OpCompositeInsert %v4int %2 %v4int_0_0_0_0 0\n" +
5122 "%4 = OpCompositeInsert %v4int %int_1 %3 1\n" +
5123 "%5 = OpCompositeInsert %v4int %int_1 %4 2\n" +
5124 "%6 = OpCompositeInsert %v4int %int_1 %5 3\n" +
5125 "%7 = OpCompositeExtract %int %6 0\n" +
5126 "OpReturn\n" +
5127 "OpFunctionEnd",
5128 7, 2),
5129 // Test case 1: fold Composite construct feeding extract (position 0)
5130 InstructionFoldingCase<uint32_t>(
5131 Header() + "%main = OpFunction %void None %void_func\n" +
5132 "%main_lab = OpLabel\n" +
5133 "%n = OpVariable %_ptr_int Function\n" +
5134 "%2 = OpLoad %int %n\n" +
5135 "%3 = OpCompositeConstruct %v4int %2 %int_0 %int_0 %int_0\n" +
5136 "%4 = OpCompositeExtract %int %3 0\n" +
5137 "OpReturn\n" +
5138 "OpFunctionEnd",
5139 4, 2),
5140 // Test case 2: fold Composite construct feeding extract (position 3)
5141 InstructionFoldingCase<uint32_t>(
5142 Header() + "%main = OpFunction %void None %void_func\n" +
5143 "%main_lab = OpLabel\n" +
5144 "%n = OpVariable %_ptr_int Function\n" +
5145 "%2 = OpLoad %int %n\n" +
5146 "%3 = OpCompositeConstruct %v4int %2 %int_0 %int_0 %100\n" +
5147 "%4 = OpCompositeExtract %int %3 3\n" +
5148 "OpReturn\n" +
5149 "OpFunctionEnd",
5150 4, INT_0_ID),
5151 // Test case 3: fold Composite construct with vectors feeding extract (scalar element)
5152 InstructionFoldingCase<uint32_t>(
5153 Header() + "%main = OpFunction %void None %void_func\n" +
5154 "%main_lab = OpLabel\n" +
5155 "%n = OpVariable %_ptr_int Function\n" +
5156 "%2 = OpLoad %int %n\n" +
5157 "%3 = OpCompositeConstruct %v2int %2 %int_0\n" +
5158 "%4 = OpCompositeConstruct %v4int %3 %int_0 %100\n" +
5159 "%5 = OpCompositeExtract %int %4 3\n" +
5160 "OpReturn\n" +
5161 "OpFunctionEnd",
5162 5, INT_0_ID),
5163 // Test case 4: fold Composite construct with vectors feeding extract (start of vector element)
5164 InstructionFoldingCase<uint32_t>(
5165 Header() + "%main = OpFunction %void None %void_func\n" +
5166 "%main_lab = OpLabel\n" +
5167 "%n = OpVariable %_ptr_int Function\n" +
5168 "%2 = OpLoad %int %n\n" +
5169 "%3 = OpCompositeConstruct %v2int %2 %int_0\n" +
5170 "%4 = OpCompositeConstruct %v4int %3 %int_0 %100\n" +
5171 "%5 = OpCompositeExtract %int %4 0\n" +
5172 "OpReturn\n" +
5173 "OpFunctionEnd",
5174 5, 2),
5175 // Test case 5: fold Composite construct with vectors feeding extract (middle of vector element)
5176 InstructionFoldingCase<uint32_t>(
5177 Header() + "%main = OpFunction %void None %void_func\n" +
5178 "%main_lab = OpLabel\n" +
5179 "%n = OpVariable %_ptr_int Function\n" +
5180 "%2 = OpLoad %int %n\n" +
5181 "%3 = OpCompositeConstruct %v2int %int_0 %2\n" +
5182 "%4 = OpCompositeConstruct %v4int %3 %int_0 %100\n" +
5183 "%5 = OpCompositeExtract %int %4 1\n" +
5184 "OpReturn\n" +
5185 "OpFunctionEnd",
5186 5, 2),
5187 // Test case 6: fold Composite construct with multiple indices.
5188 InstructionFoldingCase<uint32_t>(
5189 Header() + "%main = OpFunction %void None %void_func\n" +
5190 "%main_lab = OpLabel\n" +
5191 "%n = OpVariable %_ptr_int Function\n" +
5192 "%2 = OpLoad %int %n\n" +
5193 "%3 = OpCompositeConstruct %v2int %int_0 %2\n" +
5194 "%4 = OpCompositeConstruct %struct_v2int_int_int %3 %int_0 %100\n" +
5195 "%5 = OpCompositeExtract %int %4 0 1\n" +
5196 "OpReturn\n" +
5197 "OpFunctionEnd",
5198 5, 2),
5199 // Test case 7: fold constant extract.
5200 InstructionFoldingCase<uint32_t>(
5201 Header() + "%main = OpFunction %void None %void_func\n" +
5202 "%main_lab = OpLabel\n" +
5203 "%2 = OpCompositeExtract %int %102 1\n" +
5204 "OpReturn\n" +
5205 "OpFunctionEnd",
5206 2, INT_7_ID),
5207 // Test case 8: constant struct has OpUndef
5208 InstructionFoldingCase<uint32_t>(
5209 Header() + "%main = OpFunction %void None %void_func\n" +
5210 "%main_lab = OpLabel\n" +
5211 "%2 = OpCompositeExtract %int %struct_undef_0_0 0 1\n" +
5212 "OpReturn\n" +
5213 "OpFunctionEnd",
5214 2, 0),
5215 // Test case 9: Extracting a member of element inserted via Insert
5216 InstructionFoldingCase<uint32_t>(
5217 Header() + "%main = OpFunction %void None %void_func\n" +
5218 "%main_lab = OpLabel\n" +
5219 "%n = OpVariable %_ptr_struct_v2int_int_int Function\n" +
5220 "%2 = OpLoad %struct_v2int_int_int %n\n" +
5221 "%3 = OpCompositeInsert %struct_v2int_int_int %102 %2 0\n" +
5222 "%4 = OpCompositeExtract %int %3 0 1\n" +
5223 "OpReturn\n" +
5224 "OpFunctionEnd",
5225 4, 103),
5226 // Test case 10: Extracting a element that is partially changed by Insert. (Don't fold)
5227 InstructionFoldingCase<uint32_t>(
5228 Header() + "%main = OpFunction %void None %void_func\n" +
5229 "%main_lab = OpLabel\n" +
5230 "%n = OpVariable %_ptr_struct_v2int_int_int Function\n" +
5231 "%2 = OpLoad %struct_v2int_int_int %n\n" +
5232 "%3 = OpCompositeInsert %struct_v2int_int_int %int_0 %2 0 1\n" +
5233 "%4 = OpCompositeExtract %v2int %3 0\n" +
5234 "OpReturn\n" +
5235 "OpFunctionEnd",
5236 4, 0),
5237 // Test case 11: Extracting from result of vector shuffle (first input)
5238 InstructionFoldingCase<uint32_t>(
5239 Header() + "%main = OpFunction %void None %void_func\n" +
5240 "%main_lab = OpLabel\n" +
5241 "%n = OpVariable %_ptr_v2int Function\n" +
5242 "%2 = OpLoad %v2int %n\n" +
5243 "%3 = OpVectorShuffle %v2int %102 %2 3 0\n" +
5244 "%4 = OpCompositeExtract %int %3 1\n" +
5245 "OpReturn\n" +
5246 "OpFunctionEnd",
5247 4, INT_7_ID),
5248 // Test case 12: Extracting from result of vector shuffle (second input)
5249 InstructionFoldingCase<uint32_t>(
5250 Header() + "%main = OpFunction %void None %void_func\n" +
5251 "%main_lab = OpLabel\n" +
5252 "%n = OpVariable %_ptr_v2int Function\n" +
5253 "%2 = OpLoad %v2int %n\n" +
5254 "%3 = OpVectorShuffle %v2int %2 %102 2 0\n" +
5255 "%4 = OpCompositeExtract %int %3 0\n" +
5256 "OpReturn\n" +
5257 "OpFunctionEnd",
5258 4, INT_7_ID),
5259 // Test case 13: https://github.com/KhronosGroup/SPIRV-Tools/issues/2608
5260 // Out of bounds access. Do not fold.
5261 InstructionFoldingCase<uint32_t>(
5262 Header() + "%main = OpFunction %void None %void_func\n" +
5263 "%main_lab = OpLabel\n" +
5264 "%2 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1\n" +
5265 "%3 = OpCompositeExtract %float %2 4\n" +
5266 "OpReturn\n" +
5267 "OpFunctionEnd",
5268 3, 0),
5269 // Test case 14: https://github.com/KhronosGroup/SPIRV-Tools/issues/3631
5270 // Extract the component right after the vector constituent.
5271 InstructionFoldingCase<uint32_t>(
5272 Header() + "%main = OpFunction %void None %void_func\n" +
5273 "%main_lab = OpLabel\n" +
5274 "%2 = OpCompositeConstruct %v2int %int_0 %int_0\n" +
5275 "%3 = OpCompositeConstruct %v4int %2 %100 %int_0\n" +
5276 "%4 = OpCompositeExtract %int %3 2\n" +
5277 "OpReturn\n" +
5278 "OpFunctionEnd",
5279 4, INT_0_ID),
5280 // Test case 15:
5281 // Don't fold extract fed by construct with vector result if the index is
5282 // past the last element.
5283 InstructionFoldingCase<uint32_t>(
5284 Header() + "%main = OpFunction %void None %void_func\n" +
5285 "%main_lab = OpLabel\n" +
5286 "%2 = OpCompositeConstruct %v2int %int_0 %int_0\n" +
5287 "%3 = OpCompositeConstruct %v4int %2 %100 %int_0\n" +
5288 "%4 = OpCompositeExtract %int %3 4\n" +
5289 "OpReturn\n" +
5290 "OpFunctionEnd",
5291 4, 0)
5292 ));
5293
5294 INSTANTIATE_TEST_SUITE_P(CompositeConstructFoldingTest, GeneralInstructionFoldingTest,
5295 ::testing::Values(
5296 // Test case 0: fold Extracts feeding construct
5297 InstructionFoldingCase<uint32_t>(
5298 Header() + "%main = OpFunction %void None %void_func\n" +
5299 "%main_lab = OpLabel\n" +
5300 "%2 = OpCopyObject %v4int %v4int_0_0_0_0\n" +
5301 "%3 = OpCompositeExtract %int %2 0\n" +
5302 "%4 = OpCompositeExtract %int %2 1\n" +
5303 "%5 = OpCompositeExtract %int %2 2\n" +
5304 "%6 = OpCompositeExtract %int %2 3\n" +
5305 "%7 = OpCompositeConstruct %v4int %3 %4 %5 %6\n" +
5306 "OpReturn\n" +
5307 "OpFunctionEnd",
5308 7, 2),
5309 // Test case 1: Don't fold Extracts feeding construct (Different source)
5310 InstructionFoldingCase<uint32_t>(
5311 Header() + "%main = OpFunction %void None %void_func\n" +
5312 "%main_lab = OpLabel\n" +
5313 "%2 = OpCopyObject %v4int %v4int_0_0_0_0\n" +
5314 "%3 = OpCompositeExtract %int %2 0\n" +
5315 "%4 = OpCompositeExtract %int %2 1\n" +
5316 "%5 = OpCompositeExtract %int %2 2\n" +
5317 "%6 = OpCompositeExtract %int %v4int_0_0_0_0 3\n" +
5318 "%7 = OpCompositeConstruct %v4int %3 %4 %5 %6\n" +
5319 "OpReturn\n" +
5320 "OpFunctionEnd",
5321 7, 0),
5322 // Test case 2: Don't fold Extracts feeding construct (bad indices)
5323 InstructionFoldingCase<uint32_t>(
5324 Header() + "%main = OpFunction %void None %void_func\n" +
5325 "%main_lab = OpLabel\n" +
5326 "%2 = OpCopyObject %v4int %v4int_0_0_0_0\n" +
5327 "%3 = OpCompositeExtract %int %2 0\n" +
5328 "%4 = OpCompositeExtract %int %2 0\n" +
5329 "%5 = OpCompositeExtract %int %2 2\n" +
5330 "%6 = OpCompositeExtract %int %2 3\n" +
5331 "%7 = OpCompositeConstruct %v4int %3 %4 %5 %6\n" +
5332 "OpReturn\n" +
5333 "OpFunctionEnd",
5334 7, 0),
5335 // Test case 3: Don't fold Extracts feeding construct (different type)
5336 InstructionFoldingCase<uint32_t>(
5337 Header() + "%main = OpFunction %void None %void_func\n" +
5338 "%main_lab = OpLabel\n" +
5339 "%2 = OpCopyObject %struct_v2int_int_int %struct_v2int_int_int_null\n" +
5340 "%3 = OpCompositeExtract %v2int %2 0\n" +
5341 "%4 = OpCompositeExtract %int %2 1\n" +
5342 "%5 = OpCompositeExtract %int %2 2\n" +
5343 "%7 = OpCompositeConstruct %v4int %3 %4 %5\n" +
5344 "OpReturn\n" +
5345 "OpFunctionEnd",
5346 7, 0),
5347 // Test case 4: Fold construct with constants to constant.
5348 InstructionFoldingCase<uint32_t>(
5349 Header() + "%main = OpFunction %void None %void_func\n" +
5350 "%main_lab = OpLabel\n" +
5351 "%2 = OpCompositeConstruct %v2int %103 %103\n" +
5352 "OpReturn\n" +
5353 "OpFunctionEnd",
5354 2, VEC2_0_ID),
5355 // Test case 5: Don't segfault when trying to fold an OpCompositeConstruct
5356 // for an empty struct, and we reached the id limit.
5357 InstructionFoldingCase<uint32_t>(
5358 Header() + "%empty_struct = OpTypeStruct\n" +
5359 "%main = OpFunction %void None %void_func\n" +
5360 "%main_lab = OpLabel\n" +
5361 "%4194303 = OpCompositeConstruct %empty_struct\n" +
5362 "OpReturn\n" +
5363 "OpFunctionEnd",
5364 4194303, 0)
5365 ));
5366
5367 INSTANTIATE_TEST_SUITE_P(PhiFoldingTest, GeneralInstructionFoldingTest,
5368 ::testing::Values(
5369 // Test case 0: Fold phi with the same values for all edges.
5370 InstructionFoldingCase<uint32_t>(
5371 Header() + "%main = OpFunction %void None %void_func\n" +
5372 "%main_lab = OpLabel\n" +
5373 " OpBranchConditional %true %l1 %l2\n" +
5374 "%l1 = OpLabel\n" +
5375 " OpBranch %merge_lab\n" +
5376 "%l2 = OpLabel\n" +
5377 " OpBranch %merge_lab\n" +
5378 "%merge_lab = OpLabel\n" +
5379 "%2 = OpPhi %int %100 %l1 %100 %l2\n" +
5380 "OpReturn\n" +
5381 "OpFunctionEnd",
5382 2, INT_0_ID),
5383 // Test case 1: Fold phi in pass through loop.
5384 InstructionFoldingCase<uint32_t>(
5385 Header() + "%main = OpFunction %void None %void_func\n" +
5386 "%main_lab = OpLabel\n" +
5387 " OpBranch %l1\n" +
5388 "%l1 = OpLabel\n" +
5389 "%2 = OpPhi %int %100 %main_lab %2 %l1\n" +
5390 " OpBranchConditional %true %l1 %merge_lab\n" +
5391 "%merge_lab = OpLabel\n" +
5392 "OpReturn\n" +
5393 "OpFunctionEnd",
5394 2, INT_0_ID),
5395 // Test case 2: Don't Fold phi because of different values.
5396 InstructionFoldingCase<uint32_t>(
5397 Header() + "%main = OpFunction %void None %void_func\n" +
5398 "%main_lab = OpLabel\n" +
5399 " OpBranch %l1\n" +
5400 "%l1 = OpLabel\n" +
5401 "%2 = OpPhi %int %int_0 %main_lab %int_3 %l1\n" +
5402 " OpBranchConditional %true %l1 %merge_lab\n" +
5403 "%merge_lab = OpLabel\n" +
5404 "OpReturn\n" +
5405 "OpFunctionEnd",
5406 2, 0)
5407 ));
5408
5409 INSTANTIATE_TEST_SUITE_P(FloatRedundantFoldingTest, GeneralInstructionFoldingTest,
5410 ::testing::Values(
5411 // Test case 0: Don't fold n + 1.0
5412 InstructionFoldingCase<uint32_t>(
5413 Header() + "%main = OpFunction %void None %void_func\n" +
5414 "%main_lab = OpLabel\n" +
5415 "%n = OpVariable %_ptr_float Function\n" +
5416 "%3 = OpLoad %float %n\n" +
5417 "%2 = OpFAdd %float %3 %float_2\n" +
5418 "OpReturn\n" +
5419 "OpFunctionEnd",
5420 2, 0),
5421 // Test case 1: Don't fold n - 1.0
5422 InstructionFoldingCase<uint32_t>(
5423 Header() + "%main = OpFunction %void None %void_func\n" +
5424 "%main_lab = OpLabel\n" +
5425 "%n = OpVariable %_ptr_float Function\n" +
5426 "%3 = OpLoad %float %n\n" +
5427 "%2 = OpFSub %float %3 %float_2\n" +
5428 "OpReturn\n" +
5429 "OpFunctionEnd",
5430 2, 0),
5431 // Test case 2: Don't fold n * 2.0
5432 InstructionFoldingCase<uint32_t>(
5433 Header() + "%main = OpFunction %void None %void_func\n" +
5434 "%main_lab = OpLabel\n" +
5435 "%n = OpVariable %_ptr_float Function\n" +
5436 "%3 = OpLoad %float %n\n" +
5437 "%2 = OpFMul %float %3 %float_2\n" +
5438 "OpReturn\n" +
5439 "OpFunctionEnd",
5440 2, 0),
5441 // Test case 3: Fold n + 0.0
5442 InstructionFoldingCase<uint32_t>(
5443 Header() + "%main = OpFunction %void None %void_func\n" +
5444 "%main_lab = OpLabel\n" +
5445 "%n = OpVariable %_ptr_float Function\n" +
5446 "%3 = OpLoad %float %n\n" +
5447 "%2 = OpFAdd %float %3 %float_0\n" +
5448 "OpReturn\n" +
5449 "OpFunctionEnd",
5450 2, 3),
5451 // Test case 4: Fold 0.0 + n
5452 InstructionFoldingCase<uint32_t>(
5453 Header() + "%main = OpFunction %void None %void_func\n" +
5454 "%main_lab = OpLabel\n" +
5455 "%n = OpVariable %_ptr_float Function\n" +
5456 "%3 = OpLoad %float %n\n" +
5457 "%2 = OpFAdd %float %float_0 %3\n" +
5458 "OpReturn\n" +
5459 "OpFunctionEnd",
5460 2, 3),
5461 // Test case 5: Fold n - 0.0
5462 InstructionFoldingCase<uint32_t>(
5463 Header() + "%main = OpFunction %void None %void_func\n" +
5464 "%main_lab = OpLabel\n" +
5465 "%n = OpVariable %_ptr_float Function\n" +
5466 "%3 = OpLoad %float %n\n" +
5467 "%2 = OpFSub %float %3 %float_0\n" +
5468 "OpReturn\n" +
5469 "OpFunctionEnd",
5470 2, 3),
5471 // Test case 6: Fold n * 1.0
5472 InstructionFoldingCase<uint32_t>(
5473 Header() + "%main = OpFunction %void None %void_func\n" +
5474 "%main_lab = OpLabel\n" +
5475 "%n = OpVariable %_ptr_float Function\n" +
5476 "%3 = OpLoad %float %n\n" +
5477 "%2 = OpFMul %float %3 %float_1\n" +
5478 "OpReturn\n" +
5479 "OpFunctionEnd",
5480 2, 3),
5481 // Test case 7: Fold 1.0 * n
5482 InstructionFoldingCase<uint32_t>(
5483 Header() + "%main = OpFunction %void None %void_func\n" +
5484 "%main_lab = OpLabel\n" +
5485 "%n = OpVariable %_ptr_float Function\n" +
5486 "%3 = OpLoad %float %n\n" +
5487 "%2 = OpFMul %float %float_1 %3\n" +
5488 "OpReturn\n" +
5489 "OpFunctionEnd",
5490 2, 3),
5491 // Test case 8: Fold n / 1.0
5492 InstructionFoldingCase<uint32_t>(
5493 Header() + "%main = OpFunction %void None %void_func\n" +
5494 "%main_lab = OpLabel\n" +
5495 "%n = OpVariable %_ptr_float Function\n" +
5496 "%3 = OpLoad %float %n\n" +
5497 "%2 = OpFDiv %float %3 %float_1\n" +
5498 "OpReturn\n" +
5499 "OpFunctionEnd",
5500 2, 3),
5501 // Test case 9: Don't fold n % 1.0
5502 // If `n` is not a whole number, the answer is not 0.
5503 InstructionFoldingCase<uint32_t>(
5504 Header() + "%main = OpFunction %void None %void_func\n" +
5505 "%main_lab = OpLabel\n" +
5506 "%n = OpVariable %_ptr_float Function\n" +
5507 "%3 = OpLoad %float %n\n" +
5508 "%2 = OpFMod %float %3 %float_1\n" +
5509 "OpReturn\n" +
5510 "OpFunctionEnd",
5511 2, 0),
5512 // Test case 10: Fold n * 0.0
5513 InstructionFoldingCase<uint32_t>(
5514 Header() + "%main = OpFunction %void None %void_func\n" +
5515 "%main_lab = OpLabel\n" +
5516 "%n = OpVariable %_ptr_float Function\n" +
5517 "%3 = OpLoad %float %n\n" +
5518 "%2 = OpFMul %float %3 %104\n" +
5519 "OpReturn\n" +
5520 "OpFunctionEnd",
5521 2, FLOAT_0_ID),
5522 // Test case 11: Fold 0.0 * n
5523 InstructionFoldingCase<uint32_t>(
5524 Header() + "%main = OpFunction %void None %void_func\n" +
5525 "%main_lab = OpLabel\n" +
5526 "%n = OpVariable %_ptr_float Function\n" +
5527 "%3 = OpLoad %float %n\n" +
5528 "%2 = OpFMul %float %104 %3\n" +
5529 "OpReturn\n" +
5530 "OpFunctionEnd",
5531 2, FLOAT_0_ID),
5532 // Test case 12: Fold 0.0 / n
5533 InstructionFoldingCase<uint32_t>(
5534 Header() + "%main = OpFunction %void None %void_func\n" +
5535 "%main_lab = OpLabel\n" +
5536 "%n = OpVariable %_ptr_float Function\n" +
5537 "%3 = OpLoad %float %n\n" +
5538 "%2 = OpFDiv %float %104 %3\n" +
5539 "OpReturn\n" +
5540 "OpFunctionEnd",
5541 2, FLOAT_0_ID),
5542 // Test case 13: Fold 0.0 % n
5543 InstructionFoldingCase<uint32_t>(
5544 Header() + "%main = OpFunction %void None %void_func\n" +
5545 "%main_lab = OpLabel\n" +
5546 "%n = OpVariable %_ptr_float Function\n" +
5547 "%3 = OpLoad %float %n\n" +
5548 "%2 = OpFMod %float %104 %3\n" +
5549 "OpReturn\n" +
5550 "OpFunctionEnd",
5551 2, FLOAT_0_ID),
5552 // Test case 14: Don't fold mix(a, b, 2.0)
5553 InstructionFoldingCase<uint32_t>(
5554 Header() + "%main = OpFunction %void None %void_func\n" +
5555 "%main_lab = OpLabel\n" +
5556 "%a = OpVariable %_ptr_float Function\n" +
5557 "%b = OpVariable %_ptr_float Function\n" +
5558 "%3 = OpLoad %float %a\n" +
5559 "%4 = OpLoad %float %b\n" +
5560 "%2 = OpExtInst %float %1 FMix %3 %4 %float_2\n" +
5561 "OpReturn\n" +
5562 "OpFunctionEnd",
5563 2, 0),
5564 // Test case 15: Fold mix(a, b, 0.0)
5565 InstructionFoldingCase<uint32_t>(
5566 Header() + "%main = OpFunction %void None %void_func\n" +
5567 "%main_lab = OpLabel\n" +
5568 "%a = OpVariable %_ptr_float Function\n" +
5569 "%b = OpVariable %_ptr_float Function\n" +
5570 "%3 = OpLoad %float %a\n" +
5571 "%4 = OpLoad %float %b\n" +
5572 "%2 = OpExtInst %float %1 FMix %3 %4 %float_0\n" +
5573 "OpReturn\n" +
5574 "OpFunctionEnd",
5575 2, 3),
5576 // Test case 16: Fold mix(a, b, 1.0)
5577 InstructionFoldingCase<uint32_t>(
5578 Header() + "%main = OpFunction %void None %void_func\n" +
5579 "%main_lab = OpLabel\n" +
5580 "%a = OpVariable %_ptr_float Function\n" +
5581 "%b = OpVariable %_ptr_float Function\n" +
5582 "%3 = OpLoad %float %a\n" +
5583 "%4 = OpLoad %float %b\n" +
5584 "%2 = OpExtInst %float %1 FMix %3 %4 %float_1\n" +
5585 "OpReturn\n" +
5586 "OpFunctionEnd",
5587 2, 4),
5588 // Test case 17: Fold vector fadd with null
5589 InstructionFoldingCase<uint32_t>(
5590 Header() + "%main = OpFunction %void None %void_func\n" +
5591 "%main_lab = OpLabel\n" +
5592 "%a = OpVariable %_ptr_v2float Function\n" +
5593 "%2 = OpLoad %v2float %a\n" +
5594 "%3 = OpFAdd %v2float %2 %v2float_null\n" +
5595 "OpReturn\n" +
5596 "OpFunctionEnd",
5597 3, 2),
5598 // Test case 18: Fold vector fadd with null
5599 InstructionFoldingCase<uint32_t>(
5600 Header() + "%main = OpFunction %void None %void_func\n" +
5601 "%main_lab = OpLabel\n" +
5602 "%a = OpVariable %_ptr_v2float Function\n" +
5603 "%2 = OpLoad %v2float %a\n" +
5604 "%3 = OpFAdd %v2float %v2float_null %2\n" +
5605 "OpReturn\n" +
5606 "OpFunctionEnd",
5607 3, 2),
5608 // Test case 19: Fold vector fsub with null
5609 InstructionFoldingCase<uint32_t>(
5610 Header() + "%main = OpFunction %void None %void_func\n" +
5611 "%main_lab = OpLabel\n" +
5612 "%a = OpVariable %_ptr_v2float Function\n" +
5613 "%2 = OpLoad %v2float %a\n" +
5614 "%3 = OpFSub %v2float %2 %v2float_null\n" +
5615 "OpReturn\n" +
5616 "OpFunctionEnd",
5617 3, 2),
5618 // Test case 20: Fold 0.0(half) * n
5619 InstructionFoldingCase<uint32_t>(
5620 Header() + "%main = OpFunction %void None %void_func\n" +
5621 "%main_lab = OpLabel\n" +
5622 "%n = OpVariable %_ptr_half Function\n" +
5623 "%3 = OpLoad %half %n\n" +
5624 "%2 = OpFMul %half %108 %3\n" +
5625 "OpReturn\n" +
5626 "OpFunctionEnd",
5627 2, HALF_0_ID),
5628 // Test case 21: Don't fold 1.0(half) * n
5629 InstructionFoldingCase<uint32_t>(
5630 Header() + "%main = OpFunction %void None %void_func\n" +
5631 "%main_lab = OpLabel\n" +
5632 "%n = OpVariable %_ptr_half Function\n" +
5633 "%3 = OpLoad %half %n\n" +
5634 "%2 = OpFMul %half %half_1 %3\n" +
5635 "OpReturn\n" +
5636 "OpFunctionEnd",
5637 2, 0),
5638 // Test case 22: Don't fold 1.0 * 1.0 (half)
5639 InstructionFoldingCase<uint32_t>(
5640 Header() + "%main = OpFunction %void None %void_func\n" +
5641 "%main_lab = OpLabel\n" +
5642 "%2 = OpFMul %half %half_1 %half_1\n" +
5643 "OpReturn\n" +
5644 "OpFunctionEnd",
5645 2, 0),
5646 // Test case 23: Don't fold (0.0, 1.0) * (0.0, 1.0) (half)
5647 InstructionFoldingCase<uint32_t>(
5648 Header() + "%main = OpFunction %void None %void_func\n" +
5649 "%main_lab = OpLabel\n" +
5650 "%2 = OpFMul %v2half %half_0_1 %half_0_1\n" +
5651 "OpReturn\n" +
5652 "OpFunctionEnd",
5653 2, 0),
5654 // Test case 24: Don't fold (0.0, 1.0) dotp (0.0, 1.0) (half)
5655 InstructionFoldingCase<uint32_t>(
5656 Header() + "%main = OpFunction %void None %void_func\n" +
5657 "%main_lab = OpLabel\n" +
5658 "%2 = OpDot %half %half_0_1 %half_0_1\n" +
5659 "OpReturn\n" +
5660 "OpFunctionEnd",
5661 2, 0),
5662 // Test case 25: Don't fold 1.0(half) / 2.0(half)
5663 // We do not have to code to emulate 16-bit float operations. Just make sure we do not crash.
5664 InstructionFoldingCase<uint32_t>(
5665 Header() + "%main = OpFunction %void None %void_func\n" +
5666 "%main_lab = OpLabel\n" +
5667 "%n = OpVariable %_ptr_half Function\n" +
5668 "%3 = OpLoad %half %n\n" +
5669 "%2 = OpFDiv %half %half_1 %half_2\n" +
5670 "OpReturn\n" +
5671 "OpFunctionEnd",
5672 2, 0),
5673 // Test case 26: Don't fold OpFNegate for cooperative matrices.
5674 InstructionFoldingCase<uint32_t>(
5675 Header() + "%main = OpFunction %void None %void_func\n" +
5676 "%main_lab = OpLabel\n" +
5677 "%2 = OpFNegate %float_coop_matrix %undef_float_coop_matrix\n" +
5678 "OpReturn\n" +
5679 "OpFunctionEnd",
5680 2, 0),
5681 // Test case 27: Don't fold OpFAdd for cooperative matrices.
5682 InstructionFoldingCase<uint32_t>(
5683 Header() + "%main = OpFunction %void None %void_func\n" +
5684 "%main_lab = OpLabel\n" +
5685 "%2 = OpFAdd %float_coop_matrix %undef_float_coop_matrix %undef_float_coop_matrix\n" +
5686 "OpReturn\n" +
5687 "OpFunctionEnd",
5688 2, 0),
5689 // Test case 28: Don't fold OpFSub for cooperative matrices.
5690 InstructionFoldingCase<uint32_t>(
5691 Header() + "%main = OpFunction %void None %void_func\n" +
5692 "%main_lab = OpLabel\n" +
5693 "%2 = OpFSub %float_coop_matrix %undef_float_coop_matrix %undef_float_coop_matrix\n" +
5694 "OpReturn\n" +
5695 "OpFunctionEnd",
5696 2, 0),
5697 // Test case 29: Don't fold OpFMul for cooperative matrices.
5698 InstructionFoldingCase<uint32_t>(
5699 Header() + "%main = OpFunction %void None %void_func\n" +
5700 "%main_lab = OpLabel\n" +
5701 "%2 = OpFMul %float_coop_matrix %undef_float_coop_matrix %undef_float_coop_matrix\n" +
5702 "OpReturn\n" +
5703 "OpFunctionEnd",
5704 2, 0),
5705 // Test case 30: Don't fold OpFDiv for cooperative matrices.
5706 InstructionFoldingCase<uint32_t>(
5707 Header() + "%main = OpFunction %void None %void_func\n" +
5708 "%main_lab = OpLabel\n" +
5709 "%2 = OpFDiv %float_coop_matrix %undef_float_coop_matrix %undef_float_coop_matrix\n" +
5710 "OpReturn\n" +
5711 "OpFunctionEnd",
5712 2, 0),
5713 // Test case 31: Don't fold OpMatrixTimesScalar for cooperative matrices.
5714 InstructionFoldingCase<uint32_t>(
5715 Header() + "%main = OpFunction %void None %void_func\n" +
5716 "%main_lab = OpLabel\n" +
5717 "%2 = OpMatrixTimesScalar %float_coop_matrix %undef_float_coop_matrix %float_3\n" +
5718 "OpReturn\n" +
5719 "OpFunctionEnd",
5720 2, 0),
5721 // Test case 32: Don't fold FMix half (1.0, 2.0, 0.5)
5722 InstructionFoldingCase<uint32_t>(
5723 Header() + "%main = OpFunction %void None %void_func\n" +
5724 "%main_lab = OpLabel\n" +
5725 "%2 = OpExtInst %half %1 FMix %half_1 %half_2 %half_0p5\n" +
5726 "OpReturn\n" +
5727 "OpFunctionEnd",
5728 2, 0),
5729 // Test case 33: Fold FMix half (3.0, 2.0, 0.0)
5730 InstructionFoldingCase<uint32_t>(
5731 Header() + "%main = OpFunction %void None %void_func\n" +
5732 "%main_lab = OpLabel\n" +
5733 "%2 = OpExtInst %half %1 FMix %112 %half_2 %108\n" +
5734 "OpReturn\n" +
5735 "OpFunctionEnd",
5736 2, HALF_3_ID),
5737 // Test case 34: Fold FMix half (3.0, 2.0, null)
5738 InstructionFoldingCase<uint32_t>(
5739 Header() + "%main = OpFunction %void None %void_func\n" +
5740 "%main_lab = OpLabel\n" +
5741 "%2 = OpExtInst %half %1 FMix %112 %half_2 %half_null\n" +
5742 "OpReturn\n" +
5743 "OpFunctionEnd",
5744 2, HALF_3_ID),
5745 // Test case 35: Don't fold FMix half (1.0, 2.0, 1.0)
5746 InstructionFoldingCase<uint32_t>(
5747 Header() + "%main = OpFunction %void None %void_func\n" +
5748 "%main_lab = OpLabel\n" +
5749 "%2 = OpExtInst %half %1 FMix %half_1 %half_2 %half_1\n" +
5750 "OpReturn\n" +
5751 "OpFunctionEnd",
5752 2, 0),
5753 // Test case 36: Fold n - n
5754 InstructionFoldingCase<uint32_t>(
5755 Header() + "%main = OpFunction %void None %void_func\n" +
5756 "%main_lab = OpLabel\n" +
5757 "%n = OpVariable %_ptr_float Function\n" +
5758 "%3 = OpLoad %float %n\n" +
5759 "%2 = OpFSub %float %3 %3\n" +
5760 "OpReturn\n" +
5761 "OpFunctionEnd",
5762 2, FLOAT_NULL_ID),
5763 // Test case 37: Fold n - n
5764 InstructionFoldingCase<uint32_t>(
5765 Header() + "%main = OpFunction %void None %void_func\n" +
5766 "%main_lab = OpLabel\n" +
5767 "%n = OpVariable %_ptr_int Function\n" +
5768 "%3 = OpLoad %int %n\n" +
5769 "%2 = OpISub %int %3 %3\n" +
5770 "OpReturn\n" +
5771 "OpFunctionEnd",
5772 2, INT_NULL_ID)
5773 ));
5774
5775 INSTANTIATE_TEST_SUITE_P(DoubleRedundantFoldingTest, GeneralInstructionFoldingTest,
5776 ::testing::Values(
5777 // Test case 0: Don't fold n + 1.0
5778 InstructionFoldingCase<uint32_t>(
5779 Header() + "%main = OpFunction %void None %void_func\n" +
5780 "%main_lab = OpLabel\n" +
5781 "%n = OpVariable %_ptr_double Function\n" +
5782 "%3 = OpLoad %double %n\n" +
5783 "%2 = OpFAdd %double %3 %double_2\n" +
5784 "OpReturn\n" +
5785 "OpFunctionEnd",
5786 2, 0),
5787 // Test case 1: Don't fold n - 1.0
5788 InstructionFoldingCase<uint32_t>(
5789 Header() + "%main = OpFunction %void None %void_func\n" +
5790 "%main_lab = OpLabel\n" +
5791 "%n = OpVariable %_ptr_double Function\n" +
5792 "%3 = OpLoad %double %n\n" +
5793 "%2 = OpFSub %double %3 %double_2\n" +
5794 "OpReturn\n" +
5795 "OpFunctionEnd",
5796 2, 0),
5797 // Test case 2: Don't fold n * 2.0
5798 InstructionFoldingCase<uint32_t>(
5799 Header() + "%main = OpFunction %void None %void_func\n" +
5800 "%main_lab = OpLabel\n" +
5801 "%n = OpVariable %_ptr_double Function\n" +
5802 "%3 = OpLoad %double %n\n" +
5803 "%2 = OpFMul %double %3 %double_2\n" +
5804 "OpReturn\n" +
5805 "OpFunctionEnd",
5806 2, 0),
5807 // Test case 3: Fold n + 0.0
5808 InstructionFoldingCase<uint32_t>(
5809 Header() + "%main = OpFunction %void None %void_func\n" +
5810 "%main_lab = OpLabel\n" +
5811 "%n = OpVariable %_ptr_double Function\n" +
5812 "%3 = OpLoad %double %n\n" +
5813 "%2 = OpFAdd %double %3 %double_0\n" +
5814 "OpReturn\n" +
5815 "OpFunctionEnd",
5816 2, 3),
5817 // Test case 4: Fold 0.0 + n
5818 InstructionFoldingCase<uint32_t>(
5819 Header() + "%main = OpFunction %void None %void_func\n" +
5820 "%main_lab = OpLabel\n" +
5821 "%n = OpVariable %_ptr_double Function\n" +
5822 "%3 = OpLoad %double %n\n" +
5823 "%2 = OpFAdd %double %double_0 %3\n" +
5824 "OpReturn\n" +
5825 "OpFunctionEnd",
5826 2, 3),
5827 // Test case 5: Fold n - 0.0
5828 InstructionFoldingCase<uint32_t>(
5829 Header() + "%main = OpFunction %void None %void_func\n" +
5830 "%main_lab = OpLabel\n" +
5831 "%n = OpVariable %_ptr_double Function\n" +
5832 "%3 = OpLoad %double %n\n" +
5833 "%2 = OpFSub %double %3 %double_0\n" +
5834 "OpReturn\n" +
5835 "OpFunctionEnd",
5836 2, 3),
5837 // Test case 6: Fold n * 1.0
5838 InstructionFoldingCase<uint32_t>(
5839 Header() + "%main = OpFunction %void None %void_func\n" +
5840 "%main_lab = OpLabel\n" +
5841 "%n = OpVariable %_ptr_double Function\n" +
5842 "%3 = OpLoad %double %n\n" +
5843 "%2 = OpFMul %double %3 %double_1\n" +
5844 "OpReturn\n" +
5845 "OpFunctionEnd",
5846 2, 3),
5847 // Test case 7: Fold 1.0 * n
5848 InstructionFoldingCase<uint32_t>(
5849 Header() + "%main = OpFunction %void None %void_func\n" +
5850 "%main_lab = OpLabel\n" +
5851 "%n = OpVariable %_ptr_double Function\n" +
5852 "%3 = OpLoad %double %n\n" +
5853 "%2 = OpFMul %double %double_1 %3\n" +
5854 "OpReturn\n" +
5855 "OpFunctionEnd",
5856 2, 3),
5857 // Test case 8: Fold n / 1.0
5858 InstructionFoldingCase<uint32_t>(
5859 Header() + "%main = OpFunction %void None %void_func\n" +
5860 "%main_lab = OpLabel\n" +
5861 "%n = OpVariable %_ptr_double Function\n" +
5862 "%3 = OpLoad %double %n\n" +
5863 "%2 = OpFDiv %double %3 %double_1\n" +
5864 "OpReturn\n" +
5865 "OpFunctionEnd",
5866 2, 3),
5867 // Test case 9: Fold n * 0.0
5868 InstructionFoldingCase<uint32_t>(
5869 Header() + "%main = OpFunction %void None %void_func\n" +
5870 "%main_lab = OpLabel\n" +
5871 "%n = OpVariable %_ptr_double Function\n" +
5872 "%3 = OpLoad %double %n\n" +
5873 "%2 = OpFMul %double %3 %105\n" +
5874 "OpReturn\n" +
5875 "OpFunctionEnd",
5876 2, DOUBLE_0_ID),
5877 // Test case 10: Fold 0.0 * n
5878 InstructionFoldingCase<uint32_t>(
5879 Header() + "%main = OpFunction %void None %void_func\n" +
5880 "%main_lab = OpLabel\n" +
5881 "%n = OpVariable %_ptr_double Function\n" +
5882 "%3 = OpLoad %double %n\n" +
5883 "%2 = OpFMul %double %105 %3\n" +
5884 "OpReturn\n" +
5885 "OpFunctionEnd",
5886 2, DOUBLE_0_ID),
5887 // Test case 11: Fold 0.0 / n
5888 InstructionFoldingCase<uint32_t>(
5889 Header() + "%main = OpFunction %void None %void_func\n" +
5890 "%main_lab = OpLabel\n" +
5891 "%n = OpVariable %_ptr_double Function\n" +
5892 "%3 = OpLoad %double %n\n" +
5893 "%2 = OpFDiv %double %105 %3\n" +
5894 "OpReturn\n" +
5895 "OpFunctionEnd",
5896 2, DOUBLE_0_ID),
5897 // Test case 12: Don't fold mix(a, b, 2.0)
5898 InstructionFoldingCase<uint32_t>(
5899 Header() + "%main = OpFunction %void None %void_func\n" +
5900 "%main_lab = OpLabel\n" +
5901 "%a = OpVariable %_ptr_double Function\n" +
5902 "%b = OpVariable %_ptr_double Function\n" +
5903 "%3 = OpLoad %double %a\n" +
5904 "%4 = OpLoad %double %b\n" +
5905 "%2 = OpExtInst %double %1 FMix %3 %4 %double_2\n" +
5906 "OpReturn\n" +
5907 "OpFunctionEnd",
5908 2, 0),
5909 // Test case 13: Fold mix(a, b, 0.0)
5910 InstructionFoldingCase<uint32_t>(
5911 Header() + "%main = OpFunction %void None %void_func\n" +
5912 "%main_lab = OpLabel\n" +
5913 "%a = OpVariable %_ptr_double Function\n" +
5914 "%b = OpVariable %_ptr_double Function\n" +
5915 "%3 = OpLoad %double %a\n" +
5916 "%4 = OpLoad %double %b\n" +
5917 "%2 = OpExtInst %double %1 FMix %3 %4 %double_0\n" +
5918 "OpReturn\n" +
5919 "OpFunctionEnd",
5920 2, 3),
5921 // Test case 14: Fold mix(a, b, 1.0)
5922 InstructionFoldingCase<uint32_t>(
5923 Header() + "%main = OpFunction %void None %void_func\n" +
5924 "%main_lab = OpLabel\n" +
5925 "%a = OpVariable %_ptr_double Function\n" +
5926 "%b = OpVariable %_ptr_double Function\n" +
5927 "%3 = OpLoad %double %a\n" +
5928 "%4 = OpLoad %double %b\n" +
5929 "%2 = OpExtInst %double %1 FMix %3 %4 %double_1\n" +
5930 "OpReturn\n" +
5931 "OpFunctionEnd",
5932 2, 4)
5933 ));
5934
5935 INSTANTIATE_TEST_SUITE_P(FloatVectorRedundantFoldingTest, GeneralInstructionFoldingTest,
5936 ::testing::Values(
5937 // Test case 0: Don't fold a * vec4(0.0, 0.0, 0.0, 1.0)
5938 InstructionFoldingCase<uint32_t>(
5939 Header() + "%main = OpFunction %void None %void_func\n" +
5940 "%main_lab = OpLabel\n" +
5941 "%n = OpVariable %_ptr_v4float Function\n" +
5942 "%3 = OpLoad %v4float %n\n" +
5943 "%2 = OpFMul %v4float %3 %v4float_0_0_0_1\n" +
5944 "OpReturn\n" +
5945 "OpFunctionEnd",
5946 2, 0),
5947 // Test case 1: Fold a * vec4(0.0, 0.0, 0.0, 0.0)
5948 InstructionFoldingCase<uint32_t>(
5949 Header() + "%main = OpFunction %void None %void_func\n" +
5950 "%main_lab = OpLabel\n" +
5951 "%n = OpVariable %_ptr_v4float Function\n" +
5952 "%3 = OpLoad %v4float %n\n" +
5953 "%2 = OpFMul %v4float %3 %106\n" +
5954 "OpReturn\n" +
5955 "OpFunctionEnd",
5956 2, VEC4_0_ID),
5957 // Test case 2: Fold a * vec4(1.0, 1.0, 1.0, 1.0)
5958 InstructionFoldingCase<uint32_t>(
5959 Header() + "%main = OpFunction %void None %void_func\n" +
5960 "%main_lab = OpLabel\n" +
5961 "%n = OpVariable %_ptr_v4float Function\n" +
5962 "%3 = OpLoad %v4float %n\n" +
5963 "%2 = OpFMul %v4float %3 %v4float_1_1_1_1\n" +
5964 "OpReturn\n" +
5965 "OpFunctionEnd",
5966 2, 3)
5967 ));
5968
5969 INSTANTIATE_TEST_SUITE_P(DoubleVectorRedundantFoldingTest, GeneralInstructionFoldingTest,
5970 ::testing::Values(
5971 // Test case 0: Don't fold a * vec4(0.0, 0.0, 0.0, 1.0)
5972 InstructionFoldingCase<uint32_t>(
5973 Header() + "%main = OpFunction %void None %void_func\n" +
5974 "%main_lab = OpLabel\n" +
5975 "%n = OpVariable %_ptr_v4double Function\n" +
5976 "%3 = OpLoad %v4double %n\n" +
5977 "%2 = OpFMul %v4double %3 %v4double_0_0_0_1\n" +
5978 "OpReturn\n" +
5979 "OpFunctionEnd",
5980 2, 0),
5981 // Test case 1: Fold a * vec4(0.0, 0.0, 0.0, 0.0)
5982 InstructionFoldingCase<uint32_t>(
5983 Header() + "%main = OpFunction %void None %void_func\n" +
5984 "%main_lab = OpLabel\n" +
5985 "%n = OpVariable %_ptr_v4double Function\n" +
5986 "%3 = OpLoad %v4double %n\n" +
5987 "%2 = OpFMul %v4double %3 %106\n" +
5988 "OpReturn\n" +
5989 "OpFunctionEnd",
5990 2, DVEC4_0_ID),
5991 // Test case 2: Fold a + vec4(0.0, 0.0, 0.0, 0.0)
5992 InstructionFoldingCase<uint32_t>(
5993 Header() + "%main = OpFunction %void None %void_func\n" +
5994 "%main_lab = OpLabel\n" +
5995 "%n = OpVariable %_ptr_v4double Function\n" +
5996 "%3 = OpLoad %v4double %n\n" +
5997 "%2 = OpFAdd %v4double %3 %106\n" +
5998 "OpReturn\n" +
5999 "OpFunctionEnd",
6000 2, 3),
6001 // Test case 3: Fold a - vec4(0.0, 0.0, 0.0, 0.0)
6002 InstructionFoldingCase<uint32_t>(
6003 Header() + "%main = OpFunction %void None %void_func\n" +
6004 "%main_lab = OpLabel\n" +
6005 "%n = OpVariable %_ptr_v4double Function\n" +
6006 "%3 = OpLoad %v4double %n\n" +
6007 "%2 = OpFSub %v4double %3 %106\n" +
6008 "OpReturn\n" +
6009 "OpFunctionEnd",
6010 2, 3),
6011 // Test case 4: Fold a * vec4(1.0, 1.0, 1.0, 1.0)
6012 InstructionFoldingCase<uint32_t>(
6013 Header() + "%main = OpFunction %void None %void_func\n" +
6014 "%main_lab = OpLabel\n" +
6015 "%n = OpVariable %_ptr_v4double Function\n" +
6016 "%3 = OpLoad %v4double %n\n" +
6017 "%2 = OpFMul %v4double %3 %v4double_1_1_1_1\n" +
6018 "OpReturn\n" +
6019 "OpFunctionEnd",
6020 2, 3)
6021 ));
6022
6023 INSTANTIATE_TEST_SUITE_P(IntegerRedundantFoldingTest, GeneralInstructionFoldingTest,
6024 ::testing::Values(
6025 // Test case 0: Don't fold n + 1
6026 InstructionFoldingCase<uint32_t>(
6027 Header() + "%main = OpFunction %void None %void_func\n" +
6028 "%main_lab = OpLabel\n" +
6029 "%n = OpVariable %_ptr_uint Function\n" +
6030 "%3 = OpLoad %uint %n\n" +
6031 "%2 = OpIAdd %uint %3 %uint_1\n" +
6032 "OpReturn\n" +
6033 "OpFunctionEnd",
6034 2, 0),
6035 // Test case 1: Don't fold 1 + n
6036 InstructionFoldingCase<uint32_t>(
6037 Header() + "%main = OpFunction %void None %void_func\n" +
6038 "%main_lab = OpLabel\n" +
6039 "%n = OpVariable %_ptr_uint Function\n" +
6040 "%3 = OpLoad %uint %n\n" +
6041 "%2 = OpIAdd %uint %uint_1 %3\n" +
6042 "OpReturn\n" +
6043 "OpFunctionEnd",
6044 2, 0),
6045 // Test case 2: Fold n | 0
6046 InstructionFoldingCase<uint32_t>(
6047 Header() + "%main = OpFunction %void None %void_func\n" +
6048 "%main_lab = OpLabel\n" +
6049 "%n = OpVariable %_ptr_uint Function\n" +
6050 "%3 = OpLoad %uint %n\n" +
6051 "%2 = OpBitwiseOr %uint %3 %uint_0\n" +
6052 "OpReturn\n" +
6053 "OpFunctionEnd",
6054 2, 3),
6055 // Test case 3: Fold n ^ 0
6056 InstructionFoldingCase<uint32_t>(
6057 Header() + "%main = OpFunction %void None %void_func\n" +
6058 "%main_lab = OpLabel\n" +
6059 "%n = OpVariable %_ptr_uint Function\n" +
6060 "%3 = OpLoad %uint %n\n" +
6061 "%2 = OpBitwiseXor %uint %3 %uint_0\n" +
6062 "OpReturn\n" +
6063 "OpFunctionEnd",
6064 2, 3),
6065 // Test case 4: Fold n >> 0
6066 InstructionFoldingCase<uint32_t>(
6067 Header() + "%main = OpFunction %void None %void_func\n" +
6068 "%main_lab = OpLabel\n" +
6069 "%n = OpVariable %_ptr_uint Function\n" +
6070 "%3 = OpLoad %uint %n\n" +
6071 "%2 = OpShiftRightLogical %uint %3 %uint_0\n" +
6072 "OpReturn\n" +
6073 "OpFunctionEnd",
6074 2, 3),
6075 // Test case 5: Fold n >> 0
6076 InstructionFoldingCase<uint32_t>(
6077 Header() + "%main = OpFunction %void None %void_func\n" +
6078 "%main_lab = OpLabel\n" +
6079 "%n = OpVariable %_ptr_uint Function\n" +
6080 "%3 = OpLoad %uint %n\n" +
6081 "%2 = OpShiftRightArithmetic %uint %3 %uint_0\n" +
6082 "OpReturn\n" +
6083 "OpFunctionEnd",
6084 2, 3),
6085 // Test case 6: Fold n << 0
6086 InstructionFoldingCase<uint32_t>(
6087 Header() + "%main = OpFunction %void None %void_func\n" +
6088 "%main_lab = OpLabel\n" +
6089 "%n = OpVariable %_ptr_uint Function\n" +
6090 "%3 = OpLoad %uint %n\n" +
6091 "%2 = OpShiftLeftLogical %uint %3 %uint_0\n" +
6092 "OpReturn\n" +
6093 "OpFunctionEnd",
6094 2, 3),
6095 // Test case 7: Fold n + 0
6096 InstructionFoldingCase<uint32_t>(
6097 Header() + "%main = OpFunction %void None %void_func\n" +
6098 "%main_lab = OpLabel\n" +
6099 "%n = OpVariable %_ptr_uint Function\n" +
6100 "%3 = OpLoad %uint %n\n" +
6101 "%2 = OpIAdd %uint %3 %uint_0\n" +
6102 "OpReturn\n" +
6103 "OpFunctionEnd",
6104 2, 3),
6105 // Test case 8: Fold n - 0
6106 InstructionFoldingCase<uint32_t>(
6107 Header() + "%main = OpFunction %void None %void_func\n" +
6108 "%main_lab = OpLabel\n" +
6109 "%n = OpVariable %_ptr_uint Function\n" +
6110 "%3 = OpLoad %uint %n\n" +
6111 "%2 = OpISub %uint %3 %uint_0\n" +
6112 "OpReturn\n" +
6113 "OpFunctionEnd",
6114 2, 3),
6115 // Test case 9: Fold 0 | n
6116 InstructionFoldingCase<uint32_t>(
6117 Header() + "%main = OpFunction %void None %void_func\n" +
6118 "%main_lab = OpLabel\n" +
6119 "%n = OpVariable %_ptr_uint Function\n" +
6120 "%3 = OpLoad %uint %n\n" +
6121 "%2 = OpBitwiseOr %uint %uint_0 %3\n" +
6122 "OpReturn\n" +
6123 "OpFunctionEnd",
6124 2, 3),
6125 // Test case 10: Fold 0 ^ n
6126 InstructionFoldingCase<uint32_t>(
6127 Header() + "%main = OpFunction %void None %void_func\n" +
6128 "%main_lab = OpLabel\n" +
6129 "%n = OpVariable %_ptr_uint Function\n" +
6130 "%3 = OpLoad %uint %n\n" +
6131 "%2 = OpBitwiseXor %uint %uint_0 %3\n" +
6132 "OpReturn\n" +
6133 "OpFunctionEnd",
6134 2, 3),
6135 // Test case 11: Fold 0 + n
6136 InstructionFoldingCase<uint32_t>(
6137 Header() + "%main = OpFunction %void None %void_func\n" +
6138 "%main_lab = OpLabel\n" +
6139 "%n = OpVariable %_ptr_uint Function\n" +
6140 "%3 = OpLoad %uint %n\n" +
6141 "%2 = OpIAdd %uint %uint_0 %3\n" +
6142 "OpReturn\n" +
6143 "OpFunctionEnd",
6144 2, 3),
6145 // Test case 12: Don't fold n + (1,0)
6146 InstructionFoldingCase<uint32_t>(
6147 Header() + "%main = OpFunction %void None %void_func\n" +
6148 "%main_lab = OpLabel\n" +
6149 "%n = OpVariable %_ptr_v2int Function\n" +
6150 "%3 = OpLoad %v2int %n\n" +
6151 "%2 = OpIAdd %v2int %3 %v2int_1_0\n" +
6152 "OpReturn\n" +
6153 "OpFunctionEnd",
6154 2, 0),
6155 // Test case 13: Don't fold (1,0) + n
6156 InstructionFoldingCase<uint32_t>(
6157 Header() + "%main = OpFunction %void None %void_func\n" +
6158 "%main_lab = OpLabel\n" +
6159 "%n = OpVariable %_ptr_v2int Function\n" +
6160 "%3 = OpLoad %v2int %n\n" +
6161 "%2 = OpIAdd %v2int %v2int_1_0 %3\n" +
6162 "OpReturn\n" +
6163 "OpFunctionEnd",
6164 2, 0),
6165 // Test case 14: Fold n + (0,0)
6166 InstructionFoldingCase<uint32_t>(
6167 Header() + "%main = OpFunction %void None %void_func\n" +
6168 "%main_lab = OpLabel\n" +
6169 "%n = OpVariable %_ptr_v2int Function\n" +
6170 "%3 = OpLoad %v2int %n\n" +
6171 "%2 = OpIAdd %v2int %3 %v2int_0_0\n" +
6172 "OpReturn\n" +
6173 "OpFunctionEnd",
6174 2, 3),
6175 // Test case 15: Fold (0,0) + n
6176 InstructionFoldingCase<uint32_t>(
6177 Header() + "%main = OpFunction %void None %void_func\n" +
6178 "%main_lab = OpLabel\n" +
6179 "%n = OpVariable %_ptr_v2int Function\n" +
6180 "%3 = OpLoad %v2int %n\n" +
6181 "%2 = OpIAdd %v2int %v2int_0_0 %3\n" +
6182 "OpReturn\n" +
6183 "OpFunctionEnd",
6184 2, 3),
6185 // Test case 16: Fold n | (0,0)
6186 InstructionFoldingCase<uint32_t>(
6187 Header() + "%main = OpFunction %void None %void_func\n" +
6188 "%main_lab = OpLabel\n" +
6189 "%n = OpVariable %_ptr_v2int Function\n" +
6190 "%3 = OpLoad %v2int %n\n" +
6191 "%2 = OpBitwiseOr %v2int %3 %v2int_0_0\n" +
6192 "OpReturn\n" +
6193 "OpFunctionEnd",
6194 2, 3),
6195 // Test case 17: Fold (0,0) | n
6196 InstructionFoldingCase<uint32_t>(
6197 Header() + "%main = OpFunction %void None %void_func\n" +
6198 "%main_lab = OpLabel\n" +
6199 "%n = OpVariable %_ptr_v2int Function\n" +
6200 "%3 = OpLoad %v2int %n\n" +
6201 "%2 = OpBitwiseOr %v2int %v2int_0_0 %3\n" +
6202 "OpReturn\n" +
6203 "OpFunctionEnd",
6204 2, 3),
6205 // Test case 18: Fold 0 >> n
6206 InstructionFoldingCase<uint32_t>(
6207 Header() + "%main = OpFunction %void None %void_func\n" +
6208 "%main_lab = OpLabel\n" +
6209 "%n = OpVariable %_ptr_uint Function\n" +
6210 "%3 = OpLoad %uint %n\n" +
6211 "%2 = OpShiftRightLogical %uint %109 %3\n" +
6212 "OpReturn\n" +
6213 "OpFunctionEnd",
6214 2, UINT_0_ID),
6215 // Test case 19: Fold 0 >> n
6216 InstructionFoldingCase<uint32_t>(
6217 Header() + "%main = OpFunction %void None %void_func\n" +
6218 "%main_lab = OpLabel\n" +
6219 "%n = OpVariable %_ptr_uint Function\n" +
6220 "%3 = OpLoad %uint %n\n" +
6221 "%2 = OpShiftRightArithmetic %uint %109 %3\n" +
6222 "OpReturn\n" +
6223 "OpFunctionEnd",
6224 2, UINT_0_ID),
6225 // Test case 20: Fold 0 << n
6226 InstructionFoldingCase<uint32_t>(
6227 Header() + "%main = OpFunction %void None %void_func\n" +
6228 "%main_lab = OpLabel\n" +
6229 "%n = OpVariable %_ptr_uint Function\n" +
6230 "%3 = OpLoad %uint %n\n" +
6231 "%2 = OpShiftLeftLogical %uint %109 %3\n" +
6232 "OpReturn\n" +
6233 "OpFunctionEnd",
6234 2, UINT_0_ID),
6235 // Test case 21: Fold 0 / n
6236 InstructionFoldingCase<uint32_t>(
6237 Header() + "%main = OpFunction %void None %void_func\n" +
6238 "%main_lab = OpLabel\n" +
6239 "%n = OpVariable %_ptr_uint Function\n" +
6240 "%3 = OpLoad %int %n\n" +
6241 "%2 = OpSDiv %int %100 %3\n" +
6242 "OpReturn\n" +
6243 "OpFunctionEnd",
6244 2, INT_0_ID),
6245 // Test case 22: Fold 0 / n
6246 InstructionFoldingCase<uint32_t>(
6247 Header() + "%main = OpFunction %void None %void_func\n" +
6248 "%main_lab = OpLabel\n" +
6249 "%n = OpVariable %_ptr_uint Function\n" +
6250 "%3 = OpLoad %uint %n\n" +
6251 "%2 = OpUDiv %uint %109 %3\n" +
6252 "OpReturn\n" +
6253 "OpFunctionEnd",
6254 2, UINT_0_ID),
6255 // Test case 23: Fold 0 % n
6256 InstructionFoldingCase<uint32_t>(
6257 Header() + "%main = OpFunction %void None %void_func\n" +
6258 "%main_lab = OpLabel\n" +
6259 "%n = OpVariable %_ptr_uint Function\n" +
6260 "%3 = OpLoad %uint %n\n" +
6261 "%2 = OpSMod %int %int_0 %3\n" +
6262 "OpReturn\n" +
6263 "OpFunctionEnd",
6264 2, INT_0_ID),
6265 // Test case 24: Fold 0 % n
6266 InstructionFoldingCase<uint32_t>(
6267 Header() + "%main = OpFunction %void None %void_func\n" +
6268 "%main_lab = OpLabel\n" +
6269 "%n = OpVariable %_ptr_uint Function\n" +
6270 "%3 = OpLoad %uint %n\n" +
6271 "%2 = OpUMod %uint %109 %3\n" +
6272 "OpReturn\n" +
6273 "OpFunctionEnd",
6274 2, UINT_0_ID),
6275 // Test case 25: Fold n % 1
6276 InstructionFoldingCase<uint32_t>(
6277 Header() + "%main = OpFunction %void None %void_func\n" +
6278 "%main_lab = OpLabel\n" +
6279 "%n = OpVariable %_ptr_uint Function\n" +
6280 "%3 = OpLoad %int %n\n" +
6281 "%2 = OpSMod %int %3 %int_1\n" +
6282 "OpReturn\n" +
6283 "OpFunctionEnd",
6284 2, INT_NULL_ID),
6285 // Test case 26: Fold n % 1
6286 InstructionFoldingCase<uint32_t>(
6287 Header() + "%main = OpFunction %void None %void_func\n" +
6288 "%main_lab = OpLabel\n" +
6289 "%n = OpVariable %_ptr_uint Function\n" +
6290 "%3 = OpLoad %uint %n\n" +
6291 "%2 = OpUMod %uint %3 %uint_1\n" +
6292 "OpReturn\n" +
6293 "OpFunctionEnd",
6294 2, UINT_NULL_ID),
6295 // Test case 27: Don't fold because of undefined value. Using 4294967295
6296 // means that entry is undefined. We do not expect it to ever happen, so
6297 // not worth folding.
6298 InstructionFoldingCase<uint32_t>(
6299 Header() + "%main = OpFunction %void None %void_func\n" +
6300 "%main_lab = OpLabel\n" +
6301 "%n = OpVariable %_ptr_int Function\n" +
6302 "%load = OpLoad %int %n\n" +
6303 "%2 = OpVectorShuffle %v2int %v2int_null %v2int_2_3 4294967295 3\n" +
6304 "OpReturn\n" +
6305 "OpFunctionEnd",
6306 2, 0),
6307 // Test case 28: Don't fold because of undefined value. Using 4294967295
6308 // means that entry is undefined. We do not expect it to ever happen, so
6309 // not worth folding.
6310 InstructionFoldingCase<uint32_t>(
6311 Header() + "%main = OpFunction %void None %void_func\n" +
6312 "%main_lab = OpLabel\n" +
6313 "%n = OpVariable %_ptr_int Function\n" +
6314 "%load = OpLoad %int %n\n" +
6315 "%2 = OpVectorShuffle %v2int %v2int_null %v2int_2_3 0 4294967295 \n" +
6316 "OpReturn\n" +
6317 "OpFunctionEnd",
6318 2, 0)
6319 ));
6320
6321 INSTANTIATE_TEST_SUITE_P(ClampAndCmpLHS, GeneralInstructionFoldingTest,
6322 ::testing::Values(
6323 // Test case 0: Don't Fold 0.0 < clamp(-1, 1)
6324 InstructionFoldingCase<uint32_t>(
6325 Header() + "%main = OpFunction %void None %void_func\n" +
6326 "%main_lab = OpLabel\n" +
6327 "%n = OpVariable %_ptr_float Function\n" +
6328 "%ld = OpLoad %float %n\n" +
6329 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6330 "%2 = OpFUnordLessThan %bool %float_0 %clamp\n" +
6331 "OpReturn\n" +
6332 "OpFunctionEnd",
6333 2, 0),
6334 // Test case 1: Don't Fold 0.0 < clamp(-1, 1)
6335 InstructionFoldingCase<uint32_t>(
6336 Header() + "%main = OpFunction %void None %void_func\n" +
6337 "%main_lab = OpLabel\n" +
6338 "%n = OpVariable %_ptr_float Function\n" +
6339 "%ld = OpLoad %float %n\n" +
6340 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6341 "%2 = OpFOrdLessThan %bool %float_0 %clamp\n" +
6342 "OpReturn\n" +
6343 "OpFunctionEnd",
6344 2, 0),
6345 // Test case 2: Don't Fold 0.0 <= clamp(-1, 1)
6346 InstructionFoldingCase<uint32_t>(
6347 Header() + "%main = OpFunction %void None %void_func\n" +
6348 "%main_lab = OpLabel\n" +
6349 "%n = OpVariable %_ptr_float Function\n" +
6350 "%ld = OpLoad %float %n\n" +
6351 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6352 "%2 = OpFUnordLessThanEqual %bool %float_0 %clamp\n" +
6353 "OpReturn\n" +
6354 "OpFunctionEnd",
6355 2, 0),
6356 // Test case 3: Don't Fold 0.0 <= clamp(-1, 1)
6357 InstructionFoldingCase<uint32_t>(
6358 Header() + "%main = OpFunction %void None %void_func\n" +
6359 "%main_lab = OpLabel\n" +
6360 "%n = OpVariable %_ptr_float Function\n" +
6361 "%ld = OpLoad %float %n\n" +
6362 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6363 "%2 = OpFOrdLessThanEqual %bool %float_0 %clamp\n" +
6364 "OpReturn\n" +
6365 "OpFunctionEnd",
6366 2, 0),
6367 // Test case 4: Don't Fold 0.0 > clamp(-1, 1)
6368 InstructionFoldingCase<uint32_t>(
6369 Header() + "%main = OpFunction %void None %void_func\n" +
6370 "%main_lab = OpLabel\n" +
6371 "%n = OpVariable %_ptr_float Function\n" +
6372 "%ld = OpLoad %float %n\n" +
6373 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6374 "%2 = OpFUnordGreaterThan %bool %float_0 %clamp\n" +
6375 "OpReturn\n" +
6376 "OpFunctionEnd",
6377 2, 0),
6378 // Test case 5: Don't Fold 0.0 > clamp(-1, 1)
6379 InstructionFoldingCase<uint32_t>(
6380 Header() + "%main = OpFunction %void None %void_func\n" +
6381 "%main_lab = OpLabel\n" +
6382 "%n = OpVariable %_ptr_float Function\n" +
6383 "%ld = OpLoad %float %n\n" +
6384 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6385 "%2 = OpFOrdGreaterThan %bool %float_0 %clamp\n" +
6386 "OpReturn\n" +
6387 "OpFunctionEnd",
6388 2, 0),
6389 // Test case 6: Don't Fold 0.0 >= clamp(-1, 1)
6390 InstructionFoldingCase<uint32_t>(
6391 Header() + "%main = OpFunction %void None %void_func\n" +
6392 "%main_lab = OpLabel\n" +
6393 "%n = OpVariable %_ptr_float Function\n" +
6394 "%ld = OpLoad %float %n\n" +
6395 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6396 "%2 = OpFUnordGreaterThanEqual %bool %float_0 %clamp\n" +
6397 "OpReturn\n" +
6398 "OpFunctionEnd",
6399 2, 0),
6400 // Test case 7: Don't Fold 0.0 >= clamp(-1, 1)
6401 InstructionFoldingCase<uint32_t>(
6402 Header() + "%main = OpFunction %void None %void_func\n" +
6403 "%main_lab = OpLabel\n" +
6404 "%n = OpVariable %_ptr_float Function\n" +
6405 "%ld = OpLoad %float %n\n" +
6406 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6407 "%2 = OpFOrdGreaterThanEqual %bool %float_0 %clamp\n" +
6408 "OpReturn\n" +
6409 "OpFunctionEnd",
6410 2, 0),
6411 // Test case 8: Don't Fold 0.0 < clamp(0, 1)
6412 InstructionFoldingCase<uint32_t>(
6413 Header() + "%main = OpFunction %void None %void_func\n" +
6414 "%main_lab = OpLabel\n" +
6415 "%n = OpVariable %_ptr_float Function\n" +
6416 "%ld = OpLoad %float %n\n" +
6417 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
6418 "%2 = OpFUnordLessThan %bool %float_0 %clamp\n" +
6419 "OpReturn\n" +
6420 "OpFunctionEnd",
6421 2, 0),
6422 // Test case 9: Don't Fold 0.0 < clamp(0, 1)
6423 InstructionFoldingCase<uint32_t>(
6424 Header() + "%main = OpFunction %void None %void_func\n" +
6425 "%main_lab = OpLabel\n" +
6426 "%n = OpVariable %_ptr_float Function\n" +
6427 "%ld = OpLoad %float %n\n" +
6428 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
6429 "%2 = OpFOrdLessThan %bool %float_0 %clamp\n" +
6430 "OpReturn\n" +
6431 "OpFunctionEnd",
6432 2, 0),
6433 // Test case 10: Don't Fold 0.0 > clamp(-1, 0)
6434 InstructionFoldingCase<uint32_t>(
6435 Header() + "%main = OpFunction %void None %void_func\n" +
6436 "%main_lab = OpLabel\n" +
6437 "%n = OpVariable %_ptr_float Function\n" +
6438 "%ld = OpLoad %float %n\n" +
6439 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
6440 "%2 = OpFUnordGreaterThan %bool %float_0 %clamp\n" +
6441 "OpReturn\n" +
6442 "OpFunctionEnd",
6443 2, 0),
6444 // Test case 11: Don't Fold 0.0 > clamp(-1, 0)
6445 InstructionFoldingCase<uint32_t>(
6446 Header() + "%main = OpFunction %void None %void_func\n" +
6447 "%main_lab = OpLabel\n" +
6448 "%n = OpVariable %_ptr_float Function\n" +
6449 "%ld = OpLoad %float %n\n" +
6450 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
6451 "%2 = OpFOrdGreaterThan %bool %float_0 %clamp\n" +
6452 "OpReturn\n" +
6453 "OpFunctionEnd",
6454 2, 0)
6455 ));
6456
6457 INSTANTIATE_TEST_SUITE_P(ClampAndCmpRHS, GeneralInstructionFoldingTest,
6458 ::testing::Values(
6459 // Test case 0: Don't Fold clamp(-1, 1) < 0.0
6460 InstructionFoldingCase<uint32_t>(
6461 Header() + "%main = OpFunction %void None %void_func\n" +
6462 "%main_lab = OpLabel\n" +
6463 "%n = OpVariable %_ptr_float Function\n" +
6464 "%ld = OpLoad %float %n\n" +
6465 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6466 "%2 = OpFUnordLessThan %bool %clamp %float_0\n" +
6467 "OpReturn\n" +
6468 "OpFunctionEnd",
6469 2, 0),
6470 // Test case 1: Don't Fold clamp(-1, 1) < 0.0
6471 InstructionFoldingCase<uint32_t>(
6472 Header() + "%main = OpFunction %void None %void_func\n" +
6473 "%main_lab = OpLabel\n" +
6474 "%n = OpVariable %_ptr_float Function\n" +
6475 "%ld = OpLoad %float %n\n" +
6476 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6477 "%2 = OpFOrdLessThan %bool %clamp %float_0\n" +
6478 "OpReturn\n" +
6479 "OpFunctionEnd",
6480 2, 0),
6481 // Test case 2: Don't Fold clamp(-1, 1) <= 0.0
6482 InstructionFoldingCase<uint32_t>(
6483 Header() + "%main = OpFunction %void None %void_func\n" +
6484 "%main_lab = OpLabel\n" +
6485 "%n = OpVariable %_ptr_float Function\n" +
6486 "%ld = OpLoad %float %n\n" +
6487 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6488 "%2 = OpFUnordLessThanEqual %bool %clamp %float_0\n" +
6489 "OpReturn\n" +
6490 "OpFunctionEnd",
6491 2, 0),
6492 // Test case 3: Don't Fold clamp(-1, 1) <= 0.0
6493 InstructionFoldingCase<uint32_t>(
6494 Header() + "%main = OpFunction %void None %void_func\n" +
6495 "%main_lab = OpLabel\n" +
6496 "%n = OpVariable %_ptr_float Function\n" +
6497 "%ld = OpLoad %float %n\n" +
6498 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6499 "%2 = OpFOrdLessThanEqual %bool %clamp %float_0\n" +
6500 "OpReturn\n" +
6501 "OpFunctionEnd",
6502 2, 0),
6503 // Test case 4: Don't Fold clamp(-1, 1) > 0.0
6504 InstructionFoldingCase<uint32_t>(
6505 Header() + "%main = OpFunction %void None %void_func\n" +
6506 "%main_lab = OpLabel\n" +
6507 "%n = OpVariable %_ptr_float Function\n" +
6508 "%ld = OpLoad %float %n\n" +
6509 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6510 "%2 = OpFUnordGreaterThan %bool %clamp %float_0\n" +
6511 "OpReturn\n" +
6512 "OpFunctionEnd",
6513 2, 0),
6514 // Test case 5: Don't Fold clamp(-1, 1) > 0.0
6515 InstructionFoldingCase<uint32_t>(
6516 Header() + "%main = OpFunction %void None %void_func\n" +
6517 "%main_lab = OpLabel\n" +
6518 "%n = OpVariable %_ptr_float Function\n" +
6519 "%ld = OpLoad %float %n\n" +
6520 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6521 "%2 = OpFOrdGreaterThan %bool %clamp %float_0\n" +
6522 "OpReturn\n" +
6523 "OpFunctionEnd",
6524 2, 0),
6525 // Test case 6: Don't Fold clamp(-1, 1) >= 0.0
6526 InstructionFoldingCase<uint32_t>(
6527 Header() + "%main = OpFunction %void None %void_func\n" +
6528 "%main_lab = OpLabel\n" +
6529 "%n = OpVariable %_ptr_float Function\n" +
6530 "%ld = OpLoad %float %n\n" +
6531 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6532 "%2 = OpFUnordGreaterThanEqual %bool %clamp %float_0\n" +
6533 "OpReturn\n" +
6534 "OpFunctionEnd",
6535 2, 0),
6536 // Test case 7: Don't Fold clamp(-1, 1) >= 0.0
6537 InstructionFoldingCase<uint32_t>(
6538 Header() + "%main = OpFunction %void None %void_func\n" +
6539 "%main_lab = OpLabel\n" +
6540 "%n = OpVariable %_ptr_float Function\n" +
6541 "%ld = OpLoad %float %n\n" +
6542 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_1\n" +
6543 "%2 = OpFOrdGreaterThanEqual %bool %clamp %float_0\n" +
6544 "OpReturn\n" +
6545 "OpFunctionEnd",
6546 2, 0),
6547 // Test case 8: Don't Fold clamp(-1, 0) < 0.0
6548 InstructionFoldingCase<uint32_t>(
6549 Header() + "%main = OpFunction %void None %void_func\n" +
6550 "%main_lab = OpLabel\n" +
6551 "%n = OpVariable %_ptr_float Function\n" +
6552 "%ld = OpLoad %float %n\n" +
6553 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
6554 "%2 = OpFUnordLessThan %bool %clamp %float_0\n" +
6555 "OpReturn\n" +
6556 "OpFunctionEnd",
6557 2, 0),
6558 // Test case 9: Don't Fold clamp(0, 1) < 1
6559 InstructionFoldingCase<uint32_t>(
6560 Header() + "%main = OpFunction %void None %void_func\n" +
6561 "%main_lab = OpLabel\n" +
6562 "%n = OpVariable %_ptr_float Function\n" +
6563 "%ld = OpLoad %float %n\n" +
6564 "%clamp = OpExtInst %float %1 FClamp %ld %float_0 %float_1\n" +
6565 "%2 = OpFOrdLessThan %bool %clamp %float_1\n" +
6566 "OpReturn\n" +
6567 "OpFunctionEnd",
6568 2, 0),
6569 // Test case 10: Don't Fold clamp(-1, 0) > -1
6570 InstructionFoldingCase<uint32_t>(
6571 Header() + "%main = OpFunction %void None %void_func\n" +
6572 "%main_lab = OpLabel\n" +
6573 "%n = OpVariable %_ptr_float Function\n" +
6574 "%ld = OpLoad %float %n\n" +
6575 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
6576 "%2 = OpFUnordGreaterThan %bool %clamp %float_n1\n" +
6577 "OpReturn\n" +
6578 "OpFunctionEnd",
6579 2, 0),
6580 // Test case 11: Don't Fold clamp(-1, 0) > -1
6581 InstructionFoldingCase<uint32_t>(
6582 Header() + "%main = OpFunction %void None %void_func\n" +
6583 "%main_lab = OpLabel\n" +
6584 "%n = OpVariable %_ptr_float Function\n" +
6585 "%ld = OpLoad %float %n\n" +
6586 "%clamp = OpExtInst %float %1 FClamp %ld %float_n1 %float_0\n" +
6587 "%2 = OpFOrdGreaterThan %bool %clamp %float_n1\n" +
6588 "OpReturn\n" +
6589 "OpFunctionEnd",
6590 2, 0)
6591 ));
6592
6593 INSTANTIATE_TEST_SUITE_P(FToIConstantFoldingTest, IntegerInstructionFoldingTest,
6594 ::testing::Values(
6595 // Test case 0: Fold int(3.0)
6596 InstructionFoldingCase<uint32_t>(
6597 Header() + "%main = OpFunction %void None %void_func\n" +
6598 "%main_lab = OpLabel\n" +
6599 "%2 = OpConvertFToS %int %float_3\n" +
6600 "OpReturn\n" +
6601 "OpFunctionEnd",
6602 2, 3),
6603 // Test case 1: Fold uint(3.0)
6604 InstructionFoldingCase<uint32_t>(
6605 Header() + "%main = OpFunction %void None %void_func\n" +
6606 "%main_lab = OpLabel\n" +
6607 "%2 = OpConvertFToU %int %float_3\n" +
6608 "OpReturn\n" +
6609 "OpFunctionEnd",
6610 2, 3)
6611 ));
6612
6613 INSTANTIATE_TEST_SUITE_P(IToFConstantFoldingTest, FloatInstructionFoldingTest,
6614 ::testing::Values(
6615 // Test case 0: Fold float(3)
6616 InstructionFoldingCase<float>(
6617 Header() + "%main = OpFunction %void None %void_func\n" +
6618 "%main_lab = OpLabel\n" +
6619 "%2 = OpConvertSToF %float %int_3\n" +
6620 "OpReturn\n" +
6621 "OpFunctionEnd",
6622 2, 3.0),
6623 // Test case 1: Fold float(3u)
6624 InstructionFoldingCase<float>(
6625 Header() + "%main = OpFunction %void None %void_func\n" +
6626 "%main_lab = OpLabel\n" +
6627 "%2 = OpConvertUToF %float %uint_3\n" +
6628 "OpReturn\n" +
6629 "OpFunctionEnd",
6630 2, 3.0)
6631 ));
6632 // clang-format on
6633
6634 using ToNegateFoldingTest =
6635 ::testing::TestWithParam<InstructionFoldingCase<uint32_t>>;
6636
6637 TEST_P(ToNegateFoldingTest, Case) {
6638 const auto& tc = GetParam();
6639
6640 std::unique_ptr<IRContext> context;
6641 Instruction* inst;
6642 std::tie(context, inst) =
6643 FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_1);
6644
6645 EXPECT_TRUE((inst == nullptr) == (tc.expected_result == 0));
6646 if (inst != nullptr) {
6647 EXPECT_EQ(inst->opcode(), spv::Op::OpFNegate);
6648 EXPECT_EQ(inst->GetSingleWordInOperand(0), tc.expected_result);
6649 }
6650 }
6651
6652 // clang-format off
6653 INSTANTIATE_TEST_SUITE_P(FloatRedundantSubFoldingTest, ToNegateFoldingTest,
6654 ::testing::Values(
6655 // Test case 0: Don't fold 1.0 - n
6656 InstructionFoldingCase<uint32_t>(
6657 Header() + "%main = OpFunction %void None %void_func\n" +
6658 "%main_lab = OpLabel\n" +
6659 "%n = OpVariable %_ptr_float Function\n" +
6660 "%3 = OpLoad %float %n\n" +
6661 "%2 = OpFSub %float %float_1 %3\n" +
6662 "OpReturn\n" +
6663 "OpFunctionEnd",
6664 2, 0),
6665 // Test case 1: Fold 0.0 - n
6666 InstructionFoldingCase<uint32_t>(
6667 Header() + "%main = OpFunction %void None %void_func\n" +
6668 "%main_lab = OpLabel\n" +
6669 "%n = OpVariable %_ptr_float Function\n" +
6670 "%3 = OpLoad %float %n\n" +
6671 "%2 = OpFSub %float %float_0 %3\n" +
6672 "OpReturn\n" +
6673 "OpFunctionEnd",
6674 2, 3),
6675 // Test case 2: Don't fold (0,0,0,1) - n
6676 InstructionFoldingCase<uint32_t>(
6677 Header() + "%main = OpFunction %void None %void_func\n" +
6678 "%main_lab = OpLabel\n" +
6679 "%n = OpVariable %_ptr_v4float Function\n" +
6680 "%3 = OpLoad %v4float %n\n" +
6681 "%2 = OpFSub %v4float %v4float_0_0_0_1 %3\n" +
6682 "OpReturn\n" +
6683 "OpFunctionEnd",
6684 2, 0),
6685 // Test case 3: Fold (0,0,0,0) - n
6686 InstructionFoldingCase<uint32_t>(
6687 Header() + "%main = OpFunction %void None %void_func\n" +
6688 "%main_lab = OpLabel\n" +
6689 "%n = OpVariable %_ptr_v4float Function\n" +
6690 "%3 = OpLoad %v4float %n\n" +
6691 "%2 = OpFSub %v4float %v4float_0_0_0_0 %3\n" +
6692 "OpReturn\n" +
6693 "OpFunctionEnd",
6694 2, 3)
6695 ));
6696
6697 INSTANTIATE_TEST_SUITE_P(DoubleRedundantSubFoldingTest, ToNegateFoldingTest,
6698 ::testing::Values(
6699 // Test case 0: Don't fold 1.0 - n
6700 InstructionFoldingCase<uint32_t>(
6701 Header() + "%main = OpFunction %void None %void_func\n" +
6702 "%main_lab = OpLabel\n" +
6703 "%n = OpVariable %_ptr_double Function\n" +
6704 "%3 = OpLoad %double %n\n" +
6705 "%2 = OpFSub %double %double_1 %3\n" +
6706 "OpReturn\n" +
6707 "OpFunctionEnd",
6708 2, 0),
6709 // Test case 1: Fold 0.0 - n
6710 InstructionFoldingCase<uint32_t>(
6711 Header() + "%main = OpFunction %void None %void_func\n" +
6712 "%main_lab = OpLabel\n" +
6713 "%n = OpVariable %_ptr_double Function\n" +
6714 "%3 = OpLoad %double %n\n" +
6715 "%2 = OpFSub %double %double_0 %3\n" +
6716 "OpReturn\n" +
6717 "OpFunctionEnd",
6718 2, 3),
6719 // Test case 2: Don't fold (0,0,0,1) - n
6720 InstructionFoldingCase<uint32_t>(
6721 Header() + "%main = OpFunction %void None %void_func\n" +
6722 "%main_lab = OpLabel\n" +
6723 "%n = OpVariable %_ptr_v4double Function\n" +
6724 "%3 = OpLoad %v4double %n\n" +
6725 "%2 = OpFSub %v4double %v4double_0_0_0_1 %3\n" +
6726 "OpReturn\n" +
6727 "OpFunctionEnd",
6728 2, 0),
6729 // Test case 3: Fold (0,0,0,0) - n
6730 InstructionFoldingCase<uint32_t>(
6731 Header() + "%main = OpFunction %void None %void_func\n" +
6732 "%main_lab = OpLabel\n" +
6733 "%n = OpVariable %_ptr_v4double Function\n" +
6734 "%3 = OpLoad %v4double %n\n" +
6735 "%2 = OpFSub %v4double %v4double_0_0_0_0 %3\n" +
6736 "OpReturn\n" +
6737 "OpFunctionEnd",
6738 2, 3)
6739 ));
6740
6741 using MatchingInstructionFoldingTest =
6742 ::testing::TestWithParam<InstructionFoldingCase<bool>>;
6743
6744 TEST_P(MatchingInstructionFoldingTest, Case) {
6745 const auto& tc = GetParam();
6746
6747 std::unique_ptr<IRContext> context;
6748 Instruction* inst;
6749 std::tie(context, inst) = FoldInstruction(tc.test_body, tc.id_to_fold,SPV_ENV_UNIVERSAL_1_1);
6750
6751 EXPECT_EQ(inst != nullptr, tc.expected_result);
6752 if (inst != nullptr) {
6753 Match(tc.test_body, context.get());
6754 }
6755 }
6756
6757 INSTANTIATE_TEST_SUITE_P(RedundantIntegerMatching, MatchingInstructionFoldingTest,
6758 ::testing::Values(
6759 // Test case 0: Fold 0 + n (change sign)
6760 InstructionFoldingCase<bool>(
6761 Header() +
6762 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
6763 "; CHECK: %2 = OpBitcast [[uint]] %3\n" +
6764 "%main = OpFunction %void None %void_func\n" +
6765 "%main_lab = OpLabel\n" +
6766 "%n = OpVariable %_ptr_int Function\n" +
6767 "%3 = OpLoad %int %n\n" +
6768 "%2 = OpIAdd %uint %int_0 %3\n" +
6769 "OpReturn\n" +
6770 "OpFunctionEnd\n",
6771 2, true),
6772 // Test case 0: Fold 0 + n (change sign)
6773 InstructionFoldingCase<bool>(
6774 Header() +
6775 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
6776 "; CHECK: %2 = OpBitcast [[int]] %3\n" +
6777 "%main = OpFunction %void None %void_func\n" +
6778 "%main_lab = OpLabel\n" +
6779 "%n = OpVariable %_ptr_uint Function\n" +
6780 "%3 = OpLoad %uint %n\n" +
6781 "%2 = OpIAdd %int %uint_0 %3\n" +
6782 "OpReturn\n" +
6783 "OpFunctionEnd\n",
6784 2, true)
6785 ));
6786
6787 INSTANTIATE_TEST_SUITE_P(RedundantBitcastTest, MatchingInstructionFoldingTest,
6788 ::testing::Values(
6789 // Test case 0: uint32 x; asuint32(x) => x
6790 InstructionFoldingCase<bool>(
6791 Header() +
6792 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
6793 "; CHECK: %2 = OpCopyObject [[uint]] %3\n" +
6794 "%main = OpFunction %void None %void_func\n" +
6795 "%main_lab = OpLabel\n" +
6796 "%n = OpVariable %_ptr_uint Function\n" +
6797 "%3 = OpLoad %uint %n\n" +
6798 "%2 = OpBitcast %uint %3\n" +
6799 "OpReturn\n" +
6800 "OpFunctionEnd\n",
6801 2, true),
6802 // Test case 1: uint32 x; asuint32(asint32(x)) => x
6803 InstructionFoldingCase<bool>(
6804 Header() +
6805 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
6806 "; CHECK: %2 = OpCopyObject [[uint]] %3\n" +
6807 "%main = OpFunction %void None %void_func\n" +
6808 "%main_lab = OpLabel\n" +
6809 "%n = OpVariable %_ptr_uint Function\n" +
6810 "%3 = OpLoad %uint %n\n" +
6811 "%4 = OpBitcast %int %3\n" +
6812 "%2 = OpBitcast %uint %4\n" +
6813 "OpReturn\n" +
6814 "OpFunctionEnd\n",
6815 2, true),
6816 // Test case 2: float32 x; asuint32(asint32(x)) => asuint32(x)
6817 InstructionFoldingCase<bool>(
6818 Header() +
6819 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
6820 "; CHECK: %2 = OpBitcast [[uint]] %3\n" +
6821 "%main = OpFunction %void None %void_func\n" +
6822 "%main_lab = OpLabel\n" +
6823 "%n = OpVariable %_ptr_float Function\n" +
6824 "%3 = OpLoad %float %n\n" +
6825 "%4 = OpBitcast %int %3\n" +
6826 "%2 = OpBitcast %uint %4\n" +
6827 "OpReturn\n" +
6828 "OpFunctionEnd\n",
6829 2, true)
6830 ));
6831
6832 INSTANTIATE_TEST_SUITE_P(MergeNegateTest, MatchingInstructionFoldingTest,
6833 ::testing::Values(
6834 // Test case 0: fold consecutive fnegate
6835 // -(-x) = x
6836 InstructionFoldingCase<bool>(
6837 Header() +
6838 "; CHECK: [[ld:%\\w+]] = OpLoad [[float:%\\w+]]\n" +
6839 "; CHECK: %4 = OpCopyObject [[float]] [[ld]]\n" +
6840 "%main = OpFunction %void None %void_func\n" +
6841 "%main_lab = OpLabel\n" +
6842 "%var = OpVariable %_ptr_float Function\n" +
6843 "%2 = OpLoad %float %var\n" +
6844 "%3 = OpFNegate %float %2\n" +
6845 "%4 = OpFNegate %float %3\n" +
6846 "OpReturn\n" +
6847 "OpFunctionEnd",
6848 4, true),
6849 // Test case 1: fold fnegate(fmul with const).
6850 // -(x * 2.0) = x * -2.0
6851 InstructionFoldingCase<bool>(
6852 Header() +
6853 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
6854 "; CHECK: [[float_n2:%\\w+]] = OpConstant [[float]] -2{{[[:space:]]}}\n" +
6855 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
6856 "; CHECK: %4 = OpFMul [[float]] [[ld]] [[float_n2]]\n" +
6857 "%main = OpFunction %void None %void_func\n" +
6858 "%main_lab = OpLabel\n" +
6859 "%var = OpVariable %_ptr_float Function\n" +
6860 "%2 = OpLoad %float %var\n" +
6861 "%3 = OpFMul %float %2 %float_2\n" +
6862 "%4 = OpFNegate %float %3\n" +
6863 "OpReturn\n" +
6864 "OpFunctionEnd",
6865 4, true),
6866 // Test case 2: fold fnegate(fmul with const).
6867 // -(2.0 * x) = x * 2.0
6868 InstructionFoldingCase<bool>(
6869 Header() +
6870 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
6871 "; CHECK: [[float_n2:%\\w+]] = OpConstant [[float]] -2{{[[:space:]]}}\n" +
6872 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
6873 "; CHECK: %4 = OpFMul [[float]] [[ld]] [[float_n2]]\n" +
6874 "%main = OpFunction %void None %void_func\n" +
6875 "%main_lab = OpLabel\n" +
6876 "%var = OpVariable %_ptr_float Function\n" +
6877 "%2 = OpLoad %float %var\n" +
6878 "%3 = OpFMul %float %float_2 %2\n" +
6879 "%4 = OpFNegate %float %3\n" +
6880 "OpReturn\n" +
6881 "OpFunctionEnd",
6882 4, true),
6883 // Test case 3: fold fnegate(fdiv with const).
6884 // -(x / 2.0) = x * -0.5
6885 InstructionFoldingCase<bool>(
6886 Header() +
6887 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
6888 "; CHECK: [[float_n0p5:%\\w+]] = OpConstant [[float]] -0.5\n" +
6889 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
6890 "; CHECK: %4 = OpFMul [[float]] [[ld]] [[float_n0p5]]\n" +
6891 "%main = OpFunction %void None %void_func\n" +
6892 "%main_lab = OpLabel\n" +
6893 "%var = OpVariable %_ptr_float Function\n" +
6894 "%2 = OpLoad %float %var\n" +
6895 "%3 = OpFDiv %float %2 %float_2\n" +
6896 "%4 = OpFNegate %float %3\n" +
6897 "OpReturn\n" +
6898 "OpFunctionEnd",
6899 4, true),
6900 // Test case 4: fold fnegate(fdiv with const).
6901 // -(2.0 / x) = -2.0 / x
6902 InstructionFoldingCase<bool>(
6903 Header() +
6904 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
6905 "; CHECK: [[float_n2:%\\w+]] = OpConstant [[float]] -2{{[[:space:]]}}\n" +
6906 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
6907 "; CHECK: %4 = OpFDiv [[float]] [[float_n2]] [[ld]]\n" +
6908 "%main = OpFunction %void None %void_func\n" +
6909 "%main_lab = OpLabel\n" +
6910 "%var = OpVariable %_ptr_float Function\n" +
6911 "%2 = OpLoad %float %var\n" +
6912 "%3 = OpFDiv %float %float_2 %2\n" +
6913 "%4 = OpFNegate %float %3\n" +
6914 "OpReturn\n" +
6915 "OpFunctionEnd",
6916 4, true),
6917 // Test case 5: fold fnegate(fadd with const).
6918 // -(2.0 + x) = -2.0 - x
6919 InstructionFoldingCase<bool>(
6920 Header() +
6921 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
6922 "; CHECK: [[float_n2:%\\w+]] = OpConstant [[float]] -2{{[[:space:]]}}\n" +
6923 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
6924 "; CHECK: %4 = OpFSub [[float]] [[float_n2]] [[ld]]\n" +
6925 "%main = OpFunction %void None %void_func\n" +
6926 "%main_lab = OpLabel\n" +
6927 "%var = OpVariable %_ptr_float Function\n" +
6928 "%2 = OpLoad %float %var\n" +
6929 "%3 = OpFAdd %float %float_2 %2\n" +
6930 "%4 = OpFNegate %float %3\n" +
6931 "OpReturn\n" +
6932 "OpFunctionEnd",
6933 4, true),
6934 // Test case 6: fold fnegate(fadd with const).
6935 // -(x + 2.0) = -2.0 - x
6936 InstructionFoldingCase<bool>(
6937 Header() +
6938 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
6939 "; CHECK: [[float_n2:%\\w+]] = OpConstant [[float]] -2{{[[:space:]]}}\n" +
6940 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
6941 "; CHECK: %4 = OpFSub [[float]] [[float_n2]] [[ld]]\n" +
6942 "%main = OpFunction %void None %void_func\n" +
6943 "%main_lab = OpLabel\n" +
6944 "%var = OpVariable %_ptr_float Function\n" +
6945 "%2 = OpLoad %float %var\n" +
6946 "%3 = OpFAdd %float %2 %float_2\n" +
6947 "%4 = OpFNegate %float %3\n" +
6948 "OpReturn\n" +
6949 "OpFunctionEnd",
6950 4, true),
6951 // Test case 7: fold fnegate(fsub with const).
6952 // -(2.0 - x) = x - 2.0
6953 InstructionFoldingCase<bool>(
6954 Header() +
6955 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
6956 "; CHECK: [[float_2:%\\w+]] = OpConstant [[float]] 2\n" +
6957 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
6958 "; CHECK: %4 = OpFSub [[float]] [[ld]] [[float_2]]\n" +
6959 "%main = OpFunction %void None %void_func\n" +
6960 "%main_lab = OpLabel\n" +
6961 "%var = OpVariable %_ptr_float Function\n" +
6962 "%2 = OpLoad %float %var\n" +
6963 "%3 = OpFSub %float %float_2 %2\n" +
6964 "%4 = OpFNegate %float %3\n" +
6965 "OpReturn\n" +
6966 "OpFunctionEnd",
6967 4, true),
6968 // Test case 8: fold fnegate(fsub with const).
6969 // -(x - 2.0) = 2.0 - x
6970 InstructionFoldingCase<bool>(
6971 Header() +
6972 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
6973 "; CHECK: [[float_2:%\\w+]] = OpConstant [[float]] 2\n" +
6974 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
6975 "; CHECK: %4 = OpFSub [[float]] [[float_2]] [[ld]]\n" +
6976 "%main = OpFunction %void None %void_func\n" +
6977 "%main_lab = OpLabel\n" +
6978 "%var = OpVariable %_ptr_float Function\n" +
6979 "%2 = OpLoad %float %var\n" +
6980 "%3 = OpFSub %float %2 %float_2\n" +
6981 "%4 = OpFNegate %float %3\n" +
6982 "OpReturn\n" +
6983 "OpFunctionEnd",
6984 4, true),
6985 // Test case 9: fold consecutive snegate
6986 // -(-x) = x
6987 InstructionFoldingCase<bool>(
6988 Header() +
6989 "; CHECK: [[ld:%\\w+]] = OpLoad [[int:%\\w+]]\n" +
6990 "; CHECK: %4 = OpCopyObject [[int]] [[ld]]\n" +
6991 "%main = OpFunction %void None %void_func\n" +
6992 "%main_lab = OpLabel\n" +
6993 "%var = OpVariable %_ptr_int Function\n" +
6994 "%2 = OpLoad %int %var\n" +
6995 "%3 = OpSNegate %int %2\n" +
6996 "%4 = OpSNegate %int %3\n" +
6997 "OpReturn\n" +
6998 "OpFunctionEnd",
6999 4, true),
7000 // Test case 10: fold consecutive vector negate
7001 // -(-x) = x
7002 InstructionFoldingCase<bool>(
7003 Header() +
7004 "; CHECK: [[ld:%\\w+]] = OpLoad [[v2float:%\\w+]]\n" +
7005 "; CHECK: %4 = OpCopyObject [[v2float]] [[ld]]\n" +
7006 "%main = OpFunction %void None %void_func\n" +
7007 "%main_lab = OpLabel\n" +
7008 "%var = OpVariable %_ptr_v2float Function\n" +
7009 "%2 = OpLoad %v2float %var\n" +
7010 "%3 = OpFNegate %v2float %2\n" +
7011 "%4 = OpFNegate %v2float %3\n" +
7012 "OpReturn\n" +
7013 "OpFunctionEnd",
7014 4, true),
7015 // Test case 11: fold snegate(iadd with const).
7016 // -(2 + x) = -2 - x
7017 InstructionFoldingCase<bool>(
7018 Header() +
7019 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7020 "; CHECK: OpConstant [[int]] -2147483648\n" +
7021 "; CHECK: [[int_n2:%\\w+]] = OpConstant [[int]] -2{{[[:space:]]}}\n" +
7022 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7023 "; CHECK: %4 = OpISub [[int]] [[int_n2]] [[ld]]\n" +
7024 "%main = OpFunction %void None %void_func\n" +
7025 "%main_lab = OpLabel\n" +
7026 "%var = OpVariable %_ptr_int Function\n" +
7027 "%2 = OpLoad %int %var\n" +
7028 "%3 = OpIAdd %int %int_2 %2\n" +
7029 "%4 = OpSNegate %int %3\n" +
7030 "OpReturn\n" +
7031 "OpFunctionEnd",
7032 4, true),
7033 // Test case 12: fold snegate(iadd with const).
7034 // -(x + 2) = -2 - x
7035 InstructionFoldingCase<bool>(
7036 Header() +
7037 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7038 "; CHECK: OpConstant [[int]] -2147483648\n" +
7039 "; CHECK: [[int_n2:%\\w+]] = OpConstant [[int]] -2{{[[:space:]]}}\n" +
7040 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7041 "; CHECK: %4 = OpISub [[int]] [[int_n2]] [[ld]]\n" +
7042 "%main = OpFunction %void None %void_func\n" +
7043 "%main_lab = OpLabel\n" +
7044 "%var = OpVariable %_ptr_int Function\n" +
7045 "%2 = OpLoad %int %var\n" +
7046 "%3 = OpIAdd %int %2 %int_2\n" +
7047 "%4 = OpSNegate %int %3\n" +
7048 "OpReturn\n" +
7049 "OpFunctionEnd",
7050 4, true),
7051 // Test case 13: fold snegate(isub with const).
7052 // -(2 - x) = x - 2
7053 InstructionFoldingCase<bool>(
7054 Header() +
7055 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7056 "; CHECK: [[int_2:%\\w+]] = OpConstant [[int]] 2\n" +
7057 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7058 "; CHECK: %4 = OpISub [[int]] [[ld]] [[int_2]]\n" +
7059 "%main = OpFunction %void None %void_func\n" +
7060 "%main_lab = OpLabel\n" +
7061 "%var = OpVariable %_ptr_int Function\n" +
7062 "%2 = OpLoad %int %var\n" +
7063 "%3 = OpISub %int %int_2 %2\n" +
7064 "%4 = OpSNegate %int %3\n" +
7065 "OpReturn\n" +
7066 "OpFunctionEnd",
7067 4, true),
7068 // Test case 14: fold snegate(isub with const).
7069 // -(x - 2) = 2 - x
7070 InstructionFoldingCase<bool>(
7071 Header() +
7072 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7073 "; CHECK: [[int_2:%\\w+]] = OpConstant [[int]] 2\n" +
7074 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7075 "; CHECK: %4 = OpISub [[int]] [[int_2]] [[ld]]\n" +
7076 "%main = OpFunction %void None %void_func\n" +
7077 "%main_lab = OpLabel\n" +
7078 "%var = OpVariable %_ptr_int Function\n" +
7079 "%2 = OpLoad %int %var\n" +
7080 "%3 = OpISub %int %2 %int_2\n" +
7081 "%4 = OpSNegate %int %3\n" +
7082 "OpReturn\n" +
7083 "OpFunctionEnd",
7084 4, true),
7085 // Test case 15: fold snegate(iadd with const).
7086 // -(x + 2) = -2 - x
7087 InstructionFoldingCase<bool>(
7088 Header() +
7089 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
7090 "; CHECK: [[long_n2:%\\w+]] = OpConstant [[long]] -2{{[[:space:]]}}\n" +
7091 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
7092 "; CHECK: %4 = OpISub [[long]] [[long_n2]] [[ld]]\n" +
7093 "%main = OpFunction %void None %void_func\n" +
7094 "%main_lab = OpLabel\n" +
7095 "%var = OpVariable %_ptr_long Function\n" +
7096 "%2 = OpLoad %long %var\n" +
7097 "%3 = OpIAdd %long %2 %long_2\n" +
7098 "%4 = OpSNegate %long %3\n" +
7099 "OpReturn\n" +
7100 "OpFunctionEnd",
7101 4, true),
7102 // Test case 16: fold snegate(isub with const).
7103 // -(2 - x) = x - 2
7104 InstructionFoldingCase<bool>(
7105 Header() +
7106 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
7107 "; CHECK: [[long_2:%\\w+]] = OpConstant [[long]] 2\n" +
7108 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
7109 "; CHECK: %4 = OpISub [[long]] [[ld]] [[long_2]]\n" +
7110 "%main = OpFunction %void None %void_func\n" +
7111 "%main_lab = OpLabel\n" +
7112 "%var = OpVariable %_ptr_long Function\n" +
7113 "%2 = OpLoad %long %var\n" +
7114 "%3 = OpISub %long %long_2 %2\n" +
7115 "%4 = OpSNegate %long %3\n" +
7116 "OpReturn\n" +
7117 "OpFunctionEnd",
7118 4, true),
7119 // Test case 17: fold snegate(isub with const).
7120 // -(x - 2) = 2 - x
7121 InstructionFoldingCase<bool>(
7122 Header() +
7123 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
7124 "; CHECK: [[long_2:%\\w+]] = OpConstant [[long]] 2\n" +
7125 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
7126 "; CHECK: %4 = OpISub [[long]] [[long_2]] [[ld]]\n" +
7127 "%main = OpFunction %void None %void_func\n" +
7128 "%main_lab = OpLabel\n" +
7129 "%var = OpVariable %_ptr_long Function\n" +
7130 "%2 = OpLoad %long %var\n" +
7131 "%3 = OpISub %long %2 %long_2\n" +
7132 "%4 = OpSNegate %long %3\n" +
7133 "OpReturn\n" +
7134 "OpFunctionEnd",
7135 4, true),
7136 // Test case 18: fold -vec4(-1.0, 2.0, 1.0, 3.0)
7137 InstructionFoldingCase<bool>(
7138 Header() +
7139 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7140 "; CHECK: [[v4float:%\\w+]] = OpTypeVector [[float]] 4{{[[:space:]]}}\n" +
7141 "; CHECK: [[float_n1:%\\w+]] = OpConstant [[float]] -1{{[[:space:]]}}\n" +
7142 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1{{[[:space:]]}}\n" +
7143 "; CHECK: [[float_n2:%\\w+]] = OpConstant [[float]] -2{{[[:space:]]}}\n" +
7144 "; CHECK: [[float_n3:%\\w+]] = OpConstant [[float]] -3{{[[:space:]]}}\n" +
7145 "; CHECK: [[v4float_1_n2_n1_n3:%\\w+]] = OpConstantComposite [[v4float]] [[float_1]] [[float_n2]] [[float_n1]] [[float_n3]]\n" +
7146 "; CHECK: %2 = OpCopyObject [[v4float]] [[v4float_1_n2_n1_n3]]\n" +
7147 "%main = OpFunction %void None %void_func\n" +
7148 "%main_lab = OpLabel\n" +
7149 "%2 = OpFNegate %v4float %v4float_n1_2_1_3\n" +
7150 "OpReturn\n" +
7151 "OpFunctionEnd",
7152 2, true),
7153 // Test case 19: fold vector fnegate with null
7154 InstructionFoldingCase<bool>(
7155 Header() +
7156 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
7157 "; CHECK: [[v2double:%\\w+]] = OpTypeVector [[double]] 2\n" +
7158 "; CHECK: [[double_n0:%\\w+]] = OpConstant [[double]] -0\n" +
7159 "; CHECK: [[v2double_0_0:%\\w+]] = OpConstantComposite [[v2double]] [[double_n0]] [[double_n0]]\n" +
7160 "; CHECK: %2 = OpCopyObject [[v2double]] [[v2double_0_0]]\n" +
7161 "%main = OpFunction %void None %void_func\n" +
7162 "%main_lab = OpLabel\n" +
7163 "%2 = OpFNegate %v2double %v2double_null\n" +
7164 "OpReturn\n" +
7165 "OpFunctionEnd",
7166 2, true),
7167 // Test case 20: fold snegate with OpIMul.
7168 // -(x * 2) = x * -2
7169 InstructionFoldingCase<bool>(
7170 Header() +
7171 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
7172 "; CHECK: [[long_n2:%\\w+]] = OpConstant [[long]] -2\n" +
7173 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
7174 "; CHECK: %4 = OpIMul [[long]] [[ld]] [[long_n2]]\n" +
7175 "%main = OpFunction %void None %void_func\n" +
7176 "%main_lab = OpLabel\n" +
7177 "%var = OpVariable %_ptr_long Function\n" +
7178 "%2 = OpLoad %long %var\n" +
7179 "%3 = OpIMul %long %2 %long_2\n" +
7180 "%4 = OpSNegate %long %3\n" +
7181 "OpReturn\n" +
7182 "OpFunctionEnd",
7183 4, true),
7184 // Test case 21: fold snegate with OpIMul.
7185 // -(x * 2) = x * -2
7186 InstructionFoldingCase<bool>(
7187 Header() +
7188 "; CHECK-DAG: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7189 "; CHECK-DAG: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7190 "; CHECK: [[uint_n2:%\\w+]] = OpConstant [[uint]] 4294967294\n" +
7191 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7192 "; CHECK: %4 = OpIMul [[int]] [[ld]] [[uint_n2]]\n" +
7193 "%main = OpFunction %void None %void_func\n" +
7194 "%main_lab = OpLabel\n" +
7195 "%var = OpVariable %_ptr_int Function\n" +
7196 "%2 = OpLoad %int %var\n" +
7197 "%3 = OpIMul %int %2 %uint_2\n" +
7198 "%4 = OpSNegate %int %3\n" +
7199 "OpReturn\n" +
7200 "OpFunctionEnd",
7201 4, true),
7202 // Test case 22: fold snegate with OpIMul.
7203 // -(-24 * x) = x * 24
7204 InstructionFoldingCase<bool>(
7205 Header() +
7206 "; CHECK-DAG: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7207 "; CHECK: [[int_24:%\\w+]] = OpConstant [[int]] 24\n" +
7208 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7209 "; CHECK: %4 = OpIMul [[int]] [[ld]] [[int_24]]\n" +
7210 "%main = OpFunction %void None %void_func\n" +
7211 "%main_lab = OpLabel\n" +
7212 "%var = OpVariable %_ptr_int Function\n" +
7213 "%2 = OpLoad %int %var\n" +
7214 "%3 = OpIMul %int %int_n24 %2\n" +
7215 "%4 = OpSNegate %int %3\n" +
7216 "OpReturn\n" +
7217 "OpFunctionEnd",
7218 4, true),
7219 // Test case 23: fold snegate with OpIMul with UINT_MAX
7220 // -(UINT_MAX * x) = x
7221 InstructionFoldingCase<bool>(
7222 Header() +
7223 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7224 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7225 "; CHECK: %4 = OpCopyObject [[int]] [[ld]]\n" +
7226 "%main = OpFunction %void None %void_func\n" +
7227 "%main_lab = OpLabel\n" +
7228 "%var = OpVariable %_ptr_int Function\n" +
7229 "%2 = OpLoad %int %var\n" +
7230 "%3 = OpIMul %int %uint_max %2\n" +
7231 "%4 = OpSNegate %int %3\n" +
7232 "OpReturn\n" +
7233 "OpFunctionEnd",
7234 4, true),
7235 // Test case 24: fold snegate with OpIMul using -INT_MAX
7236 // -(x * 2147483649u) = x * 2147483647u
7237 InstructionFoldingCase<bool>(
7238 Header() +
7239 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7240 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7241 "; CHECK: [[uint_2147483647:%\\w+]] = OpConstant [[uint]] 2147483647\n" +
7242 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7243 "; CHECK: %4 = OpIMul [[int]] [[ld]] [[uint_2147483647]]\n" +
7244 "%main = OpFunction %void None %void_func\n" +
7245 "%main_lab = OpLabel\n" +
7246 "%var = OpVariable %_ptr_int Function\n" +
7247 "%2 = OpLoad %int %var\n" +
7248 "%3 = OpIMul %int %2 %uint_2147483649\n" +
7249 "%4 = OpSNegate %int %3\n" +
7250 "OpReturn\n" +
7251 "OpFunctionEnd",
7252 4, true),
7253 // Test case 25: fold snegate with OpSDiv (long).
7254 // -(x / 2) = x / -2
7255 InstructionFoldingCase<bool>(
7256 Header() +
7257 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
7258 "; CHECK: [[long_n2:%\\w+]] = OpConstant [[long]] -2\n" +
7259 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
7260 "; CHECK: %4 = OpSDiv [[long]] [[ld]] [[long_n2]]\n" +
7261 "%main = OpFunction %void None %void_func\n" +
7262 "%main_lab = OpLabel\n" +
7263 "%var = OpVariable %_ptr_long Function\n" +
7264 "%2 = OpLoad %long %var\n" +
7265 "%3 = OpSDiv %long %2 %long_2\n" +
7266 "%4 = OpSNegate %long %3\n" +
7267 "OpReturn\n" +
7268 "OpFunctionEnd",
7269 4, true),
7270 // Test case 26: fold snegate with OpSDiv (int).
7271 // -(x / 2) = x / -2
7272 InstructionFoldingCase<bool>(
7273 Header() +
7274 "; CHECK-DAG: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7275 "; CHECK-DAG: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7276 "; CHECK: [[uint_n2:%\\w+]] = OpConstant [[uint]] 4294967294\n" +
7277 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7278 "; CHECK: %4 = OpSDiv [[int]] [[ld]] [[uint_n2]]\n" +
7279 "%main = OpFunction %void None %void_func\n" +
7280 "%main_lab = OpLabel\n" +
7281 "%var = OpVariable %_ptr_int Function\n" +
7282 "%2 = OpLoad %int %var\n" +
7283 "%3 = OpSDiv %int %2 %uint_2\n" +
7284 "%4 = OpSNegate %int %3\n" +
7285 "OpReturn\n" +
7286 "OpFunctionEnd",
7287 4, true),
7288 // Test case 27: fold snegate with OpSDiv.
7289 // -(-24 / x) = 24 / x
7290 InstructionFoldingCase<bool>(
7291 Header() +
7292 "; CHECK-DAG: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7293 "; CHECK: [[int_24:%\\w+]] = OpConstant [[int]] 24\n" +
7294 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7295 "; CHECK: %4 = OpSDiv [[int]] [[int_24]] [[ld]]\n" +
7296 "%main = OpFunction %void None %void_func\n" +
7297 "%main_lab = OpLabel\n" +
7298 "%var = OpVariable %_ptr_int Function\n" +
7299 "%2 = OpLoad %int %var\n" +
7300 "%3 = OpSDiv %int %int_n24 %2\n" +
7301 "%4 = OpSNegate %int %3\n" +
7302 "OpReturn\n" +
7303 "OpFunctionEnd",
7304 4, true),
7305 // Test case 28: fold snegate with OpSDiv with UINT_MAX
7306 // -(UINT_MAX / x) = (1 / x)
7307 InstructionFoldingCase<bool>(
7308 Header() +
7309 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7310 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7311 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
7312 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7313 "; CHECK: %4 = OpSDiv [[int]] [[uint_1]] [[ld]]\n" +
7314 "%main = OpFunction %void None %void_func\n" +
7315 "%main_lab = OpLabel\n" +
7316 "%var = OpVariable %_ptr_int Function\n" +
7317 "%2 = OpLoad %int %var\n" +
7318 "%3 = OpSDiv %int %uint_max %2\n" +
7319 "%4 = OpSNegate %int %3\n" +
7320 "OpReturn\n" +
7321 "OpFunctionEnd",
7322 4, true),
7323 // Test case 29: fold snegate with OpSDiv using -INT_MAX
7324 // -(x / 2147483647u) = x / 2147483647
7325 InstructionFoldingCase<bool>(
7326 Header() +
7327 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7328 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7329 "; CHECK: [[uint_2147483647:%\\w+]] = OpConstant [[uint]] 2147483647\n" +
7330 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7331 "; CHECK: %4 = OpSDiv [[int]] [[ld]] [[uint_2147483647]]\n" +
7332 "%main = OpFunction %void None %void_func\n" +
7333 "%main_lab = OpLabel\n" +
7334 "%var = OpVariable %_ptr_int Function\n" +
7335 "%2 = OpLoad %int %var\n" +
7336 "%3 = OpSDiv %int %2 %uint_2147483649\n" +
7337 "%4 = OpSNegate %int %3\n" +
7338 "OpReturn\n" +
7339 "OpFunctionEnd",
7340 4, true),
7341 // Test case 30: Don't fold snegate int OpUDiv. The operands are interpreted
7342 // as unsigned, so negating an operand is not the same a negating the
7343 // result.
7344 InstructionFoldingCase<bool>(
7345 Header() +
7346 "%main = OpFunction %void None %void_func\n" +
7347 "%main_lab = OpLabel\n" +
7348 "%var = OpVariable %_ptr_int Function\n" +
7349 "%2 = OpLoad %int %var\n" +
7350 "%3 = OpUDiv %int %2 %uint_1\n" +
7351 "%4 = OpSNegate %int %3\n" +
7352 "OpReturn\n" +
7353 "OpFunctionEnd",
7354 4, false)
7355 ));
7356
7357 INSTANTIATE_TEST_SUITE_P(ReassociateCommutiveBitwiseTest, MatchingInstructionFoldingTest,
7358 ::testing::Values(
7359 // Test case 0: fold (n ^ 248) ^ 31 = n ^ 231
7360 InstructionFoldingCase<bool>(
7361 Header() +
7362 "%uint_248 = OpConstant %uint 248\n" +
7363 "%uint_31 = OpConstant %uint 31\n" +
7364 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7365 "; CHECK: [[uint_231:%\\w+]] = OpConstant [[uint]] 231\n" +
7366 "; CHECK: %2 = OpBitwiseXor [[uint]] %4 [[uint_231]]\n" +
7367 "%main = OpFunction %void None %void_func\n" +
7368 "%main_lab = OpLabel\n" +
7369 "%n = OpVariable %_ptr_uint Function\n" +
7370 "%4 = OpLoad %uint %n\n" +
7371 "%3 = OpBitwiseXor %uint %4 %uint_248\n" +
7372 "%2 = OpBitwiseXor %uint %3 %uint_31\n" +
7373 "OpReturn\n" +
7374 "OpFunctionEnd",
7375 2, true),
7376
7377 // Test case 1: fold 31 ^ (n ^ 248) = n ^ 231
7378 InstructionFoldingCase<bool>(
7379 Header() +
7380 "%uint_248 = OpConstant %uint 248\n" +
7381 "%uint_31 = OpConstant %uint 31\n" +
7382 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7383 "; CHECK: [[uint_231:%\\w+]] = OpConstant [[uint]] 231\n" +
7384 "; CHECK: %2 = OpBitwiseXor [[uint]] %4 [[uint_231]]\n" +
7385 "%main = OpFunction %void None %void_func\n" +
7386 "%main_lab = OpLabel\n" +
7387 "%n = OpVariable %_ptr_uint Function\n" +
7388 "%4 = OpLoad %uint %n\n" +
7389 "%3 = OpBitwiseXor %uint %4 %uint_248\n" +
7390 "%2 = OpBitwiseXor %uint %uint_31 %3\n" +
7391 "OpReturn\n" +
7392 "OpFunctionEnd",
7393 2, true),
7394
7395 // Test case 2: fold (248 ^ n) ^ 31 = n ^ 231
7396 InstructionFoldingCase<bool>(
7397 Header() +
7398 "%uint_248 = OpConstant %uint 248\n" +
7399 "%uint_31 = OpConstant %uint 31\n" +
7400 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7401 "; CHECK: [[uint_231:%\\w+]] = OpConstant [[uint]] 231\n" +
7402 "; CHECK: %2 = OpBitwiseXor [[uint]] %4 [[uint_231]]\n" +
7403 "%main = OpFunction %void None %void_func\n" +
7404 "%main_lab = OpLabel\n" +
7405 "%n = OpVariable %_ptr_uint Function\n" +
7406 "%4 = OpLoad %uint %n\n" +
7407 "%3 = OpBitwiseXor %uint %uint_248 %4\n" +
7408 "%2 = OpBitwiseXor %uint %3 %uint_31\n" +
7409 "OpReturn\n" +
7410 "OpFunctionEnd",
7411 2, true),
7412
7413 // Test case 3: fold 31 ^ (248 ^ n) = n ^ 231
7414 InstructionFoldingCase<bool>(
7415 Header() +
7416 "%uint_248 = OpConstant %uint 248\n" +
7417 "%uint_31 = OpConstant %uint 31\n" +
7418 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7419 "; CHECK: [[uint_231:%\\w+]] = OpConstant [[uint]] 231\n" +
7420 "; CHECK: %2 = OpBitwiseXor [[uint]] %4 [[uint_231]]\n" +
7421 "%main = OpFunction %void None %void_func\n" +
7422 "%main_lab = OpLabel\n" +
7423 "%n = OpVariable %_ptr_uint Function\n" +
7424 "%4 = OpLoad %uint %n\n" +
7425 "%3 = OpBitwiseXor %uint %uint_248 %4\n" +
7426 "%2 = OpBitwiseXor %uint %uint_31 %3\n" +
7427 "OpReturn\n" +
7428 "OpFunctionEnd",
7429 2, true),
7430
7431 // Test case 4: fold 3 | (n | 193) = n | 195
7432 InstructionFoldingCase<bool>(
7433 Header() +
7434 "%uint_193 = OpConstant %uint 193\n" +
7435 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7436 "; CHECK: [[uint_195:%\\w+]] = OpConstant [[uint]] 195\n" +
7437 "; CHECK: %2 = OpBitwiseOr [[uint]] %4 [[uint_195]]\n" +
7438 "%main = OpFunction %void None %void_func\n" +
7439 "%main_lab = OpLabel\n" +
7440 "%n = OpVariable %_ptr_uint Function\n" +
7441 "%4 = OpLoad %uint %n\n" +
7442 "%3 = OpBitwiseOr %uint %4 %uint_193\n" +
7443 "%2 = OpBitwiseOr %uint %uint_3 %3\n" +
7444 "OpReturn\n" +
7445 "OpFunctionEnd",
7446 2, true),
7447
7448 // Test case 5: fold (n | 193) | 3 = n | 195
7449 InstructionFoldingCase<bool>(
7450 Header() +
7451 "%uint_193 = OpConstant %uint 193\n" +
7452 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7453 "; CHECK: [[uint_195:%\\w+]] = OpConstant [[uint]] 195\n" +
7454 "; CHECK: %2 = OpBitwiseOr [[uint]] %4 [[uint_195]]\n" +
7455 "%main = OpFunction %void None %void_func\n" +
7456 "%main_lab = OpLabel\n" +
7457 "%n = OpVariable %_ptr_uint Function\n" +
7458 "%4 = OpLoad %uint %n\n" +
7459 "%3 = OpBitwiseOr %uint %4 %uint_193\n" +
7460 "%2 = OpBitwiseOr %uint %3 %uint_3\n" +
7461 "OpReturn\n" +
7462 "OpFunctionEnd",
7463 2, true),
7464
7465 // Test case 6: fold 3 | (193 | n) = n | 195
7466 InstructionFoldingCase<bool>(
7467 Header() +
7468 "%uint_193 = OpConstant %uint 193\n" +
7469 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7470 "; CHECK: [[uint_195:%\\w+]] = OpConstant [[uint]] 195\n" +
7471 "; CHECK: %2 = OpBitwiseOr [[uint]] %4 [[uint_195]]\n" +
7472 "%main = OpFunction %void None %void_func\n" +
7473 "%main_lab = OpLabel\n" +
7474 "%n = OpVariable %_ptr_uint Function\n" +
7475 "%4 = OpLoad %uint %n\n" +
7476 "%3 = OpBitwiseOr %uint %uint_193 %4\n" +
7477 "%2 = OpBitwiseOr %uint %uint_3 %3\n" +
7478 "OpReturn\n" +
7479 "OpFunctionEnd",
7480 2, true),
7481
7482 // Test case 7: fold (193 | n) | 3 = n | 195
7483 InstructionFoldingCase<bool>(
7484 Header() +
7485 "%uint_193 = OpConstant %uint 193\n" +
7486 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7487 "; CHECK: [[uint_195:%\\w+]] = OpConstant [[uint]] 195\n" +
7488 "; CHECK: %2 = OpBitwiseOr [[uint]] %4 [[uint_195]]\n" +
7489 "%main = OpFunction %void None %void_func\n" +
7490 "%main_lab = OpLabel\n" +
7491 "%n = OpVariable %_ptr_uint Function\n" +
7492 "%4 = OpLoad %uint %n\n" +
7493 "%3 = OpBitwiseOr %uint %uint_193 %4\n" +
7494 "%2 = OpBitwiseOr %uint %3 %uint_3\n" +
7495 "OpReturn\n" +
7496 "OpFunctionEnd",
7497 2, true),
7498
7499 // Test case 8: fold 65520 & (n & 4095) = n & 4080
7500 InstructionFoldingCase<bool>(
7501 Header() +
7502 "%uint_65520 = OpConstant %uint 65520\n" +
7503 "%uint_4095 = OpConstant %uint 4095\n" +
7504 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7505 "; CHECK: [[uint_4080:%\\w+]] = OpConstant [[uint]] 4080\n" +
7506 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_4080]]\n" +
7507 "%main = OpFunction %void None %void_func\n" +
7508 "%main_lab = OpLabel\n" +
7509 "%n = OpVariable %_ptr_uint Function\n" +
7510 "%4 = OpLoad %uint %n\n" +
7511 "%3 = OpBitwiseAnd %uint %4 %uint_4095\n" +
7512 "%2 = OpBitwiseAnd %uint %uint_65520 %3\n" +
7513 "OpReturn\n" +
7514 "OpFunctionEnd",
7515 2, true),
7516
7517 // Test case 9: fold (n & 4095) & 65520 = n & 4080
7518 InstructionFoldingCase<bool>(
7519 Header() +
7520 "%uint_65520 = OpConstant %uint 65520\n" +
7521 "%uint_4095 = OpConstant %uint 4095\n" +
7522 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7523 "; CHECK: [[uint_4080:%\\w+]] = OpConstant [[uint]] 4080\n" +
7524 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_4080]]\n" +
7525 "%main = OpFunction %void None %void_func\n" +
7526 "%main_lab = OpLabel\n" +
7527 "%n = OpVariable %_ptr_uint Function\n" +
7528 "%4 = OpLoad %uint %n\n" +
7529 "%3 = OpBitwiseAnd %uint %4 %uint_4095\n" +
7530 "%2 = OpBitwiseAnd %uint %3 %uint_65520\n" +
7531 "OpReturn\n" +
7532 "OpFunctionEnd",
7533 2, true),
7534
7535 // Test case 10: fold 65520 & (4095 & n) = n & 4080
7536 InstructionFoldingCase<bool>(
7537 Header() +
7538 "%uint_65520 = OpConstant %uint 65520\n" +
7539 "%uint_4095 = OpConstant %uint 4095\n" +
7540 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7541 "; CHECK: [[uint_4080:%\\w+]] = OpConstant [[uint]] 4080\n" +
7542 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_4080]]\n" +
7543 "%main = OpFunction %void None %void_func\n" +
7544 "%main_lab = OpLabel\n" +
7545 "%n = OpVariable %_ptr_uint Function\n" +
7546 "%4 = OpLoad %uint %n\n" +
7547 "%3 = OpBitwiseAnd %uint %uint_4095 %4\n" +
7548 "%2 = OpBitwiseAnd %uint %uint_65520 %3\n" +
7549 "OpReturn\n" +
7550 "OpFunctionEnd",
7551 2, true),
7552
7553 // Test case 11: fold (4095 & n) & 65520 = n & 4080
7554 InstructionFoldingCase<bool>(
7555 Header() +
7556 "%uint_65520 = OpConstant %uint 65520\n" +
7557 "%uint_4095 = OpConstant %uint 4095\n" +
7558 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
7559 "; CHECK: [[uint_4080:%\\w+]] = OpConstant [[uint]] 4080\n" +
7560 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_4080]]\n" +
7561 "%main = OpFunction %void None %void_func\n" +
7562 "%main_lab = OpLabel\n" +
7563 "%n = OpVariable %_ptr_uint Function\n" +
7564 "%4 = OpLoad %uint %n\n" +
7565 "%3 = OpBitwiseAnd %uint %uint_4095 %4\n" +
7566 "%2 = OpBitwiseAnd %uint %3 %uint_65520\n" +
7567 "OpReturn\n" +
7568 "OpFunctionEnd",
7569 2, true)
7570 ));
7571
7572 INSTANTIATE_TEST_SUITE_P(ReciprocalFDivTest, MatchingInstructionFoldingTest,
7573 ::testing::Values(
7574 // Test case 0: scalar reicprocal
7575 // x / 0.5 = x * 2.0
7576 InstructionFoldingCase<bool>(
7577 Header() +
7578 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7579 "; CHECK: [[float_2:%\\w+]] = OpConstant [[float]] 2\n" +
7580 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
7581 "; CHECK: %3 = OpFMul [[float]] [[ld]] [[float_2]]\n" +
7582 "%main = OpFunction %void None %void_func\n" +
7583 "%main_lab = OpLabel\n" +
7584 "%var = OpVariable %_ptr_float Function\n" +
7585 "%2 = OpLoad %float %var\n" +
7586 "%3 = OpFDiv %float %2 %float_0p5\n" +
7587 "OpReturn\n" +
7588 "OpFunctionEnd\n",
7589 3, true),
7590 // Test case 1: Unfoldable
7591 InstructionFoldingCase<bool>(
7592 Header() +
7593 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7594 "; CHECK: [[float_0:%\\w+]] = OpConstant [[float]] 0\n" +
7595 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
7596 "; CHECK: %3 = OpFDiv [[float]] [[ld]] [[float_0]]\n" +
7597 "%main = OpFunction %void None %void_func\n" +
7598 "%main_lab = OpLabel\n" +
7599 "%var = OpVariable %_ptr_float Function\n" +
7600 "%2 = OpLoad %float %var\n" +
7601 "%3 = OpFDiv %float %2 %104\n" +
7602 "OpReturn\n" +
7603 "OpFunctionEnd\n",
7604 3, false),
7605 // Test case 2: Vector reciprocal
7606 // x / {2.0, 0.5} = x * {0.5, 2.0}
7607 InstructionFoldingCase<bool>(
7608 Header() +
7609 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7610 "; CHECK: [[v2float:%\\w+]] = OpTypeVector [[float]] 2\n" +
7611 "; CHECK: [[float_2:%\\w+]] = OpConstant [[float]] 2\n" +
7612 "; CHECK: [[float_0p5:%\\w+]] = OpConstant [[float]] 0.5\n" +
7613 "; CHECK: [[v2float_0p5_2:%\\w+]] = OpConstantComposite [[v2float]] [[float_0p5]] [[float_2]]\n" +
7614 "; CHECK: [[ld:%\\w+]] = OpLoad [[v2float]]\n" +
7615 "; CHECK: %3 = OpFMul [[v2float]] [[ld]] [[v2float_0p5_2]]\n" +
7616 "%main = OpFunction %void None %void_func\n" +
7617 "%main_lab = OpLabel\n" +
7618 "%var = OpVariable %_ptr_v2float Function\n" +
7619 "%2 = OpLoad %v2float %var\n" +
7620 "%3 = OpFDiv %v2float %2 %v2float_2_0p5\n" +
7621 "OpReturn\n" +
7622 "OpFunctionEnd\n",
7623 3, true),
7624 // Test case 3: double reciprocal
7625 // x / 2.0 = x * 0.5
7626 InstructionFoldingCase<bool>(
7627 Header() +
7628 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
7629 "; CHECK: [[double_0p5:%\\w+]] = OpConstant [[double]] 0.5\n" +
7630 "; CHECK: [[ld:%\\w+]] = OpLoad [[double]]\n" +
7631 "; CHECK: %3 = OpFMul [[double]] [[ld]] [[double_0p5]]\n" +
7632 "%main = OpFunction %void None %void_func\n" +
7633 "%main_lab = OpLabel\n" +
7634 "%var = OpVariable %_ptr_double Function\n" +
7635 "%2 = OpLoad %double %var\n" +
7636 "%3 = OpFDiv %double %2 %double_2\n" +
7637 "OpReturn\n" +
7638 "OpFunctionEnd\n",
7639 3, true),
7640 // Test case 4: don't fold x / 0.
7641 InstructionFoldingCase<bool>(
7642 Header() +
7643 "%main = OpFunction %void None %void_func\n" +
7644 "%main_lab = OpLabel\n" +
7645 "%var = OpVariable %_ptr_v2float Function\n" +
7646 "%2 = OpLoad %v2float %var\n" +
7647 "%3 = OpFDiv %v2float %2 %v2float_null\n" +
7648 "OpReturn\n" +
7649 "OpFunctionEnd\n",
7650 3, false)
7651 ));
7652
7653 INSTANTIATE_TEST_SUITE_P(MergeMulTest, MatchingInstructionFoldingTest,
7654 ::testing::Values(
7655 // Test case 0: fold consecutive fmuls
7656 // (x * 3.0) * 2.0 = x * 6.0
7657 InstructionFoldingCase<bool>(
7658 Header() +
7659 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7660 "; CHECK: [[float_6:%\\w+]] = OpConstant [[float]] 6\n" +
7661 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
7662 "; CHECK: %4 = OpFMul [[float]] [[ld]] [[float_6]]\n" +
7663 "%main = OpFunction %void None %void_func\n" +
7664 "%main_lab = OpLabel\n" +
7665 "%var = OpVariable %_ptr_float Function\n" +
7666 "%2 = OpLoad %float %var\n" +
7667 "%3 = OpFMul %float %2 %float_3\n" +
7668 "%4 = OpFMul %float %3 %float_2\n" +
7669 "OpReturn\n" +
7670 "OpFunctionEnd\n",
7671 4, true),
7672 // Test case 1: fold consecutive fmuls
7673 // 2.0 * (x * 3.0) = x * 6.0
7674 InstructionFoldingCase<bool>(
7675 Header() +
7676 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7677 "; CHECK: [[float_6:%\\w+]] = OpConstant [[float]] 6\n" +
7678 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
7679 "; CHECK: %4 = OpFMul [[float]] [[ld]] [[float_6]]\n" +
7680 "%main = OpFunction %void None %void_func\n" +
7681 "%main_lab = OpLabel\n" +
7682 "%var = OpVariable %_ptr_float Function\n" +
7683 "%2 = OpLoad %float %var\n" +
7684 "%3 = OpFMul %float %2 %float_3\n" +
7685 "%4 = OpFMul %float %float_2 %3\n" +
7686 "OpReturn\n" +
7687 "OpFunctionEnd\n",
7688 4, true),
7689 // Test case 2: fold consecutive fmuls
7690 // (3.0 * x) * 2.0 = x * 6.0
7691 InstructionFoldingCase<bool>(
7692 Header() +
7693 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7694 "; CHECK: [[float_6:%\\w+]] = OpConstant [[float]] 6\n" +
7695 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
7696 "; CHECK: %4 = OpFMul [[float]] [[ld]] [[float_6]]\n" +
7697 "%main = OpFunction %void None %void_func\n" +
7698 "%main_lab = OpLabel\n" +
7699 "%var = OpVariable %_ptr_float Function\n" +
7700 "%2 = OpLoad %float %var\n" +
7701 "%3 = OpFMul %float %float_3 %2\n" +
7702 "%4 = OpFMul %float %float_2 %3\n" +
7703 "OpReturn\n" +
7704 "OpFunctionEnd\n",
7705 4, true),
7706 // Test case 3: fold vector fmul
7707 InstructionFoldingCase<bool>(
7708 Header() +
7709 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7710 "; CHECK: [[v2float:%\\w+]] = OpTypeVector [[float]] 2\n" +
7711 "; CHECK: [[float_6:%\\w+]] = OpConstant [[float]] 6\n" +
7712 "; CHECK: [[v2float_6_6:%\\w+]] = OpConstantComposite [[v2float]] [[float_6]] [[float_6]]\n" +
7713 "; CHECK: [[ld:%\\w+]] = OpLoad [[v2float]]\n" +
7714 "; CHECK: %4 = OpFMul [[v2float]] [[ld]] [[v2float_6_6]]\n" +
7715 "%main = OpFunction %void None %void_func\n" +
7716 "%main_lab = OpLabel\n" +
7717 "%var = OpVariable %_ptr_v2float Function\n" +
7718 "%2 = OpLoad %v2float %var\n" +
7719 "%3 = OpFMul %v2float %2 %v2float_2_3\n" +
7720 "%4 = OpFMul %v2float %3 %v2float_3_2\n" +
7721 "OpReturn\n" +
7722 "OpFunctionEnd\n",
7723 4, true),
7724 // Test case 4: fold double fmuls
7725 // (x * 3.0) * 2.0 = x * 6.0
7726 InstructionFoldingCase<bool>(
7727 Header() +
7728 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
7729 "; CHECK: [[double_6:%\\w+]] = OpConstant [[double]] 6\n" +
7730 "; CHECK: [[ld:%\\w+]] = OpLoad [[double]]\n" +
7731 "; CHECK: %4 = OpFMul [[double]] [[ld]] [[double_6]]\n" +
7732 "%main = OpFunction %void None %void_func\n" +
7733 "%main_lab = OpLabel\n" +
7734 "%var = OpVariable %_ptr_double Function\n" +
7735 "%2 = OpLoad %double %var\n" +
7736 "%3 = OpFMul %double %2 %double_3\n" +
7737 "%4 = OpFMul %double %3 %double_2\n" +
7738 "OpReturn\n" +
7739 "OpFunctionEnd\n",
7740 4, true),
7741 // Test case 5: fold 32 bit imuls
7742 // (x * 3) * 2 = x * 6
7743 InstructionFoldingCase<bool>(
7744 Header() +
7745 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7746 "; CHECK: [[int_6:%\\w+]] = OpConstant [[int]] 6\n" +
7747 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
7748 "; CHECK: %4 = OpIMul [[int]] [[ld]] [[int_6]]\n" +
7749 "%main = OpFunction %void None %void_func\n" +
7750 "%main_lab = OpLabel\n" +
7751 "%var = OpVariable %_ptr_int Function\n" +
7752 "%2 = OpLoad %int %var\n" +
7753 "%3 = OpIMul %int %2 %int_3\n" +
7754 "%4 = OpIMul %int %3 %int_2\n" +
7755 "OpReturn\n" +
7756 "OpFunctionEnd\n",
7757 4, true),
7758 // Test case 6: fold 64 bit imuls
7759 // (x * 3) * 2 = x * 6
7760 InstructionFoldingCase<bool>(
7761 Header() +
7762 "; CHECK: [[long:%\\w+]] = OpTypeInt 64\n" +
7763 "; CHECK: [[long_6:%\\w+]] = OpConstant [[long]] 6\n" +
7764 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
7765 "; CHECK: %4 = OpIMul [[long]] [[ld]] [[long_6]]\n" +
7766 "%main = OpFunction %void None %void_func\n" +
7767 "%main_lab = OpLabel\n" +
7768 "%var = OpVariable %_ptr_long Function\n" +
7769 "%2 = OpLoad %long %var\n" +
7770 "%3 = OpIMul %long %2 %long_3\n" +
7771 "%4 = OpIMul %long %3 %long_2\n" +
7772 "OpReturn\n" +
7773 "OpFunctionEnd\n",
7774 4, true),
7775 // Test case 7: merge vector integer mults
7776 InstructionFoldingCase<bool>(
7777 Header() +
7778 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7779 "; CHECK: [[v2int:%\\w+]] = OpTypeVector [[int]] 2\n" +
7780 "; CHECK: [[int_6:%\\w+]] = OpConstant [[int]] 6\n" +
7781 "; CHECK: [[v2int_6_6:%\\w+]] = OpConstantComposite [[v2int]] [[int_6]] [[int_6]]\n" +
7782 "; CHECK: [[ld:%\\w+]] = OpLoad [[v2int]]\n" +
7783 "; CHECK: %4 = OpIMul [[v2int]] [[ld]] [[v2int_6_6]]\n" +
7784 "%main = OpFunction %void None %void_func\n" +
7785 "%main_lab = OpLabel\n" +
7786 "%var = OpVariable %_ptr_v2int Function\n" +
7787 "%2 = OpLoad %v2int %var\n" +
7788 "%3 = OpIMul %v2int %2 %v2int_2_3\n" +
7789 "%4 = OpIMul %v2int %3 %v2int_3_2\n" +
7790 "OpReturn\n" +
7791 "OpFunctionEnd\n",
7792 4, true),
7793 // Test case 8: merge fmul of fdiv
7794 // 2.0 * (2.0 / x) = 4.0 / x
7795 InstructionFoldingCase<bool>(
7796 Header() +
7797 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7798 "; CHECK: [[float_4:%\\w+]] = OpConstant [[float]] 4\n" +
7799 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
7800 "; CHECK: %4 = OpFDiv [[float]] [[float_4]] [[ld]]\n" +
7801 "%main = OpFunction %void None %void_func\n" +
7802 "%main_lab = OpLabel\n" +
7803 "%var = OpVariable %_ptr_float Function\n" +
7804 "%2 = OpLoad %float %var\n" +
7805 "%3 = OpFDiv %float %float_2 %2\n" +
7806 "%4 = OpFMul %float %float_2 %3\n" +
7807 "OpReturn\n" +
7808 "OpFunctionEnd\n",
7809 4, true),
7810 // Test case 9: merge fmul of fdiv
7811 // (2.0 / x) * 2.0 = 4.0 / x
7812 InstructionFoldingCase<bool>(
7813 Header() +
7814 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7815 "; CHECK: [[float_4:%\\w+]] = OpConstant [[float]] 4\n" +
7816 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
7817 "; CHECK: %4 = OpFDiv [[float]] [[float_4]] [[ld]]\n" +
7818 "%main = OpFunction %void None %void_func\n" +
7819 "%main_lab = OpLabel\n" +
7820 "%var = OpVariable %_ptr_float Function\n" +
7821 "%2 = OpLoad %float %var\n" +
7822 "%3 = OpFDiv %float %float_2 %2\n" +
7823 "%4 = OpFMul %float %3 %float_2\n" +
7824 "OpReturn\n" +
7825 "OpFunctionEnd\n",
7826 4, true),
7827 // Test case 10: Do not merge imul of sdiv
7828 // 4 * (x / 2)
7829 InstructionFoldingCase<bool>(
7830 Header() +
7831 "%main = OpFunction %void None %void_func\n" +
7832 "%main_lab = OpLabel\n" +
7833 "%var = OpVariable %_ptr_int Function\n" +
7834 "%2 = OpLoad %int %var\n" +
7835 "%3 = OpSDiv %int %2 %int_2\n" +
7836 "%4 = OpIMul %int %int_4 %3\n" +
7837 "OpReturn\n" +
7838 "OpFunctionEnd\n",
7839 4, false),
7840 // Test case 11: Do not merge imul of sdiv
7841 // (x / 2) * 4
7842 InstructionFoldingCase<bool>(
7843 Header() +
7844 "%main = OpFunction %void None %void_func\n" +
7845 "%main_lab = OpLabel\n" +
7846 "%var = OpVariable %_ptr_int Function\n" +
7847 "%2 = OpLoad %int %var\n" +
7848 "%3 = OpSDiv %int %2 %int_2\n" +
7849 "%4 = OpIMul %int %3 %int_4\n" +
7850 "OpReturn\n" +
7851 "OpFunctionEnd\n",
7852 4, false),
7853 // Test case 12: Do not merge imul of udiv
7854 // 4 * (x / 2)
7855 InstructionFoldingCase<bool>(
7856 Header() +
7857 "%main = OpFunction %void None %void_func\n" +
7858 "%main_lab = OpLabel\n" +
7859 "%var = OpVariable %_ptr_uint Function\n" +
7860 "%2 = OpLoad %uint %var\n" +
7861 "%3 = OpUDiv %uint %2 %uint_2\n" +
7862 "%4 = OpIMul %uint %uint_4 %3\n" +
7863 "OpReturn\n" +
7864 "OpFunctionEnd\n",
7865 4, false),
7866 // Test case 13: Do not merge imul of udiv
7867 // (x / 2) * 4
7868 InstructionFoldingCase<bool>(
7869 Header() +
7870 "%main = OpFunction %void None %void_func\n" +
7871 "%main_lab = OpLabel\n" +
7872 "%var = OpVariable %_ptr_uint Function\n" +
7873 "%2 = OpLoad %uint %var\n" +
7874 "%3 = OpUDiv %uint %2 %uint_2\n" +
7875 "%4 = OpIMul %uint %3 %uint_4\n" +
7876 "OpReturn\n" +
7877 "OpFunctionEnd\n",
7878 4, false),
7879 // Test case 14: Don't fold
7880 // (x / 3) * 4
7881 InstructionFoldingCase<bool>(
7882 Header() +
7883 "%main = OpFunction %void None %void_func\n" +
7884 "%main_lab = OpLabel\n" +
7885 "%var = OpVariable %_ptr_uint Function\n" +
7886 "%2 = OpLoad %uint %var\n" +
7887 "%3 = OpUDiv %uint %2 %uint_3\n" +
7888 "%4 = OpIMul %uint %3 %uint_4\n" +
7889 "OpReturn\n" +
7890 "OpFunctionEnd\n",
7891 4, false),
7892 // Test case 15: merge vector fmul of fdiv
7893 // (x / {2,2}) * {4,4} = x * {2,2}
7894 InstructionFoldingCase<bool>(
7895 Header() +
7896 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7897 "; CHECK: [[v2float:%\\w+]] = OpTypeVector [[float]] 2\n" +
7898 "; CHECK: [[float_2:%\\w+]] = OpConstant [[float]] 2\n" +
7899 "; CHECK: [[v2float_2_2:%\\w+]] = OpConstantComposite [[v2float]] [[float_2]] [[float_2]]\n" +
7900 "; CHECK: [[ld:%\\w+]] = OpLoad [[v2float]]\n" +
7901 "; CHECK: %4 = OpFMul [[v2float]] [[ld]] [[v2float_2_2]]\n" +
7902 "%main = OpFunction %void None %void_func\n" +
7903 "%main_lab = OpLabel\n" +
7904 "%var = OpVariable %_ptr_v2float Function\n" +
7905 "%2 = OpLoad %v2float %var\n" +
7906 "%3 = OpFDiv %v2float %2 %v2float_2_2\n" +
7907 "%4 = OpFMul %v2float %3 %v2float_4_4\n" +
7908 "OpReturn\n" +
7909 "OpFunctionEnd\n",
7910 4, true),
7911 // Test case 16: merge vector imul of snegate
7912 // (-x) * {2,2} = x * {-2,-2}
7913 InstructionFoldingCase<bool>(
7914 Header() +
7915 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7916 "; CHECK: [[v2int:%\\w+]] = OpTypeVector [[int]] 2{{[[:space:]]}}\n" +
7917 "; CHECK: OpConstant [[int]] -2147483648{{[[:space:]]}}\n" +
7918 "; CHECK: [[int_n2:%\\w+]] = OpConstant [[int]] -2{{[[:space:]]}}\n" +
7919 "; CHECK: [[v2int_n2_n2:%\\w+]] = OpConstantComposite [[v2int]] [[int_n2]] [[int_n2]]\n" +
7920 "; CHECK: [[ld:%\\w+]] = OpLoad [[v2int]]\n" +
7921 "; CHECK: %4 = OpIMul [[v2int]] [[ld]] [[v2int_n2_n2]]\n" +
7922 "%main = OpFunction %void None %void_func\n" +
7923 "%main_lab = OpLabel\n" +
7924 "%var = OpVariable %_ptr_v2int Function\n" +
7925 "%2 = OpLoad %v2int %var\n" +
7926 "%3 = OpSNegate %v2int %2\n" +
7927 "%4 = OpIMul %v2int %3 %v2int_2_2\n" +
7928 "OpReturn\n" +
7929 "OpFunctionEnd\n",
7930 4, true),
7931 // Test case 17: merge vector imul of snegate
7932 // {2,2} * (-x) = x * {-2,-2}
7933 InstructionFoldingCase<bool>(
7934 Header() +
7935 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
7936 "; CHECK: [[v2int:%\\w+]] = OpTypeVector [[int]] 2{{[[:space:]]}}\n" +
7937 "; CHECK: OpConstant [[int]] -2147483648{{[[:space:]]}}\n" +
7938 "; CHECK: [[int_n2:%\\w+]] = OpConstant [[int]] -2{{[[:space:]]}}\n" +
7939 "; CHECK: [[v2int_n2_n2:%\\w+]] = OpConstantComposite [[v2int]] [[int_n2]] [[int_n2]]\n" +
7940 "; CHECK: [[ld:%\\w+]] = OpLoad [[v2int]]\n" +
7941 "; CHECK: %4 = OpIMul [[v2int]] [[ld]] [[v2int_n2_n2]]\n" +
7942 "%main = OpFunction %void None %void_func\n" +
7943 "%main_lab = OpLabel\n" +
7944 "%var = OpVariable %_ptr_v2int Function\n" +
7945 "%2 = OpLoad %v2int %var\n" +
7946 "%3 = OpSNegate %v2int %2\n" +
7947 "%4 = OpIMul %v2int %v2int_2_2 %3\n" +
7948 "OpReturn\n" +
7949 "OpFunctionEnd\n",
7950 4, true),
7951 // Test case 18: Fold OpVectorTimesScalar
7952 // {4,4} = OpVectorTimesScalar v2float {2,2} 2
7953 InstructionFoldingCase<bool>(
7954 Header() +
7955 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7956 "; CHECK: [[v2float:%\\w+]] = OpTypeVector [[float]] 2\n" +
7957 "; CHECK: [[float_4:%\\w+]] = OpConstant [[float]] 4\n" +
7958 "; CHECK: [[v2float_4_4:%\\w+]] = OpConstantComposite [[v2float]] [[float_4]] [[float_4]]\n" +
7959 "; CHECK: %2 = OpCopyObject [[v2float]] [[v2float_4_4]]\n" +
7960 "%main = OpFunction %void None %void_func\n" +
7961 "%main_lab = OpLabel\n" +
7962 "%2 = OpVectorTimesScalar %v2float %v2float_2_2 %float_2\n" +
7963 "OpReturn\n" +
7964 "OpFunctionEnd",
7965 2, true),
7966 // Test case 19: Fold OpVectorTimesScalar
7967 // {0,0} = OpVectorTimesScalar v2float v2float_null -1
7968 InstructionFoldingCase<bool>(
7969 Header() +
7970 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
7971 "; CHECK: [[v2float:%\\w+]] = OpTypeVector [[float]] 2\n" +
7972 "; CHECK: [[v2float_null:%\\w+]] = OpConstantNull [[v2float]]\n" +
7973 "; CHECK: %2 = OpCopyObject [[v2float]] [[v2float_null]]\n" +
7974 "%main = OpFunction %void None %void_func\n" +
7975 "%main_lab = OpLabel\n" +
7976 "%2 = OpVectorTimesScalar %v2float %v2float_null %float_n1\n" +
7977 "OpReturn\n" +
7978 "OpFunctionEnd",
7979 2, true),
7980 // Test case 20: Fold OpVectorTimesScalar
7981 // {4,4} = OpVectorTimesScalar v2double {2,2} 2
7982 InstructionFoldingCase<bool>(
7983 Header() +
7984 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
7985 "; CHECK: [[v2double:%\\w+]] = OpTypeVector [[double]] 2\n" +
7986 "; CHECK: [[double_4:%\\w+]] = OpConstant [[double]] 4\n" +
7987 "; CHECK: [[v2double_4_4:%\\w+]] = OpConstantComposite [[v2double]] [[double_4]] [[double_4]]\n" +
7988 "; CHECK: %2 = OpCopyObject [[v2double]] [[v2double_4_4]]\n" +
7989 "%main = OpFunction %void None %void_func\n" +
7990 "%main_lab = OpLabel\n" +
7991 "%2 = OpVectorTimesScalar %v2double %v2double_2_2 %double_2\n" +
7992 "OpReturn\n" +
7993 "OpFunctionEnd",
7994 2, true),
7995 // Test case 21: Fold OpVectorTimesScalar
7996 // {0,0} = OpVectorTimesScalar v2double {0,0} n
7997 InstructionFoldingCase<bool>(
7998 Header() +
7999 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
8000 "; CHECK: [[v2double:%\\w+]] = OpTypeVector [[double]] 2\n" +
8001 "; CHECK: {{%\\w+}} = OpConstant [[double]] 0\n" +
8002 "; CHECK: [[double_0:%\\w+]] = OpConstant [[double]] 0\n" +
8003 "; CHECK: [[v2double_0_0:%\\w+]] = OpConstantComposite [[v2double]] [[double_0]] [[double_0]]\n" +
8004 "; CHECK: %2 = OpCopyObject [[v2double]] [[v2double_0_0]]\n" +
8005 "%main = OpFunction %void None %void_func\n" +
8006 "%main_lab = OpLabel\n" +
8007 "%n = OpVariable %_ptr_double Function\n" +
8008 "%load = OpLoad %double %n\n" +
8009 "%2 = OpVectorTimesScalar %v2double %v2double_0_0 %load\n" +
8010 "OpReturn\n" +
8011 "OpFunctionEnd",
8012 2, true),
8013 // Test case 22: Fold OpVectorTimesScalar
8014 // {0,0} = OpVectorTimesScalar v2double n 0
8015 InstructionFoldingCase<bool>(
8016 Header() +
8017 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
8018 "; CHECK: [[v2double:%\\w+]] = OpTypeVector [[double]] 2\n" +
8019 "; CHECK: [[v2double_null:%\\w+]] = OpConstantNull [[v2double]]\n" +
8020 "; CHECK: %2 = OpCopyObject [[v2double]] [[v2double_null]]\n" +
8021 "%main = OpFunction %void None %void_func\n" +
8022 "%main_lab = OpLabel\n" +
8023 "%n = OpVariable %_ptr_v2double Function\n" +
8024 "%load = OpLoad %v2double %n\n" +
8025 "%2 = OpVectorTimesScalar %v2double %load %double_0\n" +
8026 "OpReturn\n" +
8027 "OpFunctionEnd",
8028 2, true),
8029 // Test case 23: merge fmul of fdiv
8030 // x * (y / x) = y
8031 InstructionFoldingCase<bool>(
8032 Header() +
8033 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8034 "; CHECK: [[ldx:%\\w+]] = OpLoad [[float]]\n" +
8035 "; CHECK: [[ldy:%\\w+]] = OpLoad [[float]] [[y:%\\w+]]\n" +
8036 "; CHECK: %5 = OpCopyObject [[float]] [[ldy]]\n" +
8037 "%main = OpFunction %void None %void_func\n" +
8038 "%main_lab = OpLabel\n" +
8039 "%x = OpVariable %_ptr_float Function\n" +
8040 "%y = OpVariable %_ptr_float Function\n" +
8041 "%2 = OpLoad %float %x\n" +
8042 "%3 = OpLoad %float %y\n" +
8043 "%4 = OpFDiv %float %3 %2\n" +
8044 "%5 = OpFMul %float %2 %4\n" +
8045 "OpReturn\n" +
8046 "OpFunctionEnd\n",
8047 5, true),
8048 // Test case 24: merge fmul of fdiv
8049 // (y / x) * x = y
8050 InstructionFoldingCase<bool>(
8051 Header() +
8052 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8053 "; CHECK: [[ldx:%\\w+]] = OpLoad [[float]]\n" +
8054 "; CHECK: [[ldy:%\\w+]] = OpLoad [[float]] [[y:%\\w+]]\n" +
8055 "; CHECK: %5 = OpCopyObject [[float]] [[ldy]]\n" +
8056 "%main = OpFunction %void None %void_func\n" +
8057 "%main_lab = OpLabel\n" +
8058 "%x = OpVariable %_ptr_float Function\n" +
8059 "%y = OpVariable %_ptr_float Function\n" +
8060 "%2 = OpLoad %float %x\n" +
8061 "%3 = OpLoad %float %y\n" +
8062 "%4 = OpFDiv %float %3 %2\n" +
8063 "%5 = OpFMul %float %4 %2\n" +
8064 "OpReturn\n" +
8065 "OpFunctionEnd\n",
8066 5, true),
8067 // Test case 25: fold overflowing signed 32 bit imuls
8068 // (x * 1073741824) * 2 = x * int_min
8069 InstructionFoldingCase<bool>(
8070 Header() +
8071 "; CHECK: [[int:%\\w+]] = OpTypeInt 32\n" +
8072 "; CHECK: [[int_min:%\\w+]] = OpConstant [[int]] -2147483648\n" +
8073 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
8074 "; CHECK: %4 = OpIMul [[int]] [[ld]] [[int_min]]\n" +
8075 "%main = OpFunction %void None %void_func\n" +
8076 "%main_lab = OpLabel\n" +
8077 "%var = OpVariable %_ptr_int Function\n" +
8078 "%2 = OpLoad %int %var\n" +
8079 "%3 = OpIMul %int %2 %int_1073741824\n" +
8080 "%4 = OpIMul %int %3 %int_2\n" +
8081 "OpReturn\n" +
8082 "OpFunctionEnd\n",
8083 4, true),
8084 // Test case 26: fold overflowing signed 64 bit imuls
8085 // (x * 4611686018427387904) * 2 = x * long_min
8086 InstructionFoldingCase<bool>(
8087 Header() +
8088 "; CHECK: [[long:%\\w+]] = OpTypeInt 64\n" +
8089 "; CHECK: [[long_min:%\\w+]] = OpConstant [[long]] -9223372036854775808\n" +
8090 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
8091 "; CHECK: %4 = OpIMul [[long]] [[ld]] [[long_min]]\n" +
8092 "%main = OpFunction %void None %void_func\n" +
8093 "%main_lab = OpLabel\n" +
8094 "%var = OpVariable %_ptr_long Function\n" +
8095 "%2 = OpLoad %long %var\n" +
8096 "%3 = OpIMul %long %2 %long_4611686018427387904\n" +
8097 "%4 = OpIMul %long %3 %long_2\n" +
8098 "OpReturn\n" +
8099 "OpFunctionEnd\n",
8100 4, true),
8101 // Test case 27: fold overflowing 32 bit unsigned imuls
8102 // (x * 2147483649) * 2 = x * 2
8103 InstructionFoldingCase<bool>(
8104 Header() +
8105 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8106 "; CHECK: [[uint_2:%\\w+]] = OpConstant [[uint]] 2\n" +
8107 "; CHECK: [[ld:%\\w+]] = OpLoad [[uint]]\n" +
8108 "; CHECK: %4 = OpIMul [[uint]] [[ld]] [[uint_2]]\n" +
8109 "%main = OpFunction %void None %void_func\n" +
8110 "%main_lab = OpLabel\n" +
8111 "%var = OpVariable %_ptr_uint Function\n" +
8112 "%2 = OpLoad %uint %var\n" +
8113 "%3 = OpIMul %uint %2 %uint_2147483649\n" +
8114 "%4 = OpIMul %uint %3 %uint_2\n" +
8115 "OpReturn\n" +
8116 "OpFunctionEnd\n",
8117 4, true),
8118 // Test case 28: fold overflowing 64 bit unsigned imuls
8119 // (x * 9223372036854775809) * 2 = x * 2
8120 InstructionFoldingCase<bool>(
8121 Header() +
8122 "; CHECK: [[ulong:%\\w+]] = OpTypeInt 64 0\n" +
8123 "; CHECK: [[ulong_2:%\\w+]] = OpConstant [[ulong]] 2\n" +
8124 "; CHECK: [[ld:%\\w+]] = OpLoad [[ulong]]\n" +
8125 "; CHECK: %4 = OpIMul [[ulong]] [[ld]] [[ulong_2]]\n" +
8126 "%main = OpFunction %void None %void_func\n" +
8127 "%main_lab = OpLabel\n" +
8128 "%var = OpVariable %_ptr_ulong Function\n" +
8129 "%2 = OpLoad %ulong %var\n" +
8130 "%3 = OpIMul %ulong %2 %ulong_9223372036854775809\n" +
8131 "%4 = OpIMul %ulong %3 %ulong_2\n" +
8132 "OpReturn\n" +
8133 "OpFunctionEnd\n",
8134 4, true),
8135 // Test case 29: fold underflowing signed 32 bit imuls
8136 // (x * (-858993459)) * 10 = x * 2
8137 InstructionFoldingCase<bool>(
8138 Header() +
8139 "; CHECK: [[int:%\\w+]] = OpTypeInt 32\n" +
8140 "; CHECK: [[int_2:%\\w+]] = OpConstant [[int]] 2\n" +
8141 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
8142 "; CHECK: %4 = OpIMul [[int]] [[ld]] [[int_2]]\n" +
8143 "%main = OpFunction %void None %void_func\n" +
8144 "%main_lab = OpLabel\n" +
8145 "%var = OpVariable %_ptr_int Function\n" +
8146 "%2 = OpLoad %int %var\n" +
8147 "%3 = OpIMul %int %2 %int_n858993459\n" +
8148 "%4 = OpIMul %int %3 %int_10\n" +
8149 "OpReturn\n" +
8150 "OpFunctionEnd\n",
8151 4, true),
8152 // Test case 30: fold underflowing signed 64 bit imuls
8153 // (x * (-3689348814741910323)) * 10 = x * 2
8154 InstructionFoldingCase<bool>(
8155 Header() +
8156 "; CHECK: [[long:%\\w+]] = OpTypeInt 64\n" +
8157 "; CHECK: [[long_2:%\\w+]] = OpConstant [[long]] 2\n" +
8158 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
8159 "; CHECK: %4 = OpIMul [[long]] [[ld]] [[long_2]]\n" +
8160 "%main = OpFunction %void None %void_func\n" +
8161 "%main_lab = OpLabel\n" +
8162 "%var = OpVariable %_ptr_long Function\n" +
8163 "%2 = OpLoad %long %var\n" +
8164 "%3 = OpIMul %long %2 %long_n3689348814741910323\n" +
8165 "%4 = OpIMul %long %3 %long_10\n" +
8166 "OpReturn\n" +
8167 "OpFunctionEnd\n",
8168 4, true),
8169 // Test case 31: merge fmul with two negatives
8170 // (-x) * (-y) = x * y
8171 InstructionFoldingCase<bool>(
8172 Header() +
8173 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8174 "; CHECK: [[x:%\\w+]] = OpLoad [[float]]\n" +
8175 "; CHECK: [[y:%\\w+]] = OpLoad [[float]]\n" +
8176 "; CHECK: %4 = OpFMul [[float]] [[x]] [[y]]\n" +
8177 "%main = OpFunction %void None %void_func\n" +
8178 "%main_lab = OpLabel\n" +
8179 "%var = OpVariable %_ptr_float Function\n" +
8180 "%x = OpLoad %float %var\n" +
8181 "%y = OpLoad %float %var\n" +
8182 "%nx = OpFNegate %float %x\n" +
8183 "%ny = OpFNegate %float %y\n" +
8184 "%4 = OpFMul %float %nx %ny\n" +
8185 "OpReturn\n" +
8186 "OpFunctionEnd\n",
8187 4, true),
8188 // Test case 32: merge OpVectorTimesScalar with two negatives
8189 // (-x) * (-y) = x * y
8190 InstructionFoldingCase<bool>(
8191 Header() +
8192 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8193 "; CHECK: [[float2:%\\w+]] = OpTypeVector [[float]] 2\n" +
8194 "; CHECK: [[x:%\\w+]] = OpLoad [[float2]]\n" +
8195 "; CHECK: [[y:%\\w+]] = OpLoad [[float]]\n" +
8196 "; CHECK: %4 = OpVectorTimesScalar [[float2]] [[x]] [[y]]\n" +
8197 "%main = OpFunction %void None %void_func\n" +
8198 "%main_lab = OpLabel\n" +
8199 "%var_f2 = OpVariable %_ptr_v2float Function\n" +
8200 "%var = OpVariable %_ptr_float Function\n" +
8201 "%x = OpLoad %v2float %var_f2\n" +
8202 "%y = OpLoad %float %var\n" +
8203 "%nx = OpFNegate %v2float %x\n" +
8204 "%ny = OpFNegate %float %y\n" +
8205 "%4 = OpVectorTimesScalar %v2float %nx %ny\n" +
8206 "OpReturn\n" +
8207 "OpFunctionEnd\n",
8208 4, true),
8209 // Test case 33: merge smul with two negatives
8210 // (-x) * (-y) = x * y
8211 InstructionFoldingCase<bool>(
8212 Header() +
8213 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
8214 "; CHECK: [[x:%\\w+]] = OpLoad [[long]]\n" +
8215 "; CHECK: [[y:%\\w+]] = OpLoad [[long]]\n" +
8216 "; CHECK: %4 = OpIMul [[long]] [[x]] [[y]]\n" +
8217 "%main = OpFunction %void None %void_func\n" +
8218 "%main_lab = OpLabel\n" +
8219 "%var = OpVariable %_ptr_long Function\n" +
8220 "%x = OpLoad %long %var\n" +
8221 "%y = OpLoad %long %var\n" +
8222 "%nx = OpSNegate %long %x\n" +
8223 "%ny = OpSNegate %long %y\n" +
8224 "%4 = OpIMul %long %nx %ny\n" +
8225 "OpReturn\n" +
8226 "OpFunctionEnd\n",
8227 4, true),
8228 // Test case 34: merge fdiv with two negatives
8229 // (-x) / (-y) = x / y
8230 InstructionFoldingCase<bool>(
8231 Header() +
8232 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8233 "; CHECK: [[x:%\\w+]] = OpLoad [[float]]\n" +
8234 "; CHECK: [[y:%\\w+]] = OpLoad [[float]]\n" +
8235 "; CHECK: %4 = OpFDiv [[float]] [[x]] [[y]]\n" +
8236 "%main = OpFunction %void None %void_func\n" +
8237 "%main_lab = OpLabel\n" +
8238 "%var = OpVariable %_ptr_float Function\n" +
8239 "%x = OpLoad %float %var\n" +
8240 "%y = OpLoad %float %var\n" +
8241 "%nx = OpFNegate %float %x\n" +
8242 "%ny = OpFNegate %float %y\n" +
8243 "%4 = OpFDiv %float %nx %ny\n" +
8244 "OpReturn\n" +
8245 "OpFunctionEnd\n",
8246 4, true),
8247 // Test case 35: merge sdiv with two negatives
8248 // (-x) / (-y) = x / y
8249 InstructionFoldingCase<bool>(
8250 Header() +
8251 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
8252 "; CHECK: [[x:%\\w+]] = OpLoad [[long]]\n" +
8253 "; CHECK: [[y:%\\w+]] = OpLoad [[long]]\n" +
8254 "; CHECK: %4 = OpSDiv [[long]] [[x]] [[y]]\n" +
8255 "%main = OpFunction %void None %void_func\n" +
8256 "%main_lab = OpLabel\n" +
8257 "%var = OpVariable %_ptr_long Function\n" +
8258 "%x = OpLoad %long %var\n" +
8259 "%y = OpLoad %long %var\n" +
8260 "%nx = OpSNegate %long %x\n" +
8261 "%ny = OpSNegate %long %y\n" +
8262 "%4 = OpSDiv %long %nx %ny\n" +
8263 "OpReturn\n" +
8264 "OpFunctionEnd\n",
8265 4, true)
8266 ));
8267
8268 INSTANTIATE_TEST_SUITE_P(MergeDivTest, MatchingInstructionFoldingTest,
8269 ::testing::Values(
8270 // Test case 0: merge consecutive fdiv
8271 // 4.0 / (2.0 / x) = 2.0 * x
8272 InstructionFoldingCase<bool>(
8273 Header() +
8274 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8275 "; CHECK: [[float_2:%\\w+]] = OpConstant [[float]] 2\n" +
8276 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
8277 "; CHECK: %4 = OpFMul [[float]] [[float_2]] [[ld]]\n" +
8278 "%main = OpFunction %void None %void_func\n" +
8279 "%main_lab = OpLabel\n" +
8280 "%var = OpVariable %_ptr_float Function\n" +
8281 "%2 = OpLoad %float %var\n" +
8282 "%3 = OpFDiv %float %float_2 %2\n" +
8283 "%4 = OpFDiv %float %float_4 %3\n" +
8284 "OpReturn\n" +
8285 "OpFunctionEnd\n",
8286 4, true),
8287 // Test case 1: merge consecutive fdiv
8288 // 4.0 / (x / 2.0) = 8.0 / x
8289 InstructionFoldingCase<bool>(
8290 Header() +
8291 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8292 "; CHECK: [[float_8:%\\w+]] = OpConstant [[float]] 8\n" +
8293 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
8294 "; CHECK: %4 = OpFDiv [[float]] [[float_8]] [[ld]]\n" +
8295 "%main = OpFunction %void None %void_func\n" +
8296 "%main_lab = OpLabel\n" +
8297 "%var = OpVariable %_ptr_float Function\n" +
8298 "%2 = OpLoad %float %var\n" +
8299 "%3 = OpFDiv %float %2 %float_2\n" +
8300 "%4 = OpFDiv %float %float_4 %3\n" +
8301 "OpReturn\n" +
8302 "OpFunctionEnd\n",
8303 4, true),
8304 // Test case 2: merge consecutive fdiv
8305 // (4.0 / x) / 2.0 = 2.0 / x
8306 InstructionFoldingCase<bool>(
8307 Header() +
8308 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8309 "; CHECK: [[float_2:%\\w+]] = OpConstant [[float]] 2\n" +
8310 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
8311 "; CHECK: %4 = OpFDiv [[float]] [[float_2]] [[ld]]\n" +
8312 "%main = OpFunction %void None %void_func\n" +
8313 "%main_lab = OpLabel\n" +
8314 "%var = OpVariable %_ptr_float Function\n" +
8315 "%2 = OpLoad %float %var\n" +
8316 "%3 = OpFDiv %float %float_4 %2\n" +
8317 "%4 = OpFDiv %float %3 %float_2\n" +
8318 "OpReturn\n" +
8319 "OpFunctionEnd\n",
8320 4, true),
8321 // Test case 3: Do not merge consecutive sdiv
8322 // 4 / (2 / x)
8323 InstructionFoldingCase<bool>(
8324 Header() +
8325 "%main = OpFunction %void None %void_func\n" +
8326 "%main_lab = OpLabel\n" +
8327 "%var = OpVariable %_ptr_int Function\n" +
8328 "%2 = OpLoad %int %var\n" +
8329 "%3 = OpSDiv %int %int_2 %2\n" +
8330 "%4 = OpSDiv %int %int_4 %3\n" +
8331 "OpReturn\n" +
8332 "OpFunctionEnd\n",
8333 4, false),
8334 // Test case 4: Do not merge consecutive sdiv
8335 // 4 / (x / 2)
8336 InstructionFoldingCase<bool>(
8337 Header() +
8338 "%main = OpFunction %void None %void_func\n" +
8339 "%main_lab = OpLabel\n" +
8340 "%var = OpVariable %_ptr_int Function\n" +
8341 "%2 = OpLoad %int %var\n" +
8342 "%3 = OpSDiv %int %2 %int_2\n" +
8343 "%4 = OpSDiv %int %int_4 %3\n" +
8344 "OpReturn\n" +
8345 "OpFunctionEnd\n",
8346 4, false),
8347 // Test case 5: Do not merge consecutive sdiv
8348 // (4 / x) / 2
8349 InstructionFoldingCase<bool>(
8350 Header() +
8351 "%main = OpFunction %void None %void_func\n" +
8352 "%main_lab = OpLabel\n" +
8353 "%var = OpVariable %_ptr_int Function\n" +
8354 "%2 = OpLoad %int %var\n" +
8355 "%3 = OpSDiv %int %int_4 %2\n" +
8356 "%4 = OpSDiv %int %3 %int_2\n" +
8357 "OpReturn\n" +
8358 "OpFunctionEnd\n",
8359 4, false),
8360 // Test case 6: Do not merge consecutive sdiv
8361 // (x / 4) / 2
8362 InstructionFoldingCase<bool>(
8363 Header() +
8364 "%main = OpFunction %void None %void_func\n" +
8365 "%main_lab = OpLabel\n" +
8366 "%var = OpVariable %_ptr_int Function\n" +
8367 "%2 = OpLoad %int %var\n" +
8368 "%3 = OpSDiv %int %2 %int_4\n" +
8369 "%4 = OpSDiv %int %3 %int_2\n" +
8370 "OpReturn\n" +
8371 "OpFunctionEnd\n",
8372 4, false),
8373 // Test case 7: Do not merge sdiv of imul
8374 // 4 / (2 * x)
8375 InstructionFoldingCase<bool>(
8376 Header() +
8377 "%main = OpFunction %void None %void_func\n" +
8378 "%main_lab = OpLabel\n" +
8379 "%var = OpVariable %_ptr_int Function\n" +
8380 "%2 = OpLoad %int %var\n" +
8381 "%3 = OpIMul %int %int_2 %2\n" +
8382 "%4 = OpSDiv %int %int_4 %3\n" +
8383 "OpReturn\n" +
8384 "OpFunctionEnd\n",
8385 4, false),
8386 // Test case 8: Do not merge sdiv of imul
8387 // 4 / (x * 2)
8388 InstructionFoldingCase<bool>(
8389 Header() +
8390 "%main = OpFunction %void None %void_func\n" +
8391 "%main_lab = OpLabel\n" +
8392 "%var = OpVariable %_ptr_int Function\n" +
8393 "%2 = OpLoad %int %var\n" +
8394 "%3 = OpIMul %int %2 %int_2\n" +
8395 "%4 = OpSDiv %int %int_4 %3\n" +
8396 "OpReturn\n" +
8397 "OpFunctionEnd\n",
8398 4, false),
8399 // Test case 9: Do not merge sdiv of imul
8400 // (4 * x) / 2
8401 InstructionFoldingCase<bool>(
8402 Header() +
8403 "%main = OpFunction %void None %void_func\n" +
8404 "%main_lab = OpLabel\n" +
8405 "%var = OpVariable %_ptr_int Function\n" +
8406 "%2 = OpLoad %int %var\n" +
8407 "%3 = OpIMul %int %int_4 %2\n" +
8408 "%4 = OpSDiv %int %3 %int_2\n" +
8409 "OpReturn\n" +
8410 "OpFunctionEnd\n",
8411 4, false),
8412 // Test case 10: Do not merge sdiv of imul
8413 // (x * 4) / 2
8414 InstructionFoldingCase<bool>(
8415 Header() +
8416 "%main = OpFunction %void None %void_func\n" +
8417 "%main_lab = OpLabel\n" +
8418 "%var = OpVariable %_ptr_int Function\n" +
8419 "%2 = OpLoad %int %var\n" +
8420 "%3 = OpIMul %int %2 %int_4\n" +
8421 "%4 = OpSDiv %int %3 %int_2\n" +
8422 "OpReturn\n" +
8423 "OpFunctionEnd\n",
8424 4, false),
8425 // Test case 11: Do not merge sdiv of snegate. If %2 is INT_MIN, then the
8426 // sign of %3 will be the same as %2. This cannot be accounted for in OpSDiv.
8427 // Specifically, (-INT_MIN) / 2 != INT_MIN / -2.
8428 InstructionFoldingCase<bool>(
8429 Header() +
8430 "%main = OpFunction %void None %void_func\n" +
8431 "%main_lab = OpLabel\n" +
8432 "%var = OpVariable %_ptr_int Function\n" +
8433 "%2 = OpLoad %int %var\n" +
8434 "%3 = OpSNegate %int %2\n" +
8435 "%4 = OpSDiv %int %3 %int_2\n" +
8436 "OpReturn\n" +
8437 "OpFunctionEnd\n",
8438 4, false),
8439 // Test case 12: Do not merge sdiv of snegate. If %2 is INT_MIN, then the
8440 // sign of %3 will be the same as %2. This cannot be accounted for in OpSDiv.
8441 // Specifically, 2 / (-INT_MIN) != -2 / INT_MIN.
8442 InstructionFoldingCase<bool>(
8443 Header() +
8444 "%main = OpFunction %void None %void_func\n" +
8445 "%main_lab = OpLabel\n" +
8446 "%var = OpVariable %_ptr_int Function\n" +
8447 "%2 = OpLoad %int %var\n" +
8448 "%3 = OpSNegate %int %2\n" +
8449 "%4 = OpSDiv %int %int_2 %3\n" +
8450 "OpReturn\n" +
8451 "OpFunctionEnd\n",
8452 4, false),
8453 // Test case 13: Don't merge
8454 // (x / {null}) / {null}
8455 InstructionFoldingCase<bool>(
8456 Header() +
8457 "%main = OpFunction %void None %void_func\n" +
8458 "%main_lab = OpLabel\n" +
8459 "%var = OpVariable %_ptr_v2float Function\n" +
8460 "%2 = OpLoad %float %var\n" +
8461 "%3 = OpFDiv %float %2 %v2float_null\n" +
8462 "%4 = OpFDiv %float %3 %v2float_null\n" +
8463 "OpReturn\n" +
8464 "OpFunctionEnd\n",
8465 4, false),
8466 // Test case 14: merge fmul of fdiv
8467 // (y * x) / x = y
8468 InstructionFoldingCase<bool>(
8469 Header() +
8470 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8471 "; CHECK: [[ldx:%\\w+]] = OpLoad [[float]]\n" +
8472 "; CHECK: [[ldy:%\\w+]] = OpLoad [[float]] [[y:%\\w+]]\n" +
8473 "; CHECK: %5 = OpCopyObject [[float]] [[ldy]]\n" +
8474 "%main = OpFunction %void None %void_func\n" +
8475 "%main_lab = OpLabel\n" +
8476 "%x = OpVariable %_ptr_float Function\n" +
8477 "%y = OpVariable %_ptr_float Function\n" +
8478 "%2 = OpLoad %float %x\n" +
8479 "%3 = OpLoad %float %y\n" +
8480 "%4 = OpFMul %float %3 %2\n" +
8481 "%5 = OpFDiv %float %4 %2\n" +
8482 "OpReturn\n" +
8483 "OpFunctionEnd\n",
8484 5, true),
8485 // Test case 15: merge fmul of fdiv
8486 // (x * y) / x = y
8487 InstructionFoldingCase<bool>(
8488 Header() +
8489 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
8490 "; CHECK: [[ldx:%\\w+]] = OpLoad [[float]]\n" +
8491 "; CHECK: [[ldy:%\\w+]] = OpLoad [[float]] [[y:%\\w+]]\n" +
8492 "; CHECK: %5 = OpCopyObject [[float]] [[ldy]]\n" +
8493 "%main = OpFunction %void None %void_func\n" +
8494 "%main_lab = OpLabel\n" +
8495 "%x = OpVariable %_ptr_float Function\n" +
8496 "%y = OpVariable %_ptr_float Function\n" +
8497 "%2 = OpLoad %float %x\n" +
8498 "%3 = OpLoad %float %y\n" +
8499 "%4 = OpFMul %float %2 %3\n" +
8500 "%5 = OpFDiv %float %4 %2\n" +
8501 "OpReturn\n" +
8502 "OpFunctionEnd\n",
8503 5, true),
8504 // Test case 16: Do not merge udiv of snegate
8505 // (-x) / 2u
8506 InstructionFoldingCase<bool>(
8507 Header() +
8508 "%main = OpFunction %void None %void_func\n" +
8509 "%main_lab = OpLabel\n" +
8510 "%var = OpVariable %_ptr_uint Function\n" +
8511 "%2 = OpLoad %uint %var\n" +
8512 "%3 = OpSNegate %uint %2\n" +
8513 "%4 = OpUDiv %uint %3 %uint_2\n" +
8514 "OpReturn\n" +
8515 "OpFunctionEnd\n",
8516 4, false),
8517 // Test case 17: Do not merge udiv of snegate
8518 // 2u / (-x)
8519 InstructionFoldingCase<bool>(
8520 Header() +
8521 "%main = OpFunction %void None %void_func\n" +
8522 "%main_lab = OpLabel\n" +
8523 "%var = OpVariable %_ptr_uint Function\n" +
8524 "%2 = OpLoad %uint %var\n" +
8525 "%3 = OpSNegate %uint %2\n" +
8526 "%4 = OpUDiv %uint %uint_2 %3\n" +
8527 "OpReturn\n" +
8528 "OpFunctionEnd\n",
8529 4, false)
8530 ));
8531
8532 INSTANTIATE_TEST_SUITE_P(RedundantAndOrXorTest, MatchingInstructionFoldingTest,
8533 ::testing::Values(
8534 // Test case 0: Fold
8535 // 1 & (n | 2) = n & 1
8536 InstructionFoldingCase<bool>(
8537 Header() +
8538 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8539 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8540 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8541 "%main = OpFunction %void None %void_func\n" +
8542 "%main_lab = OpLabel\n" +
8543 "%n = OpVariable %_ptr_uint Function\n" +
8544 "%4 = OpLoad %uint %n\n" +
8545 "%3 = OpBitwiseOr %uint %4 %uint_2\n" +
8546 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8547 "OpReturn\n" +
8548 "OpFunctionEnd",
8549 2, true),
8550
8551 // Test case 1: Fold
8552 // 1 & (2 | n) = n & 1
8553 InstructionFoldingCase<bool>(
8554 Header() +
8555 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8556 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8557 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8558 "%main = OpFunction %void None %void_func\n" +
8559 "%main_lab = OpLabel\n" +
8560 "%n = OpVariable %_ptr_uint Function\n" +
8561 "%4 = OpLoad %uint %n\n" +
8562 "%3 = OpBitwiseOr %uint %uint_2 %4\n" +
8563 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8564 "OpReturn\n" +
8565 "OpFunctionEnd",
8566 2, true),
8567
8568 // Test case 2: Fold
8569 // (n | 2) & 1 = n & 1
8570 InstructionFoldingCase<bool>(
8571 Header() +
8572 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8573 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8574 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8575 "%main = OpFunction %void None %void_func\n" +
8576 "%main_lab = OpLabel\n" +
8577 "%n = OpVariable %_ptr_uint Function\n" +
8578 "%4 = OpLoad %uint %n\n" +
8579 "%3 = OpBitwiseOr %uint %4 %uint_2\n" +
8580 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
8581 "OpReturn\n" +
8582 "OpFunctionEnd",
8583 2, true),
8584
8585 // Test case 3: Fold
8586 // (2 | n) & 1 = n & 1
8587 InstructionFoldingCase<bool>(
8588 Header() +
8589 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8590 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8591 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8592 "%main = OpFunction %void None %void_func\n" +
8593 "%main_lab = OpLabel\n" +
8594 "%n = OpVariable %_ptr_uint Function\n" +
8595 "%4 = OpLoad %uint %n\n" +
8596 "%3 = OpBitwiseOr %uint %uint_2 %4\n" +
8597 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
8598 "OpReturn\n" +
8599 "OpFunctionEnd",
8600 2, true),
8601
8602 // Test case 4: Fold
8603 // 1 & (n | 3) = 1
8604 InstructionFoldingCase<bool>(
8605 Header() +
8606 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8607 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8608 "; CHECK: %2 = OpCopyObject [[uint]] [[uint_1]]\n" +
8609 "%main = OpFunction %void None %void_func\n" +
8610 "%main_lab = OpLabel\n" +
8611 "%n = OpVariable %_ptr_uint Function\n" +
8612 "%4 = OpLoad %uint %n\n" +
8613 "%3 = OpBitwiseOr %uint %4 %uint_3\n" +
8614 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8615 "OpReturn\n" +
8616 "OpFunctionEnd",
8617 2, true),
8618
8619 // Test case 5: Fold
8620 // 1 & (3 | n) = 1
8621 InstructionFoldingCase<bool>(
8622 Header() +
8623 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8624 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8625 "; CHECK: %2 = OpCopyObject [[uint]] [[uint_1]]\n" +
8626 "%main = OpFunction %void None %void_func\n" +
8627 "%main_lab = OpLabel\n" +
8628 "%n = OpVariable %_ptr_uint Function\n" +
8629 "%4 = OpLoad %uint %n\n" +
8630 "%3 = OpBitwiseOr %uint %uint_3 %4\n" +
8631 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8632 "OpReturn\n" +
8633 "OpFunctionEnd",
8634 2, true),
8635
8636 // Test case 6: Fold
8637 // (n | 3) & 1 = 1
8638 InstructionFoldingCase<bool>(
8639 Header() +
8640 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8641 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8642 "; CHECK: %2 = OpCopyObject [[uint]] [[uint_1]]\n" +
8643 "%main = OpFunction %void None %void_func\n" +
8644 "%main_lab = OpLabel\n" +
8645 "%n = OpVariable %_ptr_uint Function\n" +
8646 "%4 = OpLoad %uint %n\n" +
8647 "%3 = OpBitwiseOr %uint %4 %uint_3\n" +
8648 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
8649 "OpReturn\n" +
8650 "OpFunctionEnd",
8651 2, true),
8652
8653 // Test case 7: Fold
8654 // (3 | n) & 1 = 1
8655 InstructionFoldingCase<bool>(
8656 Header() +
8657 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8658 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8659 "; CHECK: %2 = OpCopyObject [[uint]] [[uint_1]]\n" +
8660 "%main = OpFunction %void None %void_func\n" +
8661 "%main_lab = OpLabel\n" +
8662 "%n = OpVariable %_ptr_uint Function\n" +
8663 "%4 = OpLoad %uint %n\n" +
8664 "%3 = OpBitwiseOr %uint %uint_3 %4\n" +
8665 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
8666 "OpReturn\n" +
8667 "OpFunctionEnd",
8668 2, true),
8669
8670 // Test case 8: Do not fold
8671 // 3 & (n | 1)
8672 InstructionFoldingCase<bool>(
8673 Header() +
8674 "%main = OpFunction %void None %void_func\n" +
8675 "%main_lab = OpLabel\n" +
8676 "%n = OpVariable %_ptr_uint Function\n" +
8677 "%4 = OpLoad %uint %n\n" +
8678 "%3 = OpBitwiseOr %uint %4 %uint_1\n" +
8679 "%2 = OpBitwiseAnd %uint %uint_3 %3\n" +
8680 "OpReturn\n" +
8681 "OpFunctionEnd",
8682 2, false),
8683
8684 // Test case 9: Do not fold
8685 // 3 & (1 | n)
8686 InstructionFoldingCase<bool>(
8687 Header() +
8688 "%main = OpFunction %void None %void_func\n" +
8689 "%main_lab = OpLabel\n" +
8690 "%n = OpVariable %_ptr_uint Function\n" +
8691 "%4 = OpLoad %uint %n\n" +
8692 "%3 = OpBitwiseOr %uint %uint_1 %4\n" +
8693 "%2 = OpBitwiseAnd %uint %uint_3 %3\n" +
8694 "OpReturn\n" +
8695 "OpFunctionEnd",
8696 2, false),
8697
8698 // Test case 10: Do not fold
8699 // (n | 1) & 3
8700 InstructionFoldingCase<bool>(
8701 Header() +
8702 "%main = OpFunction %void None %void_func\n" +
8703 "%main_lab = OpLabel\n" +
8704 "%n = OpVariable %_ptr_uint Function\n" +
8705 "%4 = OpLoad %uint %n\n" +
8706 "%3 = OpBitwiseOr %uint %4 %uint_1\n" +
8707 "%2 = OpBitwiseAnd %uint %3 %uint_3\n" +
8708 "OpReturn\n" +
8709 "OpFunctionEnd",
8710 2, false),
8711
8712 // Test case 11: Do not fold
8713 // (1 | n) & 3
8714 InstructionFoldingCase<bool>(
8715 Header() +
8716 "%main = OpFunction %void None %void_func\n" +
8717 "%main_lab = OpLabel\n" +
8718 "%n = OpVariable %_ptr_uint Function\n" +
8719 "%4 = OpLoad %uint %n\n" +
8720 "%3 = OpBitwiseOr %uint %uint_1 %4\n" +
8721 "%2 = OpBitwiseAnd %uint %3 %uint_3\n" +
8722 "OpReturn\n" +
8723 "OpFunctionEnd",
8724 2, false),
8725
8726 // Test case 12: Fold
8727 // 1 & (n ^ 2) = n & 1
8728 InstructionFoldingCase<bool>(
8729 Header() +
8730 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8731 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8732 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8733 "%main = OpFunction %void None %void_func\n" +
8734 "%main_lab = OpLabel\n" +
8735 "%n = OpVariable %_ptr_uint Function\n" +
8736 "%4 = OpLoad %uint %n\n" +
8737 "%3 = OpBitwiseXor %uint %4 %uint_2\n" +
8738 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8739 "OpReturn\n" +
8740 "OpFunctionEnd",
8741 2, true),
8742
8743 // Test case 13: Fold
8744 // 1 & (2 ^ n) = n & 1
8745 InstructionFoldingCase<bool>(
8746 Header() +
8747 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8748 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8749 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8750 "%main = OpFunction %void None %void_func\n" +
8751 "%main_lab = OpLabel\n" +
8752 "%n = OpVariable %_ptr_uint Function\n" +
8753 "%4 = OpLoad %uint %n\n" +
8754 "%3 = OpBitwiseXor %uint %uint_2 %4\n" +
8755 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8756 "OpReturn\n" +
8757 "OpFunctionEnd",
8758 2, true),
8759
8760 // Test case 14: Fold
8761 // (n ^ 2) & 1 = n & 1
8762 InstructionFoldingCase<bool>(
8763 Header() +
8764 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8765 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8766 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8767 "%main = OpFunction %void None %void_func\n" +
8768 "%main_lab = OpLabel\n" +
8769 "%n = OpVariable %_ptr_uint Function\n" +
8770 "%4 = OpLoad %uint %n\n" +
8771 "%3 = OpBitwiseXor %uint %4 %uint_2\n" +
8772 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
8773 "OpReturn\n" +
8774 "OpFunctionEnd",
8775 2, true),
8776
8777 // Test case 15: Fold
8778 // (2 ^ n) & 1 = n & 1
8779 InstructionFoldingCase<bool>(
8780 Header() +
8781 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8782 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8783 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8784 "%main = OpFunction %void None %void_func\n" +
8785 "%main_lab = OpLabel\n" +
8786 "%n = OpVariable %_ptr_uint Function\n" +
8787 "%4 = OpLoad %uint %n\n" +
8788 "%3 = OpBitwiseXor %uint %uint_2 %4\n" +
8789 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
8790 "OpReturn\n" +
8791 "OpFunctionEnd",
8792 2, true),
8793
8794 // Test case 16: Do not fold
8795 // 1 & (n ^ 3)
8796 InstructionFoldingCase<bool>(
8797 Header() +
8798 "%main = OpFunction %void None %void_func\n" +
8799 "%main_lab = OpLabel\n" +
8800 "%n = OpVariable %_ptr_uint Function\n" +
8801 "%4 = OpLoad %uint %n\n" +
8802 "%3 = OpBitwiseXor %uint %4 %uint_3\n" +
8803 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8804 "OpReturn\n" +
8805 "OpFunctionEnd",
8806 2, false),
8807
8808 // Test case 17: Do not fold
8809 // 1 & (3 ^ n)
8810 InstructionFoldingCase<bool>(
8811 Header() +
8812 "%main = OpFunction %void None %void_func\n" +
8813 "%main_lab = OpLabel\n" +
8814 "%n = OpVariable %_ptr_uint Function\n" +
8815 "%4 = OpLoad %uint %n\n" +
8816 "%3 = OpBitwiseXor %uint %uint_3 %4\n" +
8817 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8818 "OpReturn\n" +
8819 "OpFunctionEnd",
8820 2, false),
8821
8822 // Test case 18: Do not fold
8823 // (n ^ 3) & 1
8824 InstructionFoldingCase<bool>(
8825 Header() +
8826 "%main = OpFunction %void None %void_func\n" +
8827 "%main_lab = OpLabel\n" +
8828 "%n = OpVariable %_ptr_uint Function\n" +
8829 "%4 = OpLoad %uint %n\n" +
8830 "%3 = OpBitwiseXor %uint %4 %uint_3\n" +
8831 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
8832 "OpReturn\n" +
8833 "OpFunctionEnd",
8834 2, false),
8835
8836 // Test case 19: Do not fold
8837 // (3 ^ n) & 1
8838 InstructionFoldingCase<bool>(
8839 Header() +
8840 "%main = OpFunction %void None %void_func\n" +
8841 "%main_lab = OpLabel\n" +
8842 "%n = OpVariable %_ptr_uint Function\n" +
8843 "%4 = OpLoad %uint %n\n" +
8844 "%3 = OpBitwiseXor %uint %uint_3 %4\n" +
8845 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
8846 "OpReturn\n" +
8847 "OpFunctionEnd",
8848 2, false),
8849
8850 // Test case 20: Fold
8851 // 0b101 & (n | 0b10101) = 0b101
8852 InstructionFoldingCase<bool>(
8853 Header() +
8854 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8855 "; CHECK: [[uint_5:%\\w+]] = OpConstant [[uint]] 5\n" +
8856 "; CHECK: %2 = OpCopyObject [[uint]] [[uint_5]]\n" +
8857 "%uint_5 = OpConstant %uint 5\n" +
8858 "%uint_21 = OpConstant %uint 21\n" +
8859 "%main = OpFunction %void None %void_func\n" +
8860 "%main_lab = OpLabel\n" +
8861 "%n = OpVariable %_ptr_uint Function\n" +
8862 "%4 = OpLoad %uint %n\n" +
8863 "%3 = OpBitwiseOr %uint %4 %uint_21\n" +
8864 "%2 = OpBitwiseAnd %uint %uint_5 %3\n" +
8865 "OpReturn\n" +
8866 "OpFunctionEnd",
8867 2, true),
8868
8869 // Test case 21: Do not fold
8870 // 0b101 & (n ^ 0b10101)
8871 InstructionFoldingCase<bool>(
8872 Header() +
8873 "%uint_5 = OpConstant %uint 5\n" +
8874 "%uint_21 = OpConstant %uint 21\n" +
8875 "%main = OpFunction %void None %void_func\n" +
8876 "%main_lab = OpLabel\n" +
8877 "%n = OpVariable %_ptr_uint Function\n" +
8878 "%4 = OpLoad %uint %n\n" +
8879 "%3 = OpBitwiseXor %uint %4 %uint_21\n" +
8880 "%2 = OpBitwiseAnd %uint %uint_5 %3\n" +
8881 "OpReturn\n" +
8882 "OpFunctionEnd",
8883 2, false),
8884
8885 // Test case 22: Fold
8886 // 0b101 & (n ^ 0b1010) = n & 0b101
8887 InstructionFoldingCase<bool>(
8888 Header() +
8889 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8890 "; CHECK: [[uint_5:%\\w+]] = OpConstant [[uint]] 5\n" +
8891 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_5]]\n" +
8892 "%uint_5 = OpConstant %uint 5\n" +
8893 "%uint_10 = OpConstant %uint 10\n" +
8894 "%main = OpFunction %void None %void_func\n" +
8895 "%main_lab = OpLabel\n" +
8896 "%n = OpVariable %_ptr_uint Function\n" +
8897 "%4 = OpLoad %uint %n\n" +
8898 "%3 = OpBitwiseXor %uint %4 %uint_10\n" +
8899 "%2 = OpBitwiseAnd %uint %uint_5 %3\n" +
8900 "OpReturn\n" +
8901 "OpFunctionEnd",
8902 2, true),
8903
8904 // Test case 23: Fold
8905 // 0b101 & (n | 0b1010) = n & 0b101
8906 InstructionFoldingCase<bool>(
8907 Header() +
8908 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8909 "; CHECK: [[uint_5:%\\w+]] = OpConstant [[uint]] 5\n" +
8910 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_5]]\n" +
8911 "%uint_5 = OpConstant %uint 5\n" +
8912 "%uint_10 = OpConstant %uint 10\n" +
8913 "%main = OpFunction %void None %void_func\n" +
8914 "%main_lab = OpLabel\n" +
8915 "%n = OpVariable %_ptr_uint Function\n" +
8916 "%4 = OpLoad %uint %n\n" +
8917 "%3 = OpBitwiseOr %uint %4 %uint_10\n" +
8918 "%2 = OpBitwiseAnd %uint %uint_5 %3\n" +
8919 "OpReturn\n" +
8920 "OpFunctionEnd",
8921 2, true),
8922
8923 // Test case 24: Fold
8924 // 0b11000 & (n ^ 0b00111) = n & 0b11000
8925 InstructionFoldingCase<bool>(
8926 Header() +
8927 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8928 "; CHECK: [[uint_24:%\\w+]] = OpConstant [[uint]] 24\n" +
8929 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_24]]\n" +
8930 "%uint_24 = OpConstant %uint 24\n" +
8931 "%uint_7 = OpConstant %uint 7\n" +
8932 "%main = OpFunction %void None %void_func\n" +
8933 "%main_lab = OpLabel\n" +
8934 "%n = OpVariable %_ptr_uint Function\n" +
8935 "%4 = OpLoad %uint %n\n" +
8936 "%3 = OpBitwiseXor %uint %4 %uint_7\n" +
8937 "%2 = OpBitwiseAnd %uint %uint_24 %3\n" +
8938 "OpReturn\n" +
8939 "OpFunctionEnd",
8940 2, true),
8941
8942 // Test case 25: Fold
8943 // 0b11000 & (n | 0b00111) = n & 0b11000
8944 InstructionFoldingCase<bool>(
8945 Header() +
8946 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8947 "; CHECK: [[uint_24:%\\w+]] = OpConstant [[uint]] 24\n" +
8948 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_24]]\n" +
8949 "%uint_24 = OpConstant %uint 24\n" +
8950 "%uint_7 = OpConstant %uint 7\n" +
8951 "%main = OpFunction %void None %void_func\n" +
8952 "%main_lab = OpLabel\n" +
8953 "%n = OpVariable %_ptr_uint Function\n" +
8954 "%4 = OpLoad %uint %n\n" +
8955 "%3 = OpBitwiseOr %uint %4 %uint_7\n" +
8956 "%2 = OpBitwiseAnd %uint %uint_24 %3\n" +
8957 "OpReturn\n" +
8958 "OpFunctionEnd",
8959 2, true)
8960 ));
8961
8962 INSTANTIATE_TEST_SUITE_P(RedundantAndAddSubTest, MatchingInstructionFoldingTest,
8963 ::testing::Values(
8964 // Test case 0: Fold
8965 // 1 & (n + 2) = n & 1
8966 InstructionFoldingCase<bool>(
8967 Header() +
8968 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8969 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8970 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8971 "%main = OpFunction %void None %void_func\n" +
8972 "%main_lab = OpLabel\n" +
8973 "%n = OpVariable %_ptr_uint Function\n" +
8974 "%4 = OpLoad %uint %n\n" +
8975 "%3 = OpIAdd %uint %4 %uint_2\n" +
8976 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8977 "OpReturn\n" +
8978 "OpFunctionEnd",
8979 2, true),
8980
8981 // Test case 1: Fold
8982 // 1 & (2 + n) = n & 1
8983 InstructionFoldingCase<bool>(
8984 Header() +
8985 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
8986 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
8987 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
8988 "%main = OpFunction %void None %void_func\n" +
8989 "%main_lab = OpLabel\n" +
8990 "%n = OpVariable %_ptr_uint Function\n" +
8991 "%4 = OpLoad %uint %n\n" +
8992 "%3 = OpIAdd %uint %uint_2 %4\n" +
8993 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
8994 "OpReturn\n" +
8995 "OpFunctionEnd",
8996 2, true),
8997
8998 // Test case 2: Fold
8999 // (n + 2) & 1 = n & 1
9000 InstructionFoldingCase<bool>(
9001 Header() +
9002 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
9003 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
9004 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
9005 "%main = OpFunction %void None %void_func\n" +
9006 "%main_lab = OpLabel\n" +
9007 "%n = OpVariable %_ptr_uint Function\n" +
9008 "%4 = OpLoad %uint %n\n" +
9009 "%3 = OpIAdd %uint %4 %uint_2\n" +
9010 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
9011 "OpReturn\n" +
9012 "OpFunctionEnd",
9013 2, true),
9014
9015 // Test case 3: Fold
9016 // (2 + n) & 1 = n & 1
9017 InstructionFoldingCase<bool>(
9018 Header() +
9019 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
9020 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
9021 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
9022 "%main = OpFunction %void None %void_func\n" +
9023 "%main_lab = OpLabel\n" +
9024 "%n = OpVariable %_ptr_uint Function\n" +
9025 "%4 = OpLoad %uint %n\n" +
9026 "%3 = OpIAdd %uint %uint_2 %4\n" +
9027 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
9028 "OpReturn\n" +
9029 "OpFunctionEnd",
9030 2, true),
9031
9032 // Test case 4: Fold
9033 // 1 & (n - 2) = n & 1
9034 InstructionFoldingCase<bool>(
9035 Header() +
9036 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
9037 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
9038 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
9039 "%main = OpFunction %void None %void_func\n" +
9040 "%main_lab = OpLabel\n" +
9041 "%n = OpVariable %_ptr_uint Function\n" +
9042 "%4 = OpLoad %uint %n\n" +
9043 "%3 = OpISub %uint %4 %uint_2\n" +
9044 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
9045 "OpReturn\n" +
9046 "OpFunctionEnd",
9047 2, true),
9048
9049 // Test case 5: Do not fold
9050 // 1 & (2 - n)
9051 InstructionFoldingCase<bool>(
9052 Header() +
9053 "%main = OpFunction %void None %void_func\n" +
9054 "%main_lab = OpLabel\n" +
9055 "%n = OpVariable %_ptr_uint Function\n" +
9056 "%4 = OpLoad %uint %n\n" +
9057 "%3 = OpISub %uint %uint_2 %4\n" +
9058 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
9059 "OpReturn\n" +
9060 "OpFunctionEnd",
9061 2, false),
9062
9063 // Test case 6: Fold
9064 // (n - 2) & 1 = n & 1
9065 InstructionFoldingCase<bool>(
9066 Header() +
9067 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
9068 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
9069 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_1]]\n" +
9070 "%main = OpFunction %void None %void_func\n" +
9071 "%main_lab = OpLabel\n" +
9072 "%n = OpVariable %_ptr_uint Function\n" +
9073 "%4 = OpLoad %uint %n\n" +
9074 "%3 = OpISub %uint %4 %uint_2\n" +
9075 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
9076 "OpReturn\n" +
9077 "OpFunctionEnd",
9078 2, true),
9079
9080 // Test case 7: Do not fold
9081 // (2 - n) & 1
9082 InstructionFoldingCase<bool>(
9083 Header() +
9084 "%main = OpFunction %void None %void_func\n" +
9085 "%main_lab = OpLabel\n" +
9086 "%n = OpVariable %_ptr_uint Function\n" +
9087 "%4 = OpLoad %uint %n\n" +
9088 "%3 = OpISub %uint %uint_2 %4\n" +
9089 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
9090 "OpReturn\n" +
9091 "OpFunctionEnd",
9092 2, false),
9093
9094 // Test case 8: Do not fold
9095 // 1 & (n + 1)
9096 InstructionFoldingCase<bool>(
9097 Header() +
9098 "%main = OpFunction %void None %void_func\n" +
9099 "%main_lab = OpLabel\n" +
9100 "%n = OpVariable %_ptr_uint Function\n" +
9101 "%4 = OpLoad %uint %n\n" +
9102 "%3 = OpIAdd %uint %4 %uint_1\n" +
9103 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
9104 "OpReturn\n" +
9105 "OpFunctionEnd",
9106 2, false),
9107
9108 // Test case 9: Do not fold
9109 // 1 & (n - 1)
9110 InstructionFoldingCase<bool>(
9111 Header() +
9112 "%main = OpFunction %void None %void_func\n" +
9113 "%main_lab = OpLabel\n" +
9114 "%n = OpVariable %_ptr_uint Function\n" +
9115 "%4 = OpLoad %uint %n\n" +
9116 "%3 = OpISub %uint %4 %uint_1\n" +
9117 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
9118 "OpReturn\n" +
9119 "OpFunctionEnd",
9120 2, false),
9121
9122 // Test case 10: Do not fold
9123 // 2 & (n + 1)
9124 InstructionFoldingCase<bool>(
9125 Header() +
9126 "%main = OpFunction %void None %void_func\n" +
9127 "%main_lab = OpLabel\n" +
9128 "%n = OpVariable %_ptr_uint Function\n" +
9129 "%4 = OpLoad %uint %n\n" +
9130 "%3 = OpIAdd %uint %4 %uint_1\n" +
9131 "%2 = OpBitwiseAnd %uint %3 %uint_2\n" +
9132 "OpReturn\n" +
9133 "OpFunctionEnd",
9134 2, false),
9135
9136 // Test case 11: Do not fold
9137 // 2 & (n - 1)
9138 InstructionFoldingCase<bool>(
9139 Header() +
9140 "%main = OpFunction %void None %void_func\n" +
9141 "%main_lab = OpLabel\n" +
9142 "%n = OpVariable %_ptr_uint Function\n" +
9143 "%4 = OpLoad %uint %n\n" +
9144 "%3 = OpISub %uint %4 %uint_1\n" +
9145 "%2 = OpBitwiseAnd %uint %3 %uint_2\n" +
9146 "OpReturn\n" +
9147 "OpFunctionEnd",
9148 2, false),
9149
9150 // Test case 12: Do not fold
9151 // 0b10 & (n + 0b101)
9152 InstructionFoldingCase<bool>(
9153 Header() +
9154 "%uint_5 = OpConstant %uint 5\n" +
9155 "%main = OpFunction %void None %void_func\n" +
9156 "%main_lab = OpLabel\n" +
9157 "%n = OpVariable %_ptr_uint Function\n" +
9158 "%4 = OpLoad %uint %n\n" +
9159 "%3 = OpIAdd %uint %4 %uint_5\n" +
9160 "%2 = OpBitwiseAnd %uint %3 %uint_2\n" +
9161 "OpReturn\n" +
9162 "OpFunctionEnd",
9163 2, false),
9164
9165 // Test case 13: Fold
9166 // 0b10010 & (n + 0b11100000) = n & 0b10010
9167 InstructionFoldingCase<bool>(
9168 Header() +
9169 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
9170 "; CHECK: [[uint_18:%\\w+]] = OpConstant [[uint]] 18\n" +
9171 "; CHECK: %2 = OpBitwiseAnd [[uint]] %4 [[uint_18]]\n" +
9172 "%uint_18 = OpConstant %uint 18\n" +
9173 "%uint_224 = OpConstant %uint 224\n" +
9174 "%main = OpFunction %void None %void_func\n" +
9175 "%main_lab = OpLabel\n" +
9176 "%n = OpVariable %_ptr_uint Function\n" +
9177 "%4 = OpLoad %uint %n\n" +
9178 "%3 = OpIAdd %uint %4 %uint_224\n" +
9179 "%2 = OpBitwiseAnd %uint %3 %uint_18\n" +
9180 "OpReturn\n" +
9181 "OpFunctionEnd",
9182 2, true),
9183
9184 // Testcase 14: Do not fold
9185 // 0b10000 & (n + 0b11100001)
9186 InstructionFoldingCase<bool>(
9187 Header() +
9188 "%uint_16 = OpConstant %uint 16\n" +
9189 "%uint_225 = OpConstant %uint 225\n" +
9190 "%main = OpFunction %void None %void_func\n" +
9191 "%main_lab = OpLabel\n" +
9192 "%n = OpVariable %_ptr_uint Function\n" +
9193 "%4 = OpLoad %uint %n\n" +
9194 "%3 = OpIAdd %uint %4 %uint_225\n" +
9195 "%2 = OpBitwiseAnd %uint %3 %uint_16\n" +
9196 "OpReturn\n" +
9197 "OpFunctionEnd",
9198 2, false)
9199 ));
9200
9201 INSTANTIATE_TEST_SUITE_P(RedundantAndShiftTest, GeneralInstructionFoldingTest,
9202 ::testing::Values(
9203 // Test case 0: Fold
9204 // 1 & (n << 1) = 0
9205 InstructionFoldingCase<uint32_t>(
9206 Header() +
9207 "%main = OpFunction %void None %void_func\n" +
9208 "%main_lab = OpLabel\n" +
9209 "%n = OpVariable %_ptr_uint Function\n" +
9210 "%4 = OpLoad %uint %n\n" +
9211 "%3 = OpShiftLeftLogical %uint %4 %uint_1\n" +
9212 "%2 = OpBitwiseAnd %uint %uint_1 %3\n" +
9213 "OpReturn\n" +
9214 "OpFunctionEnd",
9215 2, UINT_NULL_ID),
9216
9217 // Test case 1: Fold
9218 // (n << 1) & 1 = 0
9219 InstructionFoldingCase<uint32_t>(
9220 Header() +
9221 "%main = OpFunction %void None %void_func\n" +
9222 "%main_lab = OpLabel\n" +
9223 "%n = OpVariable %_ptr_uint Function\n" +
9224 "%4 = OpLoad %uint %n\n" +
9225 "%3 = OpShiftLeftLogical %uint %4 %uint_1\n" +
9226 "%2 = OpBitwiseAnd %uint %3 %uint_1\n" +
9227 "OpReturn\n" +
9228 "OpFunctionEnd",
9229 2, UINT_NULL_ID),
9230
9231 // Test case 2: Do not fold
9232 // 3 & (n << 1)
9233 InstructionFoldingCase<uint32_t>(
9234 Header() +
9235 "%main = OpFunction %void None %void_func\n" +
9236 "%main_lab = OpLabel\n" +
9237 "%n = OpVariable %_ptr_uint Function\n" +
9238 "%4 = OpLoad %uint %n\n" +
9239 "%3 = OpShiftLeftLogical %uint %4 %uint_1\n" +
9240 "%2 = OpBitwiseAnd %uint %uint_3 %3\n" +
9241 "OpReturn\n" +
9242 "OpFunctionEnd",
9243 2, 0),
9244
9245 // Test case 3: Do not fold
9246 // (n << 1) & 3
9247 InstructionFoldingCase<uint32_t>(
9248 Header() +
9249 "%main = OpFunction %void None %void_func\n" +
9250 "%main_lab = OpLabel\n" +
9251 "%n = OpVariable %_ptr_uint Function\n" +
9252 "%4 = OpLoad %uint %n\n" +
9253 "%3 = OpShiftLeftLogical %uint %4 %uint_1\n" +
9254 "%2 = OpBitwiseAnd %uint %3 %uint_3\n" +
9255 "OpReturn\n" +
9256 "OpFunctionEnd",
9257 2, 0),
9258
9259 // Test case 4: Fold
9260 // 0x80000000 & (n >> 1) = 0
9261 InstructionFoldingCase<uint32_t>(
9262 Header() +
9263 "%uint_2147483648 = OpConstant %uint 2147483648\n" +
9264 "%main = OpFunction %void None %void_func\n" +
9265 "%main_lab = OpLabel\n" +
9266 "%n = OpVariable %_ptr_uint Function\n" +
9267 "%4 = OpLoad %uint %n\n" +
9268 "%3 = OpShiftRightLogical %uint %4 %uint_1\n" +
9269 "%2 = OpBitwiseAnd %uint %uint_2147483648 %3\n" +
9270 "OpReturn\n" +
9271 "OpFunctionEnd",
9272 2, UINT_NULL_ID),
9273
9274 // Test case 5: Fold
9275 // (n >> 1) & 0x80000000 = 0
9276 InstructionFoldingCase<uint32_t>(
9277 Header() +
9278 "%uint_2147483648 = OpConstant %uint 2147483648\n" +
9279 "%main = OpFunction %void None %void_func\n" +
9280 "%main_lab = OpLabel\n" +
9281 "%n = OpVariable %_ptr_uint Function\n" +
9282 "%4 = OpLoad %uint %n\n" +
9283 "%3 = OpShiftRightLogical %uint %4 %uint_1\n" +
9284 "%2 = OpBitwiseAnd %uint %3 %uint_2147483648\n" +
9285 "OpReturn\n" +
9286 "OpFunctionEnd",
9287 2, UINT_NULL_ID),
9288
9289 // Test case 6: Do not fold
9290 // (n >> 1) & 0xc0000000
9291 InstructionFoldingCase<uint32_t>(
9292 Header() +
9293 "%uint_3221225472 = OpConstant %uint 3221225472\n" +
9294 "%main = OpFunction %void None %void_func\n" +
9295 "%main_lab = OpLabel\n" +
9296 "%n = OpVariable %_ptr_uint Function\n" +
9297 "%4 = OpLoad %uint %n\n" +
9298 "%3 = OpShiftRightLogical %uint %4 %uint_1\n" +
9299 "%2 = OpBitwiseAnd %uint %3 %uint_3221225472\n" +
9300 "OpReturn\n" +
9301 "OpFunctionEnd",
9302 2, 0),
9303
9304 // Test case 7: Do not fold
9305 // 0xc0000000 & (n >> 1)
9306 InstructionFoldingCase<uint32_t>(
9307 Header() +
9308 "%uint_3221225472 = OpConstant %uint 3221225472\n" +
9309 "%main = OpFunction %void None %void_func\n" +
9310 "%main_lab = OpLabel\n" +
9311 "%n = OpVariable %_ptr_uint Function\n" +
9312 "%4 = OpLoad %uint %n\n" +
9313 "%3 = OpShiftRightLogical %uint %4 %uint_1\n" +
9314 "%2 = OpBitwiseAnd %uint %uint_3221225472 %3\n" +
9315 "OpReturn\n" +
9316 "OpFunctionEnd",
9317 2, 0),
9318
9319 // Test case 8: Fold
9320 // 0b1111 & (n << 4) = 0
9321 InstructionFoldingCase<uint32_t>(
9322 Header() +
9323 "%uint_15 = OpConstant %uint 15\n" +
9324 "%main = OpFunction %void None %void_func\n" +
9325 "%main_lab = OpLabel\n" +
9326 "%n = OpVariable %_ptr_uint Function\n" +
9327 "%4 = OpLoad %uint %n\n" +
9328 "%3 = OpShiftLeftLogical %uint %4 %uint_4\n" +
9329 "%2 = OpBitwiseAnd %uint %uint_15 %3\n" +
9330 "OpReturn\n" +
9331 "OpFunctionEnd",
9332 2, UINT_NULL_ID),
9333
9334 // Test case 9: Fold
9335 // 0b1000 & (n << 4) = 0
9336 InstructionFoldingCase<uint32_t>(
9337 Header() +
9338 "%uint_8 = OpConstant %uint 8\n" +
9339 "%main = OpFunction %void None %void_func\n" +
9340 "%main_lab = OpLabel\n" +
9341 "%n = OpVariable %_ptr_uint Function\n" +
9342 "%4 = OpLoad %uint %n\n" +
9343 "%3 = OpShiftLeftLogical %uint %4 %uint_4\n" +
9344 "%2 = OpBitwiseAnd %uint %uint_8 %3\n" +
9345 "OpReturn\n" +
9346 "OpFunctionEnd",
9347 2, UINT_NULL_ID),
9348
9349 // Test case 10: Do not fold
9350 // 0b1111 & (n << 3)
9351 InstructionFoldingCase<uint32_t>(
9352 Header() +
9353 "%uint_15 = OpConstant %uint 15\n" +
9354 "%main = OpFunction %void None %void_func\n" +
9355 "%main_lab = OpLabel\n" +
9356 "%n = OpVariable %_ptr_uint Function\n" +
9357 "%4 = OpLoad %uint %n\n" +
9358 "%3 = OpShiftLeftLogical %uint %4 %uint_3\n" +
9359 "%2 = OpBitwiseAnd %uint %uint_15 %3\n" +
9360 "OpReturn\n" +
9361 "OpFunctionEnd",
9362 2, 0),
9363
9364 // Test case 11: Do not fold
9365 // 0b1000 & (n << 3)
9366 InstructionFoldingCase<uint32_t>(
9367 Header() +
9368 "%uint_8 = OpConstant %uint 8\n" +
9369 "%main = OpFunction %void None %void_func\n" +
9370 "%main_lab = OpLabel\n" +
9371 "%n = OpVariable %_ptr_uint Function\n" +
9372 "%4 = OpLoad %uint %n\n" +
9373 "%3 = OpShiftLeftLogical %uint %4 %uint_3\n" +
9374 "%2 = OpBitwiseAnd %uint %uint_8 %3\n" +
9375 "OpReturn\n" +
9376 "OpFunctionEnd",
9377 2, 0),
9378
9379 // Test case 12: Fold
9380 // 0xf0000000 & (n >> 4) = 0
9381 InstructionFoldingCase<uint32_t>(
9382 Header() +
9383 "%uint_4026531840 = OpConstant %uint 4026531840\n" +
9384 "%main = OpFunction %void None %void_func\n" +
9385 "%main_lab = OpLabel\n" +
9386 "%n = OpVariable %_ptr_uint Function\n" +
9387 "%4 = OpLoad %uint %n\n" +
9388 "%3 = OpShiftRightLogical %uint %4 %uint_4\n" +
9389 "%2 = OpBitwiseAnd %uint %uint_4026531840 %3\n" +
9390 "OpReturn\n" +
9391 "OpFunctionEnd",
9392 2, UINT_NULL_ID),
9393
9394 // Test case 13: Do not fold
9395 // 0xf0000000 & (n >> 3)
9396 InstructionFoldingCase<uint32_t>(
9397 Header() +
9398 "%uint_4026531840 = OpConstant %uint 4026531840\n" +
9399 "%main = OpFunction %void None %void_func\n" +
9400 "%main_lab = OpLabel\n" +
9401 "%n = OpVariable %_ptr_uint Function\n" +
9402 "%4 = OpLoad %uint %n\n" +
9403 "%3 = OpShiftRightLogical %uint %4 %uint_3\n" +
9404 "%2 = OpBitwiseAnd %uint %uint_4026531840 %3\n" +
9405 "OpReturn\n" +
9406 "OpFunctionEnd",
9407 2, 0),
9408
9409 // Test case 14: Do not fold
9410 // 0xf0000001 & (n >> 4)
9411 InstructionFoldingCase<uint32_t>(
9412 Header() +
9413 "%uint_4026531841 = OpConstant %uint 4026531841\n" +
9414 "%main = OpFunction %void None %void_func\n" +
9415 "%main_lab = OpLabel\n" +
9416 "%n = OpVariable %_ptr_uint Function\n" +
9417 "%4 = OpLoad %uint %n\n" +
9418 "%3 = OpShiftRightLogical %uint %4 %uint_4\n" +
9419 "%2 = OpBitwiseAnd %uint %uint_4026531841 %3\n" +
9420 "OpReturn\n" +
9421 "OpFunctionEnd",
9422 2, 0)
9423 ));
9424
9425 INSTANTIATE_TEST_SUITE_P(MergeAddTest, MatchingInstructionFoldingTest,
9426 ::testing::Values(
9427 // Test case 0: merge add of negate
9428 // (-x) + y = y - x
9429 InstructionFoldingCase<bool>(
9430 Header() +
9431 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9432 "; CHECK: [[x:%\\w+]] = OpLoad [[float]]\n" +
9433 "; CHECK: [[y:%\\w+]] = OpLoad [[float]]\n" +
9434 "; CHECK: %4 = OpFSub [[float]] [[y]] [[x]]\n" +
9435 "%main = OpFunction %void None %void_func\n" +
9436 "%main_lab = OpLabel\n" +
9437 "%var = OpVariable %_ptr_float Function\n" +
9438 "%x = OpLoad %float %var\n" +
9439 "%y = OpLoad %float %var\n" +
9440 "%nx = OpFNegate %float %x\n" +
9441 "%4 = OpFAdd %float %nx %y\n" +
9442 "OpReturn\n" +
9443 "OpFunctionEnd\n",
9444 4, true),
9445 // Test case 1: merge add of negate
9446 // y + (-x) = y - x
9447 InstructionFoldingCase<bool>(
9448 Header() +
9449 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9450 "; CHECK: [[x:%\\w+]] = OpLoad [[float]]\n" +
9451 "; CHECK: [[y:%\\w+]] = OpLoad [[float]]\n" +
9452 "; CHECK: %4 = OpFSub [[float]] [[y]] [[x]]\n" +
9453 "%main = OpFunction %void None %void_func\n" +
9454 "%main_lab = OpLabel\n" +
9455 "%var = OpVariable %_ptr_float Function\n" +
9456 "%x = OpLoad %float %var\n" +
9457 "%y = OpLoad %float %var\n" +
9458 "%nx = OpFNegate %float %x\n" +
9459 "%4 = OpFAdd %float %y %nx\n" +
9460 "OpReturn\n" +
9461 "OpFunctionEnd\n",
9462 4, true),
9463 // Test case 2: merge add of negate
9464 // (-x) + y = y - x
9465 InstructionFoldingCase<bool>(
9466 Header() +
9467 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
9468 "; CHECK: [[x:%\\w+]] = OpLoad [[long]]\n" +
9469 "; CHECK: [[y:%\\w+]] = OpLoad [[long]]\n" +
9470 "; CHECK: %4 = OpISub [[long]] [[y]] [[x]]\n" +
9471 "%main = OpFunction %void None %void_func\n" +
9472 "%main_lab = OpLabel\n" +
9473 "%var = OpVariable %_ptr_long Function\n" +
9474 "%x = OpLoad %long %var\n" +
9475 "%y = OpLoad %long %var\n" +
9476 "%nx = OpSNegate %long %x\n" +
9477 "%4 = OpIAdd %long %nx %y\n" +
9478 "OpReturn\n" +
9479 "OpFunctionEnd\n",
9480 4, true),
9481 // Test case 3: merge add of negate
9482 // y + (-x) = y - x
9483 InstructionFoldingCase<bool>(
9484 Header() +
9485 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
9486 "; CHECK: [[x:%\\w+]] = OpLoad [[long]]\n" +
9487 "; CHECK: [[y:%\\w+]] = OpLoad [[long]]\n" +
9488 "; CHECK: %4 = OpISub [[long]] [[y]] [[x]]\n" +
9489 "%main = OpFunction %void None %void_func\n" +
9490 "%main_lab = OpLabel\n" +
9491 "%var = OpVariable %_ptr_long Function\n" +
9492 "%x = OpLoad %long %var\n" +
9493 "%y = OpLoad %long %var\n" +
9494 "%nx = OpSNegate %long %x\n" +
9495 "%4 = OpIAdd %long %y %nx\n" +
9496 "OpReturn\n" +
9497 "OpFunctionEnd\n",
9498 4, true),
9499 // Test case 4: merge add of subtract
9500 // (x - 1) + 2 = x + 1
9501 InstructionFoldingCase<bool>(
9502 Header() +
9503 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9504 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1\n" +
9505 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9506 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_1]]\n" +
9507 "%main = OpFunction %void None %void_func\n" +
9508 "%main_lab = OpLabel\n" +
9509 "%var = OpVariable %_ptr_float Function\n" +
9510 "%2 = OpLoad %float %var\n" +
9511 "%3 = OpFSub %float %2 %float_1\n" +
9512 "%4 = OpFAdd %float %3 %float_2\n" +
9513 "OpReturn\n" +
9514 "OpFunctionEnd\n",
9515 4, true),
9516 // Test case 5: merge add of subtract
9517 // (1 - x) + 2 = 3 - x
9518 InstructionFoldingCase<bool>(
9519 Header() +
9520 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9521 "; CHECK: [[float_3:%\\w+]] = OpConstant [[float]] 3\n" +
9522 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9523 "; CHECK: %4 = OpFSub [[float]] [[float_3]] [[ld]]\n" +
9524 "%main = OpFunction %void None %void_func\n" +
9525 "%main_lab = OpLabel\n" +
9526 "%var = OpVariable %_ptr_float Function\n" +
9527 "%2 = OpLoad %float %var\n" +
9528 "%3 = OpFSub %float %float_1 %2\n" +
9529 "%4 = OpFAdd %float %3 %float_2\n" +
9530 "OpReturn\n" +
9531 "OpFunctionEnd\n",
9532 4, true),
9533 // Test case 6: merge add of subtract
9534 // 2 + (x - 1) = x + 1
9535 InstructionFoldingCase<bool>(
9536 Header() +
9537 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9538 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1\n" +
9539 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9540 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_1]]\n" +
9541 "%main = OpFunction %void None %void_func\n" +
9542 "%main_lab = OpLabel\n" +
9543 "%var = OpVariable %_ptr_float Function\n" +
9544 "%2 = OpLoad %float %var\n" +
9545 "%3 = OpFSub %float %2 %float_1\n" +
9546 "%4 = OpFAdd %float %float_2 %3\n" +
9547 "OpReturn\n" +
9548 "OpFunctionEnd\n",
9549 4, true),
9550 // Test case 7: merge add of subtract
9551 // 2 + (1 - x) = 3 - x
9552 InstructionFoldingCase<bool>(
9553 Header() +
9554 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9555 "; CHECK: [[float_3:%\\w+]] = OpConstant [[float]] 3\n" +
9556 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9557 "; CHECK: %4 = OpFSub [[float]] [[float_3]] [[ld]]\n" +
9558 "%main = OpFunction %void None %void_func\n" +
9559 "%main_lab = OpLabel\n" +
9560 "%var = OpVariable %_ptr_float Function\n" +
9561 "%2 = OpLoad %float %var\n" +
9562 "%3 = OpFSub %float %float_1 %2\n" +
9563 "%4 = OpFAdd %float %float_2 %3\n" +
9564 "OpReturn\n" +
9565 "OpFunctionEnd\n",
9566 4, true),
9567 // Test case 8: merge add of add
9568 // (x + 1) + 2 = x + 3
9569 InstructionFoldingCase<bool>(
9570 Header() +
9571 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9572 "; CHECK: [[float_3:%\\w+]] = OpConstant [[float]] 3\n" +
9573 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9574 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_3]]\n" +
9575 "%main = OpFunction %void None %void_func\n" +
9576 "%main_lab = OpLabel\n" +
9577 "%var = OpVariable %_ptr_float Function\n" +
9578 "%2 = OpLoad %float %var\n" +
9579 "%3 = OpFAdd %float %2 %float_1\n" +
9580 "%4 = OpFAdd %float %3 %float_2\n" +
9581 "OpReturn\n" +
9582 "OpFunctionEnd\n",
9583 4, true),
9584 // Test case 9: merge add of add
9585 // (1 + x) + 2 = 3 + x
9586 InstructionFoldingCase<bool>(
9587 Header() +
9588 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9589 "; CHECK: [[float_3:%\\w+]] = OpConstant [[float]] 3\n" +
9590 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9591 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_3]]\n" +
9592 "%main = OpFunction %void None %void_func\n" +
9593 "%main_lab = OpLabel\n" +
9594 "%var = OpVariable %_ptr_float Function\n" +
9595 "%2 = OpLoad %float %var\n" +
9596 "%3 = OpFAdd %float %float_1 %2\n" +
9597 "%4 = OpFAdd %float %3 %float_2\n" +
9598 "OpReturn\n" +
9599 "OpFunctionEnd\n",
9600 4, true),
9601 // Test case 10: merge add of add
9602 // 2 + (x + 1) = x + 1
9603 InstructionFoldingCase<bool>(
9604 Header() +
9605 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9606 "; CHECK: [[float_3:%\\w+]] = OpConstant [[float]] 3\n" +
9607 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9608 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_3]]\n" +
9609 "%main = OpFunction %void None %void_func\n" +
9610 "%main_lab = OpLabel\n" +
9611 "%var = OpVariable %_ptr_float Function\n" +
9612 "%2 = OpLoad %float %var\n" +
9613 "%3 = OpFAdd %float %2 %float_1\n" +
9614 "%4 = OpFAdd %float %float_2 %3\n" +
9615 "OpReturn\n" +
9616 "OpFunctionEnd\n",
9617 4, true),
9618 // Test case 11: merge add of add
9619 // 2 + (1 + x) = 3 - x
9620 InstructionFoldingCase<bool>(
9621 Header() +
9622 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9623 "; CHECK: [[float_3:%\\w+]] = OpConstant [[float]] 3\n" +
9624 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9625 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_3]]\n" +
9626 "%main = OpFunction %void None %void_func\n" +
9627 "%main_lab = OpLabel\n" +
9628 "%var = OpVariable %_ptr_float Function\n" +
9629 "%2 = OpLoad %float %var\n" +
9630 "%3 = OpFAdd %float %float_1 %2\n" +
9631 "%4 = OpFAdd %float %float_2 %3\n" +
9632 "OpReturn\n" +
9633 "OpFunctionEnd\n",
9634 4, true),
9635 // Test case 12: fold overflowing signed 32 bit iadds
9636 // (x + int_max) + 1 = x + int_min
9637 InstructionFoldingCase<bool>(
9638 Header() +
9639 "; CHECK: [[int:%\\w+]] = OpTypeInt 32\n" +
9640 "; CHECK: [[int_min:%\\w+]] = OpConstant [[int]] -2147483648\n" +
9641 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
9642 "; CHECK: %4 = OpIAdd [[int]] [[ld]] [[int_min]]\n" +
9643 "%main = OpFunction %void None %void_func\n" +
9644 "%main_lab = OpLabel\n" +
9645 "%var = OpVariable %_ptr_int Function\n" +
9646 "%2 = OpLoad %int %var\n" +
9647 "%3 = OpIAdd %int %2 %int_max\n" +
9648 "%4 = OpIAdd %int %3 %int_1\n" +
9649 "OpReturn\n" +
9650 "OpFunctionEnd\n",
9651 4, true),
9652 // Test case 13: fold overflowing signed 64 bit iadds
9653 // (x + long_max) + 1 = x + long_min
9654 InstructionFoldingCase<bool>(
9655 Header() +
9656 "; CHECK: [[long:%\\w+]] = OpTypeInt 64\n" +
9657 "; CHECK: [[long_min:%\\w+]] = OpConstant [[long]] -9223372036854775808\n" +
9658 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
9659 "; CHECK: %4 = OpIAdd [[long]] [[ld]] [[long_min]]\n" +
9660 "%main = OpFunction %void None %void_func\n" +
9661 "%main_lab = OpLabel\n" +
9662 "%var = OpVariable %_ptr_long Function\n" +
9663 "%2 = OpLoad %long %var\n" +
9664 "%3 = OpIAdd %long %2 %long_max\n" +
9665 "%4 = OpIAdd %long %3 %long_1\n" +
9666 "OpReturn\n" +
9667 "OpFunctionEnd\n",
9668 4, true),
9669 // Test case 14: fold overflowing 32 bit unsigned iadds
9670 // (x + uint_max) + 2 = x + 1
9671 InstructionFoldingCase<bool>(
9672 Header() +
9673 "; CHECK: [[uint:%\\w+]] = OpTypeInt 32 0\n" +
9674 "; CHECK: [[uint_1:%\\w+]] = OpConstant [[uint]] 1\n" +
9675 "; CHECK: [[ld:%\\w+]] = OpLoad [[uint]]\n" +
9676 "; CHECK: %4 = OpIAdd [[uint]] [[ld]] [[uint_1]]\n" +
9677 "%main = OpFunction %void None %void_func\n" +
9678 "%main_lab = OpLabel\n" +
9679 "%var = OpVariable %_ptr_uint Function\n" +
9680 "%2 = OpLoad %uint %var\n" +
9681 "%3 = OpIAdd %uint %2 %uint_max\n" +
9682 "%4 = OpIAdd %uint %3 %uint_2\n" +
9683 "OpReturn\n" +
9684 "OpFunctionEnd\n",
9685 4, true),
9686 // Test case 15: fold overflowing 64 bit unsigned iadds
9687 // (x + ulong_max) + 2 = x + 1
9688 InstructionFoldingCase<bool>(
9689 Header() +
9690 "; CHECK: [[ulong:%\\w+]] = OpTypeInt 64 0\n" +
9691 "; CHECK: [[ulong_1:%\\w+]] = OpConstant [[ulong]] 1\n" +
9692 "; CHECK: [[ld:%\\w+]] = OpLoad [[ulong]]\n" +
9693 "; CHECK: %4 = OpIAdd [[ulong]] [[ld]] [[ulong_1]]\n" +
9694 "%main = OpFunction %void None %void_func\n" +
9695 "%main_lab = OpLabel\n" +
9696 "%var = OpVariable %_ptr_ulong Function\n" +
9697 "%2 = OpLoad %ulong %var\n" +
9698 "%3 = OpIAdd %ulong %2 %ulong_max\n" +
9699 "%4 = OpIAdd %ulong %3 %ulong_2\n" +
9700 "OpReturn\n" +
9701 "OpFunctionEnd\n",
9702 4, true),
9703 // Test case 16: fold underflowing signed 32 bit iadds
9704 // (x + int_min) + (-1) = x + int_max
9705 InstructionFoldingCase<bool>(
9706 Header() +
9707 "; CHECK: [[int:%\\w+]] = OpTypeInt 32\n" +
9708 "; CHECK: [[int_max:%\\w+]] = OpConstant [[int]] 2147483647\n" +
9709 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
9710 "; CHECK: %4 = OpIAdd [[int]] [[ld]] [[int_max]]\n" +
9711 "%main = OpFunction %void None %void_func\n" +
9712 "%main_lab = OpLabel\n" +
9713 "%var = OpVariable %_ptr_int Function\n" +
9714 "%2 = OpLoad %int %var\n" +
9715 "%3 = OpIAdd %int %2 %int_min\n" +
9716 "%4 = OpIAdd %int %3 %int_n1\n" +
9717 "OpReturn\n" +
9718 "OpFunctionEnd\n",
9719 4, true),
9720 // Test case 17: fold underflowing signed 64 bit iadds
9721 // (x + long_min) + (-1) = x + long_max
9722 InstructionFoldingCase<bool>(
9723 Header() +
9724 "; CHECK: [[long:%\\w+]] = OpTypeInt 64\n" +
9725 "; CHECK: [[long_max:%\\w+]] = OpConstant [[long]] 9223372036854775807\n" +
9726 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
9727 "; CHECK: %4 = OpIAdd [[long]] [[ld]] [[long_max]]\n" +
9728 "%main = OpFunction %void None %void_func\n" +
9729 "%main_lab = OpLabel\n" +
9730 "%var = OpVariable %_ptr_long Function\n" +
9731 "%2 = OpLoad %long %var\n" +
9732 "%3 = OpIAdd %long %2 %long_min\n" +
9733 "%4 = OpIAdd %long %3 %long_n1\n" +
9734 "OpReturn\n" +
9735 "OpFunctionEnd\n",
9736 4, true)
9737 ));
9738
9739 INSTANTIATE_TEST_SUITE_P(MergeGenericAddSub, MatchingInstructionFoldingTest,
9740 ::testing::Values(
9741 // Test case 0: merge of add of sub
9742 // (a - b) + b => a
9743 InstructionFoldingCase<bool>(
9744 Header() +
9745 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9746 "; CHECK: %6 = OpCopyObject [[float]] %3\n" +
9747 "%main = OpFunction %void None %void_func\n" +
9748 "%main_lab = OpLabel\n" +
9749 "%var0 = OpVariable %_ptr_float Function\n" +
9750 "%var1 = OpVariable %_ptr_float Function\n" +
9751 "%3 = OpLoad %float %var0\n" +
9752 "%4 = OpLoad %float %var1\n" +
9753 "%5 = OpFSub %float %3 %4\n" +
9754 "%6 = OpFAdd %float %5 %4\n" +
9755 "OpReturn\n" +
9756 "OpFunctionEnd\n",
9757 6, true),
9758 // Test case 1: merge of add of sub
9759 // b + (a - b) => a
9760 InstructionFoldingCase<bool>(
9761 Header() +
9762 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9763 "; CHECK: %6 = OpCopyObject [[float]] %3\n" +
9764 "%main = OpFunction %void None %void_func\n" +
9765 "%main_lab = OpLabel\n" +
9766 "%var0 = OpVariable %_ptr_float Function\n" +
9767 "%var1 = OpVariable %_ptr_float Function\n" +
9768 "%3 = OpLoad %float %var0\n" +
9769 "%4 = OpLoad %float %var1\n" +
9770 "%5 = OpFSub %float %3 %4\n" +
9771 "%6 = OpFAdd %float %4 %5\n" +
9772 "OpReturn\n" +
9773 "OpFunctionEnd\n",
9774 6, true)
9775 ));
9776
9777 INSTANTIATE_TEST_SUITE_P(FactorAddMul, MatchingInstructionFoldingTest,
9778 ::testing::Values(
9779 // Test case 0: factor of add of muls
9780 // (a * b) + (a * c) => a * (b + c)
9781 InstructionFoldingCase<bool>(
9782 Header() +
9783 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9784 "; CHECK: [[newadd:%\\w+]] = OpFAdd [[float]] %4 %5\n" +
9785 "; CHECK: %9 = OpFMul [[float]] %6 [[newadd]]\n" +
9786 "%main = OpFunction %void None %void_func\n" +
9787 "%main_lab = OpLabel\n" +
9788 "%var0 = OpVariable %_ptr_float Function\n" +
9789 "%var1 = OpVariable %_ptr_float Function\n" +
9790 "%var2 = OpVariable %_ptr_float Function\n" +
9791 "%4 = OpLoad %float %var0\n" +
9792 "%5 = OpLoad %float %var1\n" +
9793 "%6 = OpLoad %float %var2\n" +
9794 "%7 = OpFMul %float %6 %4\n" +
9795 "%8 = OpFMul %float %6 %5\n" +
9796 "%9 = OpFAdd %float %7 %8\n" +
9797 "OpReturn\n" +
9798 "OpFunctionEnd\n",
9799 9, true),
9800 // Test case 1: factor of add of muls
9801 // (b * a) + (a * c) => a * (b + c)
9802 InstructionFoldingCase<bool>(
9803 Header() +
9804 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9805 "; CHECK: [[newadd:%\\w+]] = OpFAdd [[float]] %4 %5\n" +
9806 "; CHECK: %9 = OpFMul [[float]] %6 [[newadd]]\n" +
9807 "%main = OpFunction %void None %void_func\n" +
9808 "%main_lab = OpLabel\n" +
9809 "%var0 = OpVariable %_ptr_float Function\n" +
9810 "%var1 = OpVariable %_ptr_float Function\n" +
9811 "%var2 = OpVariable %_ptr_float Function\n" +
9812 "%4 = OpLoad %float %var0\n" +
9813 "%5 = OpLoad %float %var1\n" +
9814 "%6 = OpLoad %float %var2\n" +
9815 "%7 = OpFMul %float %4 %6\n" +
9816 "%8 = OpFMul %float %6 %5\n" +
9817 "%9 = OpFAdd %float %7 %8\n" +
9818 "OpReturn\n" +
9819 "OpFunctionEnd\n",
9820 9, true),
9821 // Test case 2: factor of add of muls
9822 // (a * b) + (c * a) => a * (b + c)
9823 InstructionFoldingCase<bool>(
9824 Header() +
9825 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9826 "; CHECK: [[newadd:%\\w+]] = OpFAdd [[float]] %4 %5\n" +
9827 "; CHECK: %9 = OpFMul [[float]] %6 [[newadd]]\n" +
9828 "%main = OpFunction %void None %void_func\n" +
9829 "%main_lab = OpLabel\n" +
9830 "%var0 = OpVariable %_ptr_float Function\n" +
9831 "%var1 = OpVariable %_ptr_float Function\n" +
9832 "%var2 = OpVariable %_ptr_float Function\n" +
9833 "%4 = OpLoad %float %var0\n" +
9834 "%5 = OpLoad %float %var1\n" +
9835 "%6 = OpLoad %float %var2\n" +
9836 "%7 = OpFMul %float %6 %4\n" +
9837 "%8 = OpFMul %float %5 %6\n" +
9838 "%9 = OpFAdd %float %7 %8\n" +
9839 "OpReturn\n" +
9840 "OpFunctionEnd\n",
9841 9, true),
9842 // Test case 3: factor of add of muls
9843 // (b * a) + (c * a) => a * (b + c)
9844 InstructionFoldingCase<bool>(
9845 Header() +
9846 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9847 "; CHECK: [[newadd:%\\w+]] = OpFAdd [[float]] %4 %5\n" +
9848 "; CHECK: %9 = OpFMul [[float]] %6 [[newadd]]\n" +
9849 "%main = OpFunction %void None %void_func\n" +
9850 "%main_lab = OpLabel\n" +
9851 "%var0 = OpVariable %_ptr_float Function\n" +
9852 "%var1 = OpVariable %_ptr_float Function\n" +
9853 "%var2 = OpVariable %_ptr_float Function\n" +
9854 "%4 = OpLoad %float %var0\n" +
9855 "%5 = OpLoad %float %var1\n" +
9856 "%6 = OpLoad %float %var2\n" +
9857 "%7 = OpFMul %float %4 %6\n" +
9858 "%8 = OpFMul %float %5 %6\n" +
9859 "%9 = OpFAdd %float %7 %8\n" +
9860 "OpReturn\n" +
9861 "OpFunctionEnd\n",
9862 9, true)
9863 ));
9864
9865 INSTANTIATE_TEST_SUITE_P(FactorSubMul, MatchingInstructionFoldingTest,
9866 ::testing::Values(
9867 // Test case 0: factor of sub of muls
9868 // (a * b) - (a * c) => a * (b - c)
9869 InstructionFoldingCase<bool>(
9870 Header() +
9871 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9872 "; CHECK: [[newsub:%\\w+]] = OpFSub [[float]] %4 %5\n" +
9873 "; CHECK: %9 = OpFMul [[float]] %6 [[newsub]]\n" +
9874 "%main = OpFunction %void None %void_func\n" +
9875 "%main_lab = OpLabel\n" +
9876 "%var0 = OpVariable %_ptr_float Function\n" +
9877 "%var1 = OpVariable %_ptr_float Function\n" +
9878 "%var2 = OpVariable %_ptr_float Function\n" +
9879 "%4 = OpLoad %float %var0\n" +
9880 "%5 = OpLoad %float %var1\n" +
9881 "%6 = OpLoad %float %var2\n" +
9882 "%7 = OpFMul %float %6 %4\n" +
9883 "%8 = OpFMul %float %6 %5\n" +
9884 "%9 = OpFSub %float %7 %8\n" +
9885 "OpReturn\n" +
9886 "OpFunctionEnd\n",
9887 9, true),
9888 // Test case 1: factor of sub of muls
9889 // (b * a) - (a * c) => a * (b - c)
9890 InstructionFoldingCase<bool>(
9891 Header() +
9892 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9893 "; CHECK: [[newsub:%\\w+]] = OpFSub [[float]] %4 %5\n" +
9894 "; CHECK: %9 = OpFMul [[float]] %6 [[newsub]]\n" +
9895 "%main = OpFunction %void None %void_func\n" +
9896 "%main_lab = OpLabel\n" +
9897 "%var0 = OpVariable %_ptr_float Function\n" +
9898 "%var1 = OpVariable %_ptr_float Function\n" +
9899 "%var2 = OpVariable %_ptr_float Function\n" +
9900 "%4 = OpLoad %float %var0\n" +
9901 "%5 = OpLoad %float %var1\n" +
9902 "%6 = OpLoad %float %var2\n" +
9903 "%7 = OpFMul %float %4 %6\n" +
9904 "%8 = OpFMul %float %6 %5\n" +
9905 "%9 = OpFSub %float %7 %8\n" +
9906 "OpReturn\n" +
9907 "OpFunctionEnd\n",
9908 9, true),
9909 // Test case 2: factor of sub of muls
9910 // (a * b) - (c * a) => a * (b - c)
9911 InstructionFoldingCase<bool>(
9912 Header() +
9913 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9914 "; CHECK: [[newsub:%\\w+]] = OpFSub [[float]] %4 %5\n" +
9915 "; CHECK: %9 = OpFMul [[float]] %6 [[newsub]]\n" +
9916 "%main = OpFunction %void None %void_func\n" +
9917 "%main_lab = OpLabel\n" +
9918 "%var0 = OpVariable %_ptr_float Function\n" +
9919 "%var1 = OpVariable %_ptr_float Function\n" +
9920 "%var2 = OpVariable %_ptr_float Function\n" +
9921 "%4 = OpLoad %float %var0\n" +
9922 "%5 = OpLoad %float %var1\n" +
9923 "%6 = OpLoad %float %var2\n" +
9924 "%7 = OpFMul %float %6 %4\n" +
9925 "%8 = OpFMul %float %5 %6\n" +
9926 "%9 = OpFSub %float %7 %8\n" +
9927 "OpReturn\n" +
9928 "OpFunctionEnd\n",
9929 9, true),
9930 // Test case 3: factor of sub of muls
9931 // (b * a) - (c * a) => a * (b - c)
9932 InstructionFoldingCase<bool>(
9933 Header() +
9934 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9935 "; CHECK: [[newsub:%\\w+]] = OpFSub [[float]] %4 %5\n" +
9936 "; CHECK: %9 = OpFMul [[float]] %6 [[newsub]]\n" +
9937 "%main = OpFunction %void None %void_func\n" +
9938 "%main_lab = OpLabel\n" +
9939 "%var0 = OpVariable %_ptr_float Function\n" +
9940 "%var1 = OpVariable %_ptr_float Function\n" +
9941 "%var2 = OpVariable %_ptr_float Function\n" +
9942 "%4 = OpLoad %float %var0\n" +
9943 "%5 = OpLoad %float %var1\n" +
9944 "%6 = OpLoad %float %var2\n" +
9945 "%7 = OpFMul %float %4 %6\n" +
9946 "%8 = OpFMul %float %5 %6\n" +
9947 "%9 = OpFSub %float %7 %8\n" +
9948 "OpReturn\n" +
9949 "OpFunctionEnd\n",
9950 9, true)
9951 ));
9952
9953 INSTANTIATE_TEST_SUITE_P(MergeSubTest, MatchingInstructionFoldingTest,
9954 ::testing::Values(
9955 // Test case 0: merge sub of negate
9956 // (-x) - 2 = -2 - x
9957 InstructionFoldingCase<bool>(
9958 Header() +
9959 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9960 "; CHECK: [[float_n2:%\\w+]] = OpConstant [[float]] -2{{[[:space:]]}}\n" +
9961 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
9962 "; CHECK: %4 = OpFSub [[float]] [[float_n2]] [[ld]]\n" +
9963 "%main = OpFunction %void None %void_func\n" +
9964 "%main_lab = OpLabel\n" +
9965 "%var = OpVariable %_ptr_float Function\n" +
9966 "%2 = OpLoad %float %var\n" +
9967 "%3 = OpFNegate %float %2\n" +
9968 "%4 = OpFSub %float %3 %float_2\n" +
9969 "OpReturn\n" +
9970 "OpFunctionEnd\n",
9971 4, true),
9972 // Test case 1: merge sub of negate
9973 // y - (-x) = y + x
9974 InstructionFoldingCase<bool>(
9975 Header() +
9976 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
9977 "; CHECK: [[x:%\\w+]] = OpLoad [[float]]\n" +
9978 "; CHECK: [[y:%\\w+]] = OpLoad [[float]]\n" +
9979 "; CHECK: %4 = OpFAdd [[float]] [[y]] [[x]]\n" +
9980 "%main = OpFunction %void None %void_func\n" +
9981 "%main_lab = OpLabel\n" +
9982 "%var = OpVariable %_ptr_float Function\n" +
9983 "%x = OpLoad %float %var\n" +
9984 "%y = OpLoad %float %var\n" +
9985 "%nx = OpFNegate %float %x\n" +
9986 "%4 = OpFSub %float %y %nx\n" +
9987 "OpReturn\n" +
9988 "OpFunctionEnd\n",
9989 4, true),
9990 // Test case 2: merge sub of negate
9991 // (-x) - 2 = -2 - x
9992 InstructionFoldingCase<bool>(
9993 Header() +
9994 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
9995 "; CHECK: [[long_n2:%\\w+]] = OpConstant [[long]] -2{{[[:space:]]}}\n" +
9996 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
9997 "; CHECK: %4 = OpISub [[long]] [[long_n2]] [[ld]]\n" +
9998 "%main = OpFunction %void None %void_func\n" +
9999 "%main_lab = OpLabel\n" +
10000 "%var = OpVariable %_ptr_long Function\n" +
10001 "%2 = OpLoad %long %var\n" +
10002 "%3 = OpSNegate %long %2\n" +
10003 "%4 = OpISub %long %3 %long_2\n" +
10004 "OpReturn\n" +
10005 "OpFunctionEnd\n",
10006 4, true),
10007 // Test case 3: merge sub of negate
10008 // y - (-x) = y + x
10009 InstructionFoldingCase<bool>(
10010 Header() +
10011 "; CHECK: [[long:%\\w+]] = OpTypeInt 64 1\n" +
10012 "; CHECK: [[x:%\\w+]] = OpLoad [[long]]\n" +
10013 "; CHECK: [[y:%\\w+]] = OpLoad [[long]]\n" +
10014 "; CHECK: %4 = OpIAdd [[long]] [[y]] [[x]]\n" +
10015 "%main = OpFunction %void None %void_func\n" +
10016 "%main_lab = OpLabel\n" +
10017 "%var = OpVariable %_ptr_long Function\n" +
10018 "%x = OpLoad %long %var\n" +
10019 "%y = OpLoad %long %var\n" +
10020 "%nx = OpSNegate %long %x\n" +
10021 "%4 = OpISub %long %y %nx\n" +
10022 "OpReturn\n" +
10023 "OpFunctionEnd\n",
10024 4, true),
10025 // Test case 4: merge add of subtract
10026 // (x + 2) - 1 = x + 1
10027 InstructionFoldingCase<bool>(
10028 Header() +
10029 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10030 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1\n" +
10031 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10032 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_1]]\n" +
10033 "%main = OpFunction %void None %void_func\n" +
10034 "%main_lab = OpLabel\n" +
10035 "%var = OpVariable %_ptr_float Function\n" +
10036 "%2 = OpLoad %float %var\n" +
10037 "%3 = OpFAdd %float %2 %float_2\n" +
10038 "%4 = OpFSub %float %3 %float_1\n" +
10039 "OpReturn\n" +
10040 "OpFunctionEnd\n",
10041 4, true),
10042 // Test case 5: merge add of subtract
10043 // (2 + x) - 1 = x + 1
10044 InstructionFoldingCase<bool>(
10045 Header() +
10046 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10047 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1\n" +
10048 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10049 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_1]]\n" +
10050 "%main = OpFunction %void None %void_func\n" +
10051 "%main_lab = OpLabel\n" +
10052 "%var = OpVariable %_ptr_float Function\n" +
10053 "%2 = OpLoad %float %var\n" +
10054 "%3 = OpFAdd %float %float_2 %2\n" +
10055 "%4 = OpFSub %float %3 %float_1\n" +
10056 "OpReturn\n" +
10057 "OpFunctionEnd\n",
10058 4, true),
10059 // Test case 6: merge add of subtract
10060 // 2 - (x + 1) = 1 - x
10061 InstructionFoldingCase<bool>(
10062 Header() +
10063 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10064 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1\n" +
10065 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10066 "; CHECK: %4 = OpFSub [[float]] [[float_1]] [[ld]]\n" +
10067 "%main = OpFunction %void None %void_func\n" +
10068 "%main_lab = OpLabel\n" +
10069 "%var = OpVariable %_ptr_float Function\n" +
10070 "%2 = OpLoad %float %var\n" +
10071 "%3 = OpFAdd %float %2 %float_1\n" +
10072 "%4 = OpFSub %float %float_2 %3\n" +
10073 "OpReturn\n" +
10074 "OpFunctionEnd\n",
10075 4, true),
10076 // Test case 7: merge add of subtract
10077 // 2 - (1 + x) = 1 - x
10078 InstructionFoldingCase<bool>(
10079 Header() +
10080 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10081 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1\n" +
10082 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10083 "; CHECK: %4 = OpFSub [[float]] [[float_1]] [[ld]]\n" +
10084 "%main = OpFunction %void None %void_func\n" +
10085 "%main_lab = OpLabel\n" +
10086 "%var = OpVariable %_ptr_float Function\n" +
10087 "%2 = OpLoad %float %var\n" +
10088 "%3 = OpFAdd %float %float_1 %2\n" +
10089 "%4 = OpFSub %float %float_2 %3\n" +
10090 "OpReturn\n" +
10091 "OpFunctionEnd\n",
10092 4, true),
10093 // Test case 8: merge subtract of subtract
10094 // (x - 2) - 1 = x - 3
10095 InstructionFoldingCase<bool>(
10096 Header() +
10097 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10098 "; CHECK: [[float_3:%\\w+]] = OpConstant [[float]] 3\n" +
10099 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10100 "; CHECK: %4 = OpFSub [[float]] [[ld]] [[float_3]]\n" +
10101 "%main = OpFunction %void None %void_func\n" +
10102 "%main_lab = OpLabel\n" +
10103 "%var = OpVariable %_ptr_float Function\n" +
10104 "%2 = OpLoad %float %var\n" +
10105 "%3 = OpFSub %float %2 %float_2\n" +
10106 "%4 = OpFSub %float %3 %float_1\n" +
10107 "OpReturn\n" +
10108 "OpFunctionEnd\n",
10109 4, true),
10110 // Test case 9: merge subtract of subtract
10111 // (2 - x) - 1 = 1 - x
10112 InstructionFoldingCase<bool>(
10113 Header() +
10114 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10115 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1\n" +
10116 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10117 "; CHECK: %4 = OpFSub [[float]] [[float_1]] [[ld]]\n" +
10118 "%main = OpFunction %void None %void_func\n" +
10119 "%main_lab = OpLabel\n" +
10120 "%var = OpVariable %_ptr_float Function\n" +
10121 "%2 = OpLoad %float %var\n" +
10122 "%3 = OpFSub %float %float_2 %2\n" +
10123 "%4 = OpFSub %float %3 %float_1\n" +
10124 "OpReturn\n" +
10125 "OpFunctionEnd\n",
10126 4, true),
10127 // Test case 10: merge subtract of subtract
10128 // 2 - (x - 1) = 3 - x
10129 InstructionFoldingCase<bool>(
10130 Header() +
10131 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10132 "; CHECK: [[float_3:%\\w+]] = OpConstant [[float]] 3\n" +
10133 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10134 "; CHECK: %4 = OpFSub [[float]] [[float_3]] [[ld]]\n" +
10135 "%main = OpFunction %void None %void_func\n" +
10136 "%main_lab = OpLabel\n" +
10137 "%var = OpVariable %_ptr_float Function\n" +
10138 "%2 = OpLoad %float %var\n" +
10139 "%3 = OpFSub %float %2 %float_1\n" +
10140 "%4 = OpFSub %float %float_2 %3\n" +
10141 "OpReturn\n" +
10142 "OpFunctionEnd\n",
10143 4, true),
10144 // Test case 11: merge subtract of subtract
10145 // 1 - (2 - x) = x + (-1)
10146 InstructionFoldingCase<bool>(
10147 Header() +
10148 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10149 "; CHECK: [[float_n1:%\\w+]] = OpConstant [[float]] -1\n" +
10150 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10151 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_n1]]\n" +
10152 "%main = OpFunction %void None %void_func\n" +
10153 "%main_lab = OpLabel\n" +
10154 "%var = OpVariable %_ptr_float Function\n" +
10155 "%2 = OpLoad %float %var\n" +
10156 "%3 = OpFSub %float %float_2 %2\n" +
10157 "%4 = OpFSub %float %float_1 %3\n" +
10158 "OpReturn\n" +
10159 "OpFunctionEnd\n",
10160 4, true),
10161 // Test case 12: merge subtract of subtract
10162 // 2 - (1 - x) = x + 1
10163 InstructionFoldingCase<bool>(
10164 Header() +
10165 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10166 "; CHECK: [[float_1:%\\w+]] = OpConstant [[float]] 1\n" +
10167 "; CHECK: [[ld:%\\w+]] = OpLoad [[float]]\n" +
10168 "; CHECK: %4 = OpFAdd [[float]] [[ld]] [[float_1]]\n" +
10169 "%main = OpFunction %void None %void_func\n" +
10170 "%main_lab = OpLabel\n" +
10171 "%var = OpVariable %_ptr_float Function\n" +
10172 "%2 = OpLoad %float %var\n" +
10173 "%3 = OpFSub %float %float_1 %2\n" +
10174 "%4 = OpFSub %float %float_2 %3\n" +
10175 "OpReturn\n" +
10176 "OpFunctionEnd\n",
10177 4, true),
10178 // Test case 13: merge subtract of subtract with mixed types.
10179 // 2 - (1 - x) = x + 1
10180 InstructionFoldingCase<bool>(
10181 Header() +
10182 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10183 "; CHECK: [[int_1:%\\w+]] = OpConstant [[int]] 1\n" +
10184 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
10185 "; CHECK: %4 = OpIAdd [[int]] [[ld]] [[int_1]]\n" +
10186 "%main = OpFunction %void None %void_func\n" +
10187 "%main_lab = OpLabel\n" +
10188 "%var = OpVariable %_ptr_int Function\n" +
10189 "%2 = OpLoad %int %var\n" +
10190 "%3 = OpISub %int %uint_1 %2\n" +
10191 "%4 = OpISub %int %int_2 %3\n" +
10192 "OpReturn\n" +
10193 "OpFunctionEnd\n",
10194 4, true),
10195 // Test case 14: fold overflowing signed 32 bit isubs
10196 // (x - int_max) - 1 = x - int_min
10197 InstructionFoldingCase<bool>(
10198 Header() +
10199 "; CHECK: [[int:%\\w+]] = OpTypeInt 32\n" +
10200 "; CHECK: [[int_min:%\\w+]] = OpConstant [[int]] -2147483648\n" +
10201 "; CHECK: [[ld:%\\w+]] = OpLoad [[int]]\n" +
10202 "; CHECK: %4 = OpISub [[int]] [[ld]] [[int_min]]\n" +
10203 "%main = OpFunction %void None %void_func\n" +
10204 "%main_lab = OpLabel\n" +
10205 "%var = OpVariable %_ptr_int Function\n" +
10206 "%2 = OpLoad %int %var\n" +
10207 "%3 = OpISub %int %2 %int_max\n" +
10208 "%4 = OpISub %int %3 %int_1\n" +
10209 "OpReturn\n" +
10210 "OpFunctionEnd\n",
10211 4, true),
10212 // Test case 15: fold overflowing signed 64 bit isubs
10213 // (x - long_max) - 1 = x - long_min
10214 InstructionFoldingCase<bool>(
10215 Header() +
10216 "; CHECK: [[long:%\\w+]] = OpTypeInt 64\n" +
10217 "; CHECK: [[long_min:%\\w+]] = OpConstant [[long]] -9223372036854775808\n" +
10218 "; CHECK: [[ld:%\\w+]] = OpLoad [[long]]\n" +
10219 "; CHECK: %4 = OpISub [[long]] [[ld]] [[long_min]]\n" +
10220 "%main = OpFunction %void None %void_func\n" +
10221 "%main_lab = OpLabel\n" +
10222 "%var = OpVariable %_ptr_long Function\n" +
10223 "%2 = OpLoad %long %var\n" +
10224 "%3 = OpISub %long %2 %long_max\n" +
10225 "%4 = OpISub %long %3 %long_1\n" +
10226 "OpReturn\n" +
10227 "OpFunctionEnd\n",
10228 4, true)
10229 ));
10230
10231 INSTANTIATE_TEST_SUITE_P(SelectFoldingTest, MatchingInstructionFoldingTest,
10232 ::testing::Values(
10233 // Test case 0: Fold select with the same values for both sides
10234 InstructionFoldingCase<bool>(
10235 Header() +
10236 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10237 "; CHECK: [[int0:%\\w+]] = OpConstant [[int]] 0\n" +
10238 "; CHECK: %2 = OpCopyObject [[int]] [[int0]]\n" +
10239 "%main = OpFunction %void None %void_func\n" +
10240 "%main_lab = OpLabel\n" +
10241 "%n = OpVariable %_ptr_bool Function\n" +
10242 "%load = OpLoad %bool %n\n" +
10243 "%2 = OpSelect %int %load %100 %100\n" +
10244 "OpReturn\n" +
10245 "OpFunctionEnd",
10246 2, true),
10247 // Test case 1: Fold select true to left side
10248 InstructionFoldingCase<bool>(
10249 Header() +
10250 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10251 "; CHECK: [[int0:%\\w+]] = OpConstant [[int]] 0\n" +
10252 "; CHECK: %2 = OpCopyObject [[int]] [[int0]]\n" +
10253 "%main = OpFunction %void None %void_func\n" +
10254 "%main_lab = OpLabel\n" +
10255 "%n = OpVariable %_ptr_int Function\n" +
10256 "%load = OpLoad %bool %n\n" +
10257 "%2 = OpSelect %int %true %100 %n\n" +
10258 "OpReturn\n" +
10259 "OpFunctionEnd",
10260 2, true),
10261 // Test case 2: Fold select false to right side
10262 InstructionFoldingCase<bool>(
10263 Header() +
10264 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10265 "; CHECK: [[int0:%\\w+]] = OpConstant [[int]] 0\n" +
10266 "; CHECK: %2 = OpCopyObject [[int]] [[int0]]\n" +
10267 "%main = OpFunction %void None %void_func\n" +
10268 "%main_lab = OpLabel\n" +
10269 "%n = OpVariable %_ptr_int Function\n" +
10270 "%load = OpLoad %bool %n\n" +
10271 "%2 = OpSelect %int %false %n %100\n" +
10272 "OpReturn\n" +
10273 "OpFunctionEnd",
10274 2, true),
10275 // Test case 3: Fold select null to right side
10276 InstructionFoldingCase<bool>(
10277 Header() +
10278 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10279 "; CHECK: [[int0:%\\w+]] = OpConstant [[int]] 0\n" +
10280 "; CHECK: %2 = OpCopyObject [[int]] [[int0]]\n" +
10281 "%main = OpFunction %void None %void_func\n" +
10282 "%main_lab = OpLabel\n" +
10283 "%n = OpVariable %_ptr_int Function\n" +
10284 "%load = OpLoad %int %n\n" +
10285 "%2 = OpSelect %int %bool_null %load %100\n" +
10286 "OpReturn\n" +
10287 "OpFunctionEnd",
10288 2, true),
10289 // Test case 4: vector null
10290 InstructionFoldingCase<bool>(
10291 Header() +
10292 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10293 "; CHECK: [[v2int:%\\w+]] = OpTypeVector [[int]] 2\n" +
10294 "; CHECK: [[int2:%\\w+]] = OpConstant [[int]] 2\n" +
10295 "; CHECK: [[v2int2_2:%\\w+]] = OpConstantComposite [[v2int]] [[int2]] [[int2]]\n" +
10296 "; CHECK: %2 = OpCopyObject [[v2int]] [[v2int2_2]]\n" +
10297 "%main = OpFunction %void None %void_func\n" +
10298 "%main_lab = OpLabel\n" +
10299 "%n = OpVariable %_ptr_v2int Function\n" +
10300 "%load = OpLoad %v2int %n\n" +
10301 "%2 = OpSelect %v2int %v2bool_null %load %v2int_2_2\n" +
10302 "OpReturn\n" +
10303 "OpFunctionEnd",
10304 2, true),
10305 // Test case 5: vector select
10306 InstructionFoldingCase<bool>(
10307 Header() +
10308 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10309 "; CHECK: [[v2int:%\\w+]] = OpTypeVector [[int]] 2\n" +
10310 "; CHECK: %4 = OpVectorShuffle [[v2int]] %2 %3 0 3\n" +
10311 "%main = OpFunction %void None %void_func\n" +
10312 "%main_lab = OpLabel\n" +
10313 "%m = OpVariable %_ptr_v2int Function\n" +
10314 "%n = OpVariable %_ptr_v2int Function\n" +
10315 "%2 = OpLoad %v2int %n\n" +
10316 "%3 = OpLoad %v2int %n\n" +
10317 "%4 = OpSelect %v2int %v2bool_true_false %2 %3\n" +
10318 "OpReturn\n" +
10319 "OpFunctionEnd",
10320 4, true),
10321 // Test case 6: vector select
10322 InstructionFoldingCase<bool>(
10323 Header() +
10324 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10325 "; CHECK: [[v2int:%\\w+]] = OpTypeVector [[int]] 2\n" +
10326 "; CHECK: %4 = OpVectorShuffle [[v2int]] %2 %3 2 1\n" +
10327 "%main = OpFunction %void None %void_func\n" +
10328 "%main_lab = OpLabel\n" +
10329 "%m = OpVariable %_ptr_v2int Function\n" +
10330 "%n = OpVariable %_ptr_v2int Function\n" +
10331 "%2 = OpLoad %v2int %n\n" +
10332 "%3 = OpLoad %v2int %n\n" +
10333 "%4 = OpSelect %v2int %v2bool_false_true %2 %3\n" +
10334 "OpReturn\n" +
10335 "OpFunctionEnd",
10336 4, true),
10337 // Test case 7: Fold select with different type of zeros for both sides
10338 InstructionFoldingCase<bool>(
10339 Header() +
10340 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
10341 "; CHECK: [[float_null:%\\w+]] = OpConstantNull [[float]]\n" +
10342 "; CHECK: %2 = OpCopyObject [[float]] [[float_null]]\n" +
10343 "%main = OpFunction %void None %void_func\n" +
10344 "%main_lab = OpLabel\n" +
10345 "%n = OpVariable %_ptr_bool Function\n" +
10346 "%load = OpLoad %bool %n\n" +
10347 "%2 = OpSelect %float %load %float_null %float_0\n" +
10348 "OpReturn\n" +
10349 "OpFunctionEnd",
10350 2, true)
10351 ));
10352
10353 INSTANTIATE_TEST_SUITE_P(FoldConstantBooleanSelectTest, MatchingInstructionFoldingTest,
10354 ::testing::Values(
10355 // Test case 0:
10356 // x ? true : false = x
10357 InstructionFoldingCase<bool>(
10358 Header() +
10359 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10360 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10361 "%main = OpFunction %void None %void_func\n" +
10362 "%main_lab = OpLabel\n" +
10363 "%n = OpVariable %_ptr_bool Function\n" +
10364 "%3 = OpLoad %bool %n\n" +
10365 "%2 = OpSelect %bool %3 %true %false\n" +
10366 "OpReturn\n" +
10367 "OpFunctionEnd",
10368 2, true),
10369 // Test case 1:
10370 // x ? false : true = !x
10371 InstructionFoldingCase<bool>(
10372 Header() +
10373 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10374 "; CHECK: %2 = OpLogicalNot [[bool]] %3\n" +
10375 "%main = OpFunction %void None %void_func\n" +
10376 "%main_lab = OpLabel\n" +
10377 "%n = OpVariable %_ptr_bool Function\n" +
10378 "%3 = OpLoad %bool %n\n" +
10379 "%2 = OpSelect %bool %3 %false %true\n" +
10380 "OpReturn\n" +
10381 "OpFunctionEnd",
10382 2, true),
10383 // Test case 2:
10384 // x ? true : null_false = x
10385 InstructionFoldingCase<bool>(
10386 Header() +
10387 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10388 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10389 "%main = OpFunction %void None %void_func\n" +
10390 "%main_lab = OpLabel\n" +
10391 "%n = OpVariable %_ptr_bool Function\n" +
10392 "%3 = OpLoad %bool %n\n" +
10393 "%2 = OpSelect %bool %3 %true %bool_null\n" +
10394 "OpReturn\n" +
10395 "OpFunctionEnd",
10396 2, true),
10397 // Test case 3:
10398 // x ? null_false : true = !x
10399 InstructionFoldingCase<bool>(
10400 Header() +
10401 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10402 "; CHECK: %2 = OpLogicalNot [[bool]] %3\n" +
10403 "%main = OpFunction %void None %void_func\n" +
10404 "%main_lab = OpLabel\n" +
10405 "%n = OpVariable %_ptr_bool Function\n" +
10406 "%3 = OpLoad %bool %n\n" +
10407 "%2 = OpSelect %bool %3 %bool_null %true\n" +
10408 "OpReturn\n" +
10409 "OpFunctionEnd",
10410 2, true)
10411 ));
10412
10413 INSTANTIATE_TEST_SUITE_P(RedundantLogicalAndTest, MatchingInstructionFoldingTest,
10414 ::testing::Values(
10415 // Test case 0:
10416 // x && true = x
10417 InstructionFoldingCase<bool>(
10418 Header() +
10419 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10420 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10421 "%main = OpFunction %void None %void_func\n" +
10422 "%main_lab = OpLabel\n" +
10423 "%n = OpVariable %_ptr_bool Function\n" +
10424 "%3 = OpLoad %bool %n\n" +
10425 "%2 = OpLogicalAnd %bool %3 %true\n" +
10426 "OpReturn\n" +
10427 "OpFunctionEnd",
10428 2, true),
10429 // Test case 1:
10430 // true && x = x
10431 InstructionFoldingCase<bool>(
10432 Header() +
10433 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10434 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10435 "%main = OpFunction %void None %void_func\n" +
10436 "%main_lab = OpLabel\n" +
10437 "%n = OpVariable %_ptr_bool Function\n" +
10438 "%3 = OpLoad %bool %n\n" +
10439 "%2 = OpLogicalAnd %bool %true %3\n" +
10440 "OpReturn\n" +
10441 "OpFunctionEnd",
10442 2, true)
10443 ));
10444
10445 INSTANTIATE_TEST_SUITE_P(RedundantLogicalOrTest, MatchingInstructionFoldingTest,
10446 ::testing::Values(
10447 // Test case 0:
10448 // x || false = x
10449 InstructionFoldingCase<bool>(
10450 Header() +
10451 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10452 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10453 "%main = OpFunction %void None %void_func\n" +
10454 "%main_lab = OpLabel\n" +
10455 "%n = OpVariable %_ptr_bool Function\n" +
10456 "%3 = OpLoad %bool %n\n" +
10457 "%2 = OpLogicalOr %bool %3 %false\n" +
10458 "OpReturn\n" +
10459 "OpFunctionEnd",
10460 2, true),
10461 // Test case 1:
10462 // false || x = x
10463 InstructionFoldingCase<bool>(
10464 Header() +
10465 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10466 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10467 "%main = OpFunction %void None %void_func\n" +
10468 "%main_lab = OpLabel\n" +
10469 "%n = OpVariable %_ptr_bool Function\n" +
10470 "%3 = OpLoad %bool %n\n" +
10471 "%2 = OpLogicalOr %bool %false %3\n" +
10472 "OpReturn\n" +
10473 "OpFunctionEnd",
10474 2, true),
10475 // Test case 2:
10476 // x || null_false = x
10477 InstructionFoldingCase<bool>(
10478 Header() +
10479 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10480 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10481 "%main = OpFunction %void None %void_func\n" +
10482 "%main_lab = OpLabel\n" +
10483 "%n = OpVariable %_ptr_bool Function\n" +
10484 "%3 = OpLoad %bool %n\n" +
10485 "%2 = OpLogicalOr %bool %3 %bool_null\n" +
10486 "OpReturn\n" +
10487 "OpFunctionEnd",
10488 2, true),
10489 // Test case 3:
10490 // null_false || x = x
10491 InstructionFoldingCase<bool>(
10492 Header() +
10493 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10494 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10495 "%main = OpFunction %void None %void_func\n" +
10496 "%main_lab = OpLabel\n" +
10497 "%n = OpVariable %_ptr_bool Function\n" +
10498 "%3 = OpLoad %bool %n\n" +
10499 "%2 = OpLogicalOr %bool %bool_null %3\n" +
10500 "OpReturn\n" +
10501 "OpFunctionEnd",
10502 2, true)
10503 ));
10504
10505 INSTANTIATE_TEST_SUITE_P(RedundantLogicalNotTest, MatchingInstructionFoldingTest,
10506 ::testing::Values(
10507 // Test case 0:
10508 // !!x = x
10509 InstructionFoldingCase<bool>(
10510 Header() +
10511 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10512 "; CHECK: %2 = OpCopyObject [[bool]] %3\n" +
10513 "%main = OpFunction %void None %void_func\n" +
10514 "%main_lab = OpLabel\n" +
10515 "%n = OpVariable %_ptr_bool Function\n" +
10516 "%3 = OpLoad %bool %n\n" +
10517 "%4 = OpLogicalNot %bool %3\n" +
10518 "%2 = OpLogicalNot %bool %4\n" +
10519 "OpReturn\n" +
10520 "OpFunctionEnd",
10521 2, true)
10522 ));
10523
10524 INSTANTIATE_TEST_SUITE_P(FoldLogicalNotComparisonTest, MatchingInstructionFoldingTest,
10525 ::testing::Values(
10526 // Test case 0:
10527 // !(a == b) = (a != b) [OpIEqual => OpINotEqual]
10528 InstructionFoldingCase<bool>(
10529 Header() +
10530 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10531 "; CHECK: %2 = OpINotEqual [[bool]] %3 %4\n" +
10532 "%main = OpFunction %void None %void_func\n" +
10533 "%main_lab = OpLabel\n" +
10534 "%n = OpVariable %_ptr_int Function\n" +
10535 "%m = OpVariable %_ptr_int Function\n" +
10536 "%3 = OpLoad %int %n\n" +
10537 "%4 = OpLoad %int %m\n" +
10538 "%5 = OpIEqual %bool %3 %4\n" +
10539 "%2 = OpLogicalNot %bool %5\n" +
10540 "OpReturn\n" +
10541 "OpFunctionEnd",
10542 2, true),
10543 // Test case 1:
10544 // !(a != b) = (a == b) [OpINotEqual => OpIEqual]
10545 InstructionFoldingCase<bool>(
10546 Header() +
10547 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10548 "; CHECK: %2 = OpIEqual [[bool]] %3 %4\n" +
10549 "%main = OpFunction %void None %void_func\n" +
10550 "%main_lab = OpLabel\n" +
10551 "%n = OpVariable %_ptr_int Function\n" +
10552 "%m = OpVariable %_ptr_int Function\n" +
10553 "%3 = OpLoad %int %n\n" +
10554 "%4 = OpLoad %int %m\n" +
10555 "%5 = OpINotEqual %bool %3 %4\n" +
10556 "%2 = OpLogicalNot %bool %5\n" +
10557 "OpReturn\n" +
10558 "OpFunctionEnd",
10559 2, true),
10560 // Test case 2:
10561 // !(a == b) = (a != b) [OpFOrdEqual => OpFUnordNotEqual]
10562 InstructionFoldingCase<bool>(
10563 Header() +
10564 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10565 "; CHECK: %2 = OpFUnordNotEqual [[bool]] %3 %4\n" +
10566 "%main = OpFunction %void None %void_func\n" +
10567 "%main_lab = OpLabel\n" +
10568 "%n = OpVariable %_ptr_float Function\n" +
10569 "%m = OpVariable %_ptr_float Function\n" +
10570 "%3 = OpLoad %float %n\n" +
10571 "%4 = OpLoad %float %m\n" +
10572 "%5 = OpFOrdEqual %bool %3 %4\n" +
10573 "%2 = OpLogicalNot %bool %5\n" +
10574 "OpReturn\n" +
10575 "OpFunctionEnd",
10576 2, true),
10577 // Test case 3:
10578 // !(a != b) = (a == b) [OpFUnordNotEqual => OpFOrdEqual]
10579 InstructionFoldingCase<bool>(
10580 Header() +
10581 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10582 "; CHECK: %2 = OpFOrdEqual [[bool]] %3 %4\n" +
10583 "%main = OpFunction %void None %void_func\n" +
10584 "%main_lab = OpLabel\n" +
10585 "%n = OpVariable %_ptr_float Function\n" +
10586 "%m = OpVariable %_ptr_float Function\n" +
10587 "%3 = OpLoad %float %n\n" +
10588 "%4 = OpLoad %float %m\n" +
10589 "%5 = OpFUnordNotEqual %bool %3 %4\n" +
10590 "%2 = OpLogicalNot %bool %5\n" +
10591 "OpReturn\n" +
10592 "OpFunctionEnd",
10593 2, true),
10594 // Test case 4:
10595 // !(a == b) = (a != b) [OpFUnordEqual => OpFOrdNotEqual]
10596 InstructionFoldingCase<bool>(
10597 Header() +
10598 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10599 "; CHECK: %2 = OpFOrdNotEqual [[bool]] %3 %4\n" +
10600 "%main = OpFunction %void None %void_func\n" +
10601 "%main_lab = OpLabel\n" +
10602 "%n = OpVariable %_ptr_float Function\n" +
10603 "%m = OpVariable %_ptr_float Function\n" +
10604 "%3 = OpLoad %float %n\n" +
10605 "%4 = OpLoad %float %m\n" +
10606 "%5 = OpFUnordEqual %bool %3 %4\n" +
10607 "%2 = OpLogicalNot %bool %5\n" +
10608 "OpReturn\n" +
10609 "OpFunctionEnd",
10610 2, true),
10611 // Test case 5:
10612 // !(a != b) = (a == b) [OpFOrdNotEqual => OpFUnordEqual]
10613 InstructionFoldingCase<bool>(
10614 Header() +
10615 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10616 "; CHECK: %2 = OpFUnordEqual [[bool]] %3 %4\n" +
10617 "%main = OpFunction %void None %void_func\n" +
10618 "%main_lab = OpLabel\n" +
10619 "%n = OpVariable %_ptr_float Function\n" +
10620 "%m = OpVariable %_ptr_float Function\n" +
10621 "%3 = OpLoad %float %n\n" +
10622 "%4 = OpLoad %float %m\n" +
10623 "%5 = OpFOrdNotEqual %bool %3 %4\n" +
10624 "%2 = OpLogicalNot %bool %5\n" +
10625 "OpReturn\n" +
10626 "OpFunctionEnd",
10627 2, true),
10628 // Test case 6:
10629 // !(a == b) = (a != b) [OpLogicalEqual => OpLogicalNotEqual]
10630 InstructionFoldingCase<bool>(
10631 Header() +
10632 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10633 "; CHECK: %2 = OpLogicalNotEqual [[bool]] %3 %4\n" +
10634 "%main = OpFunction %void None %void_func\n" +
10635 "%main_lab = OpLabel\n" +
10636 "%n = OpVariable %_ptr_bool Function\n" +
10637 "%m = OpVariable %_ptr_bool Function\n" +
10638 "%3 = OpLoad %bool %n\n" +
10639 "%4 = OpLoad %bool %m\n" +
10640 "%5 = OpLogicalEqual %bool %3 %4\n" +
10641 "%2 = OpLogicalNot %bool %5\n" +
10642 "OpReturn\n" +
10643 "OpFunctionEnd",
10644 2, true),
10645 // Test case 7:
10646 // !(a != b) = (a == b) [OpLogicalNotEqual => OpLogicalEqual]
10647 InstructionFoldingCase<bool>(
10648 Header() +
10649 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10650 "; CHECK: %2 = OpLogicalEqual [[bool]] %3 %4\n" +
10651 "%main = OpFunction %void None %void_func\n" +
10652 "%main_lab = OpLabel\n" +
10653 "%n = OpVariable %_ptr_bool Function\n" +
10654 "%m = OpVariable %_ptr_bool Function\n" +
10655 "%3 = OpLoad %bool %n\n" +
10656 "%4 = OpLoad %bool %m\n" +
10657 "%5 = OpLogicalNotEqual %bool %3 %4\n" +
10658 "%2 = OpLogicalNot %bool %5\n" +
10659 "OpReturn\n" +
10660 "OpFunctionEnd",
10661 2, true),
10662 // Test case 8:
10663 // !(a > b) = (a <= b) [OpUGreaterThan => OpULessThanEqual]
10664 InstructionFoldingCase<bool>(
10665 Header() +
10666 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10667 "; CHECK: %2 = OpULessThanEqual [[bool]] %3 %4\n" +
10668 "%main = OpFunction %void None %void_func\n" +
10669 "%main_lab = OpLabel\n" +
10670 "%n = OpVariable %_ptr_uint Function\n" +
10671 "%m = OpVariable %_ptr_uint Function\n" +
10672 "%3 = OpLoad %uint %n\n" +
10673 "%4 = OpLoad %uint %m\n" +
10674 "%5 = OpUGreaterThan %bool %3 %4\n" +
10675 "%2 = OpLogicalNot %bool %5\n" +
10676 "OpReturn\n" +
10677 "OpFunctionEnd",
10678 2, true),
10679 // Test case 9:
10680 // !(a <= b) = (a > b) [OpULessThanEqual => OpUGreaterThan]
10681 InstructionFoldingCase<bool>(
10682 Header() +
10683 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10684 "; CHECK: %2 = OpUGreaterThan [[bool]] %3 %4\n" +
10685 "%main = OpFunction %void None %void_func\n" +
10686 "%main_lab = OpLabel\n" +
10687 "%n = OpVariable %_ptr_uint Function\n" +
10688 "%m = OpVariable %_ptr_uint Function\n" +
10689 "%3 = OpLoad %uint %n\n" +
10690 "%4 = OpLoad %uint %m\n" +
10691 "%5 = OpULessThanEqual %bool %3 %4\n" +
10692 "%2 = OpLogicalNot %bool %5\n" +
10693 "OpReturn\n" +
10694 "OpFunctionEnd",
10695 2, true),
10696 // Test case 10:
10697 // !(a > b) = (a <= b) [OpSGreaterThan => OpSLessThanEqual]
10698 InstructionFoldingCase<bool>(
10699 Header() +
10700 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10701 "; CHECK: %2 = OpSLessThanEqual [[bool]] %3 %4\n" +
10702 "%main = OpFunction %void None %void_func\n" +
10703 "%main_lab = OpLabel\n" +
10704 "%n = OpVariable %_ptr_int Function\n" +
10705 "%m = OpVariable %_ptr_int Function\n" +
10706 "%3 = OpLoad %int %n\n" +
10707 "%4 = OpLoad %int %m\n" +
10708 "%5 = OpSGreaterThan %bool %3 %4\n" +
10709 "%2 = OpLogicalNot %bool %5\n" +
10710 "OpReturn\n" +
10711 "OpFunctionEnd",
10712 2, true),
10713 // Test case 11:
10714 // !(a <= b) = (a > b) [OpSLessThanEqual => OpSGreaterThan]
10715 InstructionFoldingCase<bool>(
10716 Header() +
10717 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10718 "; CHECK: %2 = OpSGreaterThan [[bool]] %3 %4\n" +
10719 "%main = OpFunction %void None %void_func\n" +
10720 "%main_lab = OpLabel\n" +
10721 "%n = OpVariable %_ptr_int Function\n" +
10722 "%m = OpVariable %_ptr_int Function\n" +
10723 "%3 = OpLoad %int %n\n" +
10724 "%4 = OpLoad %int %m\n" +
10725 "%5 = OpSLessThanEqual %bool %3 %4\n" +
10726 "%2 = OpLogicalNot %bool %5\n" +
10727 "OpReturn\n" +
10728 "OpFunctionEnd",
10729 2, true),
10730 // Test case 12:
10731 // !(a > b) = (a <= b) [OpFOrdGreaterThan => OpFUnordLessThanEqual]
10732 InstructionFoldingCase<bool>(
10733 Header() +
10734 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10735 "; CHECK: %2 = OpFUnordLessThanEqual [[bool]] %3 %4\n" +
10736 "%main = OpFunction %void None %void_func\n" +
10737 "%main_lab = OpLabel\n" +
10738 "%n = OpVariable %_ptr_float Function\n" +
10739 "%m = OpVariable %_ptr_float Function\n" +
10740 "%3 = OpLoad %float %n\n" +
10741 "%4 = OpLoad %float %m\n" +
10742 "%5 = OpFOrdGreaterThan %bool %3 %4\n" +
10743 "%2 = OpLogicalNot %bool %5\n" +
10744 "OpReturn\n" +
10745 "OpFunctionEnd",
10746 2, true),
10747 // Test case 13:
10748 // !(a <= b) = (a > b) [OpFUnordLessThanEqual => OpFOrdGreaterThan]
10749 InstructionFoldingCase<bool>(
10750 Header() +
10751 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10752 "; CHECK: %2 = OpFOrdGreaterThan [[bool]] %3 %4\n" +
10753 "%main = OpFunction %void None %void_func\n" +
10754 "%main_lab = OpLabel\n" +
10755 "%n = OpVariable %_ptr_float Function\n" +
10756 "%m = OpVariable %_ptr_float Function\n" +
10757 "%3 = OpLoad %float %n\n" +
10758 "%4 = OpLoad %float %m\n" +
10759 "%5 = OpFUnordLessThanEqual %bool %3 %4\n" +
10760 "%2 = OpLogicalNot %bool %5\n" +
10761 "OpReturn\n" +
10762 "OpFunctionEnd",
10763 2, true),
10764 // Test case 14:
10765 // !(a > b) = (a <= b) [OpFUnordGreaterThan => OpFOrdLessThanEqual]
10766 InstructionFoldingCase<bool>(
10767 Header() +
10768 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10769 "; CHECK: %2 = OpFOrdLessThanEqual [[bool]] %3 %4\n" +
10770 "%main = OpFunction %void None %void_func\n" +
10771 "%main_lab = OpLabel\n" +
10772 "%n = OpVariable %_ptr_float Function\n" +
10773 "%m = OpVariable %_ptr_float Function\n" +
10774 "%3 = OpLoad %float %n\n" +
10775 "%4 = OpLoad %float %m\n" +
10776 "%5 = OpFUnordGreaterThan %bool %3 %4\n" +
10777 "%2 = OpLogicalNot %bool %5\n" +
10778 "OpReturn\n" +
10779 "OpFunctionEnd",
10780 2, true),
10781 // Test case 15:
10782 // !(a <= b) = (a > b) [OpFOrdLessThanEqual => OpFUnordGreaterThan]
10783 InstructionFoldingCase<bool>(
10784 Header() +
10785 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10786 "; CHECK: %2 = OpFUnordGreaterThan [[bool]] %3 %4\n" +
10787 "%main = OpFunction %void None %void_func\n" +
10788 "%main_lab = OpLabel\n" +
10789 "%n = OpVariable %_ptr_float Function\n" +
10790 "%m = OpVariable %_ptr_float Function\n" +
10791 "%3 = OpLoad %float %n\n" +
10792 "%4 = OpLoad %float %m\n" +
10793 "%5 = OpFOrdLessThanEqual %bool %3 %4\n" +
10794 "%2 = OpLogicalNot %bool %5\n" +
10795 "OpReturn\n" +
10796 "OpFunctionEnd",
10797 2, true),
10798 // Test case 16:
10799 // !(a < b) = (a >= b) [OpULessThan => OpUGreaterThanEqual]
10800 InstructionFoldingCase<bool>(
10801 Header() +
10802 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10803 "; CHECK: %2 = OpUGreaterThanEqual [[bool]] %3 %4\n" +
10804 "%main = OpFunction %void None %void_func\n" +
10805 "%main_lab = OpLabel\n" +
10806 "%n = OpVariable %_ptr_uint Function\n" +
10807 "%m = OpVariable %_ptr_uint Function\n" +
10808 "%3 = OpLoad %uint %n\n" +
10809 "%4 = OpLoad %uint %m\n" +
10810 "%5 = OpULessThan %bool %3 %4\n" +
10811 "%2 = OpLogicalNot %bool %5\n" +
10812 "OpReturn\n" +
10813 "OpFunctionEnd",
10814 2, true),
10815 // Test case 17:
10816 // !(a >= b) = (a < b) [OpUGreaterThanEqual => OpULessThan]
10817 InstructionFoldingCase<bool>(
10818 Header() +
10819 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10820 "; CHECK: %2 = OpULessThan [[bool]] %3 %4\n" +
10821 "%main = OpFunction %void None %void_func\n" +
10822 "%main_lab = OpLabel\n" +
10823 "%n = OpVariable %_ptr_uint Function\n" +
10824 "%m = OpVariable %_ptr_uint Function\n" +
10825 "%3 = OpLoad %uint %n\n" +
10826 "%4 = OpLoad %uint %m\n" +
10827 "%5 = OpUGreaterThanEqual %bool %3 %4\n" +
10828 "%2 = OpLogicalNot %bool %5\n" +
10829 "OpReturn\n" +
10830 "OpFunctionEnd",
10831 2, true),
10832 // Test case 18:
10833 // !(a < b) = (a >= b) [OpSLessThan => OpSGreaterThanEqual]
10834 InstructionFoldingCase<bool>(
10835 Header() +
10836 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10837 "; CHECK: %2 = OpSGreaterThanEqual [[bool]] %3 %4\n" +
10838 "%main = OpFunction %void None %void_func\n" +
10839 "%main_lab = OpLabel\n" +
10840 "%n = OpVariable %_ptr_int Function\n" +
10841 "%m = OpVariable %_ptr_int Function\n" +
10842 "%3 = OpLoad %int %n\n" +
10843 "%4 = OpLoad %int %m\n" +
10844 "%5 = OpSLessThan %bool %3 %4\n" +
10845 "%2 = OpLogicalNot %bool %5\n" +
10846 "OpReturn\n" +
10847 "OpFunctionEnd",
10848 2, true),
10849 // Test case 19:
10850 // !(a >= b) = (a < b) [OpSGreaterThanEqual => OpSLessThan]
10851 InstructionFoldingCase<bool>(
10852 Header() +
10853 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10854 "; CHECK: %2 = OpSLessThan [[bool]] %3 %4\n" +
10855 "%main = OpFunction %void None %void_func\n" +
10856 "%main_lab = OpLabel\n" +
10857 "%n = OpVariable %_ptr_int Function\n" +
10858 "%m = OpVariable %_ptr_int Function\n" +
10859 "%3 = OpLoad %int %n\n" +
10860 "%4 = OpLoad %int %m\n" +
10861 "%5 = OpSGreaterThanEqual %bool %3 %4\n" +
10862 "%2 = OpLogicalNot %bool %5\n" +
10863 "OpReturn\n" +
10864 "OpFunctionEnd",
10865 2, true),
10866 // Test case 20:
10867 // !(a < b) = (a >= b) [OpFOrdLessThan => OpFUnordGreaterThanEqual]
10868 InstructionFoldingCase<bool>(
10869 Header() +
10870 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10871 "; CHECK: %2 = OpFUnordGreaterThanEqual [[bool]] %3 %4\n" +
10872 "%main = OpFunction %void None %void_func\n" +
10873 "%main_lab = OpLabel\n" +
10874 "%n = OpVariable %_ptr_float Function\n" +
10875 "%m = OpVariable %_ptr_float Function\n" +
10876 "%3 = OpLoad %float %n\n" +
10877 "%4 = OpLoad %float %m\n" +
10878 "%5 = OpFOrdLessThan %bool %3 %4\n" +
10879 "%2 = OpLogicalNot %bool %5\n" +
10880 "OpReturn\n" +
10881 "OpFunctionEnd",
10882 2, true),
10883 // Test case 21:
10884 // !(a >= b) = (a < b) [OpFUnordGreaterThanEqual => OpFOrdLessThan]
10885 InstructionFoldingCase<bool>(
10886 Header() +
10887 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10888 "; CHECK: %2 = OpFOrdLessThan [[bool]] %3 %4\n" +
10889 "%main = OpFunction %void None %void_func\n" +
10890 "%main_lab = OpLabel\n" +
10891 "%n = OpVariable %_ptr_float Function\n" +
10892 "%m = OpVariable %_ptr_float Function\n" +
10893 "%3 = OpLoad %float %n\n" +
10894 "%4 = OpLoad %float %m\n" +
10895 "%5 = OpFUnordGreaterThanEqual %bool %3 %4\n" +
10896 "%2 = OpLogicalNot %bool %5\n" +
10897 "OpReturn\n" +
10898 "OpFunctionEnd",
10899 2, true),
10900 // Test case 22:
10901 // !(a < b) = (a >= b) [OpFUnordLessThan => OpFOrdGreaterThanEqual]
10902 InstructionFoldingCase<bool>(
10903 Header() +
10904 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10905 "; CHECK: %2 = OpFOrdGreaterThanEqual [[bool]] %3 %4\n" +
10906 "%main = OpFunction %void None %void_func\n" +
10907 "%main_lab = OpLabel\n" +
10908 "%n = OpVariable %_ptr_float Function\n" +
10909 "%m = OpVariable %_ptr_float Function\n" +
10910 "%3 = OpLoad %float %n\n" +
10911 "%4 = OpLoad %float %m\n" +
10912 "%5 = OpFUnordLessThan %bool %3 %4\n" +
10913 "%2 = OpLogicalNot %bool %5\n" +
10914 "OpReturn\n" +
10915 "OpFunctionEnd",
10916 2, true),
10917 // Test case 23:
10918 // !(a >= b) = (a < b) [OpFOrdGreaterThanEqual => OpFUnordLessThan]
10919 InstructionFoldingCase<bool>(
10920 Header() +
10921 "; CHECK: [[bool:%\\w+]] = OpTypeBool\n" +
10922 "; CHECK: %2 = OpFUnordLessThan [[bool]] %3 %4\n" +
10923 "%main = OpFunction %void None %void_func\n" +
10924 "%main_lab = OpLabel\n" +
10925 "%n = OpVariable %_ptr_float Function\n" +
10926 "%m = OpVariable %_ptr_float Function\n" +
10927 "%3 = OpLoad %float %n\n" +
10928 "%4 = OpLoad %float %m\n" +
10929 "%5 = OpFOrdGreaterThanEqual %bool %3 %4\n" +
10930 "%2 = OpLogicalNot %bool %5\n" +
10931 "OpReturn\n" +
10932 "OpFunctionEnd",
10933 2, true)
10934 ));
10935
10936 INSTANTIATE_TEST_SUITE_P(CompositeExtractOrInsertMatchingTest, MatchingInstructionFoldingTest,
10937 ::testing::Values(
10938 // Test case 0: Extracting from result of consecutive shuffles of differing
10939 // size.
10940 InstructionFoldingCase<bool>(
10941 Header() +
10942 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10943 "; CHECK: %5 = OpCompositeExtract [[int]] %2 2\n" +
10944 "%main = OpFunction %void None %void_func\n" +
10945 "%main_lab = OpLabel\n" +
10946 "%n = OpVariable %_ptr_v4int Function\n" +
10947 "%2 = OpLoad %v4int %n\n" +
10948 "%3 = OpVectorShuffle %v2int %2 %2 2 3\n" +
10949 "%4 = OpVectorShuffle %v4int %2 %3 0 4 2 5\n" +
10950 "%5 = OpCompositeExtract %int %4 1\n" +
10951 "OpReturn\n" +
10952 "OpFunctionEnd",
10953 5, true),
10954 // Test case 1: Extracting from result of vector shuffle of differing
10955 // input and result sizes.
10956 InstructionFoldingCase<bool>(
10957 Header() +
10958 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10959 "; CHECK: %4 = OpCompositeExtract [[int]] %2 2\n" +
10960 "%main = OpFunction %void None %void_func\n" +
10961 "%main_lab = OpLabel\n" +
10962 "%n = OpVariable %_ptr_v4int Function\n" +
10963 "%2 = OpLoad %v4int %n\n" +
10964 "%3 = OpVectorShuffle %v2int %2 %2 2 3\n" +
10965 "%4 = OpCompositeExtract %int %3 0\n" +
10966 "OpReturn\n" +
10967 "OpFunctionEnd",
10968 4, true),
10969 // Test case 2: Extracting from result of vector shuffle of differing
10970 // input and result sizes.
10971 InstructionFoldingCase<bool>(
10972 Header() +
10973 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
10974 "; CHECK: %4 = OpCompositeExtract [[int]] %2 3\n" +
10975 "%main = OpFunction %void None %void_func\n" +
10976 "%main_lab = OpLabel\n" +
10977 "%n = OpVariable %_ptr_v4int Function\n" +
10978 "%2 = OpLoad %v4int %n\n" +
10979 "%3 = OpVectorShuffle %v2int %2 %2 2 3\n" +
10980 "%4 = OpCompositeExtract %int %3 1\n" +
10981 "OpReturn\n" +
10982 "OpFunctionEnd",
10983 4, true),
10984 // Test case 3: Using fmix feeding extract with a 1 in the a position.
10985 InstructionFoldingCase<bool>(
10986 Header() +
10987 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
10988 "; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 4\n" +
10989 "; CHECK: [[ptr_v4double:%\\w+]] = OpTypePointer Function [[v4double]]\n" +
10990 "; CHECK: [[m:%\\w+]] = OpVariable [[ptr_v4double]] Function\n" +
10991 "; CHECK: [[n:%\\w+]] = OpVariable [[ptr_v4double]] Function\n" +
10992 "; CHECK: [[ld:%\\w+]] = OpLoad [[v4double]] [[n]]\n" +
10993 "; CHECK: %5 = OpCompositeExtract [[double]] [[ld]] 1\n" +
10994 "%main = OpFunction %void None %void_func\n" +
10995 "%main_lab = OpLabel\n" +
10996 "%m = OpVariable %_ptr_v4double Function\n" +
10997 "%n = OpVariable %_ptr_v4double Function\n" +
10998 "%2 = OpLoad %v4double %m\n" +
10999 "%3 = OpLoad %v4double %n\n" +
11000 "%4 = OpExtInst %v4double %1 FMix %2 %3 %v4double_0_1_0_0\n" +
11001 "%5 = OpCompositeExtract %double %4 1\n" +
11002 "OpReturn\n" +
11003 "OpFunctionEnd",
11004 5, true),
11005 // Test case 4: Using fmix feeding extract with a 0 in the a position.
11006 InstructionFoldingCase<bool>(
11007 Header() +
11008 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11009 "; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 4\n" +
11010 "; CHECK: [[ptr_v4double:%\\w+]] = OpTypePointer Function [[v4double]]\n" +
11011 "; CHECK: [[m:%\\w+]] = OpVariable [[ptr_v4double]] Function\n" +
11012 "; CHECK: [[n:%\\w+]] = OpVariable [[ptr_v4double]] Function\n" +
11013 "; CHECK: [[ld:%\\w+]] = OpLoad [[v4double]] [[m]]\n" +
11014 "; CHECK: %5 = OpCompositeExtract [[double]] [[ld]] 2\n" +
11015 "%main = OpFunction %void None %void_func\n" +
11016 "%main_lab = OpLabel\n" +
11017 "%m = OpVariable %_ptr_v4double Function\n" +
11018 "%n = OpVariable %_ptr_v4double Function\n" +
11019 "%2 = OpLoad %v4double %m\n" +
11020 "%3 = OpLoad %v4double %n\n" +
11021 "%4 = OpExtInst %v4double %1 FMix %2 %3 %v4double_0_1_0_0\n" +
11022 "%5 = OpCompositeExtract %double %4 2\n" +
11023 "OpReturn\n" +
11024 "OpFunctionEnd",
11025 5, true),
11026 // Test case 5: Using fmix feeding extract with a null for the alpha
11027 InstructionFoldingCase<bool>(
11028 Header() +
11029 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11030 "; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 4\n" +
11031 "; CHECK: [[ptr_v4double:%\\w+]] = OpTypePointer Function [[v4double]]\n" +
11032 "; CHECK: [[m:%\\w+]] = OpVariable [[ptr_v4double]] Function\n" +
11033 "; CHECK: [[n:%\\w+]] = OpVariable [[ptr_v4double]] Function\n" +
11034 "; CHECK: [[ld:%\\w+]] = OpLoad [[v4double]] [[m]]\n" +
11035 "; CHECK: %5 = OpCompositeExtract [[double]] [[ld]] 0\n" +
11036 "%main = OpFunction %void None %void_func\n" +
11037 "%main_lab = OpLabel\n" +
11038 "%m = OpVariable %_ptr_v4double Function\n" +
11039 "%n = OpVariable %_ptr_v4double Function\n" +
11040 "%2 = OpLoad %v4double %m\n" +
11041 "%3 = OpLoad %v4double %n\n" +
11042 "%4 = OpExtInst %v4double %1 FMix %2 %3 %v4double_null\n" +
11043 "%5 = OpCompositeExtract %double %4 0\n" +
11044 "OpReturn\n" +
11045 "OpFunctionEnd",
11046 5, true),
11047 // Test case 6: Don't fold: Using fmix feeding extract with 0.5 in the a
11048 // position.
11049 InstructionFoldingCase<bool>(
11050 Header() +
11051 "%main = OpFunction %void None %void_func\n" +
11052 "%main_lab = OpLabel\n" +
11053 "%m = OpVariable %_ptr_v4double Function\n" +
11054 "%n = OpVariable %_ptr_v4double Function\n" +
11055 "%2 = OpLoad %v4double %m\n" +
11056 "%3 = OpLoad %v4double %n\n" +
11057 "%4 = OpExtInst %v4double %1 FMix %2 %3 %v4double_1_1_1_0p5\n" +
11058 "%5 = OpCompositeExtract %double %4 3\n" +
11059 "OpReturn\n" +
11060 "OpFunctionEnd",
11061 5, false),
11062 // Test case 7: Don't fold: Using fmix feeding extract with half type in
11063 // the a position.
11064 InstructionFoldingCase<bool>(
11065 Header() +
11066 "%main = OpFunction %void None %void_func\n" +
11067 "%main_lab = OpLabel\n" +
11068 "%m = OpVariable %_ptr_v4half Function\n" +
11069 "%n = OpVariable %_ptr_v4half Function\n" +
11070 "%2 = OpLoad %v4half %m\n" +
11071 "%3 = OpLoad %v4half %n\n" +
11072 "%4 = OpExtInst %v4half %1 FMix %2 %3 %v4half_0_1_0_0\n" +
11073 "%5 = OpCompositeExtract %half %4 0\n" +
11074 "OpReturn\n" +
11075 "OpFunctionEnd",
11076 5, false),
11077 // Test case 8: Extracting the undefined literal value from a vector
11078 // shuffle.
11079 InstructionFoldingCase<bool>(
11080 Header() +
11081 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11082 "; CHECK: %4 = OpUndef [[int]]\n" +
11083 "%main = OpFunction %void None %void_func\n" +
11084 "%main_lab = OpLabel\n" +
11085 "%n = OpVariable %_ptr_v4int Function\n" +
11086 "%2 = OpLoad %v4int %n\n" +
11087 "%3 = OpVectorShuffle %v2int %2 %2 2 4294967295\n" +
11088 "%4 = OpCompositeExtract %int %3 1\n" +
11089 "OpReturn\n" +
11090 "OpFunctionEnd",
11091 4, true),
11092 // Test case 9: Inserting every element of a vector turns into a composite construct.
11093 InstructionFoldingCase<bool>(
11094 Header() +
11095 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11096 "; CHECK-DAG: [[v4:%\\w+]] = OpTypeVector [[int]] 4\n" +
11097 "; CHECK-DAG: [[int1:%\\w+]] = OpConstant [[int]] 1\n" +
11098 "; CHECK-DAG: [[int2:%\\w+]] = OpConstant [[int]] 2\n" +
11099 "; CHECK-DAG: [[int3:%\\w+]] = OpConstant [[int]] 3\n" +
11100 "; CHECK: [[construct:%\\w+]] = OpCompositeConstruct [[v4]] %100 [[int1]] [[int2]] [[int3]]\n" +
11101 "; CHECK: %5 = OpCopyObject [[v4]] [[construct]]\n" +
11102 "%main = OpFunction %void None %void_func\n" +
11103 "%main_lab = OpLabel\n" +
11104 "%2 = OpCompositeInsert %v4int %100 %v4int_undef 0\n" +
11105 "%3 = OpCompositeInsert %v4int %int_1 %2 1\n" +
11106 "%4 = OpCompositeInsert %v4int %int_2 %3 2\n" +
11107 "%5 = OpCompositeInsert %v4int %int_3 %4 3\n" +
11108 "OpReturn\n" +
11109 "OpFunctionEnd",
11110 5, true),
11111 // Test case 10: Inserting every element of a vector turns into a composite construct in a different order.
11112 InstructionFoldingCase<bool>(
11113 Header() +
11114 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11115 "; CHECK-DAG: [[v4:%\\w+]] = OpTypeVector [[int]] 4\n" +
11116 "; CHECK-DAG: [[int1:%\\w+]] = OpConstant [[int]] 1\n" +
11117 "; CHECK-DAG: [[int2:%\\w+]] = OpConstant [[int]] 2\n" +
11118 "; CHECK-DAG: [[int3:%\\w+]] = OpConstant [[int]] 3\n" +
11119 "; CHECK: [[construct:%\\w+]] = OpCompositeConstruct [[v4]] %100 [[int1]] [[int2]] [[int3]]\n" +
11120 "; CHECK: %5 = OpCopyObject [[v4]] [[construct]]\n" +
11121 "%main = OpFunction %void None %void_func\n" +
11122 "%main_lab = OpLabel\n" +
11123 "%2 = OpCompositeInsert %v4int %100 %v4int_undef 0\n" +
11124 "%4 = OpCompositeInsert %v4int %int_2 %2 2\n" +
11125 "%3 = OpCompositeInsert %v4int %int_1 %4 1\n" +
11126 "%5 = OpCompositeInsert %v4int %int_3 %3 3\n" +
11127 "OpReturn\n" +
11128 "OpFunctionEnd",
11129 5, true),
11130 // Test case 11: Check multiple inserts to the same position are handled correctly.
11131 InstructionFoldingCase<bool>(
11132 Header() +
11133 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11134 "; CHECK-DAG: [[v4:%\\w+]] = OpTypeVector [[int]] 4\n" +
11135 "; CHECK-DAG: [[int1:%\\w+]] = OpConstant [[int]] 1\n" +
11136 "; CHECK-DAG: [[int2:%\\w+]] = OpConstant [[int]] 2\n" +
11137 "; CHECK-DAG: [[int3:%\\w+]] = OpConstant [[int]] 3\n" +
11138 "; CHECK: [[construct:%\\w+]] = OpCompositeConstruct [[v4]] %100 [[int1]] [[int2]] [[int3]]\n" +
11139 "; CHECK: %6 = OpCopyObject [[v4]] [[construct]]\n" +
11140 "%main = OpFunction %void None %void_func\n" +
11141 "%main_lab = OpLabel\n" +
11142 "%2 = OpCompositeInsert %v4int %100 %v4int_undef 0\n" +
11143 "%3 = OpCompositeInsert %v4int %int_2 %2 2\n" +
11144 "%4 = OpCompositeInsert %v4int %int_4 %3 1\n" +
11145 "%5 = OpCompositeInsert %v4int %int_1 %4 1\n" +
11146 "%6 = OpCompositeInsert %v4int %int_3 %5 3\n" +
11147 "OpReturn\n" +
11148 "OpFunctionEnd",
11149 6, true),
11150 // Test case 12: The last indexes are 0 and 1, but they have different first indexes. This should not be folded.
11151 InstructionFoldingCase<bool>(
11152 Header() +
11153 "%main = OpFunction %void None %void_func\n" +
11154 "%main_lab = OpLabel\n" +
11155 "%2 = OpCompositeInsert %m2x2int %100 %m2x2int_undef 0 0\n" +
11156 "%3 = OpCompositeInsert %m2x2int %int_1 %2 1 1\n" +
11157 "OpReturn\n" +
11158 "OpFunctionEnd",
11159 3, false),
11160 // Test case 13: Don't fold when there is a partial insertion.
11161 InstructionFoldingCase<bool>(
11162 Header() +
11163 "%main = OpFunction %void None %void_func\n" +
11164 "%main_lab = OpLabel\n" +
11165 "%2 = OpCompositeInsert %m2x2int %v2int_1_0 %m2x2int_undef 0\n" +
11166 "%3 = OpCompositeInsert %m2x2int %int_4 %2 0 0\n" +
11167 "%4 = OpCompositeInsert %m2x2int %v2int_2_3 %3 1\n" +
11168 "OpReturn\n" +
11169 "OpFunctionEnd",
11170 4, false),
11171 // Test case 14: Insert into a column of a matrix
11172 InstructionFoldingCase<bool>(
11173 Header() +
11174 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11175 "; CHECK-DAG: [[v2:%\\w+]] = OpTypeVector [[int]] 2\n" +
11176 "; CHECK: [[m2x2:%\\w+]] = OpTypeMatrix [[v2]] 2\n" +
11177 "; CHECK-DAG: [[m2x2_undef:%\\w+]] = OpUndef [[m2x2]]\n" +
11178 "; CHECK-DAG: [[int1:%\\w+]] = OpConstant [[int]] 1\n" +
11179 // We keep this insert in the chain. DeadInsertElimPass should remove it.
11180 "; CHECK: [[insert:%\\w+]] = OpCompositeInsert [[m2x2]] %100 [[m2x2_undef]] 0 0\n" +
11181 "; CHECK: [[construct:%\\w+]] = OpCompositeConstruct [[v2]] %100 [[int1]]\n" +
11182 "; CHECK: %3 = OpCompositeInsert [[m2x2]] [[construct]] [[insert]] 0\n" +
11183 "%main = OpFunction %void None %void_func\n" +
11184 "%main_lab = OpLabel\n" +
11185 "%2 = OpCompositeInsert %m2x2int %100 %m2x2int_undef 0 0\n" +
11186 "%3 = OpCompositeInsert %m2x2int %int_1 %2 0 1\n" +
11187 "OpReturn\n" +
11188 "OpFunctionEnd",
11189 3, true),
11190 // Test case 15: Insert all elements of the matrix.
11191 InstructionFoldingCase<bool>(
11192 Header() +
11193 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11194 "; CHECK-DAG: [[v2:%\\w+]] = OpTypeVector [[int]] 2\n" +
11195 "; CHECK: [[m2x2:%\\w+]] = OpTypeMatrix [[v2]] 2\n" +
11196 "; CHECK-DAG: [[m2x2_undef:%\\w+]] = OpUndef [[m2x2]]\n" +
11197 "; CHECK-DAG: [[int1:%\\w+]] = OpConstant [[int]] 1\n" +
11198 "; CHECK-DAG: [[int2:%\\w+]] = OpConstant [[int]] 2\n" +
11199 "; CHECK-DAG: [[int3:%\\w+]] = OpConstant [[int]] 3\n" +
11200 "; CHECK: [[c0:%\\w+]] = OpCompositeConstruct [[v2]] %100 [[int1]]\n" +
11201 "; CHECK: [[c1:%\\w+]] = OpCompositeConstruct [[v2]] [[int2]] [[int3]]\n" +
11202 "; CHECK: [[matrix:%\\w+]] = OpCompositeConstruct [[m2x2]] [[c0]] [[c1]]\n" +
11203 "; CHECK: %5 = OpCopyObject [[m2x2]] [[matrix]]\n" +
11204 "%main = OpFunction %void None %void_func\n" +
11205 "%main_lab = OpLabel\n" +
11206 "%2 = OpCompositeConstruct %v2int %100 %int_1\n" +
11207 "%3 = OpCompositeInsert %m2x2int %2 %m2x2int_undef 0\n" +
11208 "%4 = OpCompositeInsert %m2x2int %int_2 %3 1 0\n" +
11209 "%5 = OpCompositeInsert %m2x2int %int_3 %4 1 1\n" +
11210 "OpReturn\n" +
11211 "OpFunctionEnd",
11212 5, true),
11213 // Test case 16: Replace construct with extract when reconstructing a member
11214 // of another object.
11215 InstructionFoldingCase<bool>(
11216 Header() +
11217 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11218 "; CHECK: [[v2:%\\w+]] = OpTypeVector [[int]] 2\n" +
11219 "; CHECK: [[m2x2:%\\w+]] = OpTypeMatrix [[v2]] 2\n" +
11220 "; CHECK: [[m2x2_undef:%\\w+]] = OpUndef [[m2x2]]\n" +
11221 "; CHECK: %5 = OpCompositeExtract [[v2]] [[m2x2_undef]]\n" +
11222 "%main = OpFunction %void None %void_func\n" +
11223 "%main_lab = OpLabel\n" +
11224 "%3 = OpCompositeExtract %int %m2x2int_undef 1 0\n" +
11225 "%4 = OpCompositeExtract %int %m2x2int_undef 1 1\n" +
11226 "%5 = OpCompositeConstruct %v2int %3 %4\n" +
11227 "OpReturn\n" +
11228 "OpFunctionEnd",
11229 5, true),
11230 // Test case 17: Don't fold when type cannot be deduced to a constant.
11231 InstructionFoldingCase<bool>(
11232 Header() +
11233 "%main = OpFunction %void None %void_func\n" +
11234 "%main_lab = OpLabel\n" +
11235 "%4 = OpCompositeInsert %struct_v2int_int_int %int_1 %struct_v2int_int_int_null 2\n" +
11236 "OpReturn\n" +
11237 "OpFunctionEnd",
11238 4, false),
11239 // Test case 18: Don't fold when index into composite is out of bounds.
11240 InstructionFoldingCase<bool>(
11241 Header() +
11242 "%main = OpFunction %void None %void_func\n" +
11243 "%main_lab = OpLabel\n" +
11244 "%4 = OpCompositeExtract %int %struct_v2int_int_int 3\n" +
11245 "OpReturn\n" +
11246 "OpFunctionEnd",
11247 4, false),
11248 // Test case 19: Fold when every element of an array is inserted.
11249 InstructionFoldingCase<bool>(
11250 Header() +
11251 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11252 "; CHECK: [[int2:%\\w+]] = OpConstant [[int]] 2\n" +
11253 "; CHECK-DAG: [[arr_type:%\\w+]] = OpTypeArray [[int]] [[int2]]\n" +
11254 "; CHECK-DAG: [[int10:%\\w+]] = OpConstant [[int]] 10\n" +
11255 "; CHECK-DAG: [[int1:%\\w+]] = OpConstant [[int]] 1\n" +
11256 "; CHECK: [[construct:%\\w+]] = OpCompositeConstruct [[arr_type]] [[int10]] [[int1]]\n" +
11257 "; CHECK: %5 = OpCopyObject [[arr_type]] [[construct]]\n" +
11258 "%main = OpFunction %void None %void_func\n" +
11259 "%main_lab = OpLabel\n" +
11260 "%4 = OpCompositeInsert %int_arr_2 %int_10 %int_arr_2_undef 0\n" +
11261 "%5 = OpCompositeInsert %int_arr_2 %int_1 %4 1\n" +
11262 "OpReturn\n" +
11263 "OpFunctionEnd",
11264 5, true),
11265 // Test case 20: Don't fold for isomorphic structs
11266 InstructionFoldingCase<bool>(
11267 Header() +
11268 "%structA = OpTypeStruct %ulong\n" +
11269 "%structB = OpTypeStruct %ulong\n" +
11270 "%structC = OpTypeStruct %structB\n" +
11271 "%struct_a_undef = OpUndef %structA\n" +
11272 "%main = OpFunction %void None %void_func\n" +
11273 "%main_lab = OpLabel\n" +
11274 "%3 = OpCompositeExtract %ulong %struct_a_undef 0\n" +
11275 "%4 = OpCompositeConstruct %structB %3\n" +
11276 "OpReturn\n" +
11277 "OpFunctionEnd",
11278 4, false)
11279 ));
11280
11281 INSTANTIATE_TEST_SUITE_P(DotProductMatchingTest, MatchingInstructionFoldingTest,
11282 ::testing::Values(
11283 // Test case 0: Using OpDot to extract last element.
11284 InstructionFoldingCase<bool>(
11285 Header() +
11286 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
11287 "; CHECK: %3 = OpCompositeExtract [[float]] %2 3\n" +
11288 "%main = OpFunction %void None %void_func\n" +
11289 "%main_lab = OpLabel\n" +
11290 "%n = OpVariable %_ptr_v4float Function\n" +
11291 "%2 = OpLoad %v4float %n\n" +
11292 "%3 = OpDot %float %2 %v4float_0_0_0_1\n" +
11293 "OpReturn\n" +
11294 "OpFunctionEnd",
11295 3, true),
11296 // Test case 1: Using OpDot to extract last element.
11297 InstructionFoldingCase<bool>(
11298 Header() +
11299 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
11300 "; CHECK: %3 = OpCompositeExtract [[float]] %2 3\n" +
11301 "%main = OpFunction %void None %void_func\n" +
11302 "%main_lab = OpLabel\n" +
11303 "%n = OpVariable %_ptr_v4float Function\n" +
11304 "%2 = OpLoad %v4float %n\n" +
11305 "%3 = OpDot %float %v4float_0_0_0_1 %2\n" +
11306 "OpReturn\n" +
11307 "OpFunctionEnd",
11308 3, true),
11309 // Test case 2: Using OpDot to extract second element.
11310 InstructionFoldingCase<bool>(
11311 Header() +
11312 "; CHECK: [[float:%\\w+]] = OpTypeFloat 32\n" +
11313 "; CHECK: %3 = OpCompositeExtract [[float]] %2 1\n" +
11314 "%main = OpFunction %void None %void_func\n" +
11315 "%main_lab = OpLabel\n" +
11316 "%n = OpVariable %_ptr_v4float Function\n" +
11317 "%2 = OpLoad %v4float %n\n" +
11318 "%3 = OpDot %float %v4float_0_1_0_0 %2\n" +
11319 "OpReturn\n" +
11320 "OpFunctionEnd",
11321 3, true),
11322 // Test case 3: Using OpDot to extract last element.
11323 InstructionFoldingCase<bool>(
11324 Header() +
11325 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11326 "; CHECK: %3 = OpCompositeExtract [[double]] %2 3\n" +
11327 "%main = OpFunction %void None %void_func\n" +
11328 "%main_lab = OpLabel\n" +
11329 "%n = OpVariable %_ptr_v4double Function\n" +
11330 "%2 = OpLoad %v4double %n\n" +
11331 "%3 = OpDot %double %2 %v4double_0_0_0_1\n" +
11332 "OpReturn\n" +
11333 "OpFunctionEnd",
11334 3, true),
11335 // Test case 4: Using OpDot to extract last element.
11336 InstructionFoldingCase<bool>(
11337 Header() +
11338 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11339 "; CHECK: %3 = OpCompositeExtract [[double]] %2 3\n" +
11340 "%main = OpFunction %void None %void_func\n" +
11341 "%main_lab = OpLabel\n" +
11342 "%n = OpVariable %_ptr_v4double Function\n" +
11343 "%2 = OpLoad %v4double %n\n" +
11344 "%3 = OpDot %double %v4double_0_0_0_1 %2\n" +
11345 "OpReturn\n" +
11346 "OpFunctionEnd",
11347 3, true),
11348 // Test case 5: Using OpDot to extract second element.
11349 InstructionFoldingCase<bool>(
11350 Header() +
11351 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11352 "; CHECK: %3 = OpCompositeExtract [[double]] %2 1\n" +
11353 "%main = OpFunction %void None %void_func\n" +
11354 "%main_lab = OpLabel\n" +
11355 "%n = OpVariable %_ptr_v4double Function\n" +
11356 "%2 = OpLoad %v4double %n\n" +
11357 "%3 = OpDot %double %v4double_0_1_0_0 %2\n" +
11358 "OpReturn\n" +
11359 "OpFunctionEnd",
11360 3, true)
11361 ));
11362
11363 INSTANTIATE_TEST_SUITE_P(VectorShuffleMatchingTest, MatchingInstructionFoldingTest,
11364 ::testing::Values(
11365 // Test case 0: Using OpDot to extract last element.
11366 InstructionFoldingCase<bool>(
11367 Header() +
11368 "; CHECK: [[int:%\\w+]] = OpTypeInt 32 1\n" +
11369 "; CHECK: [[v2int:%\\w+]] = OpTypeVector [[int]] 2{{[[:space:]]}}\n" +
11370 "; CHECK: [[null:%\\w+]] = OpConstantNull [[v2int]]\n" +
11371 "; CHECK: OpVectorShuffle\n" +
11372 "; CHECK: %3 = OpVectorShuffle [[v2int]] [[null]] {{%\\w+}} 4294967295 2\n" +
11373 "%main = OpFunction %void None %void_func\n" +
11374 "%main_lab = OpLabel\n" +
11375 "%n = OpVariable %_ptr_int Function\n" +
11376 "%load = OpLoad %int %n\n" +
11377 "%2 = OpVectorShuffle %v2int %v2int_null %v2int_2_3 3 0xFFFFFFFF \n" +
11378 "%3 = OpVectorShuffle %v2int %2 %v2int_2_3 1 2 \n" +
11379 "OpReturn\n" +
11380 "OpFunctionEnd",
11381 3, true)
11382 ));
11383
11384 // Issue #5658: The Adreno compiler does not handle 16-bit FMA instructions well.
11385 // We want to avoid this by not generating FMA. We decided to never generate
11386 // FMAs because, from a SPIR-V perspective, it is neutral. The ICD can generate
11387 // the FMA if it wants. The simplest code is no code.
11388 INSTANTIATE_TEST_SUITE_P(FmaGenerationMatchingTest, MatchingInstructionFoldingTest,
11389 ::testing::Values(
11390 // Test case 0: Don't fold (x * y) + a
11391 InstructionFoldingCase<bool>(
11392 Header() +
11393 "%main = OpFunction %void None %void_func\n" +
11394 "%main_lab = OpLabel\n" +
11395 "%x = OpVariable %_ptr_float Function\n" +
11396 "%y = OpVariable %_ptr_float Function\n" +
11397 "%a = OpVariable %_ptr_float Function\n" +
11398 "%lx = OpLoad %float %x\n" +
11399 "%ly = OpLoad %float %y\n" +
11400 "%mul = OpFMul %float %lx %ly\n" +
11401 "%la = OpLoad %float %a\n" +
11402 "%3 = OpFAdd %float %mul %la\n" +
11403 "OpStore %a %3\n" +
11404 "OpReturn\n" +
11405 "OpFunctionEnd",
11406 3, false),
11407 // Test case 1: Don't fold a + (x * y)
11408 InstructionFoldingCase<bool>(
11409 Header() +
11410 "%main = OpFunction %void None %void_func\n" +
11411 "%main_lab = OpLabel\n" +
11412 "%x = OpVariable %_ptr_float Function\n" +
11413 "%y = OpVariable %_ptr_float Function\n" +
11414 "%a = OpVariable %_ptr_float Function\n" +
11415 "%lx = OpLoad %float %x\n" +
11416 "%ly = OpLoad %float %y\n" +
11417 "%mul = OpFMul %float %lx %ly\n" +
11418 "%la = OpLoad %float %a\n" +
11419 "%3 = OpFAdd %float %la %mul\n" +
11420 "OpStore %a %3\n" +
11421 "OpReturn\n" +
11422 "OpFunctionEnd",
11423 3, false),
11424 // Test case 2: Don't fold (x * y) + a with vectors
11425 InstructionFoldingCase<bool>(
11426 Header() +
11427 "%main = OpFunction %void None %void_func\n" +
11428 "%main_lab = OpLabel\n" +
11429 "%x = OpVariable %_ptr_v4float Function\n" +
11430 "%y = OpVariable %_ptr_v4float Function\n" +
11431 "%a = OpVariable %_ptr_v4float Function\n" +
11432 "%lx = OpLoad %v4float %x\n" +
11433 "%ly = OpLoad %v4float %y\n" +
11434 "%mul = OpFMul %v4float %lx %ly\n" +
11435 "%la = OpLoad %v4float %a\n" +
11436 "%3 = OpFAdd %v4float %mul %la\n" +
11437 "OpStore %a %3\n" +
11438 "OpReturn\n" +
11439 "OpFunctionEnd",
11440 3,false),
11441 // Test case 3: Don't fold a + (x * y) with vectors
11442 InstructionFoldingCase<bool>(
11443 Header() +
11444 "%main = OpFunction %void None %void_func\n" +
11445 "%main_lab = OpLabel\n" +
11446 "%x = OpVariable %_ptr_float Function\n" +
11447 "%y = OpVariable %_ptr_float Function\n" +
11448 "%a = OpVariable %_ptr_float Function\n" +
11449 "%lx = OpLoad %float %x\n" +
11450 "%ly = OpLoad %float %y\n" +
11451 "%mul = OpFMul %float %lx %ly\n" +
11452 "%la = OpLoad %float %a\n" +
11453 "%3 = OpFAdd %float %la %mul\n" +
11454 "OpStore %a %3\n" +
11455 "OpReturn\n" +
11456 "OpFunctionEnd",
11457 3, false),
11458 // Test 4: Don't fold if the multiple is marked no contract.
11459 InstructionFoldingCase<bool>(
11460 std::string() +
11461 "OpCapability Shader\n" +
11462 "OpMemoryModel Logical GLSL450\n" +
11463 "OpEntryPoint Fragment %main \"main\"\n" +
11464 "OpExecutionMode %main OriginUpperLeft\n" +
11465 "OpSource GLSL 140\n" +
11466 "OpName %main \"main\"\n" +
11467 "OpDecorate %mul NoContraction\n" +
11468 "%void = OpTypeVoid\n" +
11469 "%void_func = OpTypeFunction %void\n" +
11470 "%bool = OpTypeBool\n" +
11471 "%float = OpTypeFloat 32\n" +
11472 "%_ptr_float = OpTypePointer Function %float\n" +
11473 "%main = OpFunction %void None %void_func\n" +
11474 "%main_lab = OpLabel\n" +
11475 "%x = OpVariable %_ptr_float Function\n" +
11476 "%y = OpVariable %_ptr_float Function\n" +
11477 "%a = OpVariable %_ptr_float Function\n" +
11478 "%lx = OpLoad %float %x\n" +
11479 "%ly = OpLoad %float %y\n" +
11480 "%mul = OpFMul %float %lx %ly\n" +
11481 "%la = OpLoad %float %a\n" +
11482 "%3 = OpFAdd %float %mul %la\n" +
11483 "OpStore %a %3\n" +
11484 "OpReturn\n" +
11485 "OpFunctionEnd",
11486 3, false),
11487 // Test 5: Don't fold if the add is marked no contract.
11488 InstructionFoldingCase<bool>(
11489 std::string() +
11490 "OpCapability Shader\n" +
11491 "OpMemoryModel Logical GLSL450\n" +
11492 "OpEntryPoint Fragment %main \"main\"\n" +
11493 "OpExecutionMode %main OriginUpperLeft\n" +
11494 "OpSource GLSL 140\n" +
11495 "OpName %main \"main\"\n" +
11496 "OpDecorate %3 NoContraction\n" +
11497 "%void = OpTypeVoid\n" +
11498 "%void_func = OpTypeFunction %void\n" +
11499 "%bool = OpTypeBool\n" +
11500 "%float = OpTypeFloat 32\n" +
11501 "%_ptr_float = OpTypePointer Function %float\n" +
11502 "%main = OpFunction %void None %void_func\n" +
11503 "%main_lab = OpLabel\n" +
11504 "%x = OpVariable %_ptr_float Function\n" +
11505 "%y = OpVariable %_ptr_float Function\n" +
11506 "%a = OpVariable %_ptr_float Function\n" +
11507 "%lx = OpLoad %float %x\n" +
11508 "%ly = OpLoad %float %y\n" +
11509 "%mul = OpFMul %float %lx %ly\n" +
11510 "%la = OpLoad %float %a\n" +
11511 "%3 = OpFAdd %float %mul %la\n" +
11512 "OpStore %a %3\n" +
11513 "OpReturn\n" +
11514 "OpFunctionEnd",
11515 3, false),
11516 // Test case 6: Don't fold (x * y) - a
11517 InstructionFoldingCase<bool>(
11518 Header() +
11519 "%main = OpFunction %void None %void_func\n" +
11520 "%main_lab = OpLabel\n" +
11521 "%x = OpVariable %_ptr_float Function\n" +
11522 "%y = OpVariable %_ptr_float Function\n" +
11523 "%a = OpVariable %_ptr_float Function\n" +
11524 "%lx = OpLoad %float %x\n" +
11525 "%ly = OpLoad %float %y\n" +
11526 "%mul = OpFMul %float %lx %ly\n" +
11527 "%la = OpLoad %float %a\n" +
11528 "%3 = OpFSub %float %mul %la\n" +
11529 "OpStore %a %3\n" +
11530 "OpReturn\n" +
11531 "OpFunctionEnd",
11532 3, false),
11533 // Test case 7: Don't fold a - (x * y)
11534 InstructionFoldingCase<bool>(
11535 Header() +
11536 "%main = OpFunction %void None %void_func\n" +
11537 "%main_lab = OpLabel\n" +
11538 "%x = OpVariable %_ptr_float Function\n" +
11539 "%y = OpVariable %_ptr_float Function\n" +
11540 "%a = OpVariable %_ptr_float Function\n" +
11541 "%lx = OpLoad %float %x\n" +
11542 "%ly = OpLoad %float %y\n" +
11543 "%mul = OpFMul %float %lx %ly\n" +
11544 "%la = OpLoad %float %a\n" +
11545 "%3 = OpFSub %float %la %mul\n" +
11546 "OpStore %a %3\n" +
11547 "OpReturn\n" +
11548 "OpFunctionEnd",
11549 3, false)
11550 ));
11551
11552 using MatchingInstructionWithNoResultFoldingTest =
11553 ::testing::TestWithParam<InstructionFoldingCase<bool>>;
11554
11555 // Test folding instructions that do not have a result. The instruction
11556 // that will be folded is the last instruction before the return. If there
11557 // are multiple returns, there is not guarantee which one is used.
11558 TEST_P(MatchingInstructionWithNoResultFoldingTest, Case) {
11559 const auto& tc = GetParam();
11560
11561 std::unique_ptr<IRContext> context;
11562 Instruction* inst;
11563 std::tie(context, inst) = FoldInstruction(tc.test_body, tc.id_to_fold,SPV_ENV_UNIVERSAL_1_1);
11564
11565 // Find the instruction to test.
11566 EXPECT_EQ(inst != nullptr, tc.expected_result);
11567 if (inst != nullptr) {
11568 Match(tc.test_body, context.get());
11569 }
11570 }
11571
11572 INSTANTIATE_TEST_SUITE_P(StoreMatchingTest, MatchingInstructionWithNoResultFoldingTest,
11573 ::testing::Values(
11574 // Test case 0: Remove store of undef.
11575 InstructionFoldingCase<bool>(
11576 Header() +
11577 "; CHECK: OpLabel\n" +
11578 "; CHECK-NOT: OpStore\n" +
11579 "; CHECK: OpReturn\n" +
11580 "%main = OpFunction %void None %void_func\n" +
11581 "%main_lab = OpLabel\n" +
11582 "%n = OpVariable %_ptr_v4double Function\n" +
11583 "%undef = OpUndef %v4double\n" +
11584 "OpStore %n %undef\n" +
11585 "OpReturn\n" +
11586 "OpFunctionEnd",
11587 0 /* OpStore */, true),
11588 // Test case 1: Keep volatile store.
11589 InstructionFoldingCase<bool>(
11590 Header() +
11591 "%main = OpFunction %void None %void_func\n" +
11592 "%main_lab = OpLabel\n" +
11593 "%n = OpVariable %_ptr_v4double Function\n" +
11594 "%undef = OpUndef %v4double\n" +
11595 "OpStore %n %undef Volatile\n" +
11596 "OpReturn\n" +
11597 "OpFunctionEnd",
11598 0 /* OpStore */, false)
11599 ));
11600
11601 INSTANTIATE_TEST_SUITE_P(VectorShuffleMatchingTest, MatchingInstructionWithNoResultFoldingTest,
11602 ::testing::Values(
11603 // Test case 0: Basic test 1
11604 InstructionFoldingCase<bool>(
11605 Header() +
11606 "; CHECK: OpVectorShuffle\n" +
11607 "; CHECK: OpVectorShuffle {{%\\w+}} %7 %5 2 3 6 7\n" +
11608 "; CHECK: OpReturn\n" +
11609 "%main = OpFunction %void None %void_func\n" +
11610 "%main_lab = OpLabel\n" +
11611 "%2 = OpVariable %_ptr_v4double Function\n" +
11612 "%3 = OpVariable %_ptr_v4double Function\n" +
11613 "%4 = OpVariable %_ptr_v4double Function\n" +
11614 "%5 = OpLoad %v4double %2\n" +
11615 "%6 = OpLoad %v4double %3\n" +
11616 "%7 = OpLoad %v4double %4\n" +
11617 "%8 = OpVectorShuffle %v4double %5 %6 2 3 4 5\n" +
11618 "%9 = OpVectorShuffle %v4double %7 %8 2 3 4 5\n" +
11619 "OpReturn\n" +
11620 "OpFunctionEnd",
11621 9, true),
11622 // Test case 1: Basic test 2
11623 InstructionFoldingCase<bool>(
11624 Header() +
11625 "; CHECK: OpVectorShuffle\n" +
11626 "; CHECK: OpVectorShuffle {{%\\w+}} %6 %7 0 1 4 5\n" +
11627 "; CHECK: OpReturn\n" +
11628 "%main = OpFunction %void None %void_func\n" +
11629 "%main_lab = OpLabel\n" +
11630 "%2 = OpVariable %_ptr_v4double Function\n" +
11631 "%3 = OpVariable %_ptr_v4double Function\n" +
11632 "%4 = OpVariable %_ptr_v4double Function\n" +
11633 "%5 = OpLoad %v4double %2\n" +
11634 "%6 = OpLoad %v4double %3\n" +
11635 "%7 = OpLoad %v4double %4\n" +
11636 "%8 = OpVectorShuffle %v4double %5 %6 2 3 4 5\n" +
11637 "%9 = OpVectorShuffle %v4double %8 %7 2 3 4 5\n" +
11638 "OpReturn\n" +
11639 "OpFunctionEnd",
11640 9, true),
11641 // Test case 2: Basic test 3
11642 InstructionFoldingCase<bool>(
11643 Header() +
11644 "; CHECK: OpVectorShuffle\n" +
11645 "; CHECK: OpVectorShuffle {{%\\w+}} %5 %7 3 2 4 5\n" +
11646 "; CHECK: OpReturn\n" +
11647 "%main = OpFunction %void None %void_func\n" +
11648 "%main_lab = OpLabel\n" +
11649 "%2 = OpVariable %_ptr_v4double Function\n" +
11650 "%3 = OpVariable %_ptr_v4double Function\n" +
11651 "%4 = OpVariable %_ptr_v4double Function\n" +
11652 "%5 = OpLoad %v4double %2\n" +
11653 "%6 = OpLoad %v4double %3\n" +
11654 "%7 = OpLoad %v4double %4\n" +
11655 "%8 = OpVectorShuffle %v4double %5 %6 2 3 4 5\n" +
11656 "%9 = OpVectorShuffle %v4double %8 %7 1 0 4 5\n" +
11657 "OpReturn\n" +
11658 "OpFunctionEnd",
11659 9, true),
11660 // Test case 3: Basic test 4
11661 InstructionFoldingCase<bool>(
11662 Header() +
11663 "; CHECK: OpVectorShuffle\n" +
11664 "; CHECK: OpVectorShuffle {{%\\w+}} %7 %6 2 3 5 4\n" +
11665 "; CHECK: OpReturn\n" +
11666 "%main = OpFunction %void None %void_func\n" +
11667 "%main_lab = OpLabel\n" +
11668 "%2 = OpVariable %_ptr_v4double Function\n" +
11669 "%3 = OpVariable %_ptr_v4double Function\n" +
11670 "%4 = OpVariable %_ptr_v4double Function\n" +
11671 "%5 = OpLoad %v4double %2\n" +
11672 "%6 = OpLoad %v4double %3\n" +
11673 "%7 = OpLoad %v4double %4\n" +
11674 "%8 = OpVectorShuffle %v4double %5 %6 2 3 4 5\n" +
11675 "%9 = OpVectorShuffle %v4double %7 %8 2 3 7 6\n" +
11676 "OpReturn\n" +
11677 "OpFunctionEnd",
11678 9, true),
11679 // Test case 4: Don't fold, need both operands of the feeder.
11680 InstructionFoldingCase<bool>(
11681 Header() +
11682 "%main = OpFunction %void None %void_func\n" +
11683 "%main_lab = OpLabel\n" +
11684 "%2 = OpVariable %_ptr_v4double Function\n" +
11685 "%3 = OpVariable %_ptr_v4double Function\n" +
11686 "%4 = OpVariable %_ptr_v4double Function\n" +
11687 "%5 = OpLoad %v4double %2\n" +
11688 "%6 = OpLoad %v4double %3\n" +
11689 "%7 = OpLoad %v4double %4\n" +
11690 "%8 = OpVectorShuffle %v4double %5 %6 2 3 4 5\n" +
11691 "%9 = OpVectorShuffle %v4double %7 %8 2 3 7 5\n" +
11692 "OpReturn\n" +
11693 "OpFunctionEnd",
11694 9, false),
11695 // Test case 5: Don't fold, need both operands of the feeder.
11696 InstructionFoldingCase<bool>(
11697 Header() +
11698 "%main = OpFunction %void None %void_func\n" +
11699 "%main_lab = OpLabel\n" +
11700 "%2 = OpVariable %_ptr_v4double Function\n" +
11701 "%3 = OpVariable %_ptr_v4double Function\n" +
11702 "%4 = OpVariable %_ptr_v4double Function\n" +
11703 "%5 = OpLoad %v4double %2\n" +
11704 "%6 = OpLoad %v4double %3\n" +
11705 "%7 = OpLoad %v4double %4\n" +
11706 "%8 = OpVectorShuffle %v4double %5 %6 2 3 4 5\n" +
11707 "%9 = OpVectorShuffle %v4double %8 %7 2 0 7 5\n" +
11708 "OpReturn\n" +
11709 "OpFunctionEnd",
11710 9, false),
11711 // Test case 6: Fold, need both operands of the feeder, but they are the same.
11712 InstructionFoldingCase<bool>(
11713 Header() +
11714 "; CHECK: OpVectorShuffle\n" +
11715 "; CHECK: OpVectorShuffle {{%\\w+}} %5 %7 0 2 7 5\n" +
11716 "; CHECK: OpReturn\n" +
11717 "%main = OpFunction %void None %void_func\n" +
11718 "%main_lab = OpLabel\n" +
11719 "%2 = OpVariable %_ptr_v4double Function\n" +
11720 "%3 = OpVariable %_ptr_v4double Function\n" +
11721 "%4 = OpVariable %_ptr_v4double Function\n" +
11722 "%5 = OpLoad %v4double %2\n" +
11723 "%6 = OpLoad %v4double %3\n" +
11724 "%7 = OpLoad %v4double %4\n" +
11725 "%8 = OpVectorShuffle %v4double %5 %5 2 3 4 5\n" +
11726 "%9 = OpVectorShuffle %v4double %8 %7 2 0 7 5\n" +
11727 "OpReturn\n" +
11728 "OpFunctionEnd",
11729 9, true),
11730 // Test case 7: Fold, need both operands of the feeder, but they are the same.
11731 InstructionFoldingCase<bool>(
11732 Header() +
11733 "; CHECK: OpVectorShuffle\n" +
11734 "; CHECK: OpVectorShuffle {{%\\w+}} %7 %5 2 0 5 7\n" +
11735 "; CHECK: OpReturn\n" +
11736 "%main = OpFunction %void None %void_func\n" +
11737 "%main_lab = OpLabel\n" +
11738 "%2 = OpVariable %_ptr_v4double Function\n" +
11739 "%3 = OpVariable %_ptr_v4double Function\n" +
11740 "%4 = OpVariable %_ptr_v4double Function\n" +
11741 "%5 = OpLoad %v4double %2\n" +
11742 "%6 = OpLoad %v4double %3\n" +
11743 "%7 = OpLoad %v4double %4\n" +
11744 "%8 = OpVectorShuffle %v4double %5 %5 2 3 4 5\n" +
11745 "%9 = OpVectorShuffle %v4double %7 %8 2 0 7 5\n" +
11746 "OpReturn\n" +
11747 "OpFunctionEnd",
11748 9, true),
11749 // Test case 8: Replace first operand with a smaller vector.
11750 InstructionFoldingCase<bool>(
11751 Header() +
11752 "; CHECK: OpVectorShuffle\n" +
11753 "; CHECK: OpVectorShuffle {{%\\w+}} %5 %7 0 0 5 3\n" +
11754 "; CHECK: OpReturn\n" +
11755 "%main = OpFunction %void None %void_func\n" +
11756 "%main_lab = OpLabel\n" +
11757 "%2 = OpVariable %_ptr_v2double Function\n" +
11758 "%3 = OpVariable %_ptr_v4double Function\n" +
11759 "%4 = OpVariable %_ptr_v4double Function\n" +
11760 "%5 = OpLoad %v2double %2\n" +
11761 "%6 = OpLoad %v4double %3\n" +
11762 "%7 = OpLoad %v4double %4\n" +
11763 "%8 = OpVectorShuffle %v4double %5 %5 0 1 2 3\n" +
11764 "%9 = OpVectorShuffle %v4double %8 %7 2 0 7 5\n" +
11765 "OpReturn\n" +
11766 "OpFunctionEnd",
11767 9, true),
11768 // Test case 9: Replace first operand with a larger vector.
11769 InstructionFoldingCase<bool>(
11770 Header() +
11771 "; CHECK: OpVectorShuffle\n" +
11772 "; CHECK: OpVectorShuffle {{%\\w+}} %5 %7 3 0 7 5\n" +
11773 "; CHECK: OpReturn\n" +
11774 "%main = OpFunction %void None %void_func\n" +
11775 "%main_lab = OpLabel\n" +
11776 "%2 = OpVariable %_ptr_v4double Function\n" +
11777 "%3 = OpVariable %_ptr_v4double Function\n" +
11778 "%4 = OpVariable %_ptr_v4double Function\n" +
11779 "%5 = OpLoad %v4double %2\n" +
11780 "%6 = OpLoad %v4double %3\n" +
11781 "%7 = OpLoad %v4double %4\n" +
11782 "%8 = OpVectorShuffle %v2double %5 %5 0 3\n" +
11783 "%9 = OpVectorShuffle %v4double %8 %7 1 0 5 3\n" +
11784 "OpReturn\n" +
11785 "OpFunctionEnd",
11786 9, true),
11787 // Test case 10: Replace unused operand with null.
11788 InstructionFoldingCase<bool>(
11789 Header() +
11790 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11791 "; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 2\n" +
11792 "; CHECK: [[null:%\\w+]] = OpConstantNull [[v4double]]\n" +
11793 "; CHECK: OpVectorShuffle\n" +
11794 "; CHECK: OpVectorShuffle {{%\\w+}} [[null]] %7 4 2 5 3\n" +
11795 "; CHECK: OpReturn\n" +
11796 "%main = OpFunction %void None %void_func\n" +
11797 "%main_lab = OpLabel\n" +
11798 "%2 = OpVariable %_ptr_v4double Function\n" +
11799 "%3 = OpVariable %_ptr_v4double Function\n" +
11800 "%4 = OpVariable %_ptr_v4double Function\n" +
11801 "%5 = OpLoad %v4double %2\n" +
11802 "%6 = OpLoad %v4double %3\n" +
11803 "%7 = OpLoad %v4double %4\n" +
11804 "%8 = OpVectorShuffle %v2double %5 %5 0 3\n" +
11805 "%9 = OpVectorShuffle %v4double %8 %7 4 2 5 3\n" +
11806 "OpReturn\n" +
11807 "OpFunctionEnd",
11808 9, true),
11809 // Test case 11: Replace unused operand with null.
11810 InstructionFoldingCase<bool>(
11811 Header() +
11812 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11813 "; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 2\n" +
11814 "; CHECK: [[null:%\\w+]] = OpConstantNull [[v4double]]\n" +
11815 "; CHECK: OpVectorShuffle\n" +
11816 "; CHECK: OpVectorShuffle {{%\\w+}} [[null]] %5 2 2 5 5\n" +
11817 "; CHECK: OpReturn\n" +
11818 "%main = OpFunction %void None %void_func\n" +
11819 "%main_lab = OpLabel\n" +
11820 "%2 = OpVariable %_ptr_v4double Function\n" +
11821 "%3 = OpVariable %_ptr_v4double Function\n" +
11822 "%5 = OpLoad %v4double %2\n" +
11823 "%6 = OpLoad %v4double %3\n" +
11824 "%8 = OpVectorShuffle %v2double %5 %5 0 3\n" +
11825 "%9 = OpVectorShuffle %v4double %8 %8 2 2 3 3\n" +
11826 "OpReturn\n" +
11827 "OpFunctionEnd",
11828 9, true),
11829 // Test case 12: Replace unused operand with null.
11830 InstructionFoldingCase<bool>(
11831 Header() +
11832 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11833 "; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 2\n" +
11834 "; CHECK: [[null:%\\w+]] = OpConstantNull [[v4double]]\n" +
11835 "; CHECK: OpVectorShuffle\n" +
11836 "; CHECK: OpVectorShuffle {{%\\w+}} %7 [[null]] 2 0 1 3\n" +
11837 "; CHECK: OpReturn\n" +
11838 "%main = OpFunction %void None %void_func\n" +
11839 "%main_lab = OpLabel\n" +
11840 "%2 = OpVariable %_ptr_v4double Function\n" +
11841 "%3 = OpVariable %_ptr_v4double Function\n" +
11842 "%4 = OpVariable %_ptr_v4double Function\n" +
11843 "%5 = OpLoad %v4double %2\n" +
11844 "%6 = OpLoad %v4double %3\n" +
11845 "%7 = OpLoad %v4double %4\n" +
11846 "%8 = OpVectorShuffle %v2double %5 %5 0 3\n" +
11847 "%9 = OpVectorShuffle %v4double %7 %8 2 0 1 3\n" +
11848 "OpReturn\n" +
11849 "OpFunctionEnd",
11850 9, true),
11851 // Test case 13: Shuffle with undef literal.
11852 InstructionFoldingCase<bool>(
11853 Header() +
11854 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11855 "; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 2\n" +
11856 "; CHECK: OpVectorShuffle\n" +
11857 "; CHECK: OpVectorShuffle {{%\\w+}} %7 {{%\\w+}} 2 0 1 4294967295\n" +
11858 "; CHECK: OpReturn\n" +
11859 "%main = OpFunction %void None %void_func\n" +
11860 "%main_lab = OpLabel\n" +
11861 "%2 = OpVariable %_ptr_v4double Function\n" +
11862 "%3 = OpVariable %_ptr_v4double Function\n" +
11863 "%4 = OpVariable %_ptr_v4double Function\n" +
11864 "%5 = OpLoad %v4double %2\n" +
11865 "%6 = OpLoad %v4double %3\n" +
11866 "%7 = OpLoad %v4double %4\n" +
11867 "%8 = OpVectorShuffle %v2double %5 %5 0 1\n" +
11868 "%9 = OpVectorShuffle %v4double %7 %8 2 0 1 4294967295\n" +
11869 "OpReturn\n" +
11870 "OpFunctionEnd",
11871 9, true),
11872 // Test case 14: Shuffle with undef literal and change size of first input vector.
11873 InstructionFoldingCase<bool>(
11874 Header() +
11875 "; CHECK: [[double:%\\w+]] = OpTypeFloat 64\n" +
11876 "; CHECK: [[v4double:%\\w+]] = OpTypeVector [[double]] 2\n" +
11877 "; CHECK: OpVectorShuffle\n" +
11878 "; CHECK: OpVectorShuffle {{%\\w+}} %5 %7 0 1 4 4294967295\n" +
11879 "; CHECK: OpReturn\n" +
11880 "%main = OpFunction %void None %void_func\n" +
11881 "%main_lab = OpLabel\n" +
11882 "%2 = OpVariable %_ptr_v4double Function\n" +
11883 "%3 = OpVariable %_ptr_v4double Function\n" +
11884 "%4 = OpVariable %_ptr_v4double Function\n" +
11885 "%5 = OpLoad %v4double %2\n" +
11886 "%6 = OpLoad %v4double %3\n" +
11887 "%7 = OpLoad %v4double %4\n" +
11888 "%8 = OpVectorShuffle %v2double %5 %5 0 1\n" +
11889 "%9 = OpVectorShuffle %v4double %8 %7 0 1 2 4294967295\n" +
11890 "OpReturn\n" +
11891 "OpFunctionEnd",
11892 9, true)
11893 ));
11894
11895 using EntryPointFoldingTest =
11896 ::testing::TestWithParam<InstructionFoldingCase<bool>>;
11897
11898 TEST_P(EntryPointFoldingTest, Case) {
11899 const auto& tc = GetParam();
11900
11901 // Build module.
11902 std::unique_ptr<IRContext> context =
11903 BuildModule(SPV_ENV_UNIVERSAL_1_1, nullptr, tc.test_body,
11904 SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
11905 ASSERT_NE(nullptr, context);
11906
11907 // Find the first entry point. That is the instruction we want to fold.
11908 Instruction* inst = nullptr;
11909 ASSERT_FALSE(context->module()->entry_points().empty());
11910 inst = &*context->module()->entry_points().begin();
11911 assert(inst && "Invalid test. Could not find entry point instruction to fold.");
11912 std::unique_ptr<Instruction> original_inst(inst->Clone(context.get()));
11913 bool succeeded = context->get_instruction_folder().FoldInstruction(inst);
11914 EXPECT_EQ(succeeded, tc.expected_result);
11915 if (succeeded) {
11916 Match(tc.test_body, context.get());
11917 }
11918 }
11919
11920 INSTANTIATE_TEST_SUITE_P(OpEntryPointFoldingTest, EntryPointFoldingTest,
11921 ::testing::Values(
11922 // Test case 0: Basic test 1
11923 InstructionFoldingCase<bool>(std::string() +
11924 "; CHECK: OpEntryPoint Fragment %2 \"main\" %3\n" +
11925 "OpCapability Shader\n" +
11926 "%1 = OpExtInstImport \"GLSL.std.450\"\n" +
11927 "OpMemoryModel Logical GLSL450\n" +
11928 "OpEntryPoint Fragment %2 \"main\" %3 %3 %3\n" +
11929 "OpExecutionMode %2 OriginUpperLeft\n" +
11930 "OpSource GLSL 430\n" +
11931 "OpDecorate %3 Location 0\n" +
11932 "%void = OpTypeVoid\n" +
11933 "%5 = OpTypeFunction %void\n" +
11934 "%float = OpTypeFloat 32\n" +
11935 "%v4float = OpTypeVector %float 4\n" +
11936 "%_ptr_Output_v4float = OpTypePointer Output %v4float\n" +
11937 "%3 = OpVariable %_ptr_Output_v4float Output\n" +
11938 "%int = OpTypeInt 32 1\n" +
11939 "%int_0 = OpConstant %int 0\n" +
11940 "%_ptr_PushConstant_v4float = OpTypePointer PushConstant %v4float\n" +
11941 "%2 = OpFunction %void None %5\n" +
11942 "%12 = OpLabel\n" +
11943 "OpReturn\n" +
11944 "OpFunctionEnd\n",
11945 9, true),
11946 InstructionFoldingCase<bool>(std::string() +
11947 "; CHECK: OpEntryPoint Fragment %2 \"main\" %3 %4\n" +
11948 "OpCapability Shader\n" +
11949 "%1 = OpExtInstImport \"GLSL.std.450\"\n" +
11950 "OpMemoryModel Logical GLSL450\n" +
11951 "OpEntryPoint Fragment %2 \"main\" %3 %4 %3\n" +
11952 "OpExecutionMode %2 OriginUpperLeft\n" +
11953 "OpSource GLSL 430\n" +
11954 "OpDecorate %3 Location 0\n" +
11955 "%void = OpTypeVoid\n" +
11956 "%5 = OpTypeFunction %void\n" +
11957 "%float = OpTypeFloat 32\n" +
11958 "%v4float = OpTypeVector %float 4\n" +
11959 "%_ptr_Output_v4float = OpTypePointer Output %v4float\n" +
11960 "%3 = OpVariable %_ptr_Output_v4float Output\n" +
11961 "%4 = OpVariable %_ptr_Output_v4float Output\n" +
11962 "%int = OpTypeInt 32 1\n" +
11963 "%int_0 = OpConstant %int 0\n" +
11964 "%_ptr_PushConstant_v4float = OpTypePointer PushConstant %v4float\n" +
11965 "%2 = OpFunction %void None %5\n" +
11966 "%12 = OpLabel\n" +
11967 "OpReturn\n" +
11968 "OpFunctionEnd\n",
11969 9, true),
11970 InstructionFoldingCase<bool>(std::string() +
11971 "; CHECK: OpEntryPoint Fragment %2 \"main\" %4 %3\n" +
11972 "OpCapability Shader\n" +
11973 "%1 = OpExtInstImport \"GLSL.std.450\"\n" +
11974 "OpMemoryModel Logical GLSL450\n" +
11975 "OpEntryPoint Fragment %2 \"main\" %4 %4 %3\n" +
11976 "OpExecutionMode %2 OriginUpperLeft\n" +
11977 "OpSource GLSL 430\n" +
11978 "OpDecorate %3 Location 0\n" +
11979 "%void = OpTypeVoid\n" +
11980 "%5 = OpTypeFunction %void\n" +
11981 "%float = OpTypeFloat 32\n" +
11982 "%v4float = OpTypeVector %float 4\n" +
11983 "%_ptr_Output_v4float = OpTypePointer Output %v4float\n" +
11984 "%3 = OpVariable %_ptr_Output_v4float Output\n" +
11985 "%4 = OpVariable %_ptr_Output_v4float Output\n" +
11986 "%int = OpTypeInt 32 1\n" +
11987 "%int_0 = OpConstant %int 0\n" +
11988 "%_ptr_PushConstant_v4float = OpTypePointer PushConstant %v4float\n" +
11989 "%2 = OpFunction %void None %5\n" +
11990 "%12 = OpLabel\n" +
11991 "OpReturn\n" +
11992 "OpFunctionEnd\n",
11993 9, true)
11994 ));
11995
11996 using SPV14FoldingTest =
11997 ::testing::TestWithParam<InstructionFoldingCase<bool>>;
11998
11999 TEST_P(SPV14FoldingTest, Case) {
12000 const auto& tc = GetParam();
12001
12002 std::unique_ptr<IRContext> context;
12003 Instruction* inst;
12004 std::tie(context, inst) = FoldInstruction(tc.test_body, tc.id_to_fold,SPV_ENV_UNIVERSAL_1_4);
12005
12006 EXPECT_EQ(inst != nullptr, tc.expected_result);
12007 if (inst != nullptr) {
12008 Match(tc.test_body, context.get());
12009 }
12010 }
12011
12012 INSTANTIATE_TEST_SUITE_P(SPV14FoldingTest, SPV14FoldingTest,
12013 ::testing::Values(
12014 // Test case 0: select vectors with scalar condition.
12015 InstructionFoldingCase<bool>(std::string() +
12016 "; CHECK-NOT: OpSelect\n" +
12017 "; CHECK: %3 = OpCopyObject {{%\\w+}} %1\n" +
12018 "OpCapability Shader\n" +
12019 "OpCapability Linkage\n" +
12020 "%void = OpTypeVoid\n" +
12021 "%bool = OpTypeBool\n" +
12022 "%true = OpConstantTrue %bool\n" +
12023 "%int = OpTypeInt 32 0\n" +
12024 "%int4 = OpTypeVector %int 4\n" +
12025 "%int_0 = OpConstant %int 0\n" +
12026 "%int_1 = OpConstant %int 1\n" +
12027 "%1 = OpUndef %int4\n" +
12028 "%2 = OpUndef %int4\n" +
12029 "%void_fn = OpTypeFunction %void\n" +
12030 "%func = OpFunction %void None %void_fn\n" +
12031 "%entry = OpLabel\n" +
12032 "%3 = OpSelect %int4 %true %1 %2\n" +
12033 "OpReturn\n" +
12034 "OpFunctionEnd\n"
12035 ,
12036 3, true),
12037 // Test case 1: select struct with scalar condition.
12038 InstructionFoldingCase<bool>(std::string() +
12039 "; CHECK-NOT: OpSelect\n" +
12040 "; CHECK: %3 = OpCopyObject {{%\\w+}} %2\n" +
12041 "OpCapability Shader\n" +
12042 "OpCapability Linkage\n" +
12043 "%void = OpTypeVoid\n" +
12044 "%bool = OpTypeBool\n" +
12045 "%true = OpConstantFalse %bool\n" +
12046 "%int = OpTypeInt 32 0\n" +
12047 "%struct = OpTypeStruct %int %int %int %int\n" +
12048 "%int_0 = OpConstant %int 0\n" +
12049 "%int_1 = OpConstant %int 1\n" +
12050 "%1 = OpUndef %struct\n" +
12051 "%2 = OpUndef %struct\n" +
12052 "%void_fn = OpTypeFunction %void\n" +
12053 "%func = OpFunction %void None %void_fn\n" +
12054 "%entry = OpLabel\n" +
12055 "%3 = OpSelect %struct %true %1 %2\n" +
12056 "OpReturn\n" +
12057 "OpFunctionEnd\n"
12058 ,
12059 3, true),
12060 // Test case 1: select array with scalar condition.
12061 InstructionFoldingCase<bool>(std::string() +
12062 "; CHECK-NOT: OpSelect\n" +
12063 "; CHECK: %3 = OpCopyObject {{%\\w+}} %2\n" +
12064 "OpCapability Shader\n" +
12065 "OpCapability Linkage\n" +
12066 "%void = OpTypeVoid\n" +
12067 "%bool = OpTypeBool\n" +
12068 "%true = OpConstantFalse %bool\n" +
12069 "%int = OpTypeInt 32 0\n" +
12070 "%int_0 = OpConstant %int 0\n" +
12071 "%int_1 = OpConstant %int 1\n" +
12072 "%int_4 = OpConstant %int 4\n" +
12073 "%array = OpTypeStruct %int %int %int %int\n" +
12074 "%1 = OpUndef %array\n" +
12075 "%2 = OpUndef %array\n" +
12076 "%void_fn = OpTypeFunction %void\n" +
12077 "%func = OpFunction %void None %void_fn\n" +
12078 "%entry = OpLabel\n" +
12079 "%3 = OpSelect %array %true %1 %2\n" +
12080 "OpReturn\n" +
12081 "OpFunctionEnd\n"
12082 ,
12083 3, true)
12084 ));
12085
12086 std::string FloatControlsHeader(const std::string& capabilities) {
12087 std::string header = R"(
12088 OpCapability Shader
12089 )" + capabilities + R"(
12090 %void = OpTypeVoid
12091 %float = OpTypeFloat 32
12092 %float_0 = OpConstant %float 0
12093 %float_1 = OpConstant %float 1
12094 %void_fn = OpTypeFunction %void
12095 %func = OpFunction %void None %void_fn
12096 %entry = OpLabel
12097 )";
12098
12099 return header;
12100 }
12101
12102 using FloatControlsFoldingTest =
12103 ::testing::TestWithParam<InstructionFoldingCase<bool>>;
12104
12105 TEST_P(FloatControlsFoldingTest, Case) {
12106 const auto& tc = GetParam();
12107
12108 std::unique_ptr<IRContext> context;
12109 Instruction* inst;
12110 std::tie(context, inst) = FoldInstruction(tc.test_body, tc.id_to_fold, SPV_ENV_UNIVERSAL_1_4);
12111
12112 EXPECT_EQ(inst != nullptr, tc.expected_result);
12113 if (inst != nullptr) {
12114 Match(tc.test_body, context.get());
12115 }
12116 }
12117
12118 INSTANTIATE_TEST_SUITE_P(FloatControlsFoldingTest, FloatControlsFoldingTest,
12119 ::testing::Values(
12120 // Test case 0: no folding with DenormPreserve
12121 InstructionFoldingCase<bool>(FloatControlsHeader("OpCapability DenormPreserve") +
12122 "%1 = OpFAdd %float %float_0 %float_1\n" +
12123 "OpReturn\n" +
12124 "OpFunctionEnd\n"
12125 ,
12126 1, false),
12127 // Test case 1: no folding with DenormFlushToZero
12128 InstructionFoldingCase<bool>(FloatControlsHeader("OpCapability DenormFlushToZero") +
12129 "%1 = OpFAdd %float %float_0 %float_1\n" +
12130 "OpReturn\n" +
12131 "OpFunctionEnd\n"
12132 ,
12133 1, false),
12134 // Test case 2: no folding with SignedZeroInfNanPreserve
12135 InstructionFoldingCase<bool>(FloatControlsHeader("OpCapability SignedZeroInfNanPreserve") +
12136 "%1 = OpFAdd %float %float_0 %float_1\n" +
12137 "OpReturn\n" +
12138 "OpFunctionEnd\n"
12139 ,
12140 1, false),
12141 // Test case 3: no folding with RoundingModeRTE
12142 InstructionFoldingCase<bool>(FloatControlsHeader("OpCapability RoundingModeRTE") +
12143 "%1 = OpFAdd %float %float_0 %float_1\n" +
12144 "OpReturn\n" +
12145 "OpFunctionEnd\n"
12146 ,
12147 1, false),
12148 // Test case 4: no folding with RoundingModeRTZ
12149 InstructionFoldingCase<bool>(FloatControlsHeader("OpCapability RoundingModeRTZ") +
12150 "%1 = OpFAdd %float %float_0 %float_1\n" +
12151 "OpReturn\n" +
12152 "OpFunctionEnd\n"
12153 ,
12154 1, false)
12155 ));
12156
12157 std::string ImageOperandsTestBody(const std::string& image_instruction) {
12158 std::string body = R"(
12159 OpCapability Shader
12160 OpCapability ImageGatherExtended
12161 OpMemoryModel Logical GLSL450
12162 OpEntryPoint Fragment %main "main"
12163 OpExecutionMode %main OriginUpperLeft
12164 OpDecorate %Texture DescriptorSet 0
12165 OpDecorate %Texture Binding 0
12166 %int = OpTypeInt 32 1
12167 %int_n1 = OpConstant %int -1
12168 %5 = OpConstant %int 0
12169 %float = OpTypeFloat 32
12170 %float_0 = OpConstant %float 0
12171 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
12172 %type_sampled_image = OpTypeSampledImage %type_2d_image
12173 %type_sampler = OpTypeSampler
12174 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
12175 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
12176 %_ptr_int = OpTypePointer Function %int
12177 %v2int = OpTypeVector %int 2
12178 %10 = OpTypeVector %float 4
12179 %void = OpTypeVoid
12180 %22 = OpTypeFunction %void
12181 %v2float = OpTypeVector %float 2
12182 %v3int = OpTypeVector %int 3
12183 %Texture = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
12184 %gSampler = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
12185 %110 = OpConstantComposite %v2int %5 %5
12186 %101 = OpConstantComposite %v2int %int_n1 %int_n1
12187 %20 = OpConstantComposite %v2float %float_0 %float_0
12188 %main = OpFunction %void None %22
12189 %23 = OpLabel
12190 %var = OpVariable %_ptr_int Function
12191 %88 = OpLoad %type_2d_image %Texture
12192 %val = OpLoad %int %var
12193 %sampler = OpLoad %type_sampler %gSampler
12194 %26 = OpSampledImage %type_sampled_image %88 %sampler
12195 )" + image_instruction + R"(
12196 OpReturn
12197 OpFunctionEnd
12198 )";
12199
12200 return body;
12201 }
12202
12203 INSTANTIATE_TEST_SUITE_P(ImageOperandsBitmaskFoldingTest, MatchingInstructionWithNoResultFoldingTest,
12204 ::testing::Values(
12205 // Test case 0: OpImageFetch without Offset
12206 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12207 "%89 = OpImageFetch %10 %88 %101 Lod %5 \n")
12208 , 89, false),
12209 // Test case 1: OpImageFetch with non-const offset
12210 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12211 "%89 = OpImageFetch %10 %88 %101 Lod|Offset %5 %val \n")
12212 , 89, false),
12213 // Test case 2: OpImageFetch with Lod and Offset
12214 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12215 " %89 = OpImageFetch %10 %88 %101 Lod|Offset %5 %101 \n"
12216 "; CHECK: %89 = OpImageFetch %10 %88 %101 Lod|ConstOffset %5 %101 \n")
12217 , 89, true),
12218 // Test case 3: OpImageFetch with Bias and Offset
12219 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12220 " %89 = OpImageFetch %10 %88 %101 Bias|Offset %5 %101 \n"
12221 "; CHECK: %89 = OpImageFetch %10 %88 %101 Bias|ConstOffset %5 %101 \n")
12222 , 89, true),
12223 // Test case 4: OpImageFetch with Grad and Offset.
12224 // Grad adds 2 operands to the instruction.
12225 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12226 " %89 = OpImageFetch %10 %88 %101 Grad|Offset %5 %5 %101 \n"
12227 "; CHECK: %89 = OpImageFetch %10 %88 %101 Grad|ConstOffset %5 %5 %101 \n")
12228 , 89, true),
12229 // Test case 5: OpImageFetch with Offset and MinLod.
12230 // This is an example of a case where the bitmask bit-offset is larger than
12231 // that of the Offset.
12232 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12233 " %89 = OpImageFetch %10 %88 %101 Offset|MinLod %101 %5 \n"
12234 "; CHECK: %89 = OpImageFetch %10 %88 %101 ConstOffset|MinLod %101 %5 \n")
12235 , 89, true),
12236 // Test case 6: OpImageGather with constant Offset
12237 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12238 " %89 = OpImageGather %10 %26 %20 %5 Offset %101 \n"
12239 "; CHECK: %89 = OpImageGather %10 %26 %20 %5 ConstOffset %101 \n")
12240 , 89, true),
12241 // Test case 7: OpImageWrite with constant Offset
12242 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12243 " OpImageWrite %88 %5 %101 Offset %101 \n"
12244 "; CHECK: OpImageWrite %88 %5 %101 ConstOffset %101 \n")
12245 , 0 /* No result-id */, true),
12246 // Test case 8: OpImageFetch with zero constant Offset
12247 InstructionFoldingCase<bool>(ImageOperandsTestBody(
12248 " %89 = OpImageFetch %10 %88 %101 Lod|Offset %5 %110 \n"
12249 "; CHECK: %89 = OpImageFetch %10 %88 %101 Lod %5 \n")
12250 , 89, true)
12251 ));
12252
12253 } // namespace
12254 } // namespace opt
12255 } // namespace spvtools