1 /*
2 * Copyright (C) 2022 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "agx_compiler.h"
25 #include "agx_builder.h"
26
27 /* Lower pseudo instructions created during optimization. */
28
29 static void
agx_lower_to_unary_bitop(agx_instr * I,enum agx_bitop_table table)30 agx_lower_to_unary_bitop(agx_instr *I, enum agx_bitop_table table)
31 {
32 I->op = AGX_OPCODE_BITOP;
33 I->truth_table = table;
34
35 /* Allocate extra source */
36 I->src = reralloc_array_size(I, I->src, sizeof(agx_index), I->nr_srcs++);
37 I->src[1] = agx_zero();
38 }
39
40 static void
agx_lower_to_binary_bitop(agx_instr * I,enum agx_bitop_table table)41 agx_lower_to_binary_bitop(agx_instr *I, enum agx_bitop_table table)
42 {
43 I->op = AGX_OPCODE_BITOP;
44 I->truth_table = table;
45 }
46
47 void
agx_lower_pseudo(agx_context * ctx)48 agx_lower_pseudo(agx_context *ctx)
49 {
50 agx_foreach_instr_global(ctx, I) {
51 switch (I->op) {
52
53 /* Various instructions are implemented as bitwise truth tables */
54 case AGX_OPCODE_MOV: agx_lower_to_unary_bitop(I, AGX_BITOP_MOV); break;
55 case AGX_OPCODE_NOT: agx_lower_to_unary_bitop(I, AGX_BITOP_NOT); break;
56 case AGX_OPCODE_AND: agx_lower_to_binary_bitop(I, AGX_BITOP_AND); break;
57 case AGX_OPCODE_XOR: agx_lower_to_binary_bitop(I, AGX_BITOP_XOR); break;
58 case AGX_OPCODE_OR: agx_lower_to_binary_bitop(I, AGX_BITOP_OR); break;
59
60 default:
61 break;
62 }
63 }
64 }
65