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 <[email protected]>
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 LLVMValueRef
lp_build_struct_get_ptr2(struct gallivm_state * gallivm,LLVMTypeRef ptr_type,LLVMValueRef ptr,unsigned member,const char * name)45 lp_build_struct_get_ptr2(struct gallivm_state *gallivm,
46 LLVMTypeRef ptr_type,
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 = LLVMBuildGEP2(gallivm->builder, ptr_type, 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_get2(struct gallivm_state * gallivm,LLVMTypeRef ptr_type,LLVMValueRef ptr,unsigned member,const char * name)64 lp_build_struct_get2(struct gallivm_state *gallivm,
65 LLVMTypeRef ptr_type,
66 LLVMValueRef ptr,
67 unsigned member,
68 const char *name)
69 {
70 LLVMValueRef member_ptr;
71 LLVMValueRef res;
72 assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
73 assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMStructTypeKind);
74 member_ptr = lp_build_struct_get_ptr2(gallivm, ptr_type, ptr, member, name);
75 LLVMTypeRef member_type = LLVMStructGetTypeAtIndex(ptr_type, member);
76 res = LLVMBuildLoad2(gallivm->builder, member_type, member_ptr, "");
77 lp_build_name(res, "%s.%s", LLVMGetValueName(ptr), name);
78 return res;
79 }
80
81 LLVMValueRef
lp_build_array_get_ptr2(struct gallivm_state * gallivm,LLVMTypeRef array_type,LLVMValueRef ptr,LLVMValueRef index)82 lp_build_array_get_ptr2(struct gallivm_state *gallivm,
83 LLVMTypeRef array_type,
84 LLVMValueRef ptr,
85 LLVMValueRef index)
86 {
87 LLVMValueRef indices[2];
88 LLVMValueRef element_ptr;
89 assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
90 assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind);
91 indices[0] = lp_build_const_int32(gallivm, 0);
92 indices[1] = index;
93 element_ptr = LLVMBuildGEP2(gallivm->builder, array_type, ptr, indices, ARRAY_SIZE(indices), "");
94 #if MESA_DEBUG
95 lp_build_name(element_ptr, "&%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index));
96 #endif
97 return element_ptr;
98 }
99
100
101 LLVMValueRef
lp_build_array_get2(struct gallivm_state * gallivm,LLVMTypeRef array_type,LLVMValueRef ptr,LLVMValueRef index)102 lp_build_array_get2(struct gallivm_state *gallivm,
103 LLVMTypeRef array_type,
104 LLVMValueRef ptr,
105 LLVMValueRef index)
106 {
107 LLVMValueRef element_ptr;
108 LLVMValueRef res;
109 assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
110 assert(LLVM_VERSION_MAJOR >= 15 || LLVMGetTypeKind(LLVMGetElementType(LLVMTypeOf(ptr))) == LLVMArrayTypeKind);
111 element_ptr = lp_build_array_get_ptr2(gallivm, array_type, ptr, index);
112 LLVMTypeRef element_type = LLVMGetElementType(array_type);
113 res = LLVMBuildLoad2(gallivm->builder, element_type, element_ptr, "");
114 #if MESA_DEBUG
115 lp_build_name(res, "%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index));
116 #endif
117 return res;
118 }
119
120 LLVMValueRef
lp_build_pointer_get_unaligned2(LLVMBuilderRef builder,LLVMTypeRef ptr_type,LLVMValueRef ptr,LLVMValueRef index,unsigned alignment)121 lp_build_pointer_get_unaligned2(LLVMBuilderRef builder,
122 LLVMTypeRef ptr_type,
123 LLVMValueRef ptr,
124 LLVMValueRef index,
125 unsigned alignment)
126 {
127 LLVMValueRef element_ptr;
128 LLVMValueRef res;
129 assert(LLVMGetTypeKind(LLVMTypeOf(ptr)) == LLVMPointerTypeKind);
130 element_ptr = LLVMBuildGEP2(builder, ptr_type, ptr, &index, 1, "");
131 res = LLVMBuildLoad2(builder, ptr_type, element_ptr, "");
132 if (alignment)
133 LLVMSetAlignment(res, alignment);
134 #if MESA_DEBUG
135 lp_build_name(res, "%s[%s]", LLVMGetValueName(ptr), LLVMGetValueName(index));
136 #endif
137 return res;
138 }
139
140
141 LLVMValueRef
lp_build_pointer_get2(LLVMBuilderRef builder,LLVMTypeRef ptr_type,LLVMValueRef ptr,LLVMValueRef index)142 lp_build_pointer_get2(LLVMBuilderRef builder,
143 LLVMTypeRef ptr_type,
144 LLVMValueRef ptr,
145 LLVMValueRef index)
146 {
147 return lp_build_pointer_get_unaligned2(builder, ptr_type, ptr, index, 0);
148 }
149
150 void
lp_build_pointer_set(LLVMBuilderRef builder,LLVMValueRef ptr,LLVMValueRef index,LLVMValueRef value)151 lp_build_pointer_set(LLVMBuilderRef builder,
152 LLVMValueRef ptr,
153 LLVMValueRef index,
154 LLVMValueRef value)
155 {
156 LLVMValueRef element_ptr;
157 element_ptr = LLVMBuildGEP2(builder, LLVMTypeOf(value), ptr, &index, 1, "");
158 LLVMBuildStore(builder, value, element_ptr);
159 }
160
161
162 void
lp_build_pointer_set_unaligned(LLVMBuilderRef builder,LLVMValueRef ptr,LLVMValueRef index,LLVMValueRef value,unsigned alignment)163 lp_build_pointer_set_unaligned(LLVMBuilderRef builder,
164 LLVMValueRef ptr,
165 LLVMValueRef index,
166 LLVMValueRef value,
167 unsigned alignment)
168 {
169 LLVMValueRef element_ptr;
170 LLVMValueRef instr;
171 element_ptr = LLVMBuildGEP2(builder, LLVMTypeOf(value), ptr, &index, 1, "");
172 instr = LLVMBuildStore(builder, value, element_ptr);
173 LLVMSetAlignment(instr, alignment);
174 }
175