1/************************************************************************** 2 * 3 * Copyright 2009 VMware, Inc. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the 8 * "Software"), to deal in the Software without restriction, including 9 * without limitation the rights to use, copy, modify, merge, publish, 10 * distribute, sub license, and/or sell copies of the Software, and to 11 * permit persons to whom the Software is furnished to do so, subject to 12 * the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 * 26 **************************************************************************/ 27 28/** 29 * LLVM control flow build helpers. 30 * 31 * @author Jose Fonseca <jfonseca@vmware.com> 32 */ 33 34#ifndef LP_BLD_FLOW_H 35#define LP_BLD_FLOW_H 36 37 38#include "gallivm/lp_bld.h" 39 40#ifdef __cplusplus 41extern "C" { 42#endif 43 44struct lp_type; 45 46 47/** 48 * Early exit. Useful to skip to the end of a function or block when 49 * the execution mask becomes zero or when there is an error condition. 50 */ 51struct lp_build_skip_context 52{ 53 struct gallivm_state *gallivm; 54 55 /** Block to skip to */ 56 LLVMBasicBlockRef block; 57}; 58 59void 60lp_build_flow_skip_begin(struct lp_build_skip_context *ctx, 61 struct gallivm_state *gallivm); 62 63void 64lp_build_flow_skip_cond_break(struct lp_build_skip_context *ctx, 65 LLVMValueRef cond); 66 67void 68lp_build_flow_skip_end(struct lp_build_skip_context *ctx); 69 70 71struct lp_build_mask_context 72{ 73 struct lp_build_skip_context skip; 74 75 LLVMTypeRef reg_type; 76 LLVMTypeRef var_type; 77 /* 'var' is a pointer (alloca) pointing to 'var_type' */ 78 LLVMValueRef var; 79}; 80 81 82void 83lp_build_mask_begin(struct lp_build_mask_context *mask, 84 struct gallivm_state *gallivm, 85 struct lp_type type, 86 LLVMValueRef value); 87 88LLVMValueRef 89lp_build_mask_value(struct lp_build_mask_context *mask); 90 91/** 92 * Bitwise AND the mask with the given value, if a previous mask was set. 93 */ 94void 95lp_build_mask_update(struct lp_build_mask_context *mask, 96 LLVMValueRef value); 97 98void 99lp_build_mask_force(struct lp_build_mask_context *mask, 100 LLVMValueRef value); 101 102void 103lp_build_mask_check(struct lp_build_mask_context *mask); 104 105LLVMValueRef 106lp_build_mask_end(struct lp_build_mask_context *mask); 107 108 109/** 110 * LLVM's IR doesn't represent for-loops directly. Furthermore it 111 * requires creating code blocks, branches, phi variables, so it 112 * requires a fair amount of code. 113 * 114 * @sa http://www.llvm.org/docs/tutorial/LangImpl5.html#for 115 */ 116struct lp_build_loop_state 117{ 118 LLVMBasicBlockRef block; 119 LLVMValueRef counter_var; 120 LLVMValueRef counter; 121 LLVMTypeRef counter_type; 122 struct gallivm_state *gallivm; 123}; 124 125 126void 127lp_build_loop_begin(struct lp_build_loop_state *state, 128 struct gallivm_state *gallivm, 129 LLVMValueRef start); 130 131void 132lp_build_loop_end(struct lp_build_loop_state *state, 133 LLVMValueRef end, 134 LLVMValueRef step); 135 136void 137lp_build_loop_force_set_counter(struct lp_build_loop_state *state, 138 LLVMValueRef end); 139 140void 141lp_build_loop_force_reload_counter(struct lp_build_loop_state *state); 142void 143lp_build_loop_end_cond(struct lp_build_loop_state *state, 144 LLVMValueRef end, 145 LLVMValueRef step, 146 LLVMIntPredicate cond); 147 148 149/** 150 * Implementation of simple C-style for loops 151 */ 152struct lp_build_for_loop_state 153{ 154 LLVMBasicBlockRef begin; 155 LLVMBasicBlockRef body; 156 LLVMBasicBlockRef exit; 157 LLVMValueRef counter_var; 158 LLVMValueRef counter; 159 LLVMTypeRef counter_type; 160 LLVMValueRef step; 161 LLVMIntPredicate cond; 162 LLVMValueRef end; 163 struct gallivm_state *gallivm; 164}; 165 166void 167lp_build_for_loop_begin(struct lp_build_for_loop_state *state, 168 struct gallivm_state *gallivm, 169 LLVMValueRef start, 170 LLVMIntPredicate llvm_cond, 171 LLVMValueRef end, 172 LLVMValueRef step); 173 174void 175lp_build_for_loop_end(struct lp_build_for_loop_state *state); 176 177 178/** 179 * if/else/endif. 180 */ 181struct lp_build_if_state 182{ 183 struct gallivm_state *gallivm; 184 LLVMValueRef condition; 185 LLVMBasicBlockRef entry_block; 186 LLVMBasicBlockRef true_block; 187 LLVMBasicBlockRef false_block; 188 LLVMBasicBlockRef merge_block; 189}; 190 191 192void 193lp_build_if(struct lp_build_if_state *ctx, 194 struct gallivm_state *gallivm, 195 LLVMValueRef condition); 196 197void 198lp_build_else(struct lp_build_if_state *ctx); 199 200void 201lp_build_endif(struct lp_build_if_state *ctx); 202 203LLVMBasicBlockRef 204lp_build_insert_new_block(struct gallivm_state *gallivm, const char *name); 205 206LLVMValueRef 207lp_build_alloca(struct gallivm_state *gallivm, 208 LLVMTypeRef type, 209 const char *name); 210 211LLVMValueRef 212lp_build_alloca_undef(struct gallivm_state *gallivm, 213 LLVMTypeRef type, 214 const char *name); 215 216LLVMValueRef 217lp_build_array_alloca(struct gallivm_state *gallivm, 218 LLVMTypeRef type, 219 LLVMValueRef count, 220 const char *name); 221 222#ifdef __cplusplus 223} 224#endif 225 226#endif /* !LP_BLD_FLOW_H */ 227