authorgravatar for arbrk1@users.noreply.github.comarbrk1 <arbrk1@users.noreply.github.com> 2024-01-02 13:08:26+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-01-02 12:08:26+02:00
log024540de15160bc9ced67d0b10c3f4eddf4a7846
tree5ccdd699de1ed46450b08c780494ac57727aa831
parent25a556107cbd10cbddf530e633f70c688498d4e3
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Liveness: fix branch operands becoming aliased


2 files changed, 34 insertions(+), 1 deletions(-)

src/Liveness.zig+1-1
...@@ -599,7 +599,7 @@ pub fn categorizeOperand(...@@ -599,7 +599,7 @@ pub fn categorizeOperand(
599599
600 .br => {600 .br => {
601 const br = air_datas[@intFromEnum(inst)].br;601 const br = air_datas[@intFromEnum(inst)].br;
602 if (br.operand == operand_ref) return matchOperandSmallIndex(l, inst, 0, .noret);602 if (br.operand == operand_ref) return matchOperandSmallIndex(l, operand, 0, .noret);
603 return .noret;603 return .noret;
604 },604 },
605 .assembly => {605 .assembly => {
test/behavior/if.zig+33
...@@ -167,3 +167,36 @@ test "if-@as-if chain" {...@@ -167,3 +167,36 @@ test "if-@as-if chain" {
167167
168 try expect(num_frames == 4);168 try expect(num_frames == 4);
169}169}
170
171fn returnTrue() bool {
172 return true;
173}
174
175test "if value shouldn't be load-elided if used later (structs)" {
176 const Foo = struct { x: i32 };
177
178 var a = Foo{ .x = 1 };
179 var b = Foo{ .x = 1 };
180
181 const c = if (@call(.never_inline, returnTrue, .{})) a else b;
182 // The second variable is superfluous with the current
183 // state of codegen optimizations, but in future
184 // "if (smthg) a else a" may be optimized simply into "a".
185
186 a.x = 2;
187 b.x = 3;
188
189 try std.testing.expectEqual(c.x, 1);
190}
191
192test "if value shouldn't be load-elided if used later (optionals)" {
193 var a: ?i32 = 1;
194 var b: ?i32 = 1;
195
196 const c = if (@call(.never_inline, returnTrue, .{})) a else b;
197
198 a = 2;
199 b = 3;
200
201 try std.testing.expectEqual(c, 1);
202}