Revision: 123559 Author: clattner Date: 2007-02-07 13:53:56 -0800 (Wed, 07 Feb 2007)
Log Message: ----------- Don't create useless bitcast instructions. Patch by Reid Spencer! Modified Paths: -------------- apple-local/branches/llvm/gcc/llvm-convert.cpp Modified: apple-local/branches/llvm/gcc/llvm-convert.cpp =================================================================== --- apple-local/branches/llvm/gcc/llvm-convert.cpp 2007-02-07 19:31:44 UTC (rev 123558) +++ apple-local/branches/llvm/gcc/llvm-convert.cpp 2007-02-07 21:53:56 UTC (rev 123559) @@ -708,7 +708,10 @@ /// CastToType - Cast the specified value to the specified type if it is /// not already that type. Value *TreeToLLVM::CastToType(unsigned opcode, Value *V, const Type* Ty) { - if (V->getType() == Ty) return V; + // Eliminate useless casts of a type to itself. + if (V->getType() == Ty) + return V; + if (Constant *C = dyn_cast<Constant>(V)) return ConstantExpr::getCast(Instruction::CastOps(opcode), C, Ty); @@ -725,28 +728,48 @@ /// assumptions about the types of the arguments. This creates an inferred cast. Value *TreeToLLVM::CastToAnyType(Value *V, bool VisSigned, const Type* Ty, bool TyIsSigned) { - Instruction::CastOps opcode = CastInst::getCastOpcode(V, VisSigned, Ty, - TyIsSigned); - return CastToType(opcode, V, Ty); + // Eliminate useless casts of a type to itself. + if (V->getType() == Ty) + return V; + + // The types are different so we must cast. Use getCastOpcode to create an + // inferred cast opcode. + Instruction::CastOps opc = + CastInst::getCastOpcode(V, VisSigned, Ty, TyIsSigned); + + // Generate the cast and return it. + return CastToType(opc, V, Ty); } /// CastToUIntType - Cast the specified value to the specified type assuming /// that the value and type are unsigned integer types. Value *TreeToLLVM::CastToUIntType(Value *V, const Type* Ty) { + // Eliminate useless casts of a type to itself. + if (V->getType() == Ty) + return V; + unsigned SrcBits = V->getType()->getPrimitiveSizeInBits(); unsigned DstBits = Ty->getPrimitiveSizeInBits(); - Instruction::CastOps opcode = (SrcBits == DstBits ? Instruction::BitCast : - (SrcBits > DstBits ? Instruction::Trunc : Instruction::ZExt)); + assert(SrcBits != DstBits && "Types are different but have same #bits?"); + + Instruction::CastOps opcode = + (SrcBits > DstBits ? Instruction::Trunc : Instruction::ZExt); return CastToType(opcode, V, Ty); } /// CastToSIntType - Cast the specified value to the specified type assuming /// that the value and type are signed integer types. Value *TreeToLLVM::CastToSIntType(Value *V, const Type* Ty) { + // Eliminate useless casts of a type to itself. + if (V->getType() == Ty) + return V; + unsigned SrcBits = V->getType()->getPrimitiveSizeInBits(); unsigned DstBits = Ty->getPrimitiveSizeInBits(); - Instruction::CastOps opcode = (SrcBits == DstBits ? Instruction::BitCast : - (SrcBits > DstBits ? Instruction::Trunc : Instruction::SExt)); + assert(SrcBits != DstBits && "Types are different but have same #bits?"); + + Instruction::CastOps opcode = + (SrcBits > DstBits ? Instruction::Trunc : Instruction::SExt); return CastToType(opcode, V, Ty); } _______________________________________________ llvm-commits mailing list llvm-commits@cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits