|
| 1 | +/* |
| 2 | + * llvmjit_emit.h |
| 3 | + * Helpers to make emitting LLVM IR a it more concise and pgindent proof. |
| 4 | + * |
| 5 | + * Copyright (c) 2018, PostgreSQL Global Development Group |
| 6 | + * |
| 7 | + * src/include/lib/llvmjit_emit.h |
| 8 | + */ |
| 9 | +#ifndefLLVMJIT_EMIT_H |
| 10 | +#defineLLVMJIT_EMIT_H |
| 11 | + |
| 12 | + |
| 13 | +#include<llvm-c/Core.h> |
| 14 | + |
| 15 | + |
| 16 | +/* |
| 17 | + * Emit a non-LLVM pointer as an LLVM constant. |
| 18 | + */ |
| 19 | +staticinlineLLVMValueRef |
| 20 | +l_ptr_const(void*ptr,LLVMTypeReftype) |
| 21 | +{ |
| 22 | +LLVMValueRefc=LLVMConstInt(TypeSizeT, (uintptr_t)ptr, false); |
| 23 | + |
| 24 | +returnLLVMConstIntToPtr(c,type); |
| 25 | +} |
| 26 | + |
| 27 | +/* |
| 28 | + * Emit pointer. |
| 29 | + */ |
| 30 | +staticinlineLLVMTypeRef |
| 31 | +l_ptr(LLVMTypeReft) |
| 32 | +{ |
| 33 | +returnLLVMPointerType(t,0); |
| 34 | +} |
| 35 | + |
| 36 | +/* |
| 37 | + * Emit constant integer. |
| 38 | + */ |
| 39 | +staticinlineLLVMValueRef |
| 40 | +l_int8_const(int8i) |
| 41 | +{ |
| 42 | +returnLLVMConstInt(LLVMInt8Type(),i, false); |
| 43 | +} |
| 44 | + |
| 45 | +/* |
| 46 | + * Emit constant integer. |
| 47 | + */ |
| 48 | +staticinlineLLVMValueRef |
| 49 | +l_int16_const(int16i) |
| 50 | +{ |
| 51 | +returnLLVMConstInt(LLVMInt16Type(),i, false); |
| 52 | +} |
| 53 | + |
| 54 | +/* |
| 55 | + * Emit constant integer. |
| 56 | + */ |
| 57 | +staticinlineLLVMValueRef |
| 58 | +l_int32_const(int32i) |
| 59 | +{ |
| 60 | +returnLLVMConstInt(LLVMInt32Type(),i, false); |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * Emit constant integer. |
| 65 | + */ |
| 66 | +staticinlineLLVMValueRef |
| 67 | +l_int64_const(int64i) |
| 68 | +{ |
| 69 | +returnLLVMConstInt(LLVMInt64Type(),i, false); |
| 70 | +} |
| 71 | + |
| 72 | +/* |
| 73 | + * Emit constant integer. |
| 74 | + */ |
| 75 | +staticinlineLLVMValueRef |
| 76 | +l_sizet_const(size_ti) |
| 77 | +{ |
| 78 | +returnLLVMConstInt(TypeSizeT,i, false); |
| 79 | +} |
| 80 | + |
| 81 | +/* |
| 82 | + * Load a pointer member idx from a struct. |
| 83 | + */ |
| 84 | +staticinlineLLVMValueRef |
| 85 | +l_load_struct_gep(LLVMBuilderRefb,LLVMValueRefv,int32idx,constchar*name) |
| 86 | +{ |
| 87 | +LLVMValueRefv_ptr=LLVMBuildStructGEP(b,v,idx,""); |
| 88 | + |
| 89 | +returnLLVMBuildLoad(b,v_ptr,name); |
| 90 | +} |
| 91 | + |
| 92 | +/* |
| 93 | + * Load value of a pointer, after applying one index operation. |
| 94 | + */ |
| 95 | +staticinlineLLVMValueRef |
| 96 | +l_load_gep1(LLVMBuilderRefb,LLVMValueRefv,LLVMValueRefidx,constchar*name) |
| 97 | +{ |
| 98 | +LLVMValueRefv_ptr=LLVMBuildGEP(b,v,&idx,1,""); |
| 99 | + |
| 100 | +returnLLVMBuildLoad(b,v_ptr,name); |
| 101 | +} |
| 102 | + |
| 103 | +/* separate, because pg_attribute_printf(2, 3) can't appear in definition */ |
| 104 | +staticinlineLLVMBasicBlockRefl_bb_before_v(LLVMBasicBlockRefr,constchar*fmt,...)pg_attribute_printf(2,3); |
| 105 | + |
| 106 | +/* |
| 107 | + * Insert a new basic block, just before r, the name being determined by fmt |
| 108 | + * and arguments. |
| 109 | + */ |
| 110 | +staticinlineLLVMBasicBlockRef |
| 111 | +l_bb_before_v(LLVMBasicBlockRefr,constchar*fmt,...) |
| 112 | +{ |
| 113 | +charbuf[512]; |
| 114 | +va_listargs; |
| 115 | + |
| 116 | +va_start(args,fmt); |
| 117 | +vsnprintf(buf,sizeof(buf),fmt,args); |
| 118 | +va_end(args); |
| 119 | + |
| 120 | +returnLLVMInsertBasicBlock(r,buf); |
| 121 | +} |
| 122 | + |
| 123 | +/* separate, because pg_attribute_printf(2, 3) can't appear in definition */ |
| 124 | +staticinlineLLVMBasicBlockRefl_bb_append_v(LLVMValueReff,constchar*fmt,...)pg_attribute_printf(2,3); |
| 125 | + |
| 126 | +/* |
| 127 | + * Insert a new basic block after previous basic blocks, the name being |
| 128 | + * determined by fmt and arguments. |
| 129 | + */ |
| 130 | +staticinlineLLVMBasicBlockRef |
| 131 | +l_bb_append_v(LLVMValueReff,constchar*fmt,...) |
| 132 | +{ |
| 133 | +charbuf[512]; |
| 134 | +va_listargs; |
| 135 | + |
| 136 | +va_start(args,fmt); |
| 137 | +vsnprintf(buf,sizeof(buf),fmt,args); |
| 138 | +va_end(args); |
| 139 | + |
| 140 | +returnLLVMAppendBasicBlock(f,buf); |
| 141 | +} |
| 142 | + |
| 143 | +/* |
| 144 | + * Mark a callsite as readonly. |
| 145 | + */ |
| 146 | +staticinlinevoid |
| 147 | +l_callsite_ro(LLVMValueReff) |
| 148 | +{ |
| 149 | +constcharargname[]="readonly"; |
| 150 | +LLVMAttributeRefref; |
| 151 | + |
| 152 | +ref=LLVMCreateStringAttribute(LLVMGetGlobalContext(), |
| 153 | +argname, |
| 154 | +sizeof(argname)-1, |
| 155 | +NULL,0); |
| 156 | + |
| 157 | +LLVMAddCallSiteAttribute(f,LLVMAttributeFunctionIndex,ref); |
| 158 | +} |
| 159 | + |
| 160 | +/* |
| 161 | + * Mark a callsite as alwaysinline. |
| 162 | + */ |
| 163 | +staticinlinevoid |
| 164 | +l_callsite_alwaysinline(LLVMValueReff) |
| 165 | +{ |
| 166 | +constcharargname[]="alwaysinline"; |
| 167 | +intid; |
| 168 | +LLVMAttributeRefattr; |
| 169 | + |
| 170 | +id=LLVMGetEnumAttributeKindForName(argname, |
| 171 | +sizeof(argname)-1); |
| 172 | +attr=LLVMCreateEnumAttribute(LLVMGetGlobalContext(),id,0); |
| 173 | +LLVMAddCallSiteAttribute(f,LLVMAttributeFunctionIndex,attr); |
| 174 | +} |
| 175 | + |
| 176 | +/* |
| 177 | + * Emit code to switch memory context. |
| 178 | + */ |
| 179 | +staticinlineLLVMValueRef |
| 180 | +l_mcxt_switch(LLVMModuleRefmod,LLVMBuilderRefb,LLVMValueRefnc) |
| 181 | +{ |
| 182 | +constchar*cmc="CurrentMemoryContext"; |
| 183 | +LLVMValueRefcur; |
| 184 | +LLVMValueRefret; |
| 185 | + |
| 186 | +if (!(cur=LLVMGetNamedGlobal(mod,cmc))) |
| 187 | +cur=LLVMAddGlobal(mod,l_ptr(StructMemoryContextData),cmc); |
| 188 | +ret=LLVMBuildLoad(b,cur,cmc); |
| 189 | +LLVMBuildStore(b,nc,cur); |
| 190 | + |
| 191 | +returnret; |
| 192 | +} |
| 193 | +#endif |