r300/compiler: Introduce control flow instructions and refactor dataflow
[mesa.git] / src / mesa / drivers / dri / r300 / compiler / radeon_dataflow_swizzles.c
1 /*
2 * Copyright (C) 2009 Nicolai Haehnle.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28 #include "radeon_dataflow.h"
29
30 #include "radeon_compiler.h"
31 #include "radeon_swizzle.h"
32
33
34 static void rewrite_source(struct radeon_compiler * c,
35 struct rc_instruction * inst, unsigned src)
36 {
37 struct rc_swizzle_split split;
38 unsigned int tempreg = rc_find_free_temporary(c);
39 unsigned int usemask;
40
41 usemask = 0;
42 for(unsigned int chan = 0; chan < 4; ++chan) {
43 if (GET_SWZ(inst->I.SrcReg[src].Swizzle, chan) != RC_SWIZZLE_UNUSED)
44 usemask |= 1 << chan;
45 }
46
47 c->SwizzleCaps->Split(inst->I.SrcReg[src], usemask, &split);
48
49 for(unsigned int phase = 0; phase < split.NumPhases; ++phase) {
50 struct rc_instruction * mov = rc_insert_new_instruction(c, inst->Prev);
51 unsigned int phase_refmask;
52 unsigned int masked_negate;
53
54 mov->I.Opcode = RC_OPCODE_MOV;
55 mov->I.DstReg.File = RC_FILE_TEMPORARY;
56 mov->I.DstReg.Index = tempreg;
57 mov->I.DstReg.WriteMask = split.Phase[phase];
58 mov->I.SrcReg[0] = inst->I.SrcReg[src];
59
60 phase_refmask = 0;
61 for(unsigned int chan = 0; chan < 4; ++chan) {
62 if (!GET_BIT(split.Phase[phase], chan))
63 SET_SWZ(mov->I.SrcReg[0].Swizzle, chan, RC_SWIZZLE_UNUSED);
64 else
65 phase_refmask |= 1 << GET_SWZ(mov->I.SrcReg[0].Swizzle, chan);
66 }
67
68 phase_refmask &= RC_MASK_XYZW;
69
70 masked_negate = split.Phase[phase] & mov->I.SrcReg[0].Negate;
71 if (masked_negate == 0)
72 mov->I.SrcReg[0].Negate = 0;
73 else if (masked_negate == split.Phase[phase])
74 mov->I.SrcReg[0].Negate = RC_MASK_XYZW;
75
76 }
77
78 inst->I.SrcReg[src].File = RC_FILE_TEMPORARY;
79 inst->I.SrcReg[src].Index = tempreg;
80 inst->I.SrcReg[src].Swizzle = 0;
81 inst->I.SrcReg[src].Negate = RC_MASK_NONE;
82 inst->I.SrcReg[src].Abs = 0;
83 for(unsigned int chan = 0; chan < 4; ++chan) {
84 SET_SWZ(inst->I.SrcReg[src].Swizzle, chan,
85 GET_BIT(usemask, chan) ? chan : RC_SWIZZLE_UNUSED);
86 }
87 }
88
89 void rc_dataflow_swizzles(struct radeon_compiler * c)
90 {
91 struct rc_instruction * inst;
92
93 for(inst = c->Program.Instructions.Next; inst != &c->Program.Instructions; inst = inst->Next) {
94 const struct rc_opcode_info * opcode = rc_get_opcode_info(inst->I.Opcode);
95 unsigned int src;
96
97 for(src = 0; src < opcode->NumSrcRegs; ++src) {
98 if (!c->SwizzleCaps->IsNative(inst->I.Opcode, inst->I.SrcReg[src]))
99 rewrite_source(c, inst, src);
100 }
101 }
102 }