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

Chris Lattner sabre at nondot.org
Tue Mar 17 21:25:14 PDT 2009


Author: lattner
Date: Tue Mar 17 23:25:13 2009
New Revision: 67165

URL: http://llvm.org/viewvc/llvm-project?rev=67165&view=rev
Log:
fix PR3809, codegen for inc/dec of function pointers.

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

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Tue Mar 17 23:25:13 2009
@@ -627,10 +627,18 @@
   int AmountVal = isInc ? 1 : -1;
   
   Value *NextVal;
-  if (isa<llvm::PointerType>(InVal->getType())) {
+  if (const llvm::PointerType *PT = 
+         dyn_cast<llvm::PointerType>(InVal->getType())) {
     // FIXME: This isn't right for VLAs.
-    NextVal = llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
-    NextVal = Builder.CreateGEP(InVal, NextVal, "ptrincdec");
+    llvm::Constant *Inc =llvm::ConstantInt::get(llvm::Type::Int32Ty, AmountVal);
+    if (!isa<llvm::FunctionType>(PT->getElementType())) {
+      NextVal = Builder.CreateGEP(InVal, Inc, "ptrincdec");
+    } else {
+      const llvm::Type *i8Ty = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+      NextVal = Builder.CreateBitCast(InVal, i8Ty, "tmp");
+      NextVal = Builder.CreateGEP(NextVal, Inc, "ptrincdec");
+      NextVal = Builder.CreateBitCast(NextVal, InVal->getType());
+    }
   } else if (InVal->getType() == llvm::Type::Int1Ty && isInc) {
     // Bool++ is an interesting case, due to promotion rules, we get:
     // Bool++ -> Bool = Bool+1 -> Bool = (int)Bool+1 ->

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

==============================================================================
--- cfe/trunk/test/CodeGen/exprs.c (original)
+++ cfe/trunk/test/CodeGen/exprs.c Tue Mar 17 23:25:13 2009
@@ -67,3 +67,15 @@
   return ((struct X)foo()).Y + 1;
 }
 
+// PR3809: INC/DEC of function pointers.
+void f2(void);
+unsigned f1(void) {
+  void (*fp)(void) = f2;
+  
+  ++fp;
+  fp++;
+  --fp;
+  fp--;
+  return (unsigned) fp;
+}  
+





More information about the cfe-commits mailing list