Skip to content

Commit f398f2a

Browse files
authored
[InstCombine] Preserve GEP no-wrap flags (#141113)
1 parent b81170e commit f398f2a

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ Instruction *InstCombinerImpl::visitExtractElementInst(ExtractElementInst &EI) {
537537

538538
GetElementPtrInst *NewGEP = GetElementPtrInst::Create(
539539
GEP->getSourceElementType(), NewPtr, NewOps);
540-
NewGEP->setIsInBounds(GEP->isInBounds());
540+
NewGEP->setNoWrapFlags(GEP->getNoWrapFlags());
541541
return NewGEP;
542542
}
543543
}
@@ -1982,7 +1982,7 @@ static Value *buildNew(Instruction *I, ArrayRef<Value*> NewOps,
19821982
ArrayRef<Value*> Idx = NewOps.slice(1);
19831983
return Builder.CreateGEP(cast<GEPOperator>(I)->getSourceElementType(),
19841984
Ptr, Idx, "",
1985-
cast<GEPOperator>(I)->isInBounds());
1985+
cast<GEPOperator>(I)->getNoWrapFlags());
19861986
}
19871987
}
19881988
llvm_unreachable("failed to rebuild vector instructions");

llvm/test/Transforms/InstCombine/gep-vector-indices.ll

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ define ptr @vector_splat_indices_v2i64_ext0(ptr %a) {
1212
ret ptr %res
1313
}
1414

15-
define ptr @vector_splat_indices_nxv2i64_ext0(ptr %a) {
16-
; CHECK-LABEL: @vector_splat_indices_nxv2i64_ext0(
15+
define ptr @vector_splat_indices_nxv2i64_ext0_inbounds(ptr %a) {
16+
; CHECK-LABEL: @vector_splat_indices_nxv2i64_ext0_inbounds(
1717
; CHECK-NEXT: [[RES:%.*]] = getelementptr inbounds nuw i8, ptr [[A:%.*]], i64 16
1818
; CHECK-NEXT: ret ptr [[RES]]
1919
;
@@ -24,6 +24,30 @@ define ptr @vector_splat_indices_nxv2i64_ext0(ptr %a) {
2424
ret ptr %res
2525
}
2626

27+
define ptr @vector_splat_indices_nxv2i64_ext0_nuw(ptr %a) {
28+
; CHECK-LABEL: @vector_splat_indices_nxv2i64_ext0_nuw(
29+
; CHECK-NEXT: [[RES:%.*]] = getelementptr nuw i8, ptr [[A:%.*]], i64 16
30+
; CHECK-NEXT: ret ptr [[RES]]
31+
;
32+
%tmp = insertelement <vscale x 2 x i64> poison, i64 4, i32 0
33+
%splatof4 = shufflevector <vscale x 2 x i64> %tmp, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
34+
%gep = getelementptr nuw i32, ptr %a, <vscale x 2 x i64> %splatof4
35+
%res = extractelement <vscale x 2 x ptr> %gep, i32 0
36+
ret ptr %res
37+
}
38+
39+
define ptr @vector_splat_indices_nxv2i64_ext0_nusw(ptr %a) {
40+
; CHECK-LABEL: @vector_splat_indices_nxv2i64_ext0_nusw(
41+
; CHECK-NEXT: [[RES:%.*]] = getelementptr nusw nuw i8, ptr [[A:%.*]], i64 16
42+
; CHECK-NEXT: ret ptr [[RES]]
43+
;
44+
%tmp = insertelement <vscale x 2 x i64> poison, i64 4, i32 0
45+
%splatof4 = shufflevector <vscale x 2 x i64> %tmp, <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
46+
%gep = getelementptr nusw i32, ptr %a, <vscale x 2 x i64> %splatof4
47+
%res = extractelement <vscale x 2 x ptr> %gep, i32 0
48+
ret ptr %res
49+
}
50+
2751
define ptr @vector_indices_v2i64_ext0(ptr %a, <2 x i64> %indices) {
2852
; CHECK-LABEL: @vector_indices_v2i64_ext0(
2953
; CHECK-NEXT: [[TMP1:%.*]] = extractelement <2 x i64> [[INDICES:%.*]], i64 0

llvm/test/Transforms/InstCombine/vec_shuffle.ll

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,45 @@ define <1 x ptr> @shuffle_gep(ptr %x1, ptr %x2) {
261261
ret <1 x ptr> %ret
262262
}
263263

264+
define <1 x ptr> @shuffle_gep_inbounds(ptr %x1, ptr %x2) {
265+
; CHECK-LABEL: @shuffle_gep_inbounds(
266+
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <1 x ptr> poison, ptr [[X2:%.*]], i64 0
267+
; CHECK-NEXT: [[RET:%.*]] = getelementptr inbounds i8, <1 x ptr> [[TMP1]], i64 5
268+
; CHECK-NEXT: ret <1 x ptr> [[RET]]
269+
;
270+
%ins.1 = insertelement <2 x ptr> poison, ptr %x1, i32 0
271+
%ins.2 = insertelement <2 x ptr> %ins.1, ptr %x2, i32 1
272+
%gep = getelementptr inbounds i8, <2 x ptr> %ins.2, i64 5
273+
%ret = shufflevector <2 x ptr> %gep, <2 x ptr> poison, <1 x i32> <i32 1>
274+
ret <1 x ptr> %ret
275+
}
276+
277+
define <1 x ptr> @shuffle_gep_nuw(ptr %x1, ptr %x2) {
278+
; CHECK-LABEL: @shuffle_gep_nuw(
279+
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <1 x ptr> poison, ptr [[X2:%.*]], i64 0
280+
; CHECK-NEXT: [[RET:%.*]] = getelementptr nuw i8, <1 x ptr> [[TMP1]], i64 5
281+
; CHECK-NEXT: ret <1 x ptr> [[RET]]
282+
;
283+
%ins.1 = insertelement <2 x ptr> poison, ptr %x1, i32 0
284+
%ins.2 = insertelement <2 x ptr> %ins.1, ptr %x2, i32 1
285+
%gep = getelementptr nuw i8, <2 x ptr> %ins.2, i64 5
286+
%ret = shufflevector <2 x ptr> %gep, <2 x ptr> poison, <1 x i32> <i32 1>
287+
ret <1 x ptr> %ret
288+
}
289+
290+
define <1 x ptr> @shuffle_gep_nusw(ptr %x1, ptr %x2) {
291+
; CHECK-LABEL: @shuffle_gep_nusw(
292+
; CHECK-NEXT: [[TMP1:%.*]] = insertelement <1 x ptr> poison, ptr [[X2:%.*]], i64 0
293+
; CHECK-NEXT: [[RET:%.*]] = getelementptr nusw i8, <1 x ptr> [[TMP1]], i64 5
294+
; CHECK-NEXT: ret <1 x ptr> [[RET]]
295+
;
296+
%ins.1 = insertelement <2 x ptr> poison, ptr %x1, i32 0
297+
%ins.2 = insertelement <2 x ptr> %ins.1, ptr %x2, i32 1
298+
%gep = getelementptr nusw i8, <2 x ptr> %ins.2, i64 5
299+
%ret = shufflevector <2 x ptr> %gep, <2 x ptr> poison, <1 x i32> <i32 1>
300+
ret <1 x ptr> %ret
301+
}
302+
264303
; Increasing length of vector ops is not a good canonicalization.
265304

266305
define <3 x i32> @add_wider(i32 %y, i32 %z) {

0 commit comments

Comments
 (0)