Instcombine cast (getelementptr Ptr, 0, 0, 0) to ... into: cast Ptr to ...
authorChris Lattner <sabre@nondot.org>
Sat, 21 Jun 2003 23:12:02 +0000 (23:12 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 21 Jun 2003 23:12:02 +0000 (23:12 +0000)
This fixes type safety problems in a variety of benchmarks that were confusing
DSA.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@6837 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index bd95a18a701fc0b33d0c65ca201903b3e08c81ae..a0a09e4b1daf0294927ad20d52571edfc0e6a63c 100644 (file)
@@ -930,6 +930,23 @@ Instruction *InstCombiner::visitCastInst(CastInst &CI) {
     }
   }
 
+  // If casting the result of a getelementptr instruction with no offset, turn
+  // this into a cast of the original pointer!
+  //
+  if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(CI.getOperand(0))) {
+    bool AllZeroOperands = true;
+    for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i)
+      if (!isa<Constant>(GEP->getOperand(i)) ||
+          !cast<Constant>(GEP->getOperand(i))->isNullValue()) {
+        AllZeroOperands = false;
+        break;
+      }
+    if (AllZeroOperands) {
+      CI.setOperand(0, GEP->getOperand(0));
+      return &CI;
+    }
+  }
+
   return 0;
 }