Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding get_allocated_type, is_terminator and is_conditional to InstructionValue #455

Merged
merged 7 commits into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/values/instruction_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use either::{
Either::{Left, Right},
};
use llvm_sys::core::{
LLVMGetAlignment, LLVMGetFCmpPredicate, LLVMGetICmpPredicate, LLVMGetInstructionOpcode, LLVMGetInstructionParent,
LLVMGetMetadata, LLVMGetNextInstruction, LLVMGetNumOperands, LLVMGetOperand, LLVMGetOperandUse,
LLVMGetPreviousInstruction, LLVMGetVolatile, LLVMHasMetadata, LLVMInstructionClone, LLVMInstructionEraseFromParent,
LLVMInstructionRemoveFromParent, LLVMIsAAllocaInst, LLVMIsABasicBlock, LLVMIsALoadInst, LLVMIsAStoreInst,
LLVMIsTailCall, LLVMSetAlignment, LLVMSetMetadata, LLVMSetOperand, LLVMSetVolatile, LLVMValueAsBasicBlock,
LLVMGetAlignment, LLVMGetAllocatedType, LLVMGetFCmpPredicate, LLVMGetICmpPredicate, LLVMGetInstructionOpcode,
LLVMGetInstructionParent, LLVMGetMetadata, LLVMGetNextInstruction, LLVMGetNumOperands, LLVMGetOperand,
LLVMGetOperandUse, LLVMGetPreviousInstruction, LLVMGetVolatile, LLVMHasMetadata, LLVMInstructionClone,
LLVMInstructionEraseFromParent, LLVMInstructionRemoveFromParent, LLVMIsAAllocaInst, LLVMIsABasicBlock,
LLVMIsALoadInst, LLVMIsAStoreInst, LLVMIsATerminatorInst, LLVMIsConditional, LLVMIsTailCall, LLVMSetAlignment,
LLVMSetMetadata, LLVMSetOperand, LLVMSetVolatile, LLVMValueAsBasicBlock,
};
use llvm_sys::core::{LLVMGetOrdering, LLVMSetOrdering};
#[llvm_versions(10.0..=latest)]
Expand All @@ -17,9 +18,9 @@ use llvm_sys::LLVMOpcode;

use std::{ffi::CStr, fmt, fmt::Display};

use crate::values::traits::AsValueRef;
use crate::values::{BasicValue, BasicValueEnum, BasicValueUse, MetadataValue, Value};
use crate::{basic_block::BasicBlock, types::AnyTypeEnum};
use crate::{types::BasicTypeEnum, values::traits::AsValueRef};
use crate::{AtomicOrdering, FloatPredicate, IntPredicate};

use super::AnyValue;
Expand Down Expand Up @@ -222,6 +223,21 @@ impl<'ctx> InstructionValue<'ctx> {
unsafe { BasicBlock::new(LLVMGetInstructionParent(self.as_value_ref())) }
}

/// Returns if the instruction is a terminator
pub fn is_terminator(self) -> bool {
unsafe { !LLVMIsATerminatorInst(self.as_value_ref()).is_null() }
}

// SubTypes: Only apply to terminators
/// Returns if a terminator is conditional or not
pub fn is_conditional(self) -> bool {
if self.is_terminator() {
unsafe { LLVMIsConditional(self.as_value_ref()) == 1 }
} else {
false
}
}

pub fn is_tail_call(self) -> bool {
// LLVMIsTailCall has UB if the value is not an llvm::CallInst*.
if self.get_opcode() == InstructionOpcode::Call {
Expand Down Expand Up @@ -282,6 +298,15 @@ impl<'ctx> InstructionValue<'ctx> {
Ok(())
}

// SubTypes: Only apply to alloca instruction
/// Returns the type that is allocated by the alloca instruction.
pub fn get_allocated_type(self) -> Result<BasicTypeEnum<'ctx>, &'static str> {
if !self.is_a_alloca_inst() {
return Err("Value is not an alloca.");
}
Ok(unsafe { BasicTypeEnum::new(LLVMGetAllocatedType(self.as_value_ref())) })
}

// SubTypes: Only apply to memory access and alloca instructions
/// Returns alignment on a memory access instruction or alloca.
pub fn get_alignment(self) -> Result<u32, &'static str> {
Expand Down
16 changes: 15 additions & 1 deletion tests/all/test_instruction_values.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use inkwell::context::Context;
use inkwell::types::AnyTypeEnum;
use inkwell::types::{AnyTypeEnum, AsTypeRef, BasicType};
use inkwell::values::{BasicValue, InstructionOpcode::*};
use inkwell::{AddressSpace, AtomicOrdering, AtomicRMWBinOp, FloatPredicate, IntPredicate};

Expand Down Expand Up @@ -239,6 +239,7 @@ fn test_instructions() {
let f32_val = f32_type.const_float(::std::f64::consts::PI);

let store_instruction = builder.build_store(arg1, f32_val).unwrap();
let alloca_val = builder.build_alloca(i64_type, "alloca").unwrap();
let ptr_val = builder.build_ptr_to_int(arg1, i64_type, "ptr_val").unwrap();
let ptr = builder.build_int_to_ptr(ptr_val, f32_ptr_type, "ptr").unwrap();
let icmp = builder.build_int_compare(IntPredicate::EQ, arg1, arg1, "icmp").unwrap();
Expand All @@ -248,7 +249,20 @@ fn test_instructions() {
.unwrap();
let free_instruction = builder.build_free(arg1).unwrap();
let return_instruction = builder.build_return(None).unwrap();
let cond_br_instruction = builder
.build_conditional_branch(i64_type.const_zero(), basic_block, basic_block)
.unwrap();

assert_eq!(
alloca_val.as_instruction().unwrap().get_allocated_type(),
Ok(i64_type.as_basic_type_enum())
);
assert!(store_instruction.get_allocated_type().is_err());
assert!(!store_instruction.is_terminator());
assert!(return_instruction.is_terminator());
assert!(!store_instruction.is_conditional());
assert!(!return_instruction.is_conditional());
assert!(cond_br_instruction.is_conditional());
assert_eq!(store_instruction.get_opcode(), Store);
assert_eq!(ptr_val.as_instruction().unwrap().get_opcode(), PtrToInt);
assert_eq!(ptr.as_instruction().unwrap().get_opcode(), IntToPtr);
Expand Down