@@ -746,7 +746,7 @@ public class IRBuilder {
746
746
}
747
747
}
748
748
749
- // MARK: Declaration Instructions
749
+ // MARK: Conditional Instructions
750
750
751
751
/// Build a phi node with the given type acting as the result of any incoming
752
752
/// basic blocks.
@@ -760,14 +760,26 @@ public class IRBuilder {
760
760
return PhiNode ( llvm: value)
761
761
}
762
762
763
- /// Build a named function body with the given type.
763
+ /// Build a select instruction to choose a value based on a condition without
764
+ /// IR-level branching.
764
765
///
765
- /// - parameter name: The name of the newly defined function.
766
- /// - parameter type: The type of the newly defined function.
766
+ /// - parameter cond: The condition to evaluate. It must have type `i1` or
767
+ /// be a vector of `i1`.
768
+ /// - parameter then: The value to select if the given condition is true.
769
+ /// - parameter else: The value to select if the given condition is false.
770
+ /// - parameter name: The name for the newly inserted instruction.
767
771
///
768
- /// - returns: A value representing the newly inserted function definition.
769
- public func addFunction( _ name: String , type: FunctionType ) -> Function {
770
- return Function ( llvm: LLVMAddFunction ( module. llvm, name, type. asLLVM ( ) ) )
772
+ /// - returns: A value representing the value selected for by the condition.
773
+ public func buildSelect( _ cond: IRValue , then: IRValue , else: IRValue , name: String = " " ) -> IRValue {
774
+ if let ty = cond. type as? IntType {
775
+ precondition ( ty. width == 1 , " Switch statement condition must have bitwidth of 1, instead has bitwidth of \( ty. width) " )
776
+ return LLVMBuildSelect ( llvm, cond. asLLVM ( ) , then. asLLVM ( ) , `else`. asLLVM ( ) , name)
777
+ } else if let ty = cond. type as? VectorType , let intTy = ty. elementType as? IntType {
778
+ precondition ( intTy. width == 1 , " Switch statement condition must have bitwidth of 1, instead has bitwidth of \( intTy. width) " )
779
+ return LLVMBuildSelect ( llvm, cond. asLLVM ( ) , then. asLLVM ( ) , `else`. asLLVM ( ) , name)
780
+ } else {
781
+ fatalError ( " Condition must have type i1 or <i1 x N> " )
782
+ }
771
783
}
772
784
773
785
/// Build a branch table that branches on the given value with the given
@@ -790,6 +802,18 @@ public class IRBuilder {
790
802
UInt32 ( caseCount) ) !)
791
803
}
792
804
805
+ // MARK: Declaration Instructions
806
+
807
+ /// Build a named function body with the given type.
808
+ ///
809
+ /// - parameter name: The name of the newly defined function.
810
+ /// - parameter type: The type of the newly defined function.
811
+ ///
812
+ /// - returns: A value representing the newly inserted function definition.
813
+ public func addFunction( _ name: String , type: FunctionType ) -> Function {
814
+ return Function ( llvm: LLVMAddFunction ( module. llvm, name, type. asLLVM ( ) ) )
815
+ }
816
+
793
817
/// Build a named structure definition.
794
818
///
795
819
/// - parameter name: The name of the structure.
0 commit comments