PostgreSQL Source Code  git master
llvmjit_expr.c File Reference
#include "postgres.h"
#include <llvm-c/Core.h>
#include <llvm-c/Target.h>
#include "access/htup_details.h"
#include "access/nbtree.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_type.h"
#include "executor/execExpr.h"
#include "executor/execdebug.h"
#include "executor/nodeAgg.h"
#include "executor/nodeSubplan.h"
#include "funcapi.h"
#include "jit/llvmjit.h"
#include "jit/llvmjit_emit.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "parser/parse_coerce.h"
#include "parser/parsetree.h"
#include "pgstat.h"
#include "utils/acl.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/fmgrtab.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/timestamp.h"
#include "utils/typcache.h"
#include "utils/xml.h"
Include dependency graph for llvmjit_expr.c:

Go to the source code of this file.

Data Structures

struct  CompiledExprState
 

Macros

#define build_EvalXFunc(b, mod, funcname, v_state, op, ...)
 

Typedefs

typedef struct CompiledExprState CompiledExprState
 

Functions

static Datum ExecRunCompiledExpr (ExprState *state, ExprContext *econtext, bool *isNull)
 
static LLVMValueRef BuildV1Call (LLVMJitContext *context, LLVMBuilderRef b, LLVMModuleRef mod, FunctionCallInfo fcinfo, LLVMValueRef *v_fcinfo_isnull)
 
static LLVMValueRef build_EvalXFuncInt (LLVMBuilderRef b, LLVMModuleRef mod, const char *funcname, LLVMValueRef v_state, ExprEvalStep *op, int natts, LLVMValueRef v_args[])
 
static LLVMValueRef create_LifetimeEnd (LLVMModuleRef mod)
 
bool llvm_compile_expr (ExprState *state)
 

Macro Definition Documentation

◆ build_EvalXFunc

#define build_EvalXFunc (   b,
  mod,
  funcname,
  v_state,
  op,
  ... 
)
Value:
build_EvalXFuncInt(b, mod, funcname, v_state, op, \
lengthof(((LLVMValueRef[]){__VA_ARGS__})), \
((LLVMValueRef[]){__VA_ARGS__}))
#define lengthof(array)
Definition: c.h:734
static LLVMValueRef build_EvalXFuncInt(LLVMBuilderRef b, LLVMModuleRef mod, const char *funcname, LLVMValueRef v_state, ExprEvalStep *op, int natts, LLVMValueRef v_args[])

Definition at line 68 of file llvmjit_expr.c.

Referenced by llvm_compile_expr().

Typedef Documentation

◆ CompiledExprState

Function Documentation

◆ build_EvalXFuncInt()

static LLVMValueRef build_EvalXFuncInt ( LLVMBuilderRef  b,
LLVMModuleRef  mod,
const char *  funcname,
LLVMValueRef  v_state,
ExprEvalStep op,
int  natts,
LLVMValueRef  v_args[] 
)
static

Definition at line 2454 of file llvmjit_expr.c.

References elog, ERROR, i, llvm_pg_func(), palloc(), pfree(), and StructExprEvalStep.

2457 {
2458  LLVMValueRef v_fn = llvm_pg_func(mod, funcname);
2459  LLVMValueRef *params;
2460  int argno = 0;
2461  LLVMValueRef v_ret;
2462 
2463  /* cheap pre-check as llvm just asserts out */
2464  if (LLVMCountParams(v_fn) != (nargs + 2))
2465  elog(ERROR, "parameter mismatch: %s expects %d passed %d",
2466  funcname, LLVMCountParams(v_fn), nargs + 2);
2467 
2468  params = palloc(sizeof(LLVMValueRef) * (2 + nargs));
2469 
2470  params[argno++] = v_state;
2471  params[argno++] = l_ptr_const(op, l_ptr(StructExprEvalStep));
2472 
2473  for (int i = 0; i < nargs; i++)
2474  params[argno++] = v_args[i];
2475 
2476  v_ret = LLVMBuildCall(b, v_fn, params, argno, "");
2477 
2478  pfree(params);
2479 
2480  return v_ret;
2481 }
LLVMTypeRef StructExprEvalStep
Definition: llvmjit.c:83
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:45
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:227
int i
LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname)
Definition: llvmjit.c:419

◆ BuildV1Call()

static LLVMValueRef BuildV1Call ( LLVMJitContext *  context,
LLVMBuilderRef  b,
LLVMModuleRef  mod,
FunctionCallInfo  fcinfo,
LLVMValueRef *  v_fcinfo_isnull 
)
static

Definition at line 2408 of file llvmjit_expr.c.

References FunctionCallInfoBaseData::args, create_LifetimeEnd(), FIELDNO_FUNCTIONCALLINFODATA_ISNULL, FunctionCallInfoBaseData::isnull, lengthof, llvm_function_reference(), FunctionCallInfoBaseData::nargs, and StructFunctionCallInfoData.

Referenced by llvm_compile_expr().

2411 {
2412  LLVMValueRef v_fn;
2413  LLVMValueRef v_fcinfo_isnullp;
2414  LLVMValueRef v_retval;
2415  LLVMValueRef v_fcinfo;
2416 
2417  v_fn = llvm_function_reference(context, b, mod, fcinfo);
2418 
2419  v_fcinfo = l_ptr_const(fcinfo, l_ptr(StructFunctionCallInfoData));
2420  v_fcinfo_isnullp = LLVMBuildStructGEP(b, v_fcinfo,
2422  "v_fcinfo_isnull");
2423  LLVMBuildStore(b, l_sbool_const(0), v_fcinfo_isnullp);
2424 
2425  v_retval = LLVMBuildCall(b, v_fn, &v_fcinfo, 1, "funccall");
2426 
2427  if (v_fcinfo_isnull)
2428  *v_fcinfo_isnull = LLVMBuildLoad(b, v_fcinfo_isnullp, "");
2429 
2430  /*
2431  * Add lifetime-end annotation, signaling that writes to memory don't have
2432  * to be retained (important for inlining potential).
2433  */
2434  {
2435  LLVMValueRef v_lifetime = create_LifetimeEnd(mod);
2436  LLVMValueRef params[2];
2437 
2438  params[0] = l_int64_const(sizeof(NullableDatum) * fcinfo->nargs);
2439  params[1] = l_ptr_const(fcinfo->args, l_ptr(LLVMInt8Type()));
2440  LLVMBuildCall(b, v_lifetime, params, lengthof(params), "");
2441 
2442  params[0] = l_int64_const(sizeof(fcinfo->isnull));
2443  params[1] = l_ptr_const(&fcinfo->isnull, l_ptr(LLVMInt8Type()));
2444  LLVMBuildCall(b, v_lifetime, params, lengthof(params), "");
2445  }
2446 
2447  return v_retval;
2448 }
#define FIELDNO_FUNCTIONCALLINFODATA_ISNULL
Definition: fmgr.h:91
LLVMTypeRef StructFunctionCallInfoData
Definition: llvmjit.c:81
static LLVMValueRef create_LifetimeEnd(LLVMModuleRef mod)
LLVMValueRef llvm_function_reference(LLVMJitContext *context, LLVMBuilderRef builder, LLVMModuleRef mod, FunctionCallInfo fcinfo)
Definition: llvmjit.c:496
#define lengthof(array)
Definition: c.h:734
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95

◆ create_LifetimeEnd()

static LLVMValueRef create_LifetimeEnd ( LLVMModuleRef  mod)
static

Definition at line 2484 of file llvmjit_expr.c.

References Assert, fn(), lengthof, and sig.

Referenced by BuildV1Call().

2485 {
2486  LLVMTypeRef sig;
2487  LLVMValueRef fn;
2488  LLVMTypeRef param_types[2];
2489 
2490  /* LLVM 5+ has a variadic pointer argument */
2491 #if LLVM_VERSION_MAJOR < 5
2492  const char *nm = "llvm.lifetime.end";
2493 #else
2494  const char *nm = "llvm.lifetime.end.p0i8";
2495 #endif
2496 
2497  fn = LLVMGetNamedFunction(mod, nm);
2498  if (fn)
2499  return fn;
2500 
2501  param_types[0] = LLVMInt64Type();
2502  param_types[1] = l_ptr(LLVMInt8Type());
2503 
2504  sig = LLVMFunctionType(LLVMVoidType(),
2505  param_types, lengthof(param_types),
2506  false);
2507  fn = LLVMAddFunction(mod, nm, sig);
2508 
2509  LLVMSetFunctionCallConv(fn, LLVMCCallConv);
2510 
2511  Assert(LLVMGetIntrinsicID(fn));
2512 
2513  return fn;
2514 }
#define lengthof(array)
Definition: c.h:734
static int sig
Definition: pg_ctl.c:84
static void * fn(void *arg)
#define Assert(condition)
Definition: c.h:804

◆ ExecRunCompiledExpr()

static Datum ExecRunCompiledExpr ( ExprState state,
ExprContext econtext,
bool isNull 
)
static

Definition at line 2388 of file llvmjit_expr.c.

References Assert, CheckExprStillValid(), CompiledExprState::context, ExprState::evalfunc, ExprState::evalfunc_private, CompiledExprState::funcname, llvm_enter_fatal_on_oom(), llvm_get_function(), and llvm_leave_fatal_on_oom().

Referenced by llvm_compile_expr().

2389 {
2390  CompiledExprState *cstate = state->evalfunc_private;
2391  ExprStateEvalFunc func;
2392 
2393  CheckExprStillValid(state, econtext);
2394 
2396  func = (ExprStateEvalFunc) llvm_get_function(cstate->context,
2397  cstate->funcname);
2399  Assert(func);
2400 
2401  /* remove indirection via this function for future calls */
2402  state->evalfunc = func;
2403 
2404  return func(state, econtext, isNull);
2405 }
Datum(* ExprStateEvalFunc)(struct ExprState *expression, struct ExprContext *econtext, bool *isNull)
Definition: execnodes.h:52
LLVMJitContext * context
Definition: llvmjit_expr.c:50
ExprStateEvalFunc evalfunc
Definition: execnodes.h:90
void * evalfunc_private
Definition: execnodes.h:96
#define Assert(condition)
Definition: c.h:804
void * llvm_get_function(LLVMJitContext *context, const char *funcname)
Definition: llvmjit.c:276
void llvm_enter_fatal_on_oom(void)
const char * funcname
Definition: llvmjit_expr.c:51
void CheckExprStillValid(ExprState *state, ExprContext *econtext)
void llvm_leave_fatal_on_oom(void)

◆ llvm_compile_expr()

bool llvm_compile_expr ( ExprState state)

Definition at line 78 of file llvmjit_expr.c.

