Skip to content

Commit

Permalink
Added is_conditional to the InstructionValue
Browse files Browse the repository at this point in the history
  • Loading branch information
tfzee committed Dec 23, 2023
1 parent 2a0ff9f commit bf35c78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/values/instruction_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ impl<'ctx> InstructionValue<'ctx> {
unsafe { !LLVMIsATerminatorInst(self.as_value_ref()).is_null() }
}

// SubTypes: Only apply to terminators
/// Returns whether 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
6 changes: 6 additions & 0 deletions tests/all/test_instruction_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ 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(),
Expand All @@ -257,6 +260,9 @@ fn test_instructions() {
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

0 comments on commit bf35c78

Please sign in to comment.