786511204a3ff2775d912582fad507995bdaf28d
[mesa.git] / src / mesa / pipe / llvm / storagesoa.cpp
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "storagesoa.h"
29
30
31 #include "pipe/p_shader_tokens.h"
32 #include <llvm/BasicBlock.h>
33 #include <llvm/Module.h>
34 #include <llvm/Value.h>
35
36 #include <llvm/CallingConv.h>
37 #include <llvm/Constants.h>
38 #include <llvm/DerivedTypes.h>
39 #include <llvm/InstrTypes.h>
40 #include <llvm/Instructions.h>
41
42 using namespace llvm;
43
44 StorageSoa::StorageSoa(llvm::BasicBlock *block,
45 llvm::Value *input,
46 llvm::Value *output,
47 llvm::Value *consts)
48 : m_block(block),
49 m_input(input),
50 m_output(output),
51 m_consts(consts),
52 m_idx(0)
53 {
54 }
55
56 void StorageSoa::addImmediate(float *vec)
57 {
58 }
59
60 llvm::Value *StorageSoa::addrElement(int idx) const
61 {
62 return 0;
63 }
64
65 std::vector<llvm::Value*> StorageSoa::inputElement(int idx, int swizzle,
66 llvm::Value *indIdx)
67 {
68 std::vector<llvm::Value*> res(4);
69
70 res[0] = element(m_input, idx, 0);
71 res[1] = element(m_input, idx, 0);
72 res[2] = element(m_input, idx, 0);
73 res[3] = element(m_input, idx, 0);
74
75 return res;
76 }
77
78 std::vector<llvm::Value*> StorageSoa::constElement(int idx, int swizzle,
79 llvm::Value *indIdx)
80 {
81 std::vector<llvm::Value*> res(4);
82
83 return res;
84 }
85
86 std::vector<llvm::Value*> StorageSoa::outputElement(int idx, int swizzle,
87 llvm::Value *indIdx)
88 {
89 std::vector<llvm::Value*> res(4);
90
91 res[0] = element(m_output, idx, 0);
92 res[1] = element(m_output, idx, 0);
93 res[2] = element(m_output, idx, 0);
94 res[3] = element(m_output, idx, 0);
95
96 return res;
97 }
98
99 std::vector<llvm::Value*> StorageSoa::tempElement(int idx, int swizzle,
100 llvm::Value *indIdx)
101 {
102 std::vector<llvm::Value*> res(4);
103
104 return res;
105 }
106
107 std::vector<llvm::Value*> StorageSoa::immediateElement(int idx, int swizzle)
108 {
109 std::vector<llvm::Value*> res(4);
110
111 return res;
112 }
113
114 llvm::Value * StorageSoa::extractIndex(llvm::Value *vec)
115 {
116 return 0;
117 }
118
119 void StorageSoa::storeOutput(int dstIdx, const std::vector<llvm::Value*> &val,
120 int mask)
121 {
122 if (mask != TGSI_WRITEMASK_XYZW) {
123 fprintf(stderr, "requires swizzle!!\n");
124 assert(0);
125 } else {
126 llvm::Value *xChannel = elementPointer(m_output, dstIdx, 0);
127 llvm::Value *yChannel = elementPointer(m_output, dstIdx, 1);
128 llvm::Value *zChannel = elementPointer(m_output, dstIdx, 2);
129 llvm::Value *wChannel = elementPointer(m_output, dstIdx, 3);
130
131 StoreInst *st = new StoreInst(val[0], xChannel, false, m_block);
132 st = new StoreInst(val[1], yChannel, false, m_block);
133 st = new StoreInst(val[2], zChannel, false, m_block);
134 st = new StoreInst(val[3], wChannel, false, m_block);
135 }
136 }
137
138 void StorageSoa::storeTemp(int idx, const std::vector<llvm::Value*> &val,
139 int mask)
140 {
141 }
142
143 void StorageSoa::storeAddress(int idx, const std::vector<llvm::Value*> &val,
144 int mask)
145 {
146 }
147
148 llvm::Value * StorageSoa::elementPointer(llvm::Value *ptr, int index,
149 int channel) const
150 {
151 std::vector<Value*> indices;
152 indices.push_back(constantInt(index));
153 indices.push_back(constantInt(channel));
154
155 GetElementPtrInst *getElem = new GetElementPtrInst(ptr,
156 indices.begin(),
157 indices.end(),
158 name("ptr"),
159 m_block);
160 return getElem;
161 }
162
163 llvm::Value * StorageSoa::element(llvm::Value *ptr, int index,
164 int channel) const
165 {
166 llvm::Value *res = elementPointer(ptr, index, channel);
167 LoadInst *load = new LoadInst(res, name("element"), false, m_block);
168 //load->setAlignment(8);
169 return load;
170 }
171
172 const char * StorageSoa::name(const char *prefix) const
173 {
174 ++m_idx;
175 snprintf(m_name, 32, "%s%d", prefix, m_idx);
176 return m_name;
177 }
178
179 llvm::ConstantInt * StorageSoa::constantInt(int idx) const
180 {
181 if (m_constInts.find(idx) != m_constInts.end()) {
182 return m_constInts[idx];
183 }
184 ConstantInt *constInt = ConstantInt::get(APInt(32, idx));
185 m_constInts[idx] = constInt;
186 return constInt;
187 }