blob: 5ab328fe23884ca0c0790a2ac43071f18831e11b [file] [log] [blame]
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_
18#define ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_
19
20#include "nodes.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000021#include "optimization.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010022
23namespace art {
24
Nicolas Geoffray31596742014-11-24 15:28:45 +000025class TransformToSsa : public HOptimization {
26 public:
27 explicit TransformToSsa(HGraph* graph) : HOptimization(graph, true, "ssa transform") {}
28
29 void Run() OVERRIDE {
30 graph_->BuildDominatorTree();
31 graph_->TransformToSSA();
32 graph_->FindNaturalLoops();
33 }
34
35 private:
36 DISALLOW_COPY_AND_ASSIGN(TransformToSsa);
37};
38
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010039static constexpr int kDefaultNumberOfLoops = 2;
40
41class SsaBuilder : public HGraphVisitor {
42 public:
43 explicit SsaBuilder(HGraph* graph)
44 : HGraphVisitor(graph),
45 current_locals_(nullptr),
46 loop_headers_(graph->GetArena(), kDefaultNumberOfLoops),
Nicolas Geoffray804d0932014-05-02 08:46:00 +010047 locals_for_(graph->GetArena(), graph->GetBlocks().Size()) {
48 locals_for_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049 }
50
51 void BuildSsa();
52
53 GrowableArray<HInstruction*>* GetLocalsFor(HBasicBlock* block) {
54 HEnvironment* env = locals_for_.Get(block->GetBlockId());
55 if (env == nullptr) {
56 env = new (GetGraph()->GetArena()) HEnvironment(
57 GetGraph()->GetArena(), GetGraph()->GetNumberOfVRegs());
58 locals_for_.Put(block->GetBlockId(), env);
59 }
60 return env->GetVRegs();
61 }
62
63 HInstruction* ValueOfLocal(HBasicBlock* block, size_t local);
64
65 void VisitBasicBlock(HBasicBlock* block);
66 void VisitLoadLocal(HLoadLocal* load);
67 void VisitStoreLocal(HStoreLocal* store);
68 void VisitInstruction(HInstruction* instruction);
Nicolas Geoffray421e9f92014-11-11 18:21:53 +000069 void VisitTemporary(HTemporary* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010071 static HInstruction* GetFloatOrDoubleEquivalent(HInstruction* user,
72 HInstruction* instruction,
73 Primitive::Type type);
74
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010075 private:
76 // Locals for the current block being visited.
77 GrowableArray<HInstruction*>* current_locals_;
78
79 // Keep track of loop headers found. The last phase of the analysis iterates
80 // over these blocks to set the inputs of their phis.
81 GrowableArray<HBasicBlock*> loop_headers_;
82
83 // HEnvironment for each block.
84 GrowableArray<HEnvironment*> locals_for_;
85
86 DISALLOW_COPY_AND_ASSIGN(SsaBuilder);
87};
88
89} // namespace art
90
91#endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_