[llvm] 873889b - [InstCombine] Extract logic for "emit offset and rewrite gep" (NFC)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Apr 24 22:18:22 PDT 2024
Author: Nikita Popov
Date: 2024-04-25T14:18:11+09:00
New Revision: 873889b7fa23434876533c603e687fc70396b0ab
URL: https://github.com/llvm/llvm-project/commit/873889b7fa23434876533c603e687fc70396b0ab
DIFF: https://github.com/llvm/llvm-project/commit/873889b7fa23434876533c603e687fc70396b0ab.diff
LOG: [InstCombine] Extract logic for "emit offset and rewrite gep" (NFC)
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/lib/Transforms/InstCombine/InstCombineInternal.h
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 6ce39be59bda98..567b27b4630439 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2050,9 +2050,9 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
// the GEP otherwise.
if (GEP->hasOneUse() &&
isa<ConstantPointerNull>(GEP->getPointerOperand())) {
- return replaceInstUsesWith(CI,
- Builder.CreateIntCast(EmitGEPOffset(GEP), Ty,
- /*isSigned=*/false));
+ return replaceInstUsesWith(
+ CI, Builder.CreateIntCast(EmitGEPOffset(cast<GEPOperator>(GEP)), Ty,
+ /*isSigned=*/false));
}
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index c2062adcd5b7f7..1064340cb53661 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -816,29 +816,9 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
}
if (GEPsInBounds || CmpInst::isEquality(Cond)) {
- auto EmitGEPOffsetAndRewrite = [&](GEPOperator *GEP) {
- IRBuilderBase::InsertPointGuard Guard(Builder);
- auto *Inst = dyn_cast<Instruction>(GEP);
- if (Inst)
- Builder.SetInsertPoint(Inst);
-
- Value *Offset = EmitGEPOffset(GEP);
- // If a non-trivial GEP has other uses, rewrite it to avoid duplicating
- // the offset arithmetic.
- if (Inst && !GEP->hasOneUse() && !GEP->hasAllConstantIndices() &&
- !GEP->getSourceElementType()->isIntegerTy(8)) {
- replaceInstUsesWith(*Inst,
- Builder.CreateGEP(Builder.getInt8Ty(),
- GEP->getPointerOperand(),
- Offset, "", GEPsInBounds));
- eraseInstFromFunction(*Inst);
- }
- return Offset;
- };
-
// ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
- Value *L = EmitGEPOffsetAndRewrite(GEPLHS);
- Value *R = EmitGEPOffsetAndRewrite(GEPRHS);
+ Value *L = EmitGEPOffset(GEPLHS, /*RewriteGEP=*/true);
+ Value *R = EmitGEPOffset(GEPRHS, /*RewriteGEP=*/true);
return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
}
}
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 4479afbd09afde..aafb4cf6ca6a62 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -376,7 +376,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
}
}
- Value *EmitGEPOffset(User *GEP);
+ Value *EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP = false);
Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
Instruction *foldBitcastExtElt(ExtractElementInst &ExtElt);
Instruction *foldCastedBitwiseLogic(BinaryOperator &I);
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index f6684834a77220..282badd4369330 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -190,8 +190,26 @@ bool InstCombiner::isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const {
return TTI.isValidAddrSpaceCast(FromAS, ToAS);
}
-Value *InstCombinerImpl::EmitGEPOffset(User *GEP) {
- return llvm::emitGEPOffset(&Builder, DL, GEP);
+Value *InstCombinerImpl::EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP) {
+ if (!RewriteGEP)
+ return llvm::emitGEPOffset(&Builder, DL, GEP);
+
+ IRBuilderBase::InsertPointGuard Guard(Builder);
+ auto *Inst = dyn_cast<Instruction>(GEP);
+ if (Inst)
+ Builder.SetInsertPoint(Inst);
+
+ Value *Offset = EmitGEPOffset(GEP);
+ // If a non-trivial GEP has other uses, rewrite it to avoid duplicating
+ // the offset arithmetic.
+ if (Inst && !GEP->hasOneUse() && !GEP->hasAllConstantIndices() &&
+ !GEP->getSourceElementType()->isIntegerTy(8)) {
+ replaceInstUsesWith(
+ *Inst, Builder.CreateGEP(Builder.getInt8Ty(), GEP->getPointerOperand(),
+ Offset, "", GEP->isInBounds()));
+ eraseInstFromFunction(*Inst);
+ }
+ return Offset;
}
/// Legal integers and common types are considered desirable. This is used to
More information about the llvm-commits
mailing list