[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp

Chris Lattner lattner at cs.uiuc.edu
Mon Jul 19 22:21:10 PDT 2004



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.227 -> 1.228

---
Log message:

Implement Transforms/InstCombine/IntPtrCast.ll


---
Diffs of the changes:  (+16 -11)

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.227 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.228
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.227	Mon Jul 19 20:48:15 2004
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Tue Jul 20 00:21:00 2004
@@ -1966,7 +1966,7 @@
 // instruction.
 //
 static inline bool isEliminableCastOfCast(const Type *SrcTy, const Type *MidTy,
-                                          const Type *DstTy) {
+                                          const Type *DstTy, TargetData *TD) {
 
   // It is legal to eliminate the instruction if casting A->B->A if the sizes
   // are identical and the bits don't get reinterpreted (for example 
@@ -1974,6 +1974,15 @@
   if (SrcTy == DstTy && SrcTy->isLosslesslyConvertibleTo(MidTy))
     return true;
 
+  // If the source and destination types are pointer types, and the intermediate
+  // is an integer type bigger than a pointer, eliminate the casts.
+  if (isa<PointerType>(SrcTy) && isa<PointerType>(DstTy)) {
+    if (isa<PointerType>(MidTy)) return true;
+
+    if (MidTy->isInteger() && MidTy->getPrimitiveSize() >= TD->getPointerSize())
+      return true;
+  }
+
   // Allow free casting and conversion of sizes as long as the sign doesn't
   // change...
   if (SrcTy->isIntegral() && MidTy->isIntegral() && DstTy->isIntegral()) {
@@ -2010,18 +2019,14 @@
       return true;
     }
   }
-
-  // Otherwise, we cannot succeed.  Specifically we do not want to allow things
-  // like:  short -> ushort -> uint, because this can create wrong results if
-  // the input short is negative!
-  //
   return false;
 }
 
-static bool ValueRequiresCast(const Value *V, const Type *Ty) {
+static bool ValueRequiresCast(const Value *V, const Type *Ty, TargetData *TD) {
   if (V->getType() == Ty || isa<Constant>(V)) return false;
   if (const CastInst *CI = dyn_cast<CastInst>(V))
-    if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty))
+    if (isEliminableCastOfCast(CI->getOperand(0)->getType(), CI->getType(), Ty,
+                               TD))
       return false;
   return true;
 }
@@ -2056,7 +2061,7 @@
   //
   if (CastInst *CSrc = dyn_cast<CastInst>(Src)) {
     if (isEliminableCastOfCast(CSrc->getOperand(0)->getType(),
-                               CSrc->getType(), CI.getType())) {
+                               CSrc->getType(), CI.getType(), TD)) {
       // This instruction now refers directly to the cast's src operand.  This
       // has a good chance of making CSrc dead.
       CI.setOperand(0, CSrc->getOperand(0));
@@ -2153,8 +2158,8 @@
           // Don't insert two casts if they cannot be eliminated.  We allow two
           // casts to be inserted if the sizes are the same.  This could only be
           // converting signedness, which is a noop.
-          if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy) ||
-              !ValueRequiresCast(Op0, DestTy)) {
+          if (DestBitSize == SrcBitSize || !ValueRequiresCast(Op1, DestTy,TD) ||
+              !ValueRequiresCast(Op0, DestTy, TD)) {
             Value *Op0c = InsertOperandCastBefore(Op0, DestTy, SrcI);
             Value *Op1c = InsertOperandCastBefore(Op1, DestTy, SrcI);
             return BinaryOperator::create(cast<BinaryOperator>(SrcI)





More information about the llvm-commits mailing list