David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "select_generator.h" |
| 18 | |
Vladimir Marko | 009d166 | 2017-10-10 13:21:15 +0100 | [diff] [blame] | 19 | #include "reference_type_propagation.h" |
| 20 | |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 21 | namespace art { |
| 22 | |
| 23 | static constexpr size_t kMaxInstructionsInBranch = 1u; |
| 24 | |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 25 | HSelectGenerator::HSelectGenerator(HGraph* graph, |
| 26 | VariableSizedHandleScope* handles, |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 27 | OptimizingCompilerStats* stats, |
| 28 | const char* name) |
| 29 | : HOptimization(graph, name, stats), |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 30 | handle_scope_(handles) { |
| 31 | } |
| 32 | |
| 33 | // Returns true if `block` has only one predecessor, ends with a Goto |
| 34 | // or a Return and contains at most `kMaxInstructionsInBranch` other |
| 35 | // movable instruction with no side-effects. |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 36 | static bool IsSimpleBlock(HBasicBlock* block) { |
| 37 | if (block->GetPredecessors().size() != 1u) { |
| 38 | return false; |
| 39 | } |
| 40 | DCHECK(block->GetPhis().IsEmpty()); |
| 41 | |
| 42 | size_t num_instructions = 0u; |
| 43 | for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) { |
| 44 | HInstruction* instruction = it.Current(); |
| 45 | if (instruction->IsControlFlow()) { |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 46 | return instruction->IsGoto() || instruction->IsReturn(); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 47 | } else if (instruction->CanBeMoved() && !instruction->HasSideEffects()) { |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame^] | 48 | if (instruction->IsSelect() && |
| 49 | instruction->AsSelect()->GetCondition()->GetBlock() == block) { |
| 50 | // Count one HCondition and HSelect in the same block as a single instruction. |
| 51 | // This enables finding nested selects. |
| 52 | continue; |
| 53 | } else if (++num_instructions > kMaxInstructionsInBranch) { |
| 54 | return false; // bail as soon as we exceed number of allowed instructions |
| 55 | } |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 56 | } else { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | LOG(FATAL) << "Unreachable"; |
| 62 | UNREACHABLE(); |
| 63 | } |
| 64 | |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 65 | // Returns true if 'block1' and 'block2' are empty and merge into the |
| 66 | // same single successor. |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 67 | static bool BlocksMergeTogether(HBasicBlock* block1, HBasicBlock* block2) { |
| 68 | return block1->GetSingleSuccessor() == block2->GetSingleSuccessor(); |
| 69 | } |
| 70 | |
| 71 | // Returns nullptr if `block` has either no phis or there is more than one phi |
| 72 | // with different inputs at `index1` and `index2`. Otherwise returns that phi. |
| 73 | static HPhi* GetSingleChangedPhi(HBasicBlock* block, size_t index1, size_t index2) { |
| 74 | DCHECK_NE(index1, index2); |
| 75 | |
| 76 | HPhi* select_phi = nullptr; |
| 77 | for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) { |
| 78 | HPhi* phi = it.Current()->AsPhi(); |
| 79 | if (phi->InputAt(index1) != phi->InputAt(index2)) { |
| 80 | if (select_phi == nullptr) { |
| 81 | // First phi with different inputs for the two indices found. |
| 82 | select_phi = phi; |
| 83 | } else { |
| 84 | // More than one phis has different inputs for the two indices. |
| 85 | return nullptr; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return select_phi; |
| 90 | } |
| 91 | |
| 92 | void HSelectGenerator::Run() { |
| 93 | // Iterate in post order in the unlikely case that removing one occurrence of |
| 94 | // the selection pattern empties a branch block of another occurrence. |
| 95 | // Otherwise the order does not matter. |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 96 | for (HBasicBlock* block : graph_->GetPostOrder()) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 97 | if (!block->EndsWithIf()) continue; |
| 98 | |
| 99 | // Find elements of the diamond pattern. |
| 100 | HIf* if_instruction = block->GetLastInstruction()->AsIf(); |
| 101 | HBasicBlock* true_block = if_instruction->IfTrueSuccessor(); |
| 102 | HBasicBlock* false_block = if_instruction->IfFalseSuccessor(); |
| 103 | DCHECK_NE(true_block, false_block); |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame^] | 104 | |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 105 | if (!IsSimpleBlock(true_block) || |
| 106 | !IsSimpleBlock(false_block) || |
| 107 | !BlocksMergeTogether(true_block, false_block)) { |
| 108 | continue; |
| 109 | } |
| 110 | HBasicBlock* merge_block = true_block->GetSingleSuccessor(); |
| 111 | |
| 112 | // If the branches are not empty, move instructions in front of the If. |
| 113 | // TODO(dbrazdil): This puts an instruction between If and its condition. |
| 114 | // Implement moving of conditions to first users if possible. |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame^] | 115 | while (!true_block->IsSingleGoto() && !true_block->IsSingleReturn()) { |
David Brazdil | d6c205e | 2016-06-07 14:20:52 +0100 | [diff] [blame] | 116 | true_block->GetFirstInstruction()->MoveBefore(if_instruction); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 117 | } |
Aart Bik | 1d746de | 2018-03-28 16:30:02 -0700 | [diff] [blame^] | 118 | while (!false_block->IsSingleGoto() && !false_block->IsSingleReturn()) { |
David Brazdil | d6c205e | 2016-06-07 14:20:52 +0100 | [diff] [blame] | 119 | false_block->GetFirstInstruction()->MoveBefore(if_instruction); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 120 | } |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 121 | DCHECK(true_block->IsSingleGoto() || true_block->IsSingleReturn()); |
| 122 | DCHECK(false_block->IsSingleGoto() || false_block->IsSingleReturn()); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 123 | |
| 124 | // Find the resulting true/false values. |
| 125 | size_t predecessor_index_true = merge_block->GetPredecessorIndexOf(true_block); |
| 126 | size_t predecessor_index_false = merge_block->GetPredecessorIndexOf(false_block); |
| 127 | DCHECK_NE(predecessor_index_true, predecessor_index_false); |
| 128 | |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 129 | bool both_successors_return = true_block->IsSingleReturn() && false_block->IsSingleReturn(); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 130 | HPhi* phi = GetSingleChangedPhi(merge_block, predecessor_index_true, predecessor_index_false); |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 131 | |
| 132 | HInstruction* true_value = nullptr; |
| 133 | HInstruction* false_value = nullptr; |
| 134 | if (both_successors_return) { |
| 135 | true_value = true_block->GetFirstInstruction()->InputAt(0); |
| 136 | false_value = false_block->GetFirstInstruction()->InputAt(0); |
| 137 | } else if (phi != nullptr) { |
| 138 | true_value = phi->InputAt(predecessor_index_true); |
| 139 | false_value = phi->InputAt(predecessor_index_false); |
| 140 | } else { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 141 | continue; |
| 142 | } |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 143 | DCHECK(both_successors_return || phi != nullptr); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 144 | |
| 145 | // Create the Select instruction and insert it in front of the If. |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 146 | HSelect* select = new (graph_->GetAllocator()) HSelect(if_instruction->InputAt(0), |
| 147 | true_value, |
| 148 | false_value, |
| 149 | if_instruction->GetDexPc()); |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 150 | if (both_successors_return) { |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 151 | if (true_value->GetType() == DataType::Type::kReference) { |
| 152 | DCHECK(false_value->GetType() == DataType::Type::kReference); |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 153 | ReferenceTypePropagation::FixUpInstructionType(select, handle_scope_); |
| 154 | } |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 155 | } else if (phi->GetType() == DataType::Type::kReference) { |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 156 | select->SetReferenceTypeInfo(phi->GetReferenceTypeInfo()); |
| 157 | } |
| 158 | block->InsertInstructionBefore(select, if_instruction); |
| 159 | |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 160 | // Remove the true branch which removes the corresponding Phi |
| 161 | // input if needed. If left only with the false branch, the Phi is |
| 162 | // automatically removed. |
| 163 | if (both_successors_return) { |
| 164 | false_block->GetFirstInstruction()->ReplaceInput(select, 0); |
| 165 | } else { |
| 166 | phi->ReplaceInput(select, predecessor_index_false); |
| 167 | } |
| 168 | |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 169 | bool only_two_predecessors = (merge_block->GetPredecessors().size() == 2u); |
| 170 | true_block->DisconnectAndDelete(); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 171 | |
| 172 | // Merge remaining blocks which are now connected with Goto. |
| 173 | DCHECK_EQ(block->GetSingleSuccessor(), false_block); |
| 174 | block->MergeWith(false_block); |
Mads Ager | 16e5289 | 2017-07-14 13:11:37 +0200 | [diff] [blame] | 175 | if (!both_successors_return && only_two_predecessors) { |
| 176 | DCHECK_EQ(only_two_predecessors, phi->GetBlock() == nullptr); |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 177 | DCHECK_EQ(block->GetSingleSuccessor(), merge_block); |
| 178 | block->MergeWith(merge_block); |
| 179 | } |
| 180 | |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 181 | MaybeRecordStat(stats_, MethodCompilationStat::kSelectGenerated); |
Jean-Philippe Halimi | 38e9e80 | 2016-02-18 16:42:03 +0100 | [diff] [blame] | 182 | |
David Brazdil | 74eb1b2 | 2015-12-14 11:44:01 +0000 | [diff] [blame] | 183 | // No need to update dominance information, as we are simplifying |
| 184 | // a simple diamond shape, where the join block is merged with the |
| 185 | // entry block. Any following blocks would have had the join block |
| 186 | // as a dominator, and `MergeWith` handles changing that to the |
| 187 | // entry block. |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | } // namespace art |