compiler/
lib/
examples/
std/
arch/
char/
collections/
lang/
alloc/
ast/
gen/
bitset/
regalloc/
assign.rad
11.1 KiB
liveness.rad
8.9 KiB
spill.rad
11.5 KiB
bitset.rad
6.1 KiB
data.rad
8.6 KiB
labels.rad
2.4 KiB
regalloc.rad
2.3 KiB
types.rad
594 B
il/
module/
parser/
resolver/
scanner/
alloc.rad
5.3 KiB
ast.rad
23.6 KiB
gen.rad
513 B
il.rad
15.5 KiB
lower.rad
277.7 KiB
module.rad
13.5 KiB
package.rad
1.3 KiB
parser.rad
79.6 KiB
resolver.rad
332.7 KiB
scanner.rad
17.6 KiB
sexpr.rad
6.4 KiB
strings.rad
2.2 KiB
types.rad
286 B
sys/
arch.rad
68 B
char.rad
855 B
collections.rad
39 B
fmt.rad
8.3 KiB
intrinsics.rad
467 B
io.rad
1.7 KiB
lang.rad
276 B
mem.rad
2.3 KiB
sys.rad
179 B
testing.rad
2.4 KiB
tests.rad
15.7 KiB
vec.rad
3.2 KiB
std.rad
281 B
scripts/
seed/
sublime/
test/
vim/
.gitignore
336 B
.gitsigners
112 B
CONTRIBUTING
2.1 KiB
LICENSE
1.1 KiB
Makefile
4.1 KiB
README
2.5 KiB
STYLE
2.6 KiB
std.lib
1.2 KiB
std.lib.test
347 B
lib/std/lang/gen/regalloc/liveness.rad
raw
| 1 | //! Backward dataflow liveness analysis. |
| 2 | //! |
| 3 | //! Computes live-in and live-out sets for each basic block. A register is |
| 4 | //! "live" at a program point if its value may be used on some path from that |
| 5 | //! point to program exit. |
| 6 | //! |
| 7 | //! Uses monotone iterative dataflow analysis. All sets start empty and grow: |
| 8 | //! |
| 9 | //! REPEAT UNTIL changed == false |
| 10 | //! changed = false |
| 11 | //! FOR EACH BLOCK b IN POST-ORDER DO |
| 12 | //! liveOut[b] |= UNION(liveIn[succ] FOR EACH SUCCESSOR OF b) |
| 13 | //! newIn = uses[b] | (liveOut[b] - defs[b]) |
| 14 | //! IF newIn <> liveIn[b] THEN |
| 15 | //! changed = true |
| 16 | //! liveIn[b] = newIn |
| 17 | //! |
| 18 | //! Only live-in changes require another pass, since predecessors read live-in. |
| 19 | //! |
| 20 | //! Usage: |
| 21 | //! |
| 22 | //! let live = liveness::analyze(func, ...); |
| 23 | //! if not liveness::hasLaterUse(&live, func, blockIdx, instrIdx, reg) { |
| 24 | //! // `reg` dies at this instruction. |
| 25 | //! } |
| 26 | |
| 27 | use std::mem; |
| 28 | use std::lang::il; |
| 29 | use std::lang::alloc; |
| 30 | use std::lang::gen::bitset; |
| 31 | |
| 32 | /// Maximum number of SSA registers supported. |
| 33 | export constant MAX_SSA_REGS: u32 = 8192; |
| 34 | |
| 35 | /// Liveness information for a function. |
| 36 | export record LiveInfo: Copy { |
| 37 | /// Per-block live-in sets (indexed by block index). |
| 38 | liveIn: *unsafe mut [bitset::Bitset], |
| 39 | /// Per-block live-out sets (indexed by block index). |
| 40 | liveOut: *unsafe mut [bitset::Bitset], |
| 41 | /// Per-block defs sets (registers defined in block). |
| 42 | defs: *unsafe mut [bitset::Bitset], |
| 43 | /// Per-block uses sets (registers used before defined in block). |
| 44 | uses: *unsafe mut [bitset::Bitset], |
| 45 | /// Number of blocks. |
| 46 | blockCount: u32, |
| 47 | /// Maximum register number used. |
| 48 | maxReg: u32, |
| 49 | } |
| 50 | |
| 51 | /// Context for collecting defs and uses during block analysis. |
| 52 | record DefsUses: Copy { |
| 53 | defs: *unsafe bitset::Bitset, |
| 54 | uses: *unsafe mut bitset::Bitset, |
| 55 | } |
| 56 | |
| 57 | /// Context for searching for a specific register in an instruction. |
| 58 | record FindCtx: Copy { |
| 59 | target: u32, |
| 60 | found: bool, |
| 61 | } |
| 62 | |
| 63 | /// Compute liveness by growing live sets until no live-in set changes. |
| 64 | export unsafe fn analyze(func: *unsafe il::Fn, arena: &mut alloc::Arena) -> LiveInfo throws (alloc::AllocError) { |
| 65 | let blockCount = func.blocks.len; |
| 66 | if blockCount == 0 { |
| 67 | return LiveInfo { |
| 68 | liveIn: &mut [], |
| 69 | liveOut: &mut [], |
| 70 | defs: &mut [], |
| 71 | uses: &mut [], |
| 72 | blockCount: 0, |
| 73 | maxReg: 0, |
| 74 | }; |
| 75 | } |
| 76 | |
| 77 | // Find max register number. |
| 78 | let mut maxReg: u32 = 0; |
| 79 | for p in func.params { |
| 80 | set maxReg = maxRegNum(p.value.n, maxReg); |
| 81 | } |
| 82 | for b in 0..blockCount { |
| 83 | let block = &func.blocks[b]; |
| 84 | for p in block.params { |
| 85 | set maxReg = maxRegNum(p.value.n, maxReg); |
| 86 | } |
| 87 | for i in 0..block.instrs.len { |
| 88 | il::forEachReg(block.instrs[i], maxRegCallback, &mut maxReg as &mut opaque); |
| 89 | if let dst = il::instrDst(block.instrs[i]) { |
| 90 | set maxReg = maxRegNum(dst.n, maxReg); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | if maxReg > MAX_SSA_REGS { |
| 95 | throw alloc::AllocError::OutOfMemory; |
| 96 | } |
| 97 | // Allocate per-block bitsets. |
| 98 | let liveIn = try alloc::allocRawSlice(arena, @sizeOf(bitset::Bitset), @alignOf(bitset::Bitset), blockCount) as *unsafe mut [bitset::Bitset]; |
| 99 | let liveOut = try alloc::allocRawSlice(arena, @sizeOf(bitset::Bitset), @alignOf(bitset::Bitset), blockCount) as *unsafe mut [bitset::Bitset]; |
| 100 | let defs = try alloc::allocRawSlice(arena, @sizeOf(bitset::Bitset), @alignOf(bitset::Bitset), blockCount) as *unsafe mut [bitset::Bitset]; |
| 101 | let uses = try alloc::allocRawSlice(arena, @sizeOf(bitset::Bitset), @alignOf(bitset::Bitset), blockCount) as *unsafe mut [bitset::Bitset]; |
| 102 | |
| 103 | for b in 0..blockCount { |
| 104 | set liveIn[b] = try bitset::allocate(arena, maxReg); |
| 105 | set liveOut[b] = try bitset::allocate(arena, maxReg); |
| 106 | set defs[b] = try bitset::allocate(arena, maxReg); |
| 107 | set uses[b] = try bitset::allocate(arena, maxReg); |
| 108 | } |
| 109 | |
| 110 | // Compute local defs and uses for each block. |
| 111 | for b in 0..blockCount { |
| 112 | computeLocalDefsUses(&func.blocks[b], &mut defs[b], &mut uses[b]); |
| 113 | } |
| 114 | // Live sets grow monotonically from empty sets; no scratch set is needed. |
| 115 | let mut changed = true; |
| 116 | |
| 117 | while changed { |
| 118 | set changed = false; |
| 119 | |
| 120 | // Process blocks in reverse order (approximates post-order). |
| 121 | let mut b = blockCount; |
| 122 | while b > 0 { |
| 123 | set b -= 1; |
| 124 | let block = &func.blocks[b]; |
| 125 | |
| 126 | // Add successor live-in sets directly to this block's live-out set. |
| 127 | addSuccessorLiveIn(func, block, liveIn, &mut liveOut[b]); |
| 128 | |
| 129 | if computeAndUpdateLiveIn(&mut liveIn[b], &liveOut[b], &defs[b], &uses[b]) { |
| 130 | set changed = true; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | return LiveInfo { liveIn, liveOut, defs, uses, blockCount, maxReg }; |
| 135 | } |
| 136 | |
| 137 | /// Compute `liveIn = uses | (liveOut - defs)` and update `dst`. |
| 138 | /// Returns `true` if `dst` changed. Combined loop avoids multiple passes. |
| 139 | unsafe fn computeAndUpdateLiveIn( |
| 140 | dst: *unsafe mut bitset::Bitset, |
| 141 | liveOut: *unsafe bitset::Bitset, |
| 142 | defs: *unsafe bitset::Bitset, |
| 143 | uses: *unsafe bitset::Bitset |
| 144 | ) -> bool { |
| 145 | let numWords = dst.bits.len; |
| 146 | let mut changed = false; |
| 147 | for i in 0..numWords { |
| 148 | let newWord = uses.bits[i] | (liveOut.bits[i] & ~defs.bits[i]); |
| 149 | if dst.bits[i] <> newWord { |
| 150 | set dst.bits[i] = newWord; |
| 151 | set changed = true; |
| 152 | } |
| 153 | } |
| 154 | return changed; |
| 155 | } |
| 156 | |
| 157 | /// Compute local defs and uses for a single block. |
| 158 | unsafe fn computeLocalDefsUses(block: *unsafe il::Block, defs: *unsafe mut bitset::Bitset, uses: *unsafe mut bitset::Bitset) { |
| 159 | for p in block.params { |
| 160 | bitset::put(defs, p.value.n); |
| 161 | } |
| 162 | for i in 0..block.instrs.len { |
| 163 | let instr = block.instrs[i]; |
| 164 | let mut ctx = DefsUses { defs, uses }; |
| 165 | il::forEachReg(instr, addUseCallback, &mut ctx as &mut opaque); |
| 166 | |
| 167 | if let dst = il::instrDst(instr) { |
| 168 | bitset::put(defs, dst.n); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// Callback for [`il::forEachReg`]: adds register to uses if not already defined. |
| 174 | unsafe fn addUseCallback(reg: il::Reg, ctx: &mut opaque) { |
| 175 | addUse(reg, ctx as &mut DefsUses); |
| 176 | } |
| 177 | |
| 178 | /// Add an undefined register to the use set. |
| 179 | unsafe fn addUse(reg: il::Reg, c: &mut DefsUses) { |
| 180 | if not bitset::contains(c.defs, reg.n) { |
| 181 | bitset::put(c.uses, reg.n); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /// Callback for [`il::forEachReg`]: updates max register number. |
| 186 | unsafe fn maxRegCallback(reg: il::Reg, ctx: &mut opaque) { |
| 187 | updateMaxReg(reg, ctx as &mut u32); |
| 188 | } |
| 189 | |
| 190 | /// Update the largest register number. |
| 191 | fn updateMaxReg(reg: il::Reg, max: &mut u32) { |
| 192 | set *max = maxRegNum(reg.n, *max); |
| 193 | } |
| 194 | |
| 195 | /// Return the larger of n+1 and current. |
| 196 | fn maxRegNum(n: u32, current: u32) -> u32 { |
| 197 | if n >= MAX_SSA_REGS { |
| 198 | return MAX_SSA_REGS + 1; |
| 199 | } |
| 200 | if n + 1 > current { |
| 201 | return n + 1; |
| 202 | } |
| 203 | return current; |
| 204 | } |
| 205 | |
| 206 | /// Add successor live-in sets to the block's live-out set. |
| 207 | unsafe fn addSuccessorLiveIn(func: *unsafe il::Fn, block: *unsafe il::Block, liveIn: *unsafe [bitset::Bitset], liveOut: *unsafe mut bitset::Bitset) { |
| 208 | if block.instrs.len == 0 { |
| 209 | return; |
| 210 | } |
| 211 | let term = block.instrs[block.instrs.len - 1]; |
| 212 | |
| 213 | match term { |
| 214 | case il::Instr::Jmp { target, .. } => |
| 215 | unionBlockLiveIn(target, liveIn, liveOut), |
| 216 | case il::Instr::Br { thenTarget, elseTarget, .. } => { |
| 217 | unionBlockLiveIn(thenTarget, liveIn, liveOut); |
| 218 | unionBlockLiveIn(elseTarget, liveIn, liveOut); |
| 219 | }, |
| 220 | case il::Instr::Switch { defaultTarget, cases, .. } => { |
| 221 | unionBlockLiveIn(defaultTarget, liveIn, liveOut); |
| 222 | for c in cases { |
| 223 | unionBlockLiveIn(c.target, liveIn, liveOut); |
| 224 | } |
| 225 | }, |
| 226 | else => {}, |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | /// Union a target block's live-in set into the block's live-out set. |
| 231 | unsafe fn unionBlockLiveIn(target: u32, liveIn: *unsafe [bitset::Bitset], liveOut: *unsafe mut bitset::Bitset) { |
| 232 | bitset::union_(liveOut, &liveIn[target]); |
| 233 | } |
| 234 | |
| 235 | /// Check if a register has any use after this instruction. |
| 236 | export unsafe fn hasLaterUse(info: *unsafe LiveInfo, func: *unsafe il::Fn, blockIdx: u32, instrIdx: u32, reg: il::Reg) -> bool { |
| 237 | let block = &func.blocks[blockIdx]; |
| 238 | |
| 239 | if bitset::contains(&info.liveOut[blockIdx], reg.n) { |
| 240 | return true; |
| 241 | } |
| 242 | for i in (instrIdx + 1)..block.instrs.len { |
| 243 | if instrUsesReg(block.instrs[i], reg) { |
| 244 | return true; |
| 245 | } |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | /// Check if an instruction uses a specific register. |
| 251 | unsafe fn instrUsesReg(instr: il::Instr, reg: il::Reg) -> bool { |
| 252 | let mut ctx = FindCtx { target: reg.n, found: false }; |
| 253 | il::forEachReg(instr, findRegCallback, &mut ctx as &mut opaque); |
| 254 | return ctx.found; |
| 255 | } |
| 256 | |
| 257 | /// Callback for [`il::forEachReg`]: sets found if register matches target. |
| 258 | unsafe fn findRegCallback(reg: il::Reg, ctx: &mut opaque) { |
| 259 | findReg(reg, ctx as &mut FindCtx); |
| 260 | } |
| 261 | |
| 262 | /// Set the match flag when the register equals the target. |
| 263 | fn findReg(reg: il::Reg, c: &mut FindCtx) { |
| 264 | if reg.n == c.target { |
| 265 | set c.found = true; |
| 266 | } |
| 267 | } |