@@ -864,6 +864,25 @@ public class IRBuilder {
864
864
return LLVMBuildCondBr ( llvm, condition. asLLVM ( ) , then. asLLVM ( ) , `else`. asLLVM ( ) )
865
865
}
866
866
867
+ /// Build an indirect branch to a label within the current function.
868
+ ///
869
+ /// - parameter address: The address of the label to branch to.
870
+ /// - parameter destinations: The set of possible destinations the address may
871
+ /// point to. The same block may appear multiple times in this list, though
872
+ /// this isn't particularly useful.
873
+ ///
874
+ /// - returns: An IRValue representing `void`.
875
+ @discardableResult
876
+ public func buildIndirectBr( address: BasicBlock . Address , destinations: [ BasicBlock ] ) -> IRValue {
877
+ guard let ret = LLVMBuildIndirectBr ( llvm, address. asLLVM ( ) , UInt32 ( destinations. count) ) else {
878
+ fatalError ( " Unable to build indirect branch to address \( address) " )
879
+ }
880
+ for dest in destinations {
881
+ LLVMAddDestination ( ret, dest. llvm)
882
+ }
883
+ return ret
884
+ }
885
+
867
886
/// Builds a return from the current function back to the calling function
868
887
/// with the given value.
869
888
///
@@ -891,6 +910,20 @@ public class IRBuilder {
891
910
return LLVMBuildUnreachable ( llvm)
892
911
}
893
912
913
+ /// Build a return from the current function back to the calling function with
914
+ /// the given array of values as members of an aggregate.
915
+ ///
916
+ /// - parameter values: The values to insert as members of the returned aggregate.
917
+ ///
918
+ /// - returns: A value representing `void`.
919
+ @discardableResult
920
+ public func buildRetAggregate( of values: [ IRValue ] ) -> IRValue {
921
+ var values = values. map { $0. asLLVM ( ) as Optional }
922
+ return values. withUnsafeMutableBufferPointer { buf in
923
+ return LLVMBuildAggregateRet ( llvm, buf. baseAddress!, UInt32 ( buf. count) )
924
+ }
925
+ }
926
+
894
927
/// Build a call to the given function with the given arguments to transfer
895
928
/// control to that function.
896
929
///
@@ -1088,6 +1121,15 @@ public class IRBuilder {
1088
1121
return LLVMBuildFPCast ( llvm, val. asLLVM ( ) , type. asLLVM ( ) , name)
1089
1122
}
1090
1123
1124
+ /// Builds an address space cast instruction that converts a pointer value
1125
+ /// to a given type in a different address space.
1126
+ ///
1127
+ /// The address spaces of the value and the destination pointer types must
1128
+ /// be distinct.
1129
+ public func buildAddrSpaceCast( _ val: IRValue , type: IRType , name: String = " " ) -> IRValue {
1130
+ return LLVMBuildAddrSpaceCast ( llvm, val. asLLVM ( ) , type. asLLVM ( ) , name)
1131
+ }
1132
+
1091
1133
/// Builds a truncate instruction to truncate the given value to the given
1092
1134
/// type with a shorter width.
1093
1135
///
@@ -1211,6 +1253,27 @@ public class IRBuilder {
1211
1253
return LLVMSizeOf ( val. asLLVM ( ) )
1212
1254
}
1213
1255
1256
+ /// Builds an expression that returns the difference between two pointer
1257
+ /// values, dividing out the size of the pointed-to objects.
1258
+ ///
1259
+ /// This is intended to implement C-style pointer subtraction. As such, the
1260
+ /// pointers must be appropriately aligned for their element types and
1261
+ /// pointing into the same object.
1262
+ ///
1263
+ /// - parameter lhs: The first pointer (the minuend).
1264
+ /// - parameter rhs: The second pointer (the subtrahend).
1265
+ /// - parameter name: The name for the newly inserted instruction.
1266
+ ///
1267
+ /// - returns: A IRValue representing a 64-bit integer value of the difference
1268
+ /// of the two pointer values modulo the size of the pointed-to objects.
1269
+ public func buildPointerDifference( _ lhs: IRValue , _ rhs: IRValue , name: String = " " ) -> IRValue {
1270
+ precondition (
1271
+ lhs. type is PointerType && rhs. type is PointerType ,
1272
+ " Cannot take pointer diff of \( lhs. type) and \( rhs. type) . "
1273
+ )
1274
+ return LLVMBuildPtrDiff ( llvm, lhs. asLLVM ( ) , rhs. asLLVM ( ) , name)
1275
+ }
1276
+
1214
1277
// MARK: Atomic Instructions
1215
1278
1216
1279
/// Builds a fence instruction that introduces "happens-before" edges between
@@ -1222,6 +1285,7 @@ public class IRBuilder {
1222
1285
/// with other atomics in the same thread. (This is useful for interacting
1223
1286
/// with signal handlers.) Otherwise this fence is atomic with respect to
1224
1287
/// all other code in the system.
1288
+ /// - parameter name: The name for the newly inserted instruction.
1225
1289
///
1226
1290
/// - returns: A value representing `void`.
1227
1291
public func buildFence( ordering: AtomicOrdering , singleThreaded: Bool = false , name: String = " " ) -> IRValue {
0 commit comments