diff --git a/include/llvm/IR/InstrTypes.h b/include/llvm/IR/InstrTypes.h index f50c28a5fc2..e12bb03a403 100644 --- a/include/llvm/IR/InstrTypes.h +++ b/include/llvm/IR/InstrTypes.h @@ -531,6 +531,12 @@ public: Type *DestTy ///< The Type to which the value should be cast. ); + /// @brief Check whether a bitcast between these types is valid + static bool isBitCastable( + Type *SrcTy, ///< The Type from which the value should be cast. + Type *DestTy ///< The Type to which the value should be cast. + ); + /// Returns the opcode necessary to cast Val into Ty using usual casting /// rules. /// @brief Infer the opcode for cast operand and type @@ -698,7 +704,7 @@ public: /// @brief Create a CmpInst static CmpInst *Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const Twine &Name, BasicBlock *InsertAtEnd); - + /// @brief Get the opcode casted to the right type OtherOps getOpcode() const { return static_cast(Instruction::getOpcode()); @@ -715,15 +721,15 @@ public: static bool isFPPredicate(Predicate P) { return P >= FIRST_FCMP_PREDICATE && P <= LAST_FCMP_PREDICATE; } - + static bool isIntPredicate(Predicate P) { return P >= FIRST_ICMP_PREDICATE && P <= LAST_ICMP_PREDICATE; } - + bool isFPPredicate() const { return isFPPredicate(getPredicate()); } bool isIntPredicate() const { return isIntPredicate(getPredicate()); } - - + + /// For example, EQ -> NE, UGT -> ULE, SLT -> SGE, /// OEQ -> UNE, UGT -> OLE, OLT -> UGE, etc. /// @returns the inverse predicate for the instruction's current predicate. @@ -821,7 +827,7 @@ public: static inline bool classof(const Value *V) { return isa(V) && classof(cast(V)); } - + /// @brief Create a result type for fcmp/icmp static Type* makeCmpResultType(Type* opnd_type) { if (VectorType* vt = dyn_cast(opnd_type)) { diff --git a/lib/IR/Instructions.cpp b/lib/IR/Instructions.cpp index 5878f77dc17..665fe66ccc3 100644 --- a/lib/IR/Instructions.cpp +++ b/lib/IR/Instructions.cpp @@ -2517,8 +2517,48 @@ bool CastInst::isCastable(Type *SrcTy, Type *DestTy) { } } -// Provide a way to get a "cast" where the cast opcode is inferred from the -// types and size of the operand. This, basically, is a parallel of the +bool CastInst::isBitCastable(Type *SrcTy, Type *DestTy) { + if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) + return false; + + if (SrcTy == DestTy) + return true; + + if (VectorType *SrcVecTy = dyn_cast(SrcTy)) { + if (VectorType *DestVecTy = dyn_cast(DestTy)) { + if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { + // An element by element cast. Valid if casting the elements is valid. + SrcTy = SrcVecTy->getElementType(); + DestTy = DestVecTy->getElementType(); + } + } + } + + if (PointerType *DestPtrTy = dyn_cast(DestTy)) { + if (PointerType *SrcPtrTy = dyn_cast(SrcTy)) { + return SrcPtrTy->getAddressSpace() == DestPtrTy->getAddressSpace(); + } + } + + unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr + unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr + + // Could still have vectors of pointers if the number of elements doesn't + // match + if (SrcBits == 0 || DestBits == 0) + return false; + + if (SrcBits != DestBits) + return false; + + if (DestTy->isX86_MMXTy() || SrcTy->isX86_MMXTy()) + return false; + + return true; +} + +// Provide a way to get a "cast" where the cast opcode is inferred from the +// types and size of the operand. This, basically, is a parallel of the // logic in the castIsValid function below. This axiom should hold: // castIsValid( getCastOpcode(Val, Ty), Val, Ty) // should not assert in castIsValid. In other words, this produces a "correct" @@ -2535,6 +2575,7 @@ CastInst::getCastOpcode( if (SrcTy == DestTy) return BitCast; + // FIXME: Check address space sizes here if (VectorType *SrcVecTy = dyn_cast(SrcTy)) if (VectorType *DestVecTy = dyn_cast(DestTy)) if (SrcVecTy->getNumElements() == DestVecTy->getNumElements()) { @@ -2601,6 +2642,7 @@ CastInst::getCastOpcode( return BitCast; } else if (DestTy->isPointerTy()) { if (SrcTy->isPointerTy()) { + // TODO: Address space pointer sizes may not match return BitCast; // ptr -> ptr } else if (SrcTy->isIntegerTy()) { return IntToPtr; // int -> ptr diff --git a/lib/Transforms/InstCombine/InstCombineCalls.cpp b/lib/Transforms/InstCombine/InstCombineCalls.cpp index 815db335ea4..9f74fd6b8d0 100644 --- a/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1010,7 +1010,8 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { if (!Caller->use_empty() && // void -> non-void is handled specially - !NewRetTy->isVoidTy() && !CastInst::isCastable(NewRetTy, OldRetTy)) + !NewRetTy->isVoidTy() && + !CastInst::isBitCastable(NewRetTy, OldRetTy)) return false; // Cannot transform this return value. if (!CallerPAL.isEmpty() && !Caller->use_empty()) { @@ -1044,8 +1045,9 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { Type *ParamTy = FT->getParamType(i); Type *ActTy = (*AI)->getType(); - if (!CastInst::isCastable(ActTy, ParamTy)) + if (!CastInst::isBitCastable(ActTy, ParamTy)) { return false; // Cannot transform this parameter value. + } if (AttrBuilder(CallerPAL.getParamAttributes(i + 1), i + 1). hasAttributes(AttributeFuncs:: @@ -1074,7 +1076,8 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { ParamTy == TD->getIntPtrType(Caller->getContext())) && (ActTy->isPointerTy() || ActTy == TD->getIntPtrType(Caller->getContext())))); - if (Callee->isDeclaration() && !isConvertible) return false; + if (Callee->isDeclaration() && !isConvertible) + return false; } if (Callee->isDeclaration()) { @@ -1141,12 +1144,11 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { AI = CS.arg_begin(); for (unsigned i = 0; i != NumCommonArgs; ++i, ++AI) { Type *ParamTy = FT->getParamType(i); + if ((*AI)->getType() == ParamTy) { Args.push_back(*AI); } else { - Instruction::CastOps opcode = CastInst::getCastOpcode(*AI, - false, ParamTy, false); - Args.push_back(Builder->CreateCast(opcode, *AI, ParamTy)); + Args.push_back(Builder->CreateBitCast(*AI, ParamTy)); } // Add any parameter attributes. @@ -1217,9 +1219,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { Value *NV = NC; if (OldRetTy != NV->getType() && !Caller->use_empty()) { if (!NV->getType()->isVoidTy()) { - Instruction::CastOps opcode = - CastInst::getCastOpcode(NC, false, OldRetTy, false); - NV = NC = CastInst::Create(opcode, NC, OldRetTy); + NV = NC = CastInst::Create(CastInst::BitCast, NC, OldRetTy); NC->setDebugLoc(Caller->getDebugLoc()); // If this is an invoke instruction, we should insert it after the first diff --git a/test/Transforms/InstCombine/2008-01-06-BitCastAttributes.ll b/test/Transforms/InstCombine/2008-01-06-BitCastAttributes.ll index 6588a918bae..22c078250ec 100644 --- a/test/Transforms/InstCombine/2008-01-06-BitCastAttributes.ll +++ b/test/Transforms/InstCombine/2008-01-06-BitCastAttributes.ll @@ -4,22 +4,27 @@ ; CHECK-NOT: bitcast define void @a() { - ret void + ret void } define signext i32 @b(i32* inreg %x) { - ret i32 0 + ret i32 0 } define void @c(...) { - ret void + ret void } define void @g(i32* %y) { - call void bitcast (void ()* @a to void (i32*)*)( i32* noalias %y ) - call <2 x i32> bitcast (i32 (i32*)* @b to <2 x i32> (i32*)*)( i32* inreg null ) ; <<2 x i32>>:1 [#uses=0] +; CHECK-LABEL: @g( +; CHECK: call i64 bitcast (i32 (i32*)* @b to i64 (i32)*)(i32 0) %x = call i64 bitcast (i32 (i32*)* @b to i64 (i32)*)( i32 0 ) ; [#uses=0] - call void bitcast (void (...)* @c to void (i32)*)( i32 0 ) - call void bitcast (void (...)* @c to void (i32)*)( i32 zeroext 0 ) - ret void + +; The rest should not have bitcasts remaining +; CHECK-NOT: bitcast + call void bitcast (void ()* @a to void (i32*)*)( i32* noalias %y ) + call <2 x i32> bitcast (i32 (i32*)* @b to <2 x i32> (i32*)*)( i32* inreg null ) ; <<2 x i32>>:1 [#uses=0] + call void bitcast (void (...)* @c to void (i32)*)( i32 0 ) + call void bitcast (void (...)* @c to void (i32)*)( i32 zeroext 0 ) + ret void } diff --git a/test/Transforms/InstCombine/2008-01-06-VoidCast.ll b/test/Transforms/InstCombine/2008-01-06-VoidCast.ll index ebd5c4229d4..5dcaa38edc0 100644 --- a/test/Transforms/InstCombine/2008-01-06-VoidCast.ll +++ b/test/Transforms/InstCombine/2008-01-06-VoidCast.ll @@ -1,10 +1,12 @@ ; RUN: opt < %s -instcombine -S | FileCheck %s define void @f(i16 %y) { - ret void + ret void } -; CHECK-NOT: bitcast + define i32 @g(i32 %y) { - %x = call i32 bitcast (void (i16)* @f to i32 (i32)*)( i32 %y ) ; [#uses=1] - ret i32 %x +; CHECK-LABEL: @g( +; CHECK: call i32 bitcast + %x = call i32 bitcast (void (i16)* @f to i32 (i32)*)( i32 %y ) ; [#uses=1] + ret i32 %x } diff --git a/test/Transforms/InstCombine/apint-call-cast-target.ll b/test/Transforms/InstCombine/apint-call-cast-target.ll index 7d7c8fa503f..4e98f9b2b3a 100644 --- a/test/Transforms/InstCombine/apint-call-cast-target.ll +++ b/test/Transforms/InstCombine/apint-call-cast-target.ll @@ -3,16 +3,17 @@ target datalayout = "e-p:32:32" target triple = "i686-pc-linux-gnu" -; CHECK-NOT: bitcast -; CHECK: call -; CHECK-NOT: bitcast define i32 @main() { +; CHECK-LABEL: @main( +; CHECK: call i32 bitcast entry: %tmp = call i32 bitcast (i7* (i999*)* @ctime to i32 (i99*)*)( i99* null ) ret i32 %tmp } define i7* @ctime(i999*) { +; CHECK-LABEL: @ctime( +; CHECK: call i7* bitcast entry: %tmp = call i7* bitcast (i32 ()* @main to i7* ()*)( ) ret i7* %tmp diff --git a/test/Transforms/InstCombine/bitcast-alias-function.ll b/test/Transforms/InstCombine/bitcast-alias-function.ll new file mode 100644 index 00000000000..a6b56f94ffb --- /dev/null +++ b/test/Transforms/InstCombine/bitcast-alias-function.ll @@ -0,0 +1,229 @@ +; RUN: opt -S -instcombine -o - %s | FileCheck %s +target datalayout = "e-p:32:32:32-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v64:64:64-v128:128:128-a0:0:64" + + + +; Cases that should be bitcast + +; Test cast between scalars with same bit sizes +@alias_i32_to_f32 = alias bitcast (i32 (i32)* @func_i32 to float (float)*) + +; Test cast between vectors with same number of elements and bit sizes +@alias_v2i32_to_v2f32 = alias bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <2 x float> (<2 x float>)*) + +; Test cast from vector to scalar with same number of bits +@alias_v2f32_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <2 x float> (<2 x float>)*) + +; Test cast from scalar to vector with same number of bits +@alias_i64_to_v2f32 = alias bitcast (<2 x float> (<2 x float>)* @func_v2f32 to i64 (i64)*) + +; Test cast between vectors of pointers +@alias_v2i32p_to_v2i64p = alias bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to <2 x i64*> (<2 x i64*>)*) + + +; Cases that should be invalid and unchanged + +; Test cast between scalars with different bit sizes +@alias_i64_to_f32 = alias bitcast (i64 (i64)* @func_i64 to float (float)*) + +; Test cast between vectors with different bit sizes but the +; same number of elements +@alias_v2i64_to_v2f32 = alias bitcast (<2 x i64> (<2 x i64>)* @func_v2i64 to <2 x float> (<2 x float>)*) + +; Test cast between vectors with same number of bits and different +; numbers of elements +@alias_v2i32_to_v4f32 = alias bitcast (<2 x i32> (<2 x i32>)* @func_v2i32 to <4 x float> (<4 x float>)*) + +; Test cast between scalar and vector with different number of bits +@alias_i64_to_v4f32 = alias bitcast (<4 x float> (<4 x float>)* @func_v4f32 to i64 (i64)*) + +; Test cast between vector and scalar with different number of bits +@alias_v4f32_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <4 x float> (<4 x float>)*) + +; Test cast from scalar to vector of pointers with same number of bits +; We don't know the pointer size at this point, so this can't be done +@alias_i64_to_v2i32p = alias bitcast (<2 x i32*> (<2 x i32*>)* @func_v2i32p to i64 (i64)*) + +; Test cast between vector of pointers and scalar with different number of bits +@alias_v4i32p_to_i64 = alias bitcast (i64 (i64)* @func_i64 to <4 x i32*> (<4 x i32*>)*) + + + +define internal <2 x i32> @func_v2i32(<2 x i32> %v) noinline nounwind { +entry: + ret <2 x i32> %v +} + +define internal <2 x float> @func_v2f32(<2 x float> %v) noinline nounwind { +entry: + ret <2 x float> %v +} + +define internal <4 x float> @func_v4f32(<4 x float> %v) noinline nounwind { +entry: + ret <4 x float> %v +} + +define internal i32 @func_i32(i32 %v) noinline nounwind { +entry: + ret i32 %v +} + +define internal i64 @func_i64(i64 %v) noinline nounwind { +entry: + ret i64 %v +} + +define internal <2 x i64> @func_v2i64(<2 x i64> %v) noinline nounwind { +entry: + ret <2 x i64> %v +} + +define internal <2 x i32*> @func_v2i32p(<2 x i32*> %v) noinline nounwind { +entry: + ret <2 x i32*> %v +} + +; Valid cases, only bitcast for argument / return type and call underlying function + +; Sizes match, should only bitcast +define void @bitcast_alias_scalar(float* noalias %source, float* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_scalar +; CHECK: bitcast float %tmp to i32 +; CHECK-NOT: fptoui +; CHECK-NOT: uitofp +; CHECK: bitcast i32 %call to float + %tmp = load float* %source, align 8 + %call = call float @alias_i32_to_f32(float %tmp) nounwind + store float %call, float* %dest, align 8 + ret void +} + +; Sizes match, should only bitcast +define void @bitcast_alias_vector(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_vector +; CHECK: bitcast <2 x float> %tmp to <2 x i32> +; CHECK-NOT: fptoui +; CHECK-NOT: uitofp +; CHECK: bitcast <2 x i32> %call to <2 x float> + %tmp = load <2 x float>* %source, align 8 + %call = call <2 x float> @alias_v2i32_to_v2f32(<2 x float> %tmp) nounwind + store <2 x float> %call, <2 x float>* %dest, align 8 + ret void +} + +; Sizes match, should only bitcast +define void @bitcast_alias_vector_scalar_same_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_vector_scalar_same_size +; CHECK: bitcast <2 x float> %tmp to i64 +; CHECK: %call = call i64 @func_i64 +; CHECK: bitcast i64 %call to <2 x float> + %tmp = load <2 x float>* %source, align 8 + %call = call <2 x float> @alias_v2f32_to_i64(<2 x float> %tmp) nounwind + store <2 x float> %call, <2 x float>* %dest, align 8 + ret void +} + +define void @bitcast_alias_scalar_vector_same_size(i64* noalias %source, i64* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_scalar_vector_same_size +; CHECK: bitcast i64 %tmp to <2 x float> +; CHECK: call <2 x float> @func_v2f32 +; CHECK: bitcast <2 x float> %call to i64 + %tmp = load i64* %source, align 8 + %call = call i64 @alias_i64_to_v2f32(i64 %tmp) nounwind + store i64 %call, i64* %dest, align 8 + ret void +} + +define void @bitcast_alias_vector_ptrs_same_size(<2 x i64*>* noalias %source, <2 x i64*>* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_vector_ptrs_same_size +; CHECK: bitcast <2 x i64*> %tmp to <2 x i32*> +; CHECK: call <2 x i32*> @func_v2i32p +; CHECK: bitcast <2 x i32*> %call to <2 x i64*> + %tmp = load <2 x i64*>* %source, align 8 + %call = call <2 x i64*> @alias_v2i32p_to_v2i64p(<2 x i64*> %tmp) nounwind + store <2 x i64*> %call, <2 x i64*>* %dest, align 8 + ret void +} + +; Invalid cases: + +define void @bitcast_alias_mismatch_scalar_size(float* noalias %source, float* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_mismatch_scalar_size +; CHECK-NOT: fptoui +; CHECK: @alias_i64_to_f32 +; CHECK-NOT: uitofp + %tmp = load float* %source, align 8 + %call = call float @alias_i64_to_f32(float %tmp) nounwind + store float %call, float* %dest, align 8 + ret void +} + +define void @bitcast_alias_mismatch_vector_element_and_bit_size(<2 x float>* noalias %source, <2 x float>* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_mismatch_vector_element_and_bit_size +; CHECK-NOT: fptoui <2 x float> %tmp to <2 x i64> +; CHECK: @alias_v2i64_to_v2f32 +; CHECK-NOT: uitofp <2 x i64> %call to <2 x float> + %tmp = load <2 x float>* %source, align 8 + %call = call <2 x float> @alias_v2i64_to_v2f32(<2 x float> %tmp) nounwind + store <2 x float> %call, <2 x float>* %dest, align 8 + ret void +} + +define void @bitcast_alias_vector_mismatched_number_elements(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_vector_mismatched_number_elements +; CHECK: %call = call <4 x float> @alias_v2i32_to_v4f32 + %tmp = load <4 x float>* %source, align 8 + %call = call <4 x float> @alias_v2i32_to_v4f32(<4 x float> %tmp) nounwind + store <4 x float> %call, <4 x float>* %dest, align 8 + ret void +} + +define void @bitcast_alias_vector_scalar_mismatched_bit_size(<4 x float>* noalias %source, <4 x float>* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_vector_scalar_mismatched_bit_size +; CHECK: %call = call <4 x float> @alias_v4f32_to_i64 + %tmp = load <4 x float>* %source, align 8 + %call = call <4 x float> @alias_v4f32_to_i64(<4 x float> %tmp) nounwind + store <4 x float> %call, <4 x float>* %dest, align 8 + ret void +} + +define void @bitcast_alias_vector_ptrs_scalar_mismatched_bit_size(<4 x i32*>* noalias %source, <4 x i32*>* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_vector_ptrs_scalar_mismatched_bit_size +; CHECK: @alias_v4i32p_to_i64 + %tmp = load <4 x i32*>* %source, align 8 + %call = call <4 x i32*> @alias_v4i32p_to_i64(<4 x i32*> %tmp) nounwind + store <4 x i32*> %call, <4 x i32*>* %dest, align 8 + ret void +} + +define void @bitcast_alias_scalar_vector_ptrs_same_size(i64* noalias %source, i64* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_scalar_vector_ptrs_same_size +; CHECK: @alias_i64_to_v2i32p + %tmp = load i64* %source, align 8 + %call = call i64 @alias_i64_to_v2i32p(i64 %tmp) nounwind + store i64 %call, i64* %dest, align 8 + ret void +} + +define void @bitcast_alias_scalar_vector_mismatched_bit_size(i64* noalias %source, i64* noalias %dest) nounwind { +entry: +; CHECK-LABEL: @bitcast_alias_scalar_vector_mismatched_bit_size +; CHECK: call i64 @alias_i64_to_v4f32 + %tmp = load i64* %source, align 8 + %call = call i64 @alias_i64_to_v4f32(i64 %tmp) nounwind + store i64 %call, i64* %dest, align 8 + ret void +} + diff --git a/test/Transforms/InstCombine/call-cast-target.ll b/test/Transforms/InstCombine/call-cast-target.ll index 19074212d3e..315c51683fd 100644 --- a/test/Transforms/InstCombine/call-cast-target.ll +++ b/test/Transforms/InstCombine/call-cast-target.ll @@ -3,14 +3,12 @@ target datalayout = "e-p:32:32" target triple = "i686-pc-linux-gnu" -; CHECK-NOT: bitcast -; CHECK: call -; CHECK-NOT: bitcast - define i32 @main() { +; CHECK-LABEL: @main +; CHECK: call i32 bitcast entry: - %tmp = call i32 bitcast (i8* (i32*)* @ctime to i32 (i32*)*)( i32* null ) ; [#uses=1] - ret i32 %tmp + %tmp = call i32 bitcast (i8* (i32*)* @ctime to i32 (i32*)*)( i32* null ) ; [#uses=1] + ret i32 %tmp } declare i8* @ctime(i32*) diff --git a/test/Transforms/InstCombine/call.ll b/test/Transforms/InstCombine/call.ll index 974a871820a..55833fbd1c6 100644 --- a/test/Transforms/InstCombine/call.ll +++ b/test/Transforms/InstCombine/call.ll @@ -7,92 +7,94 @@ target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:1 declare void @test1a(i8*) define void @test1(i32* %A) { - call void bitcast (void (i8*)* @test1a to void (i32*)*)( i32* %A ) - ret void +; CHECK-LABEL: @test1( ; CHECK: %1 = bitcast i32* %A to i8* ; CHECK: call void @test1a(i8* %1) ; CHECK: ret void + call void bitcast (void (i8*)* @test1a to void (i32*)*)( i32* %A ) + ret void } -; More complex case, translate argument because of resolution. This is safe +; More complex case, translate argument because of resolution. This is safe ; because we have the body of the function define void @test2a(i8 %A) { - ret void +; CHECK-LABEL: @test2a( ; CHECK: ret void + ret void } define i32 @test2(i32 %A) { - call void bitcast (void (i8)* @test2a to void (i32)*)( i32 %A ) - ret i32 %A -; CHECK: %1 = trunc i32 %A to i8 -; CHECK: call void @test2a(i8 %1) +; CHECK-LABEL: @test2( +; CHECK: call void bitcast ; CHECK: ret i32 %A + call void bitcast (void (i8)* @test2a to void (i32)*)( i32 %A ) + ret i32 %A } -; Resolving this should insert a cast from sbyte to int, following the C +; Resolving this should insert a cast from sbyte to int, following the C ; promotion rules. define void @test3a(i8, ...) {unreachable } define void @test3(i8 %A, i8 %B) { - call void bitcast (void (i8, ...)* @test3a to void (i8, i8)*)( i8 %A, i8 %B -) - ret void +; CHECK-LABEL: @test3( ; CHECK: %1 = zext i8 %B to i32 ; CHECK: call void (i8, ...)* @test3a(i8 %A, i32 %1) ; CHECK: ret void + call void bitcast (void (i8, ...)* @test3a to void (i8, i8)*)( i8 %A, i8 %B) + ret void } - ; test conversion of return value... define i8 @test4a() { - ret i8 0 +; CHECK-LABEL: @test4a( ; CHECK: ret i8 0 + ret i8 0 } define i32 @test4() { - %X = call i32 bitcast (i8 ()* @test4a to i32 ()*)( ) ; [#uses=1] - ret i32 %X -; CHECK: %X = call i8 @test4a() -; CHECK: %1 = zext i8 %X to i32 -; CHECK: ret i32 %1 +; CHECK-LABEL: @test4( +; CHECK: call i32 bitcast + %X = call i32 bitcast (i8 ()* @test4a to i32 ()*)( ) ; [#uses=1] + ret i32 %X } - -; test conversion of return value... no value conversion occurs so we can do +; test conversion of return value... no value conversion occurs so we can do ; this with just a prototype... declare i32 @test5a() define i32 @test5() { - %X = call i32 @test5a( ) ; [#uses=1] - ret i32 %X +; CHECK-LABEL: @test5( ; CHECK: %X = call i32 @test5a() ; CHECK: ret i32 %X + %X = call i32 @test5a( ) ; [#uses=1] + ret i32 %X } - ; test addition of new arguments... declare i32 @test6a(i32) define i32 @test6() { - %X = call i32 bitcast (i32 (i32)* @test6a to i32 ()*)( ) - ret i32 %X +; CHECK-LABEL: @test6( ; CHECK: %X = call i32 @test6a(i32 0) ; CHECK: ret i32 %X + %X = call i32 bitcast (i32 (i32)* @test6a to i32 ()*)( ) + ret i32 %X } - ; test removal of arguments, only can happen with a function body define void @test7a() { - ret void +; CHECK-LABEL: @test7a( ; CHECK: ret void + ret void } define void @test7() { - call void bitcast (void ()* @test7a to void (i32)*)( i32 5 ) - ret void +; CHECK-LABEL: @test7( ; CHECK: call void @test7a() ; CHECK: ret void + call void bitcast (void ()* @test7a to void (i32)*)( i32 5 ) + ret void } @@ -100,6 +102,11 @@ define void @test7() { declare void @test8a() define i8* @test8() { +; CHECK-LABEL: @test8( +; CHECK-NEXT: invoke void @test8a() +; Don't turn this into "unreachable": the callee and caller don't agree in +; calling conv, but the implementation of test8a may actually end up using the +; right calling conv. invoke void @test8a() to label %invoke.cont unwind label %try.handler @@ -114,19 +121,13 @@ try.handler: ; preds = %entry declare i32 @__gxx_personality_v0(...) -; Don't turn this into "unreachable": the callee and caller don't agree in -; calling conv, but the implementation of test8a may actually end up using the -; right calling conv. -; CHECK: @test8() { -; CHECK-NEXT: invoke void @test8a() - - -; Don't turn this into a direct call, because test9x is just a prototype and +; Don't turn this into a direct call, because test9x is just a prototype and ; doing so will make it varargs. ; rdar://9038601 declare i8* @test9x(i8*, i8*, ...) noredzone define i8* @test9(i8* %arg, i8* %tmp3) nounwind ssp noredzone { +; CHECK-LABEL: @test9 entry: %call = call i8* bitcast (i8* (i8*, i8*, ...)* @test9x to i8* (i8*, i8*)*)(i8* %arg, i8* %tmp3) noredzone ret i8* %call diff --git a/unittests/IR/InstructionsTest.cpp b/unittests/IR/InstructionsTest.cpp index 3aa30af6b92..34d662dcb34 100644 --- a/unittests/IR/InstructionsTest.cpp +++ b/unittests/IR/InstructionsTest.cpp @@ -116,11 +116,35 @@ TEST(InstructionsTest, BranchInst) { TEST(InstructionsTest, CastInst) { LLVMContext &C(getGlobalContext()); - Type* Int8Ty = Type::getInt8Ty(C); - Type* Int64Ty = Type::getInt64Ty(C); - Type* V8x8Ty = VectorType::get(Int8Ty, 8); - Type* V8x64Ty = VectorType::get(Int64Ty, 8); - Type* X86MMXTy = Type::getX86_MMXTy(C); + Type *Int8Ty = Type::getInt8Ty(C); + Type *Int16Ty = Type::getInt16Ty(C); + Type *Int32Ty = Type::getInt32Ty(C); + Type *Int64Ty = Type::getInt64Ty(C); + Type *V8x8Ty = VectorType::get(Int8Ty, 8); + Type *V8x64Ty = VectorType::get(Int64Ty, 8); + Type *X86MMXTy = Type::getX86_MMXTy(C); + + Type *HalfTy = Type::getHalfTy(C); + Type *FloatTy = Type::getFloatTy(C); + Type *DoubleTy = Type::getDoubleTy(C); + + Type *V2Int32Ty = VectorType::get(Int32Ty, 2); + Type *V2Int64Ty = VectorType::get(Int64Ty, 2); + Type *V4Int16Ty = VectorType::get(Int16Ty, 4); + + Type *Int32PtrTy = PointerType::get(Int32Ty, 0); + Type *Int64PtrTy = PointerType::get(Int64Ty, 0); + + Type *Int32PtrAS1Ty = PointerType::get(Int32Ty, 1); + Type *Int64PtrAS1Ty = PointerType::get(Int64Ty, 1); + + Type *V2Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 2); + Type *V2Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 2); + Type *V4Int32PtrAS1Ty = VectorType::get(Int32PtrAS1Ty, 4); + Type *V4Int64PtrAS1Ty = VectorType::get(Int64PtrAS1Ty, 4); + + Type *V2Int64PtrTy = VectorType::get(Int64PtrTy, 2); + Type *V2Int32PtrTy = VectorType::get(Int32PtrTy, 2); const Constant* c8 = Constant::getNullValue(V8x8Ty); const Constant* c64 = Constant::getNullValue(V8x64Ty); @@ -132,10 +156,49 @@ TEST(InstructionsTest, CastInst) { EXPECT_TRUE(CastInst::isCastable(V8x8Ty, V8x64Ty)); EXPECT_EQ(CastInst::Trunc, CastInst::getCastOpcode(c64, true, V8x8Ty, true)); EXPECT_EQ(CastInst::SExt, CastInst::getCastOpcode(c8, true, V8x64Ty, true)); + + EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, X86MMXTy)); + EXPECT_FALSE(CastInst::isBitCastable(X86MMXTy, V8x8Ty)); + EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, X86MMXTy)); + EXPECT_FALSE(CastInst::isBitCastable(V8x64Ty, V8x8Ty)); + EXPECT_FALSE(CastInst::isBitCastable(V8x8Ty, V8x64Ty)); + + // Check address space casts are rejected since we don't know the sizes here + EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, Int32PtrAS1Ty)); + EXPECT_FALSE(CastInst::isBitCastable(Int32PtrAS1Ty, Int32PtrTy)); + EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, V2Int32PtrAS1Ty)); + EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int32PtrTy)); + EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V2Int64PtrAS1Ty)); + + // Test mismatched number of elements for pointers + EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int64PtrAS1Ty)); + EXPECT_FALSE(CastInst::isBitCastable(V4Int64PtrAS1Ty, V2Int32PtrAS1Ty)); + EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrAS1Ty, V4Int32PtrAS1Ty)); + EXPECT_FALSE(CastInst::isBitCastable(Int32PtrTy, V2Int32PtrTy)); + EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int32PtrTy)); + + EXPECT_TRUE(CastInst::isBitCastable(Int32PtrTy, Int64PtrTy)); + EXPECT_FALSE(CastInst::isBitCastable(DoubleTy, FloatTy)); + EXPECT_FALSE(CastInst::isBitCastable(FloatTy, DoubleTy)); + EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); + EXPECT_TRUE(CastInst::isBitCastable(FloatTy, FloatTy)); + EXPECT_TRUE(CastInst::isBitCastable(FloatTy, Int32Ty)); + EXPECT_TRUE(CastInst::isBitCastable(Int16Ty, HalfTy)); + EXPECT_TRUE(CastInst::isBitCastable(Int32Ty, FloatTy)); + EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, Int64Ty)); + + EXPECT_TRUE(CastInst::isBitCastable(V2Int32Ty, V4Int16Ty)); + EXPECT_FALSE(CastInst::isBitCastable(Int32Ty, Int64Ty)); + EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, Int32Ty)); + + EXPECT_FALSE(CastInst::isBitCastable(V2Int32PtrTy, Int64Ty)); + EXPECT_FALSE(CastInst::isBitCastable(Int64Ty, V2Int32PtrTy)); + EXPECT_TRUE(CastInst::isBitCastable(V2Int64PtrTy, V2Int32PtrTy)); + EXPECT_TRUE(CastInst::isBitCastable(V2Int32PtrTy, V2Int64PtrTy)); + EXPECT_FALSE(CastInst::isBitCastable(V2Int32Ty, V2Int64Ty)); + EXPECT_FALSE(CastInst::isBitCastable(V2Int64Ty, V2Int32Ty)); } - - TEST(InstructionsTest, VectorGep) { LLVMContext &C(getGlobalContext());