Path: blob/21.2-virgl/src/panfrost/bifrost/bi_opt_constant_fold.c
4564 views
/*1* Copyright (C) 2020 Collabora Ltd.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include "compiler.h"24#include "bi_builder.h"2526/* Dead simple constant folding to cleanup compiler frontend patterns. Before27* adding a new pattern here, check why you need it and whether we can avoid28* generating the constant BIR at all. */2930static uint32_t31bi_fold_constant(bi_instr *I, bool *unsupported)32{33uint32_t a = I->src[0].value;34uint32_t b = I->src[1].value;3536switch (I->op) {37case BI_OPCODE_SWZ_V2I16:38{39uint16_t lo = (a & 0xFFFF);40uint16_t hi = (a >> 16);4142enum bi_swizzle swz = I->src[0].swizzle;43assert(swz < BI_SWIZZLE_H11);4445/* Note order is H00, H01, H10, H11 */46return (((swz & (1 << 1)) ? hi : lo) << 0) |47(((swz & (1 << 0)) ? hi : lo) << 16);48}4950case BI_OPCODE_MKVEC_V2I16:51{52bool hi_a = I->src[0].swizzle & BI_SWIZZLE_H11;53bool hi_b = I->src[1].swizzle & BI_SWIZZLE_H11;5455uint16_t lo = (hi_a ? (a >> 16) : (a & 0xFFFF));56uint16_t hi = (hi_b ? (b >> 16) : (b & 0xFFFF));5758return (hi << 16) | lo;59}6061default:62*unsupported = true;63return 0;64}65}6667static bool68bi_all_srcs_const(bi_instr *I)69{70bi_foreach_src(I, s) {71enum bi_index_type type = I->src[s].type;7273if (!(type == BI_INDEX_NULL || type == BI_INDEX_CONSTANT))74return false;75}7677return true;78}7980void81bi_opt_constant_fold(bi_context *ctx)82{83bi_foreach_instr_global_safe(ctx, ins) {84if (!bi_all_srcs_const(ins)) continue;8586bool unsupported = false;87uint32_t replace = bi_fold_constant(ins, &unsupported);88if (unsupported) continue;8990/* Replace with constant move, to be copypropped */91bi_builder b = bi_init_builder(ctx, bi_after_instr(ins));92bi_mov_i32_to(&b, ins->dest[0], bi_imm_u32(replace));93bi_remove_instruction(ins);94}95}969798