i965/fs: Loop from 0 to inst->sources, not 0 to 3.
[mesa.git] / src / mesa / drivers / dri / i965 / 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
31 static bool
32 opt_saturate_propagation_local(fs_visitor *v, bblock_t *block)
33 {
34 bool progress = false;
35 int ip = block->start_ip - 1;
36
37 for (fs_inst *inst = (fs_inst *)block->start;
38 inst != block->end->next;
39 inst = (fs_inst *) inst->next) {
40 ip++;
41
42 if (inst->opcode != BRW_OPCODE_MOV ||
43 inst->dst.file != GRF ||
44 inst->src[0].file != GRF ||
45 inst->src[0].abs ||
46 inst->src[0].negate ||
47 !inst->saturate)
48 continue;
49
50 int src_var = v->live_intervals->var_from_reg(&inst->src[0]);
51 int src_end_ip = v->live_intervals->end[src_var];
52 if (src_end_ip > ip && !inst->dst.equals(inst->src[0]))
53 continue;
54
55 int scan_ip = ip;
56 bool interfered = false;
57 for (fs_inst *scan_inst = (fs_inst *) inst->prev;
58 scan_inst != block->start->prev;
59 scan_inst = (fs_inst *) scan_inst->prev) {
60 scan_ip--;
61
62 if (scan_inst->dst.file == GRF &&
63 scan_inst->dst.reg == inst->src[0].reg &&
64 scan_inst->dst.reg_offset == inst->src[0].reg_offset &&
65 !scan_inst->is_partial_write()) {
66 if (scan_inst->can_do_saturate()) {
67 scan_inst->saturate = true;
68 inst->saturate = false;
69 progress = true;
70 }
71 break;
72 }
73 for (int i = 0; i < scan_inst->sources; i++) {
74 if (scan_inst->src[i].file == GRF &&
75 scan_inst->src[i].reg == inst->src[0].reg &&
76 scan_inst->src[i].reg_offset == inst->src[0].reg_offset) {
77 interfered = true;
78 break;
79 }
80 }
81
82 if (interfered)
83 break;
84 }
85 }
86
87 return progress;
88 }
89
90 bool
91 fs_visitor::opt_saturate_propagation()
92 {
93 bool progress = false;
94
95 calculate_live_intervals();
96
97 cfg_t cfg(&instructions);
98
99 for (int b = 0; b < cfg.num_blocks; b++) {
100 progress = opt_saturate_propagation_local(this, cfg.blocks[b])
101 || progress;
102 }
103
104 if (progress)
105 invalidate_live_intervals();
106
107 return progress;
108 }