+/// stripPointerAdjustments - This is like Value::stripPointerCasts, but also
+/// removes inbounds gep operations, regardless of their indices.
+static Value *stripPointerAdjustmentsImpl(Value *V,
+ SmallPtrSet<GEPOperator*, 8> &VisitedGEPs) {
+ GEPOperator *GEP = dyn_cast<GEPOperator>(V);
+ if (GEP == 0 || !GEP->isInBounds())
+ return V;
+
+ // If we've already seen this GEP, we will end up infinitely looping. This
+ // can happen in unreachable code.
+ if (!VisitedGEPs.insert(GEP))
+ return V;
+
+ return stripPointerAdjustmentsImpl(GEP->getOperand(0)->stripPointerCasts(),
+ VisitedGEPs);
+}
+
+static Value *stripPointerAdjustments(Value *V) {
+ SmallPtrSet<GEPOperator*, 8> VisitedGEPs;
+ return stripPointerAdjustmentsImpl(V, VisitedGEPs);
+}
+
+