David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 "boolean_simplifier.h" |
| 18 | |
| 19 | namespace art { |
| 20 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 21 | void HBooleanSimplifier::TryRemovingNegatedCondition(HBasicBlock* block) { |
| 22 | DCHECK(block->EndsWithIf()); |
| 23 | |
| 24 | // Check if the condition is a Boolean negation. |
| 25 | HIf* if_instruction = block->GetLastInstruction()->AsIf(); |
| 26 | HInstruction* boolean_not = if_instruction->InputAt(0); |
| 27 | if (!boolean_not->IsBooleanNot()) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | // Make BooleanNot's input the condition of the If and swap branches. |
| 32 | if_instruction->ReplaceInput(boolean_not->InputAt(0), 0); |
| 33 | block->SwapSuccessors(); |
| 34 | |
| 35 | // Remove the BooleanNot if it is now unused. |
| 36 | if (!boolean_not->HasUses()) { |
| 37 | boolean_not->GetBlock()->RemoveInstruction(boolean_not); |
| 38 | } |
| 39 | } |
| 40 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 41 | // Returns true if 'block1' and 'block2' are empty, merge into the same single |
| 42 | // successor and the successor can only be reached from them. |
| 43 | static bool BlocksDoMergeTogether(HBasicBlock* block1, HBasicBlock* block2) { |
| 44 | if (!block1->IsSingleGoto() || !block2->IsSingleGoto()) return false; |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 45 | HBasicBlock* succ1 = block1->GetSuccessors()[0]; |
| 46 | HBasicBlock* succ2 = block2->GetSuccessors()[0]; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 47 | return succ1 == succ2 && succ1->GetPredecessors().size() == 2u; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Returns true if the outcome of the branching matches the boolean value of |
| 51 | // the branching condition. |
| 52 | static bool PreservesCondition(HInstruction* input_true, HInstruction* input_false) { |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 53 | return input_true->IsIntConstant() && input_true->AsIntConstant()->IsOne() |
| 54 | && input_false->IsIntConstant() && input_false->AsIntConstant()->IsZero(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | // Returns true if the outcome of the branching is exactly opposite of the |
| 58 | // boolean value of the branching condition. |
| 59 | static bool NegatesCondition(HInstruction* input_true, HInstruction* input_false) { |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 60 | return input_true->IsIntConstant() && input_true->AsIntConstant()->IsZero() |
| 61 | && input_false->IsIntConstant() && input_false->AsIntConstant()->IsOne(); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 62 | } |
| 63 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 64 | void HBooleanSimplifier::TryRemovingBooleanSelection(HBasicBlock* block) { |
| 65 | DCHECK(block->EndsWithIf()); |
| 66 | |
| 67 | // Find elements of the pattern. |
| 68 | HIf* if_instruction = block->GetLastInstruction()->AsIf(); |
| 69 | HBasicBlock* true_block = if_instruction->IfTrueSuccessor(); |
| 70 | HBasicBlock* false_block = if_instruction->IfFalseSuccessor(); |
| 71 | if (!BlocksDoMergeTogether(true_block, false_block)) { |
| 72 | return; |
| 73 | } |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 74 | HBasicBlock* merge_block = true_block->GetSuccessors()[0]; |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 75 | if (!merge_block->HasSinglePhi()) { |
| 76 | return; |
| 77 | } |
| 78 | HPhi* phi = merge_block->GetFirstPhi()->AsPhi(); |
| 79 | HInstruction* true_value = phi->InputAt(merge_block->GetPredecessorIndexOf(true_block)); |
| 80 | HInstruction* false_value = phi->InputAt(merge_block->GetPredecessorIndexOf(false_block)); |
| 81 | |
| 82 | // Check if the selection negates/preserves the value of the condition and |
| 83 | // if so, generate a suitable replacement instruction. |
| 84 | HInstruction* if_condition = if_instruction->InputAt(0); |
Mark Mendell | c470193 | 2015-04-10 13:18:51 -0400 | [diff] [blame] | 85 | |
| 86 | // Don't change FP compares. The definition of compares involving NaNs forces |
| 87 | // the compares to be done as written by the user. |
| 88 | if (if_condition->IsCondition() && |
| 89 | Primitive::IsFloatingPointType(if_condition->InputAt(0)->GetType())) { |
| 90 | return; |
| 91 | } |
| 92 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 93 | HInstruction* replacement; |
| 94 | if (NegatesCondition(true_value, false_value)) { |
Mark Mendell | f652917 | 2015-11-17 11:16:56 -0500 | [diff] [blame^] | 95 | replacement = graph_->InsertOppositeCondition(if_condition, if_instruction); |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 96 | } else if (PreservesCondition(true_value, false_value)) { |
| 97 | replacement = if_condition; |
| 98 | } else { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // Replace the selection outcome with the new instruction. |
| 103 | phi->ReplaceWith(replacement); |
| 104 | merge_block->RemovePhi(phi); |
| 105 | |
| 106 | // Delete the true branch and merge the resulting chain of blocks |
| 107 | // 'block->false_block->merge_block' into one. |
| 108 | true_block->DisconnectAndDelete(); |
| 109 | block->MergeWith(false_block); |
| 110 | block->MergeWith(merge_block); |
| 111 | |
Nicolas Geoffray | 1f82ecc | 2015-06-24 12:20:24 +0100 | [diff] [blame] | 112 | // No need to update any dominance information, as we are simplifying |
| 113 | // a simple diamond shape, where the join block is merged with the |
| 114 | // entry block. Any following blocks would have had the join block |
| 115 | // as a dominator, and `MergeWith` handles changing that to the |
| 116 | // entry block. |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 117 | } |
| 118 | |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 119 | void HBooleanSimplifier::Run() { |
| 120 | // Iterate in post order in the unlikely case that removing one occurrence of |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 121 | // the selection pattern empties a branch block of another occurrence. |
| 122 | // Otherwise the order does not matter. |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 123 | for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 124 | HBasicBlock* block = it.Current(); |
David Brazdil | b2bd1c5 | 2015-03-25 11:17:37 +0000 | [diff] [blame] | 125 | if (!block->EndsWithIf()) continue; |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 126 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 127 | // If condition is negated, remove the negation and swap the branches. |
| 128 | TryRemovingNegatedCondition(block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 129 | |
David Brazdil | 769c9e5 | 2015-04-27 13:54:09 +0100 | [diff] [blame] | 130 | // If this is a boolean-selection diamond pattern, replace its result with |
| 131 | // the condition value (or its negation) and simplify the graph. |
| 132 | TryRemovingBooleanSelection(block); |
David Brazdil | 46e2a39 | 2015-03-16 17:31:52 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | } // namespace art |