Flutter Engine
The Flutter Engine
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Static Public Member Functions | List of all members
dart::DeadCodeElimination Class Reference

#include <redundancy_elimination.h>

Inheritance diagram for dart::DeadCodeElimination:
dart::AllStatic

Static Public Member Functions

static void EliminateDeadPhis (FlowGraph *graph)
 
static void RemoveDeadAndRedundantPhisFromTheGraph (FlowGraph *graph)
 
static void EliminateDeadCode (FlowGraph *graph)
 

Detailed Description

Definition at line 110 of file redundancy_elimination.h.

Member Function Documentation

◆ EliminateDeadCode()

void dart::DeadCodeElimination::EliminateDeadCode ( FlowGraph graph)
static

Definition at line 4480 of file redundancy_elimination.cc.

4480 {
4481 GrowableArray<Instruction*> worklist;
4482 BitVector live(flow_graph->zone(), flow_graph->current_ssa_temp_index());
4483
4484 // Mark all instructions with side-effects as live.
4485 for (BlockIterator block_it = flow_graph->reverse_postorder_iterator();
4486 !block_it.Done(); block_it.Advance()) {
4487 BlockEntryInstr* block = block_it.Current();
4488 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) {
4489 Instruction* current = it.Current();
4490 ASSERT(!current->IsMoveArgument());
4491 // TODO(alexmarkov): take control dependencies into account and
4492 // eliminate dead branches/conditions.
4493 if (!current->CanEliminate(block)) {
4494 worklist.Add(current);
4495 if (Definition* def = current->AsDefinition()) {
4496 if (def->HasSSATemp()) {
4497 live.Add(def->ssa_temp_index());
4498 }
4499 }
4500 }
4501 }
4502 }
4503
4504 // Iteratively follow inputs of instructions in the work list.
4505 while (!worklist.is_empty()) {
4506 Instruction* current = worklist.RemoveLast();
4507 for (intptr_t i = 0, n = current->InputCount(); i < n; ++i) {
4508 Definition* input = current->InputAt(i)->definition();
4509 ASSERT(input->HasSSATemp());
4510 if (!live.Contains(input->ssa_temp_index())) {
4511 worklist.Add(input);
4512 live.Add(input->ssa_temp_index());
4513 }
4514 }
4515 for (intptr_t i = 0, n = current->ArgumentCount(); i < n; ++i) {
4516 Definition* input = current->ArgumentAt(i);
4517 ASSERT(input->HasSSATemp());
4518 if (!live.Contains(input->ssa_temp_index())) {
4519 worklist.Add(input);
4520 live.Add(input->ssa_temp_index());
4521 }
4522 }
4523 if (current->env() != nullptr) {
4524 for (Environment::DeepIterator it(current->env()); !it.Done();
4525 it.Advance()) {
4526 Definition* input = it.CurrentValue()->definition();
4527 ASSERT(!input->IsMoveArgument());
4528 if (input->HasSSATemp() && !live.Contains(input->ssa_temp_index())) {
4529 worklist.Add(input);
4530 live.Add(input->ssa_temp_index());
4531 }
4532 }
4533 }
4534 }
4535
4536 // Remove all instructions which are not marked as live.
4537 for (BlockIterator block_it = flow_graph->reverse_postorder_iterator();
4538 !block_it.Done(); block_it.Advance()) {
4539 BlockEntryInstr* block = block_it.Current();
4540 if (JoinEntryInstr* join = block->AsJoinEntry()) {
4541 for (PhiIterator it(join); !it.Done(); it.Advance()) {
4542 PhiInstr* current = it.Current();
4543 if (!live.Contains(current->ssa_temp_index())) {
4544 it.RemoveCurrentFromGraph();
4545 }
4546 }
4547 }
4548 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) {
4549 Instruction* current = it.Current();
4550 if (!current->CanEliminate(block)) {
4551 continue;
4552 }
4553 ASSERT(!current->IsMoveArgument());
4554 ASSERT((current->ArgumentCount() == 0) || !current->HasMoveArguments());
4555 if (Definition* def = current->AsDefinition()) {
4556 if (def->HasSSATemp() && live.Contains(def->ssa_temp_index())) {
4557 continue;
4558 }
4559 }
4560 it.RemoveCurrentFromGraph();
4561 }
4562 }
4563}
#define ASSERT(E)
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741

◆ EliminateDeadPhis()

void dart::DeadCodeElimination::EliminateDeadPhis ( FlowGraph graph)
static

Definition at line 4413 of file redundancy_elimination.cc.

4413 {
4414 GrowableArray<PhiInstr*> live_phis;
4415 for (BlockIterator b = flow_graph->postorder_iterator(); !b.Done();
4416 b.Advance()) {
4417 JoinEntryInstr* join = b.Current()->AsJoinEntry();
4418 if (join != nullptr) {
4419 for (PhiIterator it(join); !it.Done(); it.Advance()) {
4420 PhiInstr* phi = it.Current();
4421 // Phis that have uses and phis inside try blocks are
4422 // marked as live.
4423 if (HasRealUse(phi)) {
4424 live_phis.Add(phi);
4425 phi->mark_alive();
4426 } else {
4427 phi->mark_dead();
4428 }
4429 }
4430 }
4431 }
4432
4433 while (!live_phis.is_empty()) {
4434 PhiInstr* phi = live_phis.RemoveLast();
4435 for (intptr_t i = 0; i < phi->InputCount(); i++) {
4436 Value* val = phi->InputAt(i);
4437 PhiInstr* used_phi = val->definition()->AsPhi();
4438 if ((used_phi != nullptr) && !used_phi->is_alive()) {
4439 used_phi->mark_alive();
4440 live_phis.Add(used_phi);
4441 }
4442 }
4443 }
4444
4446}
static void RemoveDeadAndRedundantPhisFromTheGraph(FlowGraph *graph)
static bool b
static bool HasRealUse(Definition *def)

◆ RemoveDeadAndRedundantPhisFromTheGraph()

void dart::DeadCodeElimination::RemoveDeadAndRedundantPhisFromTheGraph ( FlowGraph graph)
static

Definition at line 4448 of file redundancy_elimination.cc.

4449 {
4450 for (auto block : flow_graph->postorder()) {
4451 if (JoinEntryInstr* join = block->AsJoinEntry()) {
4452 if (join->phis_ == nullptr) continue;
4453
4454 // Eliminate dead phis and compact the phis_ array of the block.
4455 intptr_t to_index = 0;
4456 for (intptr_t i = 0; i < join->phis_->length(); ++i) {
4457 PhiInstr* phi = (*join->phis_)[i];
4458 if (phi != nullptr) {
4459 if (!phi->is_alive()) {
4460 phi->ReplaceUsesWith(flow_graph->constant_null());
4461 phi->UnuseAllInputs();
4462 (*join->phis_)[i] = nullptr;
4463 if (FLAG_trace_optimization && flow_graph->should_print()) {
4464 THR_Print("Removing dead phi v%" Pd "\n", phi->ssa_temp_index());
4465 }
4466 } else {
4467 (*join->phis_)[to_index++] = phi;
4468 }
4469 }
4470 }
4471 if (to_index == 0) {
4472 join->phis_ = nullptr;
4473 } else {
4474 join->phis_->TruncateTo(to_index);
4475 }
4476 }
4477 }
4478}
#define THR_Print(format,...)
Definition: log.h:20
#define Pd
Definition: globals.h:408

The documentation for this class was generated from the following files: