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 | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 20 | #include "base/arena_containers.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 21 | #include "nodes.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 22 | #include "optimization.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 26 | /** |
| 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 Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 35 | * 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 Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 38 | * |
| 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 Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame^] | 48 | class SsaBuilder : public ValueObject { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 49 | public: |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame^] | 50 | SsaBuilder(HGraph* graph, StackHandleScopeCollection* handles) |
| 51 | : graph_(graph), |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 52 | handles_(handles), |
| 53 | agets_fixed_(false), |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame^] | 54 | ambiguous_agets_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)), |
| 55 | ambiguous_asets_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)), |
| 56 | uninitialized_strings_(graph->GetArena()->Adapter(kArenaAllocGraphBuilder)) { |
| 57 | graph_->InitializeInexactObjectRTI(handles); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 60 | GraphAnalysisResult BuildSsa(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 61 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame^] | 62 | HInstruction* GetFloatOrDoubleEquivalent(HInstruction* instruction, Primitive::Type type); |
| 63 | HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 64 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame^] | 65 | void MaybeAddAmbiguousArrayGet(HArrayGet* aget) { |
| 66 | Primitive::Type type = aget->GetType(); |
| 67 | DCHECK(!Primitive::IsFloatingPointType(type)); |
| 68 | if (Primitive::IsIntOrLongType(type)) { |
| 69 | ambiguous_agets_.push_back(aget); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void MaybeAddAmbiguousArraySet(HArraySet* aset) { |
| 74 | Primitive::Type type = aset->GetValue()->GetType(); |
| 75 | if (Primitive::IsIntOrLongType(type)) { |
| 76 | ambiguous_asets_.push_back(aset); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | void AddUninitializedString(HNewInstance* string) { |
| 81 | // In some rare cases (b/27847265), the same NewInstance may be seen |
| 82 | // multiple times. We should only consider it once for removal, so we |
| 83 | // ensure it is not added more than once. |
| 84 | // Note that we cannot check whether this really is a NewInstance of String |
| 85 | // before RTP. We DCHECK that in RemoveRedundantUninitializedStrings. |
| 86 | if (!ContainsElement(uninitialized_strings_, string)) { |
| 87 | uninitialized_strings_.push_back(string); |
| 88 | } |
| 89 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 90 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 91 | private: |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 92 | void SetLoopHeaderPhiInputs(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 93 | void FixEnvironmentPhis(); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 94 | void FixNullConstantType(); |
| 95 | void EquivalentPhisCleanup(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 96 | void RunPrimitiveTypePropagation(); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 97 | |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 98 | // Attempts to resolve types of aget(-wide) instructions and type values passed |
| 99 | // to aput(-wide) instructions from reference type information on the array |
| 100 | // input. Returns false if the type of an array is unknown. |
| 101 | bool FixAmbiguousArrayOps(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 102 | |
| 103 | bool TypeInputsOfPhi(HPhi* phi, ArenaVector<HPhi*>* worklist); |
| 104 | bool UpdatePrimitiveType(HPhi* phi, ArenaVector<HPhi*>* worklist); |
| 105 | void ProcessPrimitiveTypePropagationWorklist(ArenaVector<HPhi*>* worklist); |
| 106 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 107 | HFloatConstant* GetFloatEquivalent(HIntConstant* constant); |
| 108 | HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant); |
| 109 | HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type); |
| 110 | HArrayGet* GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget); |
| 111 | |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 112 | void RemoveRedundantUninitializedStrings(); |
| 113 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame^] | 114 | HGraph* graph_; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 115 | StackHandleScopeCollection* const handles_; |
| 116 | |
| 117 | // True if types of ambiguous ArrayGets have been resolved. |
| 118 | bool agets_fixed_; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 119 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 120 | ArenaVector<HArrayGet*> ambiguous_agets_; |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 121 | ArenaVector<HArraySet*> ambiguous_asets_; |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 122 | ArenaVector<HNewInstance*> uninitialized_strings_; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 123 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 124 | DISALLOW_COPY_AND_ASSIGN(SsaBuilder); |
| 125 | }; |
| 126 | |
| 127 | } // namespace art |
| 128 | |
| 129 | #endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_ |