Skip to content

Commit 8a160d6

Browse files
authored
Rollup merge of #114382 - scottmcm:compare-bytes-intrinsic, r=cjgillot
Add a new `compare_bytes` intrinsic instead of calling `memcmp` directly As discussed in #113435, this lets the backends be the place that can have the "don't call the function if n == 0" logic, if it's needed for the target. (I didn't actually *add* those checks, though, since as I understood it we didn't actually need them on known targets?) Doing this also let me make it `const` (unstable), which I don't think `extern "C" fn memcmp` can be. cc `@RalfJung` `@Amanieu`
2 parents fe6a477 + b132a7e commit 8a160d6

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Diff for: src/intrinsic/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,21 @@ impl<'a, 'gcc, 'tcx> IntrinsicCallMethods<'tcx> for Builder<'a, 'gcc, 'tcx> {
302302
}
303303
}
304304

305+
sym::compare_bytes => {
306+
let a = args[0].immediate();
307+
let b = args[1].immediate();
308+
let n = args[2].immediate();
309+
310+
let void_ptr_type = self.context.new_type::<*const ()>();
311+
let a_ptr = self.bitcast(a, void_ptr_type);
312+
let b_ptr = self.bitcast(b, void_ptr_type);
313+
314+
// Here we assume that the `memcmp` provided by the target is a NOP for size 0.
315+
let builtin = self.context.get_builtin_function("memcmp");
316+
let cmp = self.context.new_call(None, builtin, &[a_ptr, b_ptr, n]);
317+
self.sext(cmp, self.type_ix(32))
318+
}
319+
305320
sym::black_box => {
306321
args[0].val.store(self, result);
307322

0 commit comments

Comments
 (0)