Skip to main content

InstructionValue

Struct InstructionValue 

Source
pub struct InstructionValue<'ctx> { /* private fields */ }

Implementations§

Source§

impl<'ctx> InstructionValue<'ctx>

Source

pub unsafe fn new(instruction_value: LLVMValueRef) -> Self

Get a value from an LLVMValueRef.

§Safety

The ref must be valid and of type instruction.

Source

pub fn explicit_clone(&self) -> Self

Creates a clone of this InstructionValue, and returns it. The clone will have no parent, and no name.

Source

pub fn get_name(&self) -> Option<&CStr>

Get name of the InstructionValue.

Source

pub fn get_instruction_with_name( &self, name: &str, ) -> Option<InstructionValue<'ctx>>

Get a instruction with it’s name Compares against all instructions after self, and self.

Source

pub fn set_name(&self, name: &str) -> Result<(), InstructionValueError>

Set name of the InstructionValue.

Source

pub fn get_type(self) -> AnyTypeEnum<'ctx>

Get type of the current InstructionValue

Source

pub fn get_opcode(self) -> InstructionOpcode

Source

pub fn get_previous_instruction(self) -> Option<Self>

Source

pub fn get_next_instruction(self) -> Option<Self>

Source

pub fn erase_from_basic_block(self)

Source

pub fn remove_from_basic_block(self)

Source

pub fn get_parent(self) -> Option<BasicBlock<'ctx>>

Source

pub fn is_terminator(self) -> bool

Returns if the instruction is a terminator

Source

pub fn is_conditional(self) -> bool

Returns if a terminator is conditional or not

Source

pub fn is_tail_call(self) -> bool

Source

pub fn replace_all_uses_with(self, other: &InstructionValue<'ctx>)

Source

pub fn get_volatile(self) -> Result<bool, InstructionValueError>

Returns whether or not a memory access instruction is volatile.

Source

pub fn set_volatile(self, volatile: bool) -> Result<(), InstructionValueError>

Sets whether or not a memory access instruction is volatile.

Source

pub fn get_allocated_type( self, ) -> Result<BasicTypeEnum<'ctx>, InstructionValueError>

Returns the type that is allocated by the alloca instruction.

Source

pub fn get_gep_source_element_type( self, ) -> Result<BasicTypeEnum<'ctx>, InstructionValueError>

Returns the source element type of the given GEP.

Source

pub fn get_alignment(self) -> Result<u32, InstructionValueError>

Returns alignment on a memory access instruction or alloca.

Source

pub fn set_alignment(self, alignment: u32) -> Result<(), InstructionValueError>

Sets alignment on a memory access instruction or alloca.

Source

pub fn get_atomic_ordering( self, ) -> Result<AtomicOrdering, InstructionValueError>

Returns atomic ordering on a memory access instruction.

Source

pub fn set_atomic_ordering( self, ordering: AtomicOrdering, ) -> Result<(), InstructionValueError>

Sets atomic ordering on a memory access instruction.

Source

pub fn get_num_operands(self) -> u32

Obtains the number of operands an InstructionValue has. An operand is a BasicValue used in an IR instruction.

The following example,

use inkwell::AddressSpace;
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("ivs");
let builder = context.create_builder();
let void_type = context.void_type();
let f32_type = context.f32_type();
#[cfg(feature = "typed-pointers")]
let f32_ptr_type = f32_type.ptr_type(AddressSpace::default());
#[cfg(not(feature = "typed-pointers"))]
let f32_ptr_type = context.ptr_type(AddressSpace::default());
let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false);

let function = module.add_function("take_f32_ptr", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);

let arg1 = function.get_first_param().unwrap().into_pointer_value();
let f32_val = f32_type.const_float(std::f64::consts::PI);
let store_instruction = builder.build_store(arg1, f32_val).unwrap();
let free_instruction = builder.build_free(arg1).unwrap();
let return_instruction = builder.build_return(None).unwrap();

assert_eq!(store_instruction.get_num_operands(), 2);
assert_eq!(free_instruction.get_num_operands(), 2);
assert_eq!(return_instruction.get_num_operands(), 0);

will generate LLVM IR roughly like (varying slightly across LLVM versions):

; ModuleID = 'ivs'
source_filename = "ivs"

define void @take_f32_ptr(float* %0) {
entry:
  store float 0x400921FB60000000, float* %0
  %1 = bitcast float* %0 to i8*
  tail call void @free(i8* %1)
  ret void
}

declare void @free(i8*)

