Path: blob/21.2-virgl/src/intel/compiler/brw_fs_copy_propagation.cpp
4550 views
/*1* Copyright © 2012 Intel Corporation2*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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/** @file brw_fs_copy_propagation.cpp24*25* Support for global copy propagation in two passes: A local pass that does26* intra-block copy (and constant) propagation, and a global pass that uses27* dataflow analysis on the copies available at the end of each block to re-do28* local copy propagation with more copies available.29*30* See Muchnick's Advanced Compiler Design and Implementation, section31* 12.5 (p356).32*/3334#define ACP_HASH_SIZE 643536#include "util/bitset.h"37#include "util/u_math.h"38#include "brw_fs.h"39#include "brw_fs_live_variables.h"40#include "brw_cfg.h"41#include "brw_eu.h"4243using namespace brw;4445namespace { /* avoid conflict with opt_copy_propagation_elements */46struct acp_entry : public exec_node {47fs_reg dst;48fs_reg src;49unsigned global_idx;50unsigned size_written;51unsigned size_read;52enum opcode opcode;53bool saturate;54};5556struct block_data {57/**58* Which entries in the fs_copy_prop_dataflow acp table are live at the59* start of this block. This is the useful output of the analysis, since60* it lets us plug those into the local copy propagation on the second61* pass.62*/63BITSET_WORD *livein;6465/**66* Which entries in the fs_copy_prop_dataflow acp table are live at the end67* of this block. This is done in initial setup from the per-block acps68* returned by the first local copy prop pass.69*/70BITSET_WORD *liveout;7172/**73* Which entries in the fs_copy_prop_dataflow acp table are generated by74* instructions in this block which reach the end of the block without75* being killed.76*/77BITSET_WORD *copy;7879/**80* Which entries in the fs_copy_prop_dataflow acp table are killed over the81* course of this block.82*/83BITSET_WORD *kill;8485/**86* Which entries in the fs_copy_prop_dataflow acp table are guaranteed to87* have a fully uninitialized destination at the end of this block.88*/89BITSET_WORD *undef;90};9192class fs_copy_prop_dataflow93{94public:95fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,96const fs_live_variables &live,97exec_list *out_acp[ACP_HASH_SIZE]);9899void setup_initial_values();100void run();101102void dump_block_data() const UNUSED;103104void *mem_ctx;105cfg_t *cfg;106const fs_live_variables &live;107108acp_entry **acp;109int num_acp;110int bitset_words;111112struct block_data *bd;113};114} /* anonymous namespace */115116fs_copy_prop_dataflow::fs_copy_prop_dataflow(void *mem_ctx, cfg_t *cfg,117const fs_live_variables &live,118exec_list *out_acp[ACP_HASH_SIZE])119: mem_ctx(mem_ctx), cfg(cfg), live(live)120{121bd = rzalloc_array(mem_ctx, struct block_data, cfg->num_blocks);122123num_acp = 0;124foreach_block (block, cfg) {125for (int i = 0; i < ACP_HASH_SIZE; i++) {126num_acp += out_acp[block->num][i].length();127}128}129130acp = rzalloc_array(mem_ctx, struct acp_entry *, num_acp);131132bitset_words = BITSET_WORDS(num_acp);133134int next_acp = 0;135foreach_block (block, cfg) {136bd[block->num].livein = rzalloc_array(bd, BITSET_WORD, bitset_words);137bd[block->num].liveout = rzalloc_array(bd, BITSET_WORD, bitset_words);138bd[block->num].copy = rzalloc_array(bd, BITSET_WORD, bitset_words);139bd[block->num].kill = rzalloc_array(bd, BITSET_WORD, bitset_words);140bd[block->num].undef = rzalloc_array(bd, BITSET_WORD, bitset_words);141142for (int i = 0; i < ACP_HASH_SIZE; i++) {143foreach_in_list(acp_entry, entry, &out_acp[block->num][i]) {144acp[next_acp] = entry;145146entry->global_idx = next_acp;147148/* opt_copy_propagation_local populates out_acp with copies created149* in a block which are still live at the end of the block. This150* is exactly what we want in the COPY set.151*/152BITSET_SET(bd[block->num].copy, next_acp);153154next_acp++;155}156}157}158159assert(next_acp == num_acp);160161setup_initial_values();162run();163}164165/**166* Set up initial values for each of the data flow sets, prior to running167* the fixed-point algorithm.168*/169void170fs_copy_prop_dataflow::setup_initial_values()171{172/* Initialize the COPY and KILL sets. */173{174/* Create a temporary table of ACP entries which we'll use for efficient175* look-up. Unfortunately, we have to do this in two steps because we176* have to match both sources and destinations and an ACP entry can only177* be in one list at a time.178*179* We choose to make the table size between num_acp/2 and num_acp/4 to180* try and trade off between the time it takes to initialize the table181* via exec_list constructors or make_empty() and the cost of182* collisions. In practice, it doesn't appear to matter too much what183* size we make the table as long as it's roughly the same order of184* magnitude as num_acp. We get most of the benefit of the table185* approach even if we use a table of size ACP_HASH_SIZE though a186* full-sized table is 1-2% faster in practice.187*/188unsigned acp_table_size = util_next_power_of_two(num_acp) / 4;189acp_table_size = MAX2(acp_table_size, ACP_HASH_SIZE);190exec_list *acp_table = new exec_list[acp_table_size];191192/* First, get all the KILLs for instructions which overwrite ACP193* destinations.194*/195for (int i = 0; i < num_acp; i++) {196unsigned idx = reg_space(acp[i]->dst) & (acp_table_size - 1);197acp_table[idx].push_tail(acp[i]);198}199200foreach_block (block, cfg) {201foreach_inst_in_block(fs_inst, inst, block) {202if (inst->dst.file != VGRF)203continue;204205unsigned idx = reg_space(inst->dst) & (acp_table_size - 1);206foreach_in_list(acp_entry, entry, &acp_table[idx]) {207if (regions_overlap(inst->dst, inst->size_written,208entry->dst, entry->size_written))209BITSET_SET(bd[block->num].kill, entry->global_idx);210}211}212}213214/* Clear the table for the second pass */215for (unsigned i = 0; i < acp_table_size; i++)216acp_table[i].make_empty();217218/* Next, get all the KILLs for instructions which overwrite ACP219* sources.220*/221for (int i = 0; i < num_acp; i++) {222unsigned idx = reg_space(acp[i]->src) & (acp_table_size - 1);223acp_table[idx].push_tail(acp[i]);224}225226foreach_block (block, cfg) {227foreach_inst_in_block(fs_inst, inst, block) {228if (inst->dst.file != VGRF &&229inst->dst.file != FIXED_GRF)230continue;231232unsigned idx = reg_space(inst->dst) & (acp_table_size - 1);233foreach_in_list(acp_entry, entry, &acp_table[idx]) {234if (regions_overlap(inst->dst, inst->size_written,235entry->src, entry->size_read))236BITSET_SET(bd[block->num].kill, entry->global_idx);237}238}239}240241delete [] acp_table;242}243244/* Populate the initial values for the livein and liveout sets. For the245* block at the start of the program, livein = 0 and liveout = copy.246* For the others, set liveout and livein to ~0 (the universal set).247*/248foreach_block (block, cfg) {249if (block->parents.is_empty()) {250for (int i = 0; i < bitset_words; i++) {251bd[block->num].livein[i] = 0u;252bd[block->num].liveout[i] = bd[block->num].copy[i];253}254} else {255for (int i = 0; i < bitset_words; i++) {256bd[block->num].liveout[i] = ~0u;257bd[block->num].livein[i] = ~0u;258}259}260}261262/* Initialize the undef set. */263foreach_block (block, cfg) {264for (int i = 0; i < num_acp; i++) {265BITSET_SET(bd[block->num].undef, i);266for (unsigned off = 0; off < acp[i]->size_written; off += REG_SIZE) {267if (BITSET_TEST(live.block_data[block->num].defout,268live.var_from_reg(byte_offset(acp[i]->dst, off))))269BITSET_CLEAR(bd[block->num].undef, i);270}271}272}273}274275/**276* Walk the set of instructions in the block, marking which entries in the acp277* are killed by the block.278*/279void280fs_copy_prop_dataflow::run()281{282bool progress;283284do {285progress = false;286287foreach_block (block, cfg) {288if (block->parents.is_empty())289continue;290291for (int i = 0; i < bitset_words; i++) {292const BITSET_WORD old_liveout = bd[block->num].liveout[i];293BITSET_WORD livein_from_any_block = 0;294295/* Update livein for this block. If a copy is live out of all296* parent blocks, it's live coming in to this block.297*/298bd[block->num].livein[i] = ~0u;299foreach_list_typed(bblock_link, parent_link, link, &block->parents) {300bblock_t *parent = parent_link->block;301/* Consider ACP entries with a known-undefined destination to302* be available from the parent. This is valid because we're303* free to set the undefined variable equal to the source of304* the ACP entry without breaking the application's305* expectations, since the variable is undefined.306*/307bd[block->num].livein[i] &= (bd[parent->num].liveout[i] |308bd[parent->num].undef[i]);309livein_from_any_block |= bd[parent->num].liveout[i];310}311312/* Limit to the set of ACP entries that can possibly be available313* at the start of the block, since propagating from a variable314* which is guaranteed to be undefined (rather than potentially315* undefined for some dynamic control-flow paths) doesn't seem316* particularly useful.317*/318bd[block->num].livein[i] &= livein_from_any_block;319320/* Update liveout for this block. */321bd[block->num].liveout[i] =322bd[block->num].copy[i] | (bd[block->num].livein[i] &323~bd[block->num].kill[i]);324325if (old_liveout != bd[block->num].liveout[i])326progress = true;327}328}329} while (progress);330}331332void333fs_copy_prop_dataflow::dump_block_data() const334{335foreach_block (block, cfg) {336fprintf(stderr, "Block %d [%d, %d] (parents ", block->num,337block->start_ip, block->end_ip);338foreach_list_typed(bblock_link, link, link, &block->parents) {339bblock_t *parent = link->block;340fprintf(stderr, "%d ", parent->num);341}342fprintf(stderr, "):\n");343fprintf(stderr, " livein = 0x");344for (int i = 0; i < bitset_words; i++)345fprintf(stderr, "%08x", bd[block->num].livein[i]);346fprintf(stderr, ", liveout = 0x");347for (int i = 0; i < bitset_words; i++)348fprintf(stderr, "%08x", bd[block->num].liveout[i]);349fprintf(stderr, ",\n copy = 0x");350for (int i = 0; i < bitset_words; i++)351fprintf(stderr, "%08x", bd[block->num].copy[i]);352fprintf(stderr, ", kill = 0x");353for (int i = 0; i < bitset_words; i++)354fprintf(stderr, "%08x", bd[block->num].kill[i]);355fprintf(stderr, "\n");356}357}358359static bool360is_logic_op(enum opcode opcode)361{362return (opcode == BRW_OPCODE_AND ||363opcode == BRW_OPCODE_OR ||364opcode == BRW_OPCODE_XOR ||365opcode == BRW_OPCODE_NOT);366}367368static bool369can_take_stride(fs_inst *inst, brw_reg_type dst_type,370unsigned arg, unsigned stride,371const intel_device_info *devinfo)372{373if (stride > 4)374return false;375376/* Bail if the channels of the source need to be aligned to the byte offset377* of the corresponding channel of the destination, and the provided stride378* would break this restriction.379*/380if (has_dst_aligned_region_restriction(devinfo, inst, dst_type) &&381!(type_sz(inst->src[arg].type) * stride ==382type_sz(dst_type) * inst->dst.stride ||383stride == 0))384return false;385386/* 3-source instructions can only be Align16, which restricts what strides387* they can take. They can only take a stride of 1 (the usual case), or 0388* with a special "repctrl" bit. But the repctrl bit doesn't work for389* 64-bit datatypes, so if the source type is 64-bit then only a stride of390* 1 is allowed. From the Broadwell PRM, Volume 7 "3D Media GPGPU", page391* 944:392*393* This is applicable to 32b datatypes and 16b datatype. 64b datatypes394* cannot use the replicate control.395*/396if (inst->is_3src(devinfo)) {397if (type_sz(inst->src[arg].type) > 4)398return stride == 1;399else400return stride == 1 || stride == 0;401}402403/* From the Broadwell PRM, Volume 2a "Command Reference - Instructions",404* page 391 ("Extended Math Function"):405*406* The following restrictions apply for align1 mode: Scalar source is407* supported. Source and destination horizontal stride must be the408* same.409*410* From the Haswell PRM Volume 2b "Command Reference - Instructions", page411* 134 ("Extended Math Function"):412*413* Scalar source is supported. Source and destination horizontal stride414* must be 1.415*416* and similar language exists for IVB and SNB. Pre-SNB, math instructions417* are sends, so the sources are moved to MRF's and there are no418* restrictions.419*/420if (inst->is_math()) {421if (devinfo->ver == 6 || devinfo->ver == 7) {422assert(inst->dst.stride == 1);423return stride == 1 || stride == 0;424} else if (devinfo->ver >= 8) {425return stride == inst->dst.stride || stride == 0;426}427}428429return true;430}431432static bool433instruction_requires_packed_data(fs_inst *inst)434{435switch (inst->opcode) {436case FS_OPCODE_DDX_FINE:437case FS_OPCODE_DDX_COARSE:438case FS_OPCODE_DDY_FINE:439case FS_OPCODE_DDY_COARSE:440case SHADER_OPCODE_QUAD_SWIZZLE:441return true;442default:443return false;444}445}446447bool448fs_visitor::try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry)449{450if (inst->src[arg].file != VGRF)451return false;452453if (entry->src.file == IMM)454return false;455assert(entry->src.file == VGRF || entry->src.file == UNIFORM ||456entry->src.file == ATTR || entry->src.file == FIXED_GRF);457458/* Avoid propagating a LOAD_PAYLOAD instruction into another if there is a459* good chance that we'll be able to eliminate the latter through register460* coalescing. If only part of the sources of the second LOAD_PAYLOAD can461* be simplified through copy propagation we would be making register462* coalescing impossible, ending up with unnecessary copies in the program.463* This is also the case for is_multi_copy_payload() copies that can only464* be coalesced when the instruction is lowered into a sequence of MOVs.465*466* Worse -- In cases where the ACP entry was the result of CSE combining467* multiple LOAD_PAYLOAD subexpressions, propagating the first LOAD_PAYLOAD468* into the second would undo the work of CSE, leading to an infinite469* optimization loop. Avoid this by detecting LOAD_PAYLOAD copies from CSE470* temporaries which should match is_coalescing_payload().471*/472if (entry->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&473(is_coalescing_payload(alloc, inst) || is_multi_copy_payload(inst)))474return false;475476assert(entry->dst.file == VGRF);477if (inst->src[arg].nr != entry->dst.nr)478return false;479480/* Bail if inst is reading a range that isn't contained in the range481* that entry is writing.482*/483if (!region_contained_in(inst->src[arg], inst->size_read(arg),484entry->dst, entry->size_written))485return false;486487/* Avoid propagating a FIXED_GRF register into an EOT instruction in order488* for any register allocation restrictions to be applied.489*/490if (entry->src.file == FIXED_GRF && inst->eot)491return false;492493/* Avoid propagating odd-numbered FIXED_GRF registers into the first source494* of a LINTERP instruction on platforms where the PLN instruction has495* register alignment restrictions.496*/497if (devinfo->has_pln && devinfo->ver <= 6 &&498entry->src.file == FIXED_GRF && (entry->src.nr & 1) &&499inst->opcode == FS_OPCODE_LINTERP && arg == 0)500return false;501502/* we can't generally copy-propagate UD negations because we503* can end up accessing the resulting values as signed integers504* instead. See also resolve_ud_negate() and comment in505* fs_generator::generate_code.506*/507if (entry->src.type == BRW_REGISTER_TYPE_UD &&508entry->src.negate)509return false;510511bool has_source_modifiers = entry->src.abs || entry->src.negate;512513if (has_source_modifiers && !inst->can_do_source_mods(devinfo))514return false;515516/* Reject cases that would violate register regioning restrictions. */517if ((entry->src.file == UNIFORM || !entry->src.is_contiguous()) &&518((devinfo->ver == 6 && inst->is_math()) ||519inst->is_send_from_grf() ||520inst->uses_indirect_addressing())) {521return false;522}523524if (has_source_modifiers &&525inst->opcode == SHADER_OPCODE_GFX4_SCRATCH_WRITE)526return false;527528/* Some instructions implemented in the generator backend, such as529* derivatives, assume that their operands are packed so we can't530* generally propagate strided regions to them.531*/532const unsigned entry_stride = (entry->src.file == FIXED_GRF ? 1 :533entry->src.stride);534if (instruction_requires_packed_data(inst) && entry_stride != 1)535return false;536537const brw_reg_type dst_type = (has_source_modifiers &&538entry->dst.type != inst->src[arg].type) ?539entry->dst.type : inst->dst.type;540541/* Bail if the result of composing both strides would exceed the542* hardware limit.543*/544if (!can_take_stride(inst, dst_type, arg,545entry_stride * inst->src[arg].stride,546devinfo))547return false;548549/* From the Cherry Trail/Braswell PRMs, Volume 7: 3D Media GPGPU:550* EU Overview551* Register Region Restrictions552* Special Requirements for Handling Double Precision Data Types :553*554* "When source or destination datatype is 64b or operation is integer555* DWord multiply, regioning in Align1 must follow these rules:556*557* 1. Source and Destination horizontal stride must be aligned to the558* same qword.559* 2. Regioning must ensure Src.Vstride = Src.Width * Src.Hstride.560* 3. Source and Destination offset must be the same, except the case561* of scalar source."562*563* Most of this is already checked in can_take_stride(), we're only left564* with checking 3.565*/566if (has_dst_aligned_region_restriction(devinfo, inst, dst_type) &&567entry_stride != 0 &&568(reg_offset(inst->dst) % REG_SIZE) != (reg_offset(entry->src) % REG_SIZE))569return false;570571/* Bail if the source FIXED_GRF region of the copy cannot be trivially572* composed with the source region of the instruction -- E.g. because the573* copy uses some extended stride greater than 4 not supported natively by574* the hardware as a horizontal stride, or because instruction compression575* could require us to use a vertical stride shorter than a GRF.576*/577if (entry->src.file == FIXED_GRF &&578(inst->src[arg].stride > 4 ||579inst->dst.component_size(inst->exec_size) >580inst->src[arg].component_size(inst->exec_size)))581return false;582583/* Bail if the instruction type is larger than the execution type of the584* copy, what implies that each channel is reading multiple channels of the585* destination of the copy, and simply replacing the sources would give a586* program with different semantics.587*/588if (type_sz(entry->dst.type) < type_sz(inst->src[arg].type))589return false;590591/* Bail if the result of composing both strides cannot be expressed592* as another stride. This avoids, for example, trying to transform593* this:594*595* MOV (8) rX<1>UD rY<0;1,0>UD596* FOO (8) ... rX<8;8,1>UW597*598* into this:599*600* FOO (8) ... rY<0;1,0>UW601*602* Which would have different semantics.603*/604if (entry_stride != 1 &&605(inst->src[arg].stride *606type_sz(inst->src[arg].type)) % type_sz(entry->src.type) != 0)607return false;608609/* Since semantics of source modifiers are type-dependent we need to610* ensure that the meaning of the instruction remains the same if we611* change the type. If the sizes of the types are different the new612* instruction will read a different amount of data than the original613* and the semantics will always be different.614*/615if (has_source_modifiers &&616entry->dst.type != inst->src[arg].type &&617(!inst->can_change_types() ||618type_sz(entry->dst.type) != type_sz(inst->src[arg].type)))619return false;620621if (devinfo->ver >= 8 && (entry->src.negate || entry->src.abs) &&622is_logic_op(inst->opcode)) {623return false;624}625626if (entry->saturate) {627switch(inst->opcode) {628case BRW_OPCODE_SEL:629if ((inst->conditional_mod != BRW_CONDITIONAL_GE &&630inst->conditional_mod != BRW_CONDITIONAL_L) ||631inst->src[1].file != IMM ||632inst->src[1].f < 0.0 ||633inst->src[1].f > 1.0) {634return false;635}636break;637default:638return false;639}640}641642/* Save the offset of inst->src[arg] relative to entry->dst for it to be643* applied later.644*/645const unsigned rel_offset = inst->src[arg].offset - entry->dst.offset;646647/* Fold the copy into the instruction consuming it. */648inst->src[arg].file = entry->src.file;649inst->src[arg].nr = entry->src.nr;650inst->src[arg].subnr = entry->src.subnr;651inst->src[arg].offset = entry->src.offset;652653/* Compose the strides of both regions. */654if (entry->src.file == FIXED_GRF) {655if (inst->src[arg].stride) {656const unsigned orig_width = 1 << entry->src.width;657const unsigned reg_width = REG_SIZE / (type_sz(inst->src[arg].type) *658inst->src[arg].stride);659inst->src[arg].width = cvt(MIN2(orig_width, reg_width)) - 1;660inst->src[arg].hstride = cvt(inst->src[arg].stride);661inst->src[arg].vstride = inst->src[arg].hstride + inst->src[arg].width;662} else {663inst->src[arg].vstride = inst->src[arg].hstride =664inst->src[arg].width = 0;665}666667inst->src[arg].stride = 1;668669/* Hopefully no Align16 around here... */670assert(entry->src.swizzle == BRW_SWIZZLE_XYZW);671inst->src[arg].swizzle = entry->src.swizzle;672} else {673inst->src[arg].stride *= entry->src.stride;674}675676/* Compose any saturate modifiers. */677inst->saturate = inst->saturate || entry->saturate;678679/* Compute the first component of the copy that the instruction is680* reading, and the base byte offset within that component.681*/682assert(entry->dst.offset % REG_SIZE == 0 && entry->dst.stride == 1);683const unsigned component = rel_offset / type_sz(entry->dst.type);684const unsigned suboffset = rel_offset % type_sz(entry->dst.type);685686/* Calculate the byte offset at the origin of the copy of the given687* component and suboffset.688*/689inst->src[arg] = byte_offset(inst->src[arg],690component * entry_stride * type_sz(entry->src.type) + suboffset);691692if (has_source_modifiers) {693if (entry->dst.type != inst->src[arg].type) {694/* We are propagating source modifiers from a MOV with a different695* type. If we got here, then we can just change the source and696* destination types of the instruction and keep going.697*/698assert(inst->can_change_types());699for (int i = 0; i < inst->sources; i++) {700inst->src[i].type = entry->dst.type;701}702inst->dst.type = entry->dst.type;703}704705if (!inst->src[arg].abs) {706inst->src[arg].abs = entry->src.abs;707inst->src[arg].negate ^= entry->src.negate;708}709}710711return true;712}713714715bool716fs_visitor::try_constant_propagate(fs_inst *inst, acp_entry *entry)717{718bool progress = false;719720if (entry->src.file != IMM)721return false;722if (type_sz(entry->src.type) > 4)723return false;724if (entry->saturate)725return false;726727for (int i = inst->sources - 1; i >= 0; i--) {728if (inst->src[i].file != VGRF)729continue;730731assert(entry->dst.file == VGRF);732if (inst->src[i].nr != entry->dst.nr)733continue;734735/* Bail if inst is reading a range that isn't contained in the range736* that entry is writing.737*/738if (!region_contained_in(inst->src[i], inst->size_read(i),739entry->dst, entry->size_written))740continue;741742/* If the type sizes don't match each channel of the instruction is743* either extracting a portion of the constant (which could be handled744* with some effort but the code below doesn't) or reading multiple745* channels of the source at once.746*/747if (type_sz(inst->src[i].type) != type_sz(entry->dst.type))748continue;749750fs_reg val = entry->src;751val.type = inst->src[i].type;752753if (inst->src[i].abs) {754if ((devinfo->ver >= 8 && is_logic_op(inst->opcode)) ||755!brw_abs_immediate(val.type, &val.as_brw_reg())) {756continue;757}758}759760if (inst->src[i].negate) {761if ((devinfo->ver >= 8 && is_logic_op(inst->opcode)) ||762!brw_negate_immediate(val.type, &val.as_brw_reg())) {763continue;764}765}766767switch (inst->opcode) {768case BRW_OPCODE_MOV:769case SHADER_OPCODE_LOAD_PAYLOAD:770case FS_OPCODE_PACK:771inst->src[i] = val;772progress = true;773break;774775case SHADER_OPCODE_INT_QUOTIENT:776case SHADER_OPCODE_INT_REMAINDER:777/* FINISHME: Promote non-float constants and remove this. */778if (devinfo->ver < 8)779break;780FALLTHROUGH;781case SHADER_OPCODE_POW:782/* Allow constant propagation into src1 (except on Gen 6 which783* doesn't support scalar source math), and let constant combining784* promote the constant on Gen < 8.785*/786if (devinfo->ver == 6)787break;788FALLTHROUGH;789case BRW_OPCODE_BFI1:790case BRW_OPCODE_ASR:791case BRW_OPCODE_SHL:792case BRW_OPCODE_SHR:793case BRW_OPCODE_SUBB:794if (i == 1) {795inst->src[i] = val;796progress = true;797}798break;799800case BRW_OPCODE_MACH:801case BRW_OPCODE_MUL:802case SHADER_OPCODE_MULH:803case BRW_OPCODE_ADD:804case BRW_OPCODE_OR:805case BRW_OPCODE_AND:806case BRW_OPCODE_XOR:807case BRW_OPCODE_ADDC:808if (i == 1) {809inst->src[i] = val;810progress = true;811} else if (i == 0 && inst->src[1].file != IMM) {812/* Fit this constant in by commuting the operands.813* Exception: we can't do this for 32-bit integer MUL/MACH814* because it's asymmetric.815*816* The BSpec says for Broadwell that817*818* "When multiplying DW x DW, the dst cannot be accumulator."819*820* Integer MUL with a non-accumulator destination will be lowered821* by lower_integer_multiplication(), so don't restrict it.822*/823if (((inst->opcode == BRW_OPCODE_MUL &&824inst->dst.is_accumulator()) ||825inst->opcode == BRW_OPCODE_MACH) &&826(inst->src[1].type == BRW_REGISTER_TYPE_D ||827inst->src[1].type == BRW_REGISTER_TYPE_UD))828break;829inst->src[0] = inst->src[1];830inst->src[1] = val;831progress = true;832}833break;834835case BRW_OPCODE_CMP:836case BRW_OPCODE_IF:837if (i == 1) {838inst->src[i] = val;839progress = true;840} else if (i == 0 && inst->src[1].file != IMM) {841enum brw_conditional_mod new_cmod;842843new_cmod = brw_swap_cmod(inst->conditional_mod);844if (new_cmod != BRW_CONDITIONAL_NONE) {845/* Fit this constant in by swapping the operands and846* flipping the test847*/848inst->src[0] = inst->src[1];849inst->src[1] = val;850inst->conditional_mod = new_cmod;851progress = true;852}853}854break;855856case BRW_OPCODE_SEL:857if (i == 1) {858inst->src[i] = val;859progress = true;860} else if (i == 0 && inst->src[1].file != IMM &&861(inst->conditional_mod == BRW_CONDITIONAL_NONE ||862/* Only GE and L are commutative. */863inst->conditional_mod == BRW_CONDITIONAL_GE ||864inst->conditional_mod == BRW_CONDITIONAL_L)) {865inst->src[0] = inst->src[1];866inst->src[1] = val;867868/* If this was predicated, flipping operands means869* we also need to flip the predicate.870*/871if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {872inst->predicate_inverse =873!inst->predicate_inverse;874}875progress = true;876}877break;878879case FS_OPCODE_FB_WRITE_LOGICAL:880/* The stencil and omask sources of FS_OPCODE_FB_WRITE_LOGICAL are881* bit-cast using a strided region so they cannot be immediates.882*/883if (i != FB_WRITE_LOGICAL_SRC_SRC_STENCIL &&884i != FB_WRITE_LOGICAL_SRC_OMASK) {885inst->src[i] = val;886progress = true;887}888break;889890case SHADER_OPCODE_TEX_LOGICAL:891case SHADER_OPCODE_TXD_LOGICAL:892case SHADER_OPCODE_TXF_LOGICAL:893case SHADER_OPCODE_TXL_LOGICAL:894case SHADER_OPCODE_TXS_LOGICAL:895case FS_OPCODE_TXB_LOGICAL:896case SHADER_OPCODE_TXF_CMS_LOGICAL:897case SHADER_OPCODE_TXF_CMS_W_LOGICAL:898case SHADER_OPCODE_TXF_UMS_LOGICAL:899case SHADER_OPCODE_TXF_MCS_LOGICAL:900case SHADER_OPCODE_LOD_LOGICAL:901case SHADER_OPCODE_TG4_LOGICAL:902case SHADER_OPCODE_TG4_OFFSET_LOGICAL:903case SHADER_OPCODE_SAMPLEINFO_LOGICAL:904case SHADER_OPCODE_IMAGE_SIZE_LOGICAL:905case SHADER_OPCODE_UNTYPED_ATOMIC_LOGICAL:906case SHADER_OPCODE_UNTYPED_ATOMIC_FLOAT_LOGICAL:907case SHADER_OPCODE_UNTYPED_SURFACE_READ_LOGICAL:908case SHADER_OPCODE_UNTYPED_SURFACE_WRITE_LOGICAL:909case SHADER_OPCODE_TYPED_ATOMIC_LOGICAL:910case SHADER_OPCODE_TYPED_SURFACE_READ_LOGICAL:911case SHADER_OPCODE_TYPED_SURFACE_WRITE_LOGICAL:912case SHADER_OPCODE_BYTE_SCATTERED_WRITE_LOGICAL:913case SHADER_OPCODE_BYTE_SCATTERED_READ_LOGICAL:914inst->src[i] = val;915progress = true;916break;917918case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:919case SHADER_OPCODE_BROADCAST:920inst->src[i] = val;921progress = true;922break;923924case BRW_OPCODE_MAD:925case BRW_OPCODE_LRP:926inst->src[i] = val;927progress = true;928break;929930default:931break;932}933}934935return progress;936}937938static bool939can_propagate_from(fs_inst *inst)940{941return (inst->opcode == BRW_OPCODE_MOV &&942inst->dst.file == VGRF &&943((inst->src[0].file == VGRF &&944!regions_overlap(inst->dst, inst->size_written,945inst->src[0], inst->size_read(0))) ||946inst->src[0].file == ATTR ||947inst->src[0].file == UNIFORM ||948inst->src[0].file == IMM ||949(inst->src[0].file == FIXED_GRF &&950inst->src[0].is_contiguous())) &&951inst->src[0].type == inst->dst.type &&952!inst->is_partial_write()) ||953is_identity_payload(FIXED_GRF, inst);954}955956/* Walks a basic block and does copy propagation on it using the acp957* list.958*/959bool960fs_visitor::opt_copy_propagation_local(void *copy_prop_ctx, bblock_t *block,961exec_list *acp)962{963bool progress = false;964965foreach_inst_in_block(fs_inst, inst, block) {966/* Try propagating into this instruction. */967for (int i = 0; i < inst->sources; i++) {968if (inst->src[i].file != VGRF)969continue;970971foreach_in_list(acp_entry, entry, &acp[inst->src[i].nr % ACP_HASH_SIZE]) {972if (try_constant_propagate(inst, entry))973progress = true;974else if (try_copy_propagate(inst, i, entry))975progress = true;976}977}978979/* kill the destination from the ACP */980if (inst->dst.file == VGRF || inst->dst.file == FIXED_GRF) {981foreach_in_list_safe(acp_entry, entry, &acp[inst->dst.nr % ACP_HASH_SIZE]) {982if (regions_overlap(entry->dst, entry->size_written,983inst->dst, inst->size_written))984entry->remove();985}986987/* Oops, we only have the chaining hash based on the destination, not988* the source, so walk across the entire table.989*/990for (int i = 0; i < ACP_HASH_SIZE; i++) {991foreach_in_list_safe(acp_entry, entry, &acp[i]) {992/* Make sure we kill the entry if this instruction overwrites993* _any_ of the registers that it reads994*/995if (regions_overlap(entry->src, entry->size_read,996inst->dst, inst->size_written))997entry->remove();998}999}1000}10011002/* If this instruction's source could potentially be folded into the1003* operand of another instruction, add it to the ACP.1004*/1005if (can_propagate_from(inst)) {1006acp_entry *entry = rzalloc(copy_prop_ctx, acp_entry);1007entry->dst = inst->dst;1008entry->src = inst->src[0];1009entry->size_written = inst->size_written;1010for (unsigned i = 0; i < inst->sources; i++)1011entry->size_read += inst->size_read(i);1012entry->opcode = inst->opcode;1013entry->saturate = inst->saturate;1014acp[entry->dst.nr % ACP_HASH_SIZE].push_tail(entry);1015} else if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD &&1016inst->dst.file == VGRF) {1017int offset = 0;1018for (int i = 0; i < inst->sources; i++) {1019int effective_width = i < inst->header_size ? 8 : inst->exec_size;1020assert(effective_width * type_sz(inst->src[i].type) % REG_SIZE == 0);1021const unsigned size_written = effective_width *1022type_sz(inst->src[i].type);1023if (inst->src[i].file == VGRF ||1024(inst->src[i].file == FIXED_GRF &&1025inst->src[i].is_contiguous())) {1026acp_entry *entry = rzalloc(copy_prop_ctx, acp_entry);1027entry->dst = byte_offset(inst->dst, offset);1028entry->src = inst->src[i];1029entry->size_written = size_written;1030entry->size_read = inst->size_read(i);1031entry->opcode = inst->opcode;1032if (!entry->dst.equals(inst->src[i])) {1033acp[entry->dst.nr % ACP_HASH_SIZE].push_tail(entry);1034} else {1035ralloc_free(entry);1036}1037}1038offset += size_written;1039}1040}1041}10421043return progress;1044}10451046bool1047fs_visitor::opt_copy_propagation()1048{1049bool progress = false;1050void *copy_prop_ctx = ralloc_context(NULL);1051exec_list *out_acp[cfg->num_blocks];10521053for (int i = 0; i < cfg->num_blocks; i++)1054out_acp[i] = new exec_list [ACP_HASH_SIZE];10551056const fs_live_variables &live = live_analysis.require();10571058/* First, walk through each block doing local copy propagation and getting1059* the set of copies available at the end of the block.1060*/1061foreach_block (block, cfg) {1062progress = opt_copy_propagation_local(copy_prop_ctx, block,1063out_acp[block->num]) || progress;10641065/* If the destination of an ACP entry exists only within this block,1066* then there's no need to keep it for dataflow analysis. We can delete1067* it from the out_acp table and avoid growing the bitsets any bigger1068* than we absolutely have to.1069*1070* Because nothing in opt_copy_propagation_local touches the block1071* start/end IPs and opt_copy_propagation_local is incapable of1072* extending the live range of an ACP destination beyond the block,1073* it's safe to use the liveness information in this way.1074*/1075for (unsigned a = 0; a < ACP_HASH_SIZE; a++) {1076foreach_in_list_safe(acp_entry, entry, &out_acp[block->num][a]) {1077assert(entry->dst.file == VGRF);1078if (block->start_ip <= live.vgrf_start[entry->dst.nr] &&1079live.vgrf_end[entry->dst.nr] <= block->end_ip)1080entry->remove();1081}1082}1083}10841085/* Do dataflow analysis for those available copies. */1086fs_copy_prop_dataflow dataflow(copy_prop_ctx, cfg, live, out_acp);10871088/* Next, re-run local copy propagation, this time with the set of copies1089* provided by the dataflow analysis available at the start of a block.1090*/1091foreach_block (block, cfg) {1092exec_list in_acp[ACP_HASH_SIZE];10931094for (int i = 0; i < dataflow.num_acp; i++) {1095if (BITSET_TEST(dataflow.bd[block->num].livein, i)) {1096struct acp_entry *entry = dataflow.acp[i];1097in_acp[entry->dst.nr % ACP_HASH_SIZE].push_tail(entry);1098}1099}11001101progress = opt_copy_propagation_local(copy_prop_ctx, block, in_acp) ||1102progress;1103}11041105for (int i = 0; i < cfg->num_blocks; i++)1106delete [] out_acp[i];1107ralloc_free(copy_prop_ctx);11081109if (progress)1110invalidate_analysis(DEPENDENCY_INSTRUCTION_DATA_FLOW |1111DEPENDENCY_INSTRUCTION_DETAIL);11121113return progress;1114}111511161117