blob: 5a0b89c90a2ca3b6087b45ee010b28f54e73c474 [file] [log] [blame]
Nicolas Geoffray82091da2015-01-26 10:02:45 +00001/*
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
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010042 for (HEnvironment* environment = instruction->GetEnvironment();
43 environment != nullptr;
44 environment = environment->GetParent()) {
Nicolas Geoffray82091da2015-01-26 10:02:45 +000045 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
46 HInstruction* input = environment->GetInstructionAt(i);
47 if (input != nullptr) {
48 HLoopInformation* input_loop = input->GetBlock()->GetLoopInformation();
49 if (input_loop != nullptr && input_loop->IsIn(*info)) {
50 // We can move an instruction that takes a loop header phi in the environment:
51 // we will just replace that phi with its first input later in `UpdateLoopPhisIn`.
52 bool is_loop_header_phi = IsPhiOf(input, info->GetHeader());
53 if (!is_loop_header_phi) {
54 return false;
55 }
56 }
57 }
58 }
59 }
60 return true;
61}
62
63/**
64 * If `environment` has a loop header phi, we replace it with its first input.
65 */
66static void UpdateLoopPhisIn(HEnvironment* environment, HLoopInformation* info) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +010067 for (; environment != nullptr; environment = environment->GetParent()) {
68 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
69 HInstruction* input = environment->GetInstructionAt(i);
70 if (input != nullptr && IsPhiOf(input, info->GetHeader())) {
71 environment->RemoveAsUserOfInput(i);
72 HInstruction* incoming = input->InputAt(0);
73 environment->SetRawEnvAt(i, incoming);
74 incoming->AddEnvUseAt(environment, i);
75 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +000076 }
77 }
78}
79
80void LICM::Run() {
81 DCHECK(side_effects_.HasRun());
David Brazdilaa680e82016-04-14 16:36:15 +010082
Nicolas Geoffray82091da2015-01-26 10:02:45 +000083 // Only used during debug.
David Brazdilaa680e82016-04-14 16:36:15 +010084 ArenaBitVector* visited = nullptr;
85 if (kIsDebugBuild) {
86 visited = new (graph_->GetArena()) ArenaBitVector(graph_->GetArena(),
87 graph_->GetBlocks().size(),
88 false,
89 kArenaAllocLICM);
90 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +000091
92 // Post order visit to visit inner loops before outer loops.
93 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
94 HBasicBlock* block = it.Current();
95 if (!block->IsLoopHeader()) {
96 // Only visit the loop when we reach the header.
97 continue;
98 }
99
100 HLoopInformation* loop_info = block->GetLoopInformation();
101 SideEffects loop_effects = side_effects_.GetLoopEffects(block);
102 HBasicBlock* pre_header = loop_info->GetPreHeader();
103
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000104 bool contains_irreducible_loop = false;
105 if (graph_->HasIrreducibleLoops()) {
106 for (HBlocksInLoopIterator it_loop(*loop_info); !it_loop.Done(); it_loop.Advance()) {
107 if (it_loop.Current()->GetLoopInformation()->IsIrreducible()) {
108 contains_irreducible_loop = true;
109 break;
110 }
111 }
112 }
113
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000114 for (HBlocksInLoopIterator it_loop(*loop_info); !it_loop.Done(); it_loop.Advance()) {
115 HBasicBlock* inner = it_loop.Current();
116 DCHECK(inner->IsInLoop());
117 if (inner->GetLoopInformation() != loop_info) {
118 // Thanks to post order visit, inner loops were already visited.
David Brazdilaa680e82016-04-14 16:36:15 +0100119 DCHECK(visited->IsBitSet(inner->GetBlockId()));
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000120 continue;
121 }
David Brazdilaa680e82016-04-14 16:36:15 +0100122 if (kIsDebugBuild) {
123 visited->SetBit(inner->GetBlockId());
124 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000125
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000126 if (contains_irreducible_loop) {
127 // We cannot licm in an irreducible loop, or in a natural loop containing an
128 // irreducible loop.
129 continue;
130 }
131
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000132 // We can move an instruction that can throw only if it is the first
133 // throwing instruction in the loop. Note that the first potentially
134 // throwing instruction encountered that is not hoisted stops this
135 // optimization. Non-throwing instruction can still be hoisted.
136 bool found_first_non_hoisted_throwing_instruction_in_loop = !inner->IsLoopHeader();
137 for (HInstructionIterator inst_it(inner->GetInstructions());
138 !inst_it.Done();
139 inst_it.Advance()) {
140 HInstruction* instruction = inst_it.Current();
141 if (instruction->CanBeMoved()
142 && (!instruction->CanThrow() || !found_first_non_hoisted_throwing_instruction_in_loop)
Aart Bik854a02b2015-07-14 16:07:00 -0700143 && !instruction->GetSideEffects().MayDependOn(loop_effects)
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000144 && InputsAreDefinedBeforeLoop(instruction)) {
145 // We need to update the environment if the instruction has a loop header
146 // phi in it.
147 if (instruction->NeedsEnvironment()) {
148 UpdateLoopPhisIn(instruction->GetEnvironment(), loop_info);
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100149 } else {
150 DCHECK(!instruction->HasEnvironment());
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000151 }
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000152 instruction->MoveBefore(pre_header->GetLastInstruction());
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +0100153 MaybeRecordStat(MethodCompilationStat::kLoopInvariantMoved);
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000154 } else if (instruction->CanThrow()) {
155 // If `instruction` can throw, we cannot move further instructions
156 // that can throw as well.
157 found_first_non_hoisted_throwing_instruction_in_loop = true;
158 }
159 }
160 }
161 }
162}
163
164} // namespace art