about summary refs log tree commit diff
path: root/compiler/rustc_codegen_ssa/src/traits/builder.rs
diff options
context:
space:
mode:
authorScott McMurray <scottmcm@users.noreply.github.com>2025-02-08 00:14:31 -0800
committerScott McMurray <scottmcm@users.noreply.github.com>2025-02-12 23:01:27 -0800
commit0cc14b688d274096ee4765a33488874a8d84e361 (patch)
treefdd19e5c2ebb5628f245ee88f690a3d8ee3103f0 /compiler/rustc_codegen_ssa/src/traits/builder.rs
parent9fcc9cf4a202aadfe1f44722b39c83536eba3dba (diff)
downloadrust-0cc14b688d274096ee4765a33488874a8d84e361.tar.gz
rust-0cc14b688d274096ee4765a33488874a8d84e361.zip
`transmute` should also assume non-null pointers
Previously it only did integer-ABI things, but this way it does data pointers too.  That gives more information in general to the backend, and allows slightly simplifying one of the helpers in slice iterators.
Diffstat (limited to 'compiler/rustc_codegen_ssa/src/traits/builder.rs')
-rw-r--r--compiler/rustc_codegen_ssa/src/traits/builder.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/compiler/rustc_codegen_ssa/src/traits/builder.rs b/compiler/rustc_codegen_ssa/src/traits/builder.rs
index b7dcf16fa2b..d9bb5ae773e 100644
--- a/compiler/rustc_codegen_ssa/src/traits/builder.rs
+++ b/compiler/rustc_codegen_ssa/src/traits/builder.rs
@@ -243,6 +243,19 @@ pub trait BuilderMethods<'a, 'tcx>:
         self.assume(cmp);
     }
 
+    /// Emits an `assume` that the `val` of pointer type is non-null.
+    ///
+    /// You may want to check the optimization level before bothering calling this.
+    fn assume_nonnull(&mut self, val: Self::Value) {
+        // Arguably in LLVM it'd be better to emit an assume operand bundle instead
+        // <https://llvm.org/docs/LangRef.html#assume-operand-bundles>
+        // but this works fine for all backends.
+
+        let null = self.const_null(self.type_ptr());
+        let is_null = self.icmp(IntPredicate::IntNE, val, null);
+        self.assume(is_null);
+    }
+
     fn range_metadata(&mut self, load: Self::Value, range: WrappingRange);
     fn nonnull_metadata(&mut self, load: Self::Value);