2 * Copyright © 2013 Intel Corporation
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:
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
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
25 #include "brw_fs_live_variables.h"
30 /** @file brw_fs_saturate_propagation.cpp
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.
41 * would be transformed into
43 * ADD.SAT tmp, src0, src1
48 opt_saturate_propagation_local(const fs_live_variables
&live
, bblock_t
*block
)
50 bool progress
= false;
51 int ip
= block
->end_ip
+ 1;
53 foreach_inst_in_block_reverse(fs_inst
, inst
, block
) {
56 if (inst
->opcode
!= BRW_OPCODE_MOV
||
58 inst
->dst
.file
!= VGRF
||
59 inst
->dst
.type
!= inst
->src
[0].type
||
60 inst
->src
[0].file
!= VGRF
||
64 int src_var
= live
.var_from_reg(inst
->src
[0]);
65 int src_end_ip
= live
.end
[src_var
];
67 bool interfered
= false;
68 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
69 if (scan_inst
->exec_size
== inst
->exec_size
&&
70 regions_overlap(scan_inst
->dst
, scan_inst
->size_written
,
71 inst
->src
[0], inst
->size_read(0))) {
72 if (scan_inst
->is_partial_write() ||
73 (scan_inst
->dst
.type
!= inst
->dst
.type
&&
74 !scan_inst
->can_change_types()))
77 if (scan_inst
->saturate
) {
78 inst
->saturate
= false;
80 } else if (src_end_ip
== ip
|| inst
->dst
.equals(inst
->src
[0])) {
81 if (scan_inst
->can_do_saturate()) {
82 if (scan_inst
->dst
.type
!= inst
->dst
.type
) {
83 scan_inst
->dst
.type
= inst
->dst
.type
;
84 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
85 scan_inst
->src
[i
].type
= inst
->dst
.type
;
89 if (inst
->src
[0].negate
) {
90 if (scan_inst
->opcode
== BRW_OPCODE_MUL
) {
91 scan_inst
->src
[0].negate
= !scan_inst
->src
[0].negate
;
92 inst
->src
[0].negate
= false;
93 } else if (scan_inst
->opcode
== BRW_OPCODE_MAD
) {
94 for (int i
= 0; i
< 2; i
++) {
95 if (scan_inst
->src
[i
].file
== IMM
) {
96 brw_negate_immediate(scan_inst
->src
[i
].type
,
97 &scan_inst
->src
[i
].as_brw_reg());
99 scan_inst
->src
[i
].negate
= !scan_inst
->src
[i
].negate
;
102 inst
->src
[0].negate
= false;
103 } else if (scan_inst
->opcode
== BRW_OPCODE_ADD
) {
104 if (scan_inst
->src
[1].file
== IMM
) {
105 if (!brw_negate_immediate(scan_inst
->src
[1].type
,
106 &scan_inst
->src
[1].as_brw_reg())) {
110 scan_inst
->src
[1].negate
= !scan_inst
->src
[1].negate
;
112 scan_inst
->src
[0].negate
= !scan_inst
->src
[0].negate
;
113 inst
->src
[0].negate
= false;
119 scan_inst
->saturate
= true;
120 inst
->saturate
= false;
126 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
127 if (scan_inst
->src
[i
].file
== VGRF
&&
128 scan_inst
->src
[i
].nr
== inst
->src
[0].nr
&&
129 scan_inst
->src
[i
].offset
/ REG_SIZE
==
130 inst
->src
[0].offset
/ REG_SIZE
) {
131 if (scan_inst
->opcode
!= BRW_OPCODE_MOV
||
132 !scan_inst
->saturate
||
133 scan_inst
->src
[0].abs
||
134 scan_inst
->src
[0].negate
||
135 scan_inst
->src
[0].abs
!= inst
->src
[0].abs
||
136 scan_inst
->src
[0].negate
!= inst
->src
[0].negate
) {
152 fs_visitor::opt_saturate_propagation()
154 const fs_live_variables
&live
= live_analysis
.require();
155 bool progress
= false;
157 foreach_block (block
, cfg
) {
158 progress
= opt_saturate_propagation_local(live
, block
) || progress
;
161 /* Live intervals are still valid. */