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"
36 #include "lp_bld_type.h"
37 #include "lp_bld_flow.h"
41 lp_build_mask_begin(struct lp_build_mask_context
*mask
,
42 LLVMBuilderRef builder
,
46 memset(mask
, 0, sizeof *mask
);
48 mask
->builder
= builder
;
49 mask
->reg_type
= LLVMIntType(type
.width
* type
.length
);
55 lp_build_mask_update(struct lp_build_mask_context
*mask
,
60 LLVMBasicBlockRef current_block
;
61 LLVMBasicBlockRef next_block
;
62 LLVMBasicBlockRef new_block
;
65 mask
->value
= LLVMBuildAnd(mask
->builder
, mask
->value
, value
, "");
69 /* FIXME: disabled until we have proper control flow helpers */
71 cond
= LLVMBuildICmp(mask
->builder
,
73 LLVMBuildBitCast(mask
->builder
, mask
->value
, mask
->reg_type
, ""),
74 LLVMConstNull(mask
->reg_type
),
77 current_block
= LLVMGetInsertBlock(mask
->builder
);
79 if(!mask
->skip_block
) {
80 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
81 mask
->skip_block
= LLVMAppendBasicBlock(function
, "skip");
83 mask
->phi
= LLVMBuildPhi(mask
->builder
, LLVMTypeOf(mask
->value
), "");
86 next_block
= LLVMGetNextBasicBlock(current_block
);
89 new_block
= LLVMInsertBasicBlock(next_block
, "");
92 LLVMValueRef function
= LLVMGetBasicBlockParent(current_block
);
93 new_block
= LLVMAppendBasicBlock(function
, "");
96 LLVMAddIncoming(mask
->phi
, &mask
->value
, ¤t_block
, 1);
97 LLVMBuildCondBr(mask
->builder
, cond
, mask
->skip_block
, new_block
);
99 LLVMPositionBuilderAtEnd(mask
->builder
, new_block
);
105 lp_build_mask_end(struct lp_build_mask_context
*mask
)
107 if(mask
->skip_block
) {
108 LLVMBasicBlockRef current_block
= LLVMGetInsertBlock(mask
->builder
);
110 LLVMAddIncoming(mask
->phi
, &mask
->value
, ¤t_block
, 1);
111 LLVMBuildBr(mask
->builder
, mask
->skip_block
);
113 LLVMPositionBuilderAtEnd(mask
->builder
, mask
->skip_block
);
115 mask
->value
= mask
->phi
;
117 mask
->skip_block
= NULL
;
126 lp_build_loop_begin(LLVMBuilderRef builder
,
128 struct lp_build_loop_state
*state
)
130 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
131 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
133 state
->block
= LLVMAppendBasicBlock(function
, "loop");
135 LLVMBuildBr(builder
, state
->block
);
137 LLVMPositionBuilderAtEnd(builder
, state
->block
);
139 state
->counter
= LLVMBuildPhi(builder
, LLVMTypeOf(start
), "");
141 LLVMAddIncoming(state
->counter
, &start
, &block
, 1);
147 lp_build_loop_end(LLVMBuilderRef builder
,
150 struct lp_build_loop_state
*state
)
152 LLVMBasicBlockRef block
= LLVMGetInsertBlock(builder
);
153 LLVMValueRef function
= LLVMGetBasicBlockParent(block
);
156 LLVMBasicBlockRef after_block
;
159 step
= LLVMConstInt(LLVMTypeOf(end
), 1, 0);
161 next
= LLVMBuildAdd(builder
, state
->counter
, step
, "");
163 cond
= LLVMBuildICmp(builder
, LLVMIntNE
, next
, end
, "");
165 after_block
= LLVMAppendBasicBlock(function
, "");
167 LLVMBuildCondBr(builder
, cond
, after_block
, state
->block
);
169 LLVMAddIncoming(state
->counter
, &next
, &block
, 1);
171 LLVMPositionBuilderAtEnd(builder
, after_block
);