Skip to content

Commit 2019553

Browse files
committed
[InstCombine] Extract EmitGEPOffsets() helper (NFC)
Extract a reusable helper for emitting a sum of multiple GEP offsets.
1 parent 6fc8ec7 commit 2019553

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,27 +2131,8 @@ Value *InstCombinerImpl::OptimizePointerDifference(Value *LHS, Value *RHS,
21312131
bool RewriteGEPs = !Base.LHSGEPs.empty() && !Base.RHSGEPs.empty();
21322132

21332133
Type *IdxTy = DL.getIndexType(LHS->getType());
2134-
auto EmitOffsetFromBase = [&](ArrayRef<GEPOperator *> GEPs,
2135-
GEPNoWrapFlags NW) -> Value * {
2136-
Value *Sum = nullptr;
2137-
for (GEPOperator *GEP : reverse(GEPs)) {
2138-
Value *Offset = EmitGEPOffset(GEP, RewriteGEPs);
2139-
if (Offset->getType() != IdxTy)
2140-
Offset = Builder.CreateVectorSplat(
2141-
cast<VectorType>(IdxTy)->getElementCount(), Offset);
2142-
if (Sum)
2143-
Sum = Builder.CreateAdd(Sum, Offset, "", NW.hasNoUnsignedWrap(),
2144-
NW.isInBounds());
2145-
else
2146-
Sum = Offset;
2147-
}
2148-
if (!Sum)
2149-
return Constant::getNullValue(IdxTy);
2150-
return Sum;
2151-
};
2152-
2153-
Value *Result = EmitOffsetFromBase(Base.LHSGEPs, Base.LHSNW);
2154-
Value *Offset2 = EmitOffsetFromBase(Base.RHSGEPs, Base.RHSNW);
2134+
Value *Result = EmitGEPOffsets(Base.LHSGEPs, Base.LHSNW, IdxTy, RewriteGEPs);
2135+
Value *Offset2 = EmitGEPOffsets(Base.RHSGEPs, Base.RHSNW, IdxTy, RewriteGEPs);
21552136

21562137
// If this is a single inbounds GEP and the original sub was nuw,
21572138
// then the final multiplication is also nuw.

llvm/lib/Transforms/InstCombine/InstCombineInternal.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
378378
}
379379

380380
Value *EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP = false);
381+
/// Emit sum of multiple GEP offsets. The GEPs are processed in reverse
382+
/// order.
383+
Value *EmitGEPOffsets(ArrayRef<GEPOperator *> GEPs, GEPNoWrapFlags NW,
384+
Type *IdxTy, bool RewriteGEPs);
381385
Instruction *scalarizePHI(ExtractElementInst &EI, PHINode *PN);
382386
Instruction *foldBitcastExtElt(ExtractElementInst &ExtElt);
383387
Instruction *foldCastedBitwiseLogic(BinaryOperator &I);

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,26 @@ Value *InstCombinerImpl::EmitGEPOffset(GEPOperator *GEP, bool RewriteGEP) {
217217
return Offset;
218218
}
219219

220+
Value *InstCombinerImpl::EmitGEPOffsets(ArrayRef<GEPOperator *> GEPs,
221+
GEPNoWrapFlags NW, Type *IdxTy,
222+
bool RewriteGEPs) {
223+
Value *Sum = nullptr;
224+
for (GEPOperator *GEP : reverse(GEPs)) {
225+
Value *Offset = EmitGEPOffset(GEP, RewriteGEPs);
226+
if (Offset->getType() != IdxTy)
227+
Offset = Builder.CreateVectorSplat(
228+
cast<VectorType>(IdxTy)->getElementCount(), Offset);
229+
if (Sum)
230+
Sum = Builder.CreateAdd(Sum, Offset, "", NW.hasNoUnsignedWrap(),
231+
NW.isInBounds());
232+
else
233+
Sum = Offset;
234+
}
235+
if (!Sum)
236+
return Constant::getNullValue(IdxTy);
237+
return Sum;
238+
}
239+
220240
/// Legal integers and common types are considered desirable. This is used to
221241
/// avoid creating instructions with types that may not be supported well by the
222242
/// the backend.

0 commit comments

Comments
 (0)