lib/choir/src/backends/regalloc/position.zig
daab053ee43316e1809a84551d573ddd1e5bf3d2
1 const ir = @import("../../core/root.zig");
2
3 pub const Phase = enum(u1) {
4 source,
5 definition,
6 };
7
8 pub const Point = struct {
9 position: u32,
10 phase: Phase,
11
12 pub fn source(position: u32) Point {
13 return .{ .position = position, .phase = .source };
14 }
15
16 pub fn definition(position: u32) Point {
17 return .{ .position = position, .phase = .definition };
18 }
19
20 pub fn rank(self: Point) u64 {
21 return (@as(u64, self.position) << 1) | @backingInt(self.phase);
22 }
23
24 pub fn lessThan(self: Point, other: Point) bool {
25 return self.rank() < other.rank();
26 }
27
28 pub fn greaterThan(self: Point, other: Point) bool {
29 return self.rank() > other.rank();
30 }
31 };
32
33 pub fn valueStartPhase(value: *const ir.Value) Phase {
34 return switch (value.kind) {
35 .block_argument => .source,
36 .op_result => .definition,
37 };
38 }
39
40 test "position points order source before definition" {
41 const std = @import("std");
42 const source = Point.source(4);
43 const definition = Point.definition(4);
44 try std.testing.expect(source.lessThan(definition));
45 try std.testing.expect(definition.greaterThan(source));
46 }