1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
29 * LLVM control flow build helpers.
31 * @author Jose Fonseca <jfonseca@vmware.com>
34 #include "util/u_debug.h"
35 #include "util/u_memory.h"
37 #include "lp_bld_type.h"
38 #include "lp_bld_flow.h"
42 * Note: this function has no dependencies on the flow code and could
46 lp_build_insert_new_block(LLVMBuilderRef builder
, const char *name
)
48 LLVMBasicBlockRef current_block
;
49 LLVMBasicBlockRef next_block
;
50 LLVMBasicBlockRef new_block
;
52 /* get current basic block */
53 current_block
= LLVMGetInsertBlock(builder
);
55 /* check if there's another block after this one */
56 next_block
= LLVMGetNextBasicBlock(current_block
);
58 /* insert the new block before the next block */
59 new_block
= LLVMInsertBasicBlock(next_block
, name
);
62 /* append new block after current block */
63 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
64 new_block
= LLVMAppendBasicBlock(function
, name
);
72 * Begin a "skip" block. Inside this block we can test a condition and
73 * skip to the end of the block if the condition is false.
76 lp_build_flow_skip_begin(struct lp_build_skip_context
*skip
,
77 LLVMBuilderRef builder
)
79 skip
->builder
= builder
;
81 /* create new basic block */
82 skip
->block
= lp_build_insert_new_block(skip
->builder
, "skip");
87 * Insert code to test a condition and branch to the end of the current
88 * skip block if the condition is true.
91 lp_build_flow_skip_cond_break(struct lp_build_skip_context
*skip
,
94 LLVMBasicBlockRef new_block
;
96 new_block
= lp_build_insert_new_block(skip
->builder
, "");
98 /* if cond is true, goto skip->block, else goto new_block */
99 LLVMBuildCondBr(skip
->builder
, cond
, skip
->block
, new_block
);
101 LLVMPositionBuilderAtEnd(skip
->builder
, new_block
);
106 lp_build_flow_skip_end(struct lp_build_skip_context
*skip
)
109 LLVMBuildBr(skip
->builder
, skip
->block
);
110 LLVMPositionBuilderAtEnd(skip
->builder
, skip
->block
);
115 * Check if the mask predicate is zero. If so, jump to the end of the block.
118 lp_build_mask_check(struct lp_build_mask_context
*mask
)
120 LLVMBuilderRef builder
= mask
->skip
.builder
;
124 value
= lp_build_mask_value(mask
);
126 /* cond = (mask == 0) */
127 cond
= LLVMBuildICmp(builder
,
129 LLVMBuildBitCast(builder
, value
, mask
->reg_type
, ""),
130 LLVMConstNull(mask
->reg_type
),
133 /* if cond, goto end of block */
134 lp_build_flow_skip_cond_break(&mask
->skip
, cond
);
139 * Begin a section of code which is predicated on a mask.
140 * \param mask the mask context, initialized here
141 * \param flow the flow context
142 * \param type the type of the mask
143 * \param value storage for the mask
146 lp_build_mask_begin(struct lp_build_mask_context
*mask
,
147 LLVMBuilderRef builder
,
151 memset(mask
, 0, sizeof *mask
);
153 mask
->reg_type
= LLVMIntType(type
.width
* type
.length
);
154 mask
->var
= lp_build_alloca(builder
,
155 lp_build_int_vec_type(type
),
158 LLVMBuildStore(builder
, value
, mask
->var
);
160 lp_build_flow_skip_begin(&mask
->skip
, builder
);
165 lp_build_mask_value(struct lp_build_mask_context
*mask
)
167 return LLVMBuildLoad(mask
->skip
.builder
, mask
->var
, "");
172 * Update boolean mask with given value (bitwise AND).
173 * Typically used to update the quad's pixel alive/killed mask
174 * after depth testing, alpha testing, TGSI_OPCODE_KIL, etc.
177 lp_build_mask_update(struct lp_build_mask_context
*mask
,
180 value
= LLVMBuildAnd(mask
->skip
.builder
,
181 lp_build_mask_value(mask
),
183 LLVMBuildStore(mask
->skip
.builder
, value
, mask
->var
);
188 * End section of code which is predicated on a mask.
191 lp_build_mask_end(struct lp_build_mask_context
*mask
)
193 lp_build_flow_skip_end(&mask
->skip
);
194 return lp_build_mask_value(mask
);
200 lp_build_loop_begin(LLVMBuilderRef builder
,
202 struct lp_build_loop_state
*state
)
204 state
->block
= lp_build_insert_new_block(builder
, "loop_begin");
206 state
->counter_var
= lp_build_alloca(builder
, LLVMTypeOf(start
), "loop_counter");
208 LLVMBuildStore(builder
, start
, state
->counter_var
);
210 LLVMBuildBr(builder
, state
->block
);
212 LLVMPositionBuilderAtEnd(builder
, state
->block
);
214 state
->counter
= LLVMBuildLoad(builder
, state
->counter_var
, "");
219 lp_build_loop_end_cond(LLVMBuilderRef builder
,
222 LLVMIntPredicate llvm_cond
,
223 struct lp_build_loop_state
*state
)
227 LLVMBasicBlockRef after_block
;
230 step
= LLVMConstInt(LLVMTypeOf(end
), 1, 0);
232 next
= LLVMBuildAdd(builder
, state
->counter
, step
, "");
234 LLVMBuildStore(builder
, next
, state
->counter_var
);
236 cond
= LLVMBuildICmp(builder
, llvm_cond
, next
, end
, "");
238 after_block
= lp_build_insert_new_block(builder
, "loop_end");
240 LLVMBuildCondBr(builder
, cond
, after_block
, state
->block
);
242 LLVMPositionBuilderAtEnd(builder
, after_block
);
244 state
->counter
= LLVMBuildLoad(builder
, state
->counter_var
, "");
249 lp_build_loop_end(LLVMBuilderRef builder
,
252 struct lp_build_loop_state
*state
)
254 lp_build_loop_end_cond(builder
, end
, step
, LLVMIntNE
, state
);
260 Example of if/then/else building:
272 // x needs an alloca variable
273 x = lp_build_alloca(builder, type, "x");
276 lp_build_if(ctx, builder, cond);
277 LLVMBuildStore(LLVMBuildAdd(1, 2), x);
279 LLVMBuildStore(LLVMBuildAdd(2, 3). x);
287 * Begin an if/else/endif construct.
290 lp_build_if(struct lp_build_if_state
*ifthen
,
291 LLVMBuilderRef builder
,
292 LLVMValueRef condition
)
294 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
296 memset(ifthen
, 0, sizeof *ifthen
);
297 ifthen
->builder
= builder
;
298 ifthen
->condition
= condition
;
299 ifthen
->entry_block
= block
;
301 /* create endif/merge basic block for the phi functions */
302 ifthen
->merge_block
= lp_build_insert_new_block(builder
, "endif-block");
304 /* create/insert true_block before merge_block */
305 ifthen
->true_block
= LLVMInsertBasicBlock(ifthen
->merge_block
, "if-true-block");
307 /* successive code goes into the true block */
308 LLVMPositionBuilderAtEnd(builder
, ifthen
->true_block
);
313 * Begin else-part of a conditional
316 lp_build_else(struct lp_build_if_state
*ifthen
)
318 /* Append an unconditional Br(anch) instruction on the true_block */
319 LLVMBuildBr(ifthen
->builder
, ifthen
->merge_block
);
321 /* create/insert false_block before the merge block */
322 ifthen
->false_block
= LLVMInsertBasicBlock(ifthen
->merge_block
, "if-false-block");
324 /* successive code goes into the else block */
325 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->false_block
);
333 lp_build_endif(struct lp_build_if_state
*ifthen
)
335 /* Insert branch to the merge block from current block */
336 LLVMBuildBr(ifthen
->builder
, ifthen
->merge_block
);
339 * Now patch in the various branch instructions.
342 /* Insert the conditional branch instruction at the end of entry_block */
343 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->entry_block
);
344 if (ifthen
->false_block
) {
345 /* we have an else clause */
346 LLVMBuildCondBr(ifthen
->builder
, ifthen
->condition
,
347 ifthen
->true_block
, ifthen
->false_block
);
351 LLVMBuildCondBr(ifthen
->builder
, ifthen
->condition
,
352 ifthen
->true_block
, ifthen
->merge_block
);
355 /* Resume building code at end of the ifthen->merge_block */
356 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->merge_block
);
361 * Allocate a scalar (or vector) variable.
363 * Although not strictly part of control flow, control flow has deep impact in
364 * how variables should be allocated.
366 * The mem2reg optimization pass is the recommended way to dealing with mutable
367 * variables, and SSA. It looks for allocas and if it can handle them, it
368 * promotes them, but only looks for alloca instructions in the entry block of
369 * the function. Being in the entry block guarantees that the alloca is only
370 * executed once, which makes analysis simpler.
373 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
376 lp_build_alloca(LLVMBuilderRef builder
,
380 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
381 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
382 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
383 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
384 LLVMBuilderRef first_builder
= LLVMCreateBuilder();
388 LLVMPositionBuilderBefore(first_builder
, first_instr
);
390 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
393 res
= LLVMBuildAlloca(first_builder
, type
, name
);
394 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
396 LLVMDisposeBuilder(first_builder
);
403 * Allocate an array of scalars/vectors.
405 * mem2reg pass is not capable of promoting structs or arrays to registers, but
406 * we still put it in the first block anyway as failure to put allocas in the
407 * first block may prevent the X86 backend from successfully align the stack as
410 * Also the scalarrepl pass is supposedly more powerful and can promote
411 * arrays in many cases.
414 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
417 lp_build_array_alloca(LLVMBuilderRef builder
,
422 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
423 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
424 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
425 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
426 LLVMBuilderRef first_builder
= LLVMCreateBuilder();
430 LLVMPositionBuilderBefore(first_builder
, first_instr
);
432 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
435 res
= LLVMBuildArrayAlloca(first_builder
, type
, count
, name
);
437 LLVMDisposeBuilder(first_builder
);