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 | |
| 20 | #include "nodes.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 21 | #include "optimization.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | static constexpr int kDefaultNumberOfLoops = 2; |
| 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 | */ |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 49 | class SsaBuilder : public HGraphVisitor { |
| 50 | public: |
| 51 | explicit SsaBuilder(HGraph* graph) |
| 52 | : HGraphVisitor(graph), |
| 53 | current_locals_(nullptr), |
| 54 | loop_headers_(graph->GetArena(), kDefaultNumberOfLoops), |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 55 | locals_for_(graph->GetArena(), graph->GetBlocks().size()) { |
| 56 | locals_for_.SetSize(graph->GetBlocks().size()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void BuildSsa(); |
| 60 | |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 61 | GrowableArray<HInstruction*>* GetLocalsFor(HBasicBlock* block) { |
| 62 | GrowableArray<HInstruction*>* locals = locals_for_.Get(block->GetBlockId()); |
| 63 | if (locals == nullptr) { |
David Brazdil | ffee3d3 | 2015-07-06 11:48:53 +0100 | [diff] [blame] | 64 | const size_t vregs = GetGraph()->GetNumberOfVRegs(); |
| 65 | ArenaAllocator* arena = GetGraph()->GetArena(); |
| 66 | locals = new (arena) GrowableArray<HInstruction*>(arena, vregs); |
| 67 | locals->SetSize(vregs); |
| 68 | |
| 69 | if (block->IsCatchBlock()) { |
| 70 | // We record incoming inputs of catch phis at throwing instructions and |
| 71 | // must therefore eagerly create the phis. Unused phis will be removed |
| 72 | // in the dead phi analysis. |
| 73 | for (size_t i = 0; i < vregs; ++i) { |
| 74 | HPhi* phi = new (arena) HPhi(arena, i, 0, Primitive::kPrimVoid); |
| 75 | block->AddPhi(phi); |
| 76 | locals->Put(i, phi); |
| 77 | } |
| 78 | } |
| 79 | |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 80 | locals_for_.Put(block->GetBlockId(), locals); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 81 | } |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 82 | return locals; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | HInstruction* ValueOfLocal(HBasicBlock* block, size_t local); |
| 86 | |
| 87 | void VisitBasicBlock(HBasicBlock* block); |
| 88 | void VisitLoadLocal(HLoadLocal* load); |
| 89 | void VisitStoreLocal(HStoreLocal* store); |
| 90 | void VisitInstruction(HInstruction* instruction); |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 91 | void VisitTemporary(HTemporary* instruction); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 92 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 93 | static HInstruction* GetFloatOrDoubleEquivalent(HInstruction* user, |
| 94 | HInstruction* instruction, |
| 95 | Primitive::Type type); |
| 96 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 97 | static HInstruction* GetReferenceTypeEquivalent(HInstruction* instruction); |
| 98 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 99 | static constexpr const char* kSsaBuilderPassName = "ssa_builder"; |
| 100 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 101 | private: |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 102 | void FixNullConstantType(); |
| 103 | void EquivalentPhisCleanup(); |
| 104 | |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 105 | static HFloatConstant* GetFloatEquivalent(HIntConstant* constant); |
| 106 | static HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant); |
| 107 | static HPhi* GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type); |
| 108 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 109 | // Locals for the current block being visited. |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 110 | GrowableArray<HInstruction*>* current_locals_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 111 | |
| 112 | // Keep track of loop headers found. The last phase of the analysis iterates |
| 113 | // over these blocks to set the inputs of their phis. |
| 114 | GrowableArray<HBasicBlock*> loop_headers_; |
| 115 | |
| 116 | // HEnvironment for each block. |
Nicolas Geoffray | 8c0c91a | 2015-05-07 11:46:05 +0100 | [diff] [blame] | 117 | GrowableArray<GrowableArray<HInstruction*>*> locals_for_; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 118 | |
| 119 | DISALLOW_COPY_AND_ASSIGN(SsaBuilder); |
| 120 | }; |
| 121 | |
| 122 | } // namespace art |
| 123 | |
| 124 | #endif // ART_COMPILER_OPTIMIZING_SSA_BUILDER_H_ |