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 32
42 #define LP_BUILD_FLOW_MAX_DEPTH 32
46 * Enumeration of all possible flow constructs.
48 enum lp_build_flow_construct_kind
{
55 * Variable declaration scope.
57 struct lp_build_flow_scope
59 /** Number of variables declared in this scope */
60 unsigned num_variables
;
65 * Early exit. Useful to skip to the end of a function or block when
66 * the execution mask becomes zero or when there is an error condition.
68 struct lp_build_flow_skip
70 /** Block to skip to */
71 LLVMBasicBlockRef block
;
73 /** Number of variables declared at the beginning */
74 unsigned num_variables
;
81 * Union of all possible flow constructs' data
83 union lp_build_flow_construct_data
85 struct lp_build_flow_scope scope
;
86 struct lp_build_flow_skip skip
;
91 * Element of the flow construct stack.
93 struct lp_build_flow_construct
95 enum lp_build_flow_construct_kind kind
;
96 union lp_build_flow_construct_data data
;
101 * All necessary data to generate LLVM control flow constructs.
103 * Besides keeping track of the control flow construct themselves we also
104 * need to keep track of variables in order to generate SSA Phi values.
106 struct lp_build_flow_context
108 LLVMBuilderRef builder
;
111 * Control flow stack.
113 struct lp_build_flow_construct constructs
[LP_BUILD_FLOW_MAX_DEPTH
];
114 unsigned num_constructs
;
119 LLVMValueRef
*variables
[LP_BUILD_FLOW_MAX_VARIABLES
];
120 unsigned num_variables
;
124 struct lp_build_flow_context
*
125 lp_build_flow_create(LLVMBuilderRef builder
)
127 struct lp_build_flow_context
*flow
;
129 flow
= CALLOC_STRUCT(lp_build_flow_context
);
133 flow
->builder
= builder
;
140 lp_build_flow_destroy(struct lp_build_flow_context
*flow
)
142 assert(flow
->num_constructs
== 0);
143 assert(flow
->num_variables
== 0);
148 static union lp_build_flow_construct_data
*
149 lp_build_flow_push(struct lp_build_flow_context
*flow
,
150 enum lp_build_flow_construct_kind kind
)
152 assert(flow
->num_constructs
< LP_BUILD_FLOW_MAX_DEPTH
);
153 if(flow
->num_constructs
>= LP_BUILD_FLOW_MAX_DEPTH
)
156 flow
->constructs
[flow
->num_constructs
].kind
= kind
;
157 return &flow
->constructs
[flow
->num_constructs
++].data
;
161 static union lp_build_flow_construct_data
*
162 lp_build_flow_peek(struct lp_build_flow_context
*flow
,
163 enum lp_build_flow_construct_kind kind
)
165 assert(flow
->num_constructs
);
166 if(!flow
->num_constructs
)
169 assert(flow
->constructs
[flow
->num_constructs
- 1].kind
== kind
);
170 if(flow
->constructs
[flow
->num_constructs
- 1].kind
!= kind
)
173 return &flow
->constructs
[flow
->num_constructs
- 1].data
;
177 static union lp_build_flow_construct_data
*
178 lp_build_flow_pop(struct lp_build_flow_context
*flow
,
179 enum lp_build_flow_construct_kind kind
)
181 assert(flow
->num_constructs
);
182 if(!flow
->num_constructs
)
185 assert(flow
->constructs
[flow
->num_constructs
- 1].kind
== kind
);
186 if(flow
->constructs
[flow
->num_constructs
- 1].kind
!= kind
)
189 return &flow
->constructs
[--flow
->num_constructs
].data
;
194 * Begin a variable scope.
199 lp_build_flow_scope_begin(struct lp_build_flow_context
*flow
)
201 struct lp_build_flow_scope
*scope
;
203 scope
= &lp_build_flow_push(flow
, lP_BUILD_FLOW_SCOPE
)->scope
;
207 scope
->num_variables
= 0;
212 * Declare a variable.
214 * A variable is a named entity which can have different LLVMValueRef's at
215 * different points of the program. This is relevant for control flow because
216 * when there are mutiple branches to a same location we need to replace
217 * the variable's value with a Phi function as explained in
218 * http://en.wikipedia.org/wiki/Static_single_assignment_form .
220 * We keep track of variables by keeping around a pointer to where their
223 * There are a few cautions to observe:
225 * - Variable's value must not be NULL. If there is no initial value then
226 * LLVMGetUndef() should be used.
228 * - Variable's value must be kept up-to-date. If the variable is going to be
229 * modified by a function then a pointer should be passed so that its value
230 * is accurate. Failure to do this will cause some of the variables'
231 * transient values to be lost, leading to wrong results.
233 * - A program should be written from top to bottom, by always appending
234 * instructions to the bottom with a single LLVMBuilderRef. Inserting and/or
235 * modifying existing statements will most likely lead to wrong results.
239 lp_build_flow_scope_declare(struct lp_build_flow_context
*flow
,
240 LLVMValueRef
*variable
)
242 struct lp_build_flow_scope
*scope
;
244 scope
= &lp_build_flow_peek(flow
, lP_BUILD_FLOW_SCOPE
)->scope
;
252 assert(flow
->num_variables
< LP_BUILD_FLOW_MAX_VARIABLES
);
253 if(flow
->num_variables
>= LP_BUILD_FLOW_MAX_VARIABLES
)
256 flow
->variables
[flow
->num_variables
++] = variable
;
257 ++scope
->num_variables
;
262 lp_build_flow_scope_end(struct lp_build_flow_context
*flow
)
264 struct lp_build_flow_scope
*scope
;
266 scope
= &lp_build_flow_pop(flow
, lP_BUILD_FLOW_SCOPE
)->scope
;
270 assert(flow
->num_variables
>= scope
->num_variables
);
271 if(flow
->num_variables
< scope
->num_variables
) {
272 flow
->num_variables
= 0;
276 flow
->num_variables
-= scope
->num_variables
;
280 static LLVMBasicBlockRef
281 lp_build_flow_insert_block(struct lp_build_flow_context
*flow
)
283 LLVMBasicBlockRef current_block
;
284 LLVMBasicBlockRef next_block
;
285 LLVMBasicBlockRef new_block
;
287 current_block
= LLVMGetInsertBlock(flow
->builder
);
289 next_block
= LLVMGetNextBasicBlock(current_block
);
291 new_block
= LLVMInsertBasicBlock(next_block
, "");
294 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
295 new_block
= LLVMAppendBasicBlock(function
, "");
302 lp_build_flow_skip_begin(struct lp_build_flow_context
*flow
)
304 struct lp_build_flow_skip
*skip
;
305 LLVMBuilderRef builder
;
308 skip
= &lp_build_flow_push(flow
, LP_BUILD_FLOW_SKIP
)->skip
;
312 skip
->block
= lp_build_flow_insert_block(flow
);
313 skip
->num_variables
= flow
->num_variables
;
314 if(!skip
->num_variables
) {
319 skip
->phi
= MALLOC(skip
->num_variables
* sizeof *skip
->phi
);
321 skip
->num_variables
= 0;
325 builder
= LLVMCreateBuilder();
326 LLVMPositionBuilderAtEnd(builder
, skip
->block
);
328 for(i
= 0; i
< skip
->num_variables
; ++i
)
329 skip
->phi
[i
] = LLVMBuildPhi(builder
, LLVMTypeOf(*flow
->variables
[i
]), "");
331 LLVMDisposeBuilder(builder
);
336 lp_build_flow_skip_cond_break(struct lp_build_flow_context
*flow
,
339 struct lp_build_flow_skip
*skip
;
340 LLVMBasicBlockRef current_block
;
341 LLVMBasicBlockRef new_block
;
344 skip
= &lp_build_flow_peek(flow
, LP_BUILD_FLOW_SKIP
)->skip
;
348 current_block
= LLVMGetInsertBlock(flow
->builder
);
350 new_block
= lp_build_flow_insert_block(flow
);
352 for(i
= 0; i
< skip
->num_variables
; ++i
) {
353 assert(*flow
->variables
[i
]);
354 LLVMAddIncoming(skip
->phi
[i
], flow
->variables
[i
], ¤t_block
, 1);
357 LLVMBuildCondBr(flow
->builder
, cond
, skip
->block
, new_block
);
359 LLVMPositionBuilderAtEnd(flow
->builder
, new_block
);
364 lp_build_flow_skip_end(struct lp_build_flow_context
*flow
)
366 struct lp_build_flow_skip
*skip
;
367 LLVMBasicBlockRef current_block
;
370 skip
= &lp_build_flow_pop(flow
, LP_BUILD_FLOW_SKIP
)->skip
;
374 current_block
= LLVMGetInsertBlock(flow
->builder
);
376 for(i
= 0; i
< skip
->num_variables
; ++i
) {
377 assert(*flow
->variables
[i
]);
378 LLVMAddIncoming(skip
->phi
[i
], flow
->variables
[i
], ¤t_block
, 1);
379 *flow
->variables
[i
] = skip
->phi
[i
];
382 LLVMBuildBr(flow
->builder
, skip
->block
);
383 LLVMPositionBuilderAtEnd(flow
->builder
, skip
->block
);
390 lp_build_mask_begin(struct lp_build_mask_context
*mask
,
391 struct lp_build_flow_context
*flow
,
395 memset(mask
, 0, sizeof *mask
);
398 mask
->reg_type
= LLVMIntType(type
.width
* type
.length
);
401 lp_build_flow_scope_begin(flow
);
402 lp_build_flow_scope_declare(flow
, &mask
->value
);
403 lp_build_flow_skip_begin(flow
);
408 lp_build_mask_update(struct lp_build_mask_context
*mask
,
411 LLVMBuilderRef builder
= mask
->flow
->builder
;
414 mask
->value
= LLVMBuildAnd(builder
, mask
->value
, value
, "");
416 cond
= LLVMBuildICmp(builder
,
418 LLVMBuildBitCast(builder
, mask
->value
, mask
->reg_type
, ""),
419 LLVMConstNull(mask
->reg_type
),
422 lp_build_flow_skip_cond_break(mask
->flow
, cond
);
427 lp_build_mask_end(struct lp_build_mask_context
*mask
)
429 lp_build_flow_skip_end(mask
->flow
);
430 lp_build_flow_scope_end(mask
->flow
);
437 lp_build_loop_begin(LLVMBuilderRef builder
,
439 struct lp_build_loop_state
*state
)
441 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
442 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
444 state
->block
= LLVMAppendBasicBlock(function
, "loop");
446 LLVMBuildBr(builder
, state
->block
);
448 LLVMPositionBuilderAtEnd(builder
, state
->block
);
450 state
->counter
= LLVMBuildPhi(builder
, LLVMTypeOf(start
), "");
452 LLVMAddIncoming(state
->counter
, &start
, &block
, 1);
458 lp_build_loop_end(LLVMBuilderRef builder
,
461 struct lp_build_loop_state
*state
)
463 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
464 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
467 LLVMBasicBlockRef after_block
;
470 step
= LLVMConstInt(LLVMTypeOf(end
), 1, 0);
472 next
= LLVMBuildAdd(builder
, state
->counter
, step
, "");
474 cond
= LLVMBuildICmp(builder
, LLVMIntNE
, next
, end
, "");
476 after_block
= LLVMAppendBasicBlock(function
, "");
478 LLVMBuildCondBr(builder
, cond
, after_block
, state
->block
);
480 LLVMAddIncoming(state
->counter
, &next
, &block
, 1);
482 LLVMPositionBuilderAtEnd(builder
, after_block
);