aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/llvm/Instructions.h5
-rw-r--r--lib/VMCore/Instructions.cpp16
2 files changed, 21 insertions, 0 deletions
diff --git a/include/llvm/Instructions.h b/include/llvm/Instructions.h
index 8f3b24a1e6..2387739720 100644
--- a/include/llvm/Instructions.h
+++ b/include/llvm/Instructions.h
@@ -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; }
diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp
index f6abd85202..bfda46d983 100644
--- a/lib/VMCore/Instructions.cpp
+++ b/lib/VMCore/Instructions.cpp
@@ -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
//===----------------------------------------------------------------------===//