73c2a27c6eb6f4cc3db07ea36aec51ed03f8bf8f
[mesa.git] / src / gallium / drivers / freedreno / a3xx / ir3_cp.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 * Rob Clark <robclark@freedesktop.org>
27 */
28
29 #include "ir3.h"
30
31 /*
32 * Copy Propagate:
33 *
34 * TODO probably want some sort of visitor sort of interface to
35 * avoid duplicating the same graph traversal logic everywhere..
36 *
37 */
38
39 static void block_cp(struct ir3_block *block);
40 static struct ir3_instruction * instr_cp(struct ir3_instruction *instr, bool keep);
41
42 static bool is_eligible_mov(struct ir3_instruction *instr)
43 {
44 if ((instr->category == 1) &&
45 (instr->cat1.src_type == instr->cat1.dst_type)) {
46 struct ir3_register *dst = instr->regs[0];
47 struct ir3_register *src = instr->regs[1];
48 if (dst->flags & IR3_REG_ADDR)
49 return false;
50 if ((src->flags & IR3_REG_SSA) &&
51 /* TODO: propagate abs/neg modifiers if possible */
52 !(src->flags & (IR3_REG_ABS | IR3_REG_NEGATE | IR3_REG_RELATIV)))
53 return true;
54 }
55 return false;
56 }
57
58 static void walk_children(struct ir3_instruction *instr, bool keep)
59 {
60 unsigned i;
61
62 /* walk down the graph from each src: */
63 for (i = 1; i < instr->regs_count; i++) {
64 struct ir3_register *src = instr->regs[i];
65 if (src->flags & IR3_REG_SSA)
66 src->instr = instr_cp(src->instr, keep);
67 }
68 }
69
70 static struct ir3_instruction *
71 instr_cp_fanin(struct ir3_instruction *instr)
72 {
73 unsigned i;
74
75 /* we need to handle fanin specially, to detect cases
76 * when we need to keep a mov
77 */
78
79 for (i = 1; i < instr->regs_count; i++) {
80 struct ir3_register *src = instr->regs[i];
81 if (src->flags & IR3_REG_SSA) {
82 struct ir3_instruction *cand =
83 instr_cp(src->instr, false);
84
85 /* if the candidate is a fanout, then keep
86 * the move.
87 *
88 * This is a bit, um, fragile, but it should
89 * catch the extra mov's that the front-end
90 * puts in for us already in these cases.
91 */
92 if (is_meta(cand) && (cand->opc == OPC_META_FO))
93 cand = instr_cp(src->instr, true);
94
95 src->instr = cand;
96 }
97 }
98
99 walk_children(instr, false);
100
101 return instr;
102
103 }
104
105 static struct ir3_instruction *
106 instr_cp(struct ir3_instruction *instr, bool keep)
107 {
108 /* if we've already visited this instruction, bail now: */
109 if (ir3_instr_check_mark(instr))
110 return instr;
111
112 if (is_meta(instr) && (instr->opc == OPC_META_FI))
113 return instr_cp_fanin(instr);
114
115 if (is_eligible_mov(instr) && !keep) {
116 struct ir3_register *src = instr->regs[1];
117 return instr_cp(src->instr, false);
118 }
119
120 walk_children(instr, false);
121
122 return instr;
123 }
124
125 static void block_cp(struct ir3_block *block)
126 {
127 unsigned i, j;
128
129 for (i = 0; i < block->noutputs; i++) {
130 if (block->outputs[i]) {
131 struct ir3_instruction *out =
132 instr_cp(block->outputs[i], false);
133
134 /* To deal with things like this:
135 *
136 * 43: MOV OUT[2], TEMP[5]
137 * 44: MOV OUT[0], TEMP[5]
138 *
139 * we need to ensure that no two outputs point to
140 * the same instruction
141 */
142 for (j = 0; j < i; j++) {
143 if (block->outputs[j] == out) {
144 out = instr_cp(block->outputs[i], true);
145 break;
146 }
147 }
148
149 block->outputs[i] = out;
150 }
151 }
152 }
153
154 void ir3_block_cp(struct ir3_block *block)
155 {
156 ir3_clear_mark(block->shader);
157 block_cp(block);
158 }