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 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
205 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
207 state
->block
= LLVMAppendBasicBlock(function
, "loop");
209 LLVMBuildBr(builder
, state
->block
);
211 LLVMPositionBuilderAtEnd(builder
, state
->block
);
213 state
->counter
= LLVMBuildPhi(builder
, LLVMTypeOf(start
), "");
215 LLVMAddIncoming(state
->counter
, &start
, &block
, 1);
221 lp_build_loop_end(LLVMBuilderRef builder
,
224 struct lp_build_loop_state
*state
)
226 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
227 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
230 LLVMBasicBlockRef after_block
;
233 step
= LLVMConstInt(LLVMTypeOf(end
), 1, 0);
235 next
= LLVMBuildAdd(builder
, state
->counter
, step
, "");
237 cond
= LLVMBuildICmp(builder
, LLVMIntNE
, next
, end
, "");
239 after_block
= LLVMAppendBasicBlock(function
, "");
241 LLVMBuildCondBr(builder
, cond
, after_block
, state
->block
);
243 LLVMAddIncoming(state
->counter
, &next
, &block
, 1);
245 LLVMPositionBuilderAtEnd(builder
, after_block
);
249 lp_build_loop_end_cond(LLVMBuilderRef builder
,
253 struct lp_build_loop_state
*state
)
255 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
256 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
259 LLVMBasicBlockRef after_block
;
262 step
= LLVMConstInt(LLVMTypeOf(end
), 1, 0);
264 next
= LLVMBuildAdd(builder
, state
->counter
, step
, "");
266 cond
= LLVMBuildICmp(builder
, llvm_cond
, next
, end
, "");
268 after_block
= LLVMAppendBasicBlock(function
, "");
270 LLVMBuildCondBr(builder
, cond
, after_block
, state
->block
);
272 LLVMAddIncoming(state
->counter
, &next
, &block
, 1);
274 LLVMPositionBuilderAtEnd(builder
, after_block
);
280 Example of if/then/else building:
292 // x needs an alloca variable
293 x = lp_build_alloca(builder, type, "x");
296 lp_build_if(ctx, builder, cond);
297 LLVMBuildStore(LLVMBuildAdd(1, 2), x);
299 LLVMBuildStore(LLVMBuildAdd(2, 3). x);
307 * Begin an if/else/endif construct.
310 lp_build_if(struct lp_build_if_state
*ifthen
,
311 LLVMBuilderRef builder
,
312 LLVMValueRef condition
)
314 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
316 memset(ifthen
, 0, sizeof *ifthen
);
317 ifthen
->builder
= builder
;
318 ifthen
->condition
= condition
;
319 ifthen
->entry_block
= block
;
321 /* create endif/merge basic block for the phi functions */
322 ifthen
->merge_block
= lp_build_insert_new_block(builder
, "endif-block");
323 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
325 /* create/insert true_block before merge_block */
326 ifthen
->true_block
= LLVMInsertBasicBlock(ifthen
->merge_block
, "if-true-block");
328 /* successive code goes into the true block */
329 LLVMPositionBuilderAtEnd(builder
, ifthen
->true_block
);
334 * Begin else-part of a conditional
337 lp_build_else(struct lp_build_if_state
*ifthen
)
339 /* create/insert false_block before the merge block */
340 ifthen
->false_block
= LLVMInsertBasicBlock(ifthen
->merge_block
, "if-false-block");
342 /* successive code goes into the else block */
343 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->false_block
);
351 lp_build_endif(struct lp_build_if_state
*ifthen
)
353 /* Insert branch to the merge block from current block */
354 LLVMBuildBr(ifthen
->builder
, ifthen
->merge_block
);
357 *** Now patch in the various branch instructions.
360 /* Insert the conditional branch instruction at the end of entry_block */
361 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->entry_block
);
362 if (ifthen
->false_block
) {
363 /* we have an else clause */
364 LLVMBuildCondBr(ifthen
->builder
, ifthen
->condition
,
365 ifthen
->true_block
, ifthen
->false_block
);
369 LLVMBuildCondBr(ifthen
->builder
, ifthen
->condition
,
370 ifthen
->true_block
, ifthen
->merge_block
);
373 /* Insert branch from end of true_block to merge_block */
374 if (ifthen
->false_block
) {
375 /* Append an unconditional Br(anch) instruction on the true_block */
376 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->true_block
);
377 LLVMBuildBr(ifthen
->builder
, ifthen
->merge_block
);
381 * Note that we've already inserted the branch at the end of
382 * true_block. See the very first LLVMBuildBr() call in this function.
386 /* Resume building code at end of the ifthen->merge_block */
387 LLVMPositionBuilderAtEnd(ifthen
->builder
, ifthen
->merge_block
);
392 * Allocate a scalar (or vector) variable.
394 * Although not strictly part of control flow, control flow has deep impact in
395 * how variables should be allocated.
397 * The mem2reg optimization pass is the recommended way to dealing with mutable
398 * variables, and SSA. It looks for allocas and if it can handle them, it
399 * promotes them, but only looks for alloca instructions in the entry block of
400 * the function. Being in the entry block guarantees that the alloca is only
401 * executed once, which makes analysis simpler.
404 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
407 lp_build_alloca(LLVMBuilderRef builder
,
411 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
412 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
413 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
414 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
415 LLVMBuilderRef first_builder
= LLVMCreateBuilder();
419 LLVMPositionBuilderBefore(first_builder
, first_instr
);
421 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
424 res
= LLVMBuildAlloca(first_builder
, type
, name
);
425 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
427 LLVMDisposeBuilder(first_builder
);
434 * Allocate an array of scalars/vectors.
436 * mem2reg pass is not capable of promoting structs or arrays to registers, but
437 * we still put it in the first block anyway as failure to put allocas in the
438 * first block may prevent the X86 backend from successfully align the stack as
441 * Also the scalarrepl pass is supposedly more powerful and can promote
442 * arrays in many cases.
445 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
448 lp_build_array_alloca(LLVMBuilderRef builder
,
453 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
454 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
455 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
456 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
457 LLVMBuilderRef first_builder
= LLVMCreateBuilder();
461 LLVMPositionBuilderBefore(first_builder
, first_instr
);
463 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
466 res
= LLVMBuildArrayAlloca(first_builder
, type
, count
, name
);
468 LLVMDisposeBuilder(first_builder
);