[cfe-commits] r46599 - in /cfe/trunk: CodeGen/CGExprScalar.cpp test/CodeGen/exprs.c

Chris Lattner sabre at nondot.org
Wed Jan 30 20:12:51 PST 2008


Author: lattner
Date: Wed Jan 30 22:12:50 2008
New Revision: 46599

URL: http://llvm.org/viewvc/llvm-project?rev=46599&view=rev
Log:
Fix PR1921 by promoting negative indices to intptrty.

Modified:
    cfe/trunk/CodeGen/CGExprScalar.cpp
    cfe/trunk/test/CodeGen/exprs.c

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

==============================================================================
--- cfe/trunk/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/CodeGen/CGExprScalar.cpp Wed Jan 30 22:12:50 2008
@@ -767,8 +767,20 @@
   assert(!isa<llvm::PointerType>(Ops.RHS->getType()) &&
          "ptr-ptr shouldn't get here");
   // FIXME: The pointer could point to a VLA.
-  Value *NegatedRHS = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
-  return Builder.CreateGEP(Ops.LHS, NegatedRHS, "sub.ptr");
+  Value *Idx = Builder.CreateNeg(Ops.RHS, "sub.ptr.neg");
+  
+  unsigned Width = cast<llvm::IntegerType>(Idx->getType())->getBitWidth();
+  if (Width < CGF.LLVMPointerWidth) {
+    // Zero or sign extend the pointer value based on whether the index is
+    // signed or not.
+    const llvm::Type *IdxType = llvm::IntegerType::get(CGF.LLVMPointerWidth);
+    if (Ops.E->getRHS()->getType().getCanonicalType()->isSignedIntegerType())
+      Idx = Builder.CreateSExt(Idx, IdxType, "idx.ext");
+    else
+      Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
+  }
+  
+  return Builder.CreateGEP(Ops.LHS, Idx, "sub.ptr");
 }
 
 Value *ScalarExprEmitter::VisitBinSub(const BinaryOperator *E) {

Modified: cfe/trunk/test/CodeGen/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/exprs.c?rev=46599&r1=46598&r2=46599&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/exprs.c (original)
+++ cfe/trunk/test/CodeGen/exprs.c Wed Jan 30 22:12:50 2008
@@ -17,3 +17,9 @@
 _Bool test2b; 
 int test2() {if (test2b);}
 
+// PR1921
+int test3() {
+  const unsigned char *bp;
+  bp -= (short)1;
+}
+





More information about the cfe-commits mailing list