|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use lib::llvm::{llvm, UseRef, ValueRef}; |
| 12 | +use middle::trans::basic_block::BasicBlock; |
| 13 | +use middle::trans::common::Block; |
| 14 | +use std::libc::c_uint; |
| 15 | + |
| 16 | +pub struct Value(ValueRef); |
| 17 | + |
| 18 | +macro_rules! opt_val ( ($e:expr) => ( |
| 19 | + unsafe { |
| 20 | + match $e { |
| 21 | + p if p.is_not_null() => Some(Value(p)), |
| 22 | + _ => None |
| 23 | + } |
| 24 | + } |
| 25 | +)) |
| 26 | + |
| 27 | +/** |
| 28 | + * Wrapper for LLVM ValueRef |
| 29 | + */ |
| 30 | +impl Value { |
| 31 | + /// Returns the BasicBlock that contains this value |
| 32 | + pub fn get_parent(self) -> Option<BasicBlock> { |
| 33 | + unsafe { |
| 34 | + match llvm::LLVMGetInstructionParent(*self) { |
| 35 | + p if p.is_not_null() => Some(BasicBlock(p)), |
| 36 | + _ => None |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Removes this value from its containing BasicBlock |
| 42 | + pub fn erase_from_parent(self) { |
| 43 | + unsafe { |
| 44 | + llvm::LLVMInstructionEraseFromParent(*self); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// Returns the single dominating store to this value, if any |
| 49 | + /// This only performs a search for a trivially dominating store. The store |
| 50 | + /// must be the only user of this value, and there must not be any conditional |
| 51 | + /// branches between the store and the given block. |
| 52 | + pub fn get_dominating_store(self, bcx: @mut Block) -> Option<Value> { |
| 53 | + match self.get_single_user().chain(|user| user.as_store_inst()) { |
| 54 | + Some(store) => { |
| 55 | + do store.get_parent().chain |store_bb| { |
| 56 | + let mut bb = BasicBlock(bcx.llbb); |
| 57 | + let mut ret = Some(store); |
| 58 | + while *bb != *store_bb { |
| 59 | + match bb.get_single_predecessor() { |
| 60 | + Some(pred) => bb = pred, |
| 61 | + None => { ret = None; break } |
| 62 | + } |
| 63 | + } |
| 64 | + ret |
| 65 | + } |
| 66 | + } |
| 67 | + _ => None |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /// Returns the first use of this value, if any |
| 72 | + pub fn get_first_use(self) -> Option<Use> { |
| 73 | + unsafe { |
| 74 | + match llvm::LLVMGetFirstUse(*self) { |
| 75 | + u if u.is_not_null() => Some(Use(u)), |
| 76 | + _ => None |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// Tests if there are no uses of this value |
| 82 | + pub fn has_no_uses(self) -> bool { |
| 83 | + self.get_first_use().is_none() |
| 84 | + } |
| 85 | + |
| 86 | + /// Returns the single user of this value |
| 87 | + /// If there are no users or multiple users, this returns None |
| 88 | + pub fn get_single_user(self) -> Option<Value> { |
| 89 | + let mut iter = self.user_iter(); |
| 90 | + match (iter.next(), iter.next()) { |
| 91 | + (Some(first), None) => Some(first), |
| 92 | + _ => None |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /// Returns an iterator for the users of this value |
| 97 | + pub fn user_iter(self) -> UserIterator { |
| 98 | + UserIterator { |
| 99 | + next: self.get_first_use() |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// Returns the requested operand of this instruction |
| 104 | + /// Returns None, if there's no operand at the given index |
| 105 | + pub fn get_operand(self, i: uint) -> Option<Value> { |
| 106 | + opt_val!(llvm::LLVMGetOperand(*self, i as c_uint)) |
| 107 | + } |
| 108 | + |
| 109 | + /// Returns the Store represent by this value, if any |
| 110 | + pub fn as_store_inst(self) -> Option<Value> { |
| 111 | + opt_val!(llvm::LLVMIsAStoreInst(*self)) |
| 112 | + } |
| 113 | + |
| 114 | + /// Tests if this value is a terminator instruction |
| 115 | + pub fn is_a_terminator_inst(self) -> bool { |
| 116 | + unsafe { |
| 117 | + llvm::LLVMIsATerminatorInst(*self).is_not_null() |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +pub struct Use(UseRef); |
| 123 | + |
| 124 | +/** |
| 125 | + * Wrapper for LLVM UseRef |
| 126 | + */ |
| 127 | +impl Use { |
| 128 | + pub fn get_user(self) -> Value { |
| 129 | + unsafe { |
| 130 | + Value(llvm::LLVMGetUser(*self)) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + pub fn get_next_use(self) -> Option<Use> { |
| 135 | + unsafe { |
| 136 | + match llvm::LLVMGetNextUse(*self) { |
| 137 | + u if u.is_not_null() => Some(Use(u)), |
| 138 | + _ => None |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/// Iterator for the users of a value |
| 145 | +pub struct UserIterator { |
| 146 | + priv next: Option<Use> |
| 147 | +} |
| 148 | + |
| 149 | +impl Iterator<Value> for UserIterator { |
| 150 | + fn next(&mut self) -> Option<Value> { |
| 151 | + let current = self.next; |
| 152 | + |
| 153 | + self.next = do current.chain |u| { u.get_next_use() }; |
| 154 | + |
| 155 | + do current.map |u| { u.get_user() } |
| 156 | + } |
| 157 | +} |
0 commit comments