add GetElementPtrInst::hasAllZeroIndices, a long-overdue helper method.
authorChris Lattner <sabre@nondot.org>
Sat, 14 Apr 2007 00:12:57 +0000 (00:12 +0000)
committerChris Lattner <sabre@nondot.org>
Sat, 14 Apr 2007 00:12:57 +0000 (00:12 +0000)
Writing it twice in the same day was too much for me.

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

include/llvm/Instructions.h
lib/VMCore/Instructions.cpp

index 8f3b24a1e6f6cc7511eb426ccb710aaebc852e1a..2387739720ce08db682fd1939fa1fe2ecb727e84 100644 (file)
@@ -408,6 +408,11 @@ public:
   inline bool hasIndices() const {
     return getNumOperands() > 1;
   }
+  
+  /// hasAllZeroIndices - Return true if all of the indices of this GEP are
+  /// zeros.  If so, the result pointer and the first operand have the same
+  /// value, just potentially different types.
+  bool hasAllZeroIndices() const;
 
   // Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const GetElementPtrInst *) { return true; }
index f6abd85202cb4369d224e34fa8f9b3f7069b20c3..bfda46d983199935ea485a13c2a4d4973c95b371 100644 (file)
@@ -966,6 +966,22 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
   return PTy->getElementType();
 }
 
+
+/// hasAllZeroIndices - Return true if all of the indices of this GEP are
+/// zeros.  If so, the result pointer and the first operand have the same
+/// value, just potentially different types.
+bool GetElementPtrInst::hasAllZeroIndices() const {
+  for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
+    if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) {
+      if (!CI->isZero()) return false;
+    } else {
+      return false;
+    }
+  }
+  return true;
+}
+
+
 //===----------------------------------------------------------------------===//
 //                           ExtractElementInst Implementation
 //===----------------------------------------------------------------------===//