[cfe-commits] r54338 - in /cfe/trunk: lib/CodeGen/CGExprScalar.cpp test/CodeGen/2008-08-04-void-pointer-arithmetic.c

Daniel Dunbar daniel at zuster.org
Mon Aug 4 17:47:04 PDT 2008


Author: ddunbar
Date: Mon Aug  4 19:47:03 2008
New Revision: 54338

URL: http://llvm.org/viewvc/llvm-project?rev=54338&view=rev
Log:
Bug fix, CodeGen of (void*) - (void*) was broken.
 - <rdar://problem/6122967>

Added:
    cfe/trunk/test/CodeGen/2008-08-04-void-pointer-arithmetic.c
Modified:
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=54338&r1=54337&r2=54338&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Mon Aug  4 19:47:03 2008
@@ -851,6 +851,8 @@
       Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
   }
   
+  // The GNU void* - int case is automatically handled here because
+  // our LLVM type for void* is i8*.
   return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
 }
 
@@ -866,19 +868,26 @@
   
   const QualType LHSType = E->getLHS()->getType();
   const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
-  uint64_t ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
+  uint64_t ElementSize;
+
+  // Handle GCC extension for pointer arithmetic on void* types.
+  if (LHSElementType->isVoidType()) {
+    ElementSize = 1;
+  } else {
+    ElementSize = CGF.getContext().getTypeSize(LHSElementType) / 8;
+  }
   
   const llvm::Type *ResultType = ConvertType(E->getType());
   LHS = Builder.CreatePtrToInt(LHS, ResultType, "sub.ptr.lhs.cast");
   RHS = Builder.CreatePtrToInt(RHS, ResultType, "sub.ptr.rhs.cast");
   Value *BytesBetween = Builder.CreateSub(LHS, RHS, "sub.ptr.sub");
-  
+
   // HACK: LLVM doesn't have an divide instruction that 'knows' there is no
   // remainder.  As such, we handle common power-of-two cases here to generate
   // better code.
   if (llvm::isPowerOf2_64(ElementSize)) {
     Value *ShAmt =
-    llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
+      llvm::ConstantInt::get(ResultType, llvm::Log2_64(ElementSize));
     return Builder.CreateAShr(BytesBetween, ShAmt, "sub.ptr.shr");
   }
   

Added: cfe/trunk/test/CodeGen/2008-08-04-void-pointer-arithmetic.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/2008-08-04-void-pointer-arithmetic.c?rev=54338&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/2008-08-04-void-pointer-arithmetic.c (added)
+++ cfe/trunk/test/CodeGen/2008-08-04-void-pointer-arithmetic.c Mon Aug  4 19:47:03 2008
@@ -0,0 +1,6 @@
+// RUN: clang --emit-llvm -o - %s
+// <rdar://problem/6122967>
+
+int f0(void *a, void *b) {
+  return a - b;
+}





More information about the cfe-commits mailing list