[llvm-commits] [llvm] r76252 - in /llvm/trunk/lib: Analysis/BasicAliasAnalysis.cpp Transforms/Scalar/InstructionCombining.cpp VMCore/Value.cpp

Dan Gohman gohman at apple.com
Fri Jul 17 15:25:12 PDT 2009


Author: djg
Date: Fri Jul 17 17:25:10 2009
New Revision: 76252

URL: http://llvm.org/viewvc/llvm-project?rev=76252&view=rev
Log:
Make BasicAliasAnalysis and Value::getUnderlyingObject use
GEPOperator's hasNoPointer0verflow(), and make a few places in instcombine
that create GEPs that may overflow clear the NoOverflow value. Among
other things, this partially addresses PR2831.

Modified:
    llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp
    llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
    llvm/trunk/lib/VMCore/Value.cpp

Modified: llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp?rev=76252&r1=76251&r2=76252&view=diff

==============================================================================
--- llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp (original)
+++ llvm/trunk/lib/Analysis/BasicAliasAnalysis.cpp Fri Jul 17 17:25:10 2009
@@ -23,6 +23,7 @@
 #include "llvm/Instructions.h"
 #include "llvm/IntrinsicInst.h"
 #include "llvm/LLVMContext.h"
+#include "llvm/Operator.h"
 #include "llvm/Pass.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/ADT/SmallVector.h"
@@ -38,10 +39,11 @@
 //===----------------------------------------------------------------------===//
 
 static const User *isGEP(const Value *V) {
-  if (isa<GetElementPtrInst>(V) ||
-      (isa<ConstantExpr>(V) &&
-       cast<ConstantExpr>(V)->getOpcode() == Instruction::GetElementPtr))
-    return cast<User>(V);
+  if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V))
+    // For the purposes of BasicAliasAnalysis, if the GEP has overflow it
+    // could do crazy things.
+    if (GEP->hasNoPointerOverflow())
+      return GEP;
   return 0;
 }
 

Modified: llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp?rev=76252&r1=76251&r2=76252&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/InstructionCombining.cpp Fri Jul 17 17:25:10 2009
@@ -2310,7 +2310,10 @@
         cast<PointerType>(CI->getOperand(0)->getType())->getAddressSpace();
       Value *I2 = InsertBitCastBefore(CI->getOperand(0),
                                   Context->getPointerType(Type::Int8Ty, AS), I);
-      I2 = InsertNewInstBefore(GetElementPtrInst::Create(I2, Other, "ctg2"), I);
+      GetElementPtrInst *GEP = GetElementPtrInst::Create(I2, Other, "ctg2");
+      // A GEP formed from an arbitrary add may overflow.
+      cast<GEPOperator>(GEP)->setHasNoPointerOverflow(false);
+      I2 = InsertNewInstBefore(GEP, I);
       return new PtrToIntInst(I2, CI->getType());
     }
   }
@@ -8942,7 +8945,12 @@
       // If Offset is evenly divisible by Size, we can do this xform.
       if (Size && !APIntOps::srem(Offset, APInt(Offset.getBitWidth(), Size))){
         Offset = APIntOps::sdiv(Offset, APInt(Offset.getBitWidth(), Size));
-        return GetElementPtrInst::Create(X, Context->getConstantInt(Offset));
+        GetElementPtrInst *GEP =
+          GetElementPtrInst::Create(X, Context->getConstantInt(Offset));
+        // A gep synthesized from inttoptr+add+ptrtoint must be assumed to
+        // potentially overflow, in the absense of further analysis.
+        cast<GEPOperator>(GEP)->setHasNoPointerOverflow(false);
+        return GEP;
       }
     }
     // TODO: Could handle other cases, e.g. where add is indexing into field of
@@ -8966,8 +8974,12 @@
       
       Instruction *P = InsertNewInstBefore(new IntToPtrInst(X, CI.getType(),
                                                             "tmp"), CI);
-      return GetElementPtrInst::Create(P,
-                                       Context->getConstantInt(Offset), "tmp");
+      GetElementPtrInst *GEP =
+        GetElementPtrInst::Create(P, Context->getConstantInt(Offset), "tmp");
+      // A gep synthesized from inttoptr+add+ptrtoint must be assumed to
+      // potentially overflow, in the absense of further analysis.
+      cast<GEPOperator>(GEP)->setHasNoPointerOverflow(false);
+      return GEP;
     }
   }
   return 0;

Modified: llvm/trunk/lib/VMCore/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Value.cpp?rev=76252&r1=76251&r2=76252&view=diff

==============================================================================
--- llvm/trunk/lib/VMCore/Value.cpp (original)
+++ llvm/trunk/lib/VMCore/Value.cpp Fri Jul 17 17:25:10 2009
@@ -16,6 +16,7 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/InstrTypes.h"
 #include "llvm/Instructions.h"
+#include "llvm/Operator.h"
 #include "llvm/Module.h"
 #include "llvm/ValueSymbolTable.h"
 #include "llvm/Support/Debug.h"
@@ -372,15 +373,12 @@
   Value *V = this;
   unsigned MaxLookup = 6;
   do {
-    if (Instruction *I = dyn_cast<Instruction>(V)) {
-      if (!isa<BitCastInst>(I) && !isa<GetElementPtrInst>(I))
+    if (Operator *O = dyn_cast<Operator>(V)) {
+      if (O->getOpcode() != Instruction::BitCast &&
+          (O->getOpcode() != Instruction::GetElementPtr ||
+           !cast<GEPOperator>(V)->hasNoPointerOverflow()))
         return V;
-      V = I->getOperand(0);
-    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
-      if (CE->getOpcode() != Instruction::BitCast &&
-          CE->getOpcode() != Instruction::GetElementPtr)
-        return V;
-      V = CE->getOperand(0);
+      V = O->getOperand(0);
     } else {
       return V;
     }





More information about the llvm-commits mailing list