References ExprEvalStep::agg_deserialize, ExprEvalStep::agg_plain_pergroup_nullcheck, ExprEvalStep::agg_strict_input_check, ExprEvalStep::agg_trans, ExprEvalStep::aggref, generate_unaccent_rules::args, Assert, ExprEvalStep::assign_tmp, ExprEvalStep::assign_var, AttributeTemplate, ExprEvalStep::boolexpr, build_EvalXFunc, BuildV1Call(), ExprEvalStep::casetest, castNode, ExprEvalStep::constval, CompiledExprState::context, ExprEvalStep::cparam, ExprEvalStep::d, ExprContext::ecxt_per_tuple_memory, EEOP_AGG_DESERIALIZE, EEOP_AGG_ORDERED_TRANS_DATUM, EEOP_AGG_ORDERED_TRANS_TUPLE, EEOP_AGG_PLAIN_PERGROUP_NULLCHECK, EEOP_AGG_PLAIN_TRANS_BYREF, EEOP_AGG_PLAIN_TRANS_BYVAL, EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF, EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL, EEOP_AGG_PLAIN_TRANS_STRICT_BYREF, EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL, EEOP_AGG_STRICT_DESERIALIZE, EEOP_AGG_STRICT_INPUT_CHECK_ARGS, EEOP_AGG_STRICT_INPUT_CHECK_NULLS, EEOP_AGGREF, EEOP_ARRAYCOERCE, EEOP_ARRAYEXPR, EEOP_ASSIGN_INNER_VAR, EEOP_ASSIGN_OUTER_VAR, EEOP_ASSIGN_SCAN_VAR, EEOP_ASSIGN_TMP, EEOP_ASSIGN_TMP_MAKE_RO, EEOP_BOOL_AND_STEP, EEOP_BOOL_AND_STEP_FIRST, EEOP_BOOL_AND_STEP_LAST, EEOP_BOOL_NOT_STEP, EEOP_BOOL_OR_STEP, EEOP_BOOL_OR_STEP_FIRST, EEOP_BOOL_OR_STEP_LAST, EEOP_BOOLTEST_IS_FALSE, EEOP_BOOLTEST_IS_NOT_FALSE, EEOP_BOOLTEST_IS_NOT_TRUE, EEOP_BOOLTEST_IS_TRUE, EEOP_CASE_TESTVAL, EEOP_CONST, EEOP_CONVERT_ROWTYPE, EEOP_CURRENTOFEXPR, EEOP_DISTINCT, EEOP_DOMAIN_CHECK, EEOP_DOMAIN_NOTNULL, EEOP_DOMAIN_TESTVAL, EEOP_DONE, EEOP_FIELDSELECT, EEOP_FIELDSTORE_DEFORM, EEOP_FIELDSTORE_FORM, EEOP_FUNCEXPR, EEOP_FUNCEXPR_FUSAGE, EEOP_FUNCEXPR_STRICT, EEOP_FUNCEXPR_STRICT_FUSAGE, EEOP_GROUPING_FUNC, EEOP_INNER_FETCHSOME, EEOP_INNER_SYSVAR, EEOP_INNER_VAR, EEOP_IOCOERCE, EEOP_JUMP, EEOP_JUMP_IF_NOT_NULL, EEOP_JUMP_IF_NOT_TRUE, EEOP_JUMP_IF_NULL, EEOP_LAST, EEOP_MAKE_READONLY, EEOP_MINMAX, EEOP_NEXTVALUEEXPR, EEOP_NOT_DISTINCT, EEOP_NULLIF, EEOP_NULLTEST_ISNOTNULL, EEOP_NULLTEST_ISNULL, EEOP_NULLTEST_ROWISNOTNULL, EEOP_NULLTEST_ROWISNULL, EEOP_OUTER_FETCHSOME, EEOP_OUTER_SYSVAR, EEOP_OUTER_VAR, EEOP_PARAM_CALLBACK, EEOP_PARAM_EXEC, EEOP_PARAM_EXTERN, EEOP_QUAL, EEOP_ROW, EEOP_ROWCOMPARE_FINAL, EEOP_ROWCOMPARE_STEP, EEOP_SBSREF_ASSIGN, EEOP_SBSREF_FETCH, EEOP_SBSREF_OLD, EEOP_SBSREF_SUBSCRIPTS, EEOP_SCALARARRAYOP, EEOP_SCAN_FETCHSOME, EEOP_SCAN_SYSVAR, EEOP_SCAN_VAR, EEOP_SQLVALUEFUNCTION, EEOP_SUBPLAN, EEOP_WHOLEROW, EEOP_WINDOW_FUNC, EEOP_XMLEXPR, elog, ERROR, EState::es_jit, EState::es_jit_flags, ExecEvalStepOp(), ExecRunCompiledExpr(), ExprEvalStep::fetch, FIELDNO_AGGSTATE_ALL_PERGROUPS, FIELDNO_AGGSTATE_CURAGGCONTEXT, FIELDNO_AGGSTATE_CURPERTRANS, FIELDNO_AGGSTATE_CURRENT_SET, FIELDNO_AGGSTATEPERGROUPDATA_NOTRANSVALUE, FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE, FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL, FIELDNO_EXPRCONTEXT_AGGNULLS, FIELDNO_EXPRCONTEXT_AGGVALUES, FIELDNO_EXPRCONTEXT_CASEDATUM, FIELDNO_EXPRCONTEXT_CASENULL, FIELDNO_EXPRCONTEXT_DOMAINDATUM, FIELDNO_EXPRCONTEXT_DOMAINNULL, FIELDNO_EXPRCONTEXT_INNERTUPLE, FIELDNO_EXPRCONTEXT_OUTERTUPLE, FIELDNO_EXPRCONTEXT_SCANTUPLE, FIELDNO_EXPRSTATE_PARENT, FIELDNO_EXPRSTATE_RESNULL, FIELDNO_EXPRSTATE_RESULTSLOT, FIELDNO_EXPRSTATE_RESVALUE, FIELDNO_FUNCTIONCALLINFODATA_ISNULL, FIELDNO_NULLABLE_DATUM_ISNULL, FIELDNO_TUPLETABLESLOT_ISNULL, FIELDNO_TUPLETABLESLOT_NVALID, FIELDNO_TUPLETABLESLOT_VALUES, ExprEvalStep::func, CompiledExprState::funcname, INSTR_TIME_ACCUM_DIFF, INSTR_TIME_SET_CURRENT, ExprEvalStep::iocoerce, ExprEvalStep::jump, lengthof, llvm_copy_attributes(), llvm_create_context(), llvm_enter_fatal_on_oom(), llvm_expand_funcname(), llvm_function_reference(), llvm_leave_fatal_on_oom(), llvm_mutable_module(), llvm_pg_func(), llvm_pg_var_func_type(), ExprEvalStep::make_readonly, palloc(), palloc0(), ExprState::parent, PGJIT_DEFORM, ExprEvalStep::qualexpr, ExprEvalStep::resnull, ExprEvalStep::resvalue, ExprEvalStep::rowcompare_final, ROWCOMPARE_GE, ROWCOMPARE_GT, ROWCOMPARE_LE, ROWCOMPARE_LT, ExprEvalStep::rowcompare_step, ExprEvalStep::sbsref, ExprEvalStep::sbsref_subscript, slot_compile_deform(), PlanState::state, StructAggState, StructAggStatePerTransData, StructExprContext, StructExprEvalStep, StructFunctionCallInfoData, StructMemoryContextData, StructNullableDatum, AggState::tmpcontext, AggStatePerTransData::transfn_fcinfo, TTSOpsVirtual, TypeParamBool, TypeSizeT, TypeStorageBool, value, ExprEvalStep::var, WindowFuncExprState::wfuncno, and ExprEvalStep::window_func.

Referenced by _PG_jit_provider_init().

