Debian Codegraph @

src/asahi/compiler/agx_lower_pseudo.c

raw
1 /*
2 * Copyright 2022 Alyssa Rosenzweig
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "util/lut.h"
7 #include "agx_builder.h"
8 #include "agx_compiler.h"
9 #include "agx_opcodes.h"
10
11 /* Lower pseudo instructions created during optimization. */
12
13 static agx_instr *
14 while_for_break_if(agx_builder *b, agx_instr *I)
15 {
16 if (I->op == AGX_OPCODE_BREAK_IF_FCMP) {
17 return agx_while_fcmp(b, I->src[0], I->src[1], I->nest, I->fcond,
18 !I->invert_cond, NULL);
19 } else {
20 return agx_while_icmp(b, I->src[0], I->src[1], I->nest, I->icond,
21 !I->invert_cond, NULL);
22 }
23 }
24
25 static agx_instr *
26 cmpsel_for_break_if(agx_builder *b, agx_instr *I)
27 {
28 agx_index r0l = agx_register(0, AGX_SIZE_16);
29
30 /* If the condition is true, set r0l to nest to break */
31 agx_index t = agx_immediate(I->nest);
32 agx_index f = r0l;
33
34 if (I->invert_cond) {
35 SWAP(t, f);
36 }
37
38 if (I->op == AGX_OPCODE_BREAK_IF_FCMP)
39 agx_fcmpsel_to(b, r0l, I->src[0], I->src[1], t, f, I->fcond);
40 else
41 agx_icmpsel_to(b, r0l, I->src[0], I->src[1], t, f, I->icond);
42
43 return agx_push_exec(b, 0);
44 }
45
46 static void
47 swap(agx_builder *b, agx_index x, agx_index y)
48 {
49 assert(!x.memory && "already lowered");
50 assert(!y.memory && "already lowered");
51
52 /* We can swap lo/hi halves of a 32-bit register with a 32-bit extr */
53 if (x.size == AGX_SIZE_16 && (x.value >> 1) == (y.value >> 1)) {
54
55 assert(((x.value & 1) == (1 - (y.value & 1))) &&
56 "no trivial swaps, and only 2 halves of a register");
57
58 /* r0 = extr r0, r0, #16
59 * = (((r0 << 32) | r0) >> 16) & 0xFFFFFFFF
60 * = (((r0 << 32) >> 16) & 0xFFFFFFFF) | (r0 >> 16)
61 * = (r0l << 16) | r0h
62 */
63 agx_index reg32 = agx_register(x.value & ~1, AGX_SIZE_32);
64 agx_extr_to(b, reg32, reg32, reg32, agx_immediate(16), 0);
65 } else {
66 /* Otherwise, we're swapping GPRs and fallback on a XOR swap. */
67 agx_xor_to(b, x, x, y);
68 agx_xor_to(b, y, x, y);
69 agx_xor_to(b, x, x, y);
70 }
71 }
72
73 static agx_instr *
74 lower(agx_builder *b, agx_instr *I)
75 {
76 switch (I->op) {
77
78 /* Various instructions are implemented as bitwise truth tables */
79 case AGX_OPCODE_MOV:
80 return agx_bitop_to(b, I->dest[0], I->src[0], agx_zero(), UTIL_LUT2(a));
81
82 case AGX_OPCODE_NOT:
83 return agx_bitop_to(b, I->dest[0], I->src[0], agx_zero(), UTIL_LUT2(~a));
84
85 /* We can sign-extend with an add */
86 case AGX_OPCODE_SIGNEXT:
87 return agx_iadd_to(b, I->dest[0], I->src[0], agx_zero(), 0);
88
89 /* Unfused comparisons are fused with a 0/1 select */
90 case AGX_OPCODE_ICMP:
91 return agx_icmpsel_to(b, I->dest[0], I->src[0], I->src[1],
92 agx_immediate(I->invert_cond ? 0 : 1),
93 agx_immediate(I->invert_cond ? 1 : 0), I->icond);
94
95 case AGX_OPCODE_FCMP:
96 return agx_fcmpsel_to(b, I->dest[0], I->src[0], I->src[1],
97 agx_immediate(I->invert_cond ? 0 : 1),
98 agx_immediate(I->invert_cond ? 1 : 0), I->fcond);
99
100 case AGX_OPCODE_BALLOT:
101 return agx_icmp_ballot_to(b, I->dest[0], I->src[0], agx_zero(),
102 AGX_ICOND_UEQ, true /* invert */);
103
104 case AGX_OPCODE_QUAD_BALLOT:
105 return agx_icmp_quad_ballot_to(b, I->dest[0], I->src[0], agx_zero(),
106 AGX_ICOND_UEQ, true /* invert */);
107
108 /* Writes to the nesting counter lowered to the real register */
109 case AGX_OPCODE_BEGIN_CF:
110 return agx_mov_imm_to(b, agx_register(0, AGX_SIZE_16), 0);
111
112 case AGX_OPCODE_BREAK:
113 agx_mov_imm_to(b, agx_register(0, AGX_SIZE_16), I->nest);
114 return agx_pop_exec(b, 0);
115
116 case AGX_OPCODE_BREAK_IF_ICMP:
117 case AGX_OPCODE_BREAK_IF_FCMP: {
118 if (I->nest == 1)
119 return while_for_break_if(b, I);
120 else
121 return cmpsel_for_break_if(b, I);
122 }
123
124 case AGX_OPCODE_SWAP:
125 swap(b, I->src[0], I->src[1]);
126 return (void *)true;
127
128 default:
129 return NULL;
130 }
131 }
132
133 void
134 agx_lower_pseudo(agx_context *ctx)
135 {
136 agx_foreach_instr_global_safe(ctx, I) {
137 agx_builder b = agx_init_builder(ctx, agx_before_instr(I));
138
139 if (lower(&b, I))
140 agx_remove_instruction(I);
141 }
142 }