Skip to content

Commit f42b6fa

Browse files
committed
Auto merge of #103299 - nikic:usub-overflow, r=wesleywiser
Don't use usub.with.overflow intrinsic The canonical form of a usub.with.overflow check in LLVM are separate sub + icmp instructions, rather than a usub.with.overflow intrinsic. Using usub.with.overflow will generally result in worse optimization potential. The backend will attempt to form usub.with.overflow when it comes to actual instruction selection. This is not fully reliable, but I believe this is a better tradeoff than using the intrinsic in IR. Fixes #103285.
2 parents 5ab7445 + 7833012 commit f42b6fa

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
365365
Int(I64) => "llvm.ssub.with.overflow.i64",
366366
Int(I128) => "llvm.ssub.with.overflow.i128",
367367

368-
Uint(U8) => "llvm.usub.with.overflow.i8",
369-
Uint(U16) => "llvm.usub.with.overflow.i16",
370-
Uint(U32) => "llvm.usub.with.overflow.i32",
371-
Uint(U64) => "llvm.usub.with.overflow.i64",
372-
Uint(U128) => "llvm.usub.with.overflow.i128",
368+
Uint(_) => {
369+
// Emit sub and icmp instead of llvm.usub.with.overflow. LLVM considers these
370+
// to be the canonical form. It will attempt to reform llvm.usub.with.overflow
371+
// in the backend if profitable.
372+
let sub = self.sub(lhs, rhs);
373+
let cmp = self.icmp(IntPredicate::IntULT, lhs, rhs);
374+
return (sub, cmp);
375+
}
373376

374377
_ => unreachable!(),
375378
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: -O -C debug-assertions=yes
2+
3+
#![crate_type = "lib"]
4+
#![feature(strict_provenance)]
5+
6+
#[no_mangle]
7+
pub fn test(src: *const u8, dst: *const u8) -> usize {
8+
// CHECK-LABEL: @test(
9+
// CHECK-NOT: panic
10+
let src_usize = src.addr();
11+
let dst_usize = dst.addr();
12+
if src_usize > dst_usize {
13+
return src_usize - dst_usize;
14+
}
15+
return 0;
16+
}

0 commit comments

Comments
 (0)