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 77 LLVMValueRef var; 78}; 79 80 81void 82lp_build_mask_begin(struct lp_build_mask_context *mask, 83 struct gallivm_state *gallivm, 84 struct lp_type type, 85 LLVMValueRef value); 86 87LLVMValueRef 88lp_build_mask_value(struct lp_build_mask_context *mask); 89 90/** 91 * Bitwise AND the mask with the given value, if a previous mask was set. 92 */ 93void 94lp_build_mask_update(struct lp_build_mask_context *mask, 95 LLVMValueRef value); 96 97void 98lp_build_mask_force(struct lp_build_mask_context *mask, 99 LLVMValueRef value); 100 101void 102lp_build_mask_check(struct lp_build_mask_context *mask); 103 104LLVMValueRef 105lp_build_mask_end(struct lp_build_mask_context *mask); 106 107 108/** 109 * LLVM's IR doesn't represent for-loops directly. Furthermore it 110 * it requires creating code blocks, branches, phi variables, so it 111 * requires a fair amount of code. 112 * 113 * @sa http://www.llvm.org/docs/tutorial/LangImpl5.html#for 114 */ 115struct lp_build_loop_state 116{ 117 LLVMBasicBlockRef block; 118 LLVMValueRef counter_var; 119 LLVMValueRef counter; 120 struct gallivm_state *gallivm; 121}; 122 123 124void 125lp_build_loop_begin(struct lp_build_loop_state *state, 126 struct gallivm_state *gallivm, 127 LLVMValueRef start); 128 129void 130lp_build_loop_end(struct lp_build_loop_state *state, 131 LLVMValueRef end, 132 LLVMValueRef step); 133 134void 135lp_build_loop_force_set_counter(struct lp_build_loop_state *state, 136 LLVMValueRef end); 137 138void 139lp_build_loop_force_reload_counter(struct lp_build_loop_state *state); 140void 141lp_build_loop_end_cond(struct lp_build_loop_state *state, 142 LLVMValueRef end, 143 LLVMValueRef step, 144 LLVMIntPredicate cond); 145 146 147/** 148 * Implementation of simple C-style for loops 149 */ 150struct lp_build_for_loop_state 151{ 152 LLVMBasicBlockRef begin; 153 LLVMBasicBlockRef body; 154 LLVMBasicBlockRef exit; 155 LLVMValueRef counter_var; 156 LLVMValueRef counter; 157 LLVMValueRef step; 158 LLVMIntPredicate cond; 159 LLVMValueRef end; 160 struct gallivm_state *gallivm; 161}; 162 163void 164lp_build_for_loop_begin(struct lp_build_for_loop_state *state, 165 struct gallivm_state *gallivm, 166 LLVMValueRef start, 167 LLVMIntPredicate llvm_cond, 168 LLVMValueRef end, 169 LLVMValueRef step); 170 171void 172lp_build_for_loop_end(struct lp_build_for_loop_state *state); 173 174 175/** 176 * if/else/endif. 177 */ 178struct lp_build_if_state 179{ 180 struct gallivm_state *gallivm; 181 LLVMValueRef condition; 182 LLVMBasicBlockRef entry_block; 183 LLVMBasicBlockRef true_block; 184 LLVMBasicBlockRef false_block; 185 LLVMBasicBlockRef merge_block; 186}; 187 188 189void 190lp_build_if(struct lp_build_if_state *ctx, 191 struct gallivm_state *gallivm, 192 LLVMValueRef condition); 193 194void 195lp_build_else(struct lp_build_if_state *ctx); 196 197void 198lp_build_endif(struct lp_build_if_state *ctx); 199 200LLVMBasicBlockRef 201lp_build_insert_new_block(struct gallivm_state *gallivm, const char *name); 202 203LLVMValueRef 204lp_build_alloca(struct gallivm_state *gallivm, 205 LLVMTypeRef type, 206 const char *name); 207 208LLVMValueRef 209lp_build_alloca_undef(struct gallivm_state *gallivm, 210 LLVMTypeRef type, 211 const char *name); 212 213LLVMValueRef 214lp_build_array_alloca(struct gallivm_state *gallivm, 215 LLVMTypeRef type, 216 LLVMValueRef count, 217 const char *name); 218 219#ifdef __cplusplus 220} 221#endif 222 223#endif /* !LP_BLD_FLOW_H */ 224