Skip to content

Commit 873889b

Browse files
committed
[InstCombine] Extract logic for "emit offset and rewrite gep" (NFC)
1 parent 4b10ade commit 873889b

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,9 +2050,9 @@ Instruction *InstCombinerImpl::visitPtrToInt(PtrToIntInst &CI) {
20502050
// the GEP otherwise.
20512051
if (GEP->hasOneUse() &&
20522052
isa<ConstantPointerNull>(GEP->getPointerOperand())) {
2053-
return replaceInstUsesWith(CI,
2054-
Builder.CreateIntCast(EmitGEPOffset(GEP), Ty,
2055-
/*isSigned=*/false));
2053+
return replaceInstUsesWith(
2054+
CI, Builder.CreateIntCast(EmitGEPOffset(cast<GEPOperator>(GEP)), Ty,
2055+
/*isSigned=*/false));
20562056
}
20572057
}
20582058

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -816,29 +816,9 @@ Instruction *InstCombinerImpl::foldGEPICmp(GEPOperator *GEPLHS, Value *RHS,
816816
}
817817

818818
if (GEPsInBounds || CmpInst::isEquality(Cond)) {
819-
auto EmitGEPOffsetAndRewrite = [&](GEPOperator *GEP) {
820-
IRBuilderBase::InsertPointGuard Guard(Builder);
821-
auto *Inst = dyn_cast<Instruction>(GEP);
822-
if (Inst)
823-
Builder.SetInsertPoint(Inst);
824-
825-
Value *Offset = EmitGEPOffset(GEP);
826-
// If a non-trivial GEP has other uses, rewrite it to avoid duplicating
827-
// the offset arithmetic.
828-
if (Inst && !GEP->hasOneUse() && !GEP->hasAllConstantIndices() &&
829-
!GEP->getSourceElementType()->isIntegerTy(8)) {
830-
replaceInstUsesWith(*Inst,
831-
Builder.CreateGEP(Builder.getInt8Ty(),
832-
GEP->getPointerOperand(),
833-
Offset, "", GEPsInBounds));
834-
eraseInstFromFunction(*Inst);
835-
}
836-
return Offset;
837-
};
838-
839819
// ((gep Ptr, OFFSET1) cmp (gep Ptr, OFFSET2) ---> (OFFSET1 cmp OFFSET2)
840-
Value *L = EmitGEPOffsetAndRewrite(GEPLHS);
841-
Value *R = EmitGEPOffsetAndRewrite(GEPRHS);
820+
Value *L = EmitGEPOffset(GEPLHS, /*RewriteGEP=*/true);
821+
Value *R = EmitGEPOffset(GEPRHS, /*RewriteGEP=*/true);
842822
return new ICmpInst(ICmpInst::getSignedPredicate(Cond), L, R);
843823
}
844824
}

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
376376
}
377377
}
378378

379-
Value *EmitGEPOffset(User *GEP);
379+
Value *EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP = false);
380380
Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
381381
Instruction *foldBitcastExtElt(ExtractElementInst &ExtElt);
382382
Instruction *foldCastedBitwiseLogic(BinaryOperator &I);

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,26 @@ bool InstCombiner::isValidAddrSpaceCast(unsigned FromAS, unsigned ToAS) const {
190190
return TTI.isValidAddrSpaceCast(FromAS, ToAS);
191191
}
192192

193-
Value *InstCombinerImpl::EmitGEPOffset(User *GEP) {
194-
return llvm::emitGEPOffset(&Builder, DL, GEP);
193+
Value *InstCombinerImpl::EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP) {
194+
if (!RewriteGEP)
195+
return llvm::emitGEPOffset(&Builder, DL, GEP);
196+
197+
IRBuilderBase::InsertPointGuard Guard(Builder);
198+
auto *Inst = dyn_cast<Instruction>(GEP);
199+
if (Inst)
200+
Builder.SetInsertPoint(Inst);
201+
202+
Value *Offset = EmitGEPOffset(GEP);
203+
// If a non-trivial GEP has other uses, rewrite it to avoid duplicating
204+
// the offset arithmetic.
205+
if (Inst && !GEP->hasOneUse() && !GEP->hasAllConstantIndices() &&
206+
!GEP->getSourceElementType()->isIntegerTy(8)) {
207+
replaceInstUsesWith(
208+
*Inst, Builder.CreateGEP(Builder.getInt8Ty(), GEP->getPointerOperand(),
209+
Offset, "", GEP->isInBounds()));
210+
eraseInstFromFunction(*Inst);
211+
}
212+
return Offset;
195213
}
196214

197215
/// Legal integers and common types are considered desirable. This is used to

0 commit comments

Comments
 (0)