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 | |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 19 | #include "bytecode_utils.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 20 | #include "mirror/class-inl.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 21 | #include "nodes.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 22 | #include "reference_type_propagation.h" |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 23 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | 3159674 | 2014-11-24 15:28:45 +0000 | [diff] [blame] | 24 | #include "ssa_phi_elimination.h" |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 28 | void SsaBuilder::FixNullConstantType() { |
| 29 | // The order doesn't matter here. |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 30 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
| 31 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 32 | HInstruction* equality_instr = it.Current(); |
| 33 | if (!equality_instr->IsEqual() && !equality_instr->IsNotEqual()) { |
| 34 | continue; |
| 35 | } |
| 36 | HInstruction* left = equality_instr->InputAt(0); |
| 37 | HInstruction* right = equality_instr->InputAt(1); |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 38 | HInstruction* int_operand = nullptr; |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 39 | |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 40 | if ((left->GetType() == Primitive::kPrimNot) && (right->GetType() == Primitive::kPrimInt)) { |
| 41 | int_operand = right; |
| 42 | } else if ((right->GetType() == Primitive::kPrimNot) |
| 43 | && (left->GetType() == Primitive::kPrimInt)) { |
| 44 | int_operand = left; |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 45 | } else { |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | // If we got here, we are comparing against a reference and the int constant |
| 50 | // should be replaced with a null constant. |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 51 | // Both type propagation and redundant phi elimination ensure `int_operand` |
| 52 | // can only be the 0 constant. |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 53 | DCHECK(int_operand->IsIntConstant()) << int_operand->DebugName(); |
Nicolas Geoffray | 51d400d | 2015-06-15 09:01:08 +0100 | [diff] [blame] | 54 | DCHECK_EQ(0, int_operand->AsIntConstant()->GetValue()); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 55 | equality_instr->ReplaceInput(graph_->GetNullConstant(), int_operand == right ? 1 : 0); |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void SsaBuilder::EquivalentPhisCleanup() { |
| 61 | // The order doesn't matter here. |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 62 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
| 63 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 64 | HPhi* phi = it.Current()->AsPhi(); |
| 65 | HPhi* next = phi->GetNextEquivalentPhiWithSameType(); |
| 66 | if (next != nullptr) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 67 | // Make sure we do not replace a live phi with a dead phi. A live phi |
| 68 | // has been handled by the type propagation phase, unlike a dead phi. |
Nicolas Geoffray | 4230e18 | 2015-06-29 14:34:46 +0100 | [diff] [blame] | 69 | if (next->IsLive()) { |
| 70 | phi->ReplaceWith(next); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 71 | phi->SetDead(); |
Nicolas Geoffray | 4230e18 | 2015-06-29 14:34:46 +0100 | [diff] [blame] | 72 | } else { |
| 73 | next->ReplaceWith(phi); |
| 74 | } |
Calin Juravle | a4f8831 | 2015-04-16 12:57:19 +0100 | [diff] [blame] | 75 | DCHECK(next->GetNextEquivalentPhiWithSameType() == nullptr) |
| 76 | << "More then one phi equivalent with type " << phi->GetType() |
| 77 | << " found for phi" << phi->GetId(); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 83 | void SsaBuilder::FixEnvironmentPhis() { |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 84 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 85 | for (HInstructionIterator it_phis(block->GetPhis()); !it_phis.Done(); it_phis.Advance()) { |
| 86 | HPhi* phi = it_phis.Current()->AsPhi(); |
| 87 | // If the phi is not dead, or has no environment uses, there is nothing to do. |
| 88 | if (!phi->IsDead() || !phi->HasEnvironmentUses()) continue; |
| 89 | HInstruction* next = phi->GetNext(); |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 90 | if (!phi->IsVRegEquivalentOf(next)) continue; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 91 | if (next->AsPhi()->IsDead()) { |
| 92 | // If the phi equivalent is dead, check if there is another one. |
| 93 | next = next->GetNext(); |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 94 | if (!phi->IsVRegEquivalentOf(next)) continue; |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 95 | // There can be at most two phi equivalents. |
David Brazdil | d0180f9 | 2015-09-22 14:39:58 +0100 | [diff] [blame] | 96 | DCHECK(!phi->IsVRegEquivalentOf(next->GetNext())); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 97 | if (next->AsPhi()->IsDead()) continue; |
| 98 | } |
| 99 | // We found a live phi equivalent. Update the environment uses of `phi` with it. |
| 100 | phi->ReplaceWith(next); |
| 101 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 102 | } |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 103 | } |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 104 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 105 | static void AddDependentInstructionsToWorklist(HInstruction* instruction, |
| 106 | ArenaVector<HPhi*>* worklist) { |
| 107 | // If `instruction` is a dead phi, type conflict was just identified. All its |
| 108 | // live phi users, and transitively users of those users, therefore need to be |
| 109 | // marked dead/conflicting too, so we add them to the worklist. Otherwise we |
| 110 | // add users whose type does not match and needs to be updated. |
| 111 | bool add_all_live_phis = instruction->IsPhi() && instruction->AsPhi()->IsDead(); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 112 | for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) { |
| 113 | HInstruction* user = use.GetUser(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 114 | if (user->IsPhi() && user->AsPhi()->IsLive()) { |
| 115 | if (add_all_live_phis || user->GetType() != instruction->GetType()) { |
| 116 | worklist->push_back(user->AsPhi()); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Find a candidate primitive type for `phi` by merging the type of its inputs. |
| 123 | // Return false if conflict is identified. |
| 124 | static bool TypePhiFromInputs(HPhi* phi) { |
| 125 | Primitive::Type common_type = phi->GetType(); |
| 126 | |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 127 | for (HInstruction* input : phi->GetInputs()) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 128 | if (input->IsPhi() && input->AsPhi()->IsDead()) { |
| 129 | // Phis are constructed live so if an input is a dead phi, it must have |
| 130 | // been made dead due to type conflict. Mark this phi conflicting too. |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | Primitive::Type input_type = HPhi::ToPhiType(input->GetType()); |
| 135 | if (common_type == input_type) { |
| 136 | // No change in type. |
David Brazdil | d87f3ea | 2016-01-04 15:55:10 +0000 | [diff] [blame] | 137 | } else if (Primitive::Is64BitType(common_type) != Primitive::Is64BitType(input_type)) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 138 | // Types are of different sizes, e.g. int vs. long. Must be a conflict. |
| 139 | return false; |
| 140 | } else if (Primitive::IsIntegralType(common_type)) { |
| 141 | // Previous inputs were integral, this one is not but is of the same size. |
| 142 | // This does not imply conflict since some bytecode instruction types are |
| 143 | // ambiguous. TypeInputsOfPhi will either type them or detect a conflict. |
| 144 | DCHECK(Primitive::IsFloatingPointType(input_type) || input_type == Primitive::kPrimNot); |
| 145 | common_type = input_type; |
| 146 | } else if (Primitive::IsIntegralType(input_type)) { |
| 147 | // Input is integral, common type is not. Same as in the previous case, if |
| 148 | // there is a conflict, it will be detected during TypeInputsOfPhi. |
| 149 | DCHECK(Primitive::IsFloatingPointType(common_type) || common_type == Primitive::kPrimNot); |
| 150 | } else { |
| 151 | // Combining float and reference types. Clearly a conflict. |
| 152 | DCHECK((common_type == Primitive::kPrimFloat && input_type == Primitive::kPrimNot) || |
| 153 | (common_type == Primitive::kPrimNot && input_type == Primitive::kPrimFloat)); |
| 154 | return false; |
| 155 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 156 | } |
| 157 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 158 | // We have found a candidate type for the phi. Set it and return true. We may |
| 159 | // still discover conflict whilst typing the individual inputs in TypeInputsOfPhi. |
| 160 | phi->SetType(common_type); |
| 161 | return true; |
| 162 | } |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 163 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 164 | // Replace inputs of `phi` to match its type. Return false if conflict is identified. |
| 165 | bool SsaBuilder::TypeInputsOfPhi(HPhi* phi, ArenaVector<HPhi*>* worklist) { |
| 166 | Primitive::Type common_type = phi->GetType(); |
Nicolas Geoffray | 50a9ed0 | 2016-09-23 15:40:41 +0100 | [diff] [blame] | 167 | if (Primitive::IsIntegralType(common_type)) { |
| 168 | // We do not need to retype ambiguous inputs because they are always constructed |
| 169 | // with the integral type candidate. |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 170 | if (kIsDebugBuild) { |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 171 | for (HInstruction* input : phi->GetInputs()) { |
Nicolas Geoffray | 50a9ed0 | 2016-09-23 15:40:41 +0100 | [diff] [blame] | 172 | DCHECK(HPhi::ToPhiType(input->GetType()) == common_type); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | // Inputs did not need to be replaced, hence no conflict. Report success. |
| 176 | return true; |
| 177 | } else { |
| 178 | DCHECK(common_type == Primitive::kPrimNot || Primitive::IsFloatingPointType(common_type)); |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 179 | HInputsRef inputs = phi->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 180 | for (size_t i = 0; i < inputs.size(); ++i) { |
| 181 | HInstruction* input = inputs[i]; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 182 | if (input->GetType() != common_type) { |
| 183 | // Input type does not match phi's type. Try to retype the input or |
| 184 | // generate a suitably typed equivalent. |
| 185 | HInstruction* equivalent = (common_type == Primitive::kPrimNot) |
| 186 | ? GetReferenceTypeEquivalent(input) |
| 187 | : GetFloatOrDoubleEquivalent(input, common_type); |
| 188 | if (equivalent == nullptr) { |
| 189 | // Input could not be typed. Report conflict. |
| 190 | return false; |
| 191 | } |
| 192 | // Make sure the input did not change its type and we do not need to |
| 193 | // update its users. |
| 194 | DCHECK_NE(input, equivalent); |
| 195 | |
| 196 | phi->ReplaceInput(equivalent, i); |
| 197 | if (equivalent->IsPhi()) { |
| 198 | worklist->push_back(equivalent->AsPhi()); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | // All inputs either matched the type of the phi or we successfully replaced |
| 203 | // them with a suitable equivalent. Report success. |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // Attempt to set the primitive type of `phi` to match its inputs. Return whether |
| 209 | // it was changed by the algorithm or not. |
| 210 | bool SsaBuilder::UpdatePrimitiveType(HPhi* phi, ArenaVector<HPhi*>* worklist) { |
| 211 | DCHECK(phi->IsLive()); |
| 212 | Primitive::Type original_type = phi->GetType(); |
| 213 | |
| 214 | // Try to type the phi in two stages: |
| 215 | // (1) find a candidate type for the phi by merging types of all its inputs, |
| 216 | // (2) try to type the phi's inputs to that candidate type. |
| 217 | // Either of these stages may detect a type conflict and fail, in which case |
| 218 | // we immediately abort. |
| 219 | if (!TypePhiFromInputs(phi) || !TypeInputsOfPhi(phi, worklist)) { |
| 220 | // Conflict detected. Mark the phi dead and return true because it changed. |
| 221 | phi->SetDead(); |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | // Return true if the type of the phi has changed. |
| 226 | return phi->GetType() != original_type; |
| 227 | } |
| 228 | |
| 229 | void SsaBuilder::RunPrimitiveTypePropagation() { |
Vladimir Marko | 3ea5a97 | 2016-05-09 20:23:34 +0100 | [diff] [blame] | 230 | ArenaVector<HPhi*> worklist(graph_->GetArena()->Adapter(kArenaAllocGraphBuilder)); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 231 | |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 232 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 233 | if (block->IsLoopHeader()) { |
| 234 | for (HInstructionIterator phi_it(block->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 235 | HPhi* phi = phi_it.Current()->AsPhi(); |
| 236 | if (phi->IsLive()) { |
| 237 | worklist.push_back(phi); |
| 238 | } |
| 239 | } |
| 240 | } else { |
| 241 | for (HInstructionIterator phi_it(block->GetPhis()); !phi_it.Done(); phi_it.Advance()) { |
| 242 | // Eagerly compute the type of the phi, for quicker convergence. Note |
| 243 | // that we don't need to add users to the worklist because we are |
| 244 | // doing a reverse post-order visit, therefore either the phi users are |
| 245 | // non-loop phi and will be visited later in the visit, or are loop-phis, |
| 246 | // and they are already in the work list. |
| 247 | HPhi* phi = phi_it.Current()->AsPhi(); |
| 248 | if (phi->IsLive()) { |
| 249 | UpdatePrimitiveType(phi, &worklist); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | ProcessPrimitiveTypePropagationWorklist(&worklist); |
| 256 | EquivalentPhisCleanup(); |
| 257 | } |
| 258 | |
| 259 | void SsaBuilder::ProcessPrimitiveTypePropagationWorklist(ArenaVector<HPhi*>* worklist) { |
| 260 | // Process worklist |
| 261 | while (!worklist->empty()) { |
| 262 | HPhi* phi = worklist->back(); |
| 263 | worklist->pop_back(); |
| 264 | // The phi could have been made dead as a result of conflicts while in the |
| 265 | // worklist. If it is now dead, there is no point in updating its type. |
| 266 | if (phi->IsLive() && UpdatePrimitiveType(phi, worklist)) { |
| 267 | AddDependentInstructionsToWorklist(phi, worklist); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | static HArrayGet* FindFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) { |
| 273 | Primitive::Type type = aget->GetType(); |
| 274 | DCHECK(Primitive::IsIntOrLongType(type)); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 275 | HInstruction* next = aget->GetNext(); |
| 276 | if (next != nullptr && next->IsArrayGet()) { |
| 277 | HArrayGet* next_aget = next->AsArrayGet(); |
| 278 | if (next_aget->IsEquivalentOf(aget)) { |
| 279 | return next_aget; |
| 280 | } |
| 281 | } |
| 282 | return nullptr; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static HArrayGet* CreateFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) { |
| 286 | Primitive::Type type = aget->GetType(); |
| 287 | DCHECK(Primitive::IsIntOrLongType(type)); |
| 288 | DCHECK(FindFloatOrDoubleEquivalentOfArrayGet(aget) == nullptr); |
| 289 | |
| 290 | HArrayGet* equivalent = new (aget->GetBlock()->GetGraph()->GetArena()) HArrayGet( |
| 291 | aget->GetArray(), |
| 292 | aget->GetIndex(), |
| 293 | type == Primitive::kPrimInt ? Primitive::kPrimFloat : Primitive::kPrimDouble, |
| 294 | aget->GetDexPc()); |
| 295 | aget->GetBlock()->InsertInstructionAfter(equivalent, aget); |
| 296 | return equivalent; |
| 297 | } |
| 298 | |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 299 | static Primitive::Type GetPrimitiveArrayComponentType(HInstruction* array) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 300 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 301 | ReferenceTypeInfo array_type = array->GetReferenceTypeInfo(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 302 | DCHECK(array_type.IsPrimitiveArrayClass()); |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 303 | return array_type.GetTypeHandle()->GetComponentType()->GetPrimitiveType(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 304 | } |
| 305 | |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 306 | bool SsaBuilder::FixAmbiguousArrayOps() { |
| 307 | if (ambiguous_agets_.empty() && ambiguous_asets_.empty()) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
| 311 | // The wrong ArrayGet equivalent may still have Phi uses coming from ArraySet |
| 312 | // uses (because they are untyped) and environment uses (if --debuggable). |
| 313 | // After resolving all ambiguous ArrayGets, we will re-run primitive type |
| 314 | // propagation on the Phis which need to be updated. |
Vladimir Marko | 3ea5a97 | 2016-05-09 20:23:34 +0100 | [diff] [blame] | 315 | ArenaVector<HPhi*> worklist(graph_->GetArena()->Adapter(kArenaAllocGraphBuilder)); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 316 | |
| 317 | { |
| 318 | ScopedObjectAccess soa(Thread::Current()); |
| 319 | |
| 320 | for (HArrayGet* aget_int : ambiguous_agets_) { |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 321 | HInstruction* array = aget_int->GetArray(); |
| 322 | if (!array->GetReferenceTypeInfo().IsPrimitiveArrayClass()) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 323 | // RTP did not type the input array. Bail. |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | HArrayGet* aget_float = FindFloatOrDoubleEquivalentOfArrayGet(aget_int); |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 328 | Primitive::Type array_type = GetPrimitiveArrayComponentType(array); |
| 329 | DCHECK_EQ(Primitive::Is64BitType(aget_int->GetType()), Primitive::Is64BitType(array_type)); |
| 330 | |
| 331 | if (Primitive::IsIntOrLongType(array_type)) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 332 | if (aget_float != nullptr) { |
| 333 | // There is a float/double equivalent. We must replace it and re-run |
| 334 | // primitive type propagation on all dependent instructions. |
| 335 | aget_float->ReplaceWith(aget_int); |
| 336 | aget_float->GetBlock()->RemoveInstruction(aget_float); |
| 337 | AddDependentInstructionsToWorklist(aget_int, &worklist); |
| 338 | } |
| 339 | } else { |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 340 | DCHECK(Primitive::IsFloatingPointType(array_type)); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 341 | if (aget_float == nullptr) { |
| 342 | // This is a float/double ArrayGet but there were no typed uses which |
| 343 | // would create the typed equivalent. Create it now. |
| 344 | aget_float = CreateFloatOrDoubleEquivalentOfArrayGet(aget_int); |
| 345 | } |
| 346 | // Replace the original int/long instruction. Note that it may have phi |
| 347 | // uses, environment uses, as well as real uses (from untyped ArraySets). |
| 348 | // We need to re-run primitive type propagation on its dependent instructions. |
| 349 | aget_int->ReplaceWith(aget_float); |
| 350 | aget_int->GetBlock()->RemoveInstruction(aget_int); |
| 351 | AddDependentInstructionsToWorklist(aget_float, &worklist); |
| 352 | } |
| 353 | } |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 354 | |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 355 | // Set a flag stating that types of ArrayGets have been resolved. Requesting |
| 356 | // equivalent of the wrong type with GetFloatOrDoubleEquivalentOfArrayGet |
| 357 | // will fail from now on. |
| 358 | agets_fixed_ = true; |
| 359 | |
| 360 | for (HArraySet* aset : ambiguous_asets_) { |
| 361 | HInstruction* array = aset->GetArray(); |
| 362 | if (!array->GetReferenceTypeInfo().IsPrimitiveArrayClass()) { |
| 363 | // RTP did not type the input array. Bail. |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | HInstruction* value = aset->GetValue(); |
| 368 | Primitive::Type value_type = value->GetType(); |
| 369 | Primitive::Type array_type = GetPrimitiveArrayComponentType(array); |
| 370 | DCHECK_EQ(Primitive::Is64BitType(value_type), Primitive::Is64BitType(array_type)); |
| 371 | |
| 372 | if (Primitive::IsFloatingPointType(array_type)) { |
| 373 | if (!Primitive::IsFloatingPointType(value_type)) { |
| 374 | DCHECK(Primitive::IsIntegralType(value_type)); |
| 375 | // Array elements are floating-point but the value has not been replaced |
| 376 | // with its floating-point equivalent. The replacement must always |
| 377 | // succeed in code validated by the verifier. |
| 378 | HInstruction* equivalent = GetFloatOrDoubleEquivalent(value, array_type); |
| 379 | DCHECK(equivalent != nullptr); |
| 380 | aset->ReplaceInput(equivalent, /* input_index */ 2); |
| 381 | if (equivalent->IsPhi()) { |
| 382 | // Returned equivalent is a phi which may not have had its inputs |
| 383 | // replaced yet. We need to run primitive type propagation on it. |
| 384 | worklist.push_back(equivalent->AsPhi()); |
| 385 | } |
| 386 | } |
Aart Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 387 | // Refine the side effects of this floating point aset. Note that we do this even if |
| 388 | // no replacement occurs, since the right-hand-side may have been corrected already. |
| 389 | aset->ComputeSideEffects(); |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 390 | } else { |
| 391 | // Array elements are integral and the value assigned to it initially |
| 392 | // was integral too. Nothing to do. |
| 393 | DCHECK(Primitive::IsIntegralType(array_type)); |
| 394 | DCHECK(Primitive::IsIntegralType(value_type)); |
| 395 | } |
| 396 | } |
| 397 | } |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 398 | |
| 399 | if (!worklist.empty()) { |
| 400 | ProcessPrimitiveTypePropagationWorklist(&worklist); |
| 401 | EquivalentPhisCleanup(); |
| 402 | } |
| 403 | |
| 404 | return true; |
| 405 | } |
| 406 | |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 407 | static bool HasAliasInEnvironments(HInstruction* instruction) { |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 408 | HEnvironment* last_user = nullptr; |
| 409 | for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) { |
| 410 | DCHECK(use.GetUser() != nullptr); |
| 411 | // Note: The first comparison (== null) always fails. |
| 412 | if (use.GetUser() == last_user) { |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 413 | return true; |
| 414 | } |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 415 | last_user = use.GetUser(); |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | if (kIsDebugBuild) { |
| 419 | // Do a quadratic search to ensure same environment uses are next |
| 420 | // to each other. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 421 | const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses(); |
| 422 | for (auto current = env_uses.begin(), end = env_uses.end(); current != end; ++current) { |
| 423 | auto next = current; |
| 424 | for (++next; next != end; ++next) { |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 425 | DCHECK(next->GetUser() != current->GetUser()); |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | } |
| 429 | return false; |
| 430 | } |
| 431 | |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 432 | void SsaBuilder::RemoveRedundantUninitializedStrings() { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 433 | if (graph_->IsDebuggable()) { |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 434 | // Do not perform the optimization for consistency with the interpreter |
| 435 | // which always allocates an object for new-instance of String. |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | for (HNewInstance* new_instance : uninitialized_strings_) { |
Aart Bik | eda3140 | 2016-03-24 15:38:56 -0700 | [diff] [blame] | 440 | DCHECK(new_instance->IsInBlock()); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 441 | DCHECK(new_instance->IsStringAlloc()); |
| 442 | |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 443 | // Replace NewInstance of String with NullConstant if not used prior to |
| 444 | // calling StringFactory. In case of deoptimization, the interpreter is |
| 445 | // expected to skip null check on the `this` argument of the StringFactory call. |
Nicolas Geoffray | 98e6ce4 | 2016-02-16 18:42:15 +0000 | [diff] [blame] | 446 | if (!new_instance->HasNonEnvironmentUses() && !HasAliasInEnvironments(new_instance)) { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 447 | new_instance->ReplaceWith(graph_->GetNullConstant()); |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 448 | new_instance->GetBlock()->RemoveInstruction(new_instance); |
| 449 | |
| 450 | // Remove LoadClass if not needed any more. |
Nicolas Geoffray | 5e08e36 | 2016-02-15 15:56:11 +0000 | [diff] [blame] | 451 | HInstruction* input = new_instance->InputAt(0); |
| 452 | HLoadClass* load_class = nullptr; |
| 453 | |
| 454 | // If the class was not present in the dex cache at the point of building |
| 455 | // the graph, the builder inserted a HClinitCheck in between. Since the String |
| 456 | // class is always initialized at the point of running Java code, we can remove |
| 457 | // that check. |
| 458 | if (input->IsClinitCheck()) { |
| 459 | load_class = input->InputAt(0)->AsLoadClass(); |
| 460 | input->ReplaceWith(load_class); |
| 461 | input->GetBlock()->RemoveInstruction(input); |
| 462 | } else { |
| 463 | load_class = input->AsLoadClass(); |
| 464 | DCHECK(new_instance->IsStringAlloc()); |
| 465 | DCHECK(!load_class->NeedsAccessCheck()) << "String class is always accessible"; |
| 466 | } |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 467 | DCHECK(load_class != nullptr); |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 468 | if (!load_class->HasUses()) { |
Nicolas Geoffray | 5e08e36 | 2016-02-15 15:56:11 +0000 | [diff] [blame] | 469 | // Even if the HLoadClass needs access check, we can remove it, as we know the |
| 470 | // String class does not need it. |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 471 | load_class->GetBlock()->RemoveInstruction(load_class); |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 477 | GraphAnalysisResult SsaBuilder::BuildSsa() { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 478 | DCHECK(!graph_->IsInSsaForm()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 479 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 480 | // 1) Propagate types of phis. At this point, phis are typed void in the general |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 481 | // case, or float/double/reference if we created an equivalent phi. So we need |
| 482 | // to propagate the types across phis to give them a correct type. If a type |
| 483 | // conflict is detected in this stage, the phi is marked dead. |
| 484 | RunPrimitiveTypePropagation(); |
| 485 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 486 | // 2) Now that the correct primitive types have been assigned, we can get rid |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 487 | // of redundant phis. Note that we cannot do this phase before type propagation, |
| 488 | // otherwise we could get rid of phi equivalents, whose presence is a requirement |
| 489 | // for the type propagation phase. Note that this is to satisfy statement (a) |
| 490 | // of the SsaBuilder (see ssa_builder.h). |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 491 | SsaRedundantPhiElimination(graph_).Run(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 492 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 493 | // 3) Fix the type for null constants which are part of an equality comparison. |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 494 | // We need to do this after redundant phi elimination, to ensure the only cases |
| 495 | // that we can see are reference comparison against 0. The redundant phi |
| 496 | // elimination ensures we do not see a phi taking two 0 constants in a HEqual |
| 497 | // or HNotEqual. |
| 498 | FixNullConstantType(); |
| 499 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 500 | // 4) Compute type of reference type instructions. The pass assumes that |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 501 | // NullConstant has been fixed up. |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 502 | ReferenceTypePropagation(graph_, |
| 503 | class_loader_, |
| 504 | dex_cache_, |
| 505 | handles_, |
| 506 | /* is_first_run */ true).Run(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 507 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 508 | // 5) HInstructionBuilder duplicated ArrayGet instructions with ambiguous type |
| 509 | // (int/float or long/double) and marked ArraySets with ambiguous input type. |
| 510 | // Now that RTP computed the type of the array input, the ambiguity can be |
| 511 | // resolved and the correct equivalents kept. |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 512 | if (!FixAmbiguousArrayOps()) { |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 513 | return kAnalysisFailAmbiguousArrayOp; |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 514 | } |
| 515 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 516 | // 6) Mark dead phis. This will mark phis which are not used by instructions |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 517 | // or other live phis. If compiling as debuggable code, phis will also be kept |
| 518 | // live if they have an environment use. |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 519 | SsaDeadPhiElimination dead_phi_elimimation(graph_); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 520 | dead_phi_elimimation.MarkDeadPhis(); |
| 521 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 522 | // 7) Make sure environments use the right phi equivalent: a phi marked dead |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 523 | // can have a phi equivalent that is not dead. In that case we have to replace |
| 524 | // it with the live equivalent because deoptimization and try/catch rely on |
| 525 | // environments containing values of all live vregs at that point. Note that |
| 526 | // there can be multiple phis for the same Dex register that are live |
| 527 | // (for example when merging constants), in which case it is okay for the |
| 528 | // environments to just reference one. |
| 529 | FixEnvironmentPhis(); |
| 530 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 531 | // 8) Now that the right phis are used for the environments, we can eliminate |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 532 | // phis we do not need. Regardless of the debuggable status, this phase is |
| 533 | /// necessary for statement (b) of the SsaBuilder (see ssa_builder.h), as well |
| 534 | // as for the code generation, which does not deal with phis of conflicting |
| 535 | // input types. |
| 536 | dead_phi_elimimation.EliminateDeadPhis(); |
| 537 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 538 | // 9) HInstructionBuidler replaced uses of NewInstances of String with the |
| 539 | // results of their corresponding StringFactory calls. Unless the String |
| 540 | // objects are used before they are initialized, they can be replaced with |
| 541 | // NullConstant. Note that this optimization is valid only if unsimplified |
| 542 | // code does not use the uninitialized value because we assume execution can |
| 543 | // be deoptimized at any safepoint. We must therefore perform it before any |
| 544 | // other optimizations. |
David Brazdil | 65902e8 | 2016-01-15 09:35:13 +0000 | [diff] [blame] | 545 | RemoveRedundantUninitializedStrings(); |
| 546 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 547 | graph_->SetInSsaForm(); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 548 | return kAnalysisSuccess; |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 549 | } |
| 550 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 551 | /** |
| 552 | * Constants in the Dex format are not typed. So the builder types them as |
| 553 | * integers, but when doing the SSA form, we might realize the constant |
| 554 | * is used for floating point operations. We create a floating-point equivalent |
| 555 | * constant to make the operations correctly typed. |
| 556 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 557 | HFloatConstant* SsaBuilder::GetFloatEquivalent(HIntConstant* constant) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 558 | // We place the floating point constant next to this constant. |
| 559 | HFloatConstant* result = constant->GetNext()->AsFloatConstant(); |
| 560 | if (result == nullptr) { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 561 | float value = bit_cast<float, int32_t>(constant->GetValue()); |
| 562 | result = new (graph_->GetArena()) HFloatConstant(value); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 563 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 564 | graph_->CacheFloatConstant(result); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 565 | } else { |
| 566 | // If there is already a constant with the expected type, we know it is |
| 567 | // the floating point equivalent of this constant. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 568 | DCHECK_EQ((bit_cast<int32_t, float>(result->GetValue())), constant->GetValue()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 569 | } |
| 570 | return result; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * Wide constants in the Dex format are not typed. So the builder types them as |
| 575 | * longs, but when doing the SSA form, we might realize the constant |
| 576 | * is used for floating point operations. We create a floating-point equivalent |
| 577 | * constant to make the operations correctly typed. |
| 578 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 579 | HDoubleConstant* SsaBuilder::GetDoubleEquivalent(HLongConstant* constant) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 580 | // We place the floating point constant next to this constant. |
| 581 | HDoubleConstant* result = constant->GetNext()->AsDoubleConstant(); |
| 582 | if (result == nullptr) { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 583 | double value = bit_cast<double, int64_t>(constant->GetValue()); |
| 584 | result = new (graph_->GetArena()) HDoubleConstant(value); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 585 | constant->GetBlock()->InsertInstructionBefore(result, constant->GetNext()); |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 586 | graph_->CacheDoubleConstant(result); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 587 | } else { |
| 588 | // If there is already a constant with the expected type, we know it is |
| 589 | // the floating point equivalent of this constant. |
Roland Levillain | da4d79b | 2015-03-24 14:36:11 +0000 | [diff] [blame] | 590 | DCHECK_EQ((bit_cast<int64_t, double>(result->GetValue())), constant->GetValue()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 591 | } |
| 592 | return result; |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Because of Dex format, we might end up having the same phi being |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 597 | * used for non floating point operations and floating point / reference operations. |
| 598 | * Because we want the graph to be correctly typed (and thereafter avoid moves between |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 599 | * floating point registers and core registers), we need to create a copy of the |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 600 | * phi with a floating point / reference type. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 601 | */ |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 602 | HPhi* SsaBuilder::GetFloatDoubleOrReferenceEquivalentOfPhi(HPhi* phi, Primitive::Type type) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 603 | DCHECK(phi->IsLive()) << "Cannot get equivalent of a dead phi since it would create a live one."; |
| 604 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 605 | // We place the floating point /reference phi next to this phi. |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 606 | HInstruction* next = phi->GetNext(); |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 607 | if (next != nullptr |
| 608 | && next->AsPhi()->GetRegNumber() == phi->GetRegNumber() |
| 609 | && next->GetType() != type) { |
| 610 | // Move to the next phi to see if it is the one we are looking for. |
| 611 | next = next->GetNext(); |
| 612 | } |
| 613 | |
| 614 | if (next == nullptr |
| 615 | || (next->AsPhi()->GetRegNumber() != phi->GetRegNumber()) |
| 616 | || (next->GetType() != type)) { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 617 | ArenaAllocator* allocator = graph_->GetArena(); |
Vladimir Marko | e900491 | 2016-06-16 16:50:52 +0100 | [diff] [blame] | 618 | HInputsRef inputs = phi->GetInputs(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 619 | HPhi* new_phi = |
| 620 | new (allocator) HPhi(allocator, phi->GetRegNumber(), inputs.size(), type); |
| 621 | // Copy the inputs. Note that the graph may not be correctly typed |
| 622 | // by doing this copy, but the type propagation phase will fix it. |
| 623 | ArrayRef<HUserRecord<HInstruction*>> new_input_records = new_phi->GetInputRecords(); |
| 624 | for (size_t i = 0; i < inputs.size(); ++i) { |
| 625 | new_input_records[i] = HUserRecord<HInstruction*>(inputs[i]); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 626 | } |
| 627 | phi->GetBlock()->InsertPhiAfter(new_phi, phi); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 628 | DCHECK(new_phi->IsLive()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 629 | return new_phi; |
| 630 | } else { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 631 | // An existing equivalent was found. If it is dead, conflict was previously |
| 632 | // identified and we return nullptr instead. |
David Brazdil | 809d70f | 2015-11-19 10:29:39 +0000 | [diff] [blame] | 633 | HPhi* next_phi = next->AsPhi(); |
| 634 | DCHECK_EQ(next_phi->GetType(), type); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 635 | return next_phi->IsLive() ? next_phi : nullptr; |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 639 | HArrayGet* SsaBuilder::GetFloatOrDoubleEquivalentOfArrayGet(HArrayGet* aget) { |
| 640 | DCHECK(Primitive::IsIntegralType(aget->GetType())); |
| 641 | |
| 642 | if (!Primitive::IsIntOrLongType(aget->GetType())) { |
| 643 | // Cannot type boolean, char, byte, short to float/double. |
| 644 | return nullptr; |
| 645 | } |
| 646 | |
| 647 | DCHECK(ContainsElement(ambiguous_agets_, aget)); |
| 648 | if (agets_fixed_) { |
| 649 | // This used to be an ambiguous ArrayGet but its type has been resolved to |
| 650 | // int/long. Requesting a float/double equivalent should lead to a conflict. |
| 651 | if (kIsDebugBuild) { |
| 652 | ScopedObjectAccess soa(Thread::Current()); |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 653 | DCHECK(Primitive::IsIntOrLongType(GetPrimitiveArrayComponentType(aget->GetArray()))); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 654 | } |
| 655 | return nullptr; |
| 656 | } else { |
| 657 | // This is an ambiguous ArrayGet which has not been resolved yet. Return an |
| 658 | // equivalent float/double instruction to use until it is resolved. |
| 659 | HArrayGet* equivalent = FindFloatOrDoubleEquivalentOfArrayGet(aget); |
| 660 | return (equivalent == nullptr) ? CreateFloatOrDoubleEquivalentOfArrayGet(aget) : equivalent; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | HInstruction* SsaBuilder::GetFloatOrDoubleEquivalent(HInstruction* value, Primitive::Type type) { |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 665 | if (value->IsArrayGet()) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 666 | return GetFloatOrDoubleEquivalentOfArrayGet(value->AsArrayGet()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 667 | } else if (value->IsLongConstant()) { |
| 668 | return GetDoubleEquivalent(value->AsLongConstant()); |
| 669 | } else if (value->IsIntConstant()) { |
| 670 | return GetFloatEquivalent(value->AsIntConstant()); |
| 671 | } else if (value->IsPhi()) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 672 | return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), type); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 673 | } else { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 674 | return nullptr; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 678 | HInstruction* SsaBuilder::GetReferenceTypeEquivalent(HInstruction* value) { |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 679 | if (value->IsIntConstant() && value->AsIntConstant()->GetValue() == 0) { |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 680 | return graph_->GetNullConstant(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 681 | } else if (value->IsPhi()) { |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 682 | return GetFloatDoubleOrReferenceEquivalentOfPhi(value->AsPhi(), Primitive::kPrimNot); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 683 | } else { |
| 684 | return nullptr; |
Nicolas Geoffray | d6138ef | 2015-02-18 14:48:53 +0000 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Nicolas Geoffray | c32e770 | 2014-04-24 12:43:16 +0100 | [diff] [blame] | 688 | } // namespace art |