@@ -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
0 commit comments