1 /*
2  * Copyright © 2012 Intel Corporation
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
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #include "brw_fs.h"
25 #include "brw_fs_cfg.h"
26 
27 namespace { /* avoid conflict with opt_copy_propagation_elements */
28 struct acp_entry : public exec_node {
29    fs_reg dst;
30    fs_reg src;
31 };
32 }
33 
34 bool
try_copy_propagate(fs_inst * inst,int arg,acp_entry * entry)35 fs_visitor::try_copy_propagate(fs_inst *inst, int arg, acp_entry *entry)
36 {
37    if (inst->src[arg].file != entry->dst.file ||
38        inst->src[arg].reg != entry->dst.reg ||
39        inst->src[arg].reg_offset != entry->dst.reg_offset) {
40       return false;
41    }
42 
43    /* See resolve_ud_negate() and comment in brw_fs_emit.cpp. */
44    if (inst->conditional_mod &&
45        inst->src[arg].type == BRW_REGISTER_TYPE_UD &&
46        entry->src.negate)
47       return false;
48 
49    bool has_source_modifiers = entry->src.abs || entry->src.negate;
50 
51    if (intel->gen == 6 && inst->is_math() &&
52        (has_source_modifiers || entry->src.file == UNIFORM))
53       return false;
54 
55    inst->src[arg].file = entry->src.file;
56    inst->src[arg].reg = entry->src.reg;
57    inst->src[arg].reg_offset = entry->src.reg_offset;
58 
59    if (!inst->src[arg].abs) {
60       inst->src[arg].abs = entry->src.abs;
61       inst->src[arg].negate ^= entry->src.negate;
62    }
63 
64    return true;
65 }
66 
67 /** @file brw_fs_copy_propagation.cpp
68  *
69  * Support for local copy propagation by walking the list of instructions
70  * and maintaining the ACP table of available copies for propagation.
71  *
72  * See Muchnik's Advanced Compiler Design and Implementation, section
73  * 12.5 (p356).
74  */
75 
76 /* Walks a basic block and does copy propagation on it using the acp
77  * list.
78  */
79 bool
opt_copy_propagate_local(void * mem_ctx,fs_bblock * block,exec_list * acp)80 fs_visitor::opt_copy_propagate_local(void *mem_ctx,
81 				     fs_bblock *block, exec_list *acp)
82 {
83    bool progress = false;
84 
85    for (fs_inst *inst = block->start;
86 	inst != block->end->next;
87 	inst = (fs_inst *)inst->next) {
88 
89       /* Try propagating into this instruction. */
90       foreach_list(entry_node, acp) {
91 	 acp_entry *entry = (acp_entry *)entry_node;
92 
93 	 for (int i = 0; i < 3; i++) {
94 	    if (try_copy_propagate(inst, i, entry))
95 	       progress = true;
96 	 }
97       }
98 
99       /* kill the destination from the ACP */
100       if (inst->dst.file == GRF) {
101 	 foreach_list_safe(entry_node, acp) {
102 	    acp_entry *entry = (acp_entry *)entry_node;
103 
104 	    if (inst->overwrites_reg(entry->dst) ||
105                 inst->overwrites_reg(entry->src)) {
106 	       entry->remove();
107 	    }
108 	 }
109       }
110 
111       /* If this instruction is a raw copy, add it to the ACP. */
112       if (inst->opcode == BRW_OPCODE_MOV &&
113 	  inst->dst.file == GRF &&
114 	  ((inst->src[0].file == GRF &&
115 	    (inst->src[0].reg != inst->dst.reg ||
116 	     inst->src[0].reg_offset != inst->dst.reg_offset)) ||
117 	   inst->src[0].file == UNIFORM) &&
118 	  inst->src[0].type == inst->dst.type &&
119 	  !inst->saturate &&
120 	  !inst->predicated &&
121 	  !inst->force_uncompressed &&
122 	  !inst->force_sechalf &&
123 	  inst->src[0].smear == -1) {
124 	 acp_entry *entry = ralloc(mem_ctx, acp_entry);
125 	 entry->dst = inst->dst;
126 	 entry->src = inst->src[0];
127 	 acp->push_tail(entry);
128       }
129    }
130 
131    return progress;
132 }
133 
134 bool
opt_copy_propagate()135 fs_visitor::opt_copy_propagate()
136 {
137    bool progress = false;
138    void *mem_ctx = ralloc_context(this->mem_ctx);
139 
140    fs_cfg cfg(this);
141 
142    for (int b = 0; b < cfg.num_blocks; b++) {
143       fs_bblock *block = cfg.blocks[b];
144       exec_list acp;
145 
146       progress = opt_copy_propagate_local(mem_ctx, block, &acp) || progress;
147    }
148 
149    ralloc_free(mem_ctx);
150 
151    if (progress)
152       live_intervals_valid = false;
153 
154    return progress;
155 }
156