which makes the number of instruction operands clear:

  1. Store has two: a const float and a variable float pointer %0
  2. Bitcast has one: a variable float pointer %0
  3. Function call has two: i8 pointer %1 argument, and the free function itself
  4. Void return has zero: void is not a value and does not count as an operand even though the return instruction can take values.
Source

pub fn get_operand(self, index: u32) -> Option<Operand<'ctx>>

Obtains the operand an InstructionValue has at a given index if any. An operand is a BasicValue used in an IR instruction.

The following example,

use inkwell::AddressSpace;
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("ivs");
let builder = context.create_builder();
let void_type = context.void_type();
let f32_type = context.f32_type();
#[cfg(feature = "typed-pointers")]
let f32_ptr_type = f32_type.ptr_type(AddressSpace::default());
#[cfg(not(feature = "typed-pointers"))]
let f32_ptr_type = context.ptr_type(AddressSpace::default());
let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false);

let function = module.add_function("take_f32_ptr", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);

let arg1 = function.get_first_param().unwrap().into_pointer_value();
let f32_val = f32_type.const_float(std::f64::consts::PI);
let store_instruction = builder.build_store(arg1, f32_val).unwrap();
let free_instruction = builder.build_free(arg1).unwrap();
let return_instruction = builder.build_return(None).unwrap();

assert!(store_instruction.get_operand(0).is_some());
assert!(store_instruction.get_operand(1).is_some());
assert!(store_instruction.get_operand(2).is_none());
assert!(free_instruction.get_operand(0).is_some());
assert!(free_instruction.get_operand(1).is_some());
assert!(free_instruction.get_operand(2).is_none());
assert!(return_instruction.get_operand(0).is_none());
assert!(return_instruction.get_operand(1).is_none());

will generate LLVM IR roughly like (varying slightly across LLVM versions):

; ModuleID = 'ivs'
source_filename = "ivs"

define void @take_f32_ptr(float* %0) {
entry:
  store float 0x400921FB60000000, float* %0
  %1 = bitcast float* %0 to i8*
  tail call void @free(i8* %1)
  ret void
}

declare void @free(i8*)

which makes the instruction operands clear:

  1. Store has two: a const float and a variable float pointer %0
  2. Bitcast has one: a variable float pointer %0
  3. Function call has two: i8 pointer %1 argument, and the free function itself
  4. Void return has zero: void is not a value and does not count as an operand even though the return instruction can take values.
Source

pub unsafe fn get_operand_unchecked(self, index: u32) -> Option<Operand<'ctx>>

Get the operand of an InstructionValue.

§Safety

The index must be less than InstructionValue::get_num_operands.

Source

pub fn get_operands(self) -> OperandIter<'ctx>

Get an instruction value operand iterator.

Source

pub fn set_operand<BV: BasicValue<'ctx>>(self, index: u32, val: BV) -> bool

Sets the operand an InstructionValue has at a given index if possible. An operand is a BasicValue used in an IR instruction.

use inkwell::AddressSpace;
use inkwell::context::Context;

let context = Context::create();
let module = context.create_module("ivs");
let builder = context.create_builder();
let void_type = context.void_type();
let f32_type = context.f32_type();
#[cfg(feature = "typed-pointers")]
let f32_ptr_type = f32_type.ptr_type(AddressSpace::default());
#[cfg(not(feature = "typed-pointers"))]
let f32_ptr_type = context.ptr_type(AddressSpace::default());
let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false);

let function = module.add_function("take_f32_ptr", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);

let arg1 = function.get_first_param().unwrap().into_pointer_value();
let f32_val = f32_type.const_float(std::f64::consts::PI);
let store_instruction = builder.build_store(arg1, f32_val).unwrap();
let free_instruction = builder.build_free(arg1).unwrap();
let return_instruction = builder.build_return(None).unwrap();

// This will produce invalid IR:
free_instruction.set_operand(0, f32_val);

assert_eq!(free_instruction.get_operand(0).unwrap().unwrap_value(), f32_val);
Source

pub fn get_operand_use(self, index: u32) -> Option<BasicValueUse<'ctx>>

Gets the use of an operand(BasicValue), if any.

use inkwell::AddressSpace;
use inkwell::context::Context;
use inkwell::values::BasicValue;

let context = Context::create();
let module = context.create_module("ivs");
let builder = context.create_builder();
let void_type = context.void_type();
let f32_type = context.f32_type();
#[cfg(feature = "typed-pointers")]
let f32_ptr_type = f32_type.ptr_type(AddressSpace::default());
#[cfg(not(feature = "typed-pointers"))]
let f32_ptr_type = context.ptr_type(AddressSpace::default());
let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false);

