llvm_lib/core/values/uses.rs
1//! Defines functions that allow to inspect the uses
2//! of a `ValueRef`.
3
4use crate::core::values::ValueRef;
5use crate::CUint;
6use llvm_sys::core;
7use llvm_sys::prelude::LLVMUseRef;
8
9/// LLVM `UseRef` wrapper
10#[derive(Debug)]
11pub struct UseRef(LLVMUseRef);
12
13/// Obtain the first use of a value.
14///
15/// Uses are obtained in an iterator fashion. First, call this function
16/// to obtain a reference to the first use. Then, call `get_next_use`
17/// on that instance and all subsequently obtained instances until
18/// `get_next_use` returns `NULL`.
19///
20/// # Details
21///
22/// Obtains the first use of a value in the LLVM IR.
23///
24/// This function wraps the `LLVMGetFirstUse` function from the LLVM core library. It retrieves the first use
25/// of the value represented by `ValueRef`. In LLVM IR, a "use" represents an instance where a value is used by an
26/// instruction or another value. The use can be iterated over to find all instances where this value is used.
27///
28/// After obtaining the first use with this function, you can call `get_next_use` on the resulting `UseRef` to
29/// iterate over all uses of the value. Continue calling `get_next_use` on each subsequent `UseRef` until it returns `None`.
30///
31/// # Returns
32///
33/// Returns an `Option<UseRef>`:
34/// - `Some(UseRef)` if there is a use associated with the value.
35/// - `None` if there are no uses associated with the value.
36#[must_use]
37pub fn get_first_use(val: &ValueRef) -> Option<UseRef> {
38 let first_use = unsafe { core::LLVMGetFirstUse(val.0) };
39 if first_use.is_null() {
40 None
41 } else {
42 Some(UseRef(first_use))
43 }
44}
45
46/// Obtain the next use of a value.
47///
48/// This effectively advances the iterator. It returns `NULL` if you are on
49/// the final use and no more are available.
50///
51/// # Details
52///
53/// Obtains the next use of a value in the LLVM IR.
54///
55/// This function wraps the `LLVMGetNextUse` function from the LLVM core library. It advances the iterator
56/// of uses for a value, returning the next use after the provided `UseRef`. If there are no more uses,
57/// it returns `None`. This function is used in conjunction with `get_first_use` to iterate over all uses
58/// of a value in LLVM IR.
59///
60/// # Parameters
61///
62/// - `u`: A reference to the current `UseRef` from which to obtain the next use.
63///
64/// # Returns
65///
66/// Returns an `Option<UseRef>`:
67/// - `Some(UseRef)` if there is a subsequent use associated with the value.
68/// - `None` if there are no more uses associated with the value.
69#[must_use]
70pub fn get_next_use(u: &UseRef) -> Option<UseRef> {
71 let next_use = unsafe { core::LLVMGetNextUse(u.0) };
72 if next_use.is_null() {
73 None
74 } else {
75 Some(UseRef(next_use))
76 }
77}
78
79/// Obtain the user value for a user.
80///
81/// The returned value corresponds to a `UserRef` type.
82///
83/// # Details
84///
85/// Obtains the value that is using another value in LLVM IR.
86///
87/// This function wraps the `LLVMGetUser` function from the LLVM core library. It retrieves the user value
88/// associated with the provided `UseRef`. In LLVM IR, a "user" is an entity (typically an instruction or another
89/// value) that makes use of a particular value. This function returns the value that corresponds to the user.
90///
91/// # Parameters
92///
93/// - `u`: A reference to the `UseRef` for which to obtain the user value.
94///
95/// # Returns
96///
97/// Returns an instance of `ValueRef`, which represents the user value associated with the provided `UseRef`.
98#[must_use]
99pub fn get_user(u: &UseRef) -> ValueRef {
100 unsafe { ValueRef::from(core::LLVMGetUser(u.0)) }
101}
102
103/// Obtain the value this use corresponds to.
104///
105/// # Details
106///
107/// Obtains the value that is being used by a specific use in LLVM IR.
108///
109/// This function wraps the `LLVMGetUsedValue` function from the LLVM core library. It retrieves the value
110/// associated with a specific `UseRef`, which represents the value that is being used. This is useful for
111/// understanding which value is being utilized in a particular operation or instruction within LLVM IR.
112///
113/// # Parameters
114///
115/// - `u`: A reference to the `UseRef` for which to obtain the used value.
116///
117/// # Returns
118///
119/// Returns an instance of `ValueRef`, which represents the value that is being used by the provided `UseRef`.
120#[must_use]
121pub fn get_used_value(u: &UseRef) -> ValueRef {
122 unsafe { ValueRef::from(core::LLVMGetUsedValue(u.0)) }
123}
124
125/// Obtain an operand at a specific index in a `LLVM User` value.
126///
127/// # Details
128///
129/// Retrieves an operand at a specified index from a value in LLVM IR.
130///
131/// This function wraps the `LLVMGetOperand` function from the LLVM core library. It returns the operand
132/// at the specified index (`index`) from the value represented by `ValueRef`. Operands are the inputs to instructions
133/// or other values in LLVM IR. If the index is out of bounds or the operand cannot be retrieved, the function
134/// returns `None`.
135///
136/// # Parameters
137///
138/// - `index`: The index of the operand to retrieve. This index should be within the bounds of the number of operands the value has.
139///
140/// # Returns
141///
142/// Returns an `Option<ValueRef>`:
143/// - `Some(ValueRef)` containing the retrieved operand if the index is valid and the operand is found.
144/// - `None` if the index is out of bounds or the operand cannot be retrieved.
145#[must_use]
146pub fn get_operand(val: &ValueRef, index: u32) -> Option<ValueRef> {
147 let operand = unsafe { core::LLVMGetOperand(val.0, *CUint::from(index)) };
148 if operand.is_null() {
149 None
150 } else {
151 Some(ValueRef::from(operand))
152 }
153}
154
155/// Obtain the use of an operand at a specific index in a `LLVM User` value.
156///
157/// # Details
158///
159/// Retrieves the use of an operand at a specified index from a value in LLVM IR.
160///
161/// This function wraps the `LLVMGetOperandUse` function from the LLVM core library. It returns the `UseRef`
162/// associated with the operand at the specified index (`index`) from the value represented by `ValueRef`. In LLVM IR,
163/// a "use" refers to an instance where an operand is used by an instruction or another value. If the index is out of
164/// bounds or the operand use cannot be retrieved, the function returns `None`.
165///
166/// # Parameters
167///
168/// - `index`: The index of the operand use to retrieve. This index should be within the bounds of the number of operands the value has.
169///
170/// # Returns
171///
172/// Returns an `Option<UseRef>`:
173/// - `Some(UseRef)` containing the retrieved operand use if the index is valid and the operand use is found.
174/// - `None` if the index is out of bounds or the operand use cannot be retrieved.
175#[must_use]
176pub fn get_operand_use(val: &ValueRef, index: u32) -> Option<UseRef> {
177 let operand_use = unsafe { core::LLVMGetOperandUse(val.0, *CUint::from(index)) };
178 if operand_use.is_null() {
179 None
180 } else {
181 Some(UseRef(operand_use))
182 }
183}
184
185/// Set an operand at a specific index in a `LLVM User` value.
186///
187/// # Details
188///
189/// Sets the value of an operand at a specified index for a value in LLVM IR.
190///
191/// This function wraps the `LLVMSetOperand` function from the LLVM core library. It assigns a new value
192/// (`val`) to the operand at the specified index (`index`) for the value represented by `ValueRef`. This allows
193/// modification of the operands of an instruction or another value within LLVM IR.
194///
195/// # Parameters
196///
197/// - `index`: The index of the operand to set. This index should be within the bounds of the number of operands the value has.
198/// - `val`: A reference to the new value (`ValueRef`) that will be assigned to the operand at the specified index.
199pub fn set_operand(val1: &mut ValueRef, index: u32, val2: &ValueRef) {
200 unsafe { core::LLVMSetOperand(val1.0, index, val2.0) }
201}
202
203/// Obtain the number of operands in a `LLVM User` value.
204///
205/// # Details
206///
207/// Retrieves the number of operands associated with a value in LLVM IR.
208///
209/// This function wraps the `LLVMGetNumOperands` function from the LLVM core library. It returns the number
210/// of operands that the value represented by `ValueRef` has. This is useful for determining how many inputs or
211/// arguments a particular instruction or value takes within LLVM IR.
212///
213/// # Returns
214///
215/// Returns an `i32` representing the number of operands associated with the value.
216#[must_use]
217pub fn get_num_operands(val: &ValueRef) -> i32 {
218 unsafe { core::LLVMGetNumOperands(val.0) }
219}