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 * Insert a new block, right where builder is pointing to.
44 * This is useful important not only for aesthetic reasons, but also for
45 * performance reasons, as frequently run blocks should be laid out next to
46 * each other and fall-throughs maximized.
48 * See also llvm/lib/Transforms/Scalar/BasicBlockPlacement.cpp.
50 * Note: this function has no dependencies on the flow code and could
54 lp_build_insert_new_block(LLVMBuilderRef builder
, const char *name
)
56 LLVMBasicBlockRef current_block
;
57 LLVMBasicBlockRef next_block
;
58 LLVMBasicBlockRef new_block
;
60 /* get current basic block */
61 current_block
= LLVMGetInsertBlock(builder
);
63 /* check if there's another block after this one */
64 next_block
= LLVMGetNextBasicBlock(current_block
);
66 /* insert the new block before the next block */
67 new_block
= LLVMInsertBasicBlock(next_block
, name
);
70 /* append new block after current block */
71 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
72 new_block
= LLVMAppendBasicBlock(function
, name
);
80 * Begin a "skip" block. Inside this block we can test a condition and
81 * skip to the end of the block if the condition is false.
84 lp_build_flow_skip_begin(struct lp_build_skip_context
*skip
,
85 LLVMBuilderRef builder
)
87 skip
->builder
= builder
;
89 /* create new basic block */
90 skip
->block
= lp_build_insert_new_block(skip
->builder
, "skip");
95 * Insert code to test a condition and branch to the end of the current
96 * skip block if the condition is true.
99 lp_build_flow_skip_cond_break(struct lp_build_skip_context
*skip
,
102 LLVMBasicBlockRef new_block
;
104 new_block
= lp_build_insert_new_block(skip
->builder
, "");
106 /* if cond is true, goto skip->block, else goto new_block */
107 LLVMBuildCondBr(skip
->builder
, cond
, skip
->block
, new_block
);
109 LLVMPositionBuilderAtEnd(skip
->builder
, new_block
);
114 lp_build_flow_skip_end(struct lp_build_skip_context
*skip
)
117 LLVMBuildBr(skip
->builder
, skip
->block
);
118 LLVMPositionBuilderAtEnd(skip
->builder
, skip
->block
);
123 * Check if the mask predicate is zero. If so, jump to the end of the block.
126 lp_build_mask_check(struct lp_build_mask_context
*mask
)
128 LLVMBuilderRef builder
= mask
->skip
.builder
;
132 value
= lp_build_mask_value(mask
);
134 /* cond = (mask == 0) */
135 cond
= LLVMBuildICmp(builder
,
137 LLVMBuildBitCast(builder
, value
, mask
->reg_type
, ""),
138 LLVMConstNull(mask
->reg_type
),
141 /* if cond, goto end of block */
142 lp_build_flow_skip_cond_break(&mask
->skip
, cond
);
147 * Begin a section of code which is predicated on a mask.
148 * \param mask the mask context, initialized here
149 * \param flow the flow context
150 * \param type the type of the mask
151 * \param value storage for the mask
154 lp_build_mask_begin(struct lp_build_mask_context
*mask
,
155 LLVMBuilderRef builder
,
159 memset(mask
, 0, sizeof *mask
);
161 mask
->reg_type
= LLVMIntType(type
.width
* type
.length
);
162 mask
->var
= lp_build_alloca(builder
,
163 lp_build_int_vec_type(type
),
166 LLVMBuildStore(builder
, value
, mask
->var
);
168 lp_build_flow_skip_begin(&mask
->skip
, builder
);
173 lp_build_mask_value(struct lp_build_mask_context
*mask
)
175 return LLVMBuildLoad(mask
->skip
.builder
, mask
->var
, "");
180 * Update boolean mask with given value (bitwise AND).
181 * Typically used to update the quad's pixel alive/killed mask
182 * after depth testing, alpha testing, TGSI_OPCODE_KIL, etc.
185 lp_build_mask_update(struct lp_build_mask_context
*mask
,
188 value
= LLVMBuildAnd(mask
->skip
.builder
,
189 lp_build_mask_value(mask
),
191 LLVMBuildStore(mask
->skip
.builder
, value
, mask
->var
);
196 * End section of code which is predicated on a mask.
199 lp_build_mask_end(struct lp_build_mask_context
*mask
)
201 lp_build_flow_skip_end(&mask
->skip
);
202 return lp_build_mask_value(mask
);
208 lp_build_loop_begin(LLVMBuilderRef builder
,
210 struct lp_build_loop_state
*state
)
212 state
->block
= lp_build_insert_new_block(builder
, "loop_begin");
214 state
->counter_var
= lp_build_alloca(builder
, LLVMTypeOf(start
), "loop_counter");
216 LLVMBuildStore(builder
, start
, state
->counter_var
);
218 LLVMBuildBr(builder
, state
->block
);
220 LLVMPositionBuilderAtEnd(builder
, state
->block
);
222 state
->counter
= LLVMBuildLoad(builder
, state
->counter_var
, "");
227 lp_build_loop_end_cond(LLVMBuilderRef builder
,
230 LLVMIntPredicate llvm_cond
,
231 struct lp_build_loop_state
*state
)
235 LLVMBasicBlockRef after_block
;
238 step
= LLVMConstInt(LLVMTypeOf(end
), 1, 0);
240 next
= LLVMBuildAdd(builder
, state
->counter
, step
, "");
242 LLVMBuildStore(builder
, next
, state
->counter_var
);
244 cond
= LLVMBuildICmp(builder
, llvm_cond
, next
, end
, "");
246 after_block
= lp_build_insert_new_block(builder
, "loop_end");
248 LLVMBuildCondBr(builder
, cond
, after_block
, state
->block
);
250 LLVMPositionBuilderAtEnd(builder
, after_block
);
252 state
->counter
= LLVMBuildLoad(builder
, state
->counter_var
, "");
257 lp_build_loop_end(LLVMBuilderRef builder
,
260 struct lp_build_loop_state
*state
)
262 lp_build_loop_end_cond(builder
, end
, step
, LLVMIntNE
, state
);
268 Example of if/then/else building:
280 // x needs an alloca variable
281 x = lp_build_alloca(builder, type, "x");
284 lp_build_if(ctx, builder, cond);
285 LLVMBuildStore(LLVMBuildAdd(1, 2), x);
287 LLVMBuildStore(LLVMBuildAdd(2, 3). x);
295 * Begin an if/else/endif construct.
298 lp_build_if(struct lp_build_if_state
*ifthen
,
299 LLVMBuilderRef builder
,
300 LLVMValueRef condition
)
302 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
304 memset(ifthen
, 0, sizeof *ifthen
);
305 ifthen
->builder
= builder
;
306 ifthen
->condition
= condition
;
307 ifthen
->entry_block
= block
;
309 /* create endif/merge basic block for the phi functions */
310 ifthen
->merge_block
= lp_build_insert_new_block(builder
, "endif-block");
312 /* create/insert true_block before merge_block */
313 ifthen
->true_block
= LLVMInsertBasicBlock(ifthen
->merge_block
, "if-true-block");
315 /* successive code goes into the true block */
316 LLVMPositionBuilderAtEnd(builder
, ifthen
->true_block
);
321 * Begin else-part of a conditional
324 lp_build_else(struct lp_build_if_state
*ifthen
)
326 /* Append an unconditional Br(anch) instruction on the true_block */
327 LLVMBuildBr(ifthen
->builder
, ifthen
->merge_block
);
329 /* create/insert false_block before the merge block */
330 ifthen
->false_block
= LLVMInsertBasicBlock(ifthen
->merge_block
, "if-false-block");
332 /* successive code goes into the else block */
333 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->false_block
);
341 lp_build_endif(struct lp_build_if_state
*ifthen
)
343 /* Insert branch to the merge block from current block */
344 LLVMBuildBr(ifthen
->builder
, ifthen
->merge_block
);
347 * Now patch in the various branch instructions.
350 /* Insert the conditional branch instruction at the end of entry_block */
351 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->entry_block
);
352 if (ifthen
->false_block
) {
353 /* we have an else clause */
354 LLVMBuildCondBr(ifthen
->builder
, ifthen
->condition
,
355 ifthen
->true_block
, ifthen
->false_block
);
359 LLVMBuildCondBr(ifthen
->builder
, ifthen
->condition
,
360 ifthen
->true_block
, ifthen
->merge_block
);
363 /* Resume building code at end of the ifthen->merge_block */
364 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->merge_block
);
369 * Allocate a scalar (or vector) variable.
371 * Although not strictly part of control flow, control flow has deep impact in
372 * how variables should be allocated.
374 * The mem2reg optimization pass is the recommended way to dealing with mutable
375 * variables, and SSA. It looks for allocas and if it can handle them, it
376 * promotes them, but only looks for alloca instructions in the entry block of
377 * the function. Being in the entry block guarantees that the alloca is only
378 * executed once, which makes analysis simpler.
381 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
384 lp_build_alloca(LLVMBuilderRef builder
,
388 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
389 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
390 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
391 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
392 LLVMBuilderRef first_builder
= LLVMCreateBuilder();
396 LLVMPositionBuilderBefore(first_builder
, first_instr
);
398 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
401 res
= LLVMBuildAlloca(first_builder
, type
, name
);
402 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
404 LLVMDisposeBuilder(first_builder
);
411 * Allocate an array of scalars/vectors.
413 * mem2reg pass is not capable of promoting structs or arrays to registers, but
414 * we still put it in the first block anyway as failure to put allocas in the
415 * first block may prevent the X86 backend from successfully align the stack as
418 * Also the scalarrepl pass is supposedly more powerful and can promote
419 * arrays in many cases.
422 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
425 lp_build_array_alloca(LLVMBuilderRef builder
,
430 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
431 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
432 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
433 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
434 LLVMBuilderRef first_builder
= LLVMCreateBuilder();
438 LLVMPositionBuilderBefore(first_builder
, first_instr
);
440 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
443 res
= LLVMBuildArrayAlloca(first_builder
, type
, count
, name
);
445 LLVMDisposeBuilder(first_builder
);