1 /**
2 Copyright: Copyright (c) 2017, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 */
6 module llvm_hiwrap.value.phi;
7 
8 import llvm_hiwrap.types;
9 
10 /** Functions in this group only apply to instructions that map to
11  * llvm::PHINode instances.
12  *
13  * PHINode is a subgroup of Instruction.
14  */
15 struct PhiValue {
16     LxInstructionValue value;
17     alias value this;
18 
19     /**
20      * Add an incoming value to the end of a PHI list.
21      */
22     //void LLVMAddIncoming(LLVMValueRef PhiNode, LLVMValueRef *IncomingValues,
23     //                     LLVMBasicBlockRef *IncomingBlocks, unsigned Count);
24 
25     /// Obtain the number of incoming basic blocks to a PHI node.
26     IncomingBlocksRange incomingBlocks() {
27         return IncomingBlocksRange(this);
28     }
29 }
30 
31 /// The incoming value can be represenented as either a Value or BasicBlock.
32 struct IncomingPair {
33     import llvm_hiwrap.value.basic_block;
34 
35     BasicBlock block;
36     LxValue value;
37 }
38 
39 struct IncomingBlocksRange {
40     import llvm;
41     import llvm_hiwrap.value.basic_block;
42 
43     private PhiValue phi;
44 
45     const size_t length;
46 
47     this(PhiValue v) {
48         phi = v;
49         length = LLVMCountIncoming(v);
50     }
51 
52     private auto makePair(size_t index) {
53         // Obtain an incoming value to a PHI node as an LLVMValueRef.
54         auto llvm_value = LLVMGetIncomingValue(phi, cast(uint) index);
55         // Obtain an incoming value to a PHI node as an LLVMBasicBlockRef.
56         auto llvm_bb = LLVMGetIncomingBlock(phi, cast(uint) index);
57         return IncomingPair(llvm_bb.LxBasicBlock.BasicBlock, llvm_value.LxValue);
58     }
59 
60     IncomingPair opIndex(size_t index) {
61         assert(index < length);
62         return makePair(index);
63     }
64 
65     import llvm_hiwrap.util;
66 
67     mixin IndexedRangeX!IncomingPair;
68 }