llir/values/instruction/
select.rs

1use llvm_sys::core::LLVMGetOperand;
2use llvm_sys::prelude::LLVMValueRef;
3use std::marker::PhantomData;
4
5use crate::values::*;
6use crate::*;
7
8/// [Select instruction](https://llvm.org/docs/LangRef.html#select-instruction)
9///
10/// The code `res = a < b ? a : b` will be turned into
11///
12/// ``` llvm
13/// %cmp = icmp slt %a %b
14/// %res = select %cmp %a %b
15/// ```
16#[derive(Copy, Clone, PartialEq, Eq, Hash)]
17pub struct SelectInstruction<'ctx>(LLVMValueRef, PhantomData<&'ctx ()>);
18
19impl_instr_debug!(SelectInstruction);
20
21impl_as_operand_for_instr!(SelectInstruction);
22
23impl_send_sync!(SelectInstruction);
24
25impl<'ctx> GetType<'ctx> for SelectInstruction<'ctx> {}
26
27impl<'ctx> GetDebugMetadata<'ctx> for SelectInstruction<'ctx> {}
28
29impl<'ctx> InstructionTrait<'ctx> for SelectInstruction<'ctx> {}
30
31impl<'ctx> InstructionDebugLoc for SelectInstruction<'ctx> {}
32
33impl<'ctx> AsInstruction<'ctx> for SelectInstruction<'ctx> {
34  fn as_instruction(&self) -> Instruction<'ctx> {
35    Instruction::Select(*self)
36  }
37}
38
39impl<'ctx> ValueOpcode for SelectInstruction<'ctx> {
40  fn opcode(&self) -> Opcode {
41    Opcode::Select
42  }
43}
44
45impl<'ctx> SelectInstruction<'ctx> {
46  /// The condition to check
47  pub fn condition(&self) -> Operand<'ctx> {
48    Operand::from_llvm(unsafe { LLVMGetOperand(self.0, 0) })
49  }
50
51  /// The value to select when the condition is true
52  pub fn true_value(&self) -> Operand<'ctx> {
53    Operand::from_llvm(unsafe { LLVMGetOperand(self.0, 1) })
54  }
55
56  /// The value to select when the condition is false
57  pub fn false_value(&self) -> Operand<'ctx> {
58    Operand::from_llvm(unsafe { LLVMGetOperand(self.0, 2) })
59  }
60}
61
62impl_positional_value_ref!(SelectInstruction, 0);
63
64impl_positional_from_llvm_value!(SelectInstruction);