• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
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 VMWARE 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 
29 /**
30  * @file
31  * Helper functions for manipulation structures.
32  *
33  * @author Jose Fonseca <jfonseca@vmware.com>
34  */
35 
36 
37 #include "util/u_debug.h"
38 #include "util/u_memory.h"
39 
40 #include "lp_bld_const.h"
41 #include "lp_bld_debug.h"
42 #include "lp_bld_struct.h"
43 
44 
45 LLVMValueRef
lp_build_struct_get_ptr(struct gallivm_state * gallivm,LLVMValueRef ptr,unsigned member,const char * name)46 lp_build_struct_get_ptr(struct gallivm_state *gallivm,
47                         LLVMValueRef ptr,
48                         unsigned member,
49                         const char *name)
50 {
51    LLVMValueRef indices[2];
52    LLVMValueRef member_ptr;
53    assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
54    assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
55 
56    indices[0] = lp_build_const_int32(gallivm, 0);
57    indices[1] = lp_build_const_int32(gallivm, member);
58    member_ptr = LLVMBuildGEP(gallivm->builder, ptr, indices, ARRAY_SIZE(indices), "");
59    lp_build_name(member_ptr, "%s.%s_ptr", LLVMGetValueName(ptr), name);
60    return member_ptr;
61 }
62 
63 LLVMValueRef
lp_build_struct_get(struct gallivm_state * gallivm,LLVMValueRef ptr,unsigned member,const char * name)64 lp_build_struct_get(struct gallivm_state *gallivm,
65                     LLVMValueRef ptr,
66                     unsigned member,
67                     const char *name)
68 {
69    LLVMValueRef member_ptr;
70    LLVMValueRef res;
71    assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
72    assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
73    member_ptr = lp_build_struct_get_ptr(gallivm, ptr, member, name);
74    res = LLVMBuildLoad(gallivm->builder, member_ptr, "");
75    lp_build_name(res, "%s.%s", LLVMGetValueName(ptr), name);
76    return res;
77 }
78 
79 LLVMValueRef
lp_build_struct_get_ptr2(struct gallivm_state * gallivm,LLVMTypeRef ptr_type,LLVMValueRef ptr,unsigned member,const char * name)80 lp_build_struct_get_ptr2(struct gallivm_state *gallivm,
81                         LLVMTypeRef ptr_type,
82                         LLVMValueRef ptr,
83                         unsigned member,
84                         const char *name)
85 {
86    LLVMValueRef indices[2];
87    LLVMValueRef member_ptr;
88    assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
89    assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
90 
91    indices[0] = lp_build_const_int32(gallivm, 0);
92    indices[1] = lp_build_const_int32(gallivm, member);
93    member_ptr = LLVMBuildGEP2(gallivm->builder, ptr_type, ptr, indices, ARRAY_SIZE(indices), "");
94    lp_build_name(member_ptr, "%s.%s_ptr", LLVMGetValueName(ptr), name);
95    return member_ptr;
96 }
97 
98 LLVMValueRef
lp_build_struct_get2(struct gallivm_state * gallivm,LLVMTypeRef ptr_type,LLVMValueRef ptr,unsigned member,const char * name)99 lp_build_struct_get2(struct gallivm_state *gallivm,
100                     LLVMTypeRef ptr_type,
101                     LLVMValueRef ptr,
102                     unsigned member,
103                     const char *name)
104 {
105    LLVMValueRef member_ptr;
106    LLVMValueRef res;
107    assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
108    assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
109    member_ptr = lp_build_struct_get_ptr2(gallivm, ptr_type, ptr, member, name);
110    res = LLVMBuildLoad(gallivm->builder, member_ptr, "");
111    lp_build_name(res, "%s.%s", LLVMGetValueName(ptr), name);
112    return res;
113 }
114 
115 LLVMValueRef
lp_build_array_get_ptr(struct gallivm_state * gallivm,LLVMValueRef ptr,LLVMValueRef index)116 lp_build_array_get_ptr(struct gallivm_state *gallivm,
117                        LLVMValueRef ptr,
118                        LLVMValueRef index)
119 {
120    LLVMValueRef indices[2];
121    LLVMValueRef element_ptr;
122    assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
123    assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind);
124    indices[0] = lp_build_const_int32(gallivm, 0);
125    indices[1] = index;
126    element_ptr = LLVMBuildGEP(gallivm->builder, ptr, indices, ARRAY_SIZE(indices), "");
127 #ifdef DEBUG
128    lp_build_name(element_ptr, "&%s[%s]",
129                  LLVMGetValueName(ptr), LLVMGetValueName(index));
130 #endif
131    return element_ptr;
132 }
133 
134 
135 LLVMValueRef
lp_build_array_get(struct gallivm_state * gallivm,LLVMValueRef ptr,LLVMValueRef index)136 lp_build_array_get(struct gallivm_state *gallivm,
137                    LLVMValueRef ptr,
138                    LLVMValueRef index)
139 {
140    LLVMValueRef element_ptr;
141    LLVMValueRef res;
142    assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
143    assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind);
144    element_ptr = lp_build_array_get_ptr(gallivm, ptr, index);
145    res = LLVMBuildLoad(gallivm->builder, element_ptr, "");
146 #ifdef DEBUG
147    lp_build_name(res, "%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index));
148 #endif
149    return res;
150 }
151 
152 
153 void
lp_build_array_set(struct gallivm_state * gallivm,LLVMValueRef ptr,LLVMValueRef index,LLVMValueRef value)154 lp_build_array_set(struct gallivm_state *gallivm,
155                    LLVMValueRef ptr,
156                    LLVMValueRef index,
157                    LLVMValueRef value)
158 {
159    LLVMValueRef element_ptr;
160    assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
161    assert(LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind);
162    element_ptr = lp_build_array_get_ptr(gallivm, ptr, index);
163    LLVMBuildStore(gallivm->builder, value, element_ptr);
164 }
165 
166 
167 LLVMValueRef
lp_build_pointer_get(LLVMBuilderRef builder,LLVMValueRef ptr,LLVMValueRef index)168 lp_build_pointer_get(LLVMBuilderRef builder,
169                      LLVMValueRef ptr,
170                      LLVMValueRef index)
171 {
172    return lp_build_pointer_get_unaligned(builder, ptr, index, 0);
173 }
174 
175 
176 LLVMValueRef
lp_build_pointer_get_unaligned(LLVMBuilderRef builder,LLVMValueRef ptr,LLVMValueRef index,unsigned alignment)177 lp_build_pointer_get_unaligned(LLVMBuilderRef builder,
178                                LLVMValueRef ptr,
179                                LLVMValueRef index,
180                                unsigned alignment)
181 {
182    LLVMValueRef element_ptr;
183    LLVMValueRef res;
184    assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
185    element_ptr = LLVMBuildGEP(builder, ptr, &index, 1, "");
186    res = LLVMBuildLoad(builder, element_ptr, "");
187    if (alignment)
188       LLVMSetAlignment(res, alignment);
189 #ifdef DEBUG
190    lp_build_name(res, "%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index));
191 #endif
192    return res;
193 }
194 
195 
196 void
lp_build_pointer_set(LLVMBuilderRef builder,LLVMValueRef ptr,LLVMValueRef index,LLVMValueRef value)197 lp_build_pointer_set(LLVMBuilderRef builder,
198                      LLVMValueRef ptr,
199                      LLVMValueRef index,
200                      LLVMValueRef value)
201 {
202    LLVMValueRef element_ptr;
203    element_ptr = LLVMBuildGEP2(builder, LLVMTypeOf(value), ptr, &index, 1, "");
204    LLVMBuildStore(builder, value, element_ptr);
205 }
206 
207 
208 void
lp_build_pointer_set_unaligned(LLVMBuilderRef builder,LLVMValueRef ptr,LLVMValueRef index,LLVMValueRef value,unsigned alignment)209 lp_build_pointer_set_unaligned(LLVMBuilderRef builder,
210                                LLVMValueRef ptr,
211                                LLVMValueRef index,
212                                LLVMValueRef value,
213                                unsigned alignment)
214 {
215    LLVMValueRef element_ptr;
216    LLVMValueRef instr;
217    element_ptr = LLVMBuildGEP2(builder, LLVMTypeOf(value), ptr, &index, 1, "");
218    instr = LLVMBuildStore(builder, value, element_ptr);
219    LLVMSetAlignment(instr, alignment);
220 }
221