79 {
80  PlanState *parent = state->parent;
81  char *funcname;
82 
83  LLVMJitContext *context = NULL;
84 
85  LLVMBuilderRef b;
86  LLVMModuleRef mod;
87  LLVMValueRef eval_fn;
88  LLVMBasicBlockRef entry;
89  LLVMBasicBlockRef *opblocks;
90 
91  /* state itself */
92  LLVMValueRef v_state;
93  LLVMValueRef v_econtext;
94  LLVMValueRef v_parent;
95 
96  /* returnvalue */
97  LLVMValueRef v_isnullp;
98 
99  /* tmp vars in state */
100  LLVMValueRef v_tmpvaluep;
101  LLVMValueRef v_tmpisnullp;
102 
103  /* slots */
104  LLVMValueRef v_innerslot;
105  LLVMValueRef v_outerslot;
106  LLVMValueRef v_scanslot;
107  LLVMValueRef v_resultslot;
108 
109  /* nulls/values of slots */
110  LLVMValueRef v_innervalues;
111  LLVMValueRef v_innernulls;
112  LLVMValueRef v_outervalues;
113  LLVMValueRef v_outernulls;
114  LLVMValueRef v_scanvalues;
115  LLVMValueRef v_scannulls;
116  LLVMValueRef v_resultvalues;
117  LLVMValueRef v_resultnulls;
118 
119  /* stuff in econtext */
120  LLVMValueRef v_aggvalues;
121  LLVMValueRef v_aggnulls;
122 
123  instr_time starttime;
124  instr_time endtime;
125 
127 
128  /*
129  * Right now we don't support compiling expressions without a parent, as
130  * we need access to the EState.
131  */
132  Assert(parent);
133 
134  /* get or create JIT context */
135  if (parent->state->es_jit)
136  context = (LLVMJitContext *) parent->state->es_jit;
137  else
138  {
139  context = llvm_create_context(parent->state->es_jit_flags);
140  parent->state->es_jit = &context->base;
141  }
142 
143  INSTR_TIME_SET_CURRENT(starttime);
144 
145  mod = llvm_mutable_module(context);
146 
147  b = LLVMCreateBuilder();
148 
149  funcname = llvm_expand_funcname(context, "evalexpr");
150 
151  /* create function */
152  eval_fn = LLVMAddFunction(mod, funcname,
153  llvm_pg_var_func_type("TypeExprStateEvalFunc"));
154  LLVMSetLinkage(eval_fn, LLVMExternalLinkage);
155  LLVMSetVisibility(eval_fn, LLVMDefaultVisibility);
157 
158  entry = LLVMAppendBasicBlock(eval_fn, "entry");
159 
160  /* build state */
161  v_state = LLVMGetParam(eval_fn, 0);
162  v_econtext = LLVMGetParam(eval_fn, 1);
163  v_isnullp = LLVMGetParam(eval_fn, 2);
164 
165  LLVMPositionBuilderAtEnd(b, entry);
166 
167  v_tmpvaluep = LLVMBuildStructGEP(b, v_state,
169  "v.state.resvalue");
170  v_tmpisnullp = LLVMBuildStructGEP(b, v_state,
172  "v.state.resnull");
173  v_parent = l_load_struct_gep(b, v_state,
175  "v.state.parent");
176 
177  /* build global slots */
178  v_scanslot = l_load_struct_gep(b, v_econtext,
180  "v_scanslot");
181  v_innerslot = l_load_struct_gep(b, v_econtext,
183  "v_innerslot");
184  v_outerslot = l_load_struct_gep(b, v_econtext,
186  "v_outerslot");
187  v_resultslot = l_load_struct_gep(b, v_state,
189  "v_resultslot");
190 
191  /* build global values/isnull pointers */
192  v_scanvalues = l_load_struct_gep(b, v_scanslot,
194  "v_scanvalues");
195  v_scannulls = l_load_struct_gep(b, v_scanslot,
197  "v_scannulls");
198  v_innervalues = l_load_struct_gep(b, v_innerslot,
200  "v_innervalues");
201  v_innernulls = l_load_struct_gep(b, v_innerslot,
203  "v_innernulls");
204  v_outervalues = l_load_struct_gep(b, v_outerslot,
206  "v_outervalues");
207  v_outernulls = l_load_struct_gep(b, v_outerslot,
209  "v_outernulls");
210  v_resultvalues = l_load_struct_gep(b, v_resultslot,
212  "v_resultvalues");
213  v_resultnulls = l_load_struct_gep(b, v_resultslot,
215  "v_resultnulls");
216 
217  /* aggvalues/aggnulls */
218  v_aggvalues = l_load_struct_gep(b, v_econtext,
220  "v.econtext.aggvalues");
221  v_aggnulls = l_load_struct_gep(b, v_econtext,
223  "v.econtext.aggnulls");
224 
225  /* allocate blocks for each op upfront, so we can do jumps easily */
226  opblocks = palloc(sizeof(LLVMBasicBlockRef) * state->steps_len);
227  for (int opno = 0; opno < state->steps_len; opno++)
228  opblocks[opno] = l_bb_append_v(eval_fn, "b.op.%d.start", opno);
229 
230  /* jump from entry to first block */
231  LLVMBuildBr(b, opblocks[0]);
232 
233  for (int opno = 0; opno < state->steps_len; opno++)
234  {
235  ExprEvalStep *op;
236  ExprEvalOp opcode;
237  LLVMValueRef v_resvaluep;
238  LLVMValueRef v_resnullp;
239 
240  LLVMPositionBuilderAtEnd(b, opblocks[opno]);
241 
242  op = &state->steps[opno];
243  opcode = ExecEvalStepOp(state, op);
244 
245  v_resvaluep = l_ptr_const(op->resvalue, l_ptr(TypeSizeT));
246  v_resnullp = l_ptr_const(op->resnull, l_ptr(TypeStorageBool));
247 
248  switch (opcode)
249  {
250  case EEOP_DONE:
251  {
252  LLVMValueRef v_tmpisnull;
253  LLVMValueRef v_tmpvalue;
254 
255  v_tmpvalue = LLVMBuildLoad(b, v_tmpvaluep, "");
256  v_tmpisnull = LLVMBuildLoad(b, v_tmpisnullp, "");
257 
258  LLVMBuildStore(b, v_tmpisnull, v_isnullp);
259 
260  LLVMBuildRet(b, v_tmpvalue);
261  break;
262  }
263 
266  case EEOP_SCAN_FETCHSOME:
267  {
268  TupleDesc desc = NULL;
269  LLVMValueRef v_slot;
270  LLVMBasicBlockRef b_fetch;
271  LLVMValueRef v_nvalid;
272  LLVMValueRef l_jit_deform = NULL;
273  const TupleTableSlotOps *tts_ops = NULL;
274 
275  b_fetch = l_bb_before_v(opblocks[opno + 1],
276  "op.%d.fetch", opno);
277 
278  if (op->d.fetch.known_desc)
279  desc = op->d.fetch.known_desc;
280 
281  if (op->d.fetch.fixed)
282  tts_ops = op->d.fetch.kind;
283 
284  /* step should not have been generated */
285  Assert(tts_ops != &TTSOpsVirtual);
286 
287  if (opcode == EEOP_INNER_FETCHSOME)
288  v_slot = v_innerslot;
289  else if (opcode == EEOP_OUTER_FETCHSOME)
290  v_slot = v_outerslot;
291  else
292  v_slot = v_scanslot;
293 
294  /*
295  * Check if all required attributes are available, or
296  * whether deforming is required.
297  */
298  v_nvalid =
299  l_load_struct_gep(b, v_slot,
301  "");
302  LLVMBuildCondBr(b,
303  LLVMBuildICmp(b, LLVMIntUGE, v_nvalid,
304  l_int16_const(op->d.fetch.last_var),
305  ""),
306  opblocks[opno + 1], b_fetch);
307 
308  LLVMPositionBuilderAtEnd(b, b_fetch);
309 
310  /*
311  * If the tupledesc of the to-be-deformed tuple is known,
312  * and JITing of deforming is enabled, build deform
313  * function specific to tupledesc and the exact number of
314  * to-be-extracted attributes.
315  */
316  if (tts_ops && desc && (context->base.flags & PGJIT_DEFORM))
317  {
318  l_jit_deform =
319  slot_compile_deform(context, desc,
320  tts_ops,
321  op->d.fetch.last_var);
322  }
323 
324  if (l_jit_deform)
325  {
326  LLVMValueRef params[1];
327 
328  params[0] = v_slot;
329 
330  LLVMBuildCall(b, l_jit_deform,
331  params, lengthof(params), "");
332  }
333  else
334  {
335  LLVMValueRef params[2];
336 
337  params[0] = v_slot;
338  params[1] = l_int32_const(op->d.fetch.last_var);
339 
340  LLVMBuildCall(b,
341  llvm_pg_func(mod, "slot_getsomeattrs_int"),
342  params, lengthof(params), "");
343  }
344 
345  LLVMBuildBr(b, opblocks[opno + 1]);
346  break;
347  }
348 
349  case EEOP_INNER_VAR:
350  case EEOP_OUTER_VAR:
351  case EEOP_SCAN_VAR:
352  {
353  LLVMValueRef value,
354  isnull;
355  LLVMValueRef v_attnum;
356  LLVMValueRef v_values;
357  LLVMValueRef v_nulls;
358 
359  if (opcode == EEOP_INNER_VAR)
360  {
361  v_values = v_innervalues;
362  v_nulls = v_innernulls;
363  }
364  else if (opcode == EEOP_OUTER_VAR)
365  {
366  v_values = v_outervalues;
367  v_nulls = v_outernulls;
368  }
369  else
370  {
371  v_values = v_scanvalues;
372  v_nulls = v_scannulls;
373  }
374 
375  v_attnum = l_int32_const(op->d.var.attnum);
376  value = l_load_gep1(b, v_values, v_attnum, "");
377  isnull = l_load_gep1(b, v_nulls, v_attnum, "");
378  LLVMBuildStore(b, value, v_resvaluep);
379  LLVMBuildStore(b, isnull, v_resnullp);
380 
381  LLVMBuildBr(b, opblocks[opno + 1]);
382  break;
383  }
384 
385  case EEOP_INNER_SYSVAR:
386  case EEOP_OUTER_SYSVAR:
387  case EEOP_SCAN_SYSVAR:
388  {
389  LLVMValueRef v_slot;
390 
391  if (opcode == EEOP_INNER_SYSVAR)
392  v_slot = v_innerslot;
393  else if (opcode == EEOP_OUTER_SYSVAR)
394  v_slot = v_outerslot;
395  else
396  v_slot = v_scanslot;
397 
398  build_EvalXFunc(b, mod, "ExecEvalSysVar",
399  v_state, op, v_econtext, v_slot);
400 
401  LLVMBuildBr(b, opblocks[opno + 1]);
402  break;
403  }
404 
405  case EEOP_WHOLEROW:
406  build_EvalXFunc(b, mod, "ExecEvalWholeRowVar",
407  v_state, op, v_econtext);
408  LLVMBuildBr(b, opblocks[opno + 1]);
409  break;
410 
414  {
415  LLVMValueRef v_value;
416  LLVMValueRef v_isnull;
417  LLVMValueRef v_rvaluep;
418  LLVMValueRef v_risnullp;
419  LLVMValueRef v_attnum;
420  LLVMValueRef v_resultnum;
421  LLVMValueRef v_values;
422  LLVMValueRef v_nulls;
423 
424  if (opcode == EEOP_ASSIGN_INNER_VAR)
425  {
426  v_values = v_innervalues;
427  v_nulls = v_innernulls;
428  }
429  else if (opcode == EEOP_ASSIGN_OUTER_VAR)
430  {
431  v_values = v_outervalues;
432  v_nulls = v_outernulls;
433  }
434  else
435  {
436  v_values = v_scanvalues;
437  v_nulls = v_scannulls;
438  }
439 
440  /* load data */
441  v_attnum = l_int32_const(op->d.assign_var.attnum);
442  v_value = l_load_gep1(b, v_values, v_attnum, "");
443  v_isnull = l_load_gep1(b, v_nulls, v_attnum, "");
444 
445  /* compute addresses of targets */
446  v_resultnum = l_int32_const(op->d.assign_var.resultnum);
447  v_rvaluep = LLVMBuildGEP(b, v_resultvalues,
448  &v_resultnum, 1, "");
449  v_risnullp = LLVMBuildGEP(b, v_resultnulls,
450  &v_resultnum, 1, "");
451 
452  /* and store */
453  LLVMBuildStore(b, v_value, v_rvaluep);
454  LLVMBuildStore(b, v_isnull, v_risnullp);
455 
456  LLVMBuildBr(b, opblocks[opno + 1]);
457  break;
458  }
459 
460  case EEOP_ASSIGN_TMP:
462  {
463  LLVMValueRef v_value,
464  v_isnull;
465  LLVMValueRef v_rvaluep,
466  v_risnullp;
467  LLVMValueRef v_resultnum;
468  size_t resultnum = op->d.assign_tmp.resultnum;
469 
470  /* load data */
471  v_value = LLVMBuildLoad(b, v_tmpvaluep, "");
472  v_isnull = LLVMBuildLoad(b, v_tmpisnullp, "");
473 
474  /* compute addresses of targets */
475  v_resultnum = l_int32_const(resultnum);
476  v_rvaluep =
477  LLVMBuildGEP(b, v_resultvalues, &v_resultnum, 1, "");
478  v_risnullp =
479  LLVMBuildGEP(b, v_resultnulls, &v_resultnum, 1, "");
480 
481  /* store nullness */
482  LLVMBuildStore(b, v_isnull, v_risnullp);
483 
484  /* make value readonly if necessary */
485  if (opcode == EEOP_ASSIGN_TMP_MAKE_RO)
486  {
487  LLVMBasicBlockRef b_notnull;
488  LLVMValueRef v_params[1];
489 
490  b_notnull = l_bb_before_v(opblocks[opno + 1],
491  "op.%d.assign_tmp.notnull", opno);
492 
493  /* check if value is NULL */
494  LLVMBuildCondBr(b,
495  LLVMBuildICmp(b, LLVMIntEQ, v_isnull,
496  l_sbool_const(0), ""),
497  b_notnull, opblocks[opno + 1]);
498 
499  /* if value is not null, convert to RO datum */
500  LLVMPositionBuilderAtEnd(b, b_notnull);
501  v_params[0] = v_value;
502  v_value =
503  LLVMBuildCall(b,
504  llvm_pg_func(mod, "MakeExpandedObjectReadOnlyInternal"),
505  v_params, lengthof(v_params), "");
506 
507  /*
508  * Falling out of the if () with builder in b_notnull,
509  * which is fine - the null is already stored above.
510  */
511  }
512 
513  /* and finally store result */
514  LLVMBuildStore(b, v_value, v_rvaluep);
515 
516  LLVMBuildBr(b, opblocks[opno + 1]);
517  break;
518  }
519 
520  case EEOP_CONST:
521  {
522  LLVMValueRef v_constvalue,
523  v_constnull;
524 
525  v_constvalue = l_sizet_const(op->d.constval.value);
526  v_constnull = l_sbool_const(op->d.constval.isnull);
527 
528  LLVMBuildStore(b, v_constvalue, v_resvaluep);
529  LLVMBuildStore(b, v_constnull, v_resnullp);
530 
531  LLVMBuildBr(b, opblocks[opno + 1]);
532  break;
533  }
534 
535  case EEOP_FUNCEXPR:
537  {
538  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
539  LLVMValueRef v_fcinfo_isnull;
540  LLVMValueRef v_retval;
541 
542  if (opcode == EEOP_FUNCEXPR_STRICT)
543  {
544  LLVMBasicBlockRef b_nonull;
545  LLVMBasicBlockRef *b_checkargnulls;
546  LLVMValueRef v_fcinfo;
547 
548  /*
549  * Block for the actual function call, if args are
550  * non-NULL.
551  */
552  b_nonull = l_bb_before_v(opblocks[opno + 1],
553  "b.%d.no-null-args", opno);
554 
555  /* should make sure they're optimized beforehand */
556  if (op->d.func.nargs == 0)
557  elog(ERROR, "argumentless strict functions are pointless");
558 
559  v_fcinfo =
560  l_ptr_const(fcinfo, l_ptr(StructFunctionCallInfoData));
561 
562  /*
563  * set resnull to true, if the function is actually
564  * called, it'll be reset
565  */
566  LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
567 
568  /* create blocks for checking args, one for each */
569  b_checkargnulls =
570  palloc(sizeof(LLVMBasicBlockRef *) * op->d.func.nargs);
571  for (int argno = 0; argno < op->d.func.nargs; argno++)
572  b_checkargnulls[argno] =
573  l_bb_before_v(b_nonull, "b.%d.isnull.%d", opno,
574  argno);
575 
576  /* jump to check of first argument */
577  LLVMBuildBr(b, b_checkargnulls[0]);
578 
579  /* check each arg for NULLness */
580  for (int argno = 0; argno < op->d.func.nargs; argno++)
581  {
582  LLVMValueRef v_argisnull;
583  LLVMBasicBlockRef b_argnotnull;
584 
585  LLVMPositionBuilderAtEnd(b, b_checkargnulls[argno]);
586 
587  /*
588  * Compute block to jump to if argument is not
589  * null.
590  */
591  if (argno + 1 == op->d.func.nargs)
592  b_argnotnull = b_nonull;
593  else
594  b_argnotnull = b_checkargnulls[argno + 1];
595 
596  /* and finally load & check NULLness of arg */
597  v_argisnull = l_funcnull(b, v_fcinfo, argno);
598  LLVMBuildCondBr(b,
599  LLVMBuildICmp(b, LLVMIntEQ,
600  v_argisnull,
601  l_sbool_const(1),
602  ""),
603  opblocks[opno + 1],
604  b_argnotnull);
605  }
606 
607  LLVMPositionBuilderAtEnd(b, b_nonull);
608  }
609 
610  v_retval = BuildV1Call(context, b, mod, fcinfo,
611  &v_fcinfo_isnull);
612  LLVMBuildStore(b, v_retval, v_resvaluep);
613  LLVMBuildStore(b, v_fcinfo_isnull, v_resnullp);
614 
615  LLVMBuildBr(b, opblocks[opno + 1]);
616  break;
617  }
618 
620  build_EvalXFunc(b, mod, "ExecEvalFuncExprFusage",
621  v_state, op, v_econtext);
622  LLVMBuildBr(b, opblocks[opno + 1]);
623  break;
624 
625 
627  build_EvalXFunc(b, mod, "ExecEvalFuncExprStrictFusage",
628  v_state, op, v_econtext);
629  LLVMBuildBr(b, opblocks[opno + 1]);
630  break;
631 
632  /*
633  * Treat them the same for now, optimizer can remove
634  * redundancy. Could be worthwhile to optimize during emission
635  * though.
636  */
638  case EEOP_BOOL_AND_STEP:
640  {
641  LLVMValueRef v_boolvalue;
642  LLVMValueRef v_boolnull;
643  LLVMValueRef v_boolanynullp,
644  v_boolanynull;
645  LLVMBasicBlockRef b_boolisnull;
646  LLVMBasicBlockRef b_boolcheckfalse;
647  LLVMBasicBlockRef b_boolisfalse;
648  LLVMBasicBlockRef b_boolcont;
649  LLVMBasicBlockRef b_boolisanynull;
650 
651  b_boolisnull = l_bb_before_v(opblocks[opno + 1],
652  "b.%d.boolisnull", opno);
653  b_boolcheckfalse = l_bb_before_v(opblocks[opno + 1],
654  "b.%d.boolcheckfalse", opno);
655  b_boolisfalse = l_bb_before_v(opblocks[opno + 1],
656  "b.%d.boolisfalse", opno);
657  b_boolisanynull = l_bb_before_v(opblocks[opno + 1],
658  "b.%d.boolisanynull", opno);
659  b_boolcont = l_bb_before_v(opblocks[opno + 1],
660  "b.%d.boolcont", opno);
661 
662  v_boolanynullp = l_ptr_const(op->d.boolexpr.anynull,
663  l_ptr(TypeStorageBool));
664 
665  if (opcode == EEOP_BOOL_AND_STEP_FIRST)
666  LLVMBuildStore(b, l_sbool_const(0), v_boolanynullp);
667 
668  v_boolnull = LLVMBuildLoad(b, v_resnullp, "");
669  v_boolvalue = LLVMBuildLoad(b, v_resvaluep, "");
670 
671  /* set resnull to boolnull */
672  LLVMBuildStore(b, v_boolnull, v_resnullp);
673  /* set revalue to boolvalue */
674  LLVMBuildStore(b, v_boolvalue, v_resvaluep);
675 
676  /* check if current input is NULL */
677  LLVMBuildCondBr(b,
678  LLVMBuildICmp(b, LLVMIntEQ, v_boolnull,
679  l_sbool_const(1), ""),
680  b_boolisnull,
681  b_boolcheckfalse);
682 
683  /* build block that sets anynull */
684  LLVMPositionBuilderAtEnd(b, b_boolisnull);
685  /* set boolanynull to true */
686  LLVMBuildStore(b, l_sbool_const(1), v_boolanynullp);
687  /* and jump to next block */
688  LLVMBuildBr(b, b_boolcont);
689 
690  /* build block checking for false */
691  LLVMPositionBuilderAtEnd(b, b_boolcheckfalse);
692  LLVMBuildCondBr(b,
693  LLVMBuildICmp(b, LLVMIntEQ, v_boolvalue,
694  l_sizet_const(0), ""),
695  b_boolisfalse,
696  b_boolcont);
697 
698  /*
699  * Build block handling FALSE. Value is false, so short
700  * circuit.
701  */
702  LLVMPositionBuilderAtEnd(b, b_boolisfalse);
703  /* result is already set to FALSE, need not change it */
704  /* and jump to the end of the AND expression */
705  LLVMBuildBr(b, opblocks[op->d.boolexpr.jumpdone]);
706 
707  /* Build block that continues if bool is TRUE. */
708  LLVMPositionBuilderAtEnd(b, b_boolcont);
709 
710  v_boolanynull = LLVMBuildLoad(b, v_boolanynullp, "");
711 
712  /* set value to NULL if any previous values were NULL */
713  LLVMBuildCondBr(b,
714  LLVMBuildICmp(b, LLVMIntEQ, v_boolanynull,
715  l_sbool_const(0), ""),
716  opblocks[opno + 1], b_boolisanynull);
717 
718  LLVMPositionBuilderAtEnd(b, b_boolisanynull);
719  /* set resnull to true */
720  LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
721  /* reset resvalue */
722  LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
723 
724  LLVMBuildBr(b, opblocks[opno + 1]);
725  break;
726  }
727 
728  /*
729  * Treat them the same for now, optimizer can remove
730  * redundancy. Could be worthwhile to optimize during emission
731  * though.
732  */
734  case EEOP_BOOL_OR_STEP:
736  {
737  LLVMValueRef v_boolvalue;
738  LLVMValueRef v_boolnull;
739  LLVMValueRef v_boolanynullp,
740  v_boolanynull;
741 
742  LLVMBasicBlockRef b_boolisnull;
743  LLVMBasicBlockRef b_boolchecktrue;
744  LLVMBasicBlockRef b_boolistrue;
745  LLVMBasicBlockRef b_boolcont;
746  LLVMBasicBlockRef b_boolisanynull;
747 
748  b_boolisnull = l_bb_before_v(opblocks[opno + 1],
749  "b.%d.boolisnull", opno);
750  b_boolchecktrue = l_bb_before_v(opblocks[opno + 1],
751  "b.%d.boolchecktrue", opno);
752  b_boolistrue = l_bb_before_v(opblocks[opno + 1],
753  "b.%d.boolistrue", opno);
754  b_boolisanynull = l_bb_before_v(opblocks[opno + 1],
755  "b.%d.boolisanynull", opno);
756  b_boolcont = l_bb_before_v(opblocks[opno + 1],
757  "b.%d.boolcont", opno);
758 
759  v_boolanynullp = l_ptr_const(op->d.boolexpr.anynull,
760  l_ptr(TypeStorageBool));
761 
762  if (opcode == EEOP_BOOL_OR_STEP_FIRST)
763  LLVMBuildStore(b, l_sbool_const(0), v_boolanynullp);
764  v_boolnull = LLVMBuildLoad(b, v_resnullp, "");
765  v_boolvalue = LLVMBuildLoad(b, v_resvaluep, "");
766 
767  /* set resnull to boolnull */
768  LLVMBuildStore(b, v_boolnull, v_resnullp);
769  /* set revalue to boolvalue */
770  LLVMBuildStore(b, v_boolvalue, v_resvaluep);
771 
772  LLVMBuildCondBr(b,
773  LLVMBuildICmp(b, LLVMIntEQ, v_boolnull,
774  l_sbool_const(1), ""),
775  b_boolisnull,
776  b_boolchecktrue);
777 
778  /* build block that sets anynull */
779  LLVMPositionBuilderAtEnd(b, b_boolisnull);
780  /* set boolanynull to true */
781  LLVMBuildStore(b, l_sbool_const(1), v_boolanynullp);
782  /* and jump to next block */
783  LLVMBuildBr(b, b_boolcont);
784 
785  /* build block checking for true */
786  LLVMPositionBuilderAtEnd(b, b_boolchecktrue);
787  LLVMBuildCondBr(b,
788  LLVMBuildICmp(b, LLVMIntEQ, v_boolvalue,
789  l_sizet_const(1), ""),
790  b_boolistrue,
791  b_boolcont);
792 
793  /*
794  * Build block handling True. Value is true, so short
795  * circuit.
796  */
797  LLVMPositionBuilderAtEnd(b, b_boolistrue);
798  /* result is already set to TRUE, need not change it */
799  /* and jump to the end of the OR expression */
800  LLVMBuildBr(b, opblocks[op->d.boolexpr.jumpdone]);
801 
802  /* build block that continues if bool is FALSE */
803  LLVMPositionBuilderAtEnd(b, b_boolcont);
804 
805  v_boolanynull = LLVMBuildLoad(b, v_boolanynullp, "");
806 
807  /* set value to NULL if any previous values were NULL */
808  LLVMBuildCondBr(b,
809  LLVMBuildICmp(b, LLVMIntEQ, v_boolanynull,
810  l_sbool_const(0), ""),
811  opblocks[opno + 1], b_boolisanynull);
812 
813  LLVMPositionBuilderAtEnd(b, b_boolisanynull);
814  /* set resnull to true */
815  LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
816  /* reset resvalue */
817  LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
818 
819  LLVMBuildBr(b, opblocks[opno + 1]);
820  break;
821  }
822 
823  case EEOP_BOOL_NOT_STEP:
824  {
825  LLVMValueRef v_boolvalue;
826  LLVMValueRef v_boolnull;
827  LLVMValueRef v_negbool;
828 
829  v_boolnull = LLVMBuildLoad(b, v_resnullp, "");
830  v_boolvalue = LLVMBuildLoad(b, v_resvaluep, "");
831 
832  v_negbool = LLVMBuildZExt(b,
833  LLVMBuildICmp(b, LLVMIntEQ,
834  v_boolvalue,
835  l_sizet_const(0),
836  ""),
837  TypeSizeT, "");
838  /* set resnull to boolnull */
839  LLVMBuildStore(b, v_boolnull, v_resnullp);
840  /* set revalue to !boolvalue */
841  LLVMBuildStore(b, v_negbool, v_resvaluep);
842 
843  LLVMBuildBr(b, opblocks[opno + 1]);
844  break;
845  }
846 
847  case EEOP_QUAL:
848  {
849  LLVMValueRef v_resnull;
850  LLVMValueRef v_resvalue;
851  LLVMValueRef v_nullorfalse;
852  LLVMBasicBlockRef b_qualfail;
853 
854  b_qualfail = l_bb_before_v(opblocks[opno + 1],
855  "op.%d.qualfail", opno);
856 
857  v_resvalue = LLVMBuildLoad(b, v_resvaluep, "");
858  v_resnull = LLVMBuildLoad(b, v_resnullp, "");
859 
860  v_nullorfalse =
861  LLVMBuildOr(b,
862  LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
863  l_sbool_const(1), ""),
864  LLVMBuildICmp(b, LLVMIntEQ, v_resvalue,
865  l_sizet_const(0), ""),
866  "");
867 
868  LLVMBuildCondBr(b,
869  v_nullorfalse,
870  b_qualfail,
871  opblocks[opno + 1]);
872 
873  /* build block handling NULL or false */
874  LLVMPositionBuilderAtEnd(b, b_qualfail);
875  /* set resnull to false */
876  LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
877  /* set resvalue to false */
878  LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
879  /* and jump out */
880  LLVMBuildBr(b, opblocks[op->d.qualexpr.jumpdone]);
881  break;
882  }
883 
884  case EEOP_JUMP:
885  {
886  LLVMBuildBr(b, opblocks[op->d.jump.jumpdone]);
887  break;
888  }
889 
890  case EEOP_JUMP_IF_NULL:
891  {
892  LLVMValueRef v_resnull;
893 
894  /* Transfer control if current result is null */
895 
896  v_resnull = LLVMBuildLoad(b, v_resnullp, "");
897 
898  LLVMBuildCondBr(b,
899  LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
900  l_sbool_const(1), ""),
901  opblocks[op->d.jump.jumpdone],
902  opblocks[opno + 1]);
903  break;
904  }
905 
907  {
908  LLVMValueRef v_resnull;
909 
910  /* Transfer control if current result is non-null */
911 
912  v_resnull = LLVMBuildLoad(b, v_resnullp, "");
913 
914  LLVMBuildCondBr(b,
915  LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
916  l_sbool_const(0), ""),
917  opblocks[op->d.jump.jumpdone],
918  opblocks[opno + 1]);
919  break;
920  }
921 
922 
924  {
925  LLVMValueRef v_resnull;
926  LLVMValueRef v_resvalue;
927  LLVMValueRef v_nullorfalse;
928 
929  /* Transfer control if current result is null or false */
930 
931  v_resvalue = LLVMBuildLoad(b, v_resvaluep, "");
932  v_resnull = LLVMBuildLoad(b, v_resnullp, "");
933 
934  v_nullorfalse =
935  LLVMBuildOr(b,
936  LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
937  l_sbool_const(1), ""),
938  LLVMBuildICmp(b, LLVMIntEQ, v_resvalue,
939  l_sizet_const(0), ""),
940  "");
941 
942  LLVMBuildCondBr(b,
943  v_nullorfalse,
944  opblocks[op->d.jump.jumpdone],
945  opblocks[opno + 1]);
946  break;
947  }
948 
950  {
951  LLVMValueRef v_resnull = LLVMBuildLoad(b, v_resnullp, "");
952  LLVMValueRef v_resvalue;
953 
954  v_resvalue =
955  LLVMBuildSelect(b,
956  LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
957  l_sbool_const(1), ""),
958  l_sizet_const(1),
959  l_sizet_const(0),
960  "");
961  LLVMBuildStore(b, v_resvalue, v_resvaluep);
962  LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
963 
964  LLVMBuildBr(b, opblocks[opno + 1]);
965  break;
966  }
967 
969  {
970  LLVMValueRef v_resnull = LLVMBuildLoad(b, v_resnullp, "");
971  LLVMValueRef v_resvalue;
972 
973  v_resvalue =
974  LLVMBuildSelect(b,
975  LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
976  l_sbool_const(1), ""),
977  l_sizet_const(0),
978  l_sizet_const(1),
979  "");
980  LLVMBuildStore(b, v_resvalue, v_resvaluep);
981  LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
982 
983  LLVMBuildBr(b, opblocks[opno + 1]);
984  break;
985  }
986 
988  build_EvalXFunc(b, mod, "ExecEvalRowNull",
989  v_state, op, v_econtext);
990  LLVMBuildBr(b, opblocks[opno + 1]);
991  break;
992 
994  build_EvalXFunc(b, mod, "ExecEvalRowNotNull",
995  v_state, op, v_econtext);
996  LLVMBuildBr(b, opblocks[opno + 1]);
997  break;
998 
1003  {
1004  LLVMBasicBlockRef b_isnull,
1005  b_notnull;
1006  LLVMValueRef v_resnull = LLVMBuildLoad(b, v_resnullp, "");
1007 
1008  b_isnull = l_bb_before_v(opblocks[opno + 1],
1009  "op.%d.isnull", opno);
1010  b_notnull = l_bb_before_v(opblocks[opno + 1],
1011  "op.%d.isnotnull", opno);
1012 
1013  /* check if value is NULL */
1014  LLVMBuildCondBr(b,
1015  LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
1016  l_sbool_const(1), ""),
1017  b_isnull, b_notnull);
1018 
1019  /* if value is NULL, return false */
1020  LLVMPositionBuilderAtEnd(b, b_isnull);
1021 
1022  /* result is not null */
1023  LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
1024 
1025  if (opcode == EEOP_BOOLTEST_IS_TRUE ||
1026  opcode == EEOP_BOOLTEST_IS_FALSE)
1027  {
1028  LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
1029  }
1030  else
1031  {
1032  LLVMBuildStore(b, l_sizet_const(1), v_resvaluep);
1033  }
1034 
1035  LLVMBuildBr(b, opblocks[opno + 1]);
1036 
1037  LLVMPositionBuilderAtEnd(b, b_notnull);
1038 
1039  if (opcode == EEOP_BOOLTEST_IS_TRUE ||
1040  opcode == EEOP_BOOLTEST_IS_NOT_FALSE)
1041  {
1042  /*
1043  * if value is not null NULL, return value (already
1044  * set)
1045  */
1046  }
1047  else
1048  {
1049  LLVMValueRef v_value =
1050  LLVMBuildLoad(b, v_resvaluep, "");
1051 
1052  v_value = LLVMBuildZExt(b,
1053  LLVMBuildICmp(b, LLVMIntEQ,
1054  v_value,
1055  l_sizet_const(0),
1056  ""),
1057  TypeSizeT, "");
1058  LLVMBuildStore(b, v_value, v_resvaluep);
1059  }
1060  LLVMBuildBr(b, opblocks[opno + 1]);
1061  break;
1062  }
1063 
1064  case EEOP_PARAM_EXEC:
1065  build_EvalXFunc(b, mod, "ExecEvalParamExec",
1066  v_state, op, v_econtext);
1067  LLVMBuildBr(b, opblocks[opno + 1]);
1068  break;
1069 
1070  case EEOP_PARAM_EXTERN:
1071  build_EvalXFunc(b, mod, "ExecEvalParamExtern",
1072  v_state, op, v_econtext);
1073  LLVMBuildBr(b, opblocks[opno + 1]);
1074  break;
1075 
1076  case EEOP_PARAM_CALLBACK:
1077  {
1078  LLVMTypeRef v_functype;
1079  LLVMValueRef v_func;
1080  LLVMValueRef v_params[3];
1081 
1082  v_functype = llvm_pg_var_func_type("TypeExecEvalSubroutine");
1083  v_func = l_ptr_const(op->d.cparam.paramfunc,
1084  LLVMPointerType(v_functype, 0));
1085 
1086  v_params[0] = v_state;
1087  v_params[1] = l_ptr_const(op, l_ptr(StructExprEvalStep));
1088  v_params[2] = v_econtext;
1089  LLVMBuildCall(b,
1090  v_func,
1091  v_params, lengthof(v_params), "");
1092 
1093  LLVMBuildBr(b, opblocks[opno + 1]);
1094  break;
1095  }
1096 
1098  {
1099  int jumpdone = op->d.sbsref_subscript.jumpdone;
1100  LLVMTypeRef v_functype;
1101  LLVMValueRef v_func;
1102  LLVMValueRef v_params[3];
1103  LLVMValueRef v_ret;
1104 
1105  v_functype = llvm_pg_var_func_type("TypeExecEvalBoolSubroutine");
1106  v_func = l_ptr_const(op->d.sbsref_subscript.subscriptfunc,
1107  LLVMPointerType(v_functype, 0));
1108 
1109  v_params[0] = v_state;
1110  v_params[1] = l_ptr_const(op, l_ptr(StructExprEvalStep));
1111  v_params[2] = v_econtext;
1112  v_ret = LLVMBuildCall(b,
1113  v_func,
1114  v_params, lengthof(v_params), "");
1115  v_ret = LLVMBuildZExt(b, v_ret, TypeStorageBool, "");
1116 
1117  LLVMBuildCondBr(b,
1118  LLVMBuildICmp(b, LLVMIntEQ, v_ret,
1119  l_sbool_const(1), ""),
1120  opblocks[opno + 1],
1121  opblocks[jumpdone]);
1122  break;
1123  }
1124 
1125  case EEOP_SBSREF_OLD:
1126  case EEOP_SBSREF_ASSIGN:
1127  case EEOP_SBSREF_FETCH:
1128  {
1129  LLVMTypeRef v_functype;
1130  LLVMValueRef v_func;
1131  LLVMValueRef v_params[3];
1132 
1133  v_functype = llvm_pg_var_func_type("TypeExecEvalSubroutine");
1134  v_func = l_ptr_const(op->d.sbsref.subscriptfunc,
1135  LLVMPointerType(v_functype, 0));
1136 
1137  v_params[0] = v_state;
1138  v_params[1] = l_ptr_const(op, l_ptr(StructExprEvalStep));
1139  v_params[2] = v_econtext;
1140  LLVMBuildCall(b,
1141  v_func,
1142  v_params, lengthof(v_params), "");
1143 
1144  LLVMBuildBr(b, opblocks[opno + 1]);
1145  break;
1146  }
1147 
1148  case EEOP_CASE_TESTVAL:
1149  {
1150  LLVMBasicBlockRef b_avail,
1151  b_notavail;
1152  LLVMValueRef v_casevaluep,
1153  v_casevalue;
1154  LLVMValueRef v_casenullp,
1155  v_casenull;
1156  LLVMValueRef v_casevaluenull;
1157 
1158  b_avail = l_bb_before_v(opblocks[opno + 1],
1159  "op.%d.avail", opno);
1160  b_notavail = l_bb_before_v(opblocks[opno + 1],
1161  "op.%d.notavail", opno);
1162 
1163  v_casevaluep = l_ptr_const(op->d.casetest.value,
1164  l_ptr(TypeSizeT));
1165  v_casenullp = l_ptr_const(op->d.casetest.isnull,
1166  l_ptr(TypeStorageBool));
1167 
1168  v_casevaluenull =
1169  LLVMBuildICmp(b, LLVMIntEQ,
1170  LLVMBuildPtrToInt(b, v_casevaluep,
1171  TypeSizeT, ""),
1172  l_sizet_const(0), "");
1173  LLVMBuildCondBr(b, v_casevaluenull, b_notavail, b_avail);
1174 
1175  /* if casetest != NULL */
1176  LLVMPositionBuilderAtEnd(b, b_avail);
1177  v_casevalue = LLVMBuildLoad(b, v_casevaluep, "");
1178  v_casenull = LLVMBuildLoad(b, v_casenullp, "");
1179  LLVMBuildStore(b, v_casevalue, v_resvaluep);
1180  LLVMBuildStore(b, v_casenull, v_resnullp);
1181  LLVMBuildBr(b, opblocks[opno + 1]);
1182 
1183  /* if casetest == NULL */
1184  LLVMPositionBuilderAtEnd(b, b_notavail);
1185  v_casevalue =
1186  l_load_struct_gep(b, v_econtext,
1188  v_casenull =
1189  l_load_struct_gep(b, v_econtext,
1191  LLVMBuildStore(b, v_casevalue, v_resvaluep);
1192  LLVMBuildStore(b, v_casenull, v_resnullp);
1193 
1194  LLVMBuildBr(b, opblocks[opno + 1]);
1195  break;
1196  }
1197 
1198  case EEOP_MAKE_READONLY:
1199  {
1200  LLVMBasicBlockRef b_notnull;
1201  LLVMValueRef v_params[1];
1202  LLVMValueRef v_ret;
1203  LLVMValueRef v_nullp;
1204  LLVMValueRef v_valuep;
1205  LLVMValueRef v_null;
1206  LLVMValueRef v_value;
1207 
1208  b_notnull = l_bb_before_v(opblocks[opno + 1],
1209  "op.%d.readonly.notnull", opno);
1210 
1211  v_nullp = l_ptr_const(op->d.make_readonly.isnull,
1212  l_ptr(TypeStorageBool));
1213 
1214  v_null = LLVMBuildLoad(b, v_nullp, "");
1215 
1216  /* store null isnull value in result */
1217  LLVMBuildStore(b, v_null, v_resnullp);
1218 
1219  /* check if value is NULL */
1220  LLVMBuildCondBr(b,
1221  LLVMBuildICmp(b, LLVMIntEQ, v_null,
1222  l_sbool_const(1), ""),
1223  opblocks[opno + 1], b_notnull);
1224 
1225  /* if value is not null, convert to RO datum */
1226  LLVMPositionBuilderAtEnd(b, b_notnull);
1227 
1228  v_valuep = l_ptr_const(op->d.make_readonly.value,
1229  l_ptr(TypeSizeT));
1230 
1231  v_value = LLVMBuildLoad(b, v_valuep, "");
1232 
1233  v_params[0] = v_value;
1234  v_ret =
1235  LLVMBuildCall(b,
1236  llvm_pg_func(mod, "MakeExpandedObjectReadOnlyInternal"),
1237  v_params, lengthof(v_params), "");
1238  LLVMBuildStore(b, v_ret, v_resvaluep);
1239 
1240  LLVMBuildBr(b, opblocks[opno + 1]);
1241  break;
1242  }
1243 
1244  case EEOP_IOCOERCE:
1245  {
1246  FunctionCallInfo fcinfo_out,
1247  fcinfo_in;
1248  LLVMValueRef v_fn_out,
1249  v_fn_in;
1250  LLVMValueRef v_fcinfo_out,
1251  v_fcinfo_in;
1252  LLVMValueRef v_fcinfo_in_isnullp;
1253  LLVMValueRef v_retval;
1254  LLVMValueRef v_resvalue;
1255  LLVMValueRef v_resnull;
1256 
1257  LLVMValueRef v_output_skip;
1258  LLVMValueRef v_output;
1259 
1260  LLVMBasicBlockRef b_skipoutput;
1261  LLVMBasicBlockRef b_calloutput;
1262  LLVMBasicBlockRef b_input;
1263  LLVMBasicBlockRef b_inputcall;
1264 
1265  fcinfo_out = op->d.iocoerce.fcinfo_data_out;
1266  fcinfo_in = op->d.iocoerce.fcinfo_data_in;
1267 
1268  b_skipoutput = l_bb_before_v(opblocks[opno + 1],
1269  "op.%d.skipoutputnull", opno);
1270  b_calloutput = l_bb_before_v(opblocks[opno + 1],
1271  "op.%d.calloutput", opno);
1272  b_input = l_bb_before_v(opblocks[opno + 1],
1273  "op.%d.input", opno);
1274  b_inputcall = l_bb_before_v(opblocks[opno + 1],
1275  "op.%d.inputcall", opno);
1276 
1277  v_fn_out = llvm_function_reference(context, b, mod, fcinfo_out);
1278  v_fn_in = llvm_function_reference(context, b, mod, fcinfo_in);
1279  v_fcinfo_out = l_ptr_const(fcinfo_out, l_ptr(StructFunctionCallInfoData));
1280  v_fcinfo_in = l_ptr_const(fcinfo_in, l_ptr(StructFunctionCallInfoData));
1281 
1282  v_fcinfo_in_isnullp =
1283  LLVMBuildStructGEP(b, v_fcinfo_in,
1285  "v_fcinfo_in_isnull");
1286 
1287  /* output functions are not called on nulls */
1288  v_resnull = LLVMBuildLoad(b, v_resnullp, "");
1289  LLVMBuildCondBr(b,
1290  LLVMBuildICmp(b, LLVMIntEQ, v_resnull,
1291  l_sbool_const(1), ""),
1292  b_skipoutput,
1293  b_calloutput);
1294 
1295  LLVMPositionBuilderAtEnd(b, b_skipoutput);
1296  v_output_skip = l_sizet_const(0);
1297  LLVMBuildBr(b, b_input);
1298 
1299  LLVMPositionBuilderAtEnd(b, b_calloutput);
1300  v_resvalue = LLVMBuildLoad(b, v_resvaluep, "");
1301 
1302  /* set arg[0] */
1303  LLVMBuildStore(b,
1304  v_resvalue,
1305  l_funcvaluep(b, v_fcinfo_out, 0));
1306  LLVMBuildStore(b,
1307  l_sbool_const(0),
1308  l_funcnullp(b, v_fcinfo_out, 0));
1309  /* and call output function (can never return NULL) */
1310  v_output = LLVMBuildCall(b, v_fn_out, &v_fcinfo_out,
1311  1, "funccall_coerce_out");
1312  LLVMBuildBr(b, b_input);
1313 
1314  /* build block handling input function call */
1315  LLVMPositionBuilderAtEnd(b, b_input);
1316 
1317  /* phi between resnull and output function call branches */
1318  {
1319  LLVMValueRef incoming_values[2];
1320  LLVMBasicBlockRef incoming_blocks[2];
1321 
1322  incoming_values[0] = v_output_skip;
1323  incoming_blocks[0] = b_skipoutput;
1324 
1325  incoming_values[1] = v_output;
1326  incoming_blocks[1] = b_calloutput;
1327 
1328  v_output = LLVMBuildPhi(b, TypeSizeT, "output");
1329  LLVMAddIncoming(v_output,
1330  incoming_values, incoming_blocks,
1331  lengthof(incoming_blocks));
1332  }
1333 
1334  /*
1335  * If input function is strict, skip if input string is
1336  * NULL.
1337  */
1338  if (op->d.iocoerce.finfo_in->fn_strict)
1339  {
1340  LLVMBuildCondBr(b,
1341  LLVMBuildICmp(b, LLVMIntEQ, v_output,
1342  l_sizet_const(0), ""),
1343  opblocks[opno + 1],
1344  b_inputcall);
1345  }
1346  else
1347  {
1348  LLVMBuildBr(b, b_inputcall);
1349  }
1350 
1351  LLVMPositionBuilderAtEnd(b, b_inputcall);
1352  /* set arguments */
1353  /* arg0: output */
1354  LLVMBuildStore(b, v_output,
1355  l_funcvaluep(b, v_fcinfo_in, 0));
1356  LLVMBuildStore(b, v_resnull,
1357  l_funcnullp(b, v_fcinfo_in, 0));
1358 
1359  /* arg1: ioparam: preset in execExpr.c */
1360  /* arg2: typmod: preset in execExpr.c */
1361 
1362  /* reset fcinfo_in->isnull */
1363  LLVMBuildStore(b, l_sbool_const(0), v_fcinfo_in_isnullp);
1364  /* and call function */
1365  v_retval = LLVMBuildCall(b, v_fn_in, &v_fcinfo_in, 1,
1366  "funccall_iocoerce_in");
1367 
1368  LLVMBuildStore(b, v_retval, v_resvaluep);
1369 
1370  LLVMBuildBr(b, opblocks[opno + 1]);
1371  break;
1372  }
1373 
1374  case EEOP_DISTINCT:
1375  case EEOP_NOT_DISTINCT:
1376  {
1377  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1378 
1379  LLVMValueRef v_fcinfo;
1380  LLVMValueRef v_fcinfo_isnull;
1381 
1382  LLVMValueRef v_argnull0,
1383  v_argisnull0;
1384  LLVMValueRef v_argnull1,
1385  v_argisnull1;
1386 
1387  LLVMValueRef v_anyargisnull;
1388  LLVMValueRef v_bothargisnull;
1389 
1390  LLVMValueRef v_result;
1391 
1392  LLVMBasicBlockRef b_noargnull;
1393  LLVMBasicBlockRef b_checkbothargnull;
1394  LLVMBasicBlockRef b_bothargnull;
1395  LLVMBasicBlockRef b_anyargnull;
1396 
1397  b_noargnull = l_bb_before_v(opblocks[opno + 1], "op.%d.noargnull", opno);
1398  b_checkbothargnull = l_bb_before_v(opblocks[opno + 1], "op.%d.checkbothargnull", opno);
1399  b_bothargnull = l_bb_before_v(opblocks[opno + 1], "op.%d.bothargnull", opno);
1400  b_anyargnull = l_bb_before_v(opblocks[opno + 1], "op.%d.anyargnull", opno);
1401 
1402  v_fcinfo = l_ptr_const(fcinfo, l_ptr(StructFunctionCallInfoData));
1403 
1404  /* load args[0|1].isnull for both arguments */
1405  v_argnull0 = l_funcnull(b, v_fcinfo, 0);
1406  v_argisnull0 = LLVMBuildICmp(b, LLVMIntEQ, v_argnull0,
1407  l_sbool_const(1), "");
1408  v_argnull1 = l_funcnull(b, v_fcinfo, 1);
1409  v_argisnull1 = LLVMBuildICmp(b, LLVMIntEQ, v_argnull1,
1410  l_sbool_const(1), "");
1411 
1412  v_anyargisnull = LLVMBuildOr(b, v_argisnull0, v_argisnull1, "");
1413  v_bothargisnull = LLVMBuildAnd(b, v_argisnull0, v_argisnull1, "");
1414 
1415  /*
1416  * Check function arguments for NULLness: If either is
1417  * NULL, we check if both args are NULL. Otherwise call
1418  * comparator.
1419  */
1420  LLVMBuildCondBr(b, v_anyargisnull, b_checkbothargnull,
1421  b_noargnull);
1422 
1423  /*
1424  * build block checking if any arg is null
1425  */
1426  LLVMPositionBuilderAtEnd(b, b_checkbothargnull);
1427  LLVMBuildCondBr(b, v_bothargisnull, b_bothargnull,
1428  b_anyargnull);
1429 
1430 
1431  /* Both NULL? Then is not distinct... */
1432  LLVMPositionBuilderAtEnd(b, b_bothargnull);
1433  LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
1434  if (opcode == EEOP_NOT_DISTINCT)
1435  LLVMBuildStore(b, l_sizet_const(1), v_resvaluep);
1436  else
1437  LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
1438 
1439  LLVMBuildBr(b, opblocks[opno + 1]);
1440 
1441  /* Only one is NULL? Then is distinct... */
1442  LLVMPositionBuilderAtEnd(b, b_anyargnull);
1443  LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
1444  if (opcode == EEOP_NOT_DISTINCT)
1445  LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
1446  else
1447  LLVMBuildStore(b, l_sizet_const(1), v_resvaluep);
1448  LLVMBuildBr(b, opblocks[opno + 1]);
1449 
1450  /* neither argument is null: compare */
1451  LLVMPositionBuilderAtEnd(b, b_noargnull);
1452 
1453  v_result = BuildV1Call(context, b, mod, fcinfo,
1454  &v_fcinfo_isnull);
1455 
1456  if (opcode == EEOP_DISTINCT)
1457  {
1458  /* Must invert result of "=" */
1459  v_result =
1460  LLVMBuildZExt(b,
1461  LLVMBuildICmp(b, LLVMIntEQ,
1462  v_result,
1463  l_sizet_const(0), ""),
1464  TypeSizeT, "");
1465  }
1466 
1467  LLVMBuildStore(b, v_fcinfo_isnull, v_resnullp);
1468  LLVMBuildStore(b, v_result, v_resvaluep);
1469 
1470  LLVMBuildBr(b, opblocks[opno + 1]);
1471  break;
1472  }
1473 
1474  case EEOP_NULLIF:
1475  {
1476  FunctionCallInfo fcinfo = op->d.func.fcinfo_data;
1477 
1478  LLVMValueRef v_fcinfo;
1479  LLVMValueRef v_fcinfo_isnull;
1480  LLVMValueRef v_argnull0;
1481  LLVMValueRef v_argnull1;
1482  LLVMValueRef v_anyargisnull;
1483  LLVMValueRef v_arg0;
1484  LLVMBasicBlockRef b_hasnull;
1485  LLVMBasicBlockRef b_nonull;
1486  LLVMBasicBlockRef b_argsequal;
1487  LLVMValueRef v_retval;
1488  LLVMValueRef v_argsequal;
1489 
1490  b_hasnull = l_bb_before_v(opblocks[opno + 1],
1491  "b.%d.null-args", opno);
1492  b_nonull = l_bb_before_v(opblocks[opno + 1],
1493  "b.%d.no-null-args", opno);
1494  b_argsequal = l_bb_before_v(opblocks[opno + 1],
1495  "b.%d.argsequal", opno);
1496 
1497  v_fcinfo = l_ptr_const(fcinfo, l_ptr(StructFunctionCallInfoData));
1498 
1499  /* if either argument is NULL they can't be equal */
1500  v_argnull0 = l_funcnull(b, v_fcinfo, 0);
1501  v_argnull1 = l_funcnull(b, v_fcinfo, 1);
1502 
1503  v_anyargisnull =
1504  LLVMBuildOr(b,
1505  LLVMBuildICmp(b, LLVMIntEQ, v_argnull0,
1506  l_sbool_const(1), ""),
1507  LLVMBuildICmp(b, LLVMIntEQ, v_argnull1,
1508  l_sbool_const(1), ""),
1509  "");
1510 
1511  LLVMBuildCondBr(b, v_anyargisnull, b_hasnull, b_nonull);
1512 
1513  /* one (or both) of the arguments are null, return arg[0] */
1514  LLVMPositionBuilderAtEnd(b, b_hasnull);
1515  v_arg0 = l_funcvalue(b, v_fcinfo, 0);
1516  LLVMBuildStore(b, v_argnull0, v_resnullp);
1517  LLVMBuildStore(b, v_arg0, v_resvaluep);
1518  LLVMBuildBr(b, opblocks[opno + 1]);
1519 
1520  /* build block to invoke function and check result */
1521  LLVMPositionBuilderAtEnd(b, b_nonull);
1522 
1523  v_retval = BuildV1Call(context, b, mod, fcinfo, &v_fcinfo_isnull);
1524 
1525  /*
1526  * If result not null, and arguments are equal return null
1527  * (same result as if there'd been NULLs, hence reuse
1528  * b_hasnull).
1529  */
1530  v_argsequal = LLVMBuildAnd(b,
1531  LLVMBuildICmp(b, LLVMIntEQ,
1532  v_fcinfo_isnull,
1533  l_sbool_const(0),
1534  ""),
1535  LLVMBuildICmp(b, LLVMIntEQ,
1536  v_retval,
1537  l_sizet_const(1),
1538  ""),
1539  "");
1540  LLVMBuildCondBr(b, v_argsequal, b_argsequal, b_hasnull);
1541 
1542  /* build block setting result to NULL, if args are equal */
1543  LLVMPositionBuilderAtEnd(b, b_argsequal);
1544  LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
1545  LLVMBuildStore(b, l_sizet_const(0), v_resvaluep);
1546  LLVMBuildStore(b, v_retval, v_resvaluep);
1547 
1548  LLVMBuildBr(b, opblocks[opno + 1]);
1549  break;
1550  }
1551 
1552  case EEOP_SQLVALUEFUNCTION:
1553  build_EvalXFunc(b, mod, "ExecEvalSQLValueFunction",
1554  v_state, op);
1555  LLVMBuildBr(b, opblocks[opno + 1]);
1556  break;
1557 
1558  case EEOP_CURRENTOFEXPR:
1559  build_EvalXFunc(b, mod, "ExecEvalCurrentOfExpr",
1560  v_state, op);
1561  LLVMBuildBr(b, opblocks[opno + 1]);
1562  break;
1563 
1564  case EEOP_NEXTVALUEEXPR:
1565  build_EvalXFunc(b, mod, "ExecEvalNextValueExpr",
1566  v_state, op);
1567  LLVMBuildBr(b, opblocks[opno + 1]);
1568  break;
1569 
1570  case EEOP_ARRAYEXPR:
1571  build_EvalXFunc(b, mod, "ExecEvalArrayExpr",
1572  v_state, op);
1573  LLVMBuildBr(b, opblocks[opno + 1]);
1574  break;
1575 
1576  case EEOP_ARRAYCOERCE:
1577  build_EvalXFunc(b, mod, "ExecEvalArrayCoerce",
1578  v_state, op, v_econtext);
1579  LLVMBuildBr(b, opblocks[opno + 1]);
1580  break;
1581 
1582  case EEOP_ROW:
1583  build_EvalXFunc(b, mod, "ExecEvalRow",
1584  v_state, op);
1585  LLVMBuildBr(b, opblocks[opno + 1]);
1586  break;
1587 
1588  case EEOP_ROWCOMPARE_STEP:
1589  {
1590  FunctionCallInfo fcinfo = op->d.rowcompare_step.fcinfo_data;
1591  LLVMValueRef v_fcinfo_isnull;
1592  LLVMBasicBlockRef b_null;
1593  LLVMBasicBlockRef b_compare;
1594  LLVMBasicBlockRef b_compare_result;
1595 
1596  LLVMValueRef v_retval;
1597 
1598  b_null = l_bb_before_v(opblocks[opno + 1],
1599  "op.%d.row-null", opno);
1600  b_compare = l_bb_before_v(opblocks[opno + 1],
1601  "op.%d.row-compare", opno);
1602  b_compare_result =
1603  l_bb_before_v(opblocks[opno + 1],
1604  "op.%d.row-compare-result",
1605  opno);
1606 
1607  /*
1608  * If function is strict, and either arg is null, we're
1609  * done.
1610  */
1611  if (op->d.rowcompare_step.finfo->fn_strict)
1612  {
1613  LLVMValueRef v_fcinfo;
1614  LLVMValueRef v_argnull0;
1615  LLVMValueRef v_argnull1;
1616  LLVMValueRef v_anyargisnull;
1617 
1618  v_fcinfo = l_ptr_const(fcinfo,
1620 
1621  v_argnull0 = l_funcnull(b, v_fcinfo, 0);
1622  v_argnull1 = l_funcnull(b, v_fcinfo, 1);
1623 
1624  v_anyargisnull =
1625  LLVMBuildOr(b,
1626  LLVMBuildICmp(b,
1627  LLVMIntEQ,
1628  v_argnull0,
1629  l_sbool_const(1),
1630  ""),
1631  LLVMBuildICmp(b, LLVMIntEQ,
1632  v_argnull1,
1633  l_sbool_const(1), ""),
1634  "");
1635 
1636  LLVMBuildCondBr(b, v_anyargisnull, b_null, b_compare);
1637  }
1638  else
1639  {
1640  LLVMBuildBr(b, b_compare);
1641  }
1642 
1643  /* build block invoking comparison function */
1644  LLVMPositionBuilderAtEnd(b, b_compare);
1645 
1646  /* call function */
1647  v_retval = BuildV1Call(context, b, mod, fcinfo,
1648  &v_fcinfo_isnull);
1649  LLVMBuildStore(b, v_retval, v_resvaluep);
1650 
1651  /* if result of function is NULL, force NULL result */
1652  LLVMBuildCondBr(b,
1653  LLVMBuildICmp(b,
1654  LLVMIntEQ,
1655  v_fcinfo_isnull,
1656  l_sbool_const(0),
1657  ""),
1658  b_compare_result,
1659  b_null);
1660 
1661  /* build block analyzing the !NULL comparator result */
1662  LLVMPositionBuilderAtEnd(b, b_compare_result);
1663 
1664  /* if results equal, compare next, otherwise done */
1665  LLVMBuildCondBr(b,
1666  LLVMBuildICmp(b,
1667  LLVMIntEQ,
1668  v_retval,
1669  l_sizet_const(0), ""),
1670  opblocks[opno + 1],
1671  opblocks[op->d.rowcompare_step.jumpdone]);
1672 
1673  /*
1674  * Build block handling NULL input or NULL comparator
1675  * result.
1676  */
1677  LLVMPositionBuilderAtEnd(b, b_null);
1678  LLVMBuildStore(b, l_sbool_const(1), v_resnullp);
1679  LLVMBuildBr(b, opblocks[op->d.rowcompare_step.jumpnull]);
1680 
1681  break;
1682  }
1683 
1684  case EEOP_ROWCOMPARE_FINAL:
1685  {
1686  RowCompareType rctype = op->d.rowcompare_final.rctype;
1687 
1688  LLVMValueRef v_cmpresult;
1689  LLVMValueRef v_result;
1690  LLVMIntPredicate predicate;
1691 
1692  /*
1693  * Btree comparators return 32 bit results, need to be
1694  * careful about sign (used as a 64 bit value it's
1695  * otherwise wrong).
1696  */
1697  v_cmpresult =
1698  LLVMBuildTrunc(b,
1699  LLVMBuildLoad(b, v_resvaluep, ""),
1700  LLVMInt32Type(), "");
1701 
1702  switch (rctype)
1703  {
1704  case ROWCOMPARE_LT:
1705  predicate = LLVMIntSLT;
1706  break;
1707  case ROWCOMPARE_LE:
1708  predicate = LLVMIntSLE;
1709  break;
1710  case ROWCOMPARE_GT:
1711  predicate = LLVMIntSGT;
1712  break;
1713  case ROWCOMPARE_GE:
1714  predicate = LLVMIntSGE;
1715  break;
1716  default:
1717  /* EQ and NE cases aren't allowed here */
1718  Assert(false);
1719  predicate = 0; /* prevent compiler warning */
1720  break;
1721  }
1722 
1723  v_result = LLVMBuildICmp(b,
1724  predicate,
1725  v_cmpresult,
1726  l_int32_const(0),
1727  "");
1728  v_result = LLVMBuildZExt(b, v_result, TypeSizeT, "");
1729 
1730  LLVMBuildStore(b, l_sbool_const(0), v_resnullp);
1731  LLVMBuildStore(b, v_result, v_resvaluep);
1732 
1733  LLVMBuildBr(b, opblocks[opno + 1]);
1734  break;
1735  }
1736 
1737  case EEOP_MINMAX:
1738  build_EvalXFunc(b, mod, "ExecEvalMinMax",
1739  v_state, op);
1740  LLVMBuildBr(b, opblocks[opno + 1]);
1741  break;
1742 
1743  case EEOP_FIELDSELECT:
1744  build_EvalXFunc(b, mod, "ExecEvalFieldSelect",
1745  v_state, op, v_econtext);
1746  LLVMBuildBr(b, opblocks[opno + 1]);
1747  break;
1748 
1750  build_EvalXFunc(b, mod, "ExecEvalFieldStoreDeForm",
1751  v_state, op, v_econtext);
1752  LLVMBuildBr(b, opblocks[opno + 1]);
1753  break;
1754 
1755  case EEOP_FIELDSTORE_FORM:
1756  build_EvalXFunc(b, mod, "ExecEvalFieldStoreForm",
1757  v_state, op, v_econtext);
1758  LLVMBuildBr(b, opblocks[opno + 1]);
1759  break;
1760 
1761  case EEOP_DOMAIN_TESTVAL:
1762  {
1763  LLVMBasicBlockRef b_avail,
1764  b_notavail;
1765  LLVMValueRef v_casevaluep,
1766  v_casevalue;
1767  LLVMValueRef v_casenullp,
1768  v_casenull;
1769  LLVMValueRef v_casevaluenull;
1770 
1771  b_avail = l_bb_before_v(opblocks[opno + 1],
1772  "op.%d.avail", opno);
1773  b_notavail = l_bb_before_v(opblocks[opno + 1],
1774  "op.%d.notavail", opno);
1775 
1776  v_casevaluep = l_ptr_const(op->d.casetest.value,
1777  l_ptr(TypeSizeT));
1778  v_casenullp = l_ptr_const(op->d.casetest.isnull,
1779  l_ptr(TypeStorageBool));
1780 
1781  v_casevaluenull =
1782  LLVMBuildICmp(b, LLVMIntEQ,
1783  LLVMBuildPtrToInt(b, v_casevaluep,
1784  TypeSizeT, ""),
1785  l_sizet_const(0), "");
1786  LLVMBuildCondBr(b,
1787  v_casevaluenull,
1788  b_notavail, b_avail);
1789 
1790  /* if casetest != NULL */
1791  LLVMPositionBuilderAtEnd(b, b_avail);
1792  v_casevalue = LLVMBuildLoad(b, v_casevaluep, "");
1793  v_casenull = LLVMBuildLoad(b, v_casenullp, "");
1794  LLVMBuildStore(b, v_casevalue, v_resvaluep);
1795  LLVMBuildStore(b, v_casenull, v_resnullp);
1796  LLVMBuildBr(b, opblocks[opno + 1]);
1797 
1798  /* if casetest == NULL */
1799  LLVMPositionBuilderAtEnd(b, b_notavail);
1800  v_casevalue =
1801  l_load_struct_gep(b, v_econtext,
1803  "");
1804  v_casenull =
1805  l_load_struct_gep(b, v_econtext,
1807  "");
1808  LLVMBuildStore(b, v_casevalue, v_resvaluep);
1809  LLVMBuildStore(b, v_casenull, v_resnullp);
1810 
1811  LLVMBuildBr(b, opblocks[opno + 1]);
1812  break;
1813  }
1814 
1815  case EEOP_DOMAIN_NOTNULL:
1816  build_EvalXFunc(b, mod, "ExecEvalConstraintNotNull",
1817  v_state, op);
1818  LLVMBuildBr(b, opblocks[opno + 1]);
1819  break;
1820 
1821  case EEOP_DOMAIN_CHECK:
1822  build_EvalXFunc(b, mod, "ExecEvalConstraintCheck",
1823  v_state, op);
1824  LLVMBuildBr(b, opblocks[opno + 1]);
1825  break;
1826 
1827  case EEOP_CONVERT_ROWTYPE:
1828  build_EvalXFunc(b, mod, "ExecEvalConvertRowtype",
1829  v_state, op, v_econtext);
1830  LLVMBuildBr(b, opblocks[opno + 1]);
1831  break;
1832 
1833  case EEOP_SCALARARRAYOP:
1834  build_EvalXFunc(b, mod, "ExecEvalScalarArrayOp",
1835  v_state, op);
1836  LLVMBuildBr(b, opblocks[opno + 1]);
1837  break;
1838 
1839  case EEOP_XMLEXPR:
1840  build_EvalXFunc(b, mod, "ExecEvalXmlExpr",
1841  v_state, op);
1842  LLVMBuildBr(b, opblocks[opno + 1]);
1843  break;
1844 
1845  case EEOP_AGGREF:
1846  {
1847  LLVMValueRef v_aggno;
1848  LLVMValueRef value,
1849  isnull;
1850 
1851  v_aggno = l_int32_const(op->d.aggref.aggno);
1852 
1853  /* load agg value / null */
1854  value = l_load_gep1(b, v_aggvalues, v_aggno, "aggvalue");
1855  isnull = l_load_gep1(b, v_aggnulls, v_aggno, "aggnull");
1856 
1857  /* and store result */
1858  LLVMBuildStore(b, value, v_resvaluep);
1859  LLVMBuildStore(b, isnull, v_resnullp);
1860 
1861  LLVMBuildBr(b, opblocks[opno + 1]);
1862  break;
1863  }
1864 
1865  case EEOP_GROUPING_FUNC:
1866  build_EvalXFunc(b, mod, "ExecEvalGroupingFunc",
1867  v_state, op);
1868  LLVMBuildBr(b, opblocks[opno + 1]);
1869  break;
1870 
1871  case EEOP_WINDOW_FUNC:
1872  {
1873  WindowFuncExprState *wfunc = op->d.window_func.wfstate;
1874  LLVMValueRef v_wfuncnop;
1875  LLVMValueRef v_wfuncno;
1876  LLVMValueRef value,
1877  isnull;
1878 
1879  /*
1880  * At this point aggref->wfuncno is not yet set (it's set
1881  * up in ExecInitWindowAgg() after initializing the
1882  * expression). So load it from memory each time round.
1883  */
1884  v_wfuncnop = l_ptr_const(&wfunc->wfuncno,
1885  l_ptr(LLVMInt32Type()));
1886  v_wfuncno = LLVMBuildLoad(b, v_wfuncnop, "v_wfuncno");
1887 
1888  /* load window func value / null */
1889  value = l_load_gep1(b, v_aggvalues, v_wfuncno,
1890  "windowvalue");
1891  isnull = l_load_gep1(b, v_aggnulls, v_wfuncno,
1892  "windownull");
1893 
1894  LLVMBuildStore(b, value, v_resvaluep);
1895  LLVMBuildStore(b, isnull, v_resnullp);
1896 
1897  LLVMBuildBr(b, opblocks[opno + 1]);
1898  break;
1899  }
1900 
1901  case EEOP_SUBPLAN:
1902  build_EvalXFunc(b, mod, "ExecEvalSubPlan",
1903  v_state, op, v_econtext);
1904  LLVMBuildBr(b, opblocks[opno + 1]);
1905  break;
1906 
1908  case EEOP_AGG_DESERIALIZE:
1909  {
1910  AggState *aggstate;
1911  FunctionCallInfo fcinfo = op->d.agg_deserialize.fcinfo_data;
1912 
1913  LLVMValueRef v_retval;
1914  LLVMValueRef v_fcinfo_isnull;
1915  LLVMValueRef v_tmpcontext;
1916  LLVMValueRef v_oldcontext;
1917 
1918  if (opcode == EEOP_AGG_STRICT_DESERIALIZE)
1919  {
1920  LLVMValueRef v_fcinfo;
1921  LLVMValueRef v_argnull0;
1922  LLVMBasicBlockRef b_deserialize;
1923 
1924  b_deserialize = l_bb_before_v(opblocks[opno + 1],
1925  "op.%d.deserialize", opno);
1926 
1927  v_fcinfo = l_ptr_const(fcinfo,
1929  v_argnull0 = l_funcnull(b, v_fcinfo, 0);
1930 
1931  LLVMBuildCondBr(b,
1932  LLVMBuildICmp(b,
1933  LLVMIntEQ,
1934  v_argnull0,
1935  l_sbool_const(1),
1936  ""),
1937  opblocks[op->d.agg_deserialize.jumpnull],
1938  b_deserialize);
1939  LLVMPositionBuilderAtEnd(b, b_deserialize);
1940  }
1941 
1942  aggstate = castNode(AggState, state->parent);
1943  fcinfo = op->d.agg_deserialize.fcinfo_data;
1944 
1945  v_tmpcontext =
1946  l_ptr_const(aggstate->tmpcontext->ecxt_per_tuple_memory,
1947  l_ptr(StructMemoryContextData));
1948  v_oldcontext = l_mcxt_switch(mod, b, v_tmpcontext);
1949  v_retval = BuildV1Call(context, b, mod, fcinfo,
1950  &v_fcinfo_isnull);
1951  l_mcxt_switch(mod, b, v_oldcontext);
1952 
1953  LLVMBuildStore(b, v_retval, v_resvaluep);
1954  LLVMBuildStore(b, v_fcinfo_isnull, v_resnullp);
1955 
1956  LLVMBuildBr(b, opblocks[opno + 1]);
1957  break;
1958  }
1959 
1962  {
1963  int nargs = op->d.agg_strict_input_check.nargs;
1965  bool *nulls = op->d.agg_strict_input_check.nulls;
1966  int jumpnull;
1967 
1968  LLVMValueRef v_argsp;
1969  LLVMValueRef v_nullsp;
1970  LLVMBasicBlockRef *b_checknulls;
1971 
1972  Assert(nargs > 0);
1973 
1974  jumpnull = op->d.agg_strict_input_check.jumpnull;
1975  v_argsp = l_ptr_const(args, l_ptr(StructNullableDatum));
1976  v_nullsp = l_ptr_const(nulls, l_ptr(TypeStorageBool));
1977 
1978  /* create blocks for checking args */
1979  b_checknulls = palloc(sizeof(LLVMBasicBlockRef *) * nargs);
1980  for (int argno = 0; argno < nargs; argno++)
1981  {
1982  b_checknulls[argno] =
1983  l_bb_before_v(opblocks[opno + 1],
1984  "op.%d.check-null.%d",
1985  opno, argno);
1986  }
1987 
1988  LLVMBuildBr(b, b_checknulls[0]);
1989 
1990  /* strict function, check for NULL args */
1991  for (int argno = 0; argno < nargs; argno++)
1992  {
1993  LLVMValueRef v_argno = l_int32_const(argno);
1994  LLVMValueRef v_argisnull;
1995  LLVMBasicBlockRef b_argnotnull;
1996 
1997  LLVMPositionBuilderAtEnd(b, b_checknulls[argno]);
1998 
1999  if (argno + 1 == nargs)
2000  b_argnotnull = opblocks[opno + 1];
2001  else
2002  b_argnotnull = b_checknulls[argno + 1];
2003 
2004  if (opcode == EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
2005  v_argisnull = l_load_gep1(b, v_nullsp, v_argno, "");
2006  else
2007  {
2008  LLVMValueRef v_argn;
2009 
2010  v_argn = LLVMBuildGEP(b, v_argsp, &v_argno, 1, "");
2011  v_argisnull =
2012  l_load_struct_gep(b, v_argn,
2014  "");
2015  }
2016 
2017  LLVMBuildCondBr(b,
2018  LLVMBuildICmp(b,
2019  LLVMIntEQ,
2020  v_argisnull,
2021  l_sbool_const(1), ""),
2022  opblocks[jumpnull],
2023  b_argnotnull);
2024  }
2025 
2026  break;
2027  }
2028 
2030  {
2031  int jumpnull;
2032  LLVMValueRef v_aggstatep;
2033  LLVMValueRef v_allpergroupsp;
2034  LLVMValueRef v_pergroup_allaggs;
2035  LLVMValueRef v_setoff;
2036 
2037  jumpnull = op->d.agg_plain_pergroup_nullcheck.jumpnull;
2038 
2039  /*
2040  * pergroup_allaggs = aggstate->all_pergroups
2041  * [op->d.agg_plain_pergroup_nullcheck.setoff];
2042  */
2043  v_aggstatep = LLVMBuildBitCast(b, v_parent,
2044  l_ptr(StructAggState), "");
2045 
2046  v_allpergroupsp = l_load_struct_gep(b, v_aggstatep,
2048  "aggstate.all_pergroups");
2049 
2050  v_setoff = l_int32_const(op->d.agg_plain_pergroup_nullcheck.setoff);
2051 
2052  v_pergroup_allaggs = l_load_gep1(b, v_allpergroupsp, v_setoff, "");
2053 
2054  LLVMBuildCondBr(b,
2055  LLVMBuildICmp(b, LLVMIntEQ,
2056  LLVMBuildPtrToInt(b, v_pergroup_allaggs, TypeSizeT, ""),
2057  l_sizet_const(0), ""),
2058  opblocks[jumpnull],
2059  opblocks[opno + 1]);
2060  break;
2061  }
2062 
2069  {
2070  AggState *aggstate;
2071  AggStatePerTrans pertrans;
2072  FunctionCallInfo fcinfo;
2073 
2074  LLVMValueRef v_aggstatep;
2075  LLVMValueRef v_fcinfo;
2076  LLVMValueRef v_fcinfo_isnull;
2077 
2078  LLVMValueRef v_transvaluep;
2079  LLVMValueRef v_transnullp;
2080 
2081  LLVMValueRef v_setoff;
2082  LLVMValueRef v_transno;
2083 
2084  LLVMValueRef v_aggcontext;
2085 
2086  LLVMValueRef v_allpergroupsp;
2087  LLVMValueRef v_current_setp;
2088  LLVMValueRef v_current_pertransp;
2089  LLVMValueRef v_curaggcontext;
2090 
2091  LLVMValueRef v_pertransp;
2092 
2093  LLVMValueRef v_pergroupp;
2094 
2095  LLVMValueRef v_retval;
2096 
2097  LLVMValueRef v_tmpcontext;
2098  LLVMValueRef v_oldcontext;
2099 
2100  aggstate = castNode(AggState, state->parent);
2101  pertrans = op->d.agg_trans.pertrans;
2102 
2103  fcinfo = pertrans->transfn_fcinfo;
2104 
2105  v_aggstatep =
2106  LLVMBuildBitCast(b, v_parent, l_ptr(StructAggState), "");
2107  v_pertransp = l_ptr_const(pertrans,
2109 
2110  /*
2111  * pergroup = &aggstate->all_pergroups
2112  * [op->d.agg_strict_trans_check.setoff]
2113  * [op->d.agg_init_trans_check.transno];
2114  */
2115  v_allpergroupsp =
2116  l_load_struct_gep(b, v_aggstatep,
2118  "aggstate.all_pergroups");
2119  v_setoff = l_int32_const(op->d.agg_trans.setoff);
2120  v_transno = l_int32_const(op->d.agg_trans.transno);
2121  v_pergroupp =
2122  LLVMBuildGEP(b,
2123  l_load_gep1(b, v_allpergroupsp, v_setoff, ""),
2124  &v_transno, 1, "");
2125 
2126 
2129  {
2130  LLVMValueRef v_notransvalue;
2131  LLVMBasicBlockRef b_init;
2132  LLVMBasicBlockRef b_no_init;
2133 
2134  v_notransvalue =
2135  l_load_struct_gep(b, v_pergroupp,
2137  "notransvalue");
2138 
2139  b_init = l_bb_before_v(opblocks[opno + 1],
2140  "op.%d.inittrans", opno);
2141  b_no_init = l_bb_before_v(opblocks[opno + 1],
2142  "op.%d.no_inittrans", opno);
2143 
2144  LLVMBuildCondBr(b,
2145  LLVMBuildICmp(b, LLVMIntEQ, v_notransvalue,
2146  l_sbool_const(1), ""),
2147  b_init,
2148  b_no_init);
2149 
2150  /* block to init the transition value if necessary */
2151  {
2152  LLVMValueRef params[4];
2153 
2154  LLVMPositionBuilderAtEnd(b, b_init);
2155 
2156  v_aggcontext = l_ptr_const(op->d.agg_trans.aggcontext,
2157  l_ptr(StructExprContext));
2158 
2159  params[0] = v_aggstatep;
2160  params[1] = v_pertransp;
2161  params[2] = v_pergroupp;
2162  params[3] = v_aggcontext;
2163 
2164  LLVMBuildCall(b,
2165  llvm_pg_func(mod, "ExecAggInitGroup"),
2166  params, lengthof(params),
2167  "");
2168 
2169  LLVMBuildBr(b, opblocks[opno + 1]);
2170 
2171  }
2172 
2173  LLVMPositionBuilderAtEnd(b, b_no_init);
2174  }
2175 
2180  {
2181  LLVMValueRef v_transnull;
2182  LLVMBasicBlockRef b_strictpass;
2183 
2184  b_strictpass = l_bb_before_v(opblocks[opno + 1],
2185  "op.%d.strictpass", opno);
2186  v_transnull =
2187  l_load_struct_gep(b, v_pergroupp,
2189  "transnull");
2190 
2191  LLVMBuildCondBr(b,
2192  LLVMBuildICmp(b, LLVMIntEQ, v_transnull,
2193  l_sbool_const(1), ""),
2194  opblocks[opno + 1],
2195  b_strictpass);
2196 
2197  LLVMPositionBuilderAtEnd(b, b_strictpass);
2198  }
2199 
2200 
2201  v_fcinfo = l_ptr_const(fcinfo,
2203  v_aggcontext = l_ptr_const(op->d.agg_trans.aggcontext,
2204  l_ptr(StructExprContext));
2205 
2206  v_current_setp =
2207  LLVMBuildStructGEP(b,
2208  v_aggstatep,
2210  "aggstate.current_set");
2211  v_curaggcontext =
2212  LLVMBuildStructGEP(b,
2213  v_aggstatep,
2215  "aggstate.curaggcontext");
2216  v_current_pertransp =
2217  LLVMBuildStructGEP(b,
2218  v_aggstatep,
2220  "aggstate.curpertrans");
2221 
2222  /* set aggstate globals */
2223  LLVMBuildStore(b, v_aggcontext, v_curaggcontext);
2224  LLVMBuildStore(b, l_int32_const(op->d.agg_trans.setno),
2225  v_current_setp);
2226  LLVMBuildStore(b, v_pertransp, v_current_pertransp);
2227 
2228  /* invoke transition function in per-tuple context */
2229  v_tmpcontext =
2230  l_ptr_const(aggstate->tmpcontext->ecxt_per_tuple_memory,
2231  l_ptr(StructMemoryContextData));
2232  v_oldcontext = l_mcxt_switch(mod, b, v_tmpcontext);
2233 
2234  /* store transvalue in fcinfo->args[0] */
2235  v_transvaluep =
2236  LLVMBuildStructGEP(b, v_pergroupp,
2238  "transvalue");
2239  v_transnullp =
2240  LLVMBuildStructGEP(b, v_pergroupp,
2242  "transnullp");
2243  LLVMBuildStore(b,
2244  LLVMBuildLoad(b, v_transvaluep,
2245  "transvalue"),
2246  l_funcvaluep(b, v_fcinfo, 0));
2247  LLVMBuildStore(b,
2248  LLVMBuildLoad(b, v_transnullp, "transnull"),
2249  l_funcnullp(b, v_fcinfo, 0));
2250 
2251  /* and invoke transition function */
2252  v_retval = BuildV1Call(context, b, mod, fcinfo,
2253  &v_fcinfo_isnull);
2254 
2255  /*
2256  * For pass-by-ref datatype, must copy the new value into
2257  * aggcontext and free the prior transValue. But if
2258  * transfn returned a pointer to its first input, we don't
2259  * need to do anything. Also, if transfn returned a
2260  * pointer to a R/W expanded object that is already a
2261  * child of the aggcontext, assume we can adopt that value
2262  * without copying it.
2263  */
2266  opcode == EEOP_AGG_PLAIN_TRANS_BYREF)
2267  {
2268  LLVMBasicBlockRef b_call;
2269  LLVMBasicBlockRef b_nocall;
2270  LLVMValueRef v_fn;
2271  LLVMValueRef v_transvalue;
2272  LLVMValueRef v_transnull;
2273  LLVMValueRef v_newval;
2274  LLVMValueRef params[6];
2275 
2276  b_call = l_bb_before_v(opblocks[opno + 1],
2277  "op.%d.transcall", opno);
2278  b_nocall = l_bb_before_v(opblocks[opno + 1],
2279  "op.%d.transnocall", opno);
2280 
2281  v_transvalue = LLVMBuildLoad(b, v_transvaluep, "");
2282  v_transnull = LLVMBuildLoad(b, v_transnullp, "");
2283 
2284  /*
2285  * DatumGetPointer(newVal) !=
2286  * DatumGetPointer(pergroup->transValue))
2287  */
2288  LLVMBuildCondBr(b,
2289  LLVMBuildICmp(b, LLVMIntEQ,
2290  v_transvalue,
2291  v_retval, ""),
2292  b_nocall, b_call);
2293 
2294  /* returned datum not passed datum, reparent */
2295  LLVMPositionBuilderAtEnd(b, b_call);
2296 
2297  params[0] = v_aggstatep;
2298  params[1] = v_pertransp;
2299  params[2] = v_retval;
2300  params[3] = LLVMBuildTrunc(b, v_fcinfo_isnull,
2301  TypeParamBool, "");
2302  params[4] = v_transvalue;
2303  params[5] = LLVMBuildTrunc(b, v_transnull,
2304  TypeParamBool, "");
2305 
2306  v_fn = llvm_pg_func(mod, "ExecAggTransReparent");
2307  v_newval =
2308  LLVMBuildCall(b, v_fn,
2309  params, lengthof(params),
2310  "");
2311 
2312  /* store trans value */
2313  LLVMBuildStore(b, v_newval, v_transvaluep);
2314  LLVMBuildStore(b, v_fcinfo_isnull, v_transnullp);
2315 
2316  l_mcxt_switch(mod, b, v_oldcontext);
2317  LLVMBuildBr(b, opblocks[opno + 1]);
2318 
2319  /* returned datum passed datum, no need to reparent */
2320  LLVMPositionBuilderAtEnd(b, b_nocall);
2321  }
2322 
2323  /* store trans value */
2324  LLVMBuildStore(b, v_retval, v_transvaluep);
2325  LLVMBuildStore(b, v_fcinfo_isnull, v_transnullp);
2326 
2327  l_mcxt_switch(mod, b, v_oldcontext);
2328 
2329  LLVMBuildBr(b, opblocks[opno + 1]);
2330  break;
2331  }
2332 
2334  build_EvalXFunc(b, mod, "ExecEvalAggOrderedTransDatum",
2335  v_state, op, v_econtext);
2336  LLVMBuildBr(b, opblocks[opno + 1]);
2337  break;
2338 
2340  build_EvalXFunc(b, mod, "ExecEvalAggOrderedTransTuple",
2341  v_state, op, v_econtext);
2342  LLVMBuildBr(b, opblocks[opno + 1]);
2343  break;
2344 
2345  case EEOP_LAST:
2346  Assert(false);
2347  break;
2348  }
2349  }
2350 
2351  LLVMDisposeBuilder(b);
2352 
2353  /*
2354  * Don't immediately emit function, instead do so the first time the
2355  * expression is actually evaluated. That allows to emit a lot of
2356  * functions together, avoiding a lot of repeated llvm and memory
2357  * remapping overhead.
2358  */
2359  {
2360 
2361  CompiledExprState *cstate = palloc0(sizeof(CompiledExprState));
2362 
2363  cstate->context = context;
2364  cstate->funcname = funcname;
2365 
2366  state->evalfunc = ExecRunCompiledExpr;
2367  state->evalfunc_private = cstate;
2368  }
2369 
2371 
2372  INSTR_TIME_SET_CURRENT(endtime);
2373  INSTR_TIME_ACCUM_DIFF(context->base.instr.generation_counter,
2374  endtime, starttime);
2375 
2376  return true;
2377 }
#define FIELDNO_FUNCTIONCALLINFODATA_ISNULL
Definition: fmgr.h:91
#define FIELDNO_EXPRCONTEXT_CASENULL
Definition: execnodes.h:251
LLVMValueRef slot_compile_deform(LLVMJitContext *context, TupleDesc desc, const TupleTableSlotOps *ops, int natts)
static LLVMValueRef BuildV1Call(LLVMJitContext *context, LLVMBuilderRef b, LLVMModuleRef mod, FunctionCallInfo fcinfo, LLVMValueRef *v_fcinfo_isnull)
#define FIELDNO_EXPRSTATE_RESVALUE
Definition: execnodes.h:72
#define FIELDNO_EXPRSTATE_PARENT
Definition: execnodes.h:106
LLVMTypeRef StructFunctionCallInfoData
Definition: llvmjit.c:81
#define FIELDNO_NULLABLE_DATUM_ISNULL
Definition: postgres.h:379
struct PlanState * parent
Definition: execnodes.h:107
struct JitContext * es_jit
Definition: execnodes.h:618
Datum * resvalue
Definition: execExpr.h:257
struct ExprEvalStep::@49::@84 window_func
struct ExprEvalStep::@49::@50 fetch
#define castNode(_type_, nodeptr)
Definition: nodes.h:602
LLVMTypeRef StructExprEvalStep
Definition: llvmjit.c:83
struct ExprEvalStep * steps
Definition: execnodes.h:84
LLVMTypeRef TypeSizeT
Definition: llvmjit.c:59
#define FIELDNO_EXPRCONTEXT_SCANTUPLE
Definition: execnodes.h:224
struct ExprEvalStep::@49::@57 boolexpr
struct ExprEvalStep::@49::@58 qualexpr
MemoryContext ecxt_per_tuple_memory
Definition: execnodes.h:233
struct timeval instr_time
Definition: instr_time.h:150
LLVMTypeRef StructNullableDatum
Definition: llvmjit.c:63
const TupleTableSlotOps TTSOpsVirtual
Definition: execTuples.c:83
bool * resnull
Definition: execExpr.h:258
ExprEvalOp
Definition: execExpr.h:49
#define FIELDNO_AGGSTATE_CURAGGCONTEXT
Definition: execnodes.h:2177
#define FIELDNO_TUPLETABLESLOT_ISNULL
Definition: tuptable.h:127
struct ExprEvalStep::@49::@72 rowcompare_final
LLVMValueRef llvm_function_reference(LLVMJitContext *context, LLVMBuilderRef builder, LLVMModuleRef mod, FunctionCallInfo fcinfo)
Definition: llvmjit.c:496
#define INSTR_TIME_ACCUM_DIFF(x, y, z)
Definition: instr_time.h:182
LLVMJitContext * llvm_create_context(int jitFlags)
Definition: llvmjit.c:146
#define lengthof(array)
Definition: c.h:734
EState * state
Definition: execnodes.h:943
struct ExprEvalStep::@49::@86 agg_deserialize
LLVMTypeRef StructAggState
Definition: llvmjit.c:85
FunctionCallInfo transfn_fcinfo
Definition: nodeAgg.h:162
struct ExprEvalStep::@49::@71 rowcompare_step
struct ExprEvalStep::@49::@51 var
ExprContext * tmpcontext
Definition: execnodes.h:2176
struct ExprEvalStep::@49::@62 cparam
static struct @141 value
#define FIELDNO_AGGSTATE_ALL_PERGROUPS
Definition: execnodes.h:2231
ExprEvalOp ExecEvalStepOp(ExprState *state, ExprEvalStep *op)
#define FIELDNO_EXPRCONTEXT_OUTERTUPLE
Definition: execnodes.h:228
#define ERROR
Definition: elog.h:45
union ExprEvalStep::@49 d
#define FIELDNO_EXPRCONTEXT_DOMAINDATUM
Definition: execnodes.h:255
static Datum ExecRunCompiledExpr(ExprState *state, ExprContext *econtext, bool *isNull)
#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUEISNULL
Definition: nodeAgg.h:246
char * llvm_expand_funcname(struct LLVMJitContext *context, const char *basename)
Definition: llvmjit.c:255
LLVMJitContext * context
Definition: llvmjit_expr.c:50
struct ExprEvalStep::@49::@55 constval
ExprStateEvalFunc evalfunc
Definition: execnodes.h:90
int es_jit_flags
Definition: execnodes.h:617
LLVMTypeRef TypeStorageBool
Definition: llvmjit.c:61
#define FIELDNO_TUPLETABLESLOT_NVALID
Definition: tuptable.h:120
LLVMTypeRef StructMemoryContextData
Definition: llvmjit.c:78
#define FIELDNO_EXPRSTATE_RESNULL
Definition: execnodes.h:70
#define FIELDNO_AGGSTATEPERGROUPDATA_TRANSVALUE
Definition: nodeAgg.h:244
LLVMTypeRef StructExprContext
Definition: llvmjit.c:82
#define FIELDNO_AGGSTATE_CURRENT_SET
Definition: execnodes.h:2185
#define FIELDNO_EXPRCONTEXT_AGGNULLS
Definition: execnodes.h:245
void * evalfunc_private
Definition: execnodes.h:96
struct ExprEvalStep::@49::@76 sbsref_subscript
#define FIELDNO_AGGSTATEPERGROUPDATA_NOTRANSVALUE
Definition: nodeAgg.h:249
#define FIELDNO_EXPRCONTEXT_INNERTUPLE
Definition: execnodes.h:226
LLVMTypeRef StructAggStatePerTransData
Definition: llvmjit.c:87
struct ExprEvalStep::@49::@53 assign_var
void * palloc0(Size size)
Definition: mcxt.c:981
struct ExprEvalStep::@49::@87 agg_strict_input_check
#define FIELDNO_EXPRCONTEXT_CASEDATUM
Definition: execnodes.h:249
struct ExprEvalStep::@49::@63 casetest
struct ExprEvalStep::@49::@56 func
struct ExprEvalStep::@49::@64 make_readonly
RowCompareType
Definition: primnodes.h:1080
LLVMModuleRef llvm_mutable_module(LLVMJitContext *context)
Definition: llvmjit.c:230
struct ExprEvalStep::@49::@88 agg_plain_pergroup_nullcheck
#define Assert(condition)
Definition: c.h:804
#define FIELDNO_EXPRSTATE_RESULTSLOT
Definition: execnodes.h:78
LLVMTypeRef TypeParamBool
Definition: llvmjit.c:60
struct ExprEvalStep::@49::@77 sbsref
#define INSTR_TIME_SET_CURRENT(t)
Definition: instr_time.h:156
struct ExprEvalStep::@49::@59 jump
#define FIELDNO_EXPRCONTEXT_DOMAINNULL
Definition: execnodes.h:257
void * palloc(Size size)
Definition: mcxt.c:950
int steps_len
Definition: execnodes.h:103
void llvm_enter_fatal_on_oom(void)
#define FIELDNO_TUPLETABLESLOT_VALUES
Definition: tuptable.h:125
#define elog(elevel,...)
Definition: elog.h:227
LLVMValueRef AttributeTemplate
Definition: llvmjit.c:89
struct ExprEvalStep::@49::@89 agg_trans
struct ExprEvalStep::@49::@54 assign_tmp
struct ExprEvalStep::@49::@65 iocoerce
const char * funcname
Definition: llvmjit_expr.c:51
void llvm_copy_attributes(LLVMValueRef v_from, LLVMValueRef v_to)
Definition: llvmjit.c:475
#define PGJIT_DEFORM
Definition: jit.h:24
#define FIELDNO_EXPRCONTEXT_AGGVALUES
Definition: execnodes.h:243
#define build_EvalXFunc(b, mod, funcname, v_state, op,...)
Definition: llvmjit_expr.c:68
struct ExprEvalStep::@49::@82 aggref
LLVMTypeRef llvm_pg_var_func_type(const char *varname)
Definition: llvmjit.c:399
LLVMValueRef llvm_pg_func(LLVMModuleRef mod, const char *funcname)
Definition: llvmjit.c:419
void llvm_leave_fatal_on_oom(void)
#define FIELDNO_AGGSTATE_CURPERTRANS
Definition: execnodes.h:2180