Skip to content

Commit 111ac69

Browse files
authored
[AggressiveInstCombine] Check GEP nusw, not inbounds (#139708)
1 parent b936112 commit 111ac69

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ static bool tryToRecognizeTableBasedCttz(Instruction &I) {
530530
return false;
531531

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

536536
if (!GEP->getSourceElementType()->isArrayTy())
@@ -899,7 +899,7 @@ getStrideAndModOffsetOfGEP(Value *PtrOp, const DataLayout &DL) {
899899

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

905905
if (!Stride)

llvm/test/Transforms/AggressiveInstCombine/lower-table-based-cttz-basics.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,28 @@ entry:
113113
ret i32 %conv
114114
}
115115

116+
define i32 @ctz1_nusw(i32 %x) {
117+
; CHECK-LABEL: @ctz1_nusw(
118+
; CHECK-NEXT: entry:
119+
; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
120+
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X]], 0
121+
; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 0, i32 [[TMP0]]
122+
; CHECK-NEXT: [[TMP3:%.*]] = trunc i32 [[TMP2]] to i8
123+
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[TMP3]] to i32
124+
; CHECK-NEXT: ret i32 [[CONV]]
125+
;
126+
entry:
127+
%sub = sub i32 0, %x
128+
%and = and i32 %sub, %x
129+
%mul = mul i32 %and, 125613361
130+
%shr = lshr i32 %mul, 27
131+
%idxprom = zext i32 %shr to i64
132+
%arrayidx = getelementptr nusw [32 x i8], ptr @ctz7.table, i64 0, i64 %idxprom
133+
%0 = load i8, ptr %arrayidx, align 1
134+
%conv = zext i8 %0 to i32
135+
ret i32 %conv
136+
}
137+
116138
@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
117139

118140
define i32 @ctz2(i32 %x) {

llvm/test/Transforms/AggressiveInstCombine/patterned-load.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ define i32 @gep_load_i32_align2_const_offset_wrap(i64 %idx){
140140
ret i32 %3
141141
}
142142

143+
define i32 @gep_load_i32_align2_const_offset_nusw(i64 %idx){
144+
; LE-LABEL: @gep_load_i32_align2_const_offset_nusw(
145+
; LE-NEXT: ret i32 65537
146+
;
147+
; BE-LABEL: @gep_load_i32_align2_const_offset_nusw(
148+
; BE-NEXT: ret i32 16777472
149+
;
150+
%1 = getelementptr nusw i16, ptr @constarray2, i64 -2
151+
%2 = getelementptr nusw [3 x i16], ptr %1, i64 %idx
152+
%3 = load i32, ptr %2, align 2
153+
ret i32 %3
154+
}
155+
156+
143157
define i32 @inbounds_gep_i32_load_i32_const_ptr_array(i64 %idx){
144158
; CHECK-LABEL: @inbounds_gep_i32_load_i32_const_ptr_array(
145159
; CHECK-NEXT: ret i32 42

0 commit comments

Comments
 (0)