rapx/analysis/core/range_analysis/SSA/Replacer.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
#![allow(non_snake_case)]
#![allow(unused_variables)]
#![allow(dead_code)]
use super::SSATransformer::SSATransformer;
use rustc_index::IndexVec;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use std::collections::{HashMap, HashSet, VecDeque};
// use stable_mir::mir::FieldIdx;
// use stable_mir::ty::ConstantKind;
// // use rustc_middle::mir::visit::*;
// // use rustc_index::IndexSlice;
pub struct Replacer<'tcx> {
pub(crate) tcx: TyCtxt<'tcx>,
pub(crate) ssatransformer: super::SSATransformer::SSATransformer<'tcx>,
pub(crate) new_local_collection: HashSet<Local>,
}
impl<'tcx> Replacer<'tcx> {
pub fn insert_phi_statment(&mut self, body: &mut Body<'tcx>) {
for (block_index, blockdata) in body.basic_blocks.iter_enumerated() {}
let mut phi_functions: HashMap<BasicBlock, HashSet<Local>> = HashMap::new();
for bb in body.basic_blocks.indices() {
phi_functions.insert(bb, HashSet::new());
}
let variables: Vec<Local> = self
.ssatransformer
.local_assign_blocks
.iter()
.filter(|(_, blocks)| blocks.len() >= 2) // 只保留基本块数量大于等于 2 的条目
.map(|(&local, _)| local) // 提取 Local
.collect();
for var in &variables {
if let Some(def_blocks) = self.ssatransformer.local_assign_blocks.get(var) {
let mut worklist: VecDeque<BasicBlock> = def_blocks.iter().cloned().collect();
let mut processed: HashSet<BasicBlock> = HashSet::new();
while let Some(block) = worklist.pop_front() {
if let Some(df_blocks) = self.ssatransformer.df.get(&block) {
for &df_block in df_blocks {
if !processed.contains(&df_block) {
phi_functions.get_mut(&df_block).unwrap().insert(*var);
processed.insert(df_block);
if self.ssatransformer.local_assign_blocks[var].contains(&df_block)
{
worklist.push_back(df_block);
}
}
}
}
}
}
}
for (block, vars) in phi_functions {
for var in vars.clone() {
let decl = body.local_decls[var].clone();
// let new_var = body.local_decls.push(decl);
// print!("body.local_decls.len():{:?}\n", body.local_decls.len());
let predecessors = body.basic_blocks.predecessors()[block].clone();
let mut operands = IndexVec::with_capacity(predecessors.len());
for _ in 0..predecessors.len() {
operands.push(Operand::Copy(Place::from(var)));
}
let phi_stmt = Statement {
source_info: SourceInfo::outermost(body.span),
kind: StatementKind::Assign(Box::new((
Place::from(var),
Rvalue::Aggregate(Box::new(AggregateKind::Tuple), operands),
))),
};
body.basic_blocks_mut()[block]
.statements
.insert(0, phi_stmt);
}
for i in 0..vars.len() {
let phi_in_body = body.basic_blocks.as_mut()[block]
.statements
.get_mut(i)
.unwrap();
let phi_ptr = phi_in_body as *const _;
self.ssatransformer.phi_statements.insert(phi_ptr, true);
}
}
}
pub fn insert_essa_statement(&mut self, body: &mut Body<'tcx>) {
let order = SSATransformer::depth_first_search_preorder(
&self.ssatransformer.dom_tree,
body.basic_blocks.indices().next().unwrap(),
);
for &bb in &order {
self.essa_process_basic_block(bb, body);
}
}
fn essa_process_basic_block(&mut self, bb: BasicBlock, body: &mut Body<'tcx>) {
let switch_block_data = body.basic_blocks[bb].clone();
if let Some(terminator) = &switch_block_data.terminator {
if let TerminatorKind::SwitchInt { discr, targets, .. } = &terminator.kind {
{
for (value, target) in targets.iter() {
self.essa_assign_statement(&target, &bb, value, discr, body);
}
let otherwise = targets.otherwise();
self.essa_assign_statement(&otherwise, &bb, 1, discr, body);
}
}
}
}
fn extract_condition(
&self,
place: &Place<'tcx>,
switch_block: BasicBlockData<'tcx>,
) -> Option<(Operand<'tcx>, Operand<'tcx>, BinOp)> {
for stmt in &switch_block.statements {
if let StatementKind::Assign(box (lhs, Rvalue::BinaryOp(bin_op, box (op1, op2)))) =
&stmt.kind
{
if lhs == place {
let mut return_op1: &Operand<'tcx> = &op1;
let mut return_op2: &Operand<'tcx> = &op2;
for stmt_original in &switch_block.statements {
if let StatementKind::Assign(box (lhs, Rvalue::Use(OP1))) =
&stmt_original.kind
{
if lhs.clone() == op1.place().unwrap() {
return_op1 = OP1;
}
}
}
if op2.constant().is_none() {
for stmt_original in &switch_block.statements {
if let StatementKind::Assign(box (lhs, Rvalue::Use(OP2))) =
&stmt_original.kind
{
if lhs.clone() == op2.place().unwrap() {
return_op2 = OP2;
}
}
}
}
return Some((return_op1.clone(), return_op2.clone(), *bin_op));
}
}
}
None
}
// pub
fn essa_assign_statement(
&mut self,
bb: &BasicBlock,
switch_block: &BasicBlock,
value: u128,
discr: &Operand<'tcx>,
body: &mut Body<'tcx>,
) {
let switch_block_data = body.basic_blocks[*switch_block].clone();
let block_data: &mut BasicBlockData<'tcx> = &mut body.basic_blocks.as_mut()[*bb];
// let mut essa_operands: IndexVec<_, _> = IndexVec::with_capacity(2);
let magic_number = 213134123 as u64;
let magic_number_operand = Operand::Constant(Box::new(ConstOperand {
span: rustc_span::DUMMY_SP,
user_ty: None,
const_: Const::from_usize(self.tcx, magic_number),
}));
let Lt_operand = Operand::Constant(Box::new(ConstOperand {
span: rustc_span::DUMMY_SP,
user_ty: None,
const_: Const::from_usize(self.tcx, 1),
}));
let Le_operand = Operand::Constant(Box::new(ConstOperand {
span: rustc_span::DUMMY_SP,
user_ty: None,
const_: Const::from_usize(self.tcx, 2),
}));
let Ge_operand = Operand::Constant(Box::new(ConstOperand {
span: rustc_span::DUMMY_SP,
user_ty: None,
const_: Const::from_usize(self.tcx, 3),
}));
let Gt_operand = Operand::Constant(Box::new(ConstOperand {
span: rustc_span::DUMMY_SP,
user_ty: None,
const_: Const::from_usize(self.tcx, 4),
}));
if let Operand::Copy(switch_place) | Operand::Move(switch_place) = discr {
if let Some((op1, op2, cmp_op)) =
self.extract_condition(switch_place, switch_block_data)
{
let const_op1: Option<&ConstOperand<'_>> = op1.constant();
let const_op2: Option<&ConstOperand<'_>> = op2.constant();
let cmp_operand: Operand<'_> = match cmp_op.clone() {
BinOp::Lt => Lt_operand.clone(),
BinOp::Le => Le_operand.clone(),
BinOp::Gt => Gt_operand.clone(),
BinOp::Ge => Ge_operand.clone(),
_ => panic!("not a comparison operator"),
};
let flip_cmp_operand: Operand<'_> = match Self::flip(cmp_op) {
BinOp::Lt => Gt_operand.clone(),
BinOp::Le => Ge_operand.clone(),
BinOp::Gt => Lt_operand.clone(),
BinOp::Ge => Le_operand.clone(),
_ => panic!("not a comparison operator"),
};
match (const_op1, const_op2) {
(None, None) => {
match (op1, op2) {
(
Operand::Copy(p1) | Operand::Move(p1),
Operand::Copy(p2) | Operand::Move(p2),
) => {
let place1 = Place::from(p1);
let place2 = Place::from(p2);
let rvalue1;
let rvalue2;
let mut operand1: IndexVec<_, _> = IndexVec::with_capacity(4);
let mut operand2: IndexVec<_, _> = IndexVec::with_capacity(4);
if value == 0 {
operand1.push(Operand::Copy(Place::from(p1)));
operand1.push(Operand::Copy(Place::from(p2)));
operand1.push(flip_cmp_operand.clone());
operand1.push(magic_number_operand.clone());
operand2.push(Operand::Copy(Place::from(p2)));
operand2.push(Operand::Copy(Place::from(p1)));
operand2.push(cmp_operand.clone());
operand2.push(magic_number_operand.clone());
rvalue1 =
Rvalue::Aggregate(Box::new(AggregateKind::Tuple), operand1);
rvalue2 =
Rvalue::Aggregate(Box::new(AggregateKind::Tuple), operand2);
} else {
operand1.push(Operand::Copy(Place::from(p1)));
operand1.push(Operand::Copy(Place::from(p2)));
operand1.push(cmp_operand.clone());
operand1.push(magic_number_operand.clone());
operand2.push(Operand::Copy(Place::from(p2)));
operand2.push(Operand::Copy(Place::from(p1)));
operand2.push(flip_cmp_operand.clone());
operand2.push(magic_number_operand.clone());
rvalue1 =
Rvalue::Aggregate(Box::new(AggregateKind::Tuple), operand1);
rvalue2 =
Rvalue::Aggregate(Box::new(AggregateKind::Tuple), operand2);
}
let assign_stmt1 = Statement {
source_info: SourceInfo::outermost(body.span),
kind: StatementKind::Assign(Box::new((place1, rvalue1))),
};
let assign_stmt2 = Statement {
source_info: SourceInfo::outermost(body.span),
kind: StatementKind::Assign(Box::new((place2, rvalue2))),
};
block_data.statements.insert(0, assign_stmt1);
block_data.statements.insert(0, assign_stmt2);
for i in 0..2 {
let essa_in_body = block_data.statements.get_mut(i).unwrap();
let essa_ptr = essa_in_body as *const _; // 获取 statement 的指针作为 key
self.ssatransformer.essa_statements.insert(essa_ptr, true);
}
}
_ => panic!("Expected a place"),
};
}
(None, Some(_)) | (Some(_), None) => {
let mut operand: IndexVec<_, _> = IndexVec::with_capacity(3);
let place = match op1 {
Operand::Copy(p) | Operand::Move(p) => Place::from(p),
_ => panic!("Expected a place"),
};
operand.push(op1.clone());
operand.push(op2.clone());
let rvalue;
if value == 0 {
operand.push(flip_cmp_operand.clone());
} else {
operand.push(cmp_operand.clone());
}
rvalue = Rvalue::Aggregate(Box::new(AggregateKind::Tuple), operand);
let assign_stmt = Statement {
source_info: SourceInfo::outermost(body.span),
kind: StatementKind::Assign(Box::new((place, rvalue))),
};
block_data.statements.insert(0, assign_stmt);
for i in 0..1 {
let essa_in_body = block_data.statements.get_mut(i).unwrap();
let essa_ptr = essa_in_body as *const _; // 获取 statement 的指针作为 key
self.ssatransformer.essa_statements.insert(essa_ptr, true);
}
}
(Some(_), Some(_)) => {}
}
};
}
// block_data.statements.insert(0, assign_stmt);
}
pub fn flip(binOp: BinOp) -> BinOp {
match binOp {
BinOp::Lt => BinOp::Ge,
BinOp::Le => BinOp::Gt,
BinOp::Gt => BinOp::Le,
BinOp::Ge => BinOp::Lt,
_ => panic!("flip() called on non-comparison operator"),
}
}
pub fn rename_variables(&mut self, body: &mut Body<'tcx>) {
for local in body.local_decls.indices() {
self.ssatransformer.reaching_def.insert(local, None);
}
// self.ssatransformer.local_defination_block = Self::map_locals_to_definition_block(&self.body.borrow());
let order = SSATransformer::depth_first_search_preorder(
&self.ssatransformer.dom_tree,
body.basic_blocks.indices().next().unwrap().clone(),
);
for bb in order {
self.process_basic_block(bb, body);
}
}
fn process_basic_block(&mut self, bb: BasicBlock, body: &mut Body<'tcx>) {
self.rename_statement(bb, body);
self.rename_terminator(bb, body);
let terminator = body.basic_blocks[bb].terminator();
let successors: Vec<_> = terminator.successors().collect();
if let TerminatorKind::SwitchInt { .. } = &terminator.kind {
for succ_bb in successors.clone() {
self.process_essa_statments(succ_bb, body, bb);
}
}
for succ_bb in successors {
self.process_phi_functions(succ_bb, body, bb);
}
}
pub fn process_essa_statments(
&mut self,
succ_bb: BasicBlock,
body: &mut Body<'tcx>,
switch_bb: BasicBlock,
) {
let switch_block_data = body.basic_blocks[switch_bb].clone();
if let Some(terminator) = &switch_block_data.clone().terminator {
if let TerminatorKind::SwitchInt { discr, .. } = &terminator.kind {
if let Operand::Copy(switch_place) | Operand::Move(switch_place) = discr {
if let Some((op1, op2, cmp_op)) =
self.extract_condition(switch_place, switch_block_data)
{
if op2.constant().is_none() {
let essa_statement = body.basic_blocks.as_mut()[succ_bb]
.statements
.get_mut(0)
.unwrap();
match &mut essa_statement.kind {
StatementKind::Assign(box (place, rvalue)) => {
if let Rvalue::Aggregate(_, operands) = rvalue {
let loc_1: usize = 0;
let loc_2: usize = 1;
operands[loc_1.into()] = op1.clone();
operands[loc_2.into()] = op2.clone();
}
}
_ => {}
}
let essa_statement = body.basic_blocks.as_mut()[succ_bb]
.statements
.get_mut(1)
.unwrap();
match &mut essa_statement.kind {
StatementKind::Assign(box (place, rvalue)) => {
if let Rvalue::Aggregate(_, operands) = rvalue {
let loc_1: usize = 0;
let loc_2: usize = 1;
operands[loc_1.into()] = op2.clone();
operands[loc_2.into()] = op1.clone();
}
}
_ => {}
}
} else {
let essa_statement = body.basic_blocks.as_mut()[succ_bb]
.statements
.get_mut(0)
.unwrap();
match &mut essa_statement.kind {
StatementKind::Assign(box (place, rvalue)) => {
if let Rvalue::Aggregate(_, operands) = rvalue {
let loc: usize = 0;
operands[loc.into()] = op1.clone();
}
}
_ => {}
}
}
}
}
}
}
}
fn process_phi_functions(
&mut self,
succ_bb: BasicBlock,
body: &mut Body<'tcx>,
do_bb: BasicBlock,
) {
for statement in body.basic_blocks.as_mut()[succ_bb].statements.iter_mut() {
let phi_stmt = statement as *const _;
if SSATransformer::is_phi_statement(&self.ssatransformer, statement) {
if let StatementKind::Assign(box (_, rvalue)) = &mut statement.kind {
if let Rvalue::Aggregate(_, operands) = rvalue {
let operand_count = operands.len();
let index = self
.ssatransformer
.phi_index
.entry(phi_stmt)
.or_insert(0)
.clone();
if index < operand_count {
// self.replace_operand(&mut operands[(index).into()], &succ_bb);s
match &mut operands[(index).into()] {
Operand::Copy(place) | Operand::Move(place) => {
self.replace_place(place, &do_bb);
}
_ => {}
}
*self.ssatransformer.phi_index.entry(phi_stmt).or_insert(0) += 1;
// if *index >= operand_count {
// self.ssatransformer.phi_index.remove(&phi_stmt);
// }
}
}
}
}
}
}
pub fn rename_statement(&mut self, bb: BasicBlock, body: &mut Body<'tcx>) {
for statement in body.basic_blocks.as_mut()[bb].statements.iter_mut() {
// let rc_stat = Rc::new(RefCell::new(statement));
let is_phi = SSATransformer::is_phi_statement(&self.ssatransformer, statement);
let is_essa = SSATransformer::is_essa_statement(&self.ssatransformer, statement);
match &mut statement.kind {
StatementKind::Assign(box (place, rvalue)) => {
if !is_phi {
if !is_essa {
self.replace_rvalue(rvalue, &bb);
self.rename_local_def(place, &bb, true);
} else {
self.rename_local_def(place, &bb, true);
}
} else {
self.rename_local_def(place, &bb, false);
}
}
// 2. FakeRead: 变量使用
// StatementKind::FakeRead(_, place)
StatementKind::Deinit(place) | StatementKind::SetDiscriminant { place, .. } => {
// let place_mut = unsafe { &mut *(place as *const _ as *mut _) };
// self.replace_place(place.as_mut());
}
// 3. StorageLive: 变量定义
StatementKind::StorageLive(local) => {
// self.rename_local_def(*local);
}
// 4. StorageDead: 变量使用
StatementKind::StorageDead(local) => {
// self.replace_local(local);
}
_ => {}
}
}
}
fn rename_terminator(&mut self, bb: BasicBlock, body: &mut Body<'tcx>) {
let terminator: &mut Terminator<'tcx> = body.basic_blocks.as_mut()[bb].terminator_mut();
match &mut terminator.kind {
TerminatorKind::Call {
args, destination, ..
} => {
// for operand in args {
// self.replace_operand(operand);
// }
// if let Some((place, _)) = destination {
// self.rename_def(place);
// }
}
TerminatorKind::Assert { cond, .. } => {
self.replace_operand(cond, &bb);
}
TerminatorKind::Drop { place, .. } => {
self.replace_place(place, &bb);
}
TerminatorKind::SwitchInt { discr, .. } => {
self.replace_operand(discr, &bb);
}
_ => {}
}
}
fn replace_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>, bb: &BasicBlock) {
match rvalue {
Rvalue::Use(operand)
| Rvalue::Repeat(operand, _)
| Rvalue::UnaryOp(_, operand)
| Rvalue::Cast(_, operand, _)
| Rvalue::ShallowInitBox(operand, _) => {
self.replace_operand(operand, &bb);
}
Rvalue::BinaryOp(_, box (lhs, rhs)) => {
self.replace_operand(lhs, &bb);
self.replace_operand(rhs, &bb);
}
Rvalue::Aggregate(_, operands) => {
for operand in operands {
self.replace_operand(operand, &bb);
}
}
_ => {}
}
}
fn replace_operand(&mut self, operand: &mut Operand<'tcx>, bb: &BasicBlock) {
match operand {
Operand::Copy(place) | Operand::Move(place) => {
self.replace_place(place, bb);
// print!("replace_operand: {:?} -> {:?}\n", place.local, place.local);
}
_ => {}
}
}
fn replace_place(&mut self, place: &mut Place<'tcx>, bb: &BasicBlock) {
// let old_local = place.local;
self.update_reachinf_def(&place.local, &bb);
if let Some(Some(reaching_local)) = self.ssatransformer.reaching_def.get(&place.local) {
let local = reaching_local.clone();
*place = Place::from(local);
} else {
}
}
fn rename_local_def(&mut self, place: &mut Place<'tcx>, bb: &BasicBlock, not_phi: bool) {
// let old_local = place.as_local().as_mut().unwrap();
self.update_reachinf_def(&place.local, &bb);
let Place {
local: old_local,
projection: _,
} = place;
if self.ssatransformer.skipped.contains(&old_local.as_u32()) && not_phi {
self.ssatransformer.skipped.remove(&old_local.as_u32());
self.ssatransformer
.reaching_def
.insert(*old_local, Some(*old_local));
return;
}
let new_local = Local::from_u32(self.ssatransformer.local_index);
self.ssatransformer.local_index += 1;
let _old_local = old_local.clone();
*place = Place::from(new_local);
self.ssatransformer
.local_defination_block
.insert(new_local.clone(), bb.clone());
let old_local_reaching = self
.ssatransformer
.reaching_def
.get(&_old_local.clone())
.unwrap();
self.ssatransformer
.reaching_def
.insert(new_local.clone(), *old_local_reaching);
self.ssatransformer
.reaching_def
.insert(_old_local.clone(), Some(new_local.clone()));
// self.reaching_def
// .entry(old_local)
// .or_default()
// .replace(Some(old_local));
}
pub fn dominates_(&self, def_bb: &BasicBlock, bb: &BasicBlock) -> bool {
let mut visited = HashSet::new();
let mut stack = self.ssatransformer.dom_tree.get(def_bb).unwrap().clone();
while let Some(block) = stack.pop() {
if !visited.insert(block) {
continue;
}
if block == *bb {
return true;
}
if let Some(children) = self.ssatransformer.dom_tree.get(&block) {
stack.extend(children);
}
}
false
}
fn update_reachinf_def(&mut self, local: &Local, bb: &BasicBlock) {
// if self.ssatransformer.reaching_def[local]!= None {
// return;
// }
let mut r = self.ssatransformer.reaching_def[local];
let mut dominate_bool = true;
if r != None {
let def_bb = self.ssatransformer.local_defination_block[&r.unwrap()];
}
while !(r == None || dominate_bool) {
r = self.ssatransformer.reaching_def[&r.unwrap()];
if r != None {
let def_bb = self.ssatransformer.local_defination_block[&r.unwrap()];
dominate_bool = self.dominates_(&def_bb, bb);
}
}
if let Some(entry) = self.ssatransformer.reaching_def.get_mut(local) {
*entry = r.clone();
}
}
}
// impl<'tcx> MutVisitor<'tcx> for Replacer< 'tcx> {
// fn tcx(&self) -> TyCtxt<'tcx> {
// self.tcx
// }
// fn visit_local(&mut self, local: &mut Local, ctxt: PlaceContext, _: Location) {
// let new_local = self.copy_classes[*local];
// // We must not unify two locals that are borrowed. But this is fine if one is borrowed and
// // the other is not. We chose to check the original local, and not the target. That way, if
// // the original local is borrowed and the target is not, we do not pessimize the whole class.
// if self.borrowed_locals.contains(*local) {
// return;
// }
// match ctxt {
// // Do not modify the local in storage statements.
// PlaceContext::NonUse(NonUseContext::StorageLive | NonUseContext::StorageDead) => {}
// // The local should have been marked as non-SSA.
// PlaceContext::MutatingUse(_) => assert_eq!(*local, new_local),
// // We access the value.
// _ => *local = new_local,
// // _ => *local = new_local,
// }
// }
// fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, loc: Location) {
// if let Some(new_projection) = self.process_projection(place.projection, loc) {
// place.projection = self.tcx().mk_place_elems(&new_projection);
// }
// // Any non-mutating use context is ok.
// let ctxt = PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy);
// self.visit_local(&mut place.local, ctxt, loc);
// print!("{:?}", place);
// }
// fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) {
// if let Operand::Move(place) = *operand
// // A move out of a projection of a copy is equivalent to a copy of the original
// // projection.
// && !place.is_indirect_first_projection()
// && !self.fully_moved.contains(place.local)
// {
// *operand = Operand::Copy(place);
// }
// self.super_operand(operand, loc);
// }
// fn visit_statement(&mut self, stmt: &mut Statement<'tcx>, loc: Location) {
// // When removing storage statements, we need to remove both (#107511).
// if let StatementKind::StorageLive(l) | StatementKind::StorageDead(l) = stmt.kind
// && self.storage_to_remove.contains(l)
// {
// stmt.make_nop();
// return;
// }
// self.super_statement(stmt, loc);
// // Do not leave tautological assignments around.
// if let StatementKind::Assign(box (lhs, ref rhs)) = stmt.kind
// && let Rvalue::Use(Operand::Copy(rhs) | Operand::Move(rhs)) | Rvalue::CopyForDeref(rhs) =
// *rhs
// && lhs == rhs
// {
// stmt.make_nop();
// }
// }
// fn visit_body_preserves_cfg(&mut self, body: &mut Body<'tcx>) {}
// fn visit_basic_block_data(&mut self, block: BasicBlock, data: &mut BasicBlockData<'tcx>) {
// let BasicBlockData {
// statements,
// terminator,
// is_cleanup: _,
// } = data;
// }
// }