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