Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / gallium / drivers / llvmpipe / 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
36 #include "lp_bld_type.h"
37 #include "lp_bld_flow.h"
38
39
40 void
41 lp_build_mask_begin(struct lp_build_mask_context *mask,
42 LLVMBuilderRef builder,
43 union lp_type type,
44 LLVMValueRef value)
45 {
46 memset(mask, 0, sizeof *mask);
47
48 mask->builder = builder;
49 mask->reg_type = LLVMIntType(type.width * type.length);
50 mask->value = value;
51 }
52
53
54 void
55 lp_build_mask_update(struct lp_build_mask_context *mask,
56 LLVMValueRef value)
57 {
58
59 LLVMValueRef cond;
60 LLVMBasicBlockRef current_block;
61 LLVMBasicBlockRef next_block;
62 LLVMBasicBlockRef new_block;
63
64 if(mask->value)
65 mask->value = LLVMBuildAnd(mask->builder, mask->value, value, "");
66 else
67 mask->value = value;
68
69 /* FIXME: disabled until we have proper control flow helpers */
70 #if 0
71 cond = LLVMBuildICmp(mask->builder,
72 LLVMIntEQ,
73 LLVMBuildBitCast(mask->builder, mask->value, mask->reg_type, ""),
74 LLVMConstNull(mask->reg_type),
75 "");
76
77 current_block = LLVMGetInsertBlock(mask->builder);
78
79 if(!mask->skip_block) {
80 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
81 mask->skip_block = LLVMAppendBasicBlock(function, "skip");
82
83 mask->phi = LLVMBuildPhi(mask->builder, LLVMTypeOf(mask->value), "");
84 }
85
86 next_block = LLVMGetNextBasicBlock(current_block);
87 assert(next_block);
88 if(next_block) {
89 new_block = LLVMInsertBasicBlock(next_block, "");
90 }
91 else {
92 LLVMValueRef function = LLVMGetBasicBlockParent(current_block);
93 new_block = LLVMAppendBasicBlock(function, "");
94 }
95
96 LLVMAddIncoming(mask->phi, &mask->value, &current_block, 1);
97 LLVMBuildCondBr(mask->builder, cond, mask->skip_block, new_block);
98
99 LLVMPositionBuilderAtEnd(mask->builder, new_block);
100 #endif
101 }
102
103
104 LLVMValueRef
105 lp_build_mask_end(struct lp_build_mask_context *mask)
106 {
107 if(mask->skip_block) {
108 LLVMBasicBlockRef current_block = LLVMGetInsertBlock(mask->builder);
109
110 LLVMAddIncoming(mask->phi, &mask->value, &current_block, 1);
111 LLVMBuildBr(mask->builder, mask->skip_block);
112
113 LLVMPositionBuilderAtEnd(mask->builder, mask->skip_block);
114
115 mask->value = mask->phi;
116 mask->phi = NULL;
117 mask->skip_block = NULL;
118 }
119
120 return mask->value;
121 }
122
123
124
125 void
126 lp_build_loop_begin(LLVMBuilderRef builder,
127 LLVMValueRef start,
128 struct lp_build_loop_state *state)
129 {
130 LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
131 LLVMValueRef function = LLVMGetBasicBlockParent(block);
132
133 state->block = LLVMAppendBasicBlock(function, "loop");
134
135 LLVMBuildBr(builder, state->block);
136
137 LLVMPositionBuilderAtEnd(builder, state->block);
138
139 state->counter = LLVMBuildPhi(builder, LLVMTypeOf(start), "");
140
141 LLVMAddIncoming(state->counter, &start, &block, 1);
142
143 }
144
145
146 void
147 lp_build_loop_end(LLVMBuilderRef builder,
148 LLVMValueRef end,
149 LLVMValueRef step,
150 struct lp_build_loop_state *state)
151 {
152 LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
153 LLVMValueRef function = LLVMGetBasicBlockParent(block);
154 LLVMValueRef next;
155 LLVMValueRef cond;
156 LLVMBasicBlockRef after_block;
157
158 if (!step)
159 step = LLVMConstInt(LLVMTypeOf(end), 1, 0);
160
161 next = LLVMBuildAdd(builder, state->counter, step, "");
162
163 cond = LLVMBuildICmp(builder, LLVMIntNE, next, end, "");
164
165 after_block = LLVMAppendBasicBlock(function, "");
166
167 LLVMBuildCondBr(builder, cond, after_block, state->block);
168
169 LLVMAddIncoming(state->counter, &next, &block, 1);
170
171 LLVMPositionBuilderAtEnd(builder, after_block);
172 }
173