gallivm: Use variables instead of Phis in loops.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_flow.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * LLVM control flow build helpers.
30 *
31 * @author Jose Fonseca <jfonseca@vmware.com>
32 */
33
34 #include "util/u_debug.h"
35 #include "util/u_memory.h"
36
37 #include "lp_bld_type.h"
38 #include "lp_bld_flow.h"
39
40
41 /**
42 * Note: this function has no dependencies on the flow code and could
43 * be used elsewhere.
44 */
45 LLVMBasicBlockRef
46 lp_build_insert_new_block(LLVMBuilderRef builder, const char *name)
47 {
48 LLVMBasicBlockRef current_block;
49 LLVMBasicBlockRef next_block;
50 LLVMBasicBlockRef new_block;
51
52 /* get current basic block */
53 current_block = LLVMGetInsertBlock(builder);
54
55 /* check if there's another block after this one */
56 next_block = LLVMGetNextBasicBlock(current_block);
57 if (next_block) {
58 /* insert the new block before the next block */
59 new_block = LLVMInsertBasicBlock(next_block, name);
60 }
61 else {
62 /* append new block after current block */
63 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
64 new_block = LLVMAppendBasicBlock(function, name);
65 }
66
67 return new_block;
68 }
69
70
71 /**
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.
74 */
75 void
76 lp_build_flow_skip_begin(struct lp_build_skip_context *skip,
77 LLVMBuilderRef builder)
78 {
79 skip->builder = builder;
80
81 /* create new basic block */
82 skip->block = lp_build_insert_new_block(skip->builder, "skip");
83 }
84
85
86 /**
87 * Insert code to test a condition and branch to the end of the current
88 * skip block if the condition is true.
89 */
90 void
91 lp_build_flow_skip_cond_break(struct lp_build_skip_context *skip,
92 LLVMValueRef cond)
93 {
94 LLVMBasicBlockRef new_block;
95
96 new_block = lp_build_insert_new_block(skip->builder, "");
97
98 /* if cond is true, goto skip->block, else goto new_block */
99 LLVMBuildCondBr(skip->builder, cond, skip->block, new_block);
100
101 LLVMPositionBuilderAtEnd(skip->builder, new_block);
102 }
103
104
105 void
106 lp_build_flow_skip_end(struct lp_build_skip_context *skip)
107 {
108 /* goto block */
109 LLVMBuildBr(skip->builder, skip->block);
110 LLVMPositionBuilderAtEnd(skip->builder, skip->block);
111 }
112
113
114 /**
115 * Check if the mask predicate is zero. If so, jump to the end of the block.
116 */
117 void
118 lp_build_mask_check(struct lp_build_mask_context *mask)
119 {
120 LLVMBuilderRef builder = mask->skip.builder;
121 LLVMValueRef value;
122 LLVMValueRef cond;
123
124 value = lp_build_mask_value(mask);
125
126 /* cond = (mask == 0) */
127 cond = LLVMBuildICmp(builder,
128 LLVMIntEQ,
129 LLVMBuildBitCast(builder, value, mask->reg_type, ""),
130 LLVMConstNull(mask->reg_type),
131 "");
132
133 /* if cond, goto end of block */
134 lp_build_flow_skip_cond_break(&mask->skip, cond);
135 }
136
137
138 /**
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
144 */
145 void
146 lp_build_mask_begin(struct lp_build_mask_context *mask,
147 LLVMBuilderRef builder,
148 struct lp_type type,
149 LLVMValueRef value)
150 {
151 memset(mask, 0, sizeof *mask);
152
153 mask->reg_type = LLVMIntType(type.width * type.length);
154 mask->var = lp_build_alloca(builder,
155 lp_build_int_vec_type(type),
156 "execution_mask");
157
158 LLVMBuildStore(builder, value, mask->var);
159
160 lp_build_flow_skip_begin(&mask->skip, builder);
161 }
162
163
164 LLVMValueRef
165 lp_build_mask_value(struct lp_build_mask_context *mask)
166 {
167 return LLVMBuildLoad(mask->skip.builder, mask->var, "");
168 }
169
170
171 /**
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.
175 */
176 void
177 lp_build_mask_update(struct lp_build_mask_context *mask,
178 LLVMValueRef value)
179 {
180 value = LLVMBuildAnd(mask->skip.builder,
181 lp_build_mask_value(mask),
182 value, "");
183 LLVMBuildStore(mask->skip.builder, value, mask->var);
184 }
185
186
187 /**
188 * End section of code which is predicated on a mask.
189 */
190 LLVMValueRef
191 lp_build_mask_end(struct lp_build_mask_context *mask)
192 {
193 lp_build_flow_skip_end(&mask->skip);
194 return lp_build_mask_value(mask);
195 }
196
197
198
199 void
200 lp_build_loop_begin(LLVMBuilderRef builder,
201 LLVMValueRef start,
202 struct lp_build_loop_state *state)
203 {
204 state->block = lp_build_insert_new_block(builder, "loop_begin");
205
206 state->counter_var = lp_build_alloca(builder, LLVMTypeOf(start), "loop_counter");
207
208 LLVMBuildStore(builder, start, state->counter_var);
209
210 LLVMBuildBr(builder, state->block);
211
212 LLVMPositionBuilderAtEnd(builder, state->block);
213
214 state->counter = LLVMBuildLoad(builder, state->counter_var, "");
215 }
216
217
218 void
219 lp_build_loop_end_cond(LLVMBuilderRef builder,
220 LLVMValueRef end,
221 LLVMValueRef step,
222 LLVMIntPredicate llvm_cond,
223 struct lp_build_loop_state *state)
224 {
225 LLVMValueRef next;
226 LLVMValueRef cond;
227 LLVMBasicBlockRef after_block;
228
229 if (!step)
230 step = LLVMConstInt(LLVMTypeOf(end), 1, 0);
231
232 next = LLVMBuildAdd(builder, state->counter, step, "");
233
234 LLVMBuildStore(builder, next, state->counter_var);
235
236 cond = LLVMBuildICmp(builder, llvm_cond, next, end, "");
237
238 after_block = lp_build_insert_new_block(builder, "loop_end");
239
240 LLVMBuildCondBr(builder, cond, after_block, state->block);
241
242 LLVMPositionBuilderAtEnd(builder, after_block);
243
244 state->counter = LLVMBuildLoad(builder, state->counter_var, "");
245 }
246
247
248 void
249 lp_build_loop_end(LLVMBuilderRef builder,
250 LLVMValueRef end,
251 LLVMValueRef step,
252 struct lp_build_loop_state *state)
253 {
254 lp_build_loop_end_cond(builder, end, step, LLVMIntNE, state);
255 }
256
257
258
259 /*
260 Example of if/then/else building:
261
262 int x;
263 if (cond) {
264 x = 1 + 2;
265 }
266 else {
267 x = 2 + 3;
268 }
269
270 Is built with:
271
272 // x needs an alloca variable
273 x = lp_build_alloca(builder, type, "x");
274
275
276 lp_build_if(ctx, builder, cond);
277 LLVMBuildStore(LLVMBuildAdd(1, 2), x);
278 lp_build_else(ctx);
279 LLVMBuildStore(LLVMBuildAdd(2, 3). x);
280 lp_build_endif(ctx);
281
282 */
283
284
285
286 /**
287 * Begin an if/else/endif construct.
288 */
289 void
290 lp_build_if(struct lp_build_if_state *ifthen,
291 LLVMBuilderRef builder,
292 LLVMValueRef condition)
293 {
294 LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
295
296 memset(ifthen, 0, sizeof *ifthen);
297 ifthen->builder = builder;
298 ifthen->condition = condition;
299 ifthen->entry_block = block;
300
301 /* create endif/merge basic block for the phi functions */
302 ifthen->merge_block = lp_build_insert_new_block(builder, "endif-block");
303
304 /* create/insert true_block before merge_block */
305 ifthen->true_block = LLVMInsertBasicBlock(ifthen->merge_block, "if-true-block");
306
307 /* successive code goes into the true block */
308 LLVMPositionBuilderAtEnd(builder, ifthen->true_block);
309 }
310
311
312 /**
313 * Begin else-part of a conditional
314 */
315 void
316 lp_build_else(struct lp_build_if_state *ifthen)
317 {
318 /* Append an unconditional Br(anch) instruction on the true_block */
319 LLVMBuildBr(ifthen->builder, ifthen->merge_block);
320
321 /* create/insert false_block before the merge block */
322 ifthen->false_block = LLVMInsertBasicBlock(ifthen->merge_block, "if-false-block");
323
324 /* successive code goes into the else block */
325 LLVMPositionBuilderAtEnd(ifthen->builder, ifthen->false_block);
326 }
327
328
329 /**
330 * End a conditional.
331 */
332 void
333 lp_build_endif(struct lp_build_if_state *ifthen)
334 {
335 /* Insert branch to the merge block from current block */
336 LLVMBuildBr(ifthen->builder, ifthen->merge_block);
337
338 /*
339 * Now patch in the various branch instructions.
340 */
341
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);
348 }
349 else {
350 /* no else clause */
351 LLVMBuildCondBr(ifthen->builder, ifthen->condition,
352 ifthen->true_block, ifthen->merge_block);
353 }
354
355 /* Resume building code at end of the ifthen->merge_block */
356 LLVMPositionBuilderAtEnd(ifthen->builder, ifthen->merge_block);
357 }
358
359
360 /**
361 * Allocate a scalar (or vector) variable.
362 *
363 * Although not strictly part of control flow, control flow has deep impact in
364 * how variables should be allocated.
365 *
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.
371 *
372 * See also:
373 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
374 */
375 LLVMValueRef
376 lp_build_alloca(LLVMBuilderRef builder,
377 LLVMTypeRef type,
378 const char *name)
379 {
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();
385 LLVMValueRef res;
386
387 if (first_instr) {
388 LLVMPositionBuilderBefore(first_builder, first_instr);
389 } else {
390 LLVMPositionBuilderAtEnd(first_builder, first_block);
391 }
392
393 res = LLVMBuildAlloca(first_builder, type, name);
394 LLVMBuildStore(builder, LLVMConstNull(type), res);
395
396 LLVMDisposeBuilder(first_builder);
397
398 return res;
399 }
400
401
402 /**
403 * Allocate an array of scalars/vectors.
404 *
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
408 * required.
409 *
410 * Also the scalarrepl pass is supposedly more powerful and can promote
411 * arrays in many cases.
412 *
413 * See also:
414 * - http://www.llvm.org/docs/tutorial/OCamlLangImpl7.html#memory
415 */
416 LLVMValueRef
417 lp_build_array_alloca(LLVMBuilderRef builder,
418 LLVMTypeRef type,
419 LLVMValueRef count,
420 const char *name)
421 {
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();
427 LLVMValueRef res;
428
429 if (first_instr) {
430 LLVMPositionBuilderBefore(first_builder, first_instr);
431 } else {
432 LLVMPositionBuilderAtEnd(first_builder, first_block);
433 }
434
435 res = LLVMBuildArrayAlloca(first_builder, type, count, name);
436
437 LLVMDisposeBuilder(first_builder);
438
439 return res;
440 }