Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +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 "licm.h" |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 18 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 19 | #include "side_effects_analysis.h" |
| 20 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 21 | namespace art { |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 22 | |
| 23 | static bool IsPhiOf(HInstruction* instruction, HBasicBlock* block) { |
| 24 | return instruction->IsPhi() && instruction->GetBlock() == block; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Returns whether `instruction` has all its inputs and environment defined |
| 29 | * before the loop it is in. |
| 30 | */ |
| 31 | static bool InputsAreDefinedBeforeLoop(HInstruction* instruction) { |
| 32 | DCHECK(instruction->IsInLoop()); |
| 33 | HLoopInformation* info = instruction->GetBlock()->GetLoopInformation(); |
Vladimir Marko | 372f10e | 2016-05-17 16:30:10 +0100 | [diff] [blame] | 34 | for (const HInstruction* input : instruction->GetInputs()) { |
| 35 | HLoopInformation* input_loop = input->GetBlock()->GetLoopInformation(); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 36 | // We only need to check whether the input is defined in the loop. If it is not |
| 37 | // it is defined before the loop. |
| 38 | if (input_loop != nullptr && input_loop->IsIn(*info)) { |
| 39 | return false; |
| 40 | } |
| 41 | } |
| 42 | |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 43 | for (HEnvironment* environment = instruction->GetEnvironment(); |
| 44 | environment != nullptr; |
| 45 | environment = environment->GetParent()) { |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 46 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 47 | HInstruction* input = environment->GetInstructionAt(i); |
| 48 | if (input != nullptr) { |
| 49 | HLoopInformation* input_loop = input->GetBlock()->GetLoopInformation(); |
| 50 | if (input_loop != nullptr && input_loop->IsIn(*info)) { |
| 51 | // We can move an instruction that takes a loop header phi in the environment: |
| 52 | // we will just replace that phi with its first input later in `UpdateLoopPhisIn`. |
| 53 | bool is_loop_header_phi = IsPhiOf(input, info->GetHeader()); |
| 54 | if (!is_loop_header_phi) { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * If `environment` has a loop header phi, we replace it with its first input. |
| 66 | */ |
| 67 | static void UpdateLoopPhisIn(HEnvironment* environment, HLoopInformation* info) { |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 68 | for (; environment != nullptr; environment = environment->GetParent()) { |
| 69 | for (size_t i = 0, e = environment->Size(); i < e; ++i) { |
| 70 | HInstruction* input = environment->GetInstructionAt(i); |
| 71 | if (input != nullptr && IsPhiOf(input, info->GetHeader())) { |
| 72 | environment->RemoveAsUserOfInput(i); |
| 73 | HInstruction* incoming = input->InputAt(0); |
| 74 | environment->SetRawEnvAt(i, incoming); |
| 75 | incoming->AddEnvUseAt(environment, i); |
| 76 | } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 81 | bool LICM::Run() { |
| 82 | bool didLICM = false; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 83 | DCHECK(side_effects_.HasRun()); |
David Brazdil | aa680e8 | 2016-04-14 16:36:15 +0100 | [diff] [blame] | 84 | |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 85 | // Only used during debug. |
David Brazdil | aa680e8 | 2016-04-14 16:36:15 +0100 | [diff] [blame] | 86 | ArenaBitVector* visited = nullptr; |
| 87 | if (kIsDebugBuild) { |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 88 | visited = new (graph_->GetAllocator()) ArenaBitVector(graph_->GetAllocator(), |
| 89 | graph_->GetBlocks().size(), |
| 90 | false, |
| 91 | kArenaAllocLICM); |
David Brazdil | aa680e8 | 2016-04-14 16:36:15 +0100 | [diff] [blame] | 92 | } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 93 | |
| 94 | // Post order visit to visit inner loops before outer loops. |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 95 | for (HBasicBlock* block : graph_->GetPostOrder()) { |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 96 | if (!block->IsLoopHeader()) { |
| 97 | // Only visit the loop when we reach the header. |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | HLoopInformation* loop_info = block->GetLoopInformation(); |
| 102 | SideEffects loop_effects = side_effects_.GetLoopEffects(block); |
| 103 | HBasicBlock* pre_header = loop_info->GetPreHeader(); |
| 104 | |
| 105 | for (HBlocksInLoopIterator it_loop(*loop_info); !it_loop.Done(); it_loop.Advance()) { |
| 106 | HBasicBlock* inner = it_loop.Current(); |
| 107 | DCHECK(inner->IsInLoop()); |
| 108 | if (inner->GetLoopInformation() != loop_info) { |
| 109 | // Thanks to post order visit, inner loops were already visited. |
David Brazdil | aa680e8 | 2016-04-14 16:36:15 +0100 | [diff] [blame] | 110 | DCHECK(visited->IsBitSet(inner->GetBlockId())); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 111 | continue; |
| 112 | } |
David Brazdil | aa680e8 | 2016-04-14 16:36:15 +0100 | [diff] [blame] | 113 | if (kIsDebugBuild) { |
| 114 | visited->SetBit(inner->GetBlockId()); |
| 115 | } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 116 | |
Nicolas Geoffray | d7c2fdc | 2016-05-10 14:35:34 +0100 | [diff] [blame] | 117 | if (loop_info->ContainsIrreducibleLoop()) { |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 118 | // We cannot licm in an irreducible loop, or in a natural loop containing an |
| 119 | // irreducible loop. |
| 120 | continue; |
| 121 | } |
Nicolas Geoffray | d7c2fdc | 2016-05-10 14:35:34 +0100 | [diff] [blame] | 122 | DCHECK(!loop_info->IsIrreducible()); |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 123 | |
Aart Bik | 08ec180 | 2016-11-10 18:21:30 -0800 | [diff] [blame] | 124 | // We can move an instruction that can throw only as long as it is the first visible |
| 125 | // instruction (throw or write) in the loop. Note that the first potentially visible |
| 126 | // instruction that is not hoisted stops this optimization. Non-throwing instructions, |
| 127 | // on the other hand, can still be hoisted. |
| 128 | bool found_first_non_hoisted_visible_instruction_in_loop = !inner->IsLoopHeader(); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 129 | for (HInstructionIterator inst_it(inner->GetInstructions()); |
| 130 | !inst_it.Done(); |
| 131 | inst_it.Advance()) { |
| 132 | HInstruction* instruction = inst_it.Current(); |
Mingyao Yang | 217eb06 | 2017-12-11 15:20:07 -0800 | [diff] [blame] | 133 | bool can_move = false; |
| 134 | if (instruction->CanBeMoved() && InputsAreDefinedBeforeLoop(instruction)) { |
| 135 | if (instruction->CanThrow()) { |
| 136 | if (!found_first_non_hoisted_visible_instruction_in_loop) { |
| 137 | DCHECK(instruction->GetBlock()->IsLoopHeader()); |
| 138 | if (instruction->IsClinitCheck()) { |
| 139 | // clinit is only done once, and since all visible instructions |
| 140 | // in the loop header so far have been hoisted out, we can hoist |
| 141 | // the clinit check out also. |
| 142 | can_move = true; |
| 143 | } else if (!instruction->GetSideEffects().MayDependOn(loop_effects)) { |
| 144 | can_move = true; |
| 145 | } |
| 146 | } |
| 147 | } else if (!instruction->GetSideEffects().MayDependOn(loop_effects)) { |
| 148 | can_move = true; |
| 149 | } |
| 150 | } |
| 151 | if (can_move) { |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 152 | // We need to update the environment if the instruction has a loop header |
| 153 | // phi in it. |
| 154 | if (instruction->NeedsEnvironment()) { |
| 155 | UpdateLoopPhisIn(instruction->GetEnvironment(), loop_info); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 156 | } else { |
| 157 | DCHECK(!instruction->HasEnvironment()); |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 158 | } |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 159 | instruction->MoveBefore(pre_header->GetLastInstruction()); |
Igor Murashkin | 1e065a5 | 2017-08-09 13:20:34 -0700 | [diff] [blame] | 160 | MaybeRecordStat(stats_, MethodCompilationStat::kLoopInvariantMoved); |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 161 | didLICM = true; |
Mingyao Yang | 217eb06 | 2017-12-11 15:20:07 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if (!can_move && (instruction->CanThrow() || instruction->DoesAnyWrite())) { |
Aart Bik | 08ec180 | 2016-11-10 18:21:30 -0800 | [diff] [blame] | 165 | // If `instruction` can do something visible (throw or write), |
| 166 | // we cannot move further instructions that can throw. |
| 167 | found_first_non_hoisted_visible_instruction_in_loop = true; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 172 | return didLICM; |
Nicolas Geoffray | 82091da | 2015-01-26 10:02:45 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | } // namespace art |