Liveness: fix branch operands becoming aliased

This commit is contained in:
arbrk1 2024-01-02 13:08:26 +03:00 committed by GitHub
parent 25a556107c
commit 024540de15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 1 deletions

View File

@ -599,7 +599,7 @@ pub fn categorizeOperand(
.br => {
const br = air_datas[@intFromEnum(inst)].br;
if (br.operand == operand_ref) return matchOperandSmallIndex(l, inst, 0, .noret);
if (br.operand == operand_ref) return matchOperandSmallIndex(l, operand, 0, .noret);
return .noret;
},
.assembly => {

View File

@ -167,3 +167,36 @@ test "if-@as-if chain" {
try expect(num_frames == 4);
}
fn returnTrue() bool {
return true;
}
test "if value shouldn't be load-elided if used later (structs)" {
const Foo = struct { x: i32 };
var a = Foo{ .x = 1 };
var b = Foo{ .x = 1 };
const c = if (@call(.never_inline, returnTrue, .{})) a else b;
// The second variable is superfluous with the current
// state of codegen optimizations, but in future
// "if (smthg) a else a" may be optimized simply into "a".
a.x = 2;
b.x = 3;
try std.testing.expectEqual(c.x, 1);
}
test "if value shouldn't be load-elided if used later (optionals)" {
var a: ?i32 = 1;
var b: ?i32 = 1;
const c = if (@call(.never_inline, returnTrue, .{})) a else b;
a = 2;
b = 3;
try std.testing.expectEqual(c, 1);
}