Skip to content

[InstCombine] Don't handle non-canonical index type in icmp of load fold #151346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(
LaterIndices.push_back(IdxVal);
}

Value *Idx = GEP->getOperand(2);
// If the index type is non-canonical, wait for it to be canonicalized.
if (Idx->getType() != DL.getIndexType(GEP->getType()))
return nullptr;

enum { Overdefined = -3, Undefined = -2 };

// Variables for our state machines.
Expand Down Expand Up @@ -290,17 +295,6 @@ Instruction *InstCombinerImpl::foldCmpLoadFromIndexedGlobal(

// Now that we've scanned the entire array, emit our new comparison(s). We
// order the state machines in complexity of the generated code.
Value *Idx = GEP->getOperand(2);

// If the index is larger than the pointer offset size of the target, truncate
// the index down like the GEP would do implicitly. We don't have to do this
// for an inbounds GEP because the index can't be out of range.
if (!GEP->isInBounds()) {
Type *PtrIdxTy = DL.getIndexType(GEP->getType());
unsigned OffsetSize = PtrIdxTy->getIntegerBitWidth();
if (Idx->getType()->getPrimitiveSizeInBits().getFixedValue() > OffsetSize)
Idx = Builder.CreateTrunc(Idx, PtrIdxTy);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It reminds me that we can add nsw/nuw to the trunc when canonicalizing the index type: https://alive2.llvm.org/ce/z/niXbox

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

// If inbounds keyword is not present, Idx * ElementSize can overflow.
// Let's assume that ElementSize is 2 and the wanted value is at offset 0.
Expand Down