Skip to content

Commit a7edc95

Browse files
committed
[IR] Optimize stripAndAccumulateConstantOffsets() for common case (NFC)
For the common case where we don't have bit width changing address space casts, we can directly call accumulateConstantOffset() on the original Offset. Skip the bit width reconciliation logic in that case.
1 parent 5de443a commit a7edc95

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

llvm/lib/IR/Value.cpp

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -747,28 +747,34 @@ const Value *Value::stripAndAccumulateConstantOffsets(
747747
// means when we construct GEPOffset, we need to use the size
748748
// of GEP's pointer type rather than the size of the original
749749
// pointer type.
750-
APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0);
751-
if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis))
752-
return V;
753-
754-
// Stop traversal if the pointer offset wouldn't fit in the bit-width
755-
// provided by the Offset argument. This can happen due to AddrSpaceCast
756-
// stripping.
757-
if (GEPOffset.getSignificantBits() > BitWidth)
758-
return V;
759-
760-
// External Analysis can return a result higher/lower than the value
761-
// represents. We need to detect overflow/underflow.
762-
APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth);
763-
if (!ExternalAnalysis) {
764-
Offset += GEPOffsetST;
750+
unsigned CurBitWidth = DL.getIndexTypeSizeInBits(V->getType());
751+
if (CurBitWidth == BitWidth) {
752+
if (!GEP->accumulateConstantOffset(DL, Offset, ExternalAnalysis))
753+
return V;
765754
} else {
766-
bool Overflow = false;
767-
APInt OldOffset = Offset;
768-
Offset = Offset.sadd_ov(GEPOffsetST, Overflow);
769-
if (Overflow) {
770-
Offset = OldOffset;
755+
APInt GEPOffset(CurBitWidth, 0);
756+
if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis))
757+
return V;
758+
759+
// Stop traversal if the pointer offset wouldn't fit in the bit-width
760+
// provided by the Offset argument. This can happen due to AddrSpaceCast
761+
// stripping.
762+
if (GEPOffset.getSignificantBits() > BitWidth)
771763
return V;
764+
765+
// External Analysis can return a result higher/lower than the value
766+
// represents. We need to detect overflow/underflow.
767+
APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth);
768+
if (!ExternalAnalysis) {
769+
Offset += GEPOffsetST;
770+
} else {
771+
bool Overflow = false;
772+
APInt OldOffset = Offset;
773+
Offset = Offset.sadd_ov(GEPOffsetST, Overflow);
774+
if (Overflow) {
775+
Offset = OldOffset;
776+
return V;
777+
}
772778
}
773779
}
774780
V = GEP->getPointerOperand();

0 commit comments

Comments
 (0)