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"
28 /** @file brw_fs_saturate_propagation.cpp
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.
39 * would be transformed into
41 * ADD.SAT tmp, src0, src1
46 opt_saturate_propagation_local(fs_visitor
*v
, bblock_t
*block
)
48 bool progress
= false;
49 int ip
= block
->end_ip
+ 1;
51 foreach_inst_in_block_reverse(fs_inst
, inst
, block
) {
54 if (inst
->opcode
!= BRW_OPCODE_MOV
||
56 inst
->dst
.file
!= VGRF
||
57 inst
->dst
.type
!= inst
->src
[0].type
||
58 inst
->src
[0].file
!= VGRF
||
62 int src_var
= v
->live_intervals
->var_from_reg(inst
->src
[0]);
63 int src_end_ip
= v
->live_intervals
->end
[src_var
];
65 bool interfered
= false;
66 foreach_inst_in_block_reverse_starting_from(fs_inst
, scan_inst
, inst
) {
67 if (scan_inst
->overwrites_reg(inst
->src
[0])) {
68 if (scan_inst
->is_partial_write() ||
69 (scan_inst
->dst
.type
!= inst
->dst
.type
&&
70 !scan_inst
->can_change_types()))
73 if (scan_inst
->saturate
) {
74 inst
->saturate
= false;
76 } else if (src_end_ip
== ip
|| inst
->dst
.equals(inst
->src
[0])) {
77 if (scan_inst
->can_do_saturate()) {
78 if (scan_inst
->dst
.type
!= inst
->dst
.type
) {
79 scan_inst
->dst
.type
= inst
->dst
.type
;
80 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
81 scan_inst
->src
[i
].type
= inst
->dst
.type
;
85 if (inst
->src
[0].negate
) {
86 if (scan_inst
->opcode
== BRW_OPCODE_MUL
) {
87 scan_inst
->src
[0].negate
= !scan_inst
->src
[0].negate
;
88 inst
->src
[0].negate
= false;
89 } else if (scan_inst
->opcode
== BRW_OPCODE_MAD
) {
90 scan_inst
->src
[0].negate
= !scan_inst
->src
[0].negate
;
91 scan_inst
->src
[1].negate
= !scan_inst
->src
[1].negate
;
92 inst
->src
[0].negate
= false;
93 } else if (scan_inst
->opcode
== BRW_OPCODE_ADD
) {
94 if (scan_inst
->src
[1].file
== IMM
) {
95 if (!brw_negate_immediate(scan_inst
->src
[1].type
,
96 &scan_inst
->src
[1].as_brw_reg())) {
100 scan_inst
->src
[1].negate
= !scan_inst
->src
[1].negate
;
102 scan_inst
->src
[0].negate
= !scan_inst
->src
[0].negate
;
103 inst
->src
[0].negate
= false;
109 scan_inst
->saturate
= true;
110 inst
->saturate
= false;
116 for (int i
= 0; i
< scan_inst
->sources
; i
++) {
117 if (scan_inst
->src
[i
].file
== VGRF
&&
118 scan_inst
->src
[i
].nr
== inst
->src
[0].nr
&&
119 scan_inst
->src
[i
].reg_offset
== inst
->src
[0].reg_offset
) {
120 if (scan_inst
->opcode
!= BRW_OPCODE_MOV
||
121 !scan_inst
->saturate
||
122 scan_inst
->src
[0].abs
||
123 scan_inst
->src
[0].negate
||
124 scan_inst
->src
[0].abs
!= inst
->src
[0].abs
||
125 scan_inst
->src
[0].negate
!= inst
->src
[0].negate
) {
141 fs_visitor::opt_saturate_propagation()
143 bool progress
= false;
145 calculate_live_intervals();
147 foreach_block (block
, cfg
) {
148 progress
= opt_saturate_propagation_local(this, block
) || progress
;
151 /* Live intervals are still valid. */