1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2021 Collabora Ltd. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#include "va_compiler.h" 25bf215546Sopenharmony_ci 26bf215546Sopenharmony_ci/* Valhall specific instruction selection optimizations */ 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_cistatic enum bi_opcode 29bf215546Sopenharmony_civa_op_add_imm(enum bi_opcode op) 30bf215546Sopenharmony_ci{ 31bf215546Sopenharmony_ci switch (op) { 32bf215546Sopenharmony_ci case BI_OPCODE_FADD_F32: return BI_OPCODE_FADD_IMM_F32; 33bf215546Sopenharmony_ci case BI_OPCODE_FADD_V2F16: return BI_OPCODE_FADD_IMM_V2F16; 34bf215546Sopenharmony_ci case BI_OPCODE_IADD_S32: 35bf215546Sopenharmony_ci case BI_OPCODE_IADD_U32: return BI_OPCODE_IADD_IMM_I32; 36bf215546Sopenharmony_ci case BI_OPCODE_IADD_V2S16: 37bf215546Sopenharmony_ci case BI_OPCODE_IADD_V2U16: return BI_OPCODE_IADD_IMM_V2I16; 38bf215546Sopenharmony_ci case BI_OPCODE_IADD_V4S8: 39bf215546Sopenharmony_ci case BI_OPCODE_IADD_V4U8: return BI_OPCODE_IADD_IMM_V4I8; 40bf215546Sopenharmony_ci default: return 0; 41bf215546Sopenharmony_ci } 42bf215546Sopenharmony_ci} 43bf215546Sopenharmony_ci 44bf215546Sopenharmony_cistatic bool 45bf215546Sopenharmony_civa_is_add_imm(bi_instr *I, unsigned s) 46bf215546Sopenharmony_ci{ 47bf215546Sopenharmony_ci return I->src[s].swizzle == BI_SWIZZLE_H01 && 48bf215546Sopenharmony_ci !I->src[s].abs && !I->src[s].neg && !I->clamp && !I->round; 49bf215546Sopenharmony_ci} 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_cistatic unsigned 52bf215546Sopenharmony_civa_choose_imm(bi_instr *I) 53bf215546Sopenharmony_ci{ 54bf215546Sopenharmony_ci for (unsigned i = 0; i < 2; ++i) { 55bf215546Sopenharmony_ci if (I->src[i].type == BI_INDEX_CONSTANT) 56bf215546Sopenharmony_ci return i; 57bf215546Sopenharmony_ci } 58bf215546Sopenharmony_ci 59bf215546Sopenharmony_ci return ~0; 60bf215546Sopenharmony_ci} 61bf215546Sopenharmony_ci 62bf215546Sopenharmony_ci/* Lower MOV.i32 #constant --> IADD_IMM.i32 0x0, #constant */ 63bf215546Sopenharmony_cistatic void 64bf215546Sopenharmony_civa_lower_mov_imm(bi_instr *I) 65bf215546Sopenharmony_ci{ 66bf215546Sopenharmony_ci if (I->src[0].type == BI_INDEX_CONSTANT) { 67bf215546Sopenharmony_ci I->op = BI_OPCODE_IADD_IMM_I32; 68bf215546Sopenharmony_ci I->index = I->src[0].value; 69bf215546Sopenharmony_ci I->src[0] = bi_zero(); 70bf215546Sopenharmony_ci } 71bf215546Sopenharmony_ci} 72bf215546Sopenharmony_ci 73bf215546Sopenharmony_civoid 74bf215546Sopenharmony_civa_fuse_add_imm(bi_instr *I) 75bf215546Sopenharmony_ci{ 76bf215546Sopenharmony_ci if (I->op == BI_OPCODE_MOV_I32) { 77bf215546Sopenharmony_ci va_lower_mov_imm(I); 78bf215546Sopenharmony_ci return; 79bf215546Sopenharmony_ci } 80bf215546Sopenharmony_ci 81bf215546Sopenharmony_ci enum bi_opcode op = va_op_add_imm(I->op); 82bf215546Sopenharmony_ci if (!op) return; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci unsigned s = va_choose_imm(I); 85bf215546Sopenharmony_ci if (s > 1) return; 86bf215546Sopenharmony_ci if (!va_is_add_imm(I, 1 - s)) return; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci I->op = op; 89bf215546Sopenharmony_ci I->index = bi_apply_swizzle(I->src[s].value, I->src[s].swizzle); 90bf215546Sopenharmony_ci 91bf215546Sopenharmony_ci assert(!I->src[s].abs && "redundant .abs set"); 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci /* If the constant is negated, flip the sign bit */ 94bf215546Sopenharmony_ci if (I->src[s].neg) { 95bf215546Sopenharmony_ci if (I->op == BI_OPCODE_FADD_IMM_F32) 96bf215546Sopenharmony_ci I->index ^= (1 << 31); 97bf215546Sopenharmony_ci else if (I->op == BI_OPCODE_FADD_IMM_V2F16) 98bf215546Sopenharmony_ci I->index ^= (1 << 31) | (1 << 15); 99bf215546Sopenharmony_ci else 100bf215546Sopenharmony_ci unreachable("unexpected .neg"); 101bf215546Sopenharmony_ci } 102bf215546Sopenharmony_ci 103bf215546Sopenharmony_ci I->src[0] = I->src[1 - s]; 104bf215546Sopenharmony_ci I->src[1] = bi_null(); 105bf215546Sopenharmony_ci} 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_civoid 108bf215546Sopenharmony_civa_optimize(bi_context *ctx) 109bf215546Sopenharmony_ci{ 110bf215546Sopenharmony_ci bi_foreach_instr_global(ctx, I) { 111bf215546Sopenharmony_ci va_fuse_add_imm(I); 112bf215546Sopenharmony_ci } 113bf215546Sopenharmony_ci} 114