]> granicus.if.org Git - llvm/commitdiff
Analysis: gep inbounds (gep inbounds (...)) is inbounds.
authorPeter Collingbourne <peter@pcc.me.uk>
Tue, 22 Nov 2016 01:03:40 +0000 (01:03 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Tue, 22 Nov 2016 01:03:40 +0000 (01:03 +0000)
Differential Revision: https://reviews.llvm.org/D26441

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@287604 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ConstantFolding.cpp
test/Other/constant-fold-gep.ll

index 69e2e8ad20fe2dcec2cf1f69869ac4adc17ed59f..500faed39b1efafc36a2f869daf987cf77f2a337 100644 (file)
@@ -773,6 +773,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
                                   const DataLayout &DL,
                                   const TargetLibraryInfo *TLI) {
   const GEPOperator *InnermostGEP = GEP;
+  bool InBounds = GEP->isInBounds();
 
   Type *SrcElemTy = GEP->getSourceElementType();
   Type *ResElemTy = GEP->getResultElementType();
@@ -825,6 +826,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
   // If this is a GEP of a GEP, fold it all into a single GEP.
   while (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
     InnermostGEP = GEP;
+    InBounds &= GEP->isInBounds();
 
     SmallVector<Value *, 4> NestedOps(GEP->op_begin() + 1, GEP->op_end());
 
@@ -946,8 +948,8 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
     }
 
   // Create a GEP.
-  Constant *C = ConstantExpr::getGetElementPtr(
-      SrcElemTy, Ptr, NewIdxs, /*InBounds=*/false, InRangeIndex);
+  Constant *C = ConstantExpr::getGetElementPtr(SrcElemTy, Ptr, NewIdxs,
+                                               InBounds, InRangeIndex);
   assert(C->getType()->getPointerElementType() == Ty &&
          "Computed GetElementPtr has unexpected type!");
 
index 9d52f800bc2926599b2e6f5ccb471bb3bddc0109..77c43a200c03f23ba64a5fe92a2703279cc081ba 100644 (file)
 
 ; PLAIN: @Y = global [3 x { i32, i32 }]* getelementptr inbounds ([3 x { i32, i32 }], [3 x { i32, i32 }]* @ext, i64 2)
 ; PLAIN: @Z = global i32* getelementptr inbounds (i32, i32* getelementptr inbounds ([3 x { i32, i32 }], [3 x { i32, i32 }]* @ext, i64 0, i64 1, i32 0), i64 1)
-; OPT: @Y = local_unnamed_addr global [3 x { i32, i32 }]* getelementptr ([3 x { i32, i32 }], [3 x { i32, i32 }]* @ext, i64 2)
+; OPT: @Y = local_unnamed_addr global [3 x { i32, i32 }]* getelementptr inbounds ([3 x { i32, i32 }], [3 x { i32, i32 }]* @ext, i64 2)
 ; OPT: @Z = local_unnamed_addr global i32* getelementptr inbounds ([3 x { i32, i32 }], [3 x { i32, i32 }]* @ext, i64 0, i64 1, i32 1)
-; TO: @Y = local_unnamed_addr global [3 x { i32, i32 }]* getelementptr ([3 x { i32, i32 }], [3 x { i32, i32 }]* @ext, i64 2)
+; TO: @Y = local_unnamed_addr global [3 x { i32, i32 }]* getelementptr inbounds ([3 x { i32, i32 }], [3 x { i32, i32 }]* @ext, i64 2)
 ; TO: @Z = local_unnamed_addr global i32* getelementptr inbounds ([3 x { i32, i32 }], [3 x { i32, i32 }]* @ext, i64 0, i64 1, i32 1)
 
 @ext = external global [3 x { i32, i32 }]