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 | |
| 26 | static constexpr int kDefaultNumberOfLoops = 2; |
| 27 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 28 | /** |
| 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 Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 37 | * 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 Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 40 | * |
| 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 | */ |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 50 | class SsaBuilder : public HGraphVisitor { |
| 51 | public: |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame^] | 52 | SsaBuilder(HGraph* graph, StackHandleScopeCollection* handles) |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 53 | : HGraphVisitor(graph), |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 54 | handles_(handles), |
| 55 | agets_fixed_(false), |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 56 | current_locals_(nullptr), |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 57 | loop_headers_(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)), |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 58 | ambiguous_agets_(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)), |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 59 | ambiguous_asets_(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)), |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 60 | locals_for_(graph->GetBlocks().size(), |
| 61 | ArenaVector<HInstruction*>(graph->GetArena()->Adapter(kArenaAllocSsaBuilder)), |
| 62 | graph->GetArena()->Adapter(kArenaAllocSsaBuilder)) { |
| 63 | loop_headers_.reserve(kDefaultNumberOfLoops); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame^] | 66 | GraphAnalysisResult BuildSsa(); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 67 | |
David Brazdil | eead071 | 2015-09-18 14:58:57 +0100 | [diff] [blame] | 68 | // Returns locals vector for `block`. If it is a catch block, the vector will be |
| 69 | // prepopulated with catch phis for vregs which are defined in `current_locals_`. |
| 70 | ArenaVector<HInstruction*>* GetLocalsFor(HBasicBlock* block); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 71 | HInstruction* ValueOfLocal(HBasicBlock* block, size_t local); |
| 72 | |
| 73 | void VisitBasicBlock(HBasicBlock* block); |
| 74 | void VisitLoadLocal(HLoadLocal* load); |
| 75 | void VisitStoreLocal(HStoreLocal* store); |
| 76 | void VisitInstruction(HInstruction* instruction); |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 77 | void VisitTemporary(HTemporary* instruction); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 78 | void VisitArrayGet(HArrayGet* aget); |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 79 | void VisitArraySet(HArraySet* aset); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 80 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 81 | static constexpr const char* kSsaBuilderPassName = "ssa_builder"; |
| 82 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 83 | private: |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 84 | void SetLoopHeaderPhiInputs(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 85 | void FixEnvironmentPhis(); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 86 | void FixNullConstantType(); |
| 87 | void EquivalentPhisCleanup(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 88 | void RunPrimitiveTypePropagation(); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 89 | |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 90 | // Attempts to resolve types of aget(-wide) instructions and type values passed |
| 91 | // to aput(-wide) instructions from reference type information on the array |
| 92 | // input. Returns false if the type of an array is unknown. |
| 93 | bool FixAmbiguousArrayOps(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 94 | |
| 95 | bool TypeInputsOfPhi(HPhi* phi, ArenaVector<HPhi*>* worklist); |
| 96 | bool UpdatePrimitiveType(HPhi* phi, ArenaVector<HPhi*>* worklist); |
| 97 | void ProcessPrimitiveTypePropagationWorklist(ArenaVector<HPhi*>* worklist); |
| 98 | |
| 99 | HInstruction* GetFloatOrDoubleEquivalent(HInstruction* instruction, Primitive::Type type); |
| 100 | HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction); |
| 101 | |
| 102 | HFloatConstant* GetFloatEquivalent(HIntConstant* constant); |
| 103 | HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant); |
| 104 | HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type); |
| 105 | HArrayGet* GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget); |
| 106 | |
| 107 | StackHandleScopeCollection* const handles_; |
| 108 | |
| 109 | // True if types of ambiguous ArrayGets have been resolved. |
| 110 | bool agets_fixed_; |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 111 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 112 | // Locals for the current block being visited. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 113 | ArenaVector<HInstruction*>* current_locals_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 114 | |
| 115 | // Keep track of loop headers found. The last phase of the analysis iterates |
| 116 | // over these blocks to set the inputs of their phis. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 117 | ArenaVector<HBasicBlock*> loop_headers_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 118 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 119 | ArenaVector<HArrayGet*> ambiguous_agets_; |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 120 | ArenaVector<HArraySet*> ambiguous_asets_; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 121 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 122 | // HEnvironment for each block. |
Vladimir Marko | 71bf809 | 2015-09-15 15:33:14 +0100 | [diff] [blame] | 123 | ArenaVector<ArenaVector<HInstruction*>> locals_for_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 124 | |
| 125 | DISALLOW_COPY_AND_ASSIGN(SsaBuilder); |
| 126 | }; |
| 127 | |
| 128 | } // namespace art |
| 129 | |
| 130 | #endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_ |