blob: 99a546993263ae66fa345c626b7e8332ff649942 [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
Vladimír Marko434d9682022-11-04 14:04:17 +000020#include "base/macros.h"
Vladimir Marko69d310e2017-10-09 14:12:23 +010021#include "base/scoped_arena_allocator.h"
22#include "base/scoped_arena_containers.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010023#include "nodes.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000024#include "optimization.h"
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010025
Vladimír Marko434d9682022-11-04 14:04:17 +000026namespace art HIDDEN {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010027
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 */
David Brazdildee58d62016-04-07 09:54:26 +000050class SsaBuilder : public ValueObject {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010051 public:
Vladimir Marko456307a2016-04-19 14:12:13 +000052 SsaBuilder(HGraph* graph,
Vladimir Marko8d6768d2017-03-14 10:13:21 +000053 Handle<mirror::ClassLoader> class_loader,
Vladimir Marko456307a2016-04-19 14:12:13 +000054 Handle<mirror::DexCache> dex_cache,
Vladimir Marko69d310e2017-10-09 14:12:23 +010055 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 agets_fixed_(false),
Vladimir Marko69d310e2017-10-09 14:12:23 +010060 local_allocator_(local_allocator),
61 ambiguous_agets_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
62 ambiguous_asets_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +010063 uninitialized_strings_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
64 uninitialized_string_phis_(local_allocator->Adapter(kArenaAllocGraphBuilder)) {
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010065 }
66
Nicolas Geoffray15bd2282016-01-05 15:55:41 +000067 GraphAnalysisResult BuildSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010069 HInstruction* GetFloatOrDoubleEquivalent(HInstruction* instruction, DataType::Type type);
David Brazdildee58d62016-04-07 09:54:26 +000070 HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071
David Brazdildee58d62016-04-07 09:54:26 +000072 void MaybeAddAmbiguousArrayGet(HArrayGet* aget) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073 DataType::Type type = aget->GetType();
74 DCHECK(!DataType::IsFloatingPointType(type));
75 if (DataType::IsIntOrLongType(type)) {
David Brazdildee58d62016-04-07 09:54:26 +000076 ambiguous_agets_.push_back(aget);
77 }
78 }
79
80 void MaybeAddAmbiguousArraySet(HArraySet* aset) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010081 DataType::Type type = aset->GetValue()->GetType();
82 if (DataType::IsIntOrLongType(type)) {
David Brazdildee58d62016-04-07 09:54:26 +000083 ambiguous_asets_.push_back(aset);
84 }
85 }
86
87 void AddUninitializedString(HNewInstance* string) {
88 // In some rare cases (b/27847265), the same NewInstance may be seen
89 // multiple times. We should only consider it once for removal, so we
90 // ensure it is not added more than once.
91 // Note that we cannot check whether this really is a NewInstance of String
92 // before RTP. We DCHECK that in RemoveRedundantUninitializedStrings.
93 if (!ContainsElement(uninitialized_strings_, string)) {
94 uninitialized_strings_.push_back(string);
95 }
96 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +000097
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +010098 void AddUninitializedStringPhi(HInvoke* invoke) {
99 uninitialized_string_phis_.push_back(invoke);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100100 }
101
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100102 private:
David Brazdil809d70f2015-11-19 10:29:39 +0000103 void SetLoopHeaderPhiInputs();
David Brazdil4833f5a2015-12-16 10:37:39 +0000104 void FixEnvironmentPhis();
Calin Juravlea4f88312015-04-16 12:57:19 +0100105 void FixNullConstantType();
106 void EquivalentPhisCleanup();
David Brazdil4833f5a2015-12-16 10:37:39 +0000107 void RunPrimitiveTypePropagation();
Calin Juravlea4f88312015-04-16 12:57:19 +0100108
David Brazdil15693bf2015-12-16 10:30:45 +0000109 // Attempts to resolve types of aget(-wide) instructions and type values passed
110 // to aput(-wide) instructions from reference type information on the array
111 // input. Returns false if the type of an array is unknown.
112 bool FixAmbiguousArrayOps();
David Brazdil4833f5a2015-12-16 10:37:39 +0000113
Vladimir Marko69d310e2017-10-09 14:12:23 +0100114 bool TypeInputsOfPhi(HPhi* phi, ScopedArenaVector<HPhi*>* worklist);
115 bool UpdatePrimitiveType(HPhi* phi, ScopedArenaVector<HPhi*>* worklist);
116 void ProcessPrimitiveTypePropagationWorklist(ScopedArenaVector<HPhi*>* worklist);
David Brazdil4833f5a2015-12-16 10:37:39 +0000117
David Brazdil4833f5a2015-12-16 10:37:39 +0000118 HFloatConstant* GetFloatEquivalent(HIntConstant* constant);
119 HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100120 HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, DataType::Type type);
David Brazdil4833f5a2015-12-16 10:37:39 +0000121 HArrayGet* GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget);
122
David Brazdil65902e82016-01-15 09:35:13 +0000123 void RemoveRedundantUninitializedStrings();
Nicolas Geoffray639f2792018-09-25 00:39:48 +0100124 bool ReplaceUninitializedStringPhis();
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100125 bool HasAliasInEnvironments(HInstruction* instruction);
David Brazdil65902e82016-01-15 09:35:13 +0000126
Vladimir Marko69d310e2017-10-09 14:12:23 +0100127 HGraph* const graph_;
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000128 Handle<mirror::ClassLoader> class_loader_;
Vladimir Marko456307a2016-04-19 14:12:13 +0000129 Handle<mirror::DexCache> dex_cache_;
David Brazdil4833f5a2015-12-16 10:37:39 +0000130
131 // True if types of ambiguous ArrayGets have been resolved.
132 bool agets_fixed_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000133
Vladimir Marko69d310e2017-10-09 14:12:23 +0100134 ScopedArenaAllocator* const local_allocator_;
135 ScopedArenaVector<HArrayGet*> ambiguous_agets_;
136 ScopedArenaVector<HArraySet*> ambiguous_asets_;
137 ScopedArenaVector<HNewInstance*> uninitialized_strings_;
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100138 ScopedArenaVector<HInvoke*> uninitialized_string_phis_;
David Brazdil4833f5a2015-12-16 10:37:39 +0000139
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100140 DISALLOW_COPY_AND_ASSIGN(SsaBuilder);
141};
142
143} // namespace art
144
145#endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_