1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/*
* llvmjit_emit.h
* Helpers to make emitting LLVM IR a bit more concise and pgindent proof.
*
* Copyright (c) 2018-2021, PostgreSQL Global Development Group
*
* src/include/jit/llvmjit_emit.h
*/
#ifndef LLVMJIT_EMIT_H
#define LLVMJIT_EMIT_H
/*
* To avoid breaking cpluspluscheck, allow including the file even when LLVM
* is not available.
*/
#ifdef USE_LLVM
#include <llvm-c/Core.h>
#include "jit/llvmjit.h"
/*
* Emit a non-LLVM pointer as an LLVM constant.
*/
static inline LLVMValueRef
l_ptr_const(void *ptr, LLVMTypeRef type)
{
LLVMValueRef c = LLVMConstInt(TypeSizeT, (uintptr_t) ptr, false);
return LLVMConstIntToPtr(c, type);
}
/*
* Emit pointer.
*/
static inline LLVMTypeRef
l_ptr(LLVMTypeRef t)
{
return LLVMPointerType(t, 0);
}
/*
* Emit constant integer.
*/
static inline LLVMValueRef
l_int8_const(int8 i)
{
return LLVMConstInt(LLVMInt8Type(), i, false);
}
/*
* Emit constant integer.
*/
static inline LLVMValueRef
l_int16_const(int16 i)
{
return LLVMConstInt(LLVMInt16Type(), i, false);
}
/*
* Emit constant integer.
*/
static inline LLVMValueRef
l_int32_const(int32 i)
{
return LLVMConstInt(LLVMInt32Type(), i, false);
}
/*
* Emit constant integer.
*/
static inline LLVMValueRef
l_int64_const(int64 i)
{
return LLVMConstInt(LLVMInt64Type(), i, false);
}
/*
* Emit constant integer.
*/
static inline LLVMValueRef
l_sizet_const(size_t i)
{
return LLVMConstInt(TypeSizeT, i, false);
}
/*
* Emit constant boolean, as used for storage (e.g. global vars, structs).
*/
static inline LLVMValueRef
l_sbool_const(bool i)
{
return LLVMConstInt(TypeStorageBool, (int) i, false);
}
/*
* Emit constant boolean, as used for parameters (e.g. function parameters).
*/
static inline LLVMValueRef
l_pbool_const(bool i)
{
return LLVMConstInt(TypeParamBool, (int) i, false);
}
/*
* Load a pointer member idx from a struct.
*/
static inline LLVMValueRef
l_load_struct_gep(LLVMBuilderRef b, LLVMValueRef v, int32 idx, const char *name)
{
LLVMValueRef v_ptr = LLVMBuildStructGEP(b, v, idx, "");
return LLVMBuildLoad(b, v_ptr, name);
}
/*
* Load value of a pointer, after applying one index operation.
*/
static inline LLVMValueRef
l_load_gep1(LLVMBuilderRef b, LLVMValueRef v, LLVMValueRef idx, const char *name)
{
LLVMValueRef v_ptr = LLVMBuildGEP(b, v, &idx, 1, "");
return LLVMBuildLoad(b, v_ptr, name);
}
/* separate, because pg_attribute_printf(2, 3) can't appear in definition */
static inline LLVMBasicBlockRef l_bb_before_v(LLVMBasicBlockRef r, const char *fmt,...) pg_attribute_printf(2, 3);
/*
* Insert a new basic block, just before r, the name being determined by fmt
* and arguments.
*/
static inline LLVMBasicBlockRef
l_bb_before_v(LLVMBasicBlockRef r, const char *fmt,...)
{
char buf[512];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
return LLVMInsertBasicBlock(r, buf);
}
/* separate, because pg_attribute_printf(2, 3) can't appear in definition */
static inline LLVMBasicBlockRef l_bb_append_v(LLVMValueRef f, const char *fmt,...) pg_attribute_printf(2, 3);
/*
* Insert a new basic block after previous basic blocks, the name being
* determined by fmt and arguments.
*/
static inline LLVMBasicBlockRef
l_bb_append_v(LLVMValueRef f, const char *fmt,...)
{
char buf[512];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
return LLVMAppendBasicBlock(f, buf);
}
/*
* Mark a callsite as readonly.
*/
static inline void
l_callsite_ro(LLVMValueRef f)
{
const char argname[] = "readonly";
LLVMAttributeRef ref;
ref = LLVMCreateStringAttribute(LLVMGetGlobalContext(),
argname,
sizeof(argname) - 1,
NULL, 0);
LLVMAddCallSiteAttribute(f, LLVMAttributeFunctionIndex, ref);
}
/*
* Mark a callsite as alwaysinline.
*/
static inline void
l_callsite_alwaysinline(LLVMValueRef f)
{
const char argname[] = "alwaysinline";
int id;
LLVMAttributeRef attr;
id = LLVMGetEnumAttributeKindForName(argname,
sizeof(argname) - 1);
attr = LLVMCreateEnumAttribute(LLVMGetGlobalContext(), id, 0);
LLVMAddCallSiteAttribute(f, LLVMAttributeFunctionIndex, attr);
}
/*
* Emit code to switch memory context.
*/
static inline LLVMValueRef
l_mcxt_switch(LLVMModuleRef mod, LLVMBuilderRef b, LLVMValueRef nc)
{
const char *cmc = "CurrentMemoryContext";
LLVMValueRef cur;
LLVMValueRef ret;
if (!(cur = LLVMGetNamedGlobal(mod, cmc)))
cur = LLVMAddGlobal(mod, l_ptr(StructMemoryContextData), cmc);
ret = LLVMBuildLoad(b, cur, cmc);
LLVMBuildStore(b, nc, cur);
return ret;
}
/*
* Return pointer to the argno'th argument nullness.
*/
static inline LLVMValueRef
l_funcnullp(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
{
LLVMValueRef v_args;
LLVMValueRef v_argn;
v_args = LLVMBuildStructGEP(b,
v_fcinfo,
FIELDNO_FUNCTIONCALLINFODATA_ARGS,
"");
v_argn = LLVMBuildStructGEP(b, v_args, argno, "");
return LLVMBuildStructGEP(b, v_argn, FIELDNO_NULLABLE_DATUM_ISNULL, "");
}
/*
* Return pointer to the argno'th argument datum.
*/
static inline LLVMValueRef
l_funcvaluep(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
{
LLVMValueRef v_args;
LLVMValueRef v_argn;
v_args = LLVMBuildStructGEP(b,
v_fcinfo,
FIELDNO_FUNCTIONCALLINFODATA_ARGS,
"");
v_argn = LLVMBuildStructGEP(b, v_args, argno, "");
return LLVMBuildStructGEP(b, v_argn, FIELDNO_NULLABLE_DATUM_DATUM, "");
}
/*
* Return argno'th argument nullness.
*/
static inline LLVMValueRef
l_funcnull(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
{
return LLVMBuildLoad(b, l_funcnullp(b, v_fcinfo, argno), "");
}
/*
* Return argno'th argument datum.
*/
static inline LLVMValueRef
l_funcvalue(LLVMBuilderRef b, LLVMValueRef v_fcinfo, size_t argno)
{
return LLVMBuildLoad(b, l_funcvaluep(b, v_fcinfo, argno), "");
}
#endif /* USE_LLVM */
#endif
|