| 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 | #include "ssa_builder.h" | 
 | 18 | #include "nodes.h" | 
 | 19 |  | 
 | 20 | namespace art { | 
 | 21 |  | 
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 22 | static Primitive::Type MergeTypes(Primitive::Type existing, Primitive::Type new_type) { | 
 | 23 |   // We trust the verifier has already done the necessary checking. | 
 | 24 |   switch (existing) { | 
 | 25 |     case Primitive::kPrimFloat: | 
 | 26 |     case Primitive::kPrimDouble: | 
 | 27 |     case Primitive::kPrimNot: | 
 | 28 |       return existing; | 
 | 29 |     default: | 
 | 30 |       return new_type; | 
 | 31 |   } | 
 | 32 | } | 
 | 33 |  | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 34 | void SsaBuilder::BuildSsa() { | 
| Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 35 |   // 1) Visit in reverse post order. We need to have all predecessors of a block visited | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 36 |   // (with the exception of loops) in order to create the right environment for that | 
 | 37 |   // block. For loops, we create phis whose inputs will be set in 2). | 
| Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 38 |   for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { | 
 | 39 |     VisitBasicBlock(it.Current()); | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 40 |   } | 
 | 41 |  | 
 | 42 |   // 2) Set inputs of loop phis. | 
 | 43 |   for (size_t i = 0; i < loop_headers_.Size(); i++) { | 
 | 44 |     HBasicBlock* block = loop_headers_.Get(i); | 
| Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 45 |     for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 46 |       HPhi* phi = it.Current()->AsPhi(); | 
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 47 |       Primitive::Type type = Primitive::kPrimVoid; | 
| Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 48 |       for (size_t pred = 0; pred < block->GetPredecessors().Size(); pred++) { | 
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 49 |         HInstruction* input = ValueOfLocal(block->GetPredecessors().Get(pred), phi->GetRegNumber()); | 
 | 50 |         phi->AddInput(input); | 
 | 51 |         type = MergeTypes(type, input->GetType()); | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 52 |       } | 
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 53 |       phi->SetType(type); | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 54 |     } | 
 | 55 |   } | 
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 56 |   // TODO: Now that the type of loop phis is set, we need a type propagation phase. | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 57 |  | 
 | 58 |   // 3) Clear locals. | 
 | 59 |   // TODO: Move this to a dead code eliminator phase. | 
| Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 60 |   for (HInstructionIterator it(GetGraph()->GetEntryBlock()->GetInstructions()); | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 61 |        !it.Done(); | 
 | 62 |        it.Advance()) { | 
 | 63 |     HInstruction* current = it.Current(); | 
 | 64 |     if (current->AsLocal() != nullptr) { | 
 | 65 |       current->GetBlock()->RemoveInstruction(current); | 
 | 66 |     } | 
 | 67 |   } | 
 | 68 | } | 
 | 69 |  | 
 | 70 | HInstruction* SsaBuilder::ValueOfLocal(HBasicBlock* block, size_t local) { | 
 | 71 |   return GetLocalsFor(block)->Get(local); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | void SsaBuilder::VisitBasicBlock(HBasicBlock* block) { | 
 | 75 |   current_locals_ = GetLocalsFor(block); | 
 | 76 |  | 
 | 77 |   if (block->IsLoopHeader()) { | 
 | 78 |     // If the block is a loop header, we know we only have visited the pre header | 
| Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 79 |     // because we are visiting in reverse post order. We create phis for all initialized | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 80 |     // locals from the pre header. Their inputs will be populated at the end of | 
 | 81 |     // the analysis. | 
 | 82 |     for (size_t local = 0; local < current_locals_->Size(); local++) { | 
 | 83 |       HInstruction* incoming = ValueOfLocal(block->GetLoopInformation()->GetPreHeader(), local); | 
 | 84 |       if (incoming != nullptr) { | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 85 |         HPhi* phi = new (GetGraph()->GetArena()) HPhi( | 
 | 86 |             GetGraph()->GetArena(), local, 0, Primitive::kPrimVoid); | 
 | 87 |         block->AddPhi(phi); | 
 | 88 |         current_locals_->Put(local, phi); | 
 | 89 |       } | 
 | 90 |     } | 
 | 91 |     // Save the loop header so that the last phase of the analysis knows which | 
 | 92 |     // blocks need to be updated. | 
 | 93 |     loop_headers_.Add(block); | 
| Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 94 |   } else if (block->GetPredecessors().Size() > 0) { | 
| Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 95 |     // All predecessors have already been visited because we are visiting in reverse post order. | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 96 |     // We merge the values of all locals, creating phis if those values differ. | 
 | 97 |     for (size_t local = 0; local < current_locals_->Size(); local++) { | 
| Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 98 |       bool one_predecessor_has_no_value = false; | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 99 |       bool is_different = false; | 
| Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 100 |       HInstruction* value = ValueOfLocal(block->GetPredecessors().Get(0), local); | 
| Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 101 |  | 
 | 102 |       for (size_t i = 0, e = block->GetPredecessors().Size(); i < e; ++i) { | 
 | 103 |         HInstruction* current = ValueOfLocal(block->GetPredecessors().Get(i), local); | 
 | 104 |         if (current == nullptr) { | 
 | 105 | //          one_predecessor_has_no_value = true; | 
 | 106 | //          break; | 
 | 107 |         } else if (current != value) { | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 108 |           is_different = true; | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 109 |         } | 
 | 110 |       } | 
| Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 111 |  | 
 | 112 |       if (one_predecessor_has_no_value) { | 
 | 113 |         // If one predecessor has no value for this local, we trust the verifier has | 
 | 114 |         // successfully checked that there is a store dominating any read after this block. | 
 | 115 |         continue; | 
 | 116 |       } | 
 | 117 |  | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 118 |       if (is_different) { | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 119 |         HPhi* phi = new (GetGraph()->GetArena()) HPhi( | 
| Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 120 |             GetGraph()->GetArena(), local, block->GetPredecessors().Size(), Primitive::kPrimVoid); | 
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 121 |         Primitive::Type type = Primitive::kPrimVoid; | 
| Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 122 |         for (size_t i = 0; i < block->GetPredecessors().Size(); i++) { | 
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 123 |           HInstruction* value = ValueOfLocal(block->GetPredecessors().Get(i), local); | 
 | 124 |           // We need to merge the incoming types, as the Dex format does not | 
 | 125 |           // guarantee the inputs have the same type. In particular the 0 constant is | 
 | 126 |           // used for all types, but the graph builder treats it as an int. | 
 | 127 |           type = MergeTypes(type, value->GetType()); | 
 | 128 |           phi->SetRawInputAt(i, value); | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 129 |         } | 
| Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 130 |         phi->SetType(type); | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 131 |         block->AddPhi(phi); | 
 | 132 |         value = phi; | 
 | 133 |       } | 
 | 134 |       current_locals_->Put(local, value); | 
 | 135 |     } | 
 | 136 |   } | 
 | 137 |  | 
 | 138 |   // Visit all instructions. The instructions of interest are: | 
 | 139 |   // - HLoadLocal: replace them with the current value of the local. | 
 | 140 |   // - HStoreLocal: update current value of the local and remove the instruction. | 
 | 141 |   // - Instructions that require an environment: populate their environment | 
 | 142 |   //   with the current values of the locals. | 
| Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 143 |   for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { | 
| Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 144 |     it.Current()->Accept(this); | 
 | 145 |   } | 
 | 146 | } | 
 | 147 |  | 
 | 148 | void SsaBuilder::VisitLoadLocal(HLoadLocal* load) { | 
 | 149 |   load->ReplaceWith(current_locals_->Get(load->GetLocal()->GetRegNumber())); | 
 | 150 |   load->GetBlock()->RemoveInstruction(load); | 
 | 151 | } | 
 | 152 |  | 
 | 153 | void SsaBuilder::VisitStoreLocal(HStoreLocal* store) { | 
 | 154 |   current_locals_->Put(store->GetLocal()->GetRegNumber(), store->InputAt(1)); | 
 | 155 |   store->GetBlock()->RemoveInstruction(store); | 
 | 156 | } | 
 | 157 |  | 
 | 158 | void SsaBuilder::VisitInstruction(HInstruction* instruction) { | 
 | 159 |   if (!instruction->NeedsEnvironment()) { | 
 | 160 |     return; | 
 | 161 |   } | 
 | 162 |   HEnvironment* environment = new (GetGraph()->GetArena()) HEnvironment( | 
 | 163 |       GetGraph()->GetArena(), current_locals_->Size()); | 
 | 164 |   environment->Populate(*current_locals_); | 
 | 165 |   instruction->SetEnvironment(environment); | 
 | 166 | } | 
 | 167 |  | 
 | 168 | }  // namespace art |