lang: Lower logical conditions to branches

a6cd53fc76199749afa9cede5c105d6cc9253812087b797d9513e1683072546d
A condition such as `a and b` or `not (a or b)` was lowered to a boolean
value through merge blocks and then branched on with a test against
zero. Loop and `if` conditions paid for the materialized value on every
evaluation.

Conditions now branch on each operand: `and` and `or` branch to a block
for the right operand or straight to a target, and `not` swaps the
targets. Comparisons in the operands still fuse with their branches.
Evaluation order and short-circuiting are unchanged.

On the prog.* fixtures this runs 8.3% fewer instructions in total, and
compilation becomes slightly cheaper because the IL is smaller.

Assisted-by: Claude:opus-5.5
Alexis Sellier committed ago 1 parent 7848fea8
lib/std/lang/lower.rad +21 -3
3265 3265
    try emit(self, il::Instr::Unreachable);
3266 3266
3267 3267
    try switchToAndSeal(self, passBlock);
3268 3268
}
3269 3269
3270 -
/// Emit a conditional branch. Uses fused compare-and-branch for simple scalar
3271 -
/// comparisons, falls back to separate comparison plus branch otherwise.
3270 +
/// Emit short-circuit branches and fuse scalar comparisons with their branches.
3271 +
/// Other conditions produce a boolean value before the branch.
3272 3272
fn emitCondBranch(
3273 3273
    self: &mut Lowerer,
3274 3274
    cond: *ast::Node,
3275 3275
    thenBlock: BlockId,
3276 3276
    elseBlock: BlockId
3277 3277
) throws (LowerError) {
3278 -
    // Try fused compare-and-branch for simple scalar comparisons.
3278 +
    if let case ast::NodeValue::UnOp(unop) = cond.value; unop.op == ast::UnaryOp::Not {
3279 +
        try emitCondBranch(self, unop.value, elseBlock, thenBlock);
3280 +
        return;
3281 +
    }
3279 3282
    if let case ast::NodeValue::BinOp(binop) = cond.value {
3283 +
        if binop.op == ast::BinaryOp::And {
3284 +
            let rightBlock = try createBlock(self, "and#rhs");
3285 +
            try emitCondBranch(self, binop.left, rightBlock, elseBlock);
3286 +
            try switchToAndSeal(self, rightBlock);
3287 +
            try emitCondBranch(self, binop.right, thenBlock, elseBlock);
3288 +
            return;
3289 +
        }
3290 +
        if binop.op == ast::BinaryOp::Or {
3291 +
            let rightBlock = try createBlock(self, "or#rhs");
3292 +
            try emitCondBranch(self, binop.left, thenBlock, rightBlock);
3293 +
            try switchToAndSeal(self, rightBlock);
3294 +
            try emitCondBranch(self, binop.right, thenBlock, elseBlock);
3295 +
            return;
3296 +
        }
3297 +
        // Try fused compare-and-branch for simple scalar comparisons.
3280 3298
        let leftTy = try typeOf(self, binop.left);
3281 3299
        let rightTy = try typeOf(self, binop.right);
3282 3300
        if not isAggregateType(&self.resolver, leftTy) and not isAggregateType(
3283 3301
            &self.resolver, rightTy
3284 3302
        )
test/tests/array.repeat.ril +53 -89
8 8
  @entry0
9 9
    reserve %0 $L0;
10 10
    store w32 42 %0 0;
11 11
    store w32 42 %0 4;
12 12
    store w32 42 %0 8;
13 -
    elem %3 %0 0 $L1 3;
14 -
    sload w32 %4 %3 0;
15 -
    br.eq w32 %4 42 @and#then4 @and#else5;
13 +
    elem %2 %0 0 $L1 3;
14 +
    sload w32 %3 %2 0;
15 +
    br.eq w32 %3 42 @and#rhs4 @and#else2;
16 16
  @and#then1
17 -
    elem %9 %0 2 $L1 3;
18 -
    sload w32 %10 %9 0;
19 -
    eq w32 %11 %10 42;
20 -
    jmp @and#end3(%11);
17 +
    elem %6 %0 2 $L1 3;
18 +
    sload w32 %7 %6 0;
19 +
    eq w32 %8 %7 42;
20 +
    jmp @and#end3(%8);
21 21
  @and#else2
22 22
    jmp @and#end3(0);
23 23
  @and#end3(w8 %1)
24 24
    ret %1;
25 -
  @and#then4
26 -
    elem %5 %0 1 $L1 3;
27 -
    sload w32 %6 %5 0;
28 -
    eq w32 %7 %6 42;
29 -
    jmp @and#end6(%7);
30 -
  @and#else5
31 -
    jmp @and#end6(0);
32 -
  @and#end6(w8 %2)
33 -
    br.ne w32 %2 0 @and#then1 @and#else2;
25 +
  @and#rhs4
26 +
    elem %4 %0 1 $L1 3;
27 +
    sload w32 %5 %4 0;
28 +
    br.eq w32 %5 42 @and#then1 @and#else2;
34 29
}
35 30
36 31
fn w8 $testZero() {
37 32
  @entry0
38 33
    reserve %0 $L2;
39 34
    store w32 0 %0 0;
40 35
    store w32 0 %0 4;
41 36
    store w32 0 %0 8;
42 37
    store w32 0 %0 12;
43 38
    store w32 0 %0 16;
44 -
    elem %5 %0 0 $L1 5;
45 -
    sload w32 %6 %5 0;
46 -
    br.eq w32 %6 0 @and#then10 @and#else11;
39 +
    elem %2 %0 0 $L1 5;
40 +
    sload w32 %3 %2 0;
41 +
    br.eq w32 %3 0 @and#rhs6 @and#else2;
47 42
  @and#then1
48 -
    elem %19 %0 4 $L1 5;
49 -
    sload w32 %20 %19 0;
50 -
    eq w32 %21 %20 0;
51 -
    jmp @and#end3(%21);
43 +
    elem %10 %0 4 $L1 5;
44 +
    sload w32 %11 %10 0;
45 +
    eq w32 %12 %11 0;
46 +
    jmp @and#end3(%12);
52 47
  @and#else2
53 48
    jmp @and#end3(0);
54 49
  @and#end3(w8 %1)
55 50
    ret %1;
56 -
  @and#then4
57 -
    elem %15 %0 3 $L1 5;
58 -
    sload w32 %16 %15 0;
59 -
    eq w32 %17 %16 0;
60 -
    jmp @and#end6(%17);
61 -
  @and#else5
62 -
    jmp @and#end6(0);
63 -
  @and#end6(w8 %2)
64 -
    br.ne w32 %2 0 @and#then1 @and#else2;
65 -
  @and#then7
66 -
    elem %11 %0 2 $L1 5;
67 -
    sload w32 %12 %11 0;
68 -
    eq w32 %13 %12 0;
69 -
    jmp @and#end9(%13);
70 -
  @and#else8
71 -
    jmp @and#end9(0);
72 -
  @and#end9(w8 %3)
73 -
    br.ne w32 %3 0 @and#then4 @and#else5;
74 -
  @and#then10
75 -
    elem %7 %0 1 $L1 5;
76 -
    sload w32 %8 %7 0;
77 -
    eq w32 %9 %8 0;
78 -
    jmp @and#end12(%9);
79 -
  @and#else11
80 -
    jmp @and#end12(0);
81 -
  @and#end12(w8 %4)
82 -
    br.ne w32 %4 0 @and#then7 @and#else8;
51 +
  @and#rhs4
52 +
    elem %8 %0 3 $L1 5;
53 +
    sload w32 %9 %8 0;
54 +
    br.eq w32 %9 0 @and#then1 @and#else2;
55 +
  @and#rhs5
56 +
    elem %6 %0 2 $L1 5;
57 +
    sload w32 %7 %6 0;
58 +
    br.eq w32 %7 0 @and#rhs4 @and#else2;
59 +
  @and#rhs6
60 +
    elem %4 %0 1 $L1 5;
61 +
    sload w32 %5 %4 0;
62 +
    br.eq w32 %5 0 @and#rhs5 @and#else2;
83 63
}
84 64
85 65
fn w8 $testBoolean() {
86 66
  @entry0
87 67
    reserve %0 $L3;
88 68
    store w8 1 %0 0;
89 69
    store w8 1 %0 1;
90 70
    store w8 1 %0 2;
91 71
    store w8 1 %0 3;
92 -
    elem %4 %0 0 $L4 4;
93 -
    load w8 %5 %4 0;
94 -
    br.ne w32 %5 0 @and#then7 @and#else8;
72 +
    elem %2 %0 0 $L4 4;
73 +
    load w8 %3 %2 0;
74 +
    br.ne w32 %3 0 @and#rhs5 @and#else2;
95 75
  @and#then1
96 -
    elem %12 %0 3 $L4 4;
97 -
    load w8 %13 %12 0;
98 -
    jmp @and#end3(%13);
76 +
    elem %8 %0 3 $L4 4;
77 +
    load w8 %9 %8 0;
78 +
    jmp @and#end3(%9);
99 79
  @and#else2
100 80
    jmp @and#end3(0);
101 81
  @and#end3(w8 %1)
102 82
    ret %1;
103 -
  @and#then4
104 -
    elem %9 %0 2 $L4 4;
105 -
    load w8 %10 %9 0;
106 -
    jmp @and#end6(%10);
107 -
  @and#else5
108 -
    jmp @and#end6(0);
109 -
  @and#end6(w8 %2)
110 -
    br.ne w32 %2 0 @and#then1 @and#else2;
111 -
  @and#then7
112 -
    elem %6 %0 1 $L4 4;
83 +
  @and#rhs4
84 +
    elem %6 %0 2 $L4 4;
113 85
    load w8 %7 %6 0;
114 -
    jmp @and#end9(%7);
115 -
  @and#else8
116 -
    jmp @and#end9(0);
117 -
  @and#end9(w8 %3)
118 -
    br.ne w32 %3 0 @and#then4 @and#else5;
86 +
    br.ne w32 %7 0 @and#then1 @and#else2;
87 +
  @and#rhs5
88 +
    elem %4 %0 1 $L4 4;
89 +
    load w8 %5 %4 0;
90 +
    br.ne w32 %5 0 @and#rhs4 @and#else2;
119 91
}
120 92
121 93
fn w8 $testSingle() {
122 94
  @entry0
123 95
    reserve %0 $L1;
128 100
    ret %3;
129 101
}
130 102
131 103
fn w8 $main() {
132 104
  @entry0
133 -
    call w8 %3 $testBasic();
134 -
    br.ne w32 %3 0 @and#then7 @and#else8;
105 +
    call w8 %1 $testBasic();
106 +
    br.ne w32 %1 0 @and#rhs5 @and#else2;
135 107
  @and#then1
136 -
    call w8 %6 $testSingle();
137 -
    jmp @and#end3(%6);
108 +
    call w8 %4 $testSingle();
109 +
    jmp @and#end3(%4);
138 110
  @and#else2
139 111
    jmp @and#end3(0);
140 112
  @and#end3(w8 %0)
141 113
    ret %0;
142 -
  @and#then4
143 -
    call w8 %5 $testBoolean();
144 -
    jmp @and#end6(%5);
145 -
  @and#else5
146 -
    jmp @and#end6(0);
147 -
  @and#end6(w8 %1)
148 -
    br.ne w32 %1 0 @and#then1 @and#else2;
149 -
  @and#then7
150 -
    call w8 %4 $testZero();
151 -
    jmp @and#end9(%4);
152 -
  @and#else8
153 -
    jmp @and#end9(0);
154 -
  @and#end9(w8 %2)
155 -
    br.ne w32 %2 0 @and#then4 @and#else5;
114 +
  @and#rhs4
115 +
    call w8 %3 $testBoolean();
116 +
    br.ne w32 %3 0 @and#then1 @and#else2;
117 +
  @and#rhs5
118 +
    call w8 %2 $testZero();
119 +
    br.ne w32 %2 0 @and#rhs4 @and#else2;
156 120
}
test/tests/cast.same.size.ril +3 -5
33 33
    br.ne w32 %1 0 @assert.ok16 @assert.fail15;
34 34
  @assert.fail15
35 35
    unreachable;
36 36
  @assert.ok16
37 37
    ne w8 %2 -1 255;
38 -
    eq w8 %3 %2 0;
39 -
    br.ne w32 %3 0 @assert.ok18 @assert.fail17;
38 +
    br.ne w32 %2 0 @assert.fail17 @assert.ok18;
40 39
  @assert.fail17
41 40
    unreachable;
42 41
  @assert.ok18
43 -
    ne w8 %4 255 -1;
44 -
    eq w8 %5 %4 0;
45 -
    br.ne w32 %5 0 @assert.ok20 @assert.fail19;
42 +
    ne w8 %3 255 -1;
43 +
    br.ne w32 %3 0 @assert.fail19 @assert.ok20;
46 44
  @assert.fail19
47 45
    unreachable;
48 46
  @assert.ok20
49 47
    ret 0;
50 48
}
test/tests/cmp.narrow.live.ril +12 -16
33 33
  @merge4(w64 %11)
