1 /*
2 * Copyright (C) 2020 Collabora Ltd.
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 "bi_builder.h"
25 #include "compiler.h"
26
27 /* Dead simple constant folding to cleanup compiler frontend patterns. Before
28 * adding a new pattern here, check why you need it and whether we can avoid
29 * generating the constant BIR at all. */
30
31 static inline uint32_t
bi_source_value(const bi_instr * I,unsigned s)32 bi_source_value(const bi_instr *I, unsigned s)
33 {
34 if (s < I->nr_srcs)
35 return bi_apply_swizzle(I->src[s].value, I->src[s].swizzle);
36 else
37 return 0;
38 }
39
40 uint32_t
bi_fold_constant(bi_instr * I,bool * unsupported)41 bi_fold_constant(bi_instr *I, bool *unsupported)
42 {
43 /* We can only fold instructions where all sources are constant */
44 bi_foreach_src(I, s) {
45 if (I->src[s].type != BI_INDEX_CONSTANT) {
46 *unsupported = true;
47 return 0;
48 }
49 }
50
51 /* Grab the sources */
52 uint32_t a = bi_source_value(I, 0);
53 uint32_t b = bi_source_value(I, 1);
54 uint32_t c = bi_source_value(I, 2);
55 uint32_t d = bi_source_value(I, 3);
56
57 /* Evaluate the instruction */
58 switch (I->op) {
59 case BI_OPCODE_SWZ_V2I16:
60 return a;
61
62 case BI_OPCODE_MKVEC_V2I16:
63 return (b << 16) | (a & 0xFFFF);
64
65 case BI_OPCODE_MKVEC_V4I8:
66 return (d << 24) | ((c & 0xFF) << 16) | ((b & 0xFF) << 8) | (a & 0xFF);
67
68 case BI_OPCODE_MKVEC_V2I8:
69 return (c << 16) | ((b & 0xFF) << 8) | (a & 0xFF);
70
71 case BI_OPCODE_LSHIFT_OR_I32:
72 if (I->not_result || I->src[0].neg || I->src[1].neg)
73 break;
74
75 return (a << (c & 0x1F)) | b;
76
77 case BI_OPCODE_F32_TO_U32:
78 if (I->round == BI_ROUND_NONE) {
79 /* Explicitly clamp to prevent undefined behaviour and
80 * match hardware rules */
81 float f = uif(a);
82 return (f >= 0.0) ? (uint32_t)f : 0;
83 } else
84 break;
85
86 default:
87 break;
88 }
89
90 *unsupported = true;
91 return 0;
92 }
93
94 bool
bi_opt_constant_fold(bi_context * ctx)95 bi_opt_constant_fold(bi_context *ctx)
96 {
97 bool progress = false;
98
99 bi_foreach_instr_global_safe(ctx, ins) {
100 bool unsupported = false;
101 uint32_t replace = bi_fold_constant(ins, &unsupported);
102 if (unsupported)
103 continue;
104
105 /* Replace with constant move, to be copypropped */
106 assert(ins->nr_dests == 1);
107 bi_builder b = bi_init_builder(ctx, bi_after_instr(ins));
108 bi_mov_i32_to(&b, ins->dest[0], bi_imm_u32(replace));
109 bi_remove_instruction(ins);
110 progress = true;
111 }
112
113 return progress;
114 }
115