Skip to content

Commit 02678bc

Browse files
committed
[Local] Merge constant / non-constant code paths (NFC)
Let IRBuilder perform the constant folding. Avoids some uses of ConstantExpr.
1 parent 8301e48 commit 02678bc

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

llvm/lib/Analysis/Local.cpp

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
2828
// If the GEP is inbounds, we know that none of the addressing operations will
2929
// overflow in a signed sense.
3030
bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
31+
auto AddOffset = [&](Value *Offset) {
32+
if (Result)
33+
Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
34+
false /*NUW*/, isInBounds /*NSW*/);
35+
else
36+
Result = Offset;
37+
};
3138

3239
// Build a mask for high order bits.
3340
unsigned IntPtrWidth = IntIdxTy->getScalarType()->getIntegerBitWidth();
@@ -39,7 +46,6 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
3946
++i, ++GTI) {
4047
Value *Op = *i;
4148
uint64_t Size = DL.getTypeAllocSize(GTI.getIndexedType()) & PtrSizeMask;
42-
Value *Offset;
4349
if (Constant *OpC = dyn_cast<Constant>(Op)) {
4450
if (OpC->isZeroValue())
4551
continue;
@@ -51,42 +57,26 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
5157
if (!Size)
5258
continue;
5359

54-
Offset = ConstantInt::get(IntIdxTy, Size);
55-
} else {
56-
// Splat the constant if needed.
57-
if (IntIdxTy->isVectorTy() && !OpC->getType()->isVectorTy())
58-
OpC = ConstantVector::getSplat(
59-
cast<VectorType>(IntIdxTy)->getElementCount(), OpC);
60-
61-
Constant *Scale = ConstantInt::get(IntIdxTy, Size);
62-
Constant *OC =
63-
ConstantExpr::getIntegerCast(OpC, IntIdxTy, true /*SExt*/);
64-
Offset =
65-
ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/);
66-
}
67-
} else {
68-
// Splat the index if needed.
69-
if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
70-
Op = Builder->CreateVectorSplat(
71-
cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op);
72-
73-
// Convert to correct type.
74-
if (Op->getType() != IntIdxTy)
75-
Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
76-
if (Size != 1) {
77-
// We'll let instcombine(mul) convert this to a shl if possible.
78-
Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size),
79-
GEP->getName() + ".idx", false /*NUW*/,
80-
isInBounds /*NSW*/);
60+
AddOffset(ConstantInt::get(IntIdxTy, Size));
61+
continue;
8162
}
82-
Offset = Op;
8363
}
8464

85-
if (Result)
86-
Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
87-
false /*NUW*/, isInBounds /*NSW*/);
88-
else
89-
Result = Offset;
65+
// Splat the index if needed.
66+
if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
67+
Op = Builder->CreateVectorSplat(
68+
cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op);
69+
70+
// Convert to correct type.
71+
if (Op->getType() != IntIdxTy)
72+
Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
73+
if (Size != 1) {
74+
// We'll let instcombine(mul) convert this to a shl if possible.
75+
Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size),
76+
GEP->getName() + ".idx", false /*NUW*/,
77+
isInBounds /*NSW*/);
78+
}
79+
AddOffset(Op);
9080
}
9181
return Result ? Result : Constant::getNullValue(IntIdxTy);
9282
}

0 commit comments

Comments
 (0)