34 34
    br.ult w32 %2 %3 @merge6(%11) @then5;
35 35
  @then5
36 36
    add w64 %12 %11 4;
37 37
    jmp @merge6(%12);
38 -
  @merge6(w64 %19)
38 +
  @merge6(w64 %18)
39 39
    eq w32 %15 %2 %3;
40 40
    ult w32 %16 %2 %3;
41 -
    br.ne w32 %15 0 @or#then9 @or#else10;
41 +
    br.ne w32 %15 0 @then7 @or#rhs9;
42 42
  @then7
43 -
    add w64 %20 %19 8;
44 -
    jmp @merge8(%20);
45 -
  @merge8(w64 %26)
46 -
    sub w64 %27 %26 %26;
47 -
    add w64 %28 %0 %27;
48 -
    sub w64 %34 %1 %1;
49 -
    add w64 %35 %28 %34;
50 -
    ret %35;
51 -
  @or#then9
52 -
    jmp @or#end11(1);
53 -
  @or#else10
54 -
    jmp @or#end11(%16);
55 -
  @or#end11(w8 %17)
56 -
    br.ne w32 %17 0 @then7 @merge8(%19);
43 +
    add w64 %19 %18 8;
44 +
    jmp @merge8(%19);
45 +
  @merge8(w64 %25)
