Skip to content

Commit 0069cad

Browse files
committed
Micro-optimize InstSimplify's simplify_primitive_clone
1 parent 69b3959 commit 0069cad

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

Diff for: compiler/rustc_mir_transform/src/instsimplify.rs

+8-17
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, layout};
1010
use rustc_span::{DUMMY_SP, Symbol, sym};
1111

1212
use crate::simplify::simplify_duplicate_switch_targets;
13-
use crate::take_array;
1413

1514
pub(super) enum InstSimplify {
1615
BeforeInline,
@@ -229,39 +228,31 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
229228
terminator: &mut Terminator<'tcx>,
230229
statements: &mut Vec<Statement<'tcx>>,
231230
) {
232-
let TerminatorKind::Call { func, args, destination, target, .. } = &mut terminator.kind
231+
let TerminatorKind::Call {
232+
func, args, destination, target: Some(destination_block), ..
233+
} = &terminator.kind
233234
else {
234235
return;
235236
};
236237

237238
// It's definitely not a clone if there are multiple arguments
238239
let [arg] = &args[..] else { return };
239240

240-
let Some(destination_block) = *target else { return };
241-
242241
// Only bother looking more if it's easy to know what we're calling
243-
let Some((fn_def_id, fn_args)) = func.const_fn_def() else { return };
244-
245-
// Clone needs one arg, so we can cheaply rule out other stuff
246-
if fn_args.len() != 1 {
247-
return;
248-
}
242+
let Some((fn_def_id, ..)) = func.const_fn_def() else { return };
249243

250244
// These types are easily available from locals, so check that before
251245
// doing DefId lookups to figure out what we're actually calling.
252246
let arg_ty = arg.node.ty(self.local_decls, self.tcx);
253247

254248
let ty::Ref(_region, inner_ty, Mutability::Not) = *arg_ty.kind() else { return };
255249

256-
if !inner_ty.is_trivially_pure_clone_copy() {
257-
return;
258-
}
259-
260-
if !self.tcx.is_lang_item(fn_def_id, LangItem::CloneFn) {
250+
if !self.tcx.is_lang_item(fn_def_id, LangItem::CloneFn)
251+
|| !inner_ty.is_trivially_pure_clone_copy()
252+
{
261253
return;
262254
}
263255

264-
let Ok([arg]) = take_array(args) else { return };
265256
let Some(arg_place) = arg.node.place() else { return };
266257

267258
statements.push(Statement {
@@ -273,7 +264,7 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
273264
)),
274265
))),
275266
});
276-
terminator.kind = TerminatorKind::Goto { target: destination_block };
267+
terminator.kind = TerminatorKind::Goto { target: *destination_block };
277268
}
278269

279270
fn simplify_nounwind_call(&self, terminator: &mut Terminator<'tcx>) {

0 commit comments

Comments
 (0)