licm.cc revision 82091dad38f3e5bfaf3b6984c9ab73069fb68310
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" 18#include "side_effects_analysis.h" 19 20namespace art { 21 22static bool IsPhiOf(HInstruction* instruction, HBasicBlock* block) { 23 return instruction->IsPhi() && instruction->GetBlock() == block; 24} 25 26/** 27 * Returns whether `instruction` has all its inputs and environment defined 28 * before the loop it is in. 29 */ 30static bool InputsAreDefinedBeforeLoop(HInstruction* instruction) { 31 DCHECK(instruction->IsInLoop()); 32 HLoopInformation* info = instruction->GetBlock()->GetLoopInformation(); 33 for (HInputIterator it(instruction); !it.Done(); it.Advance()) { 34 HLoopInformation* input_loop = it.Current()->GetBlock()->GetLoopInformation(); 35 // We only need to check whether the input is defined in the loop. If it is not 36 // it is defined before the loop. 37 if (input_loop != nullptr && input_loop->IsIn(*info)) { 38 return false; 39 } 40 } 41 42 if (instruction->HasEnvironment()) { 43 HEnvironment* environment = instruction->GetEnvironment(); 44 for (size_t i = 0, e = environment->Size(); i < e; ++i) { 45 HInstruction* input = environment->GetInstructionAt(i); 46 if (input != nullptr) { 47 HLoopInformation* input_loop = input->GetBlock()->GetLoopInformation(); 48 if (input_loop != nullptr && input_loop->IsIn(*info)) { 49 // We can move an instruction that takes a loop header phi in the environment: 50 // we will just replace that phi with its first input later in `UpdateLoopPhisIn`. 51 bool is_loop_header_phi = IsPhiOf(input, info->GetHeader()); 52 if (!is_loop_header_phi) { 53 return false; 54 } 55 } 56 } 57 } 58 } 59 return true; 60} 61 62/** 63 * If `environment` has a loop header phi, we replace it with its first input. 64 */ 65static void UpdateLoopPhisIn(HEnvironment* environment, HLoopInformation* info) { 66 for (size_t i = 0, e = environment->Size(); i < e; ++i) { 67 HInstruction* input = environment->GetInstructionAt(i); 68 if (input != nullptr && IsPhiOf(input, info->GetHeader())) { 69 HUseListNode<HEnvironment*>* env_use = environment->GetInstructionEnvUseAt(i); 70 input->RemoveEnvironmentUser(env_use); 71 HInstruction* incoming = input->InputAt(0); 72 environment->SetRawEnvAt(i, incoming); 73 incoming->AddEnvUseAt(environment, i); 74 } 75 } 76} 77 78void LICM::Run() { 79 DCHECK(side_effects_.HasRun()); 80 // Only used during debug. 81 ArenaBitVector visited(graph_->GetArena(), graph_->GetBlocks().Size(), false); 82 83 // Post order visit to visit inner loops before outer loops. 84 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) { 85 HBasicBlock* block = it.Current(); 86 if (!block->IsLoopHeader()) { 87 // Only visit the loop when we reach the header. 88 continue; 89 } 90 91 HLoopInformation* loop_info = block->GetLoopInformation(); 92 SideEffects loop_effects = side_effects_.GetLoopEffects(block); 93 HBasicBlock* pre_header = loop_info->GetPreHeader(); 94 95 for (HBlocksInLoopIterator it_loop(*loop_info); !it_loop.Done(); it_loop.Advance()) { 96 HBasicBlock* inner = it_loop.Current(); 97 DCHECK(inner->IsInLoop()); 98 if (inner->GetLoopInformation() != loop_info) { 99 // Thanks to post order visit, inner loops were already visited. 100 DCHECK(visited.IsBitSet(inner->GetBlockId())); 101 continue; 102 } 103 visited.SetBit(inner->GetBlockId()); 104 105 // We can move an instruction that can throw only if it is the first 106 // throwing instruction in the loop. Note that the first potentially 107 // throwing instruction encountered that is not hoisted stops this 108 // optimization. Non-throwing instruction can still be hoisted. 109 bool found_first_non_hoisted_throwing_instruction_in_loop = !inner->IsLoopHeader(); 110 for (HInstructionIterator inst_it(inner->GetInstructions()); 111 !inst_it.Done(); 112 inst_it.Advance()) { 113 HInstruction* instruction = inst_it.Current(); 114 if (instruction->CanBeMoved() 115 && (!instruction->CanThrow() || !found_first_non_hoisted_throwing_instruction_in_loop) 116 && !instruction->GetSideEffects().DependsOn(loop_effects) 117 && InputsAreDefinedBeforeLoop(instruction)) { 118 // We need to update the environment if the instruction has a loop header 119 // phi in it. 120 if (instruction->NeedsEnvironment()) { 121 UpdateLoopPhisIn(instruction->GetEnvironment(), loop_info); 122 } 123 instruction->MoveBefore(pre_header->GetLastInstruction()); 124 } else if (instruction->CanThrow()) { 125 // If `instruction` can throw, we cannot move further instructions 126 // that can throw as well. 127 found_first_non_hoisted_throwing_instruction_in_loop = true; 128 } 129 } 130 } 131 } 132} 133 134} // namespace art 135