blob: 978f113ec4673840e5643fb8617955efb4663ab3 [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
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000026/**
27 * Transforms a graph into SSA form. The liveness guarantees of
28 * this transformation are listed below. A DEX register
29 * being killed means its value at a given position in the code
30 * will not be available to its environment uses. A merge in the
31 * following text is materialized as a `HPhi`.
32 *
33 * (a) Dex registers that do not require merging (that is, they do not
34 * have different values at a join block) are available to all their
Nicolas Geoffray915b9d02015-03-11 15:11:19 +000035 * environment uses. Note that it does not imply the instruction will
36 * have a physical location after register allocation. See the
37 * SsaLivenessAnalysis phase.
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000038 *
39 * (b) Dex registers that require merging, and the merging gives
40 * incompatible types, will be killed for environment uses of that merge.
41 *
42 * (c) When the `debuggable` flag is passed to the compiler, Dex registers
43 * that require merging and have a proper type after the merge, are
44 * available to all their environment uses. If the `debuggable` flag
45 * is not set, values of Dex registers only used by environments
46 * are killed.
47 */
David Brazdildee58d62016-04-07 09:54:26 +000048class SsaBuilder : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010049 public:
Vladimir Marko456307a2016-04-19 14:12:13 +000050 SsaBuilder(HGraph* graph,
Vladimir Marko8d6768d2017-03-14 10:13:21 +000051 Handle<mirror::ClassLoader> class_loader,
Vladimir Marko456307a2016-04-19 14:12:13 +000052 Handle<mirror::DexCache> dex_cache,
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070053 VariableSizedHandleScope* handles)
David Brazdildee58d62016-04-07 09:54:26 +000054 : graph_(graph),
Vladimir Marko8d6768d2017-03-14 10:13:21 +000055 class_loader_(class_loader),
Vladimir Marko456307a2016-04-19 14:12:13 +000056 dex_cache_(dex_cache),
David Brazdil4833f5a2015-12-16 10:37:39 +000057 handles_(handles),
58 agets_fixed_(false),
David Brazdildee58d62016-04-07 09:54:26 +000059 ambiguous_agets_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)),
60 ambiguous_asets_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)),
61 uninitialized_strings_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) {
62 graph_->InitializeInexactObjectRTI(handles);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010063 }
64
Nicolas Geoffray15bd2282016-01-05 15:55:41 +000065 GraphAnalysisResult BuildSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066
David Brazdildee58d62016-04-07 09:54:26 +000067 HInstruction* GetFloatOrDoubleEquivalent(HInstruction* instruction, Primitive::Type type);
68 HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010069
David Brazdildee58d62016-04-07 09:54:26 +000070 void MaybeAddAmbiguousArrayGet(HArrayGet* aget) {
71 Primitive::Type type = aget->GetType();
72 DCHECK(!Primitive::IsFloatingPointType(type));
73 if (Primitive::IsIntOrLongType(type)) {
74 ambiguous_agets_.push_back(aget);
75 }
76 }
77
78 void MaybeAddAmbiguousArraySet(HArraySet* aset) {
79 Primitive::Type type = aset->GetValue()->GetType();
80 if (Primitive::IsIntOrLongType(type)) {
81 ambiguous_asets_.push_back(aset);
82 }
83 }
84
85 void AddUninitializedString(HNewInstance* string) {
86 // In some rare cases (b/27847265), the same NewInstance may be seen
87 // multiple times. We should only consider it once for removal, so we
88 // ensure it is not added more than once.
89 // Note that we cannot check whether this really is a NewInstance of String
90 // before RTP. We DCHECK that in RemoveRedundantUninitializedStrings.
91 if (!ContainsElement(uninitialized_strings_, string)) {
92 uninitialized_strings_.push_back(string);
93 }
94 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000095
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010096 private:
David Brazdil809d70f2015-11-19 10:29:39 +000097 void SetLoopHeaderPhiInputs();
David Brazdil4833f5a2015-12-16 10:37:39 +000098 void FixEnvironmentPhis();
Calin Juravlea4f88312015-04-16 12:57:19 +010099 void FixNullConstantType();
100 void EquivalentPhisCleanup();
David Brazdil4833f5a2015-12-16 10:37:39 +0000101 void RunPrimitiveTypePropagation();
Calin Juravlea4f88312015-04-16 12:57:19 +0100102
David Brazdil15693bf2015-12-16 10:30:45 +0000103 // Attempts to resolve types of aget(-wide) instructions and type values passed
104 // to aput(-wide) instructions from reference type information on the array
105 // input. Returns false if the type of an array is unknown.
106 bool FixAmbiguousArrayOps();
David Brazdil4833f5a2015-12-16 10:37:39 +0000107
108 bool TypeInputsOfPhi(HPhi* phi, ArenaVector<HPhi*>* worklist);
109 bool UpdatePrimitiveType(HPhi* phi, ArenaVector<HPhi*>* worklist);
110 void ProcessPrimitiveTypePropagationWorklist(ArenaVector<HPhi*>* worklist);
111
David Brazdil4833f5a2015-12-16 10:37:39 +0000112 HFloatConstant* GetFloatEquivalent(HIntConstant* constant);
113 HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant);
114 HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type);
115 HArrayGet* GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget);
116
David Brazdil65902e82016-01-15 09:35:13 +0000117 void RemoveRedundantUninitializedStrings();
118
David Brazdildee58d62016-04-07 09:54:26 +0000119 HGraph* graph_;
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000120 Handle<mirror::ClassLoader> class_loader_;
Vladimir Marko456307a2016-04-19 14:12:13 +0000121 Handle<mirror::DexCache> dex_cache_;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700122 VariableSizedHandleScope* const handles_;
David Brazdil4833f5a2015-12-16 10:37:39 +0000123
124 // True if types of ambiguous ArrayGets have been resolved.
125 bool agets_fixed_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000126
David Brazdil4833f5a2015-12-16 10:37:39 +0000127 ArenaVector<HArrayGet*> ambiguous_agets_;
David Brazdil15693bf2015-12-16 10:30:45 +0000128 ArenaVector<HArraySet*> ambiguous_asets_;
David Brazdil65902e82016-01-15 09:35:13 +0000129 ArenaVector<HNewInstance*> uninitialized_strings_;
David Brazdil4833f5a2015-12-16 10:37:39 +0000130
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100131 DISALLOW_COPY_AND_ASSIGN(SsaBuilder);
132};
133
134} // namespace art
135
136#endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_