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 "elk_fs.h"
25 #include "elk_fs_live_variables.h"
26 #include "elk_cfg.h"
27
28 using namespace elk;
29
30 /** @file elk_fs_saturate_propagation.cpp
31 *
32 * Implements a pass that propagates the SAT modifier from a MOV.SAT into the
33 * instruction that produced the source of the MOV.SAT, thereby allowing the
34 * MOV's src and dst to be coalesced and the MOV removed.
35 *
36 * For instance,
37 *
38 * ADD tmp, src0, src1
39 * MOV.SAT dst, tmp
40 *
41 * would be transformed into
42 *
43 * ADD.SAT tmp, src0, src1
44 * MOV dst, tmp
45 */
46
47 static bool
opt_saturate_propagation_local(const intel_device_info * devinfo,const fs_live_variables & live,elk_bblock_t * block)48 opt_saturate_propagation_local(const intel_device_info *devinfo,
49 const fs_live_variables &live, elk_bblock_t *block)
50 {
51 bool progress = false;
52 int ip = block->end_ip + 1;
53
54 foreach_inst_in_block_reverse(elk_fs_inst, inst, block) {
55 ip--;
56
57 if (inst->opcode != ELK_OPCODE_MOV ||
58 !inst->saturate ||
59 inst->dst.file != VGRF ||
60 inst->dst.type != inst->src[0].type ||
61 inst->src[0].file != VGRF ||
62 inst->src[0].abs)
63 continue;
64
65 int src_var = live.var_from_reg(inst->src[0]);
66 int src_end_ip = live.end[src_var];
67
68 bool interfered = false;
69 foreach_inst_in_block_reverse_starting_from(elk_fs_inst, scan_inst, inst) {
70 if (scan_inst->exec_size == inst->exec_size &&
71 regions_overlap(scan_inst->dst, scan_inst->size_written,
72 inst->src[0], inst->size_read(0))) {
73 if (scan_inst->is_partial_write() ||
74 (scan_inst->dst.type != inst->dst.type &&
75 !scan_inst->can_change_types()))
76 break;
77
78 /* min and max pseudo ops modify the flags on Gfx4 and Gfx5, but
79 * it's not based on the result of the operation. This is the one
80 * case where it is always safe to propagate a saturate to an
81 * instruction that writes the flags.
82 */
83 if (scan_inst->flags_written(devinfo) != 0 &&
84 scan_inst->opcode != ELK_OPCODE_SEL) {
85 break;
86 }
87
88 if (scan_inst->saturate) {
89 inst->saturate = false;
90 progress = true;
91 } else if (src_end_ip == ip || inst->dst.equals(inst->src[0])) {
92 if (scan_inst->can_do_saturate()) {
93 if (scan_inst->dst.type != inst->dst.type) {
94 scan_inst->dst.type = inst->dst.type;
95 for (int i = 0; i < scan_inst->sources; i++) {
96 scan_inst->src[i].type = inst->dst.type;
97 }
98 }
99
100 if (inst->src[0].negate) {
101 if (scan_inst->opcode == ELK_OPCODE_MUL) {
102 scan_inst->src[0].negate = !scan_inst->src[0].negate;
103 inst->src[0].negate = false;
104 } else if (scan_inst->opcode == ELK_OPCODE_MAD) {
105 for (int i = 0; i < 2; i++) {
106 if (scan_inst->src[i].file == IMM) {
107 elk_negate_immediate(scan_inst->src[i].type,
108 &scan_inst->src[i].as_elk_reg());
109 } else {
110 scan_inst->src[i].negate = !scan_inst->src[i].negate;
111 }
112 }
113 inst->src[0].negate = false;
114 } else if (scan_inst->opcode == ELK_OPCODE_ADD) {
115 if (scan_inst->src[1].file == IMM) {
116 if (!elk_negate_immediate(scan_inst->src[1].type,
117 &scan_inst->src[1].as_elk_reg())) {
118 break;
119 }
120 } else {
121 scan_inst->src[1].negate = !scan_inst->src[1].negate;
122 }
123 scan_inst->src[0].negate = !scan_inst->src[0].negate;
124 inst->src[0].negate = false;
125 } else {
126 break;
127 }
128 }
129
130 scan_inst->saturate = true;
131 inst->saturate = false;
132 progress = true;
133 }
134 }
135 break;
136 }
137 for (int i = 0; i < scan_inst->sources; i++) {
138 if (scan_inst->src[i].file == VGRF &&
139 scan_inst->src[i].nr == inst->src[0].nr &&
140 regions_overlap(
141 scan_inst->src[i], scan_inst->size_read(i),
142 inst->src[0], inst->size_read(0))) {
143 if (scan_inst->opcode != ELK_OPCODE_MOV ||
144 !scan_inst->saturate ||
145 scan_inst->src[0].abs ||
146 scan_inst->src[0].negate ||
147 scan_inst->src[0].abs != inst->src[0].abs ||
148 scan_inst->src[0].negate != inst->src[0].negate) {
149 interfered = true;
150 break;
151 }
152 }
153 }
154
155 if (interfered)
156 break;
157 }
158 }
159
160 return progress;
161 }
162
163 bool
opt_saturate_propagation()164 elk_fs_visitor::opt_saturate_propagation()
165 {
166 const fs_live_variables &live = live_analysis.require();
167 bool progress = false;
168
169 foreach_block (block, cfg) {
170 progress = opt_saturate_propagation_local(devinfo, live, block) || progress;
171 }
172
173 /* Live intervals are still valid. */
174
175 return progress;
176 }
177