Here's a small patch to llvm-convert.cpp that prevents llvm-gcc from
creating useless bit casts. It just checks to make sure that the types
are different before inserting a cast. This saves a bit of memory and
gives InstCombine less work to do as these useless casts were being
inserted in numerous places only to be stripped out again later.

Reid.
Index: gcc/llvm-convert.cpp
===================================================================
--- gcc/llvm-convert.cpp	(revision 269)
+++ gcc/llvm-convert.cpp	(working copy)
@@ -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

Reply via email to