let function = module.add_function("take_f32_ptr", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);

let arg1 = function.get_first_param().unwrap().into_pointer_value();
let f32_val = f32_type.const_float(std::f64::consts::PI);
let store_instruction = builder.build_store(arg1, f32_val).unwrap();
let free_instruction = builder.build_free(arg1).unwrap();
let return_instruction = builder.build_return(None).unwrap();

assert_eq!(store_instruction.get_operand_use(1), arg1.get_first_use());
Source

pub unsafe fn get_operand_use_unchecked( self, index: u32, ) -> Option<BasicValueUse<'ctx>>

Gets the use of an operand(BasicValue), if any.

§Safety

The index must be smaller than InstructionValue::get_num_operands.

Source

pub fn get_operand_uses(self) -> OperandUseIter<'ctx>

Get an instruction value operand use iterator.

Source

pub fn get_num_indices(self) -> u32

Obtains the number of indices an InstructionValue has. An index is used in ExtractValue and InsertValue instructions to specify which field or element to access in an aggregate type (struct or array).

Returns 0 for instructions that are not ExtractValue or InsertValue.

The following example,

use inkwell::context::Context;
use inkwell::values::BasicValue;

let context = Context::create();
let module = context.create_module("ivs");
let builder = context.create_builder();
let void_type = context.void_type();
let i32_type = context.i32_type();
let struct_type = context.struct_type(&[i32_type.into(), i32_type.into()], false);
let fn_type = void_type.fn_type(&[], false);

let function = module.add_function("test", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);

let struct_val = struct_type.get_undef();
let extract_instruction = builder.build_extract_value(struct_val, 0, "extract").unwrap()
    .as_instruction_value().unwrap();

assert_eq!(extract_instruction.get_num_indices(), 1);
Source

pub fn get_indices(self) -> Vec<u32>

Obtains the indices an InstructionValue has as a vector. An index is used in ExtractValue and InsertValue instructions to specify which field or element to access in an aggregate type (struct or array).

Returns an empty vector for instructions that are not ExtractValue or InsertValue.

The following example,

use inkwell::context::Context;
use inkwell::values::BasicValue;

let context = Context::create();
let module = context.create_module("ivs");
let builder = context.create_builder();
let void_type = context.void_type();
let i32_type = context.i32_type();
let struct_type = context.struct_type(&[i32_type.into(), i32_type.into()], false);
let fn_type = void_type.fn_type(&[], false);

let function = module.add_function("test", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);

let struct_val = struct_type.get_undef();
let extract_instruction = builder.build_extract_value(struct_val, 0, "extract").unwrap()
    .as_instruction_value().unwrap();

assert_eq!(extract_instruction.get_indices(), vec![0]);
Source

pub fn get_first_use(self) -> Option<BasicValueUse<'ctx>>

Gets the first use of an InstructionValue if any.

The following example,

use inkwell::AddressSpace;
use inkwell::context::Context;
use inkwell::values::BasicValue;

let context = Context::create();
let module = context.create_module("ivs");
let builder = context.create_builder();
let void_type = context.void_type();
let f32_type = context.f32_type();
#[cfg(feature = "typed-pointers")]
let f32_ptr_type = f32_type.ptr_type(AddressSpace::default());
#[cfg(not(feature = "typed-pointers"))]
let f32_ptr_type = context.ptr_type(AddressSpace::default());
let fn_type = void_type.fn_type(&[f32_ptr_type.into()], false);

let function = module.add_function("take_f32_ptr", fn_type, None);
let basic_block = context.append_basic_block(function, "entry");

builder.position_at_end(basic_block);

let arg1 = function.get_first_param().unwrap().into_pointer_value();
let f32_val = f32_type.const_float(std::f64::consts::PI);
let store_instruction = builder.build_store(arg1, f32_val).unwrap();
let free_instruction = builder.build_free(arg1).unwrap();
let return_instruction = builder.build_return(None).unwrap();

assert!(arg1.get_first_use().is_some());
Source

pub fn get_icmp_predicate(self) -> Option<IntPredicate>

Gets the predicate of an ICmp InstructionValue. For instance, in the LLVM instruction %3 = icmp slt i32 %0, %1 this gives the slt.

If the instruction is not an ICmp, this returns None.

Source

pub fn get_fcmp_predicate(self) -> Option<FloatPredicate>

Gets the predicate of an FCmp InstructionValue. For instance, in the LLVM instruction %3 = fcmp olt float %0, %1 this gives the olt.

If the instruction is not an FCmp, this returns None.

Source

