[llvm-commits] [llvm] r68768 - /llvm/trunk/include/llvm/Support/IRBuilder.h
Chris Lattner
clattner at apple.com
Thu Apr 9 22:35:04 PDT 2009
On Apr 9, 2009, at 10:30 PM, Nick Lewycky wrote:
> URL: http://llvm.org/viewvc/llvm-project?rev=68768&view=rev
> Log:
> 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!
looks good to me. Incidentally, clang and llvm-gcc specialize the
case when the element size is a power of two into a signed right
shift. This is legal because C only allows pointer subtraction
between elements in the same array, which means the division must be
"exact". In this case, SRA x, c is the same as sdiv x, 2^c. I don't
know if you care about this case though.
-Chris
>
>
> Modified:
> llvm/trunk/include/llvm/Support/IRBuilder.h
>
> Modified: llvm/trunk/include/llvm/Support/IRBuilder.h
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/IRBuilder.h?rev=68768&r1=68767&r2=68768&view=diff
>
> =
> =
> =
> =
> =
> =
> =
> =
> ======================================================================
> --- llvm/trunk/include/llvm/Support/IRBuilder.h (original)
> +++ llvm/trunk/include/llvm/Support/IRBuilder.h Fri Apr 10 00:30:48
> 2009
> @@ -681,6 +681,20 @@
> 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);
> + }
> };
>
> }
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list