gallivm: Use variables instead of Phis in loops.

With this commit all explicit Phi emission is now gone.
This commit is contained in:
José Fonseca 2010-10-10 19:05:05 +01:00
parent 48003f3567
commit 693667bf88
2 changed files with 36 additions and 55 deletions

View File

@ -201,19 +201,47 @@ lp_build_loop_begin(LLVMBuilderRef builder,
LLVMValueRef start, LLVMValueRef start,
struct lp_build_loop_state *state) struct lp_build_loop_state *state)
{ {
LLVMBasicBlockRef block = LLVMGetInsertBlock(builder); state->block = lp_build_insert_new_block(builder, "loop_begin");
LLVMValueRef function = LLVMGetBasicBlockParent(block);
state->block = LLVMAppendBasicBlock(function, "loop"); state->counter_var = lp_build_alloca(builder, LLVMTypeOf(start), "loop_counter");
LLVMBuildStore(builder, start, state->counter_var);
LLVMBuildBr(builder, state->block); LLVMBuildBr(builder, state->block);
LLVMPositionBuilderAtEnd(builder, state->block); LLVMPositionBuilderAtEnd(builder, state->block);
state->counter = LLVMBuildPhi(builder, LLVMTypeOf(start), ""); state->counter = LLVMBuildLoad(builder, state->counter_var, "");
}
LLVMAddIncoming(state->counter, &start, &block, 1);
void
lp_build_loop_end_cond(LLVMBuilderRef builder,
LLVMValueRef end,
LLVMValueRef step,
LLVMIntPredicate llvm_cond,
struct lp_build_loop_state *state)
{
LLVMValueRef next;
LLVMValueRef cond;
LLVMBasicBlockRef after_block;
if (!step)
step = LLVMConstInt(LLVMTypeOf(end), 1, 0);
next = LLVMBuildAdd(builder, state->counter, step, "");
LLVMBuildStore(builder, next, state->counter_var);
cond = LLVMBuildICmp(builder, llvm_cond, next, end, "");
after_block = lp_build_insert_new_block(builder, "loop_end");
LLVMBuildCondBr(builder, cond, after_block, state->block);
LLVMPositionBuilderAtEnd(builder, after_block);
state->counter = LLVMBuildLoad(builder, state->counter_var, "");
} }
@ -223,55 +251,7 @@ lp_build_loop_end(LLVMBuilderRef builder,
LLVMValueRef step, LLVMValueRef step,
struct lp_build_loop_state *state) struct lp_build_loop_state *state)
{ {
LLVMBasicBlockRef block = LLVMGetInsertBlock(builder); lp_build_loop_end_cond(builder, end, step, LLVMIntNE, state);
LLVMValueRef function = LLVMGetBasicBlockParent(block);
LLVMValueRef next;
LLVMValueRef cond;
LLVMBasicBlockRef after_block;
if (!step)
step = LLVMConstInt(LLVMTypeOf(end), 1, 0);
next = LLVMBuildAdd(builder, state->counter, step, "");
cond = LLVMBuildICmp(builder, LLVMIntNE, next, end, "");
after_block = LLVMAppendBasicBlock(function, "");
LLVMBuildCondBr(builder, cond, after_block, state->block);
LLVMAddIncoming(state->counter, &next, &block, 1);
LLVMPositionBuilderAtEnd(builder, after_block);
}
void
lp_build_loop_end_cond(LLVMBuilderRef builder,
LLVMValueRef end,
LLVMValueRef step,
int llvm_cond,
struct lp_build_loop_state *state)
{
LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
LLVMValueRef function = LLVMGetBasicBlockParent(block);
LLVMValueRef next;
LLVMValueRef cond;
LLVMBasicBlockRef after_block;
if (!step)
step = LLVMConstInt(LLVMTypeOf(end), 1, 0);
next = LLVMBuildAdd(builder, state->counter, step, "");
cond = LLVMBuildICmp(builder, llvm_cond, next, end, "");
after_block = LLVMAppendBasicBlock(function, "");
LLVMBuildCondBr(builder, cond, after_block, state->block);
LLVMAddIncoming(state->counter, &next, &block, 1);
LLVMPositionBuilderAtEnd(builder, after_block);
} }

View File

@ -108,6 +108,7 @@ lp_build_mask_end(struct lp_build_mask_context *mask);
struct lp_build_loop_state struct lp_build_loop_state
{ {
LLVMBasicBlockRef block; LLVMBasicBlockRef block;
LLVMValueRef counter_var;
LLVMValueRef counter; LLVMValueRef counter;
}; };
@ -128,7 +129,7 @@ void
lp_build_loop_end_cond(LLVMBuilderRef builder, lp_build_loop_end_cond(LLVMBuilderRef builder,
LLVMValueRef end, LLVMValueRef end,
LLVMValueRef step, LLVMValueRef step,
int cond, /* LLVM condition */ LLVMIntPredicate cond,
struct lp_build_loop_state *state); struct lp_build_loop_state *state);