aboutsummaryrefslogtreecommitdiffstats
path: root/include/llvm/Support/IRBuilder.h
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2009-04-10 05:30:48 +0000
committerNick Lewycky <nicholas@mxc.ca>2009-04-10 05:30:48 +0000
commitb99625fcc7be7b5fed836ab34d7dd579f5a2ab4f (patch)
treed0d0be655a94f52b3018e18f132552158d081565 /include/llvm/Support/IRBuilder.h
parent45f0f82404620b64d056678b905838bae9ed4b18 (diff)
downloadexternal_llvm-b99625fcc7be7b5fed836ab34d7dd579f5a2ab4f.zip
external_llvm-b99625fcc7be7b5fed836ab34d7dd579f5a2ab4f.tar.gz
external_llvm-b99625fcc7be7b5fed836ab34d7dd579f5a2ab4f.tar.bz2
Add utility function to IRBuilder that takes the difference between two
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
Diffstat (limited to 'include/llvm/Support/IRBuilder.h')
-rw-r--r--include/llvm/Support/IRBuilder.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/include/llvm/Support/IRBuilder.h b/include/llvm/Support/IRBuilder.h
index a4ae671..3ab9338 100644
--- a/include/llvm/Support/IRBuilder.h
+++ b/include/llvm/Support/IRBuilder.h
@@ -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);
+ }
};
}