1fd4e5da5Sopenharmony_ci// Copyright (c) 2018 Google LLC
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#include <string>
16fd4e5da5Sopenharmony_ci
17fd4e5da5Sopenharmony_ci#include "test/opt/pass_fixture.h"
18fd4e5da5Sopenharmony_ci#include "test/opt/pass_utils.h"
19fd4e5da5Sopenharmony_ci
20fd4e5da5Sopenharmony_cinamespace spvtools {
21fd4e5da5Sopenharmony_cinamespace opt {
22fd4e5da5Sopenharmony_cinamespace {
23fd4e5da5Sopenharmony_ci
24fd4e5da5Sopenharmony_ciusing VectorDCETest = PassTest<::testing::Test>;
25fd4e5da5Sopenharmony_ci
26fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, InsertAfterInsertElim) {
27fd4e5da5Sopenharmony_ci  // With two insertions to the same offset, the first is dead.
28fd4e5da5Sopenharmony_ci  //
29fd4e5da5Sopenharmony_ci  // Note: The SPIR-V assembly has had store/load elimination
30fd4e5da5Sopenharmony_ci  // performed to allow the inserts and extracts to directly
31fd4e5da5Sopenharmony_ci  // reference each other.
32fd4e5da5Sopenharmony_ci  //
33fd4e5da5Sopenharmony_ci  // #version 450
34fd4e5da5Sopenharmony_ci  //
35fd4e5da5Sopenharmony_ci  // layout (location=0) in float In0;
36fd4e5da5Sopenharmony_ci  // layout (location=1) in float In1;
37fd4e5da5Sopenharmony_ci  // layout (location=2) in vec2 In2;
38fd4e5da5Sopenharmony_ci  // layout (location=0) out vec4 OutColor;
39fd4e5da5Sopenharmony_ci  //
40fd4e5da5Sopenharmony_ci  // void main()
41fd4e5da5Sopenharmony_ci  // {
42fd4e5da5Sopenharmony_ci  //     vec2 v = In2;
43fd4e5da5Sopenharmony_ci  //     v.x = In0 + In1; // dead
44fd4e5da5Sopenharmony_ci  //     v.x = 0.0;
45fd4e5da5Sopenharmony_ci  //     OutColor = v.xyxy;
46fd4e5da5Sopenharmony_ci  // }
47fd4e5da5Sopenharmony_ci
48fd4e5da5Sopenharmony_ci  const std::string before_predefs =
49fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
50fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
51fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
52fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In2 %In0 %In1 %OutColor
53fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
54fd4e5da5Sopenharmony_ciOpSource GLSL 450
55fd4e5da5Sopenharmony_ciOpName %main "main"
56fd4e5da5Sopenharmony_ciOpName %In2 "In2"
57fd4e5da5Sopenharmony_ciOpName %In0 "In0"
58fd4e5da5Sopenharmony_ciOpName %In1 "In1"
59fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
60fd4e5da5Sopenharmony_ciOpName %_Globals_ "_Globals_"
61fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 0 "g_b"
62fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 1 "g_n"
63fd4e5da5Sopenharmony_ciOpName %_ ""
64fd4e5da5Sopenharmony_ciOpDecorate %In2 Location 2
65fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
66fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
67fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
68fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 0 Offset 0
69fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 1 Offset 4
70fd4e5da5Sopenharmony_ciOpDecorate %_Globals_ Block
71fd4e5da5Sopenharmony_ciOpDecorate %_ DescriptorSet 0
72fd4e5da5Sopenharmony_ciOpDecorate %_ Binding 0
73fd4e5da5Sopenharmony_ci%void = OpTypeVoid
74fd4e5da5Sopenharmony_ci%11 = OpTypeFunction %void
75fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
76fd4e5da5Sopenharmony_ci%v2float = OpTypeVector %float 2
77fd4e5da5Sopenharmony_ci%_ptr_Function_v2float = OpTypePointer Function %v2float
78fd4e5da5Sopenharmony_ci%_ptr_Input_v2float = OpTypePointer Input %v2float
79fd4e5da5Sopenharmony_ci%In2 = OpVariable %_ptr_Input_v2float Input
80fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
81fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_float Input
82fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
83fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
84fd4e5da5Sopenharmony_ci%_ptr_Function_float = OpTypePointer Function %float
85fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
86fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
87fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
88fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
89fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
90fd4e5da5Sopenharmony_ci%_Globals_ = OpTypeStruct %uint %int
91fd4e5da5Sopenharmony_ci%_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
92fd4e5da5Sopenharmony_ci%_ = OpVariable %_ptr_Uniform__Globals_ Uniform
93fd4e5da5Sopenharmony_ci)";
94fd4e5da5Sopenharmony_ci
95fd4e5da5Sopenharmony_ci  const std::string after_predefs =
96fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
97fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
98fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
99fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In2 %In0 %In1 %OutColor
100fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
101fd4e5da5Sopenharmony_ciOpSource GLSL 450
102fd4e5da5Sopenharmony_ciOpName %main "main"
103fd4e5da5Sopenharmony_ciOpName %In2 "In2"
104fd4e5da5Sopenharmony_ciOpName %In0 "In0"
105fd4e5da5Sopenharmony_ciOpName %In1 "In1"
106fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
107fd4e5da5Sopenharmony_ciOpName %_Globals_ "_Globals_"
108fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 0 "g_b"
109fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 1 "g_n"
110fd4e5da5Sopenharmony_ciOpName %_ ""
111fd4e5da5Sopenharmony_ciOpDecorate %In2 Location 2
112fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
113fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
114fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
115fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 0 Offset 0
116fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 1 Offset 4
117fd4e5da5Sopenharmony_ciOpDecorate %_Globals_ Block
118fd4e5da5Sopenharmony_ciOpDecorate %_ DescriptorSet 0
119fd4e5da5Sopenharmony_ciOpDecorate %_ Binding 0
120fd4e5da5Sopenharmony_ci%void = OpTypeVoid
121fd4e5da5Sopenharmony_ci%10 = OpTypeFunction %void
122fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
123fd4e5da5Sopenharmony_ci%v2float = OpTypeVector %float 2
124fd4e5da5Sopenharmony_ci%_ptr_Function_v2float = OpTypePointer Function %v2float
125fd4e5da5Sopenharmony_ci%_ptr_Input_v2float = OpTypePointer Input %v2float
126fd4e5da5Sopenharmony_ci%In2 = OpVariable %_ptr_Input_v2float Input
127fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
128fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_float Input
129fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
130fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
131fd4e5da5Sopenharmony_ci%_ptr_Function_float = OpTypePointer Function %float
132fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
133fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
134fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
135fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
136fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
137fd4e5da5Sopenharmony_ci%_Globals_ = OpTypeStruct %uint %int
138fd4e5da5Sopenharmony_ci%_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
139fd4e5da5Sopenharmony_ci%_ = OpVariable %_ptr_Uniform__Globals_ Uniform
140fd4e5da5Sopenharmony_ci)";
141fd4e5da5Sopenharmony_ci
142fd4e5da5Sopenharmony_ci  const std::string before =
143fd4e5da5Sopenharmony_ci      R"(%main = OpFunction %void None %11
144fd4e5da5Sopenharmony_ci%25 = OpLabel
145fd4e5da5Sopenharmony_ci%26 = OpLoad %v2float %In2
146fd4e5da5Sopenharmony_ci%27 = OpLoad %float %In0
147fd4e5da5Sopenharmony_ci%28 = OpLoad %float %In1
148fd4e5da5Sopenharmony_ci%29 = OpFAdd %float %27 %28
149fd4e5da5Sopenharmony_ci%35 = OpCompositeInsert %v2float %29 %26 0
150fd4e5da5Sopenharmony_ci%37 = OpCompositeInsert %v2float %float_0 %35 0
151fd4e5da5Sopenharmony_ci%33 = OpVectorShuffle %v4float %37 %37 0 1 0 1
152fd4e5da5Sopenharmony_ciOpStore %OutColor %33
153fd4e5da5Sopenharmony_ciOpReturn
154fd4e5da5Sopenharmony_ciOpFunctionEnd
155fd4e5da5Sopenharmony_ci)";
156fd4e5da5Sopenharmony_ci
157fd4e5da5Sopenharmony_ci  const std::string after =
158fd4e5da5Sopenharmony_ci      R"(%main = OpFunction %void None %10
159fd4e5da5Sopenharmony_ci%23 = OpLabel
160fd4e5da5Sopenharmony_ci%24 = OpLoad %v2float %In2
161fd4e5da5Sopenharmony_ci%25 = OpLoad %float %In0
162fd4e5da5Sopenharmony_ci%26 = OpLoad %float %In1
163fd4e5da5Sopenharmony_ci%27 = OpFAdd %float %25 %26
164fd4e5da5Sopenharmony_ci%28 = OpCompositeInsert %v2float %27 %24 0
165fd4e5da5Sopenharmony_ci%29 = OpCompositeInsert %v2float %float_0 %24 0
166fd4e5da5Sopenharmony_ci%30 = OpVectorShuffle %v4float %29 %29 0 1 0 1
167fd4e5da5Sopenharmony_ciOpStore %OutColor %30
168fd4e5da5Sopenharmony_ciOpReturn
169fd4e5da5Sopenharmony_ciOpFunctionEnd
170fd4e5da5Sopenharmony_ci)";
171fd4e5da5Sopenharmony_ci
172fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<VectorDCE>(before_predefs + before,
173fd4e5da5Sopenharmony_ci                                   after_predefs + after, true, true);
174fd4e5da5Sopenharmony_ci}
175fd4e5da5Sopenharmony_ci
176fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, DeadInsertInChainWithPhi) {
177fd4e5da5Sopenharmony_ci  // Dead insert eliminated with phi in insertion chain.
178fd4e5da5Sopenharmony_ci  //
179fd4e5da5Sopenharmony_ci  // Note: The SPIR-V assembly has had store/load elimination
180fd4e5da5Sopenharmony_ci  // performed to allow the inserts and extracts to directly
181fd4e5da5Sopenharmony_ci  // reference each other.
182fd4e5da5Sopenharmony_ci  //
183fd4e5da5Sopenharmony_ci  // #version 450
184fd4e5da5Sopenharmony_ci  //
185fd4e5da5Sopenharmony_ci  // layout (location=0) in vec4 In0;
186fd4e5da5Sopenharmony_ci  // layout (location=1) in float In1;
187fd4e5da5Sopenharmony_ci  // layout (location=2) in float In2;
188fd4e5da5Sopenharmony_ci  // layout (location=0) out vec4 OutColor;
189fd4e5da5Sopenharmony_ci  //
190fd4e5da5Sopenharmony_ci  // layout(std140, binding = 0 ) uniform _Globals_
191fd4e5da5Sopenharmony_ci  // {
192fd4e5da5Sopenharmony_ci  //     bool g_b;
193fd4e5da5Sopenharmony_ci  // };
194fd4e5da5Sopenharmony_ci  //
195fd4e5da5Sopenharmony_ci  // void main()
196fd4e5da5Sopenharmony_ci  // {
197fd4e5da5Sopenharmony_ci  //     vec4 v = In0;
198fd4e5da5Sopenharmony_ci  //     v.z = In1 + In2;
199fd4e5da5Sopenharmony_ci  //     if (g_b) v.w = 1.0;
200fd4e5da5Sopenharmony_ci  //     OutColor = vec4(v.x,v.y,0.0,v.w);
201fd4e5da5Sopenharmony_ci  // }
202fd4e5da5Sopenharmony_ci
203fd4e5da5Sopenharmony_ci  const std::string before_predefs =
204fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
205fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
206fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
207fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In0 %In1 %In2 %OutColor
208fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
209fd4e5da5Sopenharmony_ciOpSource GLSL 450
210fd4e5da5Sopenharmony_ciOpName %main "main"
211fd4e5da5Sopenharmony_ciOpName %In0 "In0"
212fd4e5da5Sopenharmony_ciOpName %In1 "In1"
213fd4e5da5Sopenharmony_ciOpName %In2 "In2"
214fd4e5da5Sopenharmony_ciOpName %_Globals_ "_Globals_"
215fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 0 "g_b"
216fd4e5da5Sopenharmony_ciOpName %_ ""
217fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
218fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
219fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
220fd4e5da5Sopenharmony_ciOpDecorate %In2 Location 2
221fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 0 Offset 0
222fd4e5da5Sopenharmony_ciOpDecorate %_Globals_ Block
223fd4e5da5Sopenharmony_ciOpDecorate %_ DescriptorSet 0
224fd4e5da5Sopenharmony_ciOpDecorate %_ Binding 0
225fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
226fd4e5da5Sopenharmony_ci%void = OpTypeVoid
227fd4e5da5Sopenharmony_ci%11 = OpTypeFunction %void
228fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
229fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
230fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
231fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
232fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_v4float Input
233fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
234fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
235fd4e5da5Sopenharmony_ci%In2 = OpVariable %_ptr_Input_float Input
236fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
237fd4e5da5Sopenharmony_ci%_ptr_Function_float = OpTypePointer Function %float
238fd4e5da5Sopenharmony_ci%_Globals_ = OpTypeStruct %uint
239fd4e5da5Sopenharmony_ci%_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
240fd4e5da5Sopenharmony_ci%_ = OpVariable %_ptr_Uniform__Globals_ Uniform
241fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
242fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
243fd4e5da5Sopenharmony_ci%_ptr_Uniform_uint = OpTypePointer Uniform %uint
244fd4e5da5Sopenharmony_ci%bool = OpTypeBool
245fd4e5da5Sopenharmony_ci%uint_0 = OpConstant %uint 0
246fd4e5da5Sopenharmony_ci%float_1 = OpConstant %float 1
247fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
248fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
249fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
250fd4e5da5Sopenharmony_ci)";
251fd4e5da5Sopenharmony_ci
252fd4e5da5Sopenharmony_ci  const std::string after_predefs =
253fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
254fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
255fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
256fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In0 %In1 %In2 %OutColor
257fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
258fd4e5da5Sopenharmony_ciOpSource GLSL 450
259fd4e5da5Sopenharmony_ciOpName %main "main"
260fd4e5da5Sopenharmony_ciOpName %In0 "In0"
261fd4e5da5Sopenharmony_ciOpName %In1 "In1"
262fd4e5da5Sopenharmony_ciOpName %In2 "In2"
263fd4e5da5Sopenharmony_ciOpName %_Globals_ "_Globals_"
264fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 0 "g_b"
265fd4e5da5Sopenharmony_ciOpName %_ ""
266fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
267fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
268fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
269fd4e5da5Sopenharmony_ciOpDecorate %In2 Location 2
270fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 0 Offset 0
271fd4e5da5Sopenharmony_ciOpDecorate %_Globals_ Block
272fd4e5da5Sopenharmony_ciOpDecorate %_ DescriptorSet 0
273fd4e5da5Sopenharmony_ciOpDecorate %_ Binding 0
274fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
275fd4e5da5Sopenharmony_ci%void = OpTypeVoid
276fd4e5da5Sopenharmony_ci%10 = OpTypeFunction %void
277fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
278fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
279fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
280fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
281fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_v4float Input
282fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
283fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
284fd4e5da5Sopenharmony_ci%In2 = OpVariable %_ptr_Input_float Input
285fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
286fd4e5da5Sopenharmony_ci%_ptr_Function_float = OpTypePointer Function %float
287fd4e5da5Sopenharmony_ci%_Globals_ = OpTypeStruct %uint
288fd4e5da5Sopenharmony_ci%_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
289fd4e5da5Sopenharmony_ci%_ = OpVariable %_ptr_Uniform__Globals_ Uniform
290fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
291fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
292fd4e5da5Sopenharmony_ci%_ptr_Uniform_uint = OpTypePointer Uniform %uint
293fd4e5da5Sopenharmony_ci%bool = OpTypeBool
294fd4e5da5Sopenharmony_ci%uint_0 = OpConstant %uint 0
295fd4e5da5Sopenharmony_ci%float_1 = OpConstant %float 1
296fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
297fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
298fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
299fd4e5da5Sopenharmony_ci)";
300fd4e5da5Sopenharmony_ci
301fd4e5da5Sopenharmony_ci  const std::string before =
302fd4e5da5Sopenharmony_ci      R"(%main = OpFunction %void None %11
303fd4e5da5Sopenharmony_ci%31 = OpLabel
304fd4e5da5Sopenharmony_ci%32 = OpLoad %v4float %In0
305fd4e5da5Sopenharmony_ci%33 = OpLoad %float %In1
306fd4e5da5Sopenharmony_ci%34 = OpLoad %float %In2
307fd4e5da5Sopenharmony_ci%35 = OpFAdd %float %33 %34
308fd4e5da5Sopenharmony_ci%51 = OpCompositeInsert %v4float %35 %32 2
309fd4e5da5Sopenharmony_ci%37 = OpAccessChain %_ptr_Uniform_uint %_ %int_0
310fd4e5da5Sopenharmony_ci%38 = OpLoad %uint %37
311fd4e5da5Sopenharmony_ci%39 = OpINotEqual %bool %38 %uint_0
312fd4e5da5Sopenharmony_ciOpSelectionMerge %40 None
313fd4e5da5Sopenharmony_ciOpBranchConditional %39 %41 %40
314fd4e5da5Sopenharmony_ci%41 = OpLabel
315fd4e5da5Sopenharmony_ci%53 = OpCompositeInsert %v4float %float_1 %51 3
316fd4e5da5Sopenharmony_ciOpBranch %40
317fd4e5da5Sopenharmony_ci%40 = OpLabel
318fd4e5da5Sopenharmony_ci%60 = OpPhi %v4float %51 %31 %53 %41
319fd4e5da5Sopenharmony_ci%55 = OpCompositeExtract %float %60 0
320fd4e5da5Sopenharmony_ci%57 = OpCompositeExtract %float %60 1
321fd4e5da5Sopenharmony_ci%59 = OpCompositeExtract %float %60 3
322fd4e5da5Sopenharmony_ci%49 = OpCompositeConstruct %v4float %55 %57 %float_0 %59
323fd4e5da5Sopenharmony_ciOpStore %OutColor %49
324fd4e5da5Sopenharmony_ciOpReturn
325fd4e5da5Sopenharmony_ciOpFunctionEnd
326fd4e5da5Sopenharmony_ci)";
327fd4e5da5Sopenharmony_ci
328fd4e5da5Sopenharmony_ci  const std::string after =
329fd4e5da5Sopenharmony_ci      R"(%main = OpFunction %void None %10
330fd4e5da5Sopenharmony_ci%27 = OpLabel
331fd4e5da5Sopenharmony_ci%28 = OpLoad %v4float %In0
332fd4e5da5Sopenharmony_ci%29 = OpLoad %float %In1
333fd4e5da5Sopenharmony_ci%30 = OpLoad %float %In2
334fd4e5da5Sopenharmony_ci%31 = OpFAdd %float %29 %30
335fd4e5da5Sopenharmony_ci%32 = OpCompositeInsert %v4float %31 %28 2
336fd4e5da5Sopenharmony_ci%33 = OpAccessChain %_ptr_Uniform_uint %_ %int_0
337fd4e5da5Sopenharmony_ci%34 = OpLoad %uint %33
338fd4e5da5Sopenharmony_ci%35 = OpINotEqual %bool %34 %uint_0
339fd4e5da5Sopenharmony_ciOpSelectionMerge %36 None
340fd4e5da5Sopenharmony_ciOpBranchConditional %35 %37 %36
341fd4e5da5Sopenharmony_ci%37 = OpLabel
342fd4e5da5Sopenharmony_ci%38 = OpCompositeInsert %v4float %float_1 %28 3
343fd4e5da5Sopenharmony_ciOpBranch %36
344fd4e5da5Sopenharmony_ci%36 = OpLabel
345fd4e5da5Sopenharmony_ci%39 = OpPhi %v4float %28 %27 %38 %37
346fd4e5da5Sopenharmony_ci%40 = OpCompositeExtract %float %39 0
347fd4e5da5Sopenharmony_ci%41 = OpCompositeExtract %float %39 1
348fd4e5da5Sopenharmony_ci%42 = OpCompositeExtract %float %39 3
349fd4e5da5Sopenharmony_ci%43 = OpCompositeConstruct %v4float %40 %41 %float_0 %42
350fd4e5da5Sopenharmony_ciOpStore %OutColor %43
351fd4e5da5Sopenharmony_ciOpReturn
352fd4e5da5Sopenharmony_ciOpFunctionEnd
353fd4e5da5Sopenharmony_ci)";
354fd4e5da5Sopenharmony_ci
355fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<VectorDCE>(before_predefs + before,
356fd4e5da5Sopenharmony_ci                                   after_predefs + after, true, true);
357fd4e5da5Sopenharmony_ci}
358fd4e5da5Sopenharmony_ci
359fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, DeadInsertWithScalars) {
360fd4e5da5Sopenharmony_ci  // Dead insert which requires two passes to eliminate
361fd4e5da5Sopenharmony_ci  //
362fd4e5da5Sopenharmony_ci  // Note: The SPIR-V assembly has had store/load elimination
363fd4e5da5Sopenharmony_ci  // performed to allow the inserts and extracts to directly
364fd4e5da5Sopenharmony_ci  // reference each other.
365fd4e5da5Sopenharmony_ci  //
366fd4e5da5Sopenharmony_ci  // #version 450
367fd4e5da5Sopenharmony_ci  //
368fd4e5da5Sopenharmony_ci  // layout (location=0) in vec4 In0;
369fd4e5da5Sopenharmony_ci  // layout (location=1) in float In1;
370fd4e5da5Sopenharmony_ci  // layout (location=2) in float In2;
371fd4e5da5Sopenharmony_ci  // layout (location=0) out vec4 OutColor;
372fd4e5da5Sopenharmony_ci  //
373fd4e5da5Sopenharmony_ci  // layout(std140, binding = 0 ) uniform _Globals_
374fd4e5da5Sopenharmony_ci  // {
375fd4e5da5Sopenharmony_ci  //     bool g_b;
376fd4e5da5Sopenharmony_ci  //     bool g_b2;
377fd4e5da5Sopenharmony_ci  // };
378fd4e5da5Sopenharmony_ci  //
379fd4e5da5Sopenharmony_ci  // void main()
380fd4e5da5Sopenharmony_ci  // {
381fd4e5da5Sopenharmony_ci  //     vec4 v1, v2;
382fd4e5da5Sopenharmony_ci  //     v1 = In0;
383fd4e5da5Sopenharmony_ci  //     v1.y = In1 + In2; // dead, second pass
384fd4e5da5Sopenharmony_ci  //     if (g_b) v1.x = 1.0;
385fd4e5da5Sopenharmony_ci  //     v2.x = v1.x;
386fd4e5da5Sopenharmony_ci  //     v2.y = v1.y; // dead, first pass
387fd4e5da5Sopenharmony_ci  //     if (g_b2) v2.x = 0.0;
388fd4e5da5Sopenharmony_ci  //     OutColor = vec4(v2.x,v2.x,0.0,1.0);
389fd4e5da5Sopenharmony_ci  // }
390fd4e5da5Sopenharmony_ci
391fd4e5da5Sopenharmony_ci  const std::string before_predefs =
392fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
393fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
394fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
395fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In0 %In1 %In2 %OutColor
396fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
397fd4e5da5Sopenharmony_ciOpSource GLSL 450
398fd4e5da5Sopenharmony_ciOpName %main "main"
399fd4e5da5Sopenharmony_ciOpName %In0 "In0"
400fd4e5da5Sopenharmony_ciOpName %In1 "In1"
401fd4e5da5Sopenharmony_ciOpName %In2 "In2"
402fd4e5da5Sopenharmony_ciOpName %_Globals_ "_Globals_"
403fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 0 "g_b"
404fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 1 "g_b2"
405fd4e5da5Sopenharmony_ciOpName %_ ""
406fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
407fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
408fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
409fd4e5da5Sopenharmony_ciOpDecorate %In2 Location 2
410fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 0 Offset 0
411fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 1 Offset 4
412fd4e5da5Sopenharmony_ciOpDecorate %_Globals_ Block
413fd4e5da5Sopenharmony_ciOpDecorate %_ DescriptorSet 0
414fd4e5da5Sopenharmony_ciOpDecorate %_ Binding 0
415fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
416fd4e5da5Sopenharmony_ci%void = OpTypeVoid
417fd4e5da5Sopenharmony_ci%10 = OpTypeFunction %void
418fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
419fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
420fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
421fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
422fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_v4float Input
423fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
424fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
425fd4e5da5Sopenharmony_ci%In2 = OpVariable %_ptr_Input_float Input
426fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
427fd4e5da5Sopenharmony_ci%_Globals_ = OpTypeStruct %uint %uint
428fd4e5da5Sopenharmony_ci%_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
429fd4e5da5Sopenharmony_ci%_ = OpVariable %_ptr_Uniform__Globals_ Uniform
430fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
431fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
432fd4e5da5Sopenharmony_ci%_ptr_Uniform_uint = OpTypePointer Uniform %uint
433fd4e5da5Sopenharmony_ci%bool = OpTypeBool
434fd4e5da5Sopenharmony_ci%uint_0 = OpConstant %uint 0
435fd4e5da5Sopenharmony_ci%float_1 = OpConstant %float 1
436fd4e5da5Sopenharmony_ci%int_1 = OpConstant %int 1
437fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
438fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
439fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
440fd4e5da5Sopenharmony_ci%27 = OpUndef %v4float
441fd4e5da5Sopenharmony_ci)";
442fd4e5da5Sopenharmony_ci
443fd4e5da5Sopenharmony_ci  const std::string after_predefs =
444fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
445fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
446fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
447fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In0 %In1 %In2 %OutColor
448fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
449fd4e5da5Sopenharmony_ciOpSource GLSL 450
450fd4e5da5Sopenharmony_ciOpName %main "main"
451fd4e5da5Sopenharmony_ciOpName %In0 "In0"
452fd4e5da5Sopenharmony_ciOpName %In1 "In1"
453fd4e5da5Sopenharmony_ciOpName %In2 "In2"
454fd4e5da5Sopenharmony_ciOpName %_Globals_ "_Globals_"
455fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 0 "g_b"
456fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 1 "g_b2"
457fd4e5da5Sopenharmony_ciOpName %_ ""
458fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
459fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
460fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
461fd4e5da5Sopenharmony_ciOpDecorate %In2 Location 2
462fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 0 Offset 0
463fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 1 Offset 4
464fd4e5da5Sopenharmony_ciOpDecorate %_Globals_ Block
465fd4e5da5Sopenharmony_ciOpDecorate %_ DescriptorSet 0
466fd4e5da5Sopenharmony_ciOpDecorate %_ Binding 0
467fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
468fd4e5da5Sopenharmony_ci%void = OpTypeVoid
469fd4e5da5Sopenharmony_ci%10 = OpTypeFunction %void
470fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
471fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
472fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
473fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
474fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_v4float Input
475fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
476fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
477fd4e5da5Sopenharmony_ci%In2 = OpVariable %_ptr_Input_float Input
478fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
479fd4e5da5Sopenharmony_ci%_Globals_ = OpTypeStruct %uint %uint
480fd4e5da5Sopenharmony_ci%_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
481fd4e5da5Sopenharmony_ci%_ = OpVariable %_ptr_Uniform__Globals_ Uniform
482fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
483fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
484fd4e5da5Sopenharmony_ci%_ptr_Uniform_uint = OpTypePointer Uniform %uint
485fd4e5da5Sopenharmony_ci%bool = OpTypeBool
486fd4e5da5Sopenharmony_ci%uint_0 = OpConstant %uint 0
487fd4e5da5Sopenharmony_ci%float_1 = OpConstant %float 1
488fd4e5da5Sopenharmony_ci%int_1 = OpConstant %int 1
489fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
490fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
491fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
492fd4e5da5Sopenharmony_ci%27 = OpUndef %v4float
493fd4e5da5Sopenharmony_ci%55 = OpUndef %v4float
494fd4e5da5Sopenharmony_ci)";
495fd4e5da5Sopenharmony_ci
496fd4e5da5Sopenharmony_ci  const std::string before =
497fd4e5da5Sopenharmony_ci      R"(%main = OpFunction %void None %10
498fd4e5da5Sopenharmony_ci%28 = OpLabel
499fd4e5da5Sopenharmony_ci%29 = OpLoad %v4float %In0
500fd4e5da5Sopenharmony_ci%30 = OpLoad %float %In1
501fd4e5da5Sopenharmony_ci%31 = OpLoad %float %In2
502fd4e5da5Sopenharmony_ci%32 = OpFAdd %float %30 %31
503fd4e5da5Sopenharmony_ci%33 = OpCompositeInsert %v4float %32 %29 1
504fd4e5da5Sopenharmony_ci%34 = OpAccessChain %_ptr_Uniform_uint %_ %int_0
505fd4e5da5Sopenharmony_ci%35 = OpLoad %uint %34
506fd4e5da5Sopenharmony_ci%36 = OpINotEqual %bool %35 %uint_0
507fd4e5da5Sopenharmony_ciOpSelectionMerge %37 None
508fd4e5da5Sopenharmony_ciOpBranchConditional %36 %38 %37
509fd4e5da5Sopenharmony_ci%38 = OpLabel
510fd4e5da5Sopenharmony_ci%39 = OpCompositeInsert %v4float %float_1 %33 0
511fd4e5da5Sopenharmony_ciOpBranch %37
512fd4e5da5Sopenharmony_ci%37 = OpLabel
513fd4e5da5Sopenharmony_ci%40 = OpPhi %v4float %33 %28 %39 %38
514fd4e5da5Sopenharmony_ci%41 = OpCompositeExtract %float %40 0
515fd4e5da5Sopenharmony_ci%42 = OpCompositeInsert %v4float %41 %27 0
516fd4e5da5Sopenharmony_ci%43 = OpCompositeExtract %float %40 1
517fd4e5da5Sopenharmony_ci%44 = OpCompositeInsert %v4float %43 %42 1
518fd4e5da5Sopenharmony_ci%45 = OpAccessChain %_ptr_Uniform_uint %_ %int_1
519fd4e5da5Sopenharmony_ci%46 = OpLoad %uint %45
520fd4e5da5Sopenharmony_ci%47 = OpINotEqual %bool %46 %uint_0
521fd4e5da5Sopenharmony_ciOpSelectionMerge %48 None
522fd4e5da5Sopenharmony_ciOpBranchConditional %47 %49 %48
523fd4e5da5Sopenharmony_ci%49 = OpLabel
524fd4e5da5Sopenharmony_ci%50 = OpCompositeInsert %v4float %float_0 %44 0
525fd4e5da5Sopenharmony_ciOpBranch %48
526fd4e5da5Sopenharmony_ci%48 = OpLabel
527fd4e5da5Sopenharmony_ci%51 = OpPhi %v4float %44 %37 %50 %49
528fd4e5da5Sopenharmony_ci%52 = OpCompositeExtract %float %51 0
529fd4e5da5Sopenharmony_ci%53 = OpCompositeExtract %float %51 0
530fd4e5da5Sopenharmony_ci%54 = OpCompositeConstruct %v4float %52 %53 %float_0 %float_1
531fd4e5da5Sopenharmony_ciOpStore %OutColor %54
532fd4e5da5Sopenharmony_ciOpReturn
533fd4e5da5Sopenharmony_ciOpFunctionEnd
534fd4e5da5Sopenharmony_ci)";
535fd4e5da5Sopenharmony_ci
536fd4e5da5Sopenharmony_ci  const std::string after =
537fd4e5da5Sopenharmony_ci      R"(%main = OpFunction %void None %10
538fd4e5da5Sopenharmony_ci%28 = OpLabel
539fd4e5da5Sopenharmony_ci%29 = OpLoad %v4float %In0
540fd4e5da5Sopenharmony_ci%30 = OpLoad %float %In1
541fd4e5da5Sopenharmony_ci%31 = OpLoad %float %In2
542fd4e5da5Sopenharmony_ci%32 = OpFAdd %float %30 %31
543fd4e5da5Sopenharmony_ci%33 = OpCompositeInsert %v4float %32 %29 1
544fd4e5da5Sopenharmony_ci%34 = OpAccessChain %_ptr_Uniform_uint %_ %int_0
545fd4e5da5Sopenharmony_ci%35 = OpLoad %uint %34
546fd4e5da5Sopenharmony_ci%36 = OpINotEqual %bool %35 %uint_0
547fd4e5da5Sopenharmony_ciOpSelectionMerge %37 None
548fd4e5da5Sopenharmony_ciOpBranchConditional %36 %38 %37
549fd4e5da5Sopenharmony_ci%38 = OpLabel
550fd4e5da5Sopenharmony_ci%39 = OpCompositeInsert %v4float %float_1 %55 0
551fd4e5da5Sopenharmony_ciOpBranch %37
552fd4e5da5Sopenharmony_ci%37 = OpLabel
553fd4e5da5Sopenharmony_ci%40 = OpPhi %v4float %29 %28 %39 %38
554fd4e5da5Sopenharmony_ci%41 = OpCompositeExtract %float %40 0
555fd4e5da5Sopenharmony_ci%42 = OpCompositeInsert %v4float %41 %55 0
556fd4e5da5Sopenharmony_ci%43 = OpCompositeExtract %float %40 1
557fd4e5da5Sopenharmony_ci%44 = OpCompositeInsert %v4float %43 %42 1
558fd4e5da5Sopenharmony_ci%45 = OpAccessChain %_ptr_Uniform_uint %_ %int_1
559fd4e5da5Sopenharmony_ci%46 = OpLoad %uint %45
560fd4e5da5Sopenharmony_ci%47 = OpINotEqual %bool %46 %uint_0
561fd4e5da5Sopenharmony_ciOpSelectionMerge %48 None
562fd4e5da5Sopenharmony_ciOpBranchConditional %47 %49 %48
563fd4e5da5Sopenharmony_ci%49 = OpLabel
564fd4e5da5Sopenharmony_ci%50 = OpCompositeInsert %v4float %float_0 %55 0
565fd4e5da5Sopenharmony_ciOpBranch %48
566fd4e5da5Sopenharmony_ci%48 = OpLabel
567fd4e5da5Sopenharmony_ci%51 = OpPhi %v4float %42 %37 %50 %49
568fd4e5da5Sopenharmony_ci%52 = OpCompositeExtract %float %51 0
569fd4e5da5Sopenharmony_ci%53 = OpCompositeExtract %float %51 0
570fd4e5da5Sopenharmony_ci%54 = OpCompositeConstruct %v4float %52 %53 %float_0 %float_1
571fd4e5da5Sopenharmony_ciOpStore %OutColor %54
572fd4e5da5Sopenharmony_ciOpReturn
573fd4e5da5Sopenharmony_ciOpFunctionEnd
574fd4e5da5Sopenharmony_ci)";
575fd4e5da5Sopenharmony_ci
576fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<VectorDCE>(before_predefs + before,
577fd4e5da5Sopenharmony_ci                                   after_predefs + after, true, true);
578fd4e5da5Sopenharmony_ci}
579fd4e5da5Sopenharmony_ci
580fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, InsertObjectLive) {
581fd4e5da5Sopenharmony_ci  // Make sure that the object being inserted in an OpCompositeInsert
582fd4e5da5Sopenharmony_ci  // is not removed when it is uses later on.
583fd4e5da5Sopenharmony_ci  const std::string before =
584fd4e5da5Sopenharmony_ci      R"(OpCapability Shader
585fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
586fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
587fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In0 %In1 %OutColor
588fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
589fd4e5da5Sopenharmony_ciOpSource GLSL 450
590fd4e5da5Sopenharmony_ciOpName %main "main"
591fd4e5da5Sopenharmony_ciOpName %In0 "In0"
592fd4e5da5Sopenharmony_ciOpName %In1 "In1"
593fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
594fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
595fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
596fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
597fd4e5da5Sopenharmony_ci%void = OpTypeVoid
598fd4e5da5Sopenharmony_ci%10 = OpTypeFunction %void
599fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
600fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
601fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
602fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_v4float Input
603fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
604fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
605fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
606fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
607fd4e5da5Sopenharmony_ci%main = OpFunction %void None %10
608fd4e5da5Sopenharmony_ci%28 = OpLabel
609fd4e5da5Sopenharmony_ci%29 = OpLoad %v4float %In0
610fd4e5da5Sopenharmony_ci%30 = OpLoad %float %In1
611fd4e5da5Sopenharmony_ci%33 = OpCompositeInsert %v4float %30 %29 1
612fd4e5da5Sopenharmony_ciOpStore %OutColor %33
613fd4e5da5Sopenharmony_ciOpReturn
614fd4e5da5Sopenharmony_ciOpFunctionEnd
615fd4e5da5Sopenharmony_ci)";
616fd4e5da5Sopenharmony_ci
617fd4e5da5Sopenharmony_ci  SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
618fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<VectorDCE>(before, before, true, true);
619fd4e5da5Sopenharmony_ci}
620fd4e5da5Sopenharmony_ci
621fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, DeadInsertInCycle) {
622fd4e5da5Sopenharmony_ci  // Dead insert in chain with cycle. Demonstrates analysis can handle
623fd4e5da5Sopenharmony_ci  // cycles in chains going through scalars intermediate values.
624fd4e5da5Sopenharmony_ci  //
625fd4e5da5Sopenharmony_ci  // Note: The SPIR-V assembly has had store/load elimination
626fd4e5da5Sopenharmony_ci  // performed to allow the inserts and extracts to directly
627fd4e5da5Sopenharmony_ci  // reference each other.
628fd4e5da5Sopenharmony_ci  //
629fd4e5da5Sopenharmony_ci  // #version 450
630fd4e5da5Sopenharmony_ci  //
631fd4e5da5Sopenharmony_ci  // layout (location=0) in vec4 In0;
632fd4e5da5Sopenharmony_ci  // layout (location=1) in float In1;
633fd4e5da5Sopenharmony_ci  // layout (location=2) in float In2;
634fd4e5da5Sopenharmony_ci  // layout (location=0) out vec4 OutColor;
635fd4e5da5Sopenharmony_ci  //
636fd4e5da5Sopenharmony_ci  // layout(std140, binding = 0 ) uniform _Globals_
637fd4e5da5Sopenharmony_ci  // {
638fd4e5da5Sopenharmony_ci  //     int g_n  ;
639fd4e5da5Sopenharmony_ci  // };
640fd4e5da5Sopenharmony_ci  //
641fd4e5da5Sopenharmony_ci  // void main()
642fd4e5da5Sopenharmony_ci  // {
643fd4e5da5Sopenharmony_ci  //     vec2 v = vec2(0.0, 1.0);
644fd4e5da5Sopenharmony_ci  //     for (int i = 0; i < g_n; i++) {
645fd4e5da5Sopenharmony_ci  //       v.x = v.x + 1;
646fd4e5da5Sopenharmony_ci  //       v.y = v.y * 0.9; // dead
647fd4e5da5Sopenharmony_ci  //     }
648fd4e5da5Sopenharmony_ci  //     OutColor = vec4(v.x);
649fd4e5da5Sopenharmony_ci  // }
650fd4e5da5Sopenharmony_ci
651fd4e5da5Sopenharmony_ci  const std::string assembly =
652fd4e5da5Sopenharmony_ci      R"(
653fd4e5da5Sopenharmony_ci; CHECK: [[init_val:%\w+]] = OpConstantComposite %v2float %float_0 %float_1
654fd4e5da5Sopenharmony_ci; CHECK: [[undef:%\w+]] = OpUndef %v2float
655fd4e5da5Sopenharmony_ci; CHECK: OpFunction
656fd4e5da5Sopenharmony_ci; CHECK: [[entry_lab:%\w+]] = OpLabel
657fd4e5da5Sopenharmony_ci; CHECK: [[loop_header:%\w+]] = OpLabel
658fd4e5da5Sopenharmony_ci; CHECK: OpPhi %v2float [[init_val]] [[entry_lab]] [[x_insert:%\w+]] {{%\w+}}
659fd4e5da5Sopenharmony_ci; CHECK: [[x_insert:%\w+]] = OpCompositeInsert %v2float %43 [[undef]] 0
660fd4e5da5Sopenharmony_ciOpCapability Shader
661fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
662fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
663fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %OutColor %In0 %In1 %In2
664fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
665fd4e5da5Sopenharmony_ciOpSource GLSL 450
666fd4e5da5Sopenharmony_ciOpName %main "main"
667fd4e5da5Sopenharmony_ciOpName %_Globals_ "_Globals_"
668fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 0 "g_n"
669fd4e5da5Sopenharmony_ciOpName %_ ""
670fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
671fd4e5da5Sopenharmony_ciOpName %In0 "In0"
672fd4e5da5Sopenharmony_ciOpName %In1 "In1"
673fd4e5da5Sopenharmony_ciOpName %In2 "In2"
674fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 0 Offset 0
675fd4e5da5Sopenharmony_ciOpDecorate %_Globals_ Block
676fd4e5da5Sopenharmony_ciOpDecorate %_ DescriptorSet 0
677fd4e5da5Sopenharmony_ciOpDecorate %_ Binding 0
678fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
679fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
680fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
681fd4e5da5Sopenharmony_ciOpDecorate %In2 Location 2
682fd4e5da5Sopenharmony_ci%void = OpTypeVoid
683fd4e5da5Sopenharmony_ci%10 = OpTypeFunction %void
684fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
685fd4e5da5Sopenharmony_ci%v2float = OpTypeVector %float 2
686fd4e5da5Sopenharmony_ci%_ptr_Function_v2float = OpTypePointer Function %v2float
687fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
688fd4e5da5Sopenharmony_ci%float_1 = OpConstant %float 1
689fd4e5da5Sopenharmony_ci%16 = OpConstantComposite %v2float %float_0 %float_1
690fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
691fd4e5da5Sopenharmony_ci%_ptr_Function_int = OpTypePointer Function %int
692fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
693fd4e5da5Sopenharmony_ci%_Globals_ = OpTypeStruct %int
694fd4e5da5Sopenharmony_ci%_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
695fd4e5da5Sopenharmony_ci%_ = OpVariable %_ptr_Uniform__Globals_ Uniform
696fd4e5da5Sopenharmony_ci%_ptr_Uniform_int = OpTypePointer Uniform %int
697fd4e5da5Sopenharmony_ci%bool = OpTypeBool
698fd4e5da5Sopenharmony_ci%float_0_75 = OpConstant %float 0.75
699fd4e5da5Sopenharmony_ci%int_1 = OpConstant %int 1
700fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
701fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
702fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
703fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
704fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_v4float Input
705fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
706fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
707fd4e5da5Sopenharmony_ci%In2 = OpVariable %_ptr_Input_float Input
708fd4e5da5Sopenharmony_ci%main = OpFunction %void None %10
709fd4e5da5Sopenharmony_ci%29 = OpLabel
710fd4e5da5Sopenharmony_ciOpBranch %30
711fd4e5da5Sopenharmony_ci%30 = OpLabel
712fd4e5da5Sopenharmony_ci%31 = OpPhi %v2float %16 %29 %32 %33
713fd4e5da5Sopenharmony_ci%34 = OpPhi %int %int_0 %29 %35 %33
714fd4e5da5Sopenharmony_ciOpLoopMerge %36 %33 None
715fd4e5da5Sopenharmony_ciOpBranch %37
716fd4e5da5Sopenharmony_ci%37 = OpLabel
717fd4e5da5Sopenharmony_ci%38 = OpAccessChain %_ptr_Uniform_int %_ %int_0
718fd4e5da5Sopenharmony_ci%39 = OpLoad %int %38
719fd4e5da5Sopenharmony_ci%40 = OpSLessThan %bool %34 %39
720fd4e5da5Sopenharmony_ciOpBranchConditional %40 %41 %36
721fd4e5da5Sopenharmony_ci%41 = OpLabel
722fd4e5da5Sopenharmony_ci%42 = OpCompositeExtract %float %31 0
723fd4e5da5Sopenharmony_ci%43 = OpFAdd %float %42 %float_1
724fd4e5da5Sopenharmony_ci%44 = OpCompositeInsert %v2float %43 %31 0
725fd4e5da5Sopenharmony_ci%45 = OpCompositeExtract %float %44 1
726fd4e5da5Sopenharmony_ci%46 = OpFMul %float %45 %float_0_75
727fd4e5da5Sopenharmony_ci%32 = OpCompositeInsert %v2float %46 %44 1
728fd4e5da5Sopenharmony_ciOpBranch %33
729fd4e5da5Sopenharmony_ci%33 = OpLabel
730fd4e5da5Sopenharmony_ci%35 = OpIAdd %int %34 %int_1
731fd4e5da5Sopenharmony_ciOpBranch %30
732fd4e5da5Sopenharmony_ci%36 = OpLabel
733fd4e5da5Sopenharmony_ci%47 = OpCompositeExtract %float %31 0
734fd4e5da5Sopenharmony_ci%48 = OpCompositeConstruct %v4float %47 %47 %47 %47
735fd4e5da5Sopenharmony_ciOpStore %OutColor %48
736fd4e5da5Sopenharmony_ciOpReturn
737fd4e5da5Sopenharmony_ciOpFunctionEnd
738fd4e5da5Sopenharmony_ci)";
739fd4e5da5Sopenharmony_ci
740fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(assembly, true);
741fd4e5da5Sopenharmony_ci}
742fd4e5da5Sopenharmony_ci
743fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, DeadLoadFeedingCompositeConstruct) {
744fd4e5da5Sopenharmony_ci  // Detach the loads feeding the CompositeConstruct for the unused elements.
745fd4e5da5Sopenharmony_ci  // TODO: Implement the rewrite for CompositeConstruct.
746fd4e5da5Sopenharmony_ci
747fd4e5da5Sopenharmony_ci  const std::string assembly =
748fd4e5da5Sopenharmony_ci      R"(
749fd4e5da5Sopenharmony_ci; CHECK: [[undef:%\w+]] = OpUndef %float
750fd4e5da5Sopenharmony_ci; CHECK: [[ac:%\w+]] = OpAccessChain %_ptr_Input_float %In0 %uint_2
751fd4e5da5Sopenharmony_ci; CHECK: [[load:%\w+]] = OpLoad %float [[ac]]
752fd4e5da5Sopenharmony_ci; CHECK: OpCompositeConstruct %v3float [[load]] [[undef]] [[undef]]
753fd4e5da5Sopenharmony_ciOpCapability Shader
754fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
755fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
756fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In0 %OutColor
757fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
758fd4e5da5Sopenharmony_ciOpSource GLSL 450
759fd4e5da5Sopenharmony_ciOpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
760fd4e5da5Sopenharmony_ciOpSourceExtension "GL_GOOGLE_include_directive"
761fd4e5da5Sopenharmony_ciOpName %main "main"
762fd4e5da5Sopenharmony_ciOpName %In0 "In0"
763fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
764fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
765fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
766fd4e5da5Sopenharmony_ci%void = OpTypeVoid
767fd4e5da5Sopenharmony_ci%6 = OpTypeFunction %void
768fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
769fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
770fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
771fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_v4float Input
772fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
773fd4e5da5Sopenharmony_ci%uint_0 = OpConstant %uint 0
774fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
775fd4e5da5Sopenharmony_ci%uint_1 = OpConstant %uint 1
776fd4e5da5Sopenharmony_ci%uint_2 = OpConstant %uint 2
777fd4e5da5Sopenharmony_ci%v3float = OpTypeVector %float 3
778fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
779fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
780fd4e5da5Sopenharmony_ci%int_20 = OpConstant %int 20
781fd4e5da5Sopenharmony_ci%bool = OpTypeBool
782fd4e5da5Sopenharmony_ci%float_1 = OpConstant %float 1
783fd4e5da5Sopenharmony_ci%int_1 = OpConstant %int 1
784fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
785fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
786fd4e5da5Sopenharmony_ci%23 = OpUndef %v3float
787fd4e5da5Sopenharmony_ci%main = OpFunction %void None %6
788fd4e5da5Sopenharmony_ci%24 = OpLabel
789fd4e5da5Sopenharmony_ci%25 = OpAccessChain %_ptr_Input_float %In0 %uint_0
790fd4e5da5Sopenharmony_ci%26 = OpLoad %float %25
791fd4e5da5Sopenharmony_ci%27 = OpAccessChain %_ptr_Input_float %In0 %uint_1
792fd4e5da5Sopenharmony_ci%28 = OpLoad %float %27
793fd4e5da5Sopenharmony_ci%29 = OpAccessChain %_ptr_Input_float %In0 %uint_2
794fd4e5da5Sopenharmony_ci%30 = OpLoad %float %29
795fd4e5da5Sopenharmony_ci%31 = OpCompositeConstruct %v3float %30 %28 %26
796fd4e5da5Sopenharmony_ciOpBranch %32
797fd4e5da5Sopenharmony_ci%32 = OpLabel
798fd4e5da5Sopenharmony_ci%33 = OpPhi %v3float %31 %24 %34 %35
799fd4e5da5Sopenharmony_ci%36 = OpPhi %int %int_0 %24 %37 %35
800fd4e5da5Sopenharmony_ciOpLoopMerge %38 %35 None
801fd4e5da5Sopenharmony_ciOpBranch %39
802fd4e5da5Sopenharmony_ci%39 = OpLabel
803fd4e5da5Sopenharmony_ci%40 = OpSLessThan %bool %36 %int_20
804fd4e5da5Sopenharmony_ciOpBranchConditional %40 %41 %38
805fd4e5da5Sopenharmony_ci%41 = OpLabel
806fd4e5da5Sopenharmony_ci%42 = OpCompositeExtract %float %33 0
807fd4e5da5Sopenharmony_ci%43 = OpFAdd %float %42 %float_1
808fd4e5da5Sopenharmony_ci%34 = OpCompositeInsert %v3float %43 %33 0
809fd4e5da5Sopenharmony_ciOpBranch %35
810fd4e5da5Sopenharmony_ci%35 = OpLabel
811fd4e5da5Sopenharmony_ci%37 = OpIAdd %int %36 %int_1
812fd4e5da5Sopenharmony_ciOpBranch %32
813fd4e5da5Sopenharmony_ci%38 = OpLabel
814fd4e5da5Sopenharmony_ci%44 = OpCompositeExtract %float %33 0
815fd4e5da5Sopenharmony_ci%45 = OpCompositeConstruct %v4float %44 %44 %44 %44
816fd4e5da5Sopenharmony_ciOpStore %OutColor %45
817fd4e5da5Sopenharmony_ciOpReturn
818fd4e5da5Sopenharmony_ciOpFunctionEnd
819fd4e5da5Sopenharmony_ci)";
820fd4e5da5Sopenharmony_ci
821fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(assembly, true);
822fd4e5da5Sopenharmony_ci}
823fd4e5da5Sopenharmony_ci
824fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, DeadLoadFeedingVectorShuffle) {
825fd4e5da5Sopenharmony_ci  // Detach the loads feeding the CompositeConstruct for the unused elements.
826fd4e5da5Sopenharmony_ci  // TODO: Implement the rewrite for CompositeConstruct.
827fd4e5da5Sopenharmony_ci
828fd4e5da5Sopenharmony_ci  const std::string assembly =
829fd4e5da5Sopenharmony_ci      R"(
830fd4e5da5Sopenharmony_ci; MemPass Type2Undef does not reuse and already existing undef.
831fd4e5da5Sopenharmony_ci; CHECK: {{%\w+}} = OpUndef %v3float
832fd4e5da5Sopenharmony_ci; CHECK: [[undef:%\w+]] = OpUndef %v3float
833fd4e5da5Sopenharmony_ci; CHECK: OpFunction
834fd4e5da5Sopenharmony_ci; CHECK: OpVectorShuffle %v3float {{%\w+}} [[undef]] 0 4 5
835fd4e5da5Sopenharmony_ci               OpCapability Shader
836fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
837fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
838fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %main "main" %In0 %OutColor
839fd4e5da5Sopenharmony_ci               OpExecutionMode %main OriginUpperLeft
840fd4e5da5Sopenharmony_ci               OpSource GLSL 450
841fd4e5da5Sopenharmony_ci               OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
842fd4e5da5Sopenharmony_ci               OpSourceExtension "GL_GOOGLE_include_directive"
843fd4e5da5Sopenharmony_ci               OpName %main "main"
844fd4e5da5Sopenharmony_ci               OpName %In0 "In0"
845fd4e5da5Sopenharmony_ci               OpName %OutColor "OutColor"
846fd4e5da5Sopenharmony_ci               OpDecorate %In0 Location 0
847fd4e5da5Sopenharmony_ci               OpDecorate %OutColor Location 0
848fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
849fd4e5da5Sopenharmony_ci          %6 = OpTypeFunction %void
850fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
851fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
852fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
853fd4e5da5Sopenharmony_ci        %In0 = OpVariable %_ptr_Input_v4float Input
854fd4e5da5Sopenharmony_ci       %uint = OpTypeInt 32 0
855fd4e5da5Sopenharmony_ci     %uint_0 = OpConstant %uint 0
856fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
857fd4e5da5Sopenharmony_ci     %uint_1 = OpConstant %uint 1
858fd4e5da5Sopenharmony_ci     %uint_2 = OpConstant %uint 2
859fd4e5da5Sopenharmony_ci    %v3float = OpTypeVector %float 3
860fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
861fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
862fd4e5da5Sopenharmony_ci     %int_20 = OpConstant %int 20
863fd4e5da5Sopenharmony_ci       %bool = OpTypeBool
864fd4e5da5Sopenharmony_ci    %float_1 = OpConstant %float 1
865fd4e5da5Sopenharmony_ci    %vec_const = OpConstantComposite %v3float %float_1 %float_1 %float_1
866fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
867fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
868fd4e5da5Sopenharmony_ci   %OutColor = OpVariable %_ptr_Output_v4float Output
869fd4e5da5Sopenharmony_ci         %23 = OpUndef %v3float
870fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %6
871fd4e5da5Sopenharmony_ci         %24 = OpLabel
872fd4e5da5Sopenharmony_ci         %25 = OpAccessChain %_ptr_Input_float %In0 %uint_0
873fd4e5da5Sopenharmony_ci         %26 = OpLoad %float %25
874fd4e5da5Sopenharmony_ci         %27 = OpAccessChain %_ptr_Input_float %In0 %uint_1
875fd4e5da5Sopenharmony_ci         %28 = OpLoad %float %27
876fd4e5da5Sopenharmony_ci         %29 = OpAccessChain %_ptr_Input_float %In0 %uint_2
877fd4e5da5Sopenharmony_ci         %30 = OpLoad %float %29
878fd4e5da5Sopenharmony_ci         %31 = OpCompositeConstruct %v3float %30 %28 %26
879fd4e5da5Sopenharmony_ci         %sh = OpVectorShuffle %v3float %vec_const %31 0 4 5
880fd4e5da5Sopenharmony_ci               OpBranch %32
881fd4e5da5Sopenharmony_ci         %32 = OpLabel
882fd4e5da5Sopenharmony_ci         %33 = OpPhi %v3float %sh %24 %34 %35
883fd4e5da5Sopenharmony_ci         %36 = OpPhi %int %int_0 %24 %37 %35
884fd4e5da5Sopenharmony_ci               OpLoopMerge %38 %35 None
885fd4e5da5Sopenharmony_ci               OpBranch %39
886fd4e5da5Sopenharmony_ci         %39 = OpLabel
887fd4e5da5Sopenharmony_ci         %40 = OpSLessThan %bool %36 %int_20
888fd4e5da5Sopenharmony_ci               OpBranchConditional %40 %41 %38
889fd4e5da5Sopenharmony_ci         %41 = OpLabel
890fd4e5da5Sopenharmony_ci         %42 = OpCompositeExtract %float %33 0
891fd4e5da5Sopenharmony_ci         %43 = OpFAdd %float %42 %float_1
892fd4e5da5Sopenharmony_ci         %34 = OpCompositeInsert %v3float %43 %33 0
893fd4e5da5Sopenharmony_ci               OpBranch %35
894fd4e5da5Sopenharmony_ci         %35 = OpLabel
895fd4e5da5Sopenharmony_ci         %37 = OpIAdd %int %36 %int_1
896fd4e5da5Sopenharmony_ci               OpBranch %32
897fd4e5da5Sopenharmony_ci         %38 = OpLabel
898fd4e5da5Sopenharmony_ci         %44 = OpCompositeExtract %float %33 0
899fd4e5da5Sopenharmony_ci         %45 = OpCompositeConstruct %v4float %44 %44 %44 %44
900fd4e5da5Sopenharmony_ci               OpStore %OutColor %45
901fd4e5da5Sopenharmony_ci               OpReturn
902fd4e5da5Sopenharmony_ci               OpFunctionEnd
903fd4e5da5Sopenharmony_ci)";
904fd4e5da5Sopenharmony_ci
905fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(assembly, true);
906fd4e5da5Sopenharmony_ci}
907fd4e5da5Sopenharmony_ci
908fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, DeadInstThroughShuffle) {
909fd4e5da5Sopenharmony_ci  // Dead insert in chain with cycle. Demonstrates analysis can handle
910fd4e5da5Sopenharmony_ci  // cycles in chains.
911fd4e5da5Sopenharmony_ci  //
912fd4e5da5Sopenharmony_ci  // Note: The SPIR-V assembly has had store/load elimination
913fd4e5da5Sopenharmony_ci  // performed to allow the inserts and extracts to directly
914fd4e5da5Sopenharmony_ci  // reference each other.
915fd4e5da5Sopenharmony_ci  //
916fd4e5da5Sopenharmony_ci  // #version 450
917fd4e5da5Sopenharmony_ci  //
918fd4e5da5Sopenharmony_ci  // layout (location=0) out vec4 OutColor;
919fd4e5da5Sopenharmony_ci  //
920fd4e5da5Sopenharmony_ci  // void main()
921fd4e5da5Sopenharmony_ci  // {
922fd4e5da5Sopenharmony_ci  //     vec2 v;
923fd4e5da5Sopenharmony_ci  //     v.x = 0.0;
924fd4e5da5Sopenharmony_ci  //     v.y = 0.1; // dead
925fd4e5da5Sopenharmony_ci  //     for (int i = 0; i < 20; i++) {
926fd4e5da5Sopenharmony_ci  //       v.x = v.x + 1;
927fd4e5da5Sopenharmony_ci  //       v = v * 0.9;
928fd4e5da5Sopenharmony_ci  //     }
929fd4e5da5Sopenharmony_ci  //     OutColor = vec4(v.x);
930fd4e5da5Sopenharmony_ci  // }
931fd4e5da5Sopenharmony_ci
932fd4e5da5Sopenharmony_ci  const std::string assembly =
933fd4e5da5Sopenharmony_ci      R"(
934fd4e5da5Sopenharmony_ci; CHECK: OpFunction
935fd4e5da5Sopenharmony_ci; CHECK-NOT: OpCompositeInsert %v2float {{%\w+}} 1
936fd4e5da5Sopenharmony_ci; CHECK: OpFunctionEnd
937fd4e5da5Sopenharmony_ci               OpCapability Shader
938fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
939fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
940fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %main "main" %OutColor
941fd4e5da5Sopenharmony_ci               OpExecutionMode %main OriginUpperLeft
942fd4e5da5Sopenharmony_ci               OpSource GLSL 450
943fd4e5da5Sopenharmony_ci               OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
944fd4e5da5Sopenharmony_ci               OpSourceExtension "GL_GOOGLE_include_directive"
945fd4e5da5Sopenharmony_ci               OpName %main "main"
946fd4e5da5Sopenharmony_ci               OpName %OutColor "OutColor"
947fd4e5da5Sopenharmony_ci               OpDecorate %OutColor Location 0
948fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
949fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
950fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
951fd4e5da5Sopenharmony_ci    %v2float = OpTypeVector %float 2
952fd4e5da5Sopenharmony_ci    %float_0 = OpConstant %float 0
953fd4e5da5Sopenharmony_ci%float_0_100000001 = OpConstant %float 0.100000001
954fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
955fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
956fd4e5da5Sopenharmony_ci     %int_20 = OpConstant %int 20
957fd4e5da5Sopenharmony_ci       %bool = OpTypeBool
958fd4e5da5Sopenharmony_ci    %float_1 = OpConstant %float 1
959fd4e5da5Sopenharmony_ci%float_0_899999976 = OpConstant %float 0.899999976
960fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
961fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
962fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
963fd4e5da5Sopenharmony_ci   %OutColor = OpVariable %_ptr_Output_v4float Output
964fd4e5da5Sopenharmony_ci         %58 = OpUndef %v2float
965fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
966fd4e5da5Sopenharmony_ci          %5 = OpLabel
967fd4e5da5Sopenharmony_ci         %49 = OpCompositeInsert %v2float %float_0 %58 0
968fd4e5da5Sopenharmony_ci         %51 = OpCompositeInsert %v2float %float_0_100000001 %49 1
969fd4e5da5Sopenharmony_ci               OpBranch %22
970fd4e5da5Sopenharmony_ci         %22 = OpLabel
971fd4e5da5Sopenharmony_ci         %60 = OpPhi %v2float %51 %5 %38 %25
972fd4e5da5Sopenharmony_ci         %59 = OpPhi %int %int_0 %5 %41 %25
973fd4e5da5Sopenharmony_ci               OpLoopMerge %24 %25 None
974fd4e5da5Sopenharmony_ci               OpBranch %26
975fd4e5da5Sopenharmony_ci         %26 = OpLabel
976fd4e5da5Sopenharmony_ci         %30 = OpSLessThan %bool %59 %int_20
977fd4e5da5Sopenharmony_ci               OpBranchConditional %30 %23 %24
978fd4e5da5Sopenharmony_ci         %23 = OpLabel
979fd4e5da5Sopenharmony_ci         %53 = OpCompositeExtract %float %60 0
980fd4e5da5Sopenharmony_ci         %34 = OpFAdd %float %53 %float_1
981fd4e5da5Sopenharmony_ci         %55 = OpCompositeInsert %v2float %34 %60 0
982fd4e5da5Sopenharmony_ci         %38 = OpVectorTimesScalar %v2float %55 %float_0_899999976
983fd4e5da5Sopenharmony_ci               OpBranch %25
984fd4e5da5Sopenharmony_ci         %25 = OpLabel
985fd4e5da5Sopenharmony_ci         %41 = OpIAdd %int %59 %int_1
986fd4e5da5Sopenharmony_ci               OpBranch %22
987fd4e5da5Sopenharmony_ci         %24 = OpLabel
988fd4e5da5Sopenharmony_ci         %57 = OpCompositeExtract %float %60 0
989fd4e5da5Sopenharmony_ci         %47 = OpCompositeConstruct %v4float %57 %57 %57 %57
990fd4e5da5Sopenharmony_ci               OpStore %OutColor %47
991fd4e5da5Sopenharmony_ci               OpReturn
992fd4e5da5Sopenharmony_ci               OpFunctionEnd
993fd4e5da5Sopenharmony_ci)";
994fd4e5da5Sopenharmony_ci
995fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(assembly, true);
996fd4e5da5Sopenharmony_ci}
997fd4e5da5Sopenharmony_ci
998fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, DeadInsertThroughOtherInst) {
999fd4e5da5Sopenharmony_ci  // Dead insert in chain with cycle. Demonstrates analysis can handle
1000fd4e5da5Sopenharmony_ci  // cycles in chains.
1001fd4e5da5Sopenharmony_ci  //
1002fd4e5da5Sopenharmony_ci  // Note: The SPIR-V assembly has had store/load elimination
1003fd4e5da5Sopenharmony_ci  // performed to allow the inserts and extracts to directly
1004fd4e5da5Sopenharmony_ci  // reference each other.
1005fd4e5da5Sopenharmony_ci  //
1006fd4e5da5Sopenharmony_ci  // #version 450
1007fd4e5da5Sopenharmony_ci  //
1008fd4e5da5Sopenharmony_ci  // layout (location=0) out vec4 OutColor;
1009fd4e5da5Sopenharmony_ci  //
1010fd4e5da5Sopenharmony_ci  // void main()
1011fd4e5da5Sopenharmony_ci  // {
1012fd4e5da5Sopenharmony_ci  //     vec2 v;
1013fd4e5da5Sopenharmony_ci  //     v.x = 0.0;
1014fd4e5da5Sopenharmony_ci  //     v.y = 0.1; // dead
1015fd4e5da5Sopenharmony_ci  //     for (int i = 0; i < 20; i++) {
1016fd4e5da5Sopenharmony_ci  //       v.x = v.x + 1;
1017fd4e5da5Sopenharmony_ci  //       v = v * 0.9;
1018fd4e5da5Sopenharmony_ci  //     }
1019fd4e5da5Sopenharmony_ci  //     OutColor = vec4(v.x);
1020fd4e5da5Sopenharmony_ci  // }
1021fd4e5da5Sopenharmony_ci
1022fd4e5da5Sopenharmony_ci  const std::string assembly =
1023fd4e5da5Sopenharmony_ci      R"(
1024fd4e5da5Sopenharmony_ci; CHECK: OpFunction
1025fd4e5da5Sopenharmony_ci; CHECK-NOT: OpCompositeInsert %v2float {{%\w+}} 1
1026fd4e5da5Sopenharmony_ci; CHECK: OpFunctionEnd
1027fd4e5da5Sopenharmony_ci               OpCapability Shader
1028fd4e5da5Sopenharmony_ci          %1 = OpExtInstImport "GLSL.std.450"
1029fd4e5da5Sopenharmony_ci               OpMemoryModel Logical GLSL450
1030fd4e5da5Sopenharmony_ci               OpEntryPoint Fragment %main "main" %OutColor
1031fd4e5da5Sopenharmony_ci               OpExecutionMode %main OriginUpperLeft
1032fd4e5da5Sopenharmony_ci               OpSource GLSL 450
1033fd4e5da5Sopenharmony_ci               OpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
1034fd4e5da5Sopenharmony_ci               OpSourceExtension "GL_GOOGLE_include_directive"
1035fd4e5da5Sopenharmony_ci               OpName %main "main"
1036fd4e5da5Sopenharmony_ci               OpName %OutColor "OutColor"
1037fd4e5da5Sopenharmony_ci               OpDecorate %OutColor Location 0
1038fd4e5da5Sopenharmony_ci       %void = OpTypeVoid
1039fd4e5da5Sopenharmony_ci          %3 = OpTypeFunction %void
1040fd4e5da5Sopenharmony_ci      %float = OpTypeFloat 32
1041fd4e5da5Sopenharmony_ci    %v2float = OpTypeVector %float 2
1042fd4e5da5Sopenharmony_ci    %float_0 = OpConstant %float 0
1043fd4e5da5Sopenharmony_ci%float_0_100000001 = OpConstant %float 0.100000001
1044fd4e5da5Sopenharmony_ci        %int = OpTypeInt 32 1
1045fd4e5da5Sopenharmony_ci      %int_0 = OpConstant %int 0
1046fd4e5da5Sopenharmony_ci     %int_20 = OpConstant %int 20
1047fd4e5da5Sopenharmony_ci       %bool = OpTypeBool
1048fd4e5da5Sopenharmony_ci    %float_1 = OpConstant %float 1
1049fd4e5da5Sopenharmony_ci%float_0_899999976 = OpConstant %float 0.899999976
1050fd4e5da5Sopenharmony_ci      %int_1 = OpConstant %int 1
1051fd4e5da5Sopenharmony_ci    %v4float = OpTypeVector %float 4
1052fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
1053fd4e5da5Sopenharmony_ci   %OutColor = OpVariable %_ptr_Output_v4float Output
1054fd4e5da5Sopenharmony_ci         %58 = OpUndef %v2float
1055fd4e5da5Sopenharmony_ci       %main = OpFunction %void None %3
1056fd4e5da5Sopenharmony_ci          %5 = OpLabel
1057fd4e5da5Sopenharmony_ci         %49 = OpCompositeInsert %v2float %float_0 %58 0
1058fd4e5da5Sopenharmony_ci         %51 = OpCompositeInsert %v2float %float_0_100000001 %49 1
1059fd4e5da5Sopenharmony_ci               OpBranch %22
1060fd4e5da5Sopenharmony_ci         %22 = OpLabel
1061fd4e5da5Sopenharmony_ci         %60 = OpPhi %v2float %51 %5 %38 %25
1062fd4e5da5Sopenharmony_ci         %59 = OpPhi %int %int_0 %5 %41 %25
1063fd4e5da5Sopenharmony_ci               OpLoopMerge %24 %25 None
1064fd4e5da5Sopenharmony_ci               OpBranch %26
1065fd4e5da5Sopenharmony_ci         %26 = OpLabel
1066fd4e5da5Sopenharmony_ci         %30 = OpSLessThan %bool %59 %int_20
1067fd4e5da5Sopenharmony_ci               OpBranchConditional %30 %23 %24
1068fd4e5da5Sopenharmony_ci         %23 = OpLabel
1069fd4e5da5Sopenharmony_ci         %53 = OpCompositeExtract %float %60 0
1070fd4e5da5Sopenharmony_ci         %34 = OpFAdd %float %53 %float_1
1071fd4e5da5Sopenharmony_ci         %55 = OpCompositeInsert %v2float %34 %60 0
1072fd4e5da5Sopenharmony_ci         %38 = OpVectorTimesScalar %v2float %55 %float_0_899999976
1073fd4e5da5Sopenharmony_ci               OpBranch %25
1074fd4e5da5Sopenharmony_ci         %25 = OpLabel
1075fd4e5da5Sopenharmony_ci         %41 = OpIAdd %int %59 %int_1
1076fd4e5da5Sopenharmony_ci               OpBranch %22
1077fd4e5da5Sopenharmony_ci         %24 = OpLabel
1078fd4e5da5Sopenharmony_ci         %57 = OpCompositeExtract %float %60 0
1079fd4e5da5Sopenharmony_ci         %47 = OpCompositeConstruct %v4float %57 %57 %57 %57
1080fd4e5da5Sopenharmony_ci               OpStore %OutColor %47
1081fd4e5da5Sopenharmony_ci               OpReturn
1082fd4e5da5Sopenharmony_ci               OpFunctionEnd
1083fd4e5da5Sopenharmony_ci)";
1084fd4e5da5Sopenharmony_ci
1085fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(assembly, true);
1086fd4e5da5Sopenharmony_ci}
1087fd4e5da5Sopenharmony_ci
1088fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, VectorIntoCompositeConstruct) {
1089fd4e5da5Sopenharmony_ci  const std::string text = R"(OpCapability Linkage
1090fd4e5da5Sopenharmony_ciOpCapability Shader
1091fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
1092fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %1 "EntryPoint_Main" %2 %3
1093fd4e5da5Sopenharmony_ciOpExecutionMode %1 OriginUpperLeft
1094fd4e5da5Sopenharmony_ciOpDecorate %2 Location 0
1095fd4e5da5Sopenharmony_ciOpDecorate %_struct_4 Block
1096fd4e5da5Sopenharmony_ciOpDecorate %3 Location 0
1097fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
1098fd4e5da5Sopenharmony_ci%v2float = OpTypeVector %float 2
1099fd4e5da5Sopenharmony_ci%_ptr_Function_v2float = OpTypePointer Function %v2float
1100fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
1101fd4e5da5Sopenharmony_ci%_ptr_Function_v4float = OpTypePointer Function %v4float
1102fd4e5da5Sopenharmony_ci%mat4v4float = OpTypeMatrix %v4float 4
1103fd4e5da5Sopenharmony_ci%_ptr_Function_mat4v4float = OpTypePointer Function %mat4v4float
1104fd4e5da5Sopenharmony_ci%v3float = OpTypeVector %float 3
1105fd4e5da5Sopenharmony_ci%_ptr_Function_v3float = OpTypePointer Function %v3float
1106fd4e5da5Sopenharmony_ci%_struct_14 = OpTypeStruct %v2float %mat4v4float %v3float %v2float %v4float
1107fd4e5da5Sopenharmony_ci%_ptr_Function__struct_14 = OpTypePointer Function %_struct_14
1108fd4e5da5Sopenharmony_ci%void = OpTypeVoid
1109fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
1110fd4e5da5Sopenharmony_ci%int_2 = OpConstant %int 2
1111fd4e5da5Sopenharmony_ci%int_1 = OpConstant %int 1
1112fd4e5da5Sopenharmony_ci%int_4 = OpConstant %int 4
1113fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
1114fd4e5da5Sopenharmony_ci%int_3 = OpConstant %int 3
1115fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
1116fd4e5da5Sopenharmony_ci%float_1 = OpConstant %float 1
1117fd4e5da5Sopenharmony_ci%_ptr_Input_v2float = OpTypePointer Input %v2float
1118fd4e5da5Sopenharmony_ci%2 = OpVariable %_ptr_Input_v2float Input
1119fd4e5da5Sopenharmony_ci%_ptr_Output_v2float = OpTypePointer Output %v2float
1120fd4e5da5Sopenharmony_ci%_struct_4 = OpTypeStruct %v2float
1121fd4e5da5Sopenharmony_ci%_ptr_Output__struct_4 = OpTypePointer Output %_struct_4
1122fd4e5da5Sopenharmony_ci%3 = OpVariable %_ptr_Output__struct_4 Output
1123fd4e5da5Sopenharmony_ci%28 = OpTypeFunction %void
1124fd4e5da5Sopenharmony_ci%29 = OpConstantComposite %v2float %float_0 %float_0
1125fd4e5da5Sopenharmony_ci%30 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
1126fd4e5da5Sopenharmony_ci%31 = OpConstantComposite %mat4v4float %30 %30 %30 %30
1127fd4e5da5Sopenharmony_ci%32 = OpConstantComposite %v3float %float_0 %float_0 %float_0
1128fd4e5da5Sopenharmony_ci%1 = OpFunction %void None %28
1129fd4e5da5Sopenharmony_ci%33 = OpLabel
1130fd4e5da5Sopenharmony_ci%34 = OpVariable %_ptr_Function_v4float Function
1131fd4e5da5Sopenharmony_ci%35 = OpVariable %_ptr_Function__struct_14 Function
1132fd4e5da5Sopenharmony_ci%36 = OpAccessChain %_ptr_Function_v2float %35 %int_0
1133fd4e5da5Sopenharmony_ciOpStore %36 %29
1134fd4e5da5Sopenharmony_ci%37 = OpAccessChain %_ptr_Function_mat4v4float %35 %int_1
1135fd4e5da5Sopenharmony_ciOpStore %37 %31
1136fd4e5da5Sopenharmony_ci%38 = OpAccessChain %_ptr_Function_v3float %35 %int_2
1137fd4e5da5Sopenharmony_ciOpStore %38 %32
1138fd4e5da5Sopenharmony_ci%39 = OpAccessChain %_ptr_Function_v2float %35 %int_3
1139fd4e5da5Sopenharmony_ciOpStore %39 %29
1140fd4e5da5Sopenharmony_ci%40 = OpAccessChain %_ptr_Function_v4float %35 %int_4
1141fd4e5da5Sopenharmony_ciOpStore %40 %30
1142fd4e5da5Sopenharmony_ci%41 = OpLoad %v2float %2
1143fd4e5da5Sopenharmony_ciOpStore %36 %41
1144fd4e5da5Sopenharmony_ci%42 = OpLoad %v3float %38
1145fd4e5da5Sopenharmony_ci%43 = OpCompositeConstruct %v4float %42 %float_1
1146fd4e5da5Sopenharmony_ci%44 = OpLoad %mat4v4float %37
1147fd4e5da5Sopenharmony_ci%45 = OpVectorTimesMatrix %v4float %43 %44
1148fd4e5da5Sopenharmony_ciOpStore %34 %45
1149fd4e5da5Sopenharmony_ciOpCopyMemory %40 %34
1150fd4e5da5Sopenharmony_ciOpCopyMemory %36 %39
1151fd4e5da5Sopenharmony_ci%46 = OpAccessChain %_ptr_Output_v2float %3 %int_0
1152fd4e5da5Sopenharmony_ci%47 = OpLoad %v2float %36
1153fd4e5da5Sopenharmony_ciOpStore %46 %47
1154fd4e5da5Sopenharmony_ciOpReturn
1155fd4e5da5Sopenharmony_ciOpFunctionEnd
1156fd4e5da5Sopenharmony_ci)";
1157fd4e5da5Sopenharmony_ci
1158fd4e5da5Sopenharmony_ci  SinglePassRunAndCheck<VectorDCE>(text, text, true, true);
1159fd4e5da5Sopenharmony_ci}
1160fd4e5da5Sopenharmony_ci
1161fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, NotAffectedByDebugValue) {
1162fd4e5da5Sopenharmony_ci  // It tests that an OpenCL.DebugInfo.100 DebugValue instruction does
1163fd4e5da5Sopenharmony_ci  // not change the vector DCE pass result. If the composite used for
1164fd4e5da5Sopenharmony_ci  // the value of DebugValue is killed, the DebugValue must be killed as well.
1165fd4e5da5Sopenharmony_ci  const std::string text = R"(
1166fd4e5da5Sopenharmony_ciOpCapability Shader
1167fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
1168fd4e5da5Sopenharmony_ci%ext = OpExtInstImport "OpenCL.DebugInfo.100"
1169fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
1170fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In2 %In0 %In1 %OutColor
1171fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
1172fd4e5da5Sopenharmony_ciOpSource GLSL 450
1173fd4e5da5Sopenharmony_ci%file_name = OpString "test"
1174fd4e5da5Sopenharmony_ci%float_name = OpString "float"
1175fd4e5da5Sopenharmony_ci%main_name = OpString "main"
1176fd4e5da5Sopenharmony_ci%f_name = OpString "f"
1177fd4e5da5Sopenharmony_ciOpName %main "main"
1178fd4e5da5Sopenharmony_ciOpName %In2 "In2"
1179fd4e5da5Sopenharmony_ciOpName %In0 "In0"
1180fd4e5da5Sopenharmony_ciOpName %In1 "In1"
1181fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
1182fd4e5da5Sopenharmony_ciOpName %_Globals_ "_Globals_"
1183fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 0 "g_b"
1184fd4e5da5Sopenharmony_ciOpMemberName %_Globals_ 1 "g_n"
1185fd4e5da5Sopenharmony_ciOpName %_ ""
1186fd4e5da5Sopenharmony_ciOpDecorate %In2 Location 2
1187fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
1188fd4e5da5Sopenharmony_ciOpDecorate %In1 Location 1
1189fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
1190fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 0 Offset 0
1191fd4e5da5Sopenharmony_ciOpMemberDecorate %_Globals_ 1 Offset 4
1192fd4e5da5Sopenharmony_ciOpDecorate %_Globals_ Block
1193fd4e5da5Sopenharmony_ciOpDecorate %_ DescriptorSet 0
1194fd4e5da5Sopenharmony_ciOpDecorate %_ Binding 0
1195fd4e5da5Sopenharmony_ci%void = OpTypeVoid
1196fd4e5da5Sopenharmony_ci%11 = OpTypeFunction %void
1197fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
1198fd4e5da5Sopenharmony_ci%v2float = OpTypeVector %float 2
1199fd4e5da5Sopenharmony_ci%_ptr_Function_v2float = OpTypePointer Function %v2float
1200fd4e5da5Sopenharmony_ci%_ptr_Input_v2float = OpTypePointer Input %v2float
1201fd4e5da5Sopenharmony_ci%In2 = OpVariable %_ptr_Input_v2float Input
1202fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
1203fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_float Input
1204fd4e5da5Sopenharmony_ci%In1 = OpVariable %_ptr_Input_float Input
1205fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
1206fd4e5da5Sopenharmony_ci%uint_32 = OpConstant %uint 32
1207fd4e5da5Sopenharmony_ci%_ptr_Function_float = OpTypePointer Function %float
1208fd4e5da5Sopenharmony_ci%float_0 = OpConstant %float 0
1209fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
1210fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
1211fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
1212fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
1213fd4e5da5Sopenharmony_ci%_Globals_ = OpTypeStruct %uint %int
1214fd4e5da5Sopenharmony_ci%_ptr_Uniform__Globals_ = OpTypePointer Uniform %_Globals_
1215fd4e5da5Sopenharmony_ci%_ = OpVariable %_ptr_Uniform__Globals_ Uniform
1216fd4e5da5Sopenharmony_ci%null_expr = OpExtInst %void %ext DebugExpression
1217fd4e5da5Sopenharmony_ci%src = OpExtInst %void %ext DebugSource %file_name
1218fd4e5da5Sopenharmony_ci%cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
1219fd4e5da5Sopenharmony_ci%dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
1220fd4e5da5Sopenharmony_ci%main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_tf
1221fd4e5da5Sopenharmony_ci%dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
1222fd4e5da5Sopenharmony_ci%dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
1223fd4e5da5Sopenharmony_ci%main = OpFunction %void None %11
1224fd4e5da5Sopenharmony_ci%25 = OpLabel
1225fd4e5da5Sopenharmony_ci%s = OpExtInst %void %ext DebugScope %dbg_main
1226fd4e5da5Sopenharmony_ci
1227fd4e5da5Sopenharmony_ci; CHECK: [[in2:%\w+]] = OpLoad %v2float %In2
1228fd4e5da5Sopenharmony_ci%26 = OpLoad %v2float %In2
1229fd4e5da5Sopenharmony_ci%27 = OpLoad %float %In0
1230fd4e5da5Sopenharmony_ci%28 = OpLoad %float %In1
1231fd4e5da5Sopenharmony_ci%29 = OpFAdd %float %27 %28
1232fd4e5da5Sopenharmony_ci
1233fd4e5da5Sopenharmony_ci; CHECK:      OpCompositeInsert %v2float {{%\w+}} [[in2]] 0
1234fd4e5da5Sopenharmony_ci; CHECK-NEXT: OpCompositeInsert %v2float {{%\w+}} [[in2]] 0
1235fd4e5da5Sopenharmony_ci; CHECK-NOT:  DebugValue
1236fd4e5da5Sopenharmony_ci%35 = OpCompositeInsert %v2float %29 %26 0
1237fd4e5da5Sopenharmony_ci%value = OpExtInst %void %ext DebugValue %dbg_f %35 %null_expr
1238fd4e5da5Sopenharmony_ci%37 = OpCompositeInsert %v2float %float_0 %35 0
1239fd4e5da5Sopenharmony_ci%33 = OpVectorShuffle %v4float %37 %37 0 1 0 1
1240fd4e5da5Sopenharmony_ciOpStore %OutColor %33
1241fd4e5da5Sopenharmony_ciOpReturn
1242fd4e5da5Sopenharmony_ciOpFunctionEnd
1243fd4e5da5Sopenharmony_ci)";
1244fd4e5da5Sopenharmony_ci
1245fd4e5da5Sopenharmony_ci  SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
1246fd4e5da5Sopenharmony_ci                        SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
1247fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(text, true);
1248fd4e5da5Sopenharmony_ci}
1249fd4e5da5Sopenharmony_ci
1250fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, RemoveDebugValueUsesKilledInstr) {
1251fd4e5da5Sopenharmony_ci  // It tests that the vector DCE pass removes the OpenCL.DebugInfo.100
1252fd4e5da5Sopenharmony_ci  // DebugValue instruction using a killed instruction.
1253fd4e5da5Sopenharmony_ci  const std::string text = R"(
1254fd4e5da5Sopenharmony_ciOpCapability Shader
1255fd4e5da5Sopenharmony_ci%1 = OpExtInstImport "GLSL.std.450"
1256fd4e5da5Sopenharmony_ci%ext = OpExtInstImport "OpenCL.DebugInfo.100"
1257fd4e5da5Sopenharmony_ciOpMemoryModel Logical GLSL450
1258fd4e5da5Sopenharmony_ciOpEntryPoint Fragment %main "main" %In0 %OutColor
1259fd4e5da5Sopenharmony_ciOpExecutionMode %main OriginUpperLeft
1260fd4e5da5Sopenharmony_ciOpSource GLSL 450
1261fd4e5da5Sopenharmony_ci%file_name = OpString "test"
1262fd4e5da5Sopenharmony_ci%float_name = OpString "float"
1263fd4e5da5Sopenharmony_ci%main_name = OpString "main"
1264fd4e5da5Sopenharmony_ci%f_name = OpString "f"
1265fd4e5da5Sopenharmony_ciOpSourceExtension "GL_GOOGLE_cpp_style_line_directive"
1266fd4e5da5Sopenharmony_ciOpSourceExtension "GL_GOOGLE_include_directive"
1267fd4e5da5Sopenharmony_ciOpName %main "main"
1268fd4e5da5Sopenharmony_ciOpName %In0 "In0"
1269fd4e5da5Sopenharmony_ciOpName %OutColor "OutColor"
1270fd4e5da5Sopenharmony_ciOpDecorate %In0 Location 0
1271fd4e5da5Sopenharmony_ciOpDecorate %OutColor Location 0
1272fd4e5da5Sopenharmony_ci%void = OpTypeVoid
1273fd4e5da5Sopenharmony_ci%6 = OpTypeFunction %void
1274fd4e5da5Sopenharmony_ci%float = OpTypeFloat 32
1275fd4e5da5Sopenharmony_ci%v4float = OpTypeVector %float 4
1276fd4e5da5Sopenharmony_ci%_ptr_Input_v4float = OpTypePointer Input %v4float
1277fd4e5da5Sopenharmony_ci%In0 = OpVariable %_ptr_Input_v4float Input
1278fd4e5da5Sopenharmony_ci%uint = OpTypeInt 32 0
1279fd4e5da5Sopenharmony_ci%uint_0 = OpConstant %uint 0
1280fd4e5da5Sopenharmony_ci%uint_32 = OpConstant %uint 32
1281fd4e5da5Sopenharmony_ci%_ptr_Input_float = OpTypePointer Input %float
1282fd4e5da5Sopenharmony_ci%uint_1 = OpConstant %uint 1
1283fd4e5da5Sopenharmony_ci%uint_2 = OpConstant %uint 2
1284fd4e5da5Sopenharmony_ci%v3float = OpTypeVector %float 3
1285fd4e5da5Sopenharmony_ci%int = OpTypeInt 32 1
1286fd4e5da5Sopenharmony_ci%int_0 = OpConstant %int 0
1287fd4e5da5Sopenharmony_ci%int_20 = OpConstant %int 20
1288fd4e5da5Sopenharmony_ci%bool = OpTypeBool
1289fd4e5da5Sopenharmony_ci%float_1 = OpConstant %float 1
1290fd4e5da5Sopenharmony_ci%int_1 = OpConstant %int 1
1291fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
1292fd4e5da5Sopenharmony_ci%OutColor = OpVariable %_ptr_Output_v4float Output
1293fd4e5da5Sopenharmony_ci%23 = OpUndef %v3float
1294fd4e5da5Sopenharmony_ci%null_expr = OpExtInst %void %ext DebugExpression
1295fd4e5da5Sopenharmony_ci%src = OpExtInst %void %ext DebugSource %file_name
1296fd4e5da5Sopenharmony_ci%cu = OpExtInst %void %ext DebugCompilationUnit 1 4 %src HLSL
1297fd4e5da5Sopenharmony_ci%dbg_tf = OpExtInst %void %ext DebugTypeBasic %float_name %uint_32 Float
1298fd4e5da5Sopenharmony_ci%main_ty = OpExtInst %void %ext DebugTypeFunction FlagIsProtected|FlagIsPrivate %dbg_tf
1299fd4e5da5Sopenharmony_ci%dbg_main = OpExtInst %void %ext DebugFunction %main_name %main_ty %src 0 0 %cu %main_name FlagIsProtected|FlagIsPrivate 10 %main
1300fd4e5da5Sopenharmony_ci%dbg_f = OpExtInst %void %ext DebugLocalVariable %f_name %dbg_tf %src 0 0 %dbg_main FlagIsLocal
1301fd4e5da5Sopenharmony_ci%main = OpFunction %void None %6
1302fd4e5da5Sopenharmony_ci%24 = OpLabel
1303fd4e5da5Sopenharmony_ci%s0 = OpExtInst %void %ext DebugScope %dbg_main
1304fd4e5da5Sopenharmony_ci%25 = OpAccessChain %_ptr_Input_float %In0 %uint_0
1305fd4e5da5Sopenharmony_ci%26 = OpLoad %float %25
1306fd4e5da5Sopenharmony_ci%27 = OpAccessChain %_ptr_Input_float %In0 %uint_1
1307fd4e5da5Sopenharmony_ci%28 = OpLoad %float %27
1308fd4e5da5Sopenharmony_ci
1309fd4e5da5Sopenharmony_ci; CHECK:     [[undef:%\w+]] = OpUndef %float
1310fd4e5da5Sopenharmony_ci; CHECK-NOT: DebugValue
1311fd4e5da5Sopenharmony_ci%value = OpExtInst %void %ext DebugValue %dbg_f %28 %null_expr
1312fd4e5da5Sopenharmony_ci%29 = OpAccessChain %_ptr_Input_float %In0 %uint_2
1313fd4e5da5Sopenharmony_ci%30 = OpLoad %float %29
1314fd4e5da5Sopenharmony_ci
1315fd4e5da5Sopenharmony_ci; CHECK:      [[composite:%\w+]] = OpCompositeConstruct %v3float {{%\w+}} [[undef]] [[undef]]
1316fd4e5da5Sopenharmony_ci; CHECK-NEXT: DebugValue {{%\w+}} [[composite]]
1317fd4e5da5Sopenharmony_ci%31 = OpCompositeConstruct %v3float %30 %28 %26
1318fd4e5da5Sopenharmony_ci%value_live = OpExtInst %void %ext DebugValue %dbg_f %31 %null_expr
1319fd4e5da5Sopenharmony_ciOpBranch %32
1320fd4e5da5Sopenharmony_ci%32 = OpLabel
1321fd4e5da5Sopenharmony_ci%s1 = OpExtInst %void %ext DebugScope %dbg_main
1322fd4e5da5Sopenharmony_ci%33 = OpPhi %v3float %31 %24 %34 %35
1323fd4e5da5Sopenharmony_ci%36 = OpPhi %int %int_0 %24 %37 %35
1324fd4e5da5Sopenharmony_ciOpLoopMerge %38 %35 None
1325fd4e5da5Sopenharmony_ciOpBranch %39
1326fd4e5da5Sopenharmony_ci%39 = OpLabel
1327fd4e5da5Sopenharmony_ci%s2 = OpExtInst %void %ext DebugScope %dbg_main
1328fd4e5da5Sopenharmony_ci%40 = OpSLessThan %bool %36 %int_20
1329fd4e5da5Sopenharmony_ciOpBranchConditional %40 %41 %38
1330fd4e5da5Sopenharmony_ci%41 = OpLabel
1331fd4e5da5Sopenharmony_ci%s3 = OpExtInst %void %ext DebugScope %dbg_main
1332fd4e5da5Sopenharmony_ci%42 = OpCompositeExtract %float %33 0
1333fd4e5da5Sopenharmony_ci%43 = OpFAdd %float %42 %float_1
1334fd4e5da5Sopenharmony_ci%34 = OpCompositeInsert %v3float %43 %33 0
1335fd4e5da5Sopenharmony_ciOpBranch %35
1336fd4e5da5Sopenharmony_ci%35 = OpLabel
1337fd4e5da5Sopenharmony_ci%s4 = OpExtInst %void %ext DebugScope %dbg_main
1338fd4e5da5Sopenharmony_ci%37 = OpIAdd %int %36 %int_1
1339fd4e5da5Sopenharmony_ciOpBranch %32
1340fd4e5da5Sopenharmony_ci%38 = OpLabel
1341fd4e5da5Sopenharmony_ci%s5 = OpExtInst %void %ext DebugScope %dbg_main
1342fd4e5da5Sopenharmony_ci%44 = OpCompositeExtract %float %33 0
1343fd4e5da5Sopenharmony_ci%45 = OpCompositeConstruct %v4float %44 %44 %44 %44
1344fd4e5da5Sopenharmony_ciOpStore %OutColor %45
1345fd4e5da5Sopenharmony_ciOpReturn
1346fd4e5da5Sopenharmony_ciOpFunctionEnd
1347fd4e5da5Sopenharmony_ci)";
1348fd4e5da5Sopenharmony_ci
1349fd4e5da5Sopenharmony_ci  SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
1350fd4e5da5Sopenharmony_ci                        SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
1351fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(text, true);
1352fd4e5da5Sopenharmony_ci}
1353fd4e5da5Sopenharmony_ci
1354fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, OutOfBoundsExtract) {
1355fd4e5da5Sopenharmony_ci  // It tests that the vector DCE pass is able to handle an extract with an
1356fd4e5da5Sopenharmony_ci  // index that is out of bounds.
1357fd4e5da5Sopenharmony_ci  const std::string text = R"(
1358fd4e5da5Sopenharmony_ci; CHECK: [[undef:%\w+]] = OpUndef %v4float
1359fd4e5da5Sopenharmony_ci; CHECK: OpCompositeExtract %float [[undef]] 8
1360fd4e5da5Sopenharmony_ci                     OpCapability Shader
1361fd4e5da5Sopenharmony_ci                     OpMemoryModel Logical GLSL450
1362fd4e5da5Sopenharmony_ci                     OpEntryPoint Fragment %main "main" %OutColor
1363fd4e5da5Sopenharmony_ci                     OpExecutionMode %main OriginUpperLeft
1364fd4e5da5Sopenharmony_ci                     OpDecorate %OutColor Location 0
1365fd4e5da5Sopenharmony_ci             %void = OpTypeVoid
1366fd4e5da5Sopenharmony_ci               %10 = OpTypeFunction %void
1367fd4e5da5Sopenharmony_ci            %float = OpTypeFloat 32
1368fd4e5da5Sopenharmony_ci          %v4float = OpTypeVector %float 4
1369fd4e5da5Sopenharmony_ci%_ptr_Output_float = OpTypePointer Output %float
1370fd4e5da5Sopenharmony_ci         %OutColor = OpVariable %_ptr_Output_float Output
1371fd4e5da5Sopenharmony_ci             %null = OpConstantNull %v4float
1372fd4e5da5Sopenharmony_ci          %float_1 = OpConstant %float 1
1373fd4e5da5Sopenharmony_ci             %main = OpFunction %void None %10
1374fd4e5da5Sopenharmony_ci               %28 = OpLabel
1375fd4e5da5Sopenharmony_ci               %33 = OpCompositeInsert %v4float %float_1 %null 1
1376fd4e5da5Sopenharmony_ci          %extract = OpCompositeExtract %float %33 8
1377fd4e5da5Sopenharmony_ci                     OpStore %OutColor %extract
1378fd4e5da5Sopenharmony_ci                     OpReturn
1379fd4e5da5Sopenharmony_ci                     OpFunctionEnd
1380fd4e5da5Sopenharmony_ci)";
1381fd4e5da5Sopenharmony_ci
1382fd4e5da5Sopenharmony_ci  SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
1383fd4e5da5Sopenharmony_ci                        SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
1384fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(text, false);
1385fd4e5da5Sopenharmony_ci}
1386fd4e5da5Sopenharmony_ci
1387fd4e5da5Sopenharmony_ciTEST_F(VectorDCETest, OutOfBoundsShuffle) {
1388fd4e5da5Sopenharmony_ci  // It tests that the vector DCE pass is able to handle a shuffle with an
1389fd4e5da5Sopenharmony_ci  // index that is out of bounds.
1390fd4e5da5Sopenharmony_ci  const std::string text = R"(
1391fd4e5da5Sopenharmony_ci; CHECK: [[undef:%\w+]] = OpUndef %v4float
1392fd4e5da5Sopenharmony_ci; CHECK: OpVectorShuffle %v4float [[undef]] [[undef]] 9 10 11 12
1393fd4e5da5Sopenharmony_ci                     OpCapability Shader
1394fd4e5da5Sopenharmony_ci                     OpMemoryModel Logical GLSL450
1395fd4e5da5Sopenharmony_ci                     OpEntryPoint Fragment %main "main" %OutColor
1396fd4e5da5Sopenharmony_ci                     OpExecutionMode %main OriginUpperLeft
1397fd4e5da5Sopenharmony_ci                     OpDecorate %OutColor Location 0
1398fd4e5da5Sopenharmony_ci             %void = OpTypeVoid
1399fd4e5da5Sopenharmony_ci               %10 = OpTypeFunction %void
1400fd4e5da5Sopenharmony_ci            %float = OpTypeFloat 32
1401fd4e5da5Sopenharmony_ci          %v4float = OpTypeVector %float 4
1402fd4e5da5Sopenharmony_ci%_ptr_Output_v4float = OpTypePointer Output %v4float
1403fd4e5da5Sopenharmony_ci         %OutColor = OpVariable %_ptr_Output_v4float Output
1404fd4e5da5Sopenharmony_ci             %null = OpConstantNull %v4float
1405fd4e5da5Sopenharmony_ci          %float_1 = OpConstant %float 1
1406fd4e5da5Sopenharmony_ci             %main = OpFunction %void None %10
1407fd4e5da5Sopenharmony_ci               %28 = OpLabel
1408fd4e5da5Sopenharmony_ci               %33 = OpCompositeInsert %v4float %float_1 %null 1
1409fd4e5da5Sopenharmony_ci          %shuffle = OpVectorShuffle %v4float %33 %33 9 10 11 12
1410fd4e5da5Sopenharmony_ci                     OpStore %OutColor %shuffle
1411fd4e5da5Sopenharmony_ci                     OpReturn
1412fd4e5da5Sopenharmony_ci                     OpFunctionEnd
1413fd4e5da5Sopenharmony_ci)";
1414fd4e5da5Sopenharmony_ci
1415fd4e5da5Sopenharmony_ci  SetDisassembleOptions(SPV_BINARY_TO_TEXT_OPTION_NO_HEADER |
1416fd4e5da5Sopenharmony_ci                        SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES);
1417fd4e5da5Sopenharmony_ci  SinglePassRunAndMatch<VectorDCE>(text, false);
1418fd4e5da5Sopenharmony_ci}
1419fd4e5da5Sopenharmony_ci
1420fd4e5da5Sopenharmony_ci}  // namespace
1421fd4e5da5Sopenharmony_ci}  // namespace opt
1422fd4e5da5Sopenharmony_ci}  // namespace spvtools
1423