1use rustc_abi::ExternAbi;
4use rustc_ast::attr;
5use rustc_hir::LangItem;
6use rustc_middle::bug;
7use rustc_middle::mir::*;
8use rustc_middle::ty::layout::ValidityRequirement;
9use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt, layout};
10use rustc_span::{DUMMY_SP, Symbol, sym};
11
12use crate::simplify::simplify_duplicate_switch_targets;
13
14pub(super) enum InstSimplify {
15 BeforeInline,
16 AfterSimplifyCfg,
17}
18
19impl<'tcx> crate::MirPass<'tcx> for InstSimplify {
20 fn name(&self) -> &'static str {
21 match self {
22 InstSimplify::BeforeInline => "InstSimplify-before-inline",
23 InstSimplify::AfterSimplifyCfg => "InstSimplify-after-simplifycfg",
24 }
25 }
26
27 fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
28 sess.mir_opt_level() > 0
29 }
30
31 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
32 let ctx = InstSimplifyContext {
33 tcx,
34 local_decls: &body.local_decls,
35 typing_env: body.typing_env(tcx),
36 };
37 let preserve_ub_checks =
38 attr::contains_name(tcx.hir_krate_attrs(), sym::rustc_preserve_ub_checks);
39 for block in body.basic_blocks.as_mut() {
40 for statement in block.statements.iter_mut() {
41 let StatementKind::Assign(box (.., rvalue)) = &mut statement.kind else {
42 continue;
43 };
44
45 if !preserve_ub_checks {
46 ctx.simplify_ub_check(rvalue);
47 }
48 ctx.simplify_bool_cmp(rvalue);
49 ctx.simplify_ref_deref(rvalue);
50 ctx.simplify_ptr_aggregate(rvalue);
51 ctx.simplify_cast(rvalue);
52 ctx.simplify_repeated_aggregate(rvalue);
53 ctx.simplify_repeat_once(rvalue);
54 }
55
56 let terminator = block.terminator.as_mut().unwrap();
57 ctx.simplify_primitive_clone(terminator, &mut block.statements);
58 ctx.simplify_intrinsic_assert(terminator);
59 ctx.simplify_nounwind_call(terminator);
60 simplify_duplicate_switch_targets(terminator);
61 }
62 }
63
64 fn is_required(&self) -> bool {
65 false
66 }
67}
68
69struct InstSimplifyContext<'a, 'tcx> {
70 tcx: TyCtxt<'tcx>,
71 local_decls: &'a LocalDecls<'tcx>,
72 typing_env: ty::TypingEnv<'tcx>,
73}
74
75impl<'tcx> InstSimplifyContext<'_, 'tcx> {
76 fn simplify_repeated_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
80 let Rvalue::Aggregate(box AggregateKind::Array(_), fields) = &*rvalue else {
81 return;
82 };
83 if fields.len() < 5 {
84 return;
85 }
86 let (first, rest) = fields[..].split_first().unwrap();
87 let Operand::Constant(first) = first else {
88 return;
89 };
90 let Ok(first_val) = first.const_.eval(self.tcx, self.typing_env, first.span) else {
91 return;
92 };
93 if rest.iter().all(|field| {
94 let Operand::Constant(field) = field else {
95 return false;
96 };
97 let field = field.const_.eval(self.tcx, self.typing_env, field.span);
98 field == Ok(first_val)
99 }) {
100 let len = ty::Const::from_target_usize(self.tcx, fields.len().try_into().unwrap());
101 *rvalue = Rvalue::Repeat(Operand::Constant(first.clone()), len);
102 }
103 }
104
105 fn simplify_bool_cmp(&self, rvalue: &mut Rvalue<'tcx>) {
107 let Rvalue::BinaryOp(op @ (BinOp::Eq | BinOp::Ne), box (a, b)) = &*rvalue else { return };
108 *rvalue = match (op, self.try_eval_bool(a), self.try_eval_bool(b)) {
109 (BinOp::Eq, _, Some(true)) => Rvalue::Use(a.clone()),
111
112 (BinOp::Ne, _, Some(false)) => Rvalue::Use(a.clone()),
114
115 (BinOp::Eq, Some(true), _) => Rvalue::Use(b.clone()),
117
118 (BinOp::Ne, Some(false), _) => Rvalue::Use(b.clone()),
120
121 (BinOp::Eq, Some(false), _) => Rvalue::UnaryOp(UnOp::Not, b.clone()),
123
124 (BinOp::Ne, Some(true), _) => Rvalue::UnaryOp(UnOp::Not, b.clone()),
126
127 (BinOp::Eq, _, Some(false)) => Rvalue::UnaryOp(UnOp::Not, a.clone()),
129
130 (BinOp::Ne, _, Some(true)) => Rvalue::UnaryOp(UnOp::Not, a.clone()),
132
133 _ => return,
134 };
135 }
136
137 fn try_eval_bool(&self, a: &Operand<'_>) -> Option<bool> {
138 let a = a.constant()?;
139 if a.const_.ty().is_bool() { a.const_.try_to_bool() } else { None }
140 }
141
142 fn simplify_ref_deref(&self, rvalue: &mut Rvalue<'tcx>) {
144 if let Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) = rvalue
145 && let Some((base, ProjectionElem::Deref)) = place.as_ref().last_projection()
146 && rvalue.ty(self.local_decls, self.tcx) == base.ty(self.local_decls, self.tcx).ty
147 {
148 *rvalue = Rvalue::Use(Operand::Copy(Place {
149 local: base.local,
150 projection: self.tcx.mk_place_elems(base.projection),
151 }));
152 }
153 }
154
155 fn simplify_ptr_aggregate(&self, rvalue: &mut Rvalue<'tcx>) {
157 if let Rvalue::Aggregate(box AggregateKind::RawPtr(pointee_ty, mutability), fields) = rvalue
158 && let meta_ty = fields.raw[1].ty(self.local_decls, self.tcx)
159 && meta_ty.is_unit()
160 {
161 let mut fields = std::mem::take(fields);
163 let _meta = fields.pop().unwrap();
164 let data = fields.pop().unwrap();
165 let ptr_ty = Ty::new_ptr(self.tcx, *pointee_ty, *mutability);
166 *rvalue = Rvalue::Cast(CastKind::PtrToPtr, data, ptr_ty);
167 }
168 }
169
170 fn simplify_ub_check(&self, rvalue: &mut Rvalue<'tcx>) {
171 let Rvalue::NullaryOp(NullOp::UbChecks, _) = *rvalue else { return };
172
173 let const_ = Const::from_bool(self.tcx, self.tcx.sess.ub_checks());
174 let constant = ConstOperand { span: DUMMY_SP, const_, user_ty: None };
175 *rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
176 }
177
178 fn simplify_cast(&self, rvalue: &mut Rvalue<'tcx>) {
179 let Rvalue::Cast(kind, operand, cast_ty) = rvalue else { return };
180
181 let operand_ty = operand.ty(self.local_decls, self.tcx);
182 if operand_ty == *cast_ty {
183 *rvalue = Rvalue::Use(operand.clone());
184 } else if *kind == CastKind::Transmute
185 && let (ty::Int(int), ty::Uint(uint)) | (ty::Uint(uint), ty::Int(int)) =
187 (operand_ty.kind(), cast_ty.kind())
188 && int.bit_width() == uint.bit_width()
189 {
190 *kind = CastKind::IntToInt;
196 }
197 }
198
199 fn simplify_repeat_once(&self, rvalue: &mut Rvalue<'tcx>) {
201 if let Rvalue::Repeat(operand, count) = rvalue
202 && let Some(1) = count.try_to_target_usize(self.tcx)
203 {
204 *rvalue = Rvalue::Aggregate(
205 Box::new(AggregateKind::Array(operand.ty(self.local_decls, self.tcx))),
206 [operand.clone()].into(),
207 );
208 }
209 }
210
211 fn simplify_primitive_clone(
212 &self,
213 terminator: &mut Terminator<'tcx>,
214 statements: &mut Vec<Statement<'tcx>>,
215 ) {
216 let TerminatorKind::Call {
217 func, args, destination, target: Some(destination_block), ..
218 } = &terminator.kind
219 else {
220 return;
221 };
222
223 let [arg] = &args[..] else { return };
225
226 let Some((fn_def_id, ..)) = func.const_fn_def() else { return };
228
229 let arg_ty = arg.node.ty(self.local_decls, self.tcx);
232
233 let ty::Ref(_region, inner_ty, Mutability::Not) = *arg_ty.kind() else { return };
234
235 if !self.tcx.is_lang_item(fn_def_id, LangItem::CloneFn)
236 || !inner_ty.is_trivially_pure_clone_copy()
237 {
238 return;
239 }
240
241 let Some(arg_place) = arg.node.place() else { return };
242
243 statements.push(Statement {
244 source_info: terminator.source_info,
245 kind: StatementKind::Assign(Box::new((
246 *destination,
247 Rvalue::Use(Operand::Copy(
248 arg_place.project_deeper(&[ProjectionElem::Deref], self.tcx),
249 )),
250 ))),
251 });
252 terminator.kind = TerminatorKind::Goto { target: *destination_block };
253 }
254
255 fn simplify_nounwind_call(&self, terminator: &mut Terminator<'tcx>) {
256 let TerminatorKind::Call { ref func, ref mut unwind, .. } = terminator.kind else {
257 return;
258 };
259
260 let Some((def_id, _)) = func.const_fn_def() else {
261 return;
262 };
263
264 let body_ty = self.tcx.type_of(def_id).skip_binder();
265 let body_abi = match body_ty.kind() {
266 ty::FnDef(..) => body_ty.fn_sig(self.tcx).abi(),
267 ty::Closure(..) => ExternAbi::RustCall,
268 ty::Coroutine(..) => ExternAbi::Rust,
269 _ => bug!("unexpected body ty: {body_ty:?}"),
270 };
271
272 if !layout::fn_can_unwind(self.tcx, Some(def_id), body_abi) {
273 *unwind = UnwindAction::Unreachable;
274 }
275 }
276
277 fn simplify_intrinsic_assert(&self, terminator: &mut Terminator<'tcx>) {
278 let TerminatorKind::Call { ref func, target: ref mut target @ Some(target_block), .. } =
279 terminator.kind
280 else {
281 return;
282 };
283 let func_ty = func.ty(self.local_decls, self.tcx);
284 let Some((intrinsic_name, args)) = resolve_rust_intrinsic(self.tcx, func_ty) else {
285 return;
286 };
287 let [arg, ..] = args[..] else { return };
289
290 let known_is_valid =
291 intrinsic_assert_panics(self.tcx, self.typing_env, arg, intrinsic_name);
292 match known_is_valid {
293 None => {}
295 Some(true) => {
296 *target = None;
298 }
299 Some(false) => {
300 terminator.kind = TerminatorKind::Goto { target: target_block };
302 }
303 }
304 }
305}
306
307fn intrinsic_assert_panics<'tcx>(
308 tcx: TyCtxt<'tcx>,
309 typing_env: ty::TypingEnv<'tcx>,
310 arg: ty::GenericArg<'tcx>,
311 intrinsic_name: Symbol,
312) -> Option<bool> {
313 let requirement = ValidityRequirement::from_intrinsic(intrinsic_name)?;
314 let ty = arg.expect_ty();
315 Some(!tcx.check_validity_requirement((requirement, typing_env.as_query_input(ty))).ok()?)
316}
317
318fn resolve_rust_intrinsic<'tcx>(
319 tcx: TyCtxt<'tcx>,
320 func_ty: Ty<'tcx>,
321) -> Option<(Symbol, GenericArgsRef<'tcx>)> {
322 let ty::FnDef(def_id, args) = *func_ty.kind() else { return None };
323 let intrinsic = tcx.intrinsic(def_id)?;
324 Some((intrinsic.name, args))
325}