diff options
author | Nicolas Geoffray <ngeoffray@google.com> | 2014-11-18 16:53:35 +0000 |
---|---|---|
committer | Nicolas Geoffray <ngeoffray@google.com> | 2014-11-19 14:33:12 +0000 |
commit | 5e6916cea259897baaca019c5c7a5d05746306ed (patch) | |
tree | 0b4c73776c68e9ccbf4b49dbff5cdc9b3d7150f9 | |
parent | bf75c5cf32a47eecadcc5e4a324237c1f1d09cde (diff) | |
download | art-5e6916cea259897baaca019c5c7a5d05746306ed.tar.gz art-5e6916cea259897baaca019c5c7a5d05746306ed.tar.bz2 art-5e6916cea259897baaca019c5c7a5d05746306ed.zip |
Use HOptimization abstraction for running optimizations.
Move existing optimizations to it.
Change-Id: I3b43f9997faf4ed8875162e3a3abdf99375478dd
-rw-r--r-- | compiler/optimizing/constant_folding.h | 6 | ||||
-rw-r--r-- | compiler/optimizing/constant_folding_test.cc | 5 | ||||
-rw-r--r-- | compiler/optimizing/dead_code_elimination.h | 6 | ||||
-rw-r--r-- | compiler/optimizing/dead_code_elimination_test.cc | 3 | ||||
-rw-r--r-- | compiler/optimizing/gvn.h | 10 | ||||
-rw-r--r-- | compiler/optimizing/instruction_simplifier.cc | 19 | ||||
-rw-r--r-- | compiler/optimizing/instruction_simplifier.h | 13 | ||||
-rw-r--r-- | compiler/optimizing/optimization.cc | 6 | ||||
-rw-r--r-- | compiler/optimizing/optimization.h | 13 | ||||
-rw-r--r-- | compiler/optimizing/optimizing_compiler.cc | 29 | ||||
-rw-r--r-- | compiler/optimizing/ssa_phi_elimination.h | 17 |
11 files changed, 65 insertions, 62 deletions
diff --git a/compiler/optimizing/constant_folding.h b/compiler/optimizing/constant_folding.h index d2acfa697..ac00824e3 100644 --- a/compiler/optimizing/constant_folding.h +++ b/compiler/optimizing/constant_folding.h @@ -32,10 +32,10 @@ namespace art { */ class HConstantFolding : public HOptimization { public: - HConstantFolding(HGraph* graph, const HGraphVisualizer& visualizer) - : HOptimization(graph, true, kConstantFoldingPassName, visualizer) {} + explicit HConstantFolding(HGraph* graph) + : HOptimization(graph, true, kConstantFoldingPassName) {} - virtual void Run() OVERRIDE; + void Run() OVERRIDE; static constexpr const char* kConstantFoldingPassName = "constant_folding"; diff --git a/compiler/optimizing/constant_folding_test.cc b/compiler/optimizing/constant_folding_test.cc index 856c5165a..a56b9d9a1 100644 --- a/compiler/optimizing/constant_folding_test.cc +++ b/compiler/optimizing/constant_folding_test.cc @@ -47,8 +47,7 @@ static void TestCode(const uint16_t* data, ASSERT_EQ(expected_before, actual_before); x86::CodeGeneratorX86 codegen(graph); - HGraphVisualizer visualizer(nullptr, graph, codegen, ""); - HConstantFolding(graph, visualizer).Run(); + HConstantFolding(graph).Run(); SSAChecker ssa_checker(&allocator, graph); ssa_checker.Run(); ASSERT_TRUE(ssa_checker.IsValid()); @@ -60,7 +59,7 @@ static void TestCode(const uint16_t* data, check_after_cf(graph); - HDeadCodeElimination(graph, visualizer).Run(); + HDeadCodeElimination(graph).Run(); ssa_checker.Run(); ASSERT_TRUE(ssa_checker.IsValid()); diff --git a/compiler/optimizing/dead_code_elimination.h b/compiler/optimizing/dead_code_elimination.h index a4446ae04..3db2c3ff3 100644 --- a/compiler/optimizing/dead_code_elimination.h +++ b/compiler/optimizing/dead_code_elimination.h @@ -28,10 +28,10 @@ namespace art { */ class HDeadCodeElimination : public HOptimization { public: - HDeadCodeElimination(HGraph* graph, const HGraphVisualizer& visualizer) - : HOptimization(graph, true, kDeadCodeEliminationPassName, visualizer) {} + explicit HDeadCodeElimination(HGraph* graph) + : HOptimization(graph, true, kDeadCodeEliminationPassName) {} - virtual void Run() OVERRIDE; + void Run() OVERRIDE; static constexpr const char* kDeadCodeEliminationPassName = "dead_code_elimination"; diff --git a/compiler/optimizing/dead_code_elimination_test.cc b/compiler/optimizing/dead_code_elimination_test.cc index 0c6807482..5d4b9cb02 100644 --- a/compiler/optimizing/dead_code_elimination_test.cc +++ b/compiler/optimizing/dead_code_elimination_test.cc @@ -41,8 +41,7 @@ static void TestCode(const uint16_t* data, ASSERT_EQ(actual_before, expected_before); x86::CodeGeneratorX86 codegen(graph); - HGraphVisualizer visualizer(nullptr, graph, codegen, ""); - HDeadCodeElimination(graph, visualizer).Run(); + HDeadCodeElimination(graph).Run(); SSAChecker ssa_checker(&allocator, graph); ssa_checker.Run(); ASSERT_TRUE(ssa_checker.IsValid()); diff --git a/compiler/optimizing/gvn.h b/compiler/optimizing/gvn.h index 8d2c77475..a841d5f65 100644 --- a/compiler/optimizing/gvn.h +++ b/compiler/optimizing/gvn.h @@ -18,6 +18,7 @@ #define ART_COMPILER_OPTIMIZING_GVN_H_ #include "nodes.h" +#include "optimization.h" namespace art { @@ -165,11 +166,11 @@ class ValueSet : public ArenaObject<kArenaAllocMisc> { /** * Optimization phase that removes redundant instruction. */ -class GlobalValueNumberer : public ValueObject { +class GlobalValueNumberer : public HOptimization { public: GlobalValueNumberer(ArenaAllocator* allocator, HGraph* graph) - : allocator_(allocator), - graph_(graph), + : HOptimization(graph, true, "GVN"), + allocator_(allocator), block_effects_(allocator, graph->GetBlocks().Size()), loop_effects_(allocator, graph->GetBlocks().Size()), sets_(allocator, graph->GetBlocks().Size()), @@ -186,7 +187,7 @@ class GlobalValueNumberer : public ValueObject { } } - void Run(); + void Run() OVERRIDE; private: // Per-block GVN. Will also update the ValueSet of the dominated and @@ -202,7 +203,6 @@ class GlobalValueNumberer : public ValueObject { SideEffects GetBlockEffects(HBasicBlock* block) const; ArenaAllocator* const allocator_; - HGraph* const graph_; // Side effects of individual blocks, that is the union of the side effects // of the instructions in the block. diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc index 3e8361eca..3d65e9a0a 100644 --- a/compiler/optimizing/instruction_simplifier.cc +++ b/compiler/optimizing/instruction_simplifier.cc @@ -18,11 +18,22 @@ namespace art { +class InstructionSimplifierVisitor : public HGraphVisitor { + public: + explicit InstructionSimplifierVisitor(HGraph* graph) : HGraphVisitor(graph) {} + + private: + void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE; + void VisitEqual(HEqual* equal) OVERRIDE; + void VisitArraySet(HArraySet* equal) OVERRIDE; +}; + void InstructionSimplifier::Run() { - VisitInsertionOrder(); + InstructionSimplifierVisitor visitor(graph_); + visitor.VisitInsertionOrder(); } -void InstructionSimplifier::VisitSuspendCheck(HSuspendCheck* check) { +void InstructionSimplifierVisitor::VisitSuspendCheck(HSuspendCheck* check) { HBasicBlock* block = check->GetBlock(); // Currently always keep the suspend check at entry. if (block->IsEntryBlock()) return; @@ -38,7 +49,7 @@ void InstructionSimplifier::VisitSuspendCheck(HSuspendCheck* check) { block->RemoveInstruction(check); } -void InstructionSimplifier::VisitEqual(HEqual* equal) { +void InstructionSimplifierVisitor::VisitEqual(HEqual* equal) { HInstruction* input1 = equal->InputAt(0); HInstruction* input2 = equal->InputAt(1); if (input1->GetType() == Primitive::kPrimBoolean && input2->IsIntConstant()) { @@ -55,7 +66,7 @@ void InstructionSimplifier::VisitEqual(HEqual* equal) { } } -void InstructionSimplifier::VisitArraySet(HArraySet* instruction) { +void InstructionSimplifierVisitor::VisitArraySet(HArraySet* instruction) { HInstruction* value = instruction->GetValue(); if (value->GetType() != Primitive::kPrimNot) return; diff --git a/compiler/optimizing/instruction_simplifier.h b/compiler/optimizing/instruction_simplifier.h index 3844d5743..7068c7fc1 100644 --- a/compiler/optimizing/instruction_simplifier.h +++ b/compiler/optimizing/instruction_simplifier.h @@ -18,22 +18,19 @@ #define ART_COMPILER_OPTIMIZING_INSTRUCTION_SIMPLIFIER_H_ #include "nodes.h" +#include "optimization.h" namespace art { /** * Implements optimizations specific to each instruction. */ -class InstructionSimplifier : public HGraphVisitor { +class InstructionSimplifier : public HOptimization { public: - explicit InstructionSimplifier(HGraph* graph) : HGraphVisitor(graph) {} + explicit InstructionSimplifier(HGraph* graph) + : HOptimization(graph, true, "instruction_simplifier") {} - void Run(); - - private: - virtual void VisitSuspendCheck(HSuspendCheck* check) OVERRIDE; - virtual void VisitEqual(HEqual* equal) OVERRIDE; - virtual void VisitArraySet(HArraySet* equal) OVERRIDE; + void Run() OVERRIDE; }; } // namespace art diff --git a/compiler/optimizing/optimization.cc b/compiler/optimizing/optimization.cc index ea98186d1..d1178d579 100644 --- a/compiler/optimizing/optimization.cc +++ b/compiler/optimizing/optimization.cc @@ -21,12 +21,6 @@ namespace art { -void HOptimization::Execute() { - Run(); - visualizer_.DumpGraph(pass_name_); - Check(); -} - void HOptimization::Check() { if (kIsDebugBuild) { if (is_in_ssa_form_) { diff --git a/compiler/optimizing/optimization.h b/compiler/optimizing/optimization.h index 59683e207..d281248f4 100644 --- a/compiler/optimizing/optimization.h +++ b/compiler/optimizing/optimization.h @@ -29,25 +29,19 @@ class HOptimization : public ValueObject { public: HOptimization(HGraph* graph, bool is_in_ssa_form, - const char* pass_name, - const HGraphVisualizer& visualizer) + const char* pass_name) : graph_(graph), is_in_ssa_form_(is_in_ssa_form), - pass_name_(pass_name), - visualizer_(visualizer) {} + pass_name_(pass_name) {} virtual ~HOptimization() {} - // Execute the optimization pass. - void Execute(); - // Return the name of the pass. const char* GetPassName() const { return pass_name_; } // Peform the analysis itself. virtual void Run() = 0; - private: // Verify the graph; abort if it is not valid. void Check(); @@ -59,9 +53,6 @@ class HOptimization : public ValueObject { const bool is_in_ssa_form_; // Optimization pass name. const char* pass_name_; - // A graph visualiser invoked after the execution of the optimization - // pass if enabled. - const HGraphVisualizer& visualizer_; DISALLOW_COPY_AND_ASSIGN(HOptimization); }; diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc index 0de090752..83a1e11a9 100644 --- a/compiler/optimizing/optimizing_compiler.cc +++ b/compiler/optimizing/optimizing_compiler.cc @@ -189,6 +189,25 @@ static bool CanOptimize(const DexFile::CodeItem& code_item) { return code_item.tries_size_ == 0; } +static void RunOptimizations(HGraph* graph, const HGraphVisualizer& visualizer) { + HDeadCodeElimination opt1(graph); + HConstantFolding opt2(graph); + SsaRedundantPhiElimination opt3(graph); + SsaDeadPhiElimination opt4(graph); + InstructionSimplifier opt5(graph); + GlobalValueNumberer opt6(graph->GetArena(), graph); + InstructionSimplifier opt7(graph); + + HOptimization* optimizations[] = { &opt1, &opt2, &opt3, &opt4, &opt5, &opt6, &opt7 }; + + for (size_t i = 0; i < arraysize(optimizations); ++i) { + HOptimization* optimization = optimizations[i]; + optimization->Run(); + optimization->Check(); + visualizer.DumpGraph(optimization->GetPassName()); + } +} + CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, uint32_t access_flags, InvokeType invoke_type, @@ -256,17 +275,9 @@ CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, visualizer.DumpGraph("ssa"); graph->FindNaturalLoops(); - HDeadCodeElimination(graph, visualizer).Execute(); - HConstantFolding(graph, visualizer).Execute(); + RunOptimizations(graph, visualizer); - SsaRedundantPhiElimination(graph).Run(); - SsaDeadPhiElimination(graph).Run(); - InstructionSimplifier(graph).Run(); - GlobalValueNumberer(graph->GetArena(), graph).Run(); - visualizer.DumpGraph(kGVNPassName); - InstructionSimplifier(graph).Run(); PrepareForRegisterAllocation(graph).Run(); - SsaLivenessAnalysis liveness(*graph, codegen); liveness.Analyze(); visualizer.DumpGraph(kLivenessPassName); diff --git a/compiler/optimizing/ssa_phi_elimination.h b/compiler/optimizing/ssa_phi_elimination.h index 5274f09f3..b7899712d 100644 --- a/compiler/optimizing/ssa_phi_elimination.h +++ b/compiler/optimizing/ssa_phi_elimination.h @@ -18,6 +18,7 @@ #define ART_COMPILER_OPTIMIZING_SSA_PHI_ELIMINATION_H_ #include "nodes.h" +#include "optimization.h" namespace art { @@ -25,15 +26,15 @@ namespace art { * Optimization phase that removes dead phis from the graph. Dead phis are unused * phis, or phis only used by other phis. */ -class SsaDeadPhiElimination : public ValueObject { +class SsaDeadPhiElimination : public HOptimization { public: explicit SsaDeadPhiElimination(HGraph* graph) - : graph_(graph), worklist_(graph->GetArena(), kDefaultWorklistSize) {} + : HOptimization(graph, true, "dead_phi_elimination"), + worklist_(graph->GetArena(), kDefaultWorklistSize) {} - void Run(); + void Run() OVERRIDE; private: - HGraph* const graph_; GrowableArray<HPhi*> worklist_; static constexpr size_t kDefaultWorklistSize = 8; @@ -47,15 +48,15 @@ class SsaDeadPhiElimination : public ValueObject { * registers might be updated with the same value, or not updated at all. We can just * replace the phi with the value when entering the loop. */ -class SsaRedundantPhiElimination : public ValueObject { +class SsaRedundantPhiElimination : public HOptimization { public: explicit SsaRedundantPhiElimination(HGraph* graph) - : graph_(graph), worklist_(graph->GetArena(), kDefaultWorklistSize) {} + : HOptimization(graph, true, "redundant_phi_elimination"), + worklist_(graph->GetArena(), kDefaultWorklistSize) {} - void Run(); + void Run() OVERRIDE; private: - HGraph* const graph_; GrowableArray<HPhi*> worklist_; static constexpr size_t kDefaultWorklistSize = 8; |