Skip to content

Commit 802f790

Browse files
committed
[AggressiveInstCombine] Check GEP nusw, not inbounds
1 parent a1a4836 commit 802f790

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
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())
@@ -843,7 +843,7 @@ getStrideAndModOffsetOfGEP(Value *PtrOp, const DataLayout &DL) {
843843

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

849849
if (!Stride)

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,10 @@ entry:
116116
define i32 @ctz1_nusw(i32 %x) {
117117
; CHECK-LABEL: @ctz1_nusw(
118118
; CHECK-NEXT: entry:
119-
; CHECK-NEXT: [[SUB:%.*]] = sub i32 0, [[X:%.*]]
120-
; CHECK-NEXT: [[AND:%.*]] = and i32 [[SUB]], [[X]]
121-
; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[AND]], 125613361
122-
; CHECK-NEXT: [[SHR:%.*]] = lshr i32 [[MUL]], 27
123-
; CHECK-NEXT: [[IDXPROM:%.*]] = zext i32 [[SHR]] to i64
124-
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr nusw [32 x i8], ptr @ctz7.table, i64 0, i64 [[IDXPROM]]
125-
; CHECK-NEXT: [[TMP3:%.*]] = load i8, ptr [[ARRAYIDX]], align 1
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
126123
; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[TMP3]] to i32
127124
; CHECK-NEXT: ret i32 [[CONV]]
128125
;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ define i32 @gep_load_i32_align2_const_offset_wrap(i64 %idx){
141141
}
142142

143143
define i32 @gep_load_i32_align2_const_offset_nusw(i64 %idx){
144-
; CHECK-LABEL: @gep_load_i32_align2_const_offset_nusw(
145-
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr nusw i16, ptr @constarray2, i64 -2
146-
; CHECK-NEXT: [[TMP2:%.*]] = getelementptr nusw [3 x i16], ptr [[TMP1]], i64 [[IDX:%.*]]
147-
; CHECK-NEXT: [[TMP3:%.*]] = load i32, ptr [[TMP2]], align 2
148-
; CHECK-NEXT: ret i32 [[TMP3]]
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
149149
;
150150
%1 = getelementptr nusw i16, ptr @constarray2, i64 -2
151151
%2 = getelementptr nusw [3 x i16], ptr %1, i64 %idx

0 commit comments

Comments
 (0)