Skip to content

Commit 51ec9b0

Browse files
committed
Fixup: Also handle "nusw nuw"
1 parent cc91c58 commit 51ec9b0

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,17 +3119,14 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
31193119
// These rewrites is trying to preserve inbounds/nuw attributes. So we want to
31203120
// do this after having tried to derive "nuw" above.
31213121
if (GEP.getNumIndices() == 1) {
3122+
// Given (gep p, x+y) we want to determine the common nowrap flags for both
3123+
// gep if transforming into (gep (gep p, x), y).
31223124
auto GetPreservedNoWrapFlags = [&](bool AddIsNUW) {
3123-
// Preserve "inbounds nuw" if the original gep is "inbounds nuw", and the
3124-
// add is "nuw". Preserve "nuw" if the original gep is "nuw", and the add
3125-
// is "nuw".
3126-
GEPNoWrapFlags Flags = GEPNoWrapFlags::none();
3127-
if (GEP.hasNoUnsignedWrap() && AddIsNUW) {
3128-
Flags |= GEPNoWrapFlags::noUnsignedWrap();
3129-
if (GEP.isInBounds())
3130-
Flags |= GEPNoWrapFlags::inBounds();
3131-
}
3132-
return Flags;
3125+
// We can preserve both "inbounds nuw", "nusw nuw" and "nuw" if we know
3126+
// that x + y does not have unsigned wrap.
3127+
if (GEP.hasNoUnsignedWrap() && AddIsNUW)
3128+
return GEP.getNoWrapFlags();
3129+
return GEPNoWrapFlags::none();
31333130
};
31343131

31353132
// Try to replace ADD + GEP with GEP + GEP.

llvm/test/Transforms/InstCombine/array.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,11 @@ define ptr @gep_inbounds_add_nuw(ptr %ptr, i64 %a, i64 %b) {
134134
ret ptr %gep
135135
}
136136

137-
; FIXME: Preserve "nusw nuw".
138137
define ptr @gep_inbounds_add_nusw_nuw(ptr %ptr, i64 %a, i64 %b) {
139138
; CHECK-LABEL: define ptr @gep_inbounds_add_nusw_nuw(
140139
; CHECK-SAME: ptr [[PTR:%.*]], i64 [[A:%.*]], i64 [[B:%.*]]) {
141-
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr nuw i32, ptr [[PTR]], i64 [[A]]
142-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nuw i32, ptr [[TMP1]], i64 [[B]]
140+
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr nusw nuw i32, ptr [[PTR]], i64 [[A]]
141+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr nusw nuw i32, ptr [[TMP1]], i64 [[B]]
143142
; CHECK-NEXT: ret ptr [[GEP]]
144143
;
145144
%add = add nuw i64 %a, %b

0 commit comments

Comments
 (0)