@@ -123,6 +123,56 @@ extension IRBuilder {
123123 public func buildCast( _ op: OpCode . Cast , value: IRValue , type: IRType , name: String = " " ) -> IRValue {
124124 return LLVMBuildCast ( llvm, op. llvm, value. asLLVM ( ) , type. asLLVM ( ) , name)
125125 }
126+
127+ /// Builds a cast operation from a value of pointer type to any other
128+ /// integral, pointer, or vector of integral/pointer type.
129+ ///
130+ /// There are a number of restrictions on the form of the input value and
131+ /// destination type. The source value of a pointer cast must be either a
132+ /// pointer or a vector of pointers. The destination type must either be
133+ /// an integer type, a pointer type, or a vector type with integral or pointer
134+ /// element type.
135+ ///
136+ /// If the destination type is an integral type or a vector of integral
137+ /// elements, this builds a `ptrtoint` instruction. Else, if the destination
138+ /// is a pointer type or vector of pointer type, and it has an address space
139+ /// that differs from the address space of the source value, an
140+ /// `addrspacecast` instruction is built. If none of these are true, a
141+ /// `bitcast` instruction is built.
142+ ///
143+ /// - Parameters:
144+ /// - value: The value to cast. The value must have pointer type.
145+ /// - type: The destination type to cast to. This must be a pointer type,
146+ /// integer type, or vector of the same.
147+ /// - name: The name for the newly inserted instruction.
148+ /// - Returns: A value representing the result of casting the given value to
149+ /// the given destination type using the appropriate pointer cast operation.
150+ public func buildPointerCast( of value: IRValue , to type: IRType , name: String = " " ) -> IRValue {
151+ precondition ( value. type is PointerType || value. type. scalarType is PointerType ,
152+ " cast value must be a pointer or vector of pointers " )
153+ precondition ( type. scalarType is IntType || type. scalarType is PointerType ,
154+ " destination type must be int, pointer, or vector of int/pointer " )
155+
156+ return LLVMBuildPointerCast ( llvm, value. asLLVM ( ) , type. asLLVM ( ) , name)
157+ }
158+
159+ /// Builds a cast operation from a value of integral type to given integral
160+ /// type by zero-extension, sign-extension, bitcast, or truncation
161+ /// as necessary.
162+ ///
163+ /// - Parameters:
164+ /// - value: The value to cast.
165+ /// - type: The destination integer type to cast to.
166+ /// - signed: If true, if an extension is required it will be a
167+ /// sign-extension. Else, all required extensions will be
168+ /// zero-extensions.
169+ /// - name: The name for the newly inserted instruction.
170+ /// - Returns: A value reprresenting the result of casting the given value to
171+ /// the given destination integer type using the appropriate
172+ /// integral cast operation.
173+ public func buildIntCast( of value: IRValue , to type: IntType , signed: Bool = true , name: String = " " ) -> IRValue {
174+ return LLVMBuildIntCast2 ( llvm, value. asLLVM ( ) , type. asLLVM ( ) , signed. llvm, name)
175+ }
126176}
127177
128178// MARK: Arithmetic Instructions
@@ -1392,6 +1442,100 @@ extension IRBuilder {
13921442 }
13931443}
13941444
1445+ // MARK: Memory Intrinsics
1446+
1447+ extension IRBuilder {
1448+ /// Builds a call to the `llvm.memset.*` family of intrinsics to fill a
1449+ /// given block of memory with a given byte value.
1450+ ///
1451+ /// - NOTE: Unlike the standard function `memset` defined in libc,
1452+ /// `llvm.memset` does not return a value and may be volatile. The address
1453+ /// space of the source and destination values need not match.
1454+ ///
1455+ /// - Parameters:
1456+ /// - dest: A pointer value to the destination that will be filled.
1457+ /// - value: A byte value to fill the destination with.
1458+ /// - length: The number of bytes to fill.
1459+ /// - alignment: The alignment of the destination pointer value.
1460+ /// - volatile: If true, builds a volatile `llvm.memset` intrinsic, else
1461+ /// builds a non-volatile `llvm.memset` instrinsic. The exact behavior of
1462+ /// volatile memory intrinsics is platform-dependent and should not be
1463+ /// relied upon to perform consistently. For more information, see the
1464+ /// language reference's section on [Volatile Memory
1465+ /// Access](http://llvm.org/docs/LangRef.html#volatile-memory-accesses).
1466+ public func buildMemset(
1467+ to dest: IRValue , of value: IRValue , length: IRValue ,
1468+ alignment: Alignment , volatile: Bool = false
1469+ ) {
1470+ let instruction = LLVMBuildMemSet ( self . llvm, dest. asLLVM ( ) , value. asLLVM ( ) , length. asLLVM ( ) , alignment. rawValue)
1471+ LLVMSetVolatile ( instruction, volatile. llvm)
1472+ }
1473+
1474+ /// Builds a call to the `llvm.memcpy.*` family of intrinsics to copy a block
1475+ /// of memory to a given destination memory location from a given source
1476+ /// memory location.
1477+ ///
1478+ /// - WARNING: It is illegal for the destination and source locations to
1479+ /// overlap each other.
1480+ ///
1481+ /// - NOTE: Unlike the standard function `memcpy` defined in libc,
1482+ /// `llvm.memcpy` does not return a value and may be volatile. The address
1483+ /// space of the source and destination values need not match.
1484+ ///
1485+ /// - Parameters:
1486+ /// - dest: A pointer to the destination that will be filled.
1487+ /// - destAlign: The alignment of the destination pointer value.
1488+ /// - src: A pointer to the source that will be copied from.
1489+ /// - srcAlign: The alignment of the source pointer value.
1490+ /// - length: The number of bytes to fill.
1491+ /// - volatile: If true, builds a volatile `llvm.memcpy` intrinsic, else
1492+ /// builds a non-volatile `llvm.memcpy` instrinsic. The exact behavior of
1493+ /// volatile memory intrinsics is platform-dependent and should not be
1494+ /// relied upon to perform consistently. For more information, see the
1495+ /// language reference's section on [Volatile Memory
1496+ /// Access](http://llvm.org/docs/LangRef.html#volatile-memory-accesses).
1497+ public func buildMemCpy(
1498+ to dest: IRValue , _ destAlign: Alignment ,
1499+ from src: IRValue , _ srcAlign: Alignment ,
1500+ length: IRValue , volatile: Bool = false
1501+ ) {
1502+ let instruction = LLVMBuildMemCpy ( self . llvm, dest. asLLVM ( ) , destAlign. rawValue, src. asLLVM ( ) , srcAlign. rawValue, length. asLLVM ( ) )
1503+ LLVMSetVolatile ( instruction, volatile. llvm)
1504+ }
1505+
1506+ /// Builds a call to the `llvm.memmove.*` family of intrinsics to move a
1507+ /// block of memory to a given destination memory location from a given source
1508+ /// memory location.
1509+ ///
1510+ /// Unlike `llvm.memcpy.*`, the destination and source memory locations may
1511+ /// overlap with each other.
1512+ ///
1513+ /// - NOTE: Unlike the standard function `memmove` defined in libc,
1514+ /// `llvm.memmove` does not return a value and may be volatile. The address
1515+ /// space of the source and destination values need not match.
1516+ ///
1517+ /// - Parameters:
1518+ /// - dest: A pointer to the destination that will be filled.
1519+ /// - destAlign: The alignment of the destination pointer value.
1520+ /// - src: A pointer to the source that will be copied from.
1521+ /// - srcAlign: The alignment of the source pointer value.
1522+ /// - length: The number of bytes to fill.
1523+ /// - volatile: If true, builds a volatile `llvm.memmove` intrinsic, else
1524+ /// builds a non-volatile `llvm.memmove` instrinsic. The exact behavior of
1525+ /// volatile memory intrinsics is platform-dependent and should not be
1526+ /// relied upon to perform consistently. For more information, see the
1527+ /// language reference's section on [Volatile Memory
1528+ /// Access](http://llvm.org/docs/LangRef.html#volatile-memory-accesses).
1529+ public func buildMemMove(
1530+ to dest: IRValue , _ destAlign: Alignment ,
1531+ from src: IRValue , _ srcAlign: Alignment ,
1532+ length: IRValue , volatile: Bool = false
1533+ ) {
1534+ let instruction = LLVMBuildMemMove ( self . llvm, dest. asLLVM ( ) , destAlign. rawValue, src. asLLVM ( ) , srcAlign. rawValue, length. asLLVM ( ) )
1535+ LLVMSetVolatile ( instruction, volatile. llvm)
1536+ }
1537+ }
1538+
13951539// MARK: Aggregate Instructions
13961540
13971541extension IRBuilder {
@@ -1422,7 +1566,7 @@ extension IRBuilder {
14221566 ///
14231567 /// - returns: A value representing an aggregate that has been updated with
14241568 /// the given value at the given index.
1425- func buildExtractValue( aggregate: IRValue , index: Int , name: String = " " ) -> IRValue {
1569+ public func buildExtractValue( aggregate: IRValue , index: Int , name: String = " " ) -> IRValue {
14261570 return LLVMBuildExtractValue ( llvm, aggregate. asLLVM ( ) , UInt32 ( index) , name)
14271571 }
14281572}
0 commit comments