diff options
| author | Nikita Popov <npopov@redhat.com> | 2022-10-20 12:28:31 +0200 |
|---|---|---|
| committer | Nikita Popov <npopov@redhat.com> | 2022-10-20 12:47:17 +0200 |
| commit | 783301298f1e151b82c1be21bc6a214b9ef525bd (patch) | |
| tree | 8735a552bd8376607e0d763d1d78b36ba77a8908 | |
| parent | 4b3b731b55a588dd34a75bbb87fdaaec2e3f5707 (diff) | |
| download | rust-783301298f1e151b82c1be21bc6a214b9ef525bd.tar.gz rust-783301298f1e151b82c1be21bc6a214b9ef525bd.zip | |
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.
| -rw-r--r-- | compiler/rustc_codegen_llvm/src/builder.rs | 13 | ||||
| -rw-r--r-- | src/test/codegen/issue-103285-ptr-addr-overflow-check.rs | 16 |
2 files changed, 24 insertions, 5 deletions
diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs index fca43a0d86d..9cb36ce7f18 100644 --- a/compiler/rustc_codegen_llvm/src/builder.rs +++ b/compiler/rustc_codegen_llvm/src/builder.rs @@ -365,11 +365,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> { Int(I64) => "llvm.ssub.with.overflow.i64", Int(I128) => "llvm.ssub.with.overflow.i128", - Uint(U8) => "llvm.usub.with.overflow.i8", - Uint(U16) => "llvm.usub.with.overflow.i16", - Uint(U32) => "llvm.usub.with.overflow.i32", - Uint(U64) => "llvm.usub.with.overflow.i64", - Uint(U128) => "llvm.usub.with.overflow.i128", + Uint(_) => { + // Emit sub and icmp instead of llvm.usub.with.overflow. LLVM considers these + // to be the canonical form. It will attempt to reform llvm.usub.with.overflow + // in the backend if profitable. + let sub = self.sub(lhs, rhs); + let cmp = self.icmp(IntPredicate::IntULT, lhs, rhs); + return (sub, cmp); + } _ => unreachable!(), }, diff --git a/src/test/codegen/issue-103285-ptr-addr-overflow-check.rs b/src/test/codegen/issue-103285-ptr-addr-overflow-check.rs new file mode 100644 index 00000000000..a3499babea2 --- /dev/null +++ b/src/test/codegen/issue-103285-ptr-addr-overflow-check.rs @@ -0,0 +1,16 @@ +// compile-flags: -O -C debug-assertions=yes + +#![crate_type = "lib"] +#![feature(strict_provenance)] + +#[no_mangle] +pub fn test(src: *const u8, dst: *const u8) -> usize { + // CHECK-LABEL: @test( + // CHECK-NOT: panic + let src_usize = src.addr(); + let dst_usize = dst.addr(); + if src_usize > dst_usize { + return src_usize - dst_usize; + } + return 0; +} |
