Skip to content

Commit 2adcec7

Browse files
authored
[InstCombine] Simplify with.overflow intrinsics with assumption information (#84016)
This patch recognizes never-overflow assumptions generated by rustc to improve the codegen. Please refer to rust-lang/hashbrown#509 for more details. Closes rust-lang/hashbrown#509 Closes #80637
1 parent 1d15541 commit 2adcec7

File tree

2 files changed

+122
-5
lines changed

2 files changed

+122
-5
lines changed

Diff for: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,35 @@ InstCombinerImpl::foldIntrinsicWithOverflowCommon(IntrinsicInst *II) {
839839
if (OptimizeOverflowCheck(WO->getBinaryOp(), WO->isSigned(), WO->getLHS(),
840840
WO->getRHS(), *WO, OperationResult, OverflowResult))
841841
return createOverflowTuple(WO, OperationResult, OverflowResult);
842+
843+
// See whether we can optimize the overflow check with assumption information.
844+
for (User *U : WO->users()) {
845+
if (!match(U, m_ExtractValue<1>(m_Value())))
846+
continue;
847+
848+
for (auto &AssumeVH : AC.assumptionsFor(U)) {
849+
if (!AssumeVH)
850+
continue;
851+
CallInst *I = cast<CallInst>(AssumeVH);
852+
if (!match(I->getArgOperand(0), m_Not(m_Specific(U))))
853+
continue;
854+
if (!isValidAssumeForContext(I, II, /*DT=*/nullptr,
855+
/*AllowEphemerals=*/true))
856+
continue;
857+
Value *Result =
858+
Builder.CreateBinOp(WO->getBinaryOp(), WO->getLHS(), WO->getRHS());
859+
Result->takeName(WO);
860+
if (auto *Inst = dyn_cast<Instruction>(Result)) {
861+
if (WO->isSigned())
862+
Inst->setHasNoSignedWrap();
863+
else
864+
Inst->setHasNoUnsignedWrap();
865+
}
866+
return createOverflowTuple(WO, Result,
867+
ConstantInt::getFalse(U->getType()));
868+
}
869+
}
870+
842871
return nullptr;
843872
}
844873

Diff for: llvm/test/Transforms/InstCombine/overflow.ll

+93-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ define i32 @test1(i32 %a, i32 %b) nounwind ssp {
1111
; CHECK-NEXT: [[TMP0:%.*]] = extractvalue { i32, i1 } [[SADD]], 1
1212
; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
1313
; CHECK: if.then:
14-
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR2:[0-9]+]]
14+
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR3:[0-9]+]]
1515
; CHECK-NEXT: br label [[IF_END]]
1616
; CHECK: if.end:
1717
; CHECK-NEXT: [[SADD_RESULT:%.*]] = extractvalue { i32, i1 } [[SADD]], 0
@@ -49,7 +49,7 @@ define i32 @test2(i32 %a, i32 %b, ptr %P) nounwind ssp {
4949
; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt i64 [[ADD_OFF]], 4294967295
5050
; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
5151
; CHECK: if.then:
52-
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR2]]
52+
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR3]]
5353
; CHECK-NEXT: br label [[IF_END]]
5454
; CHECK: if.end:
5555
; CHECK-NEXT: [[CONV9:%.*]] = trunc i64 [[ADD]] to i32
@@ -86,7 +86,7 @@ define i64 @test3(i32 %a, i32 %b) nounwind ssp {
8686
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[TMP0]], -4294967296
8787
; CHECK-NEXT: br i1 [[TMP1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
8888
; CHECK: if.then:
89-
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR2]]
89+
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR3]]
9090
; CHECK-NEXT: br label [[IF_END]]
9191
; CHECK: if.end:
9292
; CHECK-NEXT: ret i64 [[ADD]]
@@ -116,7 +116,7 @@ define zeroext i8 @test4(i8 signext %a, i8 signext %b) nounwind ssp {
116116
; CHECK-NEXT: [[CMP:%.*]] = extractvalue { i8, i1 } [[SADD]], 1
117117
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
118118
; CHECK: if.then:
119-
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR2]]
119+
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR3]]
120120
; CHECK-NEXT: unreachable
121121
; CHECK: if.end:
122122
; CHECK-NEXT: [[SADD_RESULT:%.*]] = extractvalue { i8, i1 } [[SADD]], 0
@@ -150,7 +150,7 @@ define i32 @test8(i64 %a, i64 %b) nounwind ssp {
150150
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i64 [[TMP0]], -4294967296
151151
; CHECK-NEXT: br i1 [[TMP1]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
152152
; CHECK: if.then:
153-
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR2]]
153+
; CHECK-NEXT: tail call void @throwAnExceptionOrWhatever() #[[ATTR3]]
154154
; CHECK-NEXT: br label [[IF_END]]
155155
; CHECK: if.end:
156156
; CHECK-NEXT: [[CONV9:%.*]] = trunc i64 [[ADD]] to i32
@@ -171,3 +171,91 @@ if.end:
171171
ret i32 %conv9
172172
}
173173

