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_init.h"
38
#include "lp_bld_type.h"
39
#include "lp_bld_flow.h"
43
* Insert a new block, right where builder is pointing to.
45
* This is useful important not only for aesthetic reasons, but also for
46
* performance reasons, as frequently run blocks should be laid out next to
47
* each other and fall-throughs maximized.
49
* See also llvm/lib/Transforms/Scalar/BasicBlockPlacement.cpp.
51
* Note: this function has no dependencies on the flow code and could
55
lp_build_insert_new_block(struct gallivm_state *gallivm, const char *name)
57
LLVMBasicBlockRef current_block;
58
LLVMBasicBlockRef next_block;
59
LLVMBasicBlockRef new_block;
61
/* get current basic block */
62
current_block = LLVMGetInsertBlock(gallivm->builder);
64
/* check if there's another block after this one */
65
next_block = LLVMGetNextBasicBlock(current_block);
67
/* insert the new block before the next block */
68
new_block = LLVMInsertBasicBlockInContext(gallivm->context, next_block, name);
71
/* append new block after current block */
72
LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
73
new_block = LLVMAppendBasicBlockInContext(gallivm->context, function, name);
81
* Begin a "skip" block. Inside this block we can test a condition and
82
* skip to the end of the block if the condition is false.
85
lp_build_flow_skip_begin(struct lp_build_skip_context *skip,
86
struct gallivm_state *gallivm)
88
skip->gallivm = gallivm;
89
/* create new basic block */
90
skip->block = lp_build_insert_new_block(gallivm, "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->gallivm, "");
106
/* if cond is true, goto skip->block, else goto new_block */
107
LLVMBuildCondBr(skip->gallivm->builder, cond, skip->block, new_block);
109
LLVMPositionBuilderAtEnd(skip->gallivm->builder, new_block);
114
lp_build_flow_skip_end(struct lp_build_skip_context *skip)
117
LLVMBuildBr(skip->gallivm->builder, skip->block);
118
LLVMPositionBuilderAtEnd(skip->gallivm->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.gallivm->builder;
132
value = lp_build_mask_value(mask);
135
* XXX this doesn't quite generate the most efficient code possible, if
136
* the masks are vectors which have all bits set to the same value
138
* movmskps/pmovmskb would be more efficient to get the required value
139
* into ordinary reg (certainly with 8 floats).
140
* Not sure if llvm could figure that out on its own.
143
/* cond = (mask == 0) */
144
cond = LLVMBuildICmp(builder,
146
LLVMBuildBitCast(builder, value, mask->reg_type, ""),
147
LLVMConstNull(mask->reg_type),
150
/* if cond, goto end of block */
151
lp_build_flow_skip_cond_break(&mask->skip, cond);
156
* Begin a section of code which is predicated on a mask.
157
* \param mask the mask context, initialized here
158
* \param flow the flow context
159
* \param type the type of the mask
160
* \param value storage for the mask
163
lp_build_mask_begin(struct lp_build_mask_context *mask,
164
struct gallivm_state *gallivm,
168
memset(mask, 0, sizeof *mask);
170
mask->reg_type = LLVMIntTypeInContext(gallivm->context, type.width * type.length);
171
mask->var = lp_build_alloca(gallivm,
172
lp_build_int_vec_type(gallivm, type),
175
LLVMBuildStore(gallivm->builder, value, mask->var);
177
lp_build_flow_skip_begin(&mask->skip, gallivm);
182
lp_build_mask_value(struct lp_build_mask_context *mask)
184
return LLVMBuildLoad(mask->skip.gallivm->builder, mask->var, "");
189
* Update boolean mask with given value (bitwise AND).
190
* Typically used to update the quad's pixel alive/killed mask
191
* after depth testing, alpha testing, TGSI_OPCODE_KILL_IF, etc.
194
lp_build_mask_update(struct lp_build_mask_context *mask,
197
value = LLVMBuildAnd(mask->skip.gallivm->builder,
198
lp_build_mask_value(mask),
200
LLVMBuildStore(mask->skip.gallivm->builder, value, mask->var);
204
* Update boolean mask with given value.
205
* Used for per-sample shading to force per-sample execution masks.
208
lp_build_mask_force(struct lp_build_mask_context *mask,
211
LLVMBuildStore(mask->skip.gallivm->builder, value, mask->var);
215
* End section of code which is predicated on a mask.
218
lp_build_mask_end(struct lp_build_mask_context *mask)
220
lp_build_flow_skip_end(&mask->skip);
221
return lp_build_mask_value(mask);
227
lp_build_loop_begin(struct lp_build_loop_state *state,
228
struct gallivm_state *gallivm,
232
LLVMBuilderRef builder = gallivm->builder;
234
state->block = lp_build_insert_new_block(gallivm, "loop_begin");
236
state->counter_var = lp_build_alloca(gallivm, LLVMTypeOf(start), "loop_counter");
237
state->gallivm = gallivm;
239
LLVMBuildStore(builder, start, state->counter_var);
241
LLVMBuildBr(builder, state->block);
243
LLVMPositionBuilderAtEnd(builder, state->block);
245
state->counter = LLVMBuildLoad(builder, state->counter_var, "");
250
lp_build_loop_end_cond(struct lp_build_loop_state *state,
253
LLVMIntPredicate llvm_cond)
255
LLVMBuilderRef builder = state->gallivm->builder;
258
LLVMBasicBlockRef after_block;
261
step = LLVMConstInt(LLVMTypeOf(end), 1, 0);
263
next = LLVMBuildAdd(builder, state->counter, step, "");
265
LLVMBuildStore(builder, next, state->counter_var);
267
cond = LLVMBuildICmp(builder, llvm_cond, next, end, "");
269
after_block = lp_build_insert_new_block(state->gallivm, "loop_end");
271
LLVMBuildCondBr(builder, cond, after_block, state->block);
273
LLVMPositionBuilderAtEnd(builder, after_block);
275
state->counter = LLVMBuildLoad(builder, state->counter_var, "");
279
lp_build_loop_force_set_counter(struct lp_build_loop_state *state,
282
LLVMBuilderRef builder = state->gallivm->builder;
283
LLVMBuildStore(builder, end, state->counter_var);
287
lp_build_loop_force_reload_counter(struct lp_build_loop_state *state)
289
LLVMBuilderRef builder = state->gallivm->builder;
290
state->counter = LLVMBuildLoad(builder, state->counter_var, "");
294
lp_build_loop_end(struct lp_build_loop_state *state,
298
lp_build_loop_end_cond(state, end, step, LLVMIntNE);
302
* Creates a c-style for loop,
303
* contrasts lp_build_loop as this checks condition on entry
304
* e.g. for(i = start; i cmp_op end; i += step)
305
* \param state the for loop state, initialized here
306
* \param gallivm the gallivm state
307
* \param start starting value of iterator
308
* \param cmp_op comparison operator used for comparing current value with end value
309
* \param end value used to compare against iterator
310
* \param step value added to iterator at end of each loop
313
lp_build_for_loop_begin(struct lp_build_for_loop_state *state,
314
struct gallivm_state *gallivm,
316
LLVMIntPredicate cmp_op,
320
LLVMBuilderRef builder = gallivm->builder;
322
assert(LLVMTypeOf(start) == LLVMTypeOf(end));
323
assert(LLVMTypeOf(start) == LLVMTypeOf(step));
325
state->begin = lp_build_insert_new_block(gallivm, "loop_begin");
327
state->counter_var = lp_build_alloca(gallivm, LLVMTypeOf(start), "loop_counter");
328
state->gallivm = gallivm;
329
state->cond = cmp_op;
332
LLVMBuildStore(builder, start, state->counter_var);
333
LLVMBuildBr(builder, state->begin);
335
LLVMPositionBuilderAtEnd(builder, state->begin);
336
state->counter = LLVMBuildLoad(builder, state->counter_var, "");
338
state->body = lp_build_insert_new_block(gallivm, "loop_body");
339
LLVMPositionBuilderAtEnd(builder, state->body);
346
lp_build_for_loop_end(struct lp_build_for_loop_state *state)
348
LLVMValueRef next, cond;
349
LLVMBuilderRef builder = state->gallivm->builder;
351
next = LLVMBuildAdd(builder, state->counter, state->step, "");
352
LLVMBuildStore(builder, next, state->counter_var);
353
LLVMBuildBr(builder, state->begin);
355
state->exit = lp_build_insert_new_block(state->gallivm, "loop_exit");
358
* We build the comparison for the begin block here,
359
* if we build it earlier the output llvm ir is not human readable
360
* as the code produced is not in the standard begin -> body -> end order.
362
LLVMPositionBuilderAtEnd(builder, state->begin);
363
cond = LLVMBuildICmp(builder, state->cond, state->counter, state->end, "");
364
LLVMBuildCondBr(builder, cond, state->body, state->exit);
366
LLVMPositionBuilderAtEnd(builder, state->exit);
371
Example of if/then/else building:
383
// x needs an alloca variable
384
x = lp_build_alloca(builder, type, "x");
387
lp_build_if(ctx, builder, cond);
388
LLVMBuildStore(LLVMBuildAdd(1, 2), x);
390
LLVMBuildStore(LLVMBuildAdd(2, 3). x);
398
* Begin an if/else/endif construct.
401
lp_build_if(struct lp_build_if_state *ifthen,
402
struct gallivm_state *gallivm,
403
LLVMValueRef condition)
405
LLVMBasicBlockRef block = LLVMGetInsertBlock(gallivm->builder);
407
memset(ifthen, 0, sizeof *ifthen);
408
ifthen->gallivm = gallivm;
409
ifthen->condition = condition;
410
ifthen->entry_block = block;
412
/* create endif/merge basic block for the phi functions */
413
ifthen->merge_block = lp_build_insert_new_block(gallivm, "endif-block");
415
/* create/insert true_block before merge_block */
417
LLVMInsertBasicBlockInContext(gallivm->context,
421
/* successive code goes into the true block */
422
LLVMPositionBuilderAtEnd(gallivm->builder, ifthen->true_block);
427
* Begin else-part of a conditional
430
lp_build_else(struct lp_build_if_state *ifthen)
432
LLVMBuilderRef builder = ifthen->gallivm->builder;
434
/* Append an unconditional Br(anch) instruction on the true_block */
435
LLVMBuildBr(builder, ifthen->merge_block);
437
/* create/insert false_block before the merge block */
438
ifthen->false_block =
439
LLVMInsertBasicBlockInContext(ifthen->gallivm->context,
443
/* successive code goes into the else block */
444
LLVMPositionBuilderAtEnd(builder, ifthen->false_block);
452
lp_build_endif(struct lp_build_if_state *ifthen)
454
LLVMBuilderRef builder = ifthen->gallivm->builder;
456
/* Insert branch to the merge block from current block */
457
LLVMBuildBr(builder, ifthen->merge_block);
460
* Now patch in the various branch instructions.
463
/* Insert the conditional branch instruction at the end of entry_block */
464
LLVMPositionBuilderAtEnd(builder, ifthen->entry_block);
465
if (ifthen->false_block) {
466
/* we have an else clause */
467
LLVMBuildCondBr(builder, ifthen->condition,
468
ifthen->true_block, ifthen->false_block);
472
LLVMBuildCondBr(builder, ifthen->condition,
473
ifthen->true_block, ifthen->merge_block);
476
/* Resume building code at end of the ifthen->merge_block */
477
LLVMPositionBuilderAtEnd(builder, ifthen->merge_block);
481
static LLVMBuilderRef
482
create_builder_at_entry(struct gallivm_state *gallivm)
484
LLVMBuilderRef builder = gallivm->builder;
485
LLVMBasicBlockRef current_block = LLVMGetInsertBlock(builder);
486
LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
487
LLVMBasicBlockRef first_block = LLVMGetEntryBasicBlock(function);
488
LLVMValueRef first_instr = LLVMGetFirstInstruction(first_block);
489
LLVMBuilderRef first_builder = LLVMCreateBuilderInContext(gallivm->context);
492
LLVMPositionBuilderBefore(first_builder, first_instr);
494
LLVMPositionBuilderAtEnd(first_builder, first_block);
497
return first_builder;
502
* Allocate a scalar (or vector) variable.
504
* Although not strictly part of control flow, control flow has deep impact in
505
* how variables should be allocated.
507
* The mem2reg optimization pass is the recommended way to dealing with mutable
508
* variables, and SSA. It looks for allocas and if it can handle them, it
509
* promotes them, but only looks for alloca instructions in the entry block of
510
* the function. Being in the entry block guarantees that the alloca is only
511
* executed once, which makes analysis simpler.
514
* - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
517
lp_build_alloca(struct gallivm_state *gallivm,
521
LLVMBuilderRef builder = gallivm->builder;
522
LLVMBuilderRef first_builder = create_builder_at_entry(gallivm);
525
res = LLVMBuildAlloca(first_builder, type, name);
526
LLVMBuildStore(builder, LLVMConstNull(type), res);
528
LLVMDisposeBuilder(first_builder);
535
* Like lp_build_alloca, but do not zero-initialize the variable.
538
lp_build_alloca_undef(struct gallivm_state *gallivm,
542
LLVMBuilderRef first_builder = create_builder_at_entry(gallivm);
545
res = LLVMBuildAlloca(first_builder, type, name);
547
LLVMDisposeBuilder(first_builder);
554
* Allocate an array of scalars/vectors.
556
* mem2reg pass is not capable of promoting structs or arrays to registers, but
557
* we still put it in the first block anyway as failure to put allocas in the
558
* first block may prevent the X86 backend from successfully align the stack as
561
* Also the scalarrepl pass is supposedly more powerful and can promote
562
* arrays in many cases.
565
* - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
568
lp_build_array_alloca(struct gallivm_state *gallivm,
573
LLVMBuilderRef first_builder = create_builder_at_entry(gallivm);
576
res = LLVMBuildArrayAlloca(first_builder, type, count, name);
578
LLVMDisposeBuilder(first_builder);