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