blob: 60831a9e6af9df2c99683b5c88bc387d14271db9 [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 Marko69d310e2017-10-09 14:12:23 +010020#include "base/scoped_arena_allocator.h"
21#include "base/scoped_arena_containers.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010022#include "nodes.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000023#include "optimization.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010024
25namespace art {
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
Nicolas Geoffray915b9d02015-03-11 15:11:19 +000036 * environment uses. Note that it does not imply the instruction will
37 * have a physical location after register allocation. See the
38 * SsaLivenessAnalysis phase.
Nicolas Geoffraye0fe7ae2015-03-09 10:02:49 +000039 *
40 * (b) Dex registers that require merging, and the merging gives
41 * incompatible types, will be killed for environment uses of that merge.
42 *
43 * (c) When the `debuggable` flag is passed to the compiler, Dex registers
44 * that require merging and have a proper type after the merge, are
45 * available to all their environment uses. If the `debuggable` flag
46 * is not set, values of Dex registers only used by environments
47 * are killed.
48 */
David Brazdildee58d62016-04-07 09:54:26 +000049class SsaBuilder : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010050 public:
Vladimir Marko456307a2016-04-19 14:12:13 +000051 SsaBuilder(HGraph* graph,
Vladimir Marko8d6768d2017-03-14 10:13:21 +000052 Handle<mirror::ClassLoader> class_loader,
Vladimir Marko456307a2016-04-19 14:12:13 +000053 Handle<mirror::DexCache> dex_cache,
Vladimir Marko69d310e2017-10-09 14:12:23 +010054 VariableSizedHandleScope* handles,
55 ScopedArenaAllocator* local_allocator)
David Brazdildee58d62016-04-07 09:54:26 +000056 : graph_(graph),
Vladimir Marko8d6768d2017-03-14 10:13:21 +000057 class_loader_(class_loader),
Vladimir Marko456307a2016-04-19 14:12:13 +000058 dex_cache_(dex_cache),
David Brazdil4833f5a2015-12-16 10:37:39 +000059 handles_(handles),
60 agets_fixed_(false),
Vladimir Marko69d310e2017-10-09 14:12:23 +010061 local_allocator_(local_allocator),
62 ambiguous_agets_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
63 ambiguous_asets_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
64 uninitialized_strings_(local_allocator->Adapter(kArenaAllocGraphBuilder)) {
David Brazdildee58d62016-04-07 09:54:26 +000065 graph_->InitializeInexactObjectRTI(handles);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010066 }
67
Nicolas Geoffray15bd2282016-01-05 15:55:41 +000068 GraphAnalysisResult BuildSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010070 HInstruction* GetFloatOrDoubleEquivalent(HInstruction* instruction, DataType::Type type);
David Brazdildee58d62016-04-07 09:54:26 +000071 HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010072
David Brazdildee58d62016-04-07 09:54:26 +000073 void MaybeAddAmbiguousArrayGet(HArrayGet* aget) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010074 DataType::Type type = aget->GetType();
75 DCHECK(!DataType::IsFloatingPointType(type));
76 if (DataType::IsIntOrLongType(type)) {
David Brazdildee58d62016-04-07 09:54:26 +000077 ambiguous_agets_.push_back(aget);
78 }
79 }
80
81 void MaybeAddAmbiguousArraySet(HArraySet* aset) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010082 DataType::Type type = aset->GetValue()->GetType();
83 if (DataType::IsIntOrLongType(type)) {
David Brazdildee58d62016-04-07 09:54:26 +000084 ambiguous_asets_.push_back(aset);
85 }
86 }
87
88 void AddUninitializedString(HNewInstance* string) {
89 // In some rare cases (b/27847265), the same NewInstance may be seen
90 // multiple times. We should only consider it once for removal, so we
91 // ensure it is not added more than once.
92 // Note that we cannot check whether this really is a NewInstance of String
93 // before RTP. We DCHECK that in RemoveRedundantUninitializedStrings.
94 if (!ContainsElement(uninitialized_strings_, string)) {
95 uninitialized_strings_.push_back(string);
96 }
97 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000098
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010099 private:
David Brazdil809d70f2015-11-19 10:29:39 +0000100 void SetLoopHeaderPhiInputs();
David Brazdil4833f5a2015-12-16 10:37:39 +0000101 void FixEnvironmentPhis();
Calin Juravlea4f88312015-04-16 12:57:19 +0100102 void FixNullConstantType();
103 void EquivalentPhisCleanup();
David Brazdil4833f5a2015-12-16 10:37:39 +0000104 void RunPrimitiveTypePropagation();
Calin Juravlea4f88312015-04-16 12:57:19 +0100105
David Brazdil15693bf2015-12-16 10:30:45 +0000106 // Attempts to resolve types of aget(-wide) instructions and type values passed
107 // to aput(-wide) instructions from reference type information on the array
108 // input. Returns false if the type of an array is unknown.
109 bool FixAmbiguousArrayOps();
David Brazdil4833f5a2015-12-16 10:37:39 +0000110
Vladimir Marko69d310e2017-10-09 14:12:23 +0100111 bool TypeInputsOfPhi(HPhi* phi, ScopedArenaVector<HPhi*>* worklist);
112 bool UpdatePrimitiveType(HPhi* phi, ScopedArenaVector<HPhi*>* worklist);
113 void ProcessPrimitiveTypePropagationWorklist(ScopedArenaVector<HPhi*>* worklist);
David Brazdil4833f5a2015-12-16 10:37:39 +0000114
David Brazdil4833f5a2015-12-16 10:37:39 +0000115 HFloatConstant* GetFloatEquivalent(HIntConstant* constant);
116 HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100117 HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, DataType::Type type);
David Brazdil4833f5a2015-12-16 10:37:39 +0000118 HArrayGet* GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget);
119
David Brazdil65902e82016-01-15 09:35:13 +0000120 void RemoveRedundantUninitializedStrings();
121
Vladimir Marko69d310e2017-10-09 14:12:23 +0100122 HGraph* const graph_;
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000123 Handle<mirror::ClassLoader> class_loader_;
Vladimir Marko456307a2016-04-19 14:12:13 +0000124 Handle<mirror::DexCache> dex_cache_;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700125 VariableSizedHandleScope* const handles_;
David Brazdil4833f5a2015-12-16 10:37:39 +0000126
127 // True if types of ambiguous ArrayGets have been resolved.
128 bool agets_fixed_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000129
Vladimir Marko69d310e2017-10-09 14:12:23 +0100130 ScopedArenaAllocator* const local_allocator_;
131 ScopedArenaVector<HArrayGet*> ambiguous_agets_;
132 ScopedArenaVector<HArraySet*> ambiguous_asets_;
133 ScopedArenaVector<HNewInstance*> uninitialized_strings_;
David Brazdil4833f5a2015-12-16 10:37:39 +0000134
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100135 DISALLOW_COPY_AND_ASSIGN(SsaBuilder);
136};
137
138} // namespace art
139
140#endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_