Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[AggressiveInstCombine] Check GEP nusw, not inbounds #139708

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 2 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ static bool tryToRecognizeTableBasedCttz(Instruction &I) {
return false;

GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getPointerOperand());
if (!GEP || !GEP->isInBounds() || GEP->getNumIndices() != 2)
if (!GEP || !GEP->hasNoUnsignedSignedWrap() || GEP->getNumIndices() != 2)
return false;

if (!GEP->getSourceElementType()->isArrayTy())
Expand Down Expand Up @@ -843,7 +843,7 @@ getStrideAndModOffsetOfGEP(Value *PtrOp, const DataLayout &DL) {

for (auto [V, Scale] : VarOffsets) {
// Only keep a power of two factor for non-inbounds
if (!GEP->isInBounds())
if (!GEP->hasNoUnsignedSignedWrap())
Scale = APInt::getOneBitSet(Scale.getBitWidth(), Scale.countr_zero());

if (!Stride)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ entry:
ret i32 %conv
}

define i32 @ctz1_nusw(i32 %x) {
; CHECK-LABEL: @ctz1_nusw(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X]], 0
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
; CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[TMP3]] to i32
; CHECK-NEXT: ret i32 [[CONV]]
;
entry:
%sub = sub i32 0, %x
%and = and i32 %sub, %x
%mul = mul i32 %and, 125613361
%shr = lshr i32 %mul, 27
%idxprom = zext i32 %shr to i64
%arrayidx = getelementptr nusw [32 x i8], ptr @ctz7.table, i64 0, i64 %idxprom
%0 = load i8, ptr %arrayidx, align 1
%conv = zext i8 %0 to i32
ret i32 %conv
}

@ctz2.table = internal unnamed_addr constant [64 x i16] [i16 32, i16 0, i16 1, i16 12, i16 2, i16 6, i16 0, i16 13, i16 3, i16 0, i16 7, i16 0, i16 0, i16 0, i16 0, i16 14, i16 10, i16 4, i16 0, i16 0, i16 8, i16 0, i16 0, i16 25, i16 0, i16 0, i16 0, i16 0, i16 0, i16 21, i16 27, i16 15, i16 31, i16 11, i16 5, i16 0, i16 0, i16 0, i16 0, i16 0, i16 9, i16 0, i16 0, i16 24, i16 0, i16 0, i16 20, i16 26, i16 30, i16 0, i16 0, i16 0, i16 0, i16 23, i16 0, i16 19, i16 29, i16 0, i16 22, i16 18, i16 28, i16 17, i16 16, i16 0], align 2

define i32 @ctz2(i32 %x) {
Expand Down
14 changes: 14 additions & 0 deletions 14 llvm/test/Transforms/AggressiveInstCombine/patterned-load.ll
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ define i32 @gep_load_i32_align2_const_offset_wrap(i64 %idx){
ret i32 %3
}

define i32 @gep_load_i32_align2_const_offset_nusw(i64 %idx){
; LE-LABEL: @gep_load_i32_align2_const_offset_nusw(
; LE-NEXT: ret i32 65537
;
; BE-LABEL: @gep_load_i32_align2_const_offset_nusw(
; BE-NEXT: ret i32 16777472
;
%1 = getelementptr nusw i16, ptr @constarray2, i64 -2
%2 = getelementptr nusw [3 x i16], ptr %1, i64 %idx
%3 = load i32, ptr %2, align 2
ret i32 %3
}


define i32 @inbounds_gep_i32_load_i32_const_ptr_array(i64 %idx){
; CHECK-LABEL: @inbounds_gep_i32_load_i32_const_ptr_array(
; CHECK-NEXT: ret i32 42
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.