46 +
    sub w64 %26 %25 %25;
47 +
    add w64 %27 %0 %26;
48 +
    sub w64 %33 %1 %1;
49 +
    add w64 %34 %27 %33;
50 +
    ret %34;
51 +
  @or#rhs9
52 +
    br.ne w32 %16 0 @then7 @merge8(%18);
57 53
}
58 54
59 55
fn w64 $divide(w64 %0, w64 %1) {
60 56
  @entry0
61 57
    zext w32 %2 %0;
test/tests/cond.shortcircuit.eval.rad added +40 -0
1 +
//! returns: 0
2 +
//! Logical conditions evaluate operands in order and skip unneeded ones.
3 +
4 +
/// Count an evaluated operand and return its value.
5 +
fn probe(count: &mut u32, value: bool) -> bool {
6 +
    set *count += 1;
7 +
    return value;
8 +
}
9 +
10 +
/// Branch on `a and not (b or c)`.
11 +
fn select(a: i32, b: i32, c: i32) -> i32 {
12 +
    if a > 0 and not (b == 0 or c < 0) {
13 +
        return 1;
14 +
    }
15 +
    return 0;
16 +
}
17 +
18 +
@default fn main() -> i32 {
19 +
    assert select(1, 1, 0) == 1;
20 +
    assert select(0, 1, 0) == 0;
21 +
    assert select(1, 0, 0) == 0;
22 +
    assert select(1, 1, -1) == 0;
23 +
24 +
    let mut count: u32 = 0;
25 +
    if probe(&mut count, false) and probe(&mut count, true) {
26 +
        return 1;
27 +
    }
28 +
    assert count == 1;
29 +
    if not (probe(&mut count, true) or probe(&mut count, true)) {
30 +
        return 2;
31 +
    }
32 +
    assert count == 2;
33 +
    if probe(&mut count, true) and probe(&mut count, false) {
34 +
        return 3;
35 +
    }
36 +
    assert count == 4;
37 +
    while probe(&mut count, true) and count < 6 {}
38 +
    assert count == 6;
39 +
    return 0;
40 +
}
test/tests/cond.shortcircuit.rad added +18 -0
1 +
//! Logical conditions branch on each operand without materializing booleans.
2 +
3 +
/// Branch on `a and not (b or c)`.
4 +
fn select(a: i32, b: i32, c: i32) -> i32 {
5 +
    if a > 0 and not (b == 0 or c < 0) {
6 +
        return 1;
7 +
    }
8 +
    return 0;
9 +
}
10 +
11 +
/// Count loop iterations under a conjunction.
12 +
fn bounded(limit: u32) -> u32 {
13 +
    let mut i: u32 = 0;
14 +
    while i < limit and i < 10 {
15 +
        set i += 1;
16 +
    }
17 +
    return i;
18 +
}
test/tests/cond.shortcircuit.ril added +26 -0
1 +
fn w32 $select(w32 %0, w32 %1, w32 %2) {
2 +
  @entry0
3 +
    br.slt w32 0 %0 @and#rhs3 @merge2;
4 +
  @then1
5 +
    ret 1;
6 +
  @merge2
7 +
    ret 0;
8 +
  @and#rhs3
9 +
    br.eq w32 %1 0 @merge2 @or#rhs4;
10 +
  @or#rhs4
11 +
    br.slt w32 %2 0 @merge2 @then1;
12 +
}
13 +
14 +
fn w32 $bounded(w32 %0) {
15 +
  @entry0
16 +
    jmp @while1(0, %0);
17 +
  @while1(w32 %1, w32 %2)
18 +
    br.ult w32 %1 %2 @and#rhs4 @merge3;
19 +
  @body2
20 +
    add w32 %3 %1 1;
21 +
    jmp @while1(%3, %2);
22 +
  @merge3
23 +
    ret %1;
24 +
  @and#rhs4
25 +
    br.ult w32 %1 10 @body2 @merge3;
26 +
}
test/tests/loop.while.nested.shortcircuit.ril +12 -17
1 1
fn w32 $test(w32 %0) {
2 2
  @entry0
3 3
    jmp @while1(0, %0, 0);
4 -
  @while1(w32 %1, w32 %2, w32 %10)
4 +
  @while1(w32 %1, w32 %2, w32 %7)
5 5
    br.slt w32 %1 %2 @body2 @merge3;
6 6
  @body2
7 -
    jmp @while4(%1, %10, %1, %2);
7 +
    jmp @while4(%1);
8 8
  @merge3
9 -
    ret %10;
10 -
  @while4(w32 %4, w32 %9, w32 %12, w32 %16)
11 -
    br.slt w32 0 %4 @and#then7 @and#else8;
9 +
    ret %7;
10 +
  @while4(w32 %3)
11 +
    br.slt w32 0 %3 @and#rhs7 @merge6;
12 12
  @body5
13 -
    sub w32 %7 %4 1;
14 -
    jmp @while4(%7, %9, %12, %16);
13 +
    sub w32 %4 %3 1;
14 +
    jmp @while4(%4);
15 15
  @merge6
16 -
    add w32 %13 %9 %12;
17 -
    add w32 %14 %12 1;
18 -
    jmp @while1(%14, %16, %13);
19 -
  @and#then7
20 -
    slt w32 %5 %4 100;
21 -
    jmp @and#end9(%5);
22 -
  @and#else8
23 -
    jmp @and#end9(0);
24 -
  @and#end9(w8 %3)
25 -
    br.ne w32 %3 0 @body5 @merge6;
16 +
    add w32 %10 %7 %1;
17 +
    add w32 %11 %1 1;
18 +
    jmp @while1(%11, %2, %10);
19 +
  @and#rhs7
20 +
    br.slt w32 %3 100 @body5 @merge6;
26 21
}
test/tests/optional.pointer.wide.ril +12 -14
39 39
fn w32 $main() {
40 40
  @entry0
41 41
    copy %0 $BYTES;
42 42
    elem %1 %0 0 $L1 4;
43 43
    call w8 %2 $present(%1);
44 -
    eq w8 %3 %2 0;
45 -
    br.ne w32 %3 0 @then1 @merge2;
44 +
    br.ne w32 %2 0 @merge2 @then1;
46 45
  @then1
47 46
    ret 1;
48 47
  @merge2
49 -
    call w8 %4 $present(0);
50 -
    br.ne w32 %4 0 @then3 @merge4;
48 +
    call w8 %3 $present(0);
49 +
    br.ne w32 %3 0 @then3 @merge4;
51 50
  @then3
52 51
    ret 2;
53 52
  @merge4
54 -
    copy %5 $BYTES;
55 -
    reserve %6 $L2;
56 -
    slice.new %6 %5 $L1 4 0 4;
57 -
    call w8 %7 $presentSlice(%6);
58 -
    eq w8 %8 %7 0;
59 -
    br.ne w32 %8 0 @then5 @merge6;
53 +
    copy %4 $BYTES;
54 +
    reserve %5 $L2;
55 +
    slice.new %5 %4 $L1 4 0 4;
56 +
    call w8 %6 $presentSlice(%5);
57 +
    br.ne w32 %6 0 @merge6 @then5;
60 58
  @then5
61 59
    ret 3;
62 60
  @merge6
63 -
    reserve %9 $L2;
64 -
    slice.new %9 0 $L1 0 0 0;
65 -
    call w8 %10 $presentSlice(%9);
66 -
    br.ne w32 %10 0 @then7 @merge8;
61 +
    reserve %7 $L2;
62 +
    slice.new %7 0 $L1 0 0 0;
63 +
    call w8 %8 $presentSlice(%7);
64 +
    br.ne w32 %8 0 @then7 @merge8;
67 65
  @then7
68 66
    ret 4;
69 67
  @merge8
70 68
    ret 0;
71 69
}
test/tests/trait.supertrait.ril +23 -28
28 28
}
29 29
30 30
fn w32 $"Socket::read"(w64 %0, ptr ref const $L0 %1) {
31 31
  @entry0
32 32
    jmp @while1(0, %1, %0);
33 -
  @while1(w32 %3, ptr ref const $L0 %4, w64 %6)
34 -
    load w32 %5 %4 8;
35 -
    br.ult w32 %3 %5 @and#then4 @and#else5;
33 +
  @while1(w32 %2, ptr ref const $L0 %3, w64 %5)
34 +
    load w32 %4 %3 8;
35 +
    br.ult w32 %2 %4 @and#rhs4 @merge3;
36 36
  @body2
37 -
    sload w32 %11 %6 32;
38 -
    elem %12 %6 %11 $L1 32;
39 -
    load w8 %13 %12 0;
40 -
    load w32 %16 %4 8;
41 -
    br.ult w32 %3 %16 @guard#pass7 @guard#trap8;
37 +
    sload w32 %8 %5 32;
38 +
    elem %9 %5 %8 $L1 32;
39 +
    load w8 %10 %9 0;
40 +
    load w32 %11 %3 8;
41 +
    br.ult w32 %2 %11 @guard#pass5 @guard#trap6;
42 42
  @merge3
43 -
    ret %3;
44 -
  @and#then4
45 -
    sload w32 %7 %6 32;
46 -
    sload w32 %8 %6 36;
47 -
    slt w32 %9 %7 %8;
48 -
    jmp @and#end6(%9);
49 -
  @and#else5
50 -
    jmp @and#end6(0);
51 -
  @and#end6(w8 %2)
52 -
    br.ne w32 %2 0 @body2 @merge3;
53 -
  @guard#pass7
54 -
    load w64 %17 %4 0;
55 -
    add w64 %18 %17 %3;
56 -
    store w8 %13 %18 0;
57 -
    sload w32 %19 %6 32;
58 -
    add w32 %20 %19 1;
59 -
    store w32 %20 %6 32;
60 -
    add w32 %21 %3 1;
61 -
    jmp @while1(%21, %4, %6);
62 -
  @guard#trap8
43 +
    ret %2;
44 +
  @and#rhs4
45 +
    sload w32 %6 %5 32;
46 +
    sload w32 %7 %5 36;
47 +
    br.slt w32 %6 %7 @body2 @merge3;
48 +
  @guard#pass5
49 +
    load w64 %12 %3 0;
50 +
    add w64 %13 %12 %2;
51 +
    store w8 %10 %13 0;
52 +
    sload w32 %14 %5 32;
53 +
    add w32 %15 %14 1;
54 +
    store w32 %15 %5 32;
55 +
    add w32 %16 %2 1;
56 +
    jmp @while1(%16, %3, %5);
57 +
  @guard#trap6
63 58
    ebreak;
64 59
    unreachable;
65 60
}
66 61
67 62
fn w32 $"Socket::write"(w64 %0, ptr ref const $L3 %1) {