José Fonseca | 8783732 | 2009-07-27 01:23:15 +0100 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
José Fonseca | 9a519ec | 2009-07-28 08:14:10 +0100 | [diff] [blame] | 3 | * Copyright 2009 VMware, Inc. |
José Fonseca | 8783732 | 2009-07-27 01:23:15 +0100 | [diff] [blame] | 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 | |
José Fonseca | 8783732 | 2009-07-27 01:23:15 +0100 | [diff] [blame] | 28 | /** |
José Fonseca | c5531f5 | 2009-08-21 10:57:48 +0100 | [diff] [blame^] | 29 | * LLVM control flow build helpers. |
José Fonseca | 8783732 | 2009-07-27 01:23:15 +0100 | [diff] [blame] | 30 | * |
José Fonseca | c5531f5 | 2009-08-21 10:57:48 +0100 | [diff] [blame^] | 31 | * @author Jose Fonseca <jfonseca@vmware.com> |
José Fonseca | 8783732 | 2009-07-27 01:23:15 +0100 | [diff] [blame] | 32 | */ |
| 33 | |
José Fonseca | c5531f5 | 2009-08-21 10:57:48 +0100 | [diff] [blame^] | 34 | #include "lp_bld_flow.h" |
| 35 | |
| 36 | |
José Fonseca | 8783732 | 2009-07-27 01:23:15 +0100 | [diff] [blame] | 37 | |
| 38 | void |
| 39 | lp_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 | |
| 59 | void |
| 60 | lp_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 | |