Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 1 | /* |
| 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 Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 20 | #include "base/scoped_arena_allocator.h" |
| 21 | #include "base/scoped_arena_containers.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 22 | #include "nodes.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 23 | #include "optimization.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 27 | /** |
| 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 Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 36 | * 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 Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 39 | * |
| 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 Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 49 | class SsaBuilder : public ValueObject { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 50 | public: |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 51 | SsaBuilder(HGraph* graph, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 52 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 53 | Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 54 | VariableSizedHandleScope* handles, |
| 55 | ScopedArenaAllocator* local_allocator) |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 56 | : graph_(graph), |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 57 | class_loader_(class_loader), |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 58 | dex_cache_(dex_cache), |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 59 | handles_(handles), |
| 60 | agets_fixed_(false), |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 61 | 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 Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 65 | graph_->InitializeInexactObjectRTI(handles); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 66 | } |
| 67 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 68 | GraphAnalysisResult BuildSsa(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 69 | |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 70 | HInstruction* GetFloatOrDoubleEquivalent(HInstruction* instruction, DataType::Type type); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 71 | HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 72 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 73 | void MaybeAddAmbiguousArrayGet(HArrayGet* aget) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 74 | DataType::Type type = aget->GetType(); |
| 75 | DCHECK(!DataType::IsFloatingPointType(type)); |
| 76 | if (DataType::IsIntOrLongType(type)) { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 77 | ambiguous_agets_.push_back(aget); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void MaybeAddAmbiguousArraySet(HArraySet* aset) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 82 | DataType::Type type = aset->GetValue()->GetType(); |
| 83 | if (DataType::IsIntOrLongType(type)) { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 84 | 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 Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 98 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 99 | private: |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 100 | void SetLoopHeaderPhiInputs(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 101 | void FixEnvironmentPhis(); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 102 | void FixNullConstantType(); |
| 103 | void EquivalentPhisCleanup(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 104 | void RunPrimitiveTypePropagation(); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 105 | |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 106 | // 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 Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 110 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 111 | bool TypeInputsOfPhi(HPhi* phi, ScopedArenaVector<HPhi*>* worklist); |
| 112 | bool UpdatePrimitiveType(HPhi* phi, ScopedArenaVector<HPhi*>* worklist); |
| 113 | void ProcessPrimitiveTypePropagationWorklist(ScopedArenaVector<HPhi*>* worklist); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 114 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 115 | HFloatConstant* GetFloatEquivalent(HIntConstant* constant); |
| 116 | HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 117 | HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, DataType::Type type); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 118 | HArrayGet* GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget); |
| 119 | |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 120 | void RemoveRedundantUninitializedStrings(); |
| 121 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 122 | HGraph* const graph_; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 123 | Handle<mirror::ClassLoader> class_loader_; |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 124 | Handle<mirror::DexCache> dex_cache_; |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 125 | VariableSizedHandleScope* const handles_; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 126 | |
| 127 | // True if types of ambiguous ArrayGets have been resolved. |
| 128 | bool agets_fixed_; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 129 | |
Vladimir Marko | 69d310e | 2017-10-09 14:12:23 +0100 | [diff] [blame] | 130 | ScopedArenaAllocator* const local_allocator_; |
| 131 | ScopedArenaVector<HArrayGet*> ambiguous_agets_; |
| 132 | ScopedArenaVector<HArraySet*> ambiguous_asets_; |
| 133 | ScopedArenaVector<HNewInstance*> uninitialized_strings_; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 134 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 135 | DISALLOW_COPY_AND_ASSIGN(SsaBuilder); |
| 136 | }; |
| 137 | |
| 138 | } // namespace art |
| 139 | |
| 140 | #endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_ |