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" |
Nicolas Geoffray | 184d640 | 2014-06-09 14:06:02 +0100 | [diff] [blame] | 18 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 19 | #include "nodes.h" |
Nicolas Geoffray | 184d640 | 2014-06-09 14:06:02 +0100 | [diff] [blame] | 20 | #include "ssa_type_propagation.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 21 | #include "ssa_phi_elimination.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
| 25 | void SsaBuilder::BuildSsa() { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 26 | // 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] | 27 | // (with the exception of loops) in order to create the right environment for that |
| 28 | // 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] | 29 | for (HReversePostOrderIterator it(*GetGraph()); !it.Done(); it.Advance()) { |
| 30 | VisitBasicBlock(it.Current()); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // 2) Set inputs of loop phis. |
| 34 | for (size_t i = 0; i < loop_headers_.Size(); i++) { |
| 35 | HBasicBlock* block = loop_headers_.Get(i); |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 36 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 37 | HPhi* phi = it.Current()->AsPhi(); |
Nicolas Geoffray | 622d9c3 | 2014-05-12 16:11:02 +0100 | [diff] [blame] | 38 | for (size_t pred = 0; pred < block->GetPredecessors().Size(); pred++) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 39 | HInstruction* input = ValueOfLocal(block->GetPredecessors().Get(pred), phi->GetRegNumber()); |
| 40 | phi->AddInput(input); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 45 | // 3) Remove dead phis. This will remove phis that are only used by environments: |
| 46 | // at the DEX level, the type of these phis does not need to be consistent, but |
| 47 | // our code generator will complain if the inputs of a phi do not have the same |
| 48 | // type (modulo the special case of `null`). |
| 49 | SsaDeadPhiElimination dead_phis(GetGraph()); |
| 50 | dead_phis.Run(); |
| 51 | |
| 52 | // 4) Propagate types of phis. At this point, phis are typed void in the general |
| 53 | // case, or float or double when we created a floating-point equivalent. So we |
| 54 | // need to propagate the types across phis to give them a correct type. |
Nicolas Geoffray | 184d640 | 2014-06-09 14:06:02 +0100 | [diff] [blame] | 55 | SsaTypePropagation type_propagation(GetGraph()); |
| 56 | type_propagation.Run(); |
| 57 | |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 58 | // 5) Clear locals. |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 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(); |
Roland Levillain | 476df55 | 2014-10-09 17:51:36 +0100 | [diff] [blame] | 64 | if (current->IsLocal()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 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) { |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 105 | one_predecessor_has_no_value = true; |
| 106 | break; |
Nicolas Geoffray | 7c3560f | 2014-06-04 12:12:08 +0100 | [diff] [blame] | 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); |
| 121 | for (size_t i = 0; i < block->GetPredecessors().Size(); i++) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 122 | HInstruction* pred_value = ValueOfLocal(block->GetPredecessors().Get(i), local); |
| 123 | phi->SetRawInputAt(i, pred_value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 124 | } |
| 125 | block->AddPhi(phi); |
| 126 | value = phi; |
| 127 | } |
| 128 | current_locals_->Put(local, value); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Visit all instructions. The instructions of interest are: |
| 133 | // - HLoadLocal: replace them with the current value of the local. |
| 134 | // - HStoreLocal: update current value of the local and remove the instruction. |
| 135 | // - Instructions that require an environment: populate their environment |
| 136 | // with the current values of the locals. |
Nicolas Geoffray | f635e63 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 137 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 138 | it.Current()->Accept(this); |
| 139 | } |
| 140 | } |
| 141 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 142 | /** |
| 143 | * Constants in the Dex format are not typed. So the builder types them as |
| 144 | * integers, but when doing the SSA form, we might realize the constant |
| 145 | * is used for floating point operations. We create a floating-point equivalent |
| 146 | * constant to make the operations correctly typed. |
| 147 | */ |
| 148 | static HFloatConstant* GetFloatEquivalent(HIntConstant* constant) { |
| 149 | // We place the floating point constant next to this constant. |
| 150 | HFloatConstant* result = constant->GetNext()->AsFloatConstant(); |
| 151 | if (result == nullptr) { |
| 152 | HGraph* graph = constant->GetBlock()->GetGraph(); |
| 153 | ArenaAllocator* allocator = graph->GetArena(); |
| 154 | result = new (allocator) HFloatConstant(bit_cast<int32_t, float>(constant->GetValue())); |
| 155 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
| 156 | } else { |
| 157 | // If there is already a constant with the expected type, we know it is |
| 158 | // the floating point equivalent of this constant. |
| 159 | DCHECK_EQ((bit_cast<float, int32_t>(result->GetValue())), constant->GetValue()); |
| 160 | } |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Wide constants in the Dex format are not typed. So the builder types them as |
| 166 | * longs, but when doing the SSA form, we might realize the constant |
| 167 | * is used for floating point operations. We create a floating-point equivalent |
| 168 | * constant to make the operations correctly typed. |
| 169 | */ |
| 170 | static HDoubleConstant* GetDoubleEquivalent(HLongConstant* constant) { |
| 171 | // We place the floating point constant next to this constant. |
| 172 | HDoubleConstant* result = constant->GetNext()->AsDoubleConstant(); |
| 173 | if (result == nullptr) { |
| 174 | HGraph* graph = constant->GetBlock()->GetGraph(); |
| 175 | ArenaAllocator* allocator = graph->GetArena(); |
| 176 | result = new (allocator) HDoubleConstant(bit_cast<int64_t, double>(constant->GetValue())); |
| 177 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
| 178 | } else { |
| 179 | // If there is already a constant with the expected type, we know it is |
| 180 | // the floating point equivalent of this constant. |
| 181 | DCHECK_EQ((bit_cast<double, int64_t>(result->GetValue())), constant->GetValue()); |
| 182 | } |
| 183 | return result; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Because of Dex format, we might end up having the same phi being |
| 188 | * used for non floating point operations and floating point operations. Because |
| 189 | * we want the graph to be correctly typed (and thereafter avoid moves between |
| 190 | * floating point registers and core registers), we need to create a copy of the |
| 191 | * phi with a floating point type. |
| 192 | */ |
| 193 | static HPhi* GetFloatOrDoubleEquivalentOfPhi(HPhi* phi, Primitive::Type type) { |
| 194 | // We place the floating point phi next to this phi. |
| 195 | HInstruction* next = phi->GetNext(); |
Nicolas Geoffray | 21cc798 | 2014-11-17 17:50:33 +0000 | [diff] [blame] | 196 | if (next == nullptr || (next->AsPhi()->GetRegNumber() != phi->GetRegNumber())) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 197 | ArenaAllocator* allocator = phi->GetBlock()->GetGraph()->GetArena(); |
| 198 | HPhi* new_phi = new (allocator) HPhi(allocator, phi->GetRegNumber(), phi->InputCount(), type); |
| 199 | for (size_t i = 0, e = phi->InputCount(); i < e; ++i) { |
| 200 | // Copy the inputs. Note that the graph may not be correctly typed by doing this copy, |
| 201 | // but the type propagation phase will fix it. |
| 202 | new_phi->SetRawInputAt(i, phi->InputAt(i)); |
| 203 | } |
| 204 | phi->GetBlock()->InsertPhiAfter(new_phi, phi); |
| 205 | return new_phi; |
| 206 | } else { |
Nicolas Geoffray | 21cc798 | 2014-11-17 17:50:33 +0000 | [diff] [blame] | 207 | DCHECK_EQ(next->GetType(), type); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 208 | return next->AsPhi(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | HInstruction* SsaBuilder::GetFloatOrDoubleEquivalent(HInstruction* user, |
| 213 | HInstruction* value, |
| 214 | Primitive::Type type) { |
| 215 | if (value->IsArrayGet()) { |
| 216 | // The verifier has checked that values in arrays cannot be used for both |
| 217 | // floating point and non-floating point operations. It is therefore safe to just |
| 218 | // change the type of the operation. |
| 219 | value->AsArrayGet()->SetType(type); |
| 220 | return value; |
| 221 | } else if (value->IsLongConstant()) { |
| 222 | return GetDoubleEquivalent(value->AsLongConstant()); |
| 223 | } else if (value->IsIntConstant()) { |
| 224 | return GetFloatEquivalent(value->AsIntConstant()); |
| 225 | } else if (value->IsPhi()) { |
| 226 | return GetFloatOrDoubleEquivalentOfPhi(value->AsPhi(), type); |
| 227 | } else { |
| 228 | // For other instructions, we assume the verifier has checked that the dex format is correctly |
| 229 | // typed and the value in a dex register will not be used for both floating point and |
| 230 | // non-floating point operations. So the only reason an instruction would want a floating |
| 231 | // point equivalent is for an unused phi that will be removed by the dead phi elimination phase. |
| 232 | DCHECK(user->IsPhi()); |
| 233 | return value; |
| 234 | } |
| 235 | } |
| 236 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 237 | void SsaBuilder::VisitLoadLocal(HLoadLocal* load) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 238 | HInstruction* value = current_locals_->Get(load->GetLocal()->GetRegNumber()); |
| 239 | if (load->GetType() != value->GetType() |
| 240 | && (load->GetType() == Primitive::kPrimFloat || load->GetType() == Primitive::kPrimDouble)) { |
| 241 | // If the operation requests a specific type, we make sure its input is of that type. |
| 242 | value = GetFloatOrDoubleEquivalent(load, value, load->GetType()); |
| 243 | } |
| 244 | load->ReplaceWith(value); |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 245 | load->GetBlock()->RemoveInstruction(load); |
| 246 | } |
| 247 | |
| 248 | void SsaBuilder::VisitStoreLocal(HStoreLocal* store) { |
| 249 | current_locals_->Put(store->GetLocal()->GetRegNumber(), store->InputAt(1)); |
| 250 | store->GetBlock()->RemoveInstruction(store); |
| 251 | } |
| 252 | |
| 253 | void SsaBuilder::VisitInstruction(HInstruction* instruction) { |
| 254 | if (!instruction->NeedsEnvironment()) { |
| 255 | return; |
| 256 | } |
| 257 | HEnvironment* environment = new (GetGraph()->GetArena()) HEnvironment( |
| 258 | GetGraph()->GetArena(), current_locals_->Size()); |
| 259 | environment->Populate(*current_locals_); |
| 260 | instruction->SetEnvironment(environment); |
| 261 | } |
| 262 | |
Nicolas Geoffray | 421e9f9 | 2014-11-11 18:21:53 +0000 | [diff] [blame] | 263 | void SsaBuilder::VisitTemporary(HTemporary* temp) { |
| 264 | // Temporaries are only used by the baseline register allocator. |
| 265 | temp->GetBlock()->RemoveInstruction(temp); |
| 266 | } |
| 267 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 268 | } // namespace art |