Skip to content

Commit

Permalink
[Local] Merge constant / non-constant code paths (NFC)
Browse files Browse the repository at this point in the history
Let IRBuilder perform the constant folding. Avoids some uses of
ConstantExpr.
  • Loading branch information
nikic committed Oct 11, 2023
1 parent 8301e48 commit 02678bc
Showing 1 changed file with 24 additions and 34 deletions.
58 changes: 24 additions & 34 deletions llvm/lib/Analysis/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ Value *llvm::emitGEPOffset(IRBuilderBase *Builder, const DataLayout &DL,
// If the GEP is inbounds, we know that none of the addressing operations will
// overflow in a signed sense.
bool isInBounds = GEPOp->isInBounds() && !NoAssumptions;
auto AddOffset = [&](Value *Offset) {
if (Result)
Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
false /*NUW*/, isInBounds /*NSW*/);
else
Result = Offset;
};

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

Offset = ConstantInt::get(IntIdxTy, Size);
} else {
// Splat the constant if needed.
if (IntIdxTy->isVectorTy() && !OpC->getType()->isVectorTy())
OpC = ConstantVector::getSplat(
cast<VectorType>(IntIdxTy)->getElementCount(), OpC);

Constant *Scale = ConstantInt::get(IntIdxTy, Size);
Constant *OC =
ConstantExpr::getIntegerCast(OpC, IntIdxTy, true /*SExt*/);
Offset =
ConstantExpr::getMul(OC, Scale, false /*NUW*/, isInBounds /*NSW*/);
}
} else {
// Splat the index if needed.
if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
Op = Builder->CreateVectorSplat(
cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op);

// Convert to correct type.
if (Op->getType() != IntIdxTy)
Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
if (Size != 1) {
// We'll let instcombine(mul) convert this to a shl if possible.
Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size),
GEP->getName() + ".idx", false /*NUW*/,
isInBounds /*NSW*/);
AddOffset(ConstantInt::get(IntIdxTy, Size));
continue;
}
Offset = Op;
}

if (Result)
Result = Builder->CreateAdd(Result, Offset, GEP->getName() + ".offs",
false /*NUW*/, isInBounds /*NSW*/);
else
Result = Offset;
// Splat the index if needed.
if (IntIdxTy->isVectorTy() && !Op->getType()->isVectorTy())
Op = Builder->CreateVectorSplat(
cast<FixedVectorType>(IntIdxTy)->getNumElements(), Op);

// Convert to correct type.
if (Op->getType() != IntIdxTy)
Op = Builder->CreateIntCast(Op, IntIdxTy, true, Op->getName() + ".c");
if (Size != 1) {
// We'll let instcombine(mul) convert this to a shl if possible.
Op = Builder->CreateMul(Op, ConstantInt::get(IntIdxTy, Size),
GEP->getName() + ".idx", false /*NUW*/,
isInBounds /*NSW*/);
}
AddOffset(Op);
}
return Result ? Result : Constant::getNullValue(IntIdxTy);
}

0 comments on commit 02678bc

Please sign in to comment.