174+
define i32 @uadd_no_overflow(i32 %a, i32 %b) {
175+
; CHECK-LABEL: @uadd_no_overflow(
176+
; CHECK-NEXT: [[TMP1:%.*]] = add nuw i32 [[A:%.*]], [[B:%.*]]
177+
; CHECK-NEXT: ret i32 [[TMP1]]
178+
;
179+
%val = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)
180+
%ov = extractvalue { i32, i1 } %val, 1
181+
%nowrap = xor i1 %ov, true
182+
tail call void @llvm.assume(i1 %nowrap)
183+
%res = extractvalue { i32, i1 } %val, 0
184+
ret i32 %res
185+
}
186+
187+
define i32 @smul_no_overflow(i32 %a, i32 %b) {
188+
; CHECK-LABEL: @smul_no_overflow(
189+
; CHECK-NEXT: [[TMP1:%.*]] = mul nsw i32 [[A:%.*]], [[B:%.*]]
190+
; CHECK-NEXT: ret i32 [[TMP1]]
191+
;
192+
%val = tail call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %a, i32 %b)
193+
%ov = extractvalue { i32, i1 } %val, 1
194+
%nowrap = xor i1 %ov, true
195+
tail call void @llvm.assume(i1 %nowrap)
196+
%res = extractvalue { i32, i1 } %val, 0
197+
ret i32 %res
198+
}
199+
200+
define i32 @smul_overflow(i32 %a, i32 %b) {
201+
; CHECK-LABEL: @smul_overflow(
202+
; CHECK-NEXT: [[VAL:%.*]] = tail call { i32, i1 } @llvm.smul.with.overflow.i32(i32 [[A:%.*]], i32 [[B:%.*]])
203+
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i32, i1 } [[VAL]], 1
204+
; CHECK-NEXT: tail call void @llvm.assume(i1 [[OV]])
205+
; CHECK-NEXT: [[RES:%.*]] = extractvalue { i32, i1 } [[VAL]], 0
206+
; CHECK-NEXT: ret i32 [[RES]]
207+
;
208+
%val = tail call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %a, i32 %b)
209+
%ov = extractvalue { i32, i1 } %val, 1
210+
tail call void @llvm.assume(i1 %ov)
211+
%res = extractvalue { i32, i1 } %val, 0
212+
ret i32 %res
213+
}
214+
215+
define i32 @uadd_no_overflow_invalid1(i32 %a, i32 %b, i1 %cond) {
216+
; CHECK-LABEL: @uadd_no_overflow_invalid1(
217+
; CHECK-NEXT: [[VAL:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[A:%.*]], i32 [[B:%.*]])
218+
; CHECK-NEXT: [[RES:%.*]] = extractvalue { i32, i1 } [[VAL]], 0
219+
; CHECK-NEXT: call void @use(i32 [[RES]])
220+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
221+
; CHECK: if.then:
222+
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i32, i1 } [[VAL]], 1
223+
; CHECK-NEXT: [[NOWRAP:%.*]] = xor i1 [[OV]], true
224+
; CHECK-NEXT: tail call void @llvm.assume(i1 [[NOWRAP]])
225+
; CHECK-NEXT: ret i32 [[RES]]
226+
; CHECK: if.else:
227+
; CHECK-NEXT: ret i32 0
228+
;
229+
%val = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)
230+
%res = extractvalue { i32, i1 } %val, 0
231+
call void @use(i32 %res)
232+
br i1 %cond, label %if.then, label %if.else
233+
if.then:
234+
%ov = extractvalue { i32, i1 } %val, 1
235+
%nowrap = xor i1 %ov, true
236+
tail call void @llvm.assume(i1 %nowrap)
237+
ret i32 %res
238+
if.else:
239+
ret i32 0
240+
}
241+
242+
define i32 @uadd_no_overflow_invalid2(i32 %a, i32 %b, i1 %cond) {
243+
; CHECK-LABEL: @uadd_no_overflow_invalid2(
244+
; CHECK-NEXT: [[VAL:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[A:%.*]], i32 [[B:%.*]])
245+
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i32, i1 } [[VAL]], 1
246+
; CHECK-NEXT: [[NOWRAP:%.*]] = xor i1 [[OV]], true
247+
; CHECK-NEXT: call void @use(i32 0)
248+
; CHECK-NEXT: tail call void @llvm.assume(i1 [[NOWRAP]])
249+
; CHECK-NEXT: [[RES:%.*]] = extractvalue { i32, i1 } [[VAL]], 0
250+
; CHECK-NEXT: ret i32 [[RES]]
251+
;
252+
%val = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 %a, i32 %b)
253+
%ov = extractvalue { i32, i1 } %val, 1
254+
%nowrap = xor i1 %ov, true
255+
call void @use(i32 0) ; It is not guaranteed to transfer execution to its successors
256+
tail call void @llvm.assume(i1 %nowrap)
257+
%res = extractvalue { i32, i1 } %val, 0
258+
ret i32 %res
259+
}
260+
261+
declare void @use(i32)

0 commit comments

Comments
 (0)