blob: 9eb278875255b2eb9c242dd2aa4310c0ace2ced5 [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 Markoe2727152019-10-10 10:46:42 +010020#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
Vladimir Markoe2727152019-10-10 10:46:42 +010026namespace 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 VariableSizedHandleScope* handles,
56 ScopedArenaAllocator* local_allocator)
David Brazdildee58d62016-04-07 09:54:26 +000057 : graph_(graph),
Vladimir Marko8d6768d2017-03-14 10:13:21 +000058 class_loader_(class_loader),
Vladimir Marko456307a2016-04-19 14:12:13 +000059 dex_cache_(dex_cache),
David Brazdil4833f5a2015-12-16 10:37:39 +000060 handles_(handles),
61 agets_fixed_(false),
Vladimir Marko69d310e2017-10-09 14:12:23 +010062 local_allocator_(local_allocator),
63 ambiguous_agets_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
64 ambiguous_asets_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +010065 uninitialized_strings_(local_allocator->Adapter(kArenaAllocGraphBuilder)),
66 uninitialized_string_phis_(local_allocator->Adapter(kArenaAllocGraphBuilder)) {
David Brazdildee58d62016-04-07 09:54:26 +000067 graph_->InitializeInexactObjectRTI(handles);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010068 }
69
Nicolas Geoffray15bd2282016-01-05 15:55:41 +000070 GraphAnalysisResult BuildSsa();
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010072 HInstruction* GetFloatOrDoubleEquivalent(HInstruction* instruction, DataType::Type type);
David Brazdildee58d62016-04-07 09:54:26 +000073 HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction);
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +010074
David Brazdildee58d62016-04-07 09:54:26 +000075 void MaybeAddAmbiguousArrayGet(HArrayGet* aget) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010076 DataType::Type type = aget->GetType();
77 DCHECK(!DataType::IsFloatingPointType(type));
78 if (DataType::IsIntOrLongType(type)) {
David Brazdildee58d62016-04-07 09:54:26 +000079 ambiguous_agets_.push_back(aget);
80 }
81 }
82
83 void MaybeAddAmbiguousArraySet(HArraySet* aset) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010084 DataType::Type type = aset->GetValue()->GetType();
85 if (DataType::IsIntOrLongType(type)) {
David Brazdildee58d62016-04-07 09:54:26 +000086 ambiguous_asets_.push_back(aset);
87 }
88 }
89
90 void AddUninitializedString(HNewInstance* string) {
91 // In some rare cases (b/27847265), the same NewInstance may be seen
92 // multiple times. We should only consider it once for removal, so we
93 // ensure it is not added more than once.
94 // Note that we cannot check whether this really is a NewInstance of String
95 // before RTP. We DCHECK that in RemoveRedundantUninitializedStrings.
96 if (!ContainsElement(uninitialized_strings_, string)) {
97 uninitialized_strings_.push_back(string);
98 }
99 }
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000100
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100101 void AddUninitializedStringPhi(HInvoke* invoke) {
102 uninitialized_string_phis_.push_back(invoke);
Nicolas Geoffray8a62a4c2018-07-03 09:39:07 +0100103 }
104
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100105 private:
David Brazdil809d70f2015-11-19 10:29:39 +0000106 void SetLoopHeaderPhiInputs();
David Brazdil4833f5a2015-12-16 10:37:39 +0000107 void FixEnvironmentPhis();
Calin Juravlea4f88312015-04-16 12:57:19 +0100108 void FixNullConstantType();
109 void EquivalentPhisCleanup();
David Brazdil4833f5a2015-12-16 10:37:39 +0000110 void RunPrimitiveTypePropagation();
Calin Juravlea4f88312015-04-16 12:57:19 +0100111
David Brazdil15693bf2015-12-16 10:30:45 +0000112 // Attempts to resolve types of aget(-wide) instructions and type values passed
113 // to aput(-wide) instructions from reference type information on the array
114 // input. Returns false if the type of an array is unknown.
115 bool FixAmbiguousArrayOps();
David Brazdil4833f5a2015-12-16 10:37:39 +0000116
Vladimir Marko69d310e2017-10-09 14:12:23 +0100117 bool TypeInputsOfPhi(HPhi* phi, ScopedArenaVector<HPhi*>* worklist);
118 bool UpdatePrimitiveType(HPhi* phi, ScopedArenaVector<HPhi*>* worklist);
119 void ProcessPrimitiveTypePropagationWorklist(ScopedArenaVector<HPhi*>* worklist);
David Brazdil4833f5a2015-12-16 10:37:39 +0000120
David Brazdil4833f5a2015-12-16 10:37:39 +0000121 HFloatConstant* GetFloatEquivalent(HIntConstant* constant);
122 HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100123 HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, DataType::Type type);
David Brazdil4833f5a2015-12-16 10:37:39 +0000124 HArrayGet* GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget);
125
David Brazdil65902e82016-01-15 09:35:13 +0000126 void RemoveRedundantUninitializedStrings();
Nicolas Geoffray639f2792018-09-25 00:39:48 +0100127 bool ReplaceUninitializedStringPhis();
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100128 bool HasAliasInEnvironments(HInstruction* instruction);
David Brazdil65902e82016-01-15 09:35:13 +0000129
Vladimir Marko69d310e2017-10-09 14:12:23 +0100130 HGraph* const graph_;
Vladimir Marko8d6768d2017-03-14 10:13:21 +0000131 Handle<mirror::ClassLoader> class_loader_;
Vladimir Marko456307a2016-04-19 14:12:13 +0000132 Handle<mirror::DexCache> dex_cache_;
Mathieu Chartiere8a3c572016-10-11 16:52:17 -0700133 VariableSizedHandleScope* const handles_;
David Brazdil4833f5a2015-12-16 10:37:39 +0000134
135 // True if types of ambiguous ArrayGets have been resolved.
136 bool agets_fixed_;
David Brazdil8d5b8b22015-03-24 10:51:52 +0000137
Vladimir Marko69d310e2017-10-09 14:12:23 +0100138 ScopedArenaAllocator* const local_allocator_;
139 ScopedArenaVector<HArrayGet*> ambiguous_agets_;
140 ScopedArenaVector<HArraySet*> ambiguous_asets_;
141 ScopedArenaVector<HNewInstance*> uninitialized_strings_;
Nicolas Geoffray0846a8f2018-09-12 15:21:07 +0100142 ScopedArenaVector<HInvoke*> uninitialized_string_phis_;
David Brazdil4833f5a2015-12-16 10:37:39 +0000143
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +0100144 DISALLOW_COPY_AND_ASSIGN(SsaBuilder);
145};
146
147} // namespace art
148
149#endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_