73c2a27c6eb6f4cc3db07ea36aec51ed03f8bf8f
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
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
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
26 * Rob Clark <robclark@freedesktop.org>
34 * TODO probably want some sort of visitor sort of interface to
35 * avoid duplicating the same graph traversal logic everywhere..
39 static void block_cp(struct ir3_block
*block
);
40 static struct ir3_instruction
* instr_cp(struct ir3_instruction
*instr
, bool keep
);
42 static bool is_eligible_mov(struct ir3_instruction
*instr
)
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
)
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
)))
58 static void walk_children(struct ir3_instruction
*instr
, bool keep
)
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
);
70 static struct ir3_instruction
*
71 instr_cp_fanin(struct ir3_instruction
*instr
)
75 /* we need to handle fanin specially, to detect cases
76 * when we need to keep a mov
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);
85 /* if the candidate is a fanout, then keep
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.
92 if (is_meta(cand
) && (cand
->opc
== OPC_META_FO
))
93 cand
= instr_cp(src
->instr
, true);
99 walk_children(instr
, false);
105 static struct ir3_instruction
*
106 instr_cp(struct ir3_instruction
*instr
, bool keep
)
108 /* if we've already visited this instruction, bail now: */
109 if (ir3_instr_check_mark(instr
))
112 if (is_meta(instr
) && (instr
->opc
== OPC_META_FI
))
113 return instr_cp_fanin(instr
);
115 if (is_eligible_mov(instr
) && !keep
) {
116 struct ir3_register
*src
= instr
->regs
[1];
117 return instr_cp(src
->instr
, false);
120 walk_children(instr
, false);
125 static void block_cp(struct ir3_block
*block
)
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);
134 /* To deal with things like this:
136 * 43: MOV OUT[2], TEMP[5]
137 * 44: MOV OUT[0], TEMP[5]
139 * we need to ensure that no two outputs point to
140 * the same instruction
142 for (j
= 0; j
< i
; j
++) {
143 if (block
->outputs
[j
] == out
) {
144 out
= instr_cp(block
->outputs
[i
], true);
149 block
->outputs
[i
] = out
;
154 void ir3_block_cp(struct ir3_block
*block
)
156 ir3_clear_mark(block
->shader
);