intel/compiler: split is_partial_write() into two variants
[mesa.git] / src / intel / compiler / brw_fs_saturate_propagation.cpp
1 /*
2 * Copyright © 2013 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_live_variables.h"
26 #include "brw_cfg.h"
27
28 /** @file brw_fs_saturate_propagation.cpp
29 *
30 * Implements a pass that propagates the SAT modifier from a MOV.SAT into the
31 * instruction that produced the source of the MOV.SAT, thereby allowing the
32 * MOV's src and dst to be coalesced and the MOV removed.
33 *
34 * For instance,
35 *
36 * ADD tmp, src0, src1
37 * MOV.SAT dst, tmp
38 *
39 * would be transformed into
40 *
41 * ADD.SAT tmp, src0, src1
42 * MOV dst, tmp
43 */
44
45 static bool
46 opt_saturate_propagation_local(fs_visitor *v, bblock_t *block,
47 unsigned dispatch_width)
48 {
49 bool progress = false;
50 int ip = block->end_ip + 1;
51
52 foreach_inst_in_block_reverse(fs_inst, inst, block) {
53 ip--;
54
55 if (inst->opcode != BRW_OPCODE_MOV ||
56 !inst->saturate ||
57 inst->dst.file != VGRF ||
58 inst->dst.type != inst->src[0].type ||
59 inst->src[0].file != VGRF ||
60 inst->src[0].abs)
61 continue;
62
63 int src_var = v->live_intervals->var_from_reg(inst->src[0]);
64 int src_end_ip = v->live_intervals->end[src_var];
65
66 bool interfered = false;
67 foreach_inst_in_block_reverse_starting_from(fs_inst, scan_inst, inst) {
68 if (regions_overlap(scan_inst->dst, scan_inst->size_written,
69 inst->src[0], inst->size_read(0))) {
70 if (scan_inst->is_partial_var_write(dispatch_width) ||
71 (scan_inst->dst.type != inst->dst.type &&
72 !scan_inst->can_change_types()))
73 break;
74
75 if (scan_inst->saturate) {
76 inst->saturate = false;
77 progress = true;
78 } else if (src_end_ip == ip || inst->dst.equals(inst->src[0])) {
79 if (scan_inst->can_do_saturate()) {
80 if (scan_inst->dst.type != inst->dst.type) {
81 scan_inst->dst.type = inst->dst.type;
82 for (int i = 0; i < scan_inst->sources; i++) {
83 scan_inst->src[i].type = inst->dst.type;
84 }
85 }
86
87 if (inst->src[0].negate) {
88 if (scan_inst->opcode == BRW_OPCODE_MUL) {
89 scan_inst->src[0].negate = !scan_inst->src[0].negate;
90 inst->src[0].negate = false;
91 } else if (scan_inst->opcode == BRW_OPCODE_MAD) {
92 for (int i = 0; i < 2; i++) {
93 if (scan_inst->src[i].file == IMM) {
94 brw_negate_immediate(scan_inst->src[i].type,
95 &scan_inst->src[i].as_brw_reg());
96 } else {
97 scan_inst->src[i].negate = !scan_inst->src[i].negate;
98 }
99 }
100 inst->src[0].negate = false;
101 } else if (scan_inst->opcode == BRW_OPCODE_ADD) {
102 if (scan_inst->src[1].file == IMM) {
103 if (!brw_negate_immediate(scan_inst->src[1].type,
104 &scan_inst->src[1].as_brw_reg())) {
105 break;
106 }
107 } else {
108 scan_inst->src[1].negate = !scan_inst->src[1].negate;
109 }
110 scan_inst->src[0].negate = !scan_inst->src[0].negate;
111 inst->src[0].negate = false;
112 } else {
113 break;
114 }
115 }
116
117 scan_inst->saturate = true;
118 inst->saturate = false;
119 progress = true;
120 }
121 }
122 break;
123 }
124 for (int i = 0; i < scan_inst->sources; i++) {
125 if (scan_inst->src[i].file == VGRF &&
126 scan_inst->src[i].nr == inst->src[0].nr &&
127 scan_inst->src[i].offset / REG_SIZE ==
128 inst->src[0].offset / REG_SIZE) {
129 if (scan_inst->opcode != BRW_OPCODE_MOV ||
130 !scan_inst->saturate ||
131 scan_inst->src[0].abs ||
132 scan_inst->src[0].negate ||
133 scan_inst->src[0].abs != inst->src[0].abs ||
134 scan_inst->src[0].negate != inst->src[0].negate) {
135 interfered = true;
136 break;
137 }
138 }
139 }
140
141 if (interfered)
142 break;
143 }
144 }
145
146 return progress;
147 }
148
149 bool
150 fs_visitor::opt_saturate_propagation()
151 {
152 bool progress = false;
153
154 calculate_live_intervals();
155
156 foreach_block (block, cfg) {
157 progress = opt_saturate_propagation_local(this, block, dispatch_width) || progress;
158 }
159
160 /* Live intervals are still valid. */
161
162 return progress;
163 }