blob: 5fc85a19b843961f3ccd4f1be566c139acbd30f9 [file] [log] [blame]
José Fonseca87837322009-07-27 01:23:15 +01001/**************************************************************************
2 *
José Fonseca9a519ec2009-07-28 08:14:10 +01003 * Copyright 2009 VMware, Inc.
José Fonseca87837322009-07-27 01:23:15 +01004 * 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
José Fonseca87837322009-07-27 01:23:15 +010028/**
José Fonsecac5531f52009-08-21 10:57:48 +010029 * LLVM control flow build helpers.
José Fonseca87837322009-07-27 01:23:15 +010030 *
José Fonsecac5531f52009-08-21 10:57:48 +010031 * @author Jose Fonseca <jfonseca@vmware.com>
José Fonseca87837322009-07-27 01:23:15 +010032 */
33
José Fonsecac5531f52009-08-21 10:57:48 +010034#include "lp_bld_flow.h"
35
36
José Fonseca87837322009-07-27 01:23:15 +010037
38void
39lp_build_loop_begin(LLVMBuilderRef builder,
40 LLVMValueRef start,
41 struct lp_build_loop_state *state)
42{
43 LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
44 LLVMValueRef function = LLVMGetBasicBlockParent(block);
45
46 state->block = LLVMAppendBasicBlock(function, "loop");
47
48 LLVMBuildBr(builder, state->block);
49
50 LLVMPositionBuilderAtEnd(builder, state->block);
51
52 state->counter = LLVMBuildPhi(builder, LLVMTypeOf(start), "");
53
54 LLVMAddIncoming(state->counter, &start, &block, 1);
55
56}
57
58
59void
60lp_build_loop_end(LLVMBuilderRef builder,
61 LLVMValueRef end,
62 LLVMValueRef step,
63 struct lp_build_loop_state *state)
64{
65 LLVMBasicBlockRef block = LLVMGetInsertBlock(builder);
66 LLVMValueRef function = LLVMGetBasicBlockParent(block);
67 LLVMValueRef next;
68 LLVMValueRef cond;
69 LLVMBasicBlockRef after_block;
70
71 if (!step)
72 step = LLVMConstInt(LLVMTypeOf(end), 1, 0);
73
74 next = LLVMBuildAdd(builder, state->counter, step, "");
75
76 cond = LLVMBuildICmp(builder, LLVMIntNE, next, end, "");
77
78 after_block = LLVMAppendBasicBlock(function, "");
79
80 LLVMBuildCondBr(builder, cond, after_block, state->block);
81
82 LLVMAddIncoming(state->counter, &next, &block, 1);
83
84 LLVMPositionBuilderAtEnd(builder, after_block);
85}
86