Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 4461 of file redundancy_elimination.cc.

4461 {
4462 GrowableArray<Instruction*> worklist;
4463 BitVector live(flow_graph->zone(), flow_graph->current_ssa_temp_index());
4464
4465 // Mark all instructions with side-effects as live.
4466 for (BlockIterator block_it = flow_graph->reverse_postorder_iterator();
4467 !block_it.Done(); block_it.Advance()) {
4468 BlockEntryInstr* block = block_it.Current();
4469 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) {
4470 Instruction* current = it.Current();
4471 ASSERT(!current->IsMoveArgument());
4472 // TODO(alexmarkov): take control dependencies into account and
4473 // eliminate dead branches/conditions.
4474 if (!current->CanEliminate(block)) {
4475 worklist.Add(current);
4476 if (Definition* def = current->AsDefinition()) {
4477 if (def->HasSSATemp()) {
4478 live.Add(def->ssa_temp_index());
4479 }
4480 }
4481 }
4482 }
4483 }
4484
4485 // Iteratively follow inputs of instructions in the work list.
4486 while (!worklist.is_empty()) {
4487 Instruction* current = worklist.RemoveLast();
4488 for (intptr_t i = 0, n = current->InputCount(); i < n; ++i) {
4489 Definition* input = current->InputAt(i)->definition();
4490 ASSERT(input->HasSSATemp());
4491 if (!live.Contains(input->ssa_temp_index())) {
4492 worklist.Add(input);
4493 live.Add(input->ssa_temp_index());
4494 }
4495 }
4496 for (intptr_t i = 0, n = current->ArgumentCount(); i < n; ++i) {
4497 Definition* input = current->ArgumentAt(i);
4498 ASSERT(input->HasSSATemp());
4499 if (!live.Contains(input->ssa_temp_index())) {
4500 worklist.Add(input);
4501 live.Add(input->ssa_temp_index());
4502 }
4503 }
4504 if (current->env() != nullptr) {
4505 for (Environment::DeepIterator it(current->env()); !it.Done();
4506 it.Advance()) {
4507 Definition* input = it.CurrentValue()->definition();
4508 ASSERT(!input->IsMoveArgument());
4509 if (input->HasSSATemp() && !live.Contains(input->ssa_temp_index())) {
4510 worklist.Add(input);
4511 live.Add(input->ssa_temp_index());
4512 }
4513 }
4514 }
4515 }
4516
4517 // Remove all instructions which are not marked as live.
4518 for (BlockIterator block_it = flow_graph->reverse_postorder_iterator();
4519 !block_it.Done(); block_it.Advance()) {
4520 BlockEntryInstr* block = block_it.Current();
4521 if (JoinEntryInstr* join = block->AsJoinEntry()) {
4522 for (PhiIterator it(join); !it.Done(); it.Advance()) {
4523 PhiInstr* current = it.Current();
4524 if (!live.Contains(current->ssa_temp_index())) {
4525 it.RemoveCurrentFromGraph();
4526 }
4527 }
4528 }
4529 for (ForwardInstructionIterator it(block); !it.Done(); it.Advance()) {
4530 Instruction* current = it.Current();
4531 if (!current->CanEliminate(block)) {
4532 continue;
4533 }
4534 ASSERT(!current->IsMoveArgument());
4535 ASSERT((current->ArgumentCount() == 0) || !current->HasMoveArguments());
4536 if (Definition* def = current->AsDefinition()) {
4537 if (def->HasSSATemp() && live.Contains(def->ssa_temp_index())) {
4538 continue;
4539 }
4540 }
4541 it.RemoveCurrentFromGraph();
4542 }
4543 }
4544}
#define ASSERT(E)

◆ EliminateDeadPhis()

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

Definition at line 4394 of file redundancy_elimination.cc.

4394 {
4395 GrowableArray<PhiInstr*> live_phis;
4396 for (BlockIterator b = flow_graph->postorder_iterator(); !b.Done();
4397 b.Advance()) {
4398 JoinEntryInstr* join = b.Current()->AsJoinEntry();
4399 if (join != nullptr) {
4400 for (PhiIterator it(join); !it.Done(); it.Advance()) {
4401 PhiInstr* phi = it.Current();
4402 // Phis that have uses and phis inside try blocks are
4403 // marked as live.
4404 if (HasRealUse(phi)) {
4405 live_phis.Add(phi);
4406 phi->mark_alive();
4407 } else {
4408 phi->mark_dead();
4409 }
4410 }
4411 }
4412 }
4413
4414 while (!live_phis.is_empty()) {
4415 PhiInstr* phi = live_phis.RemoveLast();
4416 for (intptr_t i = 0; i < phi->InputCount(); i++) {
4417 Value* val = phi->InputAt(i);
4418 PhiInstr* used_phi = val->definition()->AsPhi();
4419 if ((used_phi != nullptr) && !used_phi->is_alive()) {
4420 used_phi->mark_alive();
4421 live_phis.Add(used_phi);
4422 }
4423 }
4424 }
4425
4427}
static void RemoveDeadAndRedundantPhisFromTheGraph(FlowGraph *graph)
static bool b
static bool HasRealUse(Definition *def)
SINT Vec< 2 *N, T > join(const Vec< N, T > &lo, const Vec< N, T > &hi)
Definition SkVx.h:242

◆ RemoveDeadAndRedundantPhisFromTheGraph()

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

Definition at line 4429 of file redundancy_elimination.cc.

4430 {
4431 for (auto block : flow_graph->postorder()) {
4432 if (JoinEntryInstr* join = block->AsJoinEntry()) {
4433 if (join->phis_ == nullptr) continue;
4434
4435 // Eliminate dead phis and compact the phis_ array of the block.
4436 intptr_t to_index = 0;
4437 for (intptr_t i = 0; i < join->phis_->length(); ++i) {
4438 PhiInstr* phi = (*join->phis_)[i];
4439 if (phi != nullptr) {
4440 if (!phi->is_alive()) {
4441 phi->ReplaceUsesWith(flow_graph->constant_null());
4442 phi->UnuseAllInputs();
4443 (*join->phis_)[i] = nullptr;
4444 if (FLAG_trace_optimization && flow_graph->should_print()) {
4445 THR_Print("Removing dead phi v%" Pd "\n", phi->ssa_temp_index());
4446 }
4447 } else {
4448 (*join->phis_)[to_index++] = phi;
4449 }
4450 }
4451 }
4452 if (to_index == 0) {
4453 join->phis_ = nullptr;
4454 } else {
4455 join->phis_->TruncateTo(to_index);
4456 }
4457 }
4458 }
4459}
#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: