blob: b558eb17a7030aa6ecf2b320955e6aae54c3c394 [file] [log] [blame]
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +00001/*
2 * Copyright (C) 2017 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 "code_sinking.h"
18
19#include "common_dominator.h"
20#include "nodes.h"
21
22namespace art {
23
24void CodeSinking::Run() {
25 HBasicBlock* exit = graph_->GetExitBlock();
26 if (exit == nullptr) {
27 // Infinite loop, just bail.
28 return;
29 }
30 // TODO(ngeoffray): we do not profile branches yet, so use throw instructions
31 // as an indicator of an uncommon branch.
32 for (HBasicBlock* exit_predecessor : exit->GetPredecessors()) {
33 if (exit_predecessor->GetLastInstruction()->IsThrow()) {
34 SinkCodeToUncommonBranch(exit_predecessor);
35 }
36 }
37}
38
39static bool IsInterestingInstruction(HInstruction* instruction) {
40 // Instructions from the entry graph (for example constants) are never interesting to move.
41 if (instruction->GetBlock() == instruction->GetBlock()->GetGraph()->GetEntryBlock()) {
42 return false;
43 }
44 // We want to move moveable instructions that cannot throw, as well as
45 // heap stores and allocations.
46
47 // Volatile stores cannot be moved.
48 if (instruction->IsInstanceFieldSet()) {
49 if (instruction->AsInstanceFieldSet()->IsVolatile()) {
50 return false;
51 }
52 }
53
54 // Check allocations first, as they can throw, but it is safe to move them.
55 if (instruction->IsNewInstance() || instruction->IsNewArray()) {
56 return true;
57 }
58
Igor Murashkin79d8fa72017-04-18 09:37:23 -070059 // Check it is safe to move ConstructorFence.
60 // (Safe to move ConstructorFence for only protecting the new-instance but not for finals.)
61 if (instruction->IsConstructorFence()) {
62 HConstructorFence* ctor_fence = instruction->AsConstructorFence();
63
64 // A fence with "0" inputs is dead and should've been removed in a prior pass.
65 DCHECK_NE(0u, ctor_fence->InputCount());
66
Igor Murashkindd018df2017-08-09 10:38:31 -070067 // TODO: this should be simplified to 'return true' since it's
68 // potentially pessimizing any code sinking for inlined constructors with final fields.
69 // TODO: double check that if the final field assignments are not moved,
70 // then the fence is not moved either.
71
Igor Murashkin79d8fa72017-04-18 09:37:23 -070072 return ctor_fence->GetAssociatedAllocation() != nullptr;
73 }
74
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +000075 // All other instructions that can throw cannot be moved.
76 if (instruction->CanThrow()) {
77 return false;
78 }
79
80 // We can only store on local allocations. Other heap references can
81 // be escaping. Note that allocations can escape too, but we only move
82 // allocations if their users can move to, or are in the list of
83 // post dominated blocks.
84 if (instruction->IsInstanceFieldSet()) {
85 if (!instruction->InputAt(0)->IsNewInstance()) {
86 return false;
87 }
88 }
89
90 if (instruction->IsArraySet()) {
91 if (!instruction->InputAt(0)->IsNewArray()) {
92 return false;
93 }
94 }
95
96 // Heap accesses cannot go pass instructions that have memory side effects, which
97 // we are not tracking here. Note that the load/store elimination optimization
98 // runs before this optimization, and should have removed interesting ones.
99 // In theory, we could handle loads of local allocations, but this is currently
100 // hard to test, as LSE removes them.
101 if (instruction->IsStaticFieldGet() ||
102 instruction->IsInstanceFieldGet() ||
103 instruction->IsArrayGet()) {
104 return false;
105 }
106
107 if (instruction->IsInstanceFieldSet() ||
108 instruction->IsArraySet() ||
109 instruction->CanBeMoved()) {
110 return true;
111 }
112 return false;
113}
114
115static void AddInstruction(HInstruction* instruction,
116 const ArenaBitVector& processed_instructions,
117 const ArenaBitVector& discard_blocks,
118 ArenaVector<HInstruction*>* worklist) {
119 // Add to the work list if the instruction is not in the list of blocks
120 // to discard, hasn't been already processed and is of interest.
121 if (!discard_blocks.IsBitSet(instruction->GetBlock()->GetBlockId()) &&
122 !processed_instructions.IsBitSet(instruction->GetId()) &&
123 IsInterestingInstruction(instruction)) {
124 worklist->push_back(instruction);
125 }
126}
127
128static void AddInputs(HInstruction* instruction,
129 const ArenaBitVector& processed_instructions,
130 const ArenaBitVector& discard_blocks,
131 ArenaVector<HInstruction*>* worklist) {
132 for (HInstruction* input : instruction->GetInputs()) {
133 AddInstruction(input, processed_instructions, discard_blocks, worklist);
134 }
135}
136
137static void AddInputs(HBasicBlock* block,
138 const ArenaBitVector& processed_instructions,
139 const ArenaBitVector& discard_blocks,
140 ArenaVector<HInstruction*>* worklist) {
141 for (HInstructionIterator it(block->GetPhis()); !it.Done(); it.Advance()) {
142 AddInputs(it.Current(), processed_instructions, discard_blocks, worklist);
143 }
144 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
145 AddInputs(it.Current(), processed_instructions, discard_blocks, worklist);
146 }
147}
148
149static bool ShouldFilterUse(HInstruction* instruction,
150 HInstruction* user,
151 const ArenaBitVector& post_dominated) {
152 if (instruction->IsNewInstance()) {
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700153 return (user->IsInstanceFieldSet() || user->IsConstructorFence()) &&
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000154 (user->InputAt(0) == instruction) &&
155 !post_dominated.IsBitSet(user->GetBlock()->GetBlockId());
156 } else if (instruction->IsNewArray()) {
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700157 return (user->IsArraySet() || user->IsConstructorFence()) &&
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000158 (user->InputAt(0) == instruction) &&
159 !post_dominated.IsBitSet(user->GetBlock()->GetBlockId());
160 }
161 return false;
162}
163
164
165// Find the ideal position for moving `instruction`. If `filter` is true,
166// we filter out store instructions to that instruction, which are processed
167// first in the step (3) of the sinking algorithm.
168// This method is tailored to the sinking algorithm, unlike
169// the generic HInstruction::MoveBeforeFirstUserAndOutOfLoops.
170static HInstruction* FindIdealPosition(HInstruction* instruction,
171 const ArenaBitVector& post_dominated,
172 bool filter = false) {
173 DCHECK(!instruction->IsPhi()); // Makes no sense for Phi.
174
175 // Find the target block.
176 CommonDominator finder(/* start_block */ nullptr);
177 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
178 HInstruction* user = use.GetUser();
179 if (!(filter && ShouldFilterUse(instruction, user, post_dominated))) {
Nicolas Geoffray13445e72017-04-20 15:19:46 +0100180 HBasicBlock* block = user->GetBlock();
181 if (user->IsPhi()) {
182 // Special case phis by taking the incoming block for regular ones,
183 // or the dominator for catch phis.
184 block = user->AsPhi()->IsCatchPhi()
185 ? block->GetDominator()
186 : block->GetPredecessors()[use.GetIndex()];
187 }
188 finder.Update(block);
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000189 }
190 }
191 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
192 DCHECK(!use.GetUser()->GetHolder()->IsPhi());
193 DCHECK(!filter || !ShouldFilterUse(instruction, use.GetUser()->GetHolder(), post_dominated));
194 finder.Update(use.GetUser()->GetHolder()->GetBlock());
195 }
196 HBasicBlock* target_block = finder.Get();
197 if (target_block == nullptr) {
198 // No user we can go next to? Likely a LSE or DCE limitation.
199 return nullptr;
200 }
201
202 // Move to the first dominator not in a loop, if we can.
203 while (target_block->IsInLoop()) {
204 if (!post_dominated.IsBitSet(target_block->GetDominator()->GetBlockId())) {
205 break;
206 }
207 target_block = target_block->GetDominator();
208 DCHECK(target_block != nullptr);
209 }
210
211 // Find insertion position. No need to filter anymore, as we have found a
212 // target block.
213 HInstruction* insert_pos = nullptr;
214 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
215 if (use.GetUser()->GetBlock() == target_block &&
216 (insert_pos == nullptr || use.GetUser()->StrictlyDominates(insert_pos))) {
217 insert_pos = use.GetUser();
218 }
219 }
220 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
221 HInstruction* user = use.GetUser()->GetHolder();
222 if (user->GetBlock() == target_block &&
223 (insert_pos == nullptr || user->StrictlyDominates(insert_pos))) {
224 insert_pos = user;
225 }
226 }
227 if (insert_pos == nullptr) {
228 // No user in `target_block`, insert before the control flow instruction.
229 insert_pos = target_block->GetLastInstruction();
230 DCHECK(insert_pos->IsControlFlow());
231 // Avoid splitting HCondition from HIf to prevent unnecessary materialization.
232 if (insert_pos->IsIf()) {
233 HInstruction* if_input = insert_pos->AsIf()->InputAt(0);
234 if (if_input == insert_pos->GetPrevious()) {
235 insert_pos = if_input;
236 }
237 }
238 }
239 DCHECK(!insert_pos->IsPhi());
240 return insert_pos;
241}
242
243
244void CodeSinking::SinkCodeToUncommonBranch(HBasicBlock* end_block) {
245 // Local allocator to discard data structures created below at the end of
246 // this optimization.
247 ArenaAllocator allocator(graph_->GetArena()->GetArenaPool());
248
249 size_t number_of_instructions = graph_->GetCurrentInstructionId();
250 ArenaVector<HInstruction*> worklist(allocator.Adapter(kArenaAllocMisc));
251 ArenaBitVector processed_instructions(&allocator, number_of_instructions, /* expandable */ false);
252 ArenaBitVector post_dominated(&allocator, graph_->GetBlocks().size(), /* expandable */ false);
253 ArenaBitVector instructions_that_can_move(
254 &allocator, number_of_instructions, /* expandable */ false);
255 ArenaVector<HInstruction*> move_in_order(allocator.Adapter(kArenaAllocMisc));
256
257 // Step (1): Visit post order to get a subset of blocks post dominated by `end_block`.
258 // TODO(ngeoffray): Getting the full set of post-dominated shoud be done by
259 // computint the post dominator tree, but that could be too time consuming. Also,
260 // we should start the analysis from blocks dominated by an uncommon branch, but we
261 // don't profile branches yet.
262 bool found_block = false;
263 for (HBasicBlock* block : graph_->GetPostOrder()) {
264 if (block == end_block) {
265 found_block = true;
266 post_dominated.SetBit(block->GetBlockId());
267 } else if (found_block) {
268 bool is_post_dominated = true;
269 if (block->GetSuccessors().empty()) {
270 // We currently bail for loops.
271 is_post_dominated = false;
272 } else {
273 for (HBasicBlock* successor : block->GetSuccessors()) {
274 if (!post_dominated.IsBitSet(successor->GetBlockId())) {
275 is_post_dominated = false;
276 break;
277 }
278 }
279 }
280 if (is_post_dominated) {
281 post_dominated.SetBit(block->GetBlockId());
282 }
283 }
284 }
285
286 // Now that we have found a subset of post-dominated blocks, add to the worklist all inputs
287 // of instructions in these blocks that are not themselves in these blocks.
288 // Also find the common dominator of the found post dominated blocks, to help filtering
289 // out un-movable uses in step (2).
290 CommonDominator finder(end_block);
291 for (size_t i = 0, e = graph_->GetBlocks().size(); i < e; ++i) {
292 if (post_dominated.IsBitSet(i)) {
293 finder.Update(graph_->GetBlocks()[i]);
294 AddInputs(graph_->GetBlocks()[i], processed_instructions, post_dominated, &worklist);
295 }
296 }
297 HBasicBlock* common_dominator = finder.Get();
298
299 // Step (2): iterate over the worklist to find sinking candidates.
300 while (!worklist.empty()) {
301 HInstruction* instruction = worklist.back();
302 if (processed_instructions.IsBitSet(instruction->GetId())) {
303 // The instruction has already been processed, continue. This happens
304 // when the instruction is the input/user of multiple instructions.
305 worklist.pop_back();
306 continue;
307 }
308 bool all_users_in_post_dominated_blocks = true;
309 bool can_move = true;
310 // Check users of the instruction.
311 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
312 HInstruction* user = use.GetUser();
313 if (!post_dominated.IsBitSet(user->GetBlock()->GetBlockId()) &&
314 !instructions_that_can_move.IsBitSet(user->GetId())) {
315 all_users_in_post_dominated_blocks = false;
316 // If we've already processed this user, or the user cannot be moved, or
317 // is not dominating the post dominated blocks, bail.
318 // TODO(ngeoffray): The domination check is an approximation. We should
319 // instead check if the dominated blocks post dominate the user's block,
320 // but we do not have post dominance information here.
321 if (processed_instructions.IsBitSet(user->GetId()) ||
322 !IsInterestingInstruction(user) ||
323 !user->GetBlock()->Dominates(common_dominator)) {
324 can_move = false;
325 break;
326 }
327 }
328 }
329
330 // Check environment users of the instruction. Some of these users require
331 // the instruction not to move.
332 if (all_users_in_post_dominated_blocks) {
333 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
334 HEnvironment* environment = use.GetUser();
335 HInstruction* user = environment->GetHolder();
336 if (!post_dominated.IsBitSet(user->GetBlock()->GetBlockId())) {
337 if (graph_->IsDebuggable() ||
338 user->IsDeoptimize() ||
339 user->CanThrowIntoCatchBlock() ||
340 (user->IsSuspendCheck() && graph_->IsCompilingOsr())) {
341 can_move = false;
342 break;
343 }
344 }
345 }
346 }
347 if (!can_move) {
348 // Instruction cannot be moved, mark it as processed and remove it from the work
349 // list.
350 processed_instructions.SetBit(instruction->GetId());
351 worklist.pop_back();
352 } else if (all_users_in_post_dominated_blocks) {
353 // Instruction is a candidate for being sunk. Mark it as such, remove it from the
354 // work list, and add its inputs to the work list.
355 instructions_that_can_move.SetBit(instruction->GetId());
356 move_in_order.push_back(instruction);
357 processed_instructions.SetBit(instruction->GetId());
358 worklist.pop_back();
359 AddInputs(instruction, processed_instructions, post_dominated, &worklist);
360 // Drop the environment use not in the list of post-dominated block. This is
361 // to help step (3) of this optimization, when we start moving instructions
362 // closer to their use.
363 for (const HUseListNode<HEnvironment*>& use : instruction->GetEnvUses()) {
364 HEnvironment* environment = use.GetUser();
365 HInstruction* user = environment->GetHolder();
366 if (!post_dominated.IsBitSet(user->GetBlock()->GetBlockId())) {
367 environment->RemoveAsUserOfInput(use.GetIndex());
368 environment->SetRawEnvAt(use.GetIndex(), nullptr);
369 }
370 }
371 } else {
372 // The information we have on the users was not enough to decide whether the
373 // instruction could be moved.
374 // Add the users to the work list, and keep the instruction in the work list
375 // to process it again once all users have been processed.
376 for (const HUseListNode<HInstruction*>& use : instruction->GetUses()) {
377 AddInstruction(use.GetUser(), processed_instructions, post_dominated, &worklist);
378 }
379 }
380 }
381
382 // Make sure we process instructions in dominated order. This is required for heap
383 // stores.
384 std::sort(move_in_order.begin(), move_in_order.end(), [](HInstruction* a, HInstruction* b) {
385 return b->StrictlyDominates(a);
386 });
387
388 // Step (3): Try to move sinking candidates.
389 for (HInstruction* instruction : move_in_order) {
390 HInstruction* position = nullptr;
Igor Murashkin79d8fa72017-04-18 09:37:23 -0700391 if (instruction->IsArraySet()
392 || instruction->IsInstanceFieldSet()
393 || instruction->IsConstructorFence()) {
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000394 if (!instructions_that_can_move.IsBitSet(instruction->InputAt(0)->GetId())) {
395 // A store can trivially move, but it can safely do so only if the heap
396 // location it stores to can also move.
397 // TODO(ngeoffray): Handle allocation/store cycles by pruning these instructions
398 // from the set and all their inputs.
399 continue;
400 }
401 // Find the position of the instruction we're storing into, filtering out this
402 // store and all other stores to that instruction.
403 position = FindIdealPosition(instruction->InputAt(0), post_dominated, /* filter */ true);
404
405 // The position needs to be dominated by the store, in order for the store to move there.
406 if (position == nullptr || !instruction->GetBlock()->Dominates(position->GetBlock())) {
407 continue;
408 }
409 } else {
410 // Find the ideal position within the post dominated blocks.
411 position = FindIdealPosition(instruction, post_dominated);
412 if (position == nullptr) {
413 continue;
414 }
415 }
416 // Bail if we could not find a position in the post dominated blocks (for example,
417 // if there are multiple users whose common dominator is not in the list of
418 // post dominated blocks).
419 if (!post_dominated.IsBitSet(position->GetBlock()->GetBlockId())) {
420 continue;
421 }
Igor Murashkin1e065a52017-08-09 13:20:34 -0700422 MaybeRecordStat(stats_, MethodCompilationStat::kInstructionSunk);
Nicolas Geoffrayb813ca12017-02-16 22:08:29 +0000423 instruction->MoveBefore(position, /* ensure_safety */ false);
424 }
425}
426
427} // namespace art