Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/panfrost/bifrost/bi_opt_constant_fold.c
4564 views
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 "compiler.h"
25
#include "bi_builder.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 uint32_t
32
bi_fold_constant(bi_instr *I, bool *unsupported)
33
{
34
uint32_t a = I->src[0].value;
35
uint32_t b = I->src[1].value;
36
37
switch (I->op) {
38
case BI_OPCODE_SWZ_V2I16:
39
{
40
uint16_t lo = (a & 0xFFFF);
41
uint16_t hi = (a >> 16);
42
43
enum bi_swizzle swz = I->src[0].swizzle;
44
assert(swz < BI_SWIZZLE_H11);
45
46
/* Note order is H00, H01, H10, H11 */
47
return (((swz & (1 << 1)) ? hi : lo) << 0) |
48
(((swz & (1 << 0)) ? hi : lo) << 16);
49
}
50
51
case BI_OPCODE_MKVEC_V2I16:
52
{
53
bool hi_a = I->src[0].swizzle & BI_SWIZZLE_H11;
54
bool hi_b = I->src[1].swizzle & BI_SWIZZLE_H11;
55
56
uint16_t lo = (hi_a ? (a >> 16) : (a & 0xFFFF));
57
uint16_t hi = (hi_b ? (b >> 16) : (b & 0xFFFF));
58
59
return (hi << 16) | lo;
60
}
61
62
default:
63
*unsupported = true;
64
return 0;
65
}
66
}
67
68
static bool
69
bi_all_srcs_const(bi_instr *I)
70
{
71
bi_foreach_src(I, s) {
72
enum bi_index_type type = I->src[s].type;
73
74
if (!(type == BI_INDEX_NULL || type == BI_INDEX_CONSTANT))
75
return false;
76
}
77
78
return true;
79
}
80
81
void
82
bi_opt_constant_fold(bi_context *ctx)
83
{
84
bi_foreach_instr_global_safe(ctx, ins) {
85
if (!bi_all_srcs_const(ins)) continue;
86
87
bool unsupported = false;
88
uint32_t replace = bi_fold_constant(ins, &unsupported);
89
if (unsupported) continue;
90
91
/* Replace with constant move, to be copypropped */
92
bi_builder b = bi_init_builder(ctx, bi_after_instr(ins));
93
bi_mov_i32_to(&b, ins->dest[0], bi_imm_u32(replace));
94
bi_remove_instruction(ins);
95
}
96
}
97
98