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