blob: ed6f5cab51d7331bb77e899167aaafd54ba262e8 [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
Vladimir Marko71bf8092015-09-15 15:33:14 +010020#include "base/arena_containers.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010021#include "nodes.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000022#include "optimization.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010023
24namespace art {
25
26static constexpr int kDefaultNumberOfLoops = 2;
27
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000028/**
29 * Transforms a graph into SSA form. The liveness guarantees of
30 * this transformation are listed below. A DEX register
31 * being killed means its value at a given position in the code
32 * will not be available to its environment uses. A merge in the
33 * following text is materialized as a `HPhi`.
34 *
35 * (a) Dex registers that do not require merging (that is, they do not
36 * have different values at a join block) are available to all their
Nicolas Geoffray915b9d02015-03-11 15:11:19 +000037 * environment uses. Note that it does not imply the instruction will
38 * have a physical location after register allocation. See the
39 * SsaLivenessAnalysis phase.
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000040 *
41 * (b) Dex registers that require merging, and the merging gives
42 * incompatible types, will be killed for environment uses of that merge.
43 *
44 * (c) When the `debuggable` flag is passed to the compiler, Dex registers
45 * that require merging and have a proper type after the merge, are
46 * available to all their environment uses. If the `debuggable` flag
47 * is not set, values of Dex registers only used by environments
48 * are killed.
49 */
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050class SsaBuilder : public HGraphVisitor {
51 public:
David Brazdil4833f5a2015-12-16 10:37:39 +000052 explicit SsaBuilder(HGraph* graph, StackHandleScopeCollection* handles)
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010053 : HGraphVisitor(graph),
David Brazdil4833f5a2015-12-16 10:37:39 +000054 handles_(handles),
55 agets_fixed_(false),
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010056 current_locals_(nullptr),
Vladimir Marko71bf8092015-09-15 15:33:14 +010057 loop_headers_(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)),
David Brazdil4833f5a2015-12-16 10:37:39 +000058 ambiguous_agets_(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)),
Vladimir Marko71bf8092015-09-15 15:33:14 +010059 locals_for_(graph->GetBlocks().size(),
60 ArenaVector<HInstruction*>(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)),
61 graph->GetArena()->Adapter(kArenaAllocSsaBuilder)) {
62 loop_headers_.reserve(kDefaultNumberOfLoops);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063 }
64
David Brazdil4833f5a2015-12-16 10:37:39 +000065 BuildSsaResult BuildSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066
David Brazdileead0712015-09-18 14:58:57 +010067 // Returns locals vector for `block`. If it is a catch block, the vector will be
68 // prepopulated with catch phis for vregs which are defined in `current_locals_`.
69 ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010070 HInstruction* ValueOfLocal(HBasicBlock* block, size_t local);
71
72 void VisitBasicBlock(HBasicBlock* block);
73 void VisitLoadLocal(HLoadLocal* load);
74 void VisitStoreLocal(HStoreLocal* store);
75 void VisitInstruction(HInstruction* instruction);
Nicolas Geoffray421e9f92014-11-11 18:21:53 +000076 void VisitTemporary(HTemporary* instruction);
David Brazdil4833f5a2015-12-16 10:37:39 +000077 void VisitArrayGet(HArrayGet* aget);
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000078
Andreas Gampe7c3952f2015-02-19 18:21:24 -080079 static constexpr const char* kSsaBuilderPassName = "ssa_builder";
80
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010081 private:
David Brazdil809d70f2015-11-19 10:29:39 +000082 void SetLoopHeaderPhiInputs();
David Brazdil4833f5a2015-12-16 10:37:39 +000083 void FixEnvironmentPhis();
Calin Juravlea4f88312015-04-16 12:57:19 +010084 void FixNullConstantType();
85 void EquivalentPhisCleanup();
David Brazdil4833f5a2015-12-16 10:37:39 +000086 void RunPrimitiveTypePropagation();
Calin Juravlea4f88312015-04-16 12:57:19 +010087
David Brazdil4833f5a2015-12-16 10:37:39 +000088 // Attempts to resolve types of aget and aget-wide instructions from reference
89 // type information on the input array. Returns false if the type of the array
90 // is unknown.
91 bool FixAmbiguousArrayGets();
92
93 bool TypeInputsOfPhi(HPhi* phi, ArenaVector<HPhi*>* worklist);
94 bool UpdatePrimitiveType(HPhi* phi, ArenaVector<HPhi*>* worklist);
95 void ProcessPrimitiveTypePropagationWorklist(ArenaVector<HPhi*>* worklist);
96
97 HInstruction* GetFloatOrDoubleEquivalent(HInstruction* instruction, Primitive::Type type);
98 HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction);
99
100 HFloatConstant* GetFloatEquivalent(HIntConstant* constant);
101 HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant);
102 HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type);
103 HArrayGet* GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget);
104
105 StackHandleScopeCollection* const handles_;
106
107 // True if types of ambiguous ArrayGets have been resolved.
108 bool agets_fixed_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000109
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100110 // Locals for the current block being visited.
Vladimir Marko71bf8092015-09-15 15:33:14 +0100111 ArenaVector<HInstruction*>* current_locals_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100112
113 // Keep track of loop headers found. The last phase of the analysis iterates
114 // over these blocks to set the inputs of their phis.
Vladimir Marko71bf8092015-09-15 15:33:14 +0100115 ArenaVector<HBasicBlock*> loop_headers_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100116
David Brazdil4833f5a2015-12-16 10:37:39 +0000117 ArenaVector<HArrayGet*> ambiguous_agets_;
118
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100119 // HEnvironment for each block.
Vladimir Marko71bf8092015-09-15 15:33:14 +0100120 ArenaVector<ArenaVector<HInstruction*>> locals_for_;
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100121
122 DISALLOW_COPY_AND_ASSIGN(SsaBuilder);
123};
124
125} // namespace art
126
127#endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_