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"
41 #define LP_BUILD_FLOW_MAX_VARIABLES 64
42 #define LP_BUILD_FLOW_MAX_DEPTH 32
45 * Enumeration of all possible flow constructs.
47 enum lp_build_flow_construct_kind
{
54 * Early exit. Useful to skip to the end of a function or block when
55 * the execution mask becomes zero or when there is an error condition.
57 struct lp_build_flow_skip
59 /** Block to skip to */
60 LLVMBasicBlockRef block
;
67 struct lp_build_flow_if
69 LLVMValueRef condition
;
70 LLVMBasicBlockRef entry_block
, true_block
, false_block
, merge_block
;
75 * Union of all possible flow constructs' data
77 union lp_build_flow_construct_data
79 struct lp_build_flow_skip skip
;
80 struct lp_build_flow_if ifthen
;
85 * Element of the flow construct stack.
87 struct lp_build_flow_construct
89 enum lp_build_flow_construct_kind kind
;
90 union lp_build_flow_construct_data data
;
95 * All necessary data to generate LLVM control flow constructs.
97 * Besides keeping track of the control flow construct themselves we also
98 * need to keep track of variables in order to generate SSA Phi values.
100 struct lp_build_flow_context
102 LLVMBuilderRef builder
;
105 * Control flow stack.
107 struct lp_build_flow_construct constructs
[LP_BUILD_FLOW_MAX_DEPTH
];
108 unsigned num_constructs
;
112 struct lp_build_flow_context
*
113 lp_build_flow_create(LLVMBuilderRef builder
)
115 struct lp_build_flow_context
*flow
;
117 flow
= CALLOC_STRUCT(lp_build_flow_context
);
121 flow
->builder
= builder
;
128 lp_build_flow_destroy(struct lp_build_flow_context
*flow
)
130 assert(flow
->num_constructs
== 0);
136 * Begin/push a new flow control construct, such as a loop, skip block
139 static union lp_build_flow_construct_data
*
140 lp_build_flow_push(struct lp_build_flow_context
*flow
,
141 enum lp_build_flow_construct_kind kind
)
143 assert(flow
->num_constructs
< LP_BUILD_FLOW_MAX_DEPTH
);
144 if(flow
->num_constructs
>= LP_BUILD_FLOW_MAX_DEPTH
)
147 flow
->constructs
[flow
->num_constructs
].kind
= kind
;
148 return &flow
->constructs
[flow
->num_constructs
++].data
;
153 * Return the current/top flow control construct on the stack.
154 * \param kind the expected type of the top-most construct
156 static union lp_build_flow_construct_data
*
157 lp_build_flow_peek(struct lp_build_flow_context
*flow
,
158 enum lp_build_flow_construct_kind kind
)
160 assert(flow
->num_constructs
);
161 if(!flow
->num_constructs
)
164 assert(flow
->constructs
[flow
->num_constructs
- 1].kind
== kind
);
165 if(flow
->constructs
[flow
->num_constructs
- 1].kind
!= kind
)
168 return &flow
->constructs
[flow
->num_constructs
- 1].data
;
173 * End/pop the current/top flow control construct on the stack.
174 * \param kind the expected type of the top-most construct
176 static union lp_build_flow_construct_data
*
177 lp_build_flow_pop(struct lp_build_flow_context
*flow
,
178 enum lp_build_flow_construct_kind kind
)
180 assert(flow
->num_constructs
);
181 if(!flow
->num_constructs
)
184 assert(flow
->constructs
[flow
->num_constructs
- 1].kind
== kind
);
185 if(flow
->constructs
[flow
->num_constructs
- 1].kind
!= kind
)
188 return &flow
->constructs
[--flow
->num_constructs
].data
;
193 * Note: this function has no dependencies on the flow code and could
197 lp_build_insert_new_block(LLVMBuilderRef builder
, const char *name
)
199 LLVMBasicBlockRef current_block
;
200 LLVMBasicBlockRef next_block
;
201 LLVMBasicBlockRef new_block
;
203 /* get current basic block */
204 current_block
= LLVMGetInsertBlock(builder
);
206 /* check if there's another block after this one */
207 next_block
= LLVMGetNextBasicBlock(current_block
);
209 /* insert the new block before the next block */
210 new_block
= LLVMInsertBasicBlock(next_block
, name
);
213 /* append new block after current block */
214 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
215 new_block
= LLVMAppendBasicBlock(function
, name
);
222 static LLVMBasicBlockRef
223 lp_build_flow_insert_block(struct lp_build_flow_context
*flow
)
225 return lp_build_insert_new_block(flow
->builder
, "");
230 * Begin a "skip" block. Inside this block we can test a condition and
231 * skip to the end of the block if the condition is false.
234 lp_build_flow_skip_begin(struct lp_build_flow_context
*flow
)
236 struct lp_build_flow_skip
*skip
;
237 LLVMBuilderRef builder
;
239 skip
= &lp_build_flow_push(flow
, LP_BUILD_FLOW_SKIP
)->skip
;
243 /* create new basic block */
244 skip
->block
= lp_build_flow_insert_block(flow
);
246 builder
= LLVMCreateBuilder();
247 LLVMPositionBuilderAtEnd(builder
, skip
->block
);
249 LLVMDisposeBuilder(builder
);
254 * Insert code to test a condition and branch to the end of the current
255 * skip block if the condition is true.
258 lp_build_flow_skip_cond_break(struct lp_build_flow_context
*flow
,
261 struct lp_build_flow_skip
*skip
;
262 LLVMBasicBlockRef new_block
;
264 skip
= &lp_build_flow_peek(flow
, LP_BUILD_FLOW_SKIP
)->skip
;
268 new_block
= lp_build_flow_insert_block(flow
);
270 /* if cond is true, goto skip->block, else goto new_block */
271 LLVMBuildCondBr(flow
->builder
, cond
, skip
->block
, new_block
);
273 LLVMPositionBuilderAtEnd(flow
->builder
, new_block
);
278 lp_build_flow_skip_end(struct lp_build_flow_context
*flow
)
280 struct lp_build_flow_skip
*skip
;
282 skip
= &lp_build_flow_pop(flow
, LP_BUILD_FLOW_SKIP
)->skip
;
287 LLVMBuildBr(flow
->builder
, skip
->block
);
288 LLVMPositionBuilderAtEnd(flow
->builder
, skip
->block
);
293 * Check if the mask predicate is zero. If so, jump to the end of the block.
296 lp_build_mask_check(struct lp_build_mask_context
*mask
)
298 LLVMBuilderRef builder
= mask
->flow
->builder
;
302 value
= lp_build_mask_value(mask
);
304 /* cond = (mask == 0) */
305 cond
= LLVMBuildICmp(builder
,
307 LLVMBuildBitCast(builder
, value
, mask
->reg_type
, ""),
308 LLVMConstNull(mask
->reg_type
),
311 /* if cond, goto end of block */
312 lp_build_flow_skip_cond_break(mask
->flow
, cond
);
317 * Begin a section of code which is predicated on a mask.
318 * \param mask the mask context, initialized here
319 * \param flow the flow context
320 * \param type the type of the mask
321 * \param value storage for the mask
324 lp_build_mask_begin(struct lp_build_mask_context
*mask
,
325 struct lp_build_flow_context
*flow
,
329 memset(mask
, 0, sizeof *mask
);
332 mask
->reg_type
= LLVMIntType(type
.width
* type
.length
);
333 mask
->var
= lp_build_alloca(flow
->builder
,
334 lp_build_int_vec_type(type
),
337 LLVMBuildStore(flow
->builder
, value
, mask
->var
);
339 lp_build_flow_skip_begin(flow
);
344 lp_build_mask_value(struct lp_build_mask_context
*mask
)
346 return LLVMBuildLoad(mask
->flow
->builder
, mask
->var
, "");
351 * Update boolean mask with given value (bitwise AND).
352 * Typically used to update the quad's pixel alive/killed mask
353 * after depth testing, alpha testing, TGSI_OPCODE_KIL, etc.
356 lp_build_mask_update(struct lp_build_mask_context
*mask
,
359 value
= LLVMBuildAnd(mask
->flow
->builder
,
360 lp_build_mask_value(mask
),
362 LLVMBuildStore(mask
->flow
->builder
, value
, mask
->var
);
367 * End section of code which is predicated on a mask.
370 lp_build_mask_end(struct lp_build_mask_context
*mask
)
372 lp_build_flow_skip_end(mask
->flow
);
373 return lp_build_mask_value(mask
);
379 lp_build_loop_begin(LLVMBuilderRef builder
,
381 struct lp_build_loop_state
*state
)
383 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
384 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
386 state
->block
= LLVMAppendBasicBlock(function
, "loop");
388 LLVMBuildBr(builder
, state
->block
);
390 LLVMPositionBuilderAtEnd(builder
, state
->block
);
392 state
->counter
= LLVMBuildPhi(builder
, LLVMTypeOf(start
), "");
394 LLVMAddIncoming(state
->counter
, &start
, &block
, 1);
400 lp_build_loop_end(LLVMBuilderRef builder
,
403 struct lp_build_loop_state
*state
)
405 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
406 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
409 LLVMBasicBlockRef after_block
;
412 step
= LLVMConstInt(LLVMTypeOf(end
), 1, 0);
414 next
= LLVMBuildAdd(builder
, state
->counter
, step
, "");
416 cond
= LLVMBuildICmp(builder
, LLVMIntNE
, next
, end
, "");
418 after_block
= LLVMAppendBasicBlock(function
, "");
420 LLVMBuildCondBr(builder
, cond
, after_block
, state
->block
);
422 LLVMAddIncoming(state
->counter
, &next
, &block
, 1);
424 LLVMPositionBuilderAtEnd(builder
, after_block
);
428 lp_build_loop_end_cond(LLVMBuilderRef builder
,
432 struct lp_build_loop_state
*state
)
434 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
435 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
438 LLVMBasicBlockRef after_block
;
441 step
= LLVMConstInt(LLVMTypeOf(end
), 1, 0);
443 next
= LLVMBuildAdd(builder
, state
->counter
, step
, "");
445 cond
= LLVMBuildICmp(builder
, llvm_cond
, next
, end
, "");
447 after_block
= LLVMAppendBasicBlock(function
, "");
449 LLVMBuildCondBr(builder
, cond
, after_block
, state
->block
);
451 LLVMAddIncoming(state
->counter
, &next
, &block
, 1);
453 LLVMPositionBuilderAtEnd(builder
, after_block
);
459 Example of if/then/else building:
471 LLVMValueRef x = LLVMGetUndef(); // or something else
473 flow = lp_build_flow_create(builder);
475 lp_build_flow_scope_begin(flow);
477 // x needs a phi node
478 lp_build_flow_scope_declare(flow, &x);
480 lp_build_if(ctx, flow, builder, cond);
486 lp_build_flow_scope_end(flow);
488 lp_build_flow_destroy(flow);
494 * Begin an if/else/endif construct.
497 lp_build_if(struct lp_build_if_state
*ctx
,
498 struct lp_build_flow_context
*flow
,
499 LLVMBuilderRef builder
,
500 LLVMValueRef condition
)
502 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
503 struct lp_build_flow_if
*ifthen
;
505 memset(ctx
, 0, sizeof(*ctx
));
506 ctx
->builder
= builder
;
509 /* push/create new scope */
510 ifthen
= &lp_build_flow_push(flow
, LP_BUILD_FLOW_IF
)->ifthen
;
513 ifthen
->condition
= condition
;
514 ifthen
->entry_block
= block
;
516 /* create endif/merge basic block for the phi functions */
517 ifthen
->merge_block
= lp_build_insert_new_block(builder
, "endif-block");
518 LLVMPositionBuilderAtEnd(builder
, ifthen
->merge_block
);
520 /* create/insert true_block before merge_block */
521 ifthen
->true_block
= LLVMInsertBasicBlock(ifthen
->merge_block
, "if-true-block");
523 /* successive code goes into the true block */
524 LLVMPositionBuilderAtEnd(builder
, ifthen
->true_block
);
529 * Begin else-part of a conditional
532 lp_build_else(struct lp_build_if_state
*ctx
)
534 struct lp_build_flow_context
*flow
= ctx
->flow
;
535 struct lp_build_flow_if
*ifthen
;
537 ifthen
= &lp_build_flow_peek(flow
, LP_BUILD_FLOW_IF
)->ifthen
;
540 /* create/insert false_block before the merge block */
541 ifthen
->false_block
= LLVMInsertBasicBlock(ifthen
->merge_block
, "if-false-block");
543 /* successive code goes into the else block */
544 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->false_block
);
552 lp_build_endif(struct lp_build_if_state
*ctx
)
554 struct lp_build_flow_context
*flow
= ctx
->flow
;
555 struct lp_build_flow_if
*ifthen
;
557 ifthen
= &lp_build_flow_pop(flow
, LP_BUILD_FLOW_IF
)->ifthen
;
560 /* Insert branch to the merge block from current block */
561 LLVMBuildBr(ctx
->builder
, ifthen
->merge_block
);
564 *** Now patch in the various branch instructions.
567 /* Insert the conditional branch instruction at the end of entry_block */
568 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->entry_block
);
569 if (ifthen
->false_block
) {
570 /* we have an else clause */
571 LLVMBuildCondBr(ctx
->builder
, ifthen
->condition
,
572 ifthen
->true_block
, ifthen
->false_block
);
576 LLVMBuildCondBr(ctx
->builder
, ifthen
->condition
,
577 ifthen
->true_block
, ifthen
->merge_block
);
580 /* Insert branch from end of true_block to merge_block */
581 if (ifthen
->false_block
) {
582 /* Append an unconditional Br(anch) instruction on the true_block */
583 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->true_block
);
584 LLVMBuildBr(ctx
->builder
, ifthen
->merge_block
);
588 * Note that we've already inserted the branch at the end of
589 * true_block. See the very first LLVMBuildBr() call in this function.
593 /* Resume building code at end of the ifthen->merge_block */
594 LLVMPositionBuilderAtEnd(ctx
->builder
, ifthen
->merge_block
);
599 * Allocate a scalar (or vector) variable.
601 * Although not strictly part of control flow, control flow has deep impact in
602 * how variables should be allocated.
604 * The mem2reg optimization pass is the recommended way to dealing with mutable
605 * variables, and SSA. It looks for allocas and if it can handle them, it
606 * promotes them, but only looks for alloca instructions in the entry block of
607 * the function. Being in the entry block guarantees that the alloca is only
608 * executed once, which makes analysis simpler.
611 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
614 lp_build_alloca(LLVMBuilderRef builder
,
618 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
619 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
620 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
621 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
622 LLVMBuilderRef first_builder
= LLVMCreateBuilder();
626 LLVMPositionBuilderBefore(first_builder
, first_instr
);
628 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
631 res
= LLVMBuildAlloca(first_builder
, type
, name
);
632 LLVMBuildStore(builder
, LLVMConstNull(type
), res
);
634 LLVMDisposeBuilder(first_builder
);
641 * Allocate an array of scalars/vectors.
643 * mem2reg pass is not capable of promoting structs or arrays to registers, but
644 * we still put it in the first block anyway as failure to put allocas in the
645 * first block may prevent the X86 backend from successfully align the stack as
648 * Also the scalarrepl pass is supposedly more powerful and can promote
649 * arrays in many cases.
652 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
655 lp_build_array_alloca(LLVMBuilderRef builder
,
660 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(builder
);
661 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
662 LLVMBasicBlockRef first_block
= LLVMGetEntryBasicBlock(function
);
663 LLVMValueRef first_instr
= LLVMGetFirstInstruction(first_block
);
664 LLVMBuilderRef first_builder
= LLVMCreateBuilder();
668 LLVMPositionBuilderBefore(first_builder
, first_instr
);
670 LLVMPositionBuilderAtEnd(first_builder
, first_block
);
673 res
= LLVMBuildArrayAlloca(first_builder
, type
, count
, name
);
675 LLVMDisposeBuilder(first_builder
);