From cc2dc0916ad6a00ebc9373a58854d77cf73af122 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 12 Jun 2024 09:50:14 +0200 Subject: Reapply [ConstantFold] Drop gep of gep fold entirely (#95126) Reapplying without changes. The flang+openmp buildbot failure should be addressed by https://github.com/llvm/llvm-project/pull/94541. ----- This is a followup to https://github.com/llvm/llvm-project/pull/93823 and drops the DataLayout-unaware GEP of GEP fold entirely. All cases are now left to the DataLayout-aware constant folder, which will fold everything to a single i8 GEP. We didn't have any test coverage for this fold in LLVM, but some Clang tests change. --- llvm/include/llvm/IR/ConstantFold.h | 2 +- llvm/lib/Analysis/InstructionSimplify.cpp | 5 ++--- llvm/lib/IR/ConstantFold.cpp | 33 ------------------------------- llvm/lib/IR/Constants.cpp | 3 +-- 4 files changed, 4 insertions(+), 39 deletions(-) (limited to 'llvm') diff --git a/llvm/include/llvm/IR/ConstantFold.h b/llvm/include/llvm/IR/ConstantFold.h index 9b3c8a0..42043d3 100644 --- a/llvm/include/llvm/IR/ConstantFold.h +++ b/llvm/include/llvm/IR/ConstantFold.h @@ -52,7 +52,7 @@ namespace llvm { Constant *V2); Constant *ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, Constant *C1, Constant *C2); - Constant *ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool InBounds, + Constant *ConstantFoldGetElementPtr(Type *Ty, Constant *C, std::optional InRange, ArrayRef Idxs); } // End llvm namespace diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 8b2aa6b..0089586 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -5068,9 +5068,8 @@ static Value *simplifyGEPInst(Type *SrcTy, Value *Ptr, return nullptr; if (!ConstantExpr::isSupportedGetElementPtr(SrcTy)) - // TODO(gep_nowrap): Pass on the whole GEPNoWrapFlags. - return ConstantFoldGetElementPtr(SrcTy, cast(Ptr), - NW.isInBounds(), std::nullopt, Indices); + return ConstantFoldGetElementPtr(SrcTy, cast(Ptr), std::nullopt, + Indices); auto *CE = ConstantExpr::getGetElementPtr(SrcTy, cast(Ptr), Indices, NW); diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 77a8336..34bcf36 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -1404,35 +1404,7 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate, return nullptr; } -// Combine Indices - If the source pointer to this getelementptr instruction -// is a getelementptr instruction, combine the indices of the two -// getelementptr instructions into a single instruction. -static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds, - ArrayRef Idxs) { - if (PointeeTy != GEP->getResultElementType()) - return nullptr; - - // Leave inrange handling to DL-aware constant folding. - if (GEP->getInRange()) - return nullptr; - - // Only handle simple case with leading zero index. We cannot perform an - // actual addition as we don't know the correct index type size to use. - Constant *Idx0 = cast(Idxs[0]); - if (!Idx0->isNullValue()) - return nullptr; - - SmallVector NewIndices; - NewIndices.reserve(Idxs.size() + GEP->getNumIndices()); - NewIndices.append(GEP->idx_begin(), GEP->idx_end()); - NewIndices.append(Idxs.begin() + 1, Idxs.end()); - return ConstantExpr::getGetElementPtr( - GEP->getSourceElementType(), cast(GEP->getPointerOperand()), - NewIndices, InBounds && GEP->isInBounds()); -} - Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, - bool InBounds, std::optional InRange, ArrayRef Idxs) { if (Idxs.empty()) return C; @@ -1462,10 +1434,5 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, cast(GEPTy)->getElementCount(), C) : C; - if (ConstantExpr *CE = dyn_cast(C)) - if (auto *GEP = dyn_cast(CE)) - if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs)) - return C; - return nullptr; } diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index a76be44..d079073 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -2438,8 +2438,7 @@ Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C, assert(Ty && "Must specify element type"); assert(isSupportedGetElementPtr(Ty) && "Element type is unsupported!"); - if (Constant *FC = - ConstantFoldGetElementPtr(Ty, C, NW.isInBounds(), InRange, Idxs)) + if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InRange, Idxs)) return FC; // Fold a few common cases. assert(GetElementPtrInst::getIndexedType(Ty, Idxs) && "GEP indices invalid!"); -- cgit v1.1