pub fn get_atomic_rmw_bin_op(self) -> Option<AtomicRMWBinOp>

Gets the binary operation of an AtomicRMW InstructionValue. For instance, in the LLVM instruction %3 = atomicrmw add i32* %ptr, i32 %val monotonic this gives the add.

If the instruction is not an AtomicRMW, this returns None.

Source

pub fn has_metadata(self) -> bool

Determines whether or not this Instruction has any associated metadata.

Source

pub fn get_metadata(self, kind_id: u32) -> Option<MetadataValue<'ctx>>

Gets the MetadataValue associated with this Instruction at a specific kind_id.

Source

pub fn set_metadata( self, metadata: MetadataValue<'ctx>, kind_id: u32, ) -> Result<(), InstructionValueError>

Determines whether or not this Instruction has any associated metadata kind_id.

Source

pub fn get_debug_location(self) -> Option<DILocation<'ctx>>

Get the debug location for this instruction.

Source

pub fn set_debug_location(self, location: Option<DILocation<'_>>)

Set the debug location for this instruction.

Trait Implementations§

Source§

impl<'ctx> AnyValue<'ctx> for InstructionValue<'ctx>

Source§

fn as_any_value_enum(&self) -> AnyValueEnum<'ctx>

Returns an enum containing a typed version of AnyValue.
Source§

fn print_to_string(&self) -> LLVMString

Prints a value to a LLVMString
Source§

fn is_poison(&self) -> bool

Returns whether the value is poison
Source§

impl AsValueRef for InstructionValue<'_>

Source§

impl<'ctx> Clone for InstructionValue<'ctx>

Source§

fn clone(&self) -> InstructionValue<'ctx>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'ctx> Debug for InstructionValue<'ctx>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for InstructionValue<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'ctx> From<InstructionValue<'ctx>> for AnyValueEnum<'ctx>

Source§

fn from(value: InstructionValue<'_>) -> AnyValueEnum<'_>

Converts to this type from the input type.
Source§

impl<'ctx> Hash for InstructionValue<'ctx>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'ctx> PartialEq<AnyValueEnum<'ctx>> for InstructionValue<'ctx>

Source§

fn eq(&self, other: &AnyValueEnum<'ctx>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'ctx> PartialEq<InstructionValue<'ctx>> for AnyValueEnum<'ctx>

Source§

fn eq(&self, other: &InstructionValue<'ctx>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'ctx> PartialEq for InstructionValue<'ctx>

Source§

fn eq(&self, other: &InstructionValue<'ctx>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'ctx> TryFrom<AnyValueEnum<'ctx>> for InstructionValue<'ctx>

Source§

type Error = ()

The type returned in the event of a conversion error.
Source§

fn try_from(value: AnyValueEnum<'ctx>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'ctx> TryFrom<InstructionValue<'ctx>> for CallSiteValue<'ctx>

Source§

type Error = ()

The type returned in the event of a conversion error.
Source§

fn try_from(value: InstructionValue<'ctx>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'ctx> TryFrom<InstructionValue<'ctx>> for FloatValue<'ctx>

Source§

type Error = ()

The type returned in the event of a conversion error.
Source§

fn try_from(value: InstructionValue<'_>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'ctx> TryFrom<InstructionValue<'ctx>> for IntValue<'ctx>

Source§

type Error = ()

The type returned in the event of a conversion error.
Source§

fn try_from(value: InstructionValue<'_>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'ctx> TryFrom<InstructionValue<'ctx>> for PhiValue<'ctx>

Source§

type Error = ()

The type returned in the event of a conversion error.
Source§

fn try_from(value: InstructionValue<'_>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'ctx> TryFrom<InstructionValue<'ctx>> for PointerValue<'ctx>

Source§

type Error = ()

The type returned in the event of a conversion error.
Source§

fn try_from(value: InstructionValue<'_>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<'ctx> Copy for InstructionValue<'ctx>

Source§

impl<'ctx> Eq for InstructionValue<'ctx>

Source§

impl<'ctx> StructuralPartialEq for InstructionValue<'ctx>

Auto Trait Implementations§

§

impl<'ctx> Freeze for InstructionValue<'ctx>

§

impl<'ctx> RefUnwindSafe for InstructionValue<'ctx>

§

impl<'ctx> !Send for InstructionValue<'ctx>

§

impl<'ctx> !Sync for InstructionValue<'ctx>

§

impl<'ctx> Unpin for InstructionValue<'ctx>

§

impl<'ctx> UnsafeUnpin for InstructionValue<'ctx>

§

impl<'ctx> UnwindSafe for InstructionValue<'ctx>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.