Add utility function to IRBuilder that takes the difference between two
authorNick Lewycky <nicholas@mxc.ca>
Fri, 10 Apr 2009 05:30:48 +0000 (05:30 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Fri, 10 Apr 2009 05:30:48 +0000 (05:30 +0000)
pointers, taking into account the size of the pointed-to object.
Patch by Jeffrey Yasskin!

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

include/llvm/Support/IRBuilder.h

index a4ae671e463ae7208d52a4c3b2f76256a3d6d7ad..3ab933807ac049ff9515518618704b82d4ed6732 100644 (file)
@@ -681,6 +681,20 @@ public:
                         Name);
   }
 
+  /// CreatePtrDiff - Return the i64 difference between two pointer values,
+  /// dividing out the size of the pointed-to objects.  This is intended to
+  /// implement C-style pointer subtraction.
+  Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
+    assert(LHS->getType() == RHS->getType() &&
+           "Pointer subtraction operand types must match!");
+    const PointerType *ArgType = cast<PointerType>(LHS->getType());
+    Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
+    Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
+    Value *Difference = CreateSub(LHS_int, RHS_int);
+    return CreateSDiv(Difference,
+                      ConstantExpr::getSizeOf(ArgType->getElementType()),
+                      Name);
+  }
 };
 
 }