blob: b414fb2945d45b80c5aa71f3662270bdb77b0033 [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
25static constexpr int kDefaultNumberOfLoops = 2;
26
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000027/**
28 * Transforms a graph into SSA form. The liveness guarantees of
29 * this transformation are listed below. A DEX register
30 * being killed means its value at a given position in the code
31 * will not be available to its environment uses. A merge in the
32 * following text is materialized as a `HPhi`.
33 *
34 * (a) Dex registers that do not require merging (that is, they do not
35 * have different values at a join block) are available to all their
36 * environment uses.
37 *
38 * (b) Dex registers that require merging, and the merging gives
39 * incompatible types, will be killed for environment uses of that merge.
40 *
41 * (c) When the `debuggable` flag is passed to the compiler, Dex registers
42 * that require merging and have a proper type after the merge, are
43 * available to all their environment uses. If the `debuggable` flag
44 * is not set, values of Dex registers only used by environments
45 * are killed.
46 */
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010047class SsaBuilder : public HGraphVisitor {
48 public:
49 explicit SsaBuilder(HGraph* graph)
50 : HGraphVisitor(graph),
51 current_locals_(nullptr),
52 loop_headers_(graph->GetArena(), kDefaultNumberOfLoops),
Nicolas Geoffray804d0932014-05-02 08:46:00 +010053 locals_for_(graph->GetArena(), graph->GetBlocks().Size()) {
54 locals_for_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010055 }
56
57 void BuildSsa();
58
David Brazdiled596192015-01-23 10:39:45 +000059 HEnvironment* GetLocalsFor(HBasicBlock* block) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010060 HEnvironment* env = locals_for_.Get(block->GetBlockId());
61 if (env == nullptr) {
62 env = new (GetGraph()->GetArena()) HEnvironment(
63 GetGraph()->GetArena(), GetGraph()->GetNumberOfVRegs());
64 locals_for_.Put(block->GetBlockId(), env);
65 }
David Brazdiled596192015-01-23 10:39:45 +000066 return env;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010067 }
68
69 HInstruction* ValueOfLocal(HBasicBlock* block, size_t local);
70
71 void VisitBasicBlock(HBasicBlock* block);
72 void VisitLoadLocal(HLoadLocal* load);
73 void VisitStoreLocal(HStoreLocal* store);
74 void VisitInstruction(HInstruction* instruction);
Nicolas Geoffray421e9f92014-11-11 18:21:53 +000075 void VisitTemporary(HTemporary* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010076
Nicolas Geoffray102cbed2014-10-15 18:31:05 +010077 static HInstruction* GetFloatOrDoubleEquivalent(HInstruction* user,
78 HInstruction* instruction,
79 Primitive::Type type);
80
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000081 static HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction);
82
Andreas Gampe7c3952f2015-02-19 18:21:24 -080083 static constexpr const char* kSsaBuilderPassName = "ssa_builder";
84
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010085 private:
86 // Locals for the current block being visited.
David Brazdiled596192015-01-23 10:39:45 +000087 HEnvironment* current_locals_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010088
89 // Keep track of loop headers found. The last phase of the analysis iterates
90 // over these blocks to set the inputs of their phis.
91 GrowableArray<HBasicBlock*> loop_headers_;
92
93 // HEnvironment for each block.
94 GrowableArray<HEnvironment*> locals_for_;
95
96 DISALLOW_COPY_AND_ASSIGN(SsaBuilder);
97};
98
99} // namespace art
100
101#endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_