Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "side_effects_analysis.h" |
| 18 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 19 | namespace art { |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 20 | |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 21 | bool SideEffectsAnalysis::Run() { |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 22 | // Inlining might have created more blocks, so we need to increase the size |
| 23 | // if needed. |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 24 | block_effects_.resize(graph_->GetBlocks().size()); |
| 25 | loop_effects_.resize(graph_->GetBlocks().size()); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 26 | |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 27 | // In DEBUG mode, ensure side effects are properly initialized to empty. |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 28 | if (kIsDebugBuild) { |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 29 | for (HBasicBlock* block : graph_->GetReversePostOrder()) { |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 30 | SideEffects effects = GetBlockEffects(block); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 31 | DCHECK(effects.DoesNothing()); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 32 | if (block->IsLoopHeader()) { |
| 33 | effects = GetLoopEffects(block); |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 34 | DCHECK(effects.DoesNothing()); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Do a post order visit to ensure we visit a loop header after its loop body. |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 40 | for (HBasicBlock* block : graph_->GetPostOrder()) { |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 41 | SideEffects effects = SideEffects::None(); |
| 42 | // Update `effects` with the side effects of all instructions in this block. |
| 43 | for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done(); |
| 44 | inst_it.Advance()) { |
| 45 | HInstruction* instruction = inst_it.Current(); |
| 46 | effects = effects.Union(instruction->GetSideEffects()); |
Alexandre Rames | 78e3ef6 | 2015-08-12 13:43:29 +0100 | [diff] [blame] | 47 | // If all side effects are represented, scanning further will not add any |
| 48 | // more information to side-effects of this block. |
Aart Bik | 854a02b | 2015-07-14 16:07:00 -0700 | [diff] [blame] | 49 | if (effects.DoesAll()) { |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 54 | block_effects_[block->GetBlockId()] = effects; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 55 | |
| 56 | if (block->IsLoopHeader()) { |
| 57 | // The side effects of the loop header are part of the loop. |
| 58 | UpdateLoopEffects(block->GetLoopInformation(), effects); |
| 59 | HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader(); |
| 60 | if (pre_header->IsInLoop()) { |
| 61 | // Update the side effects of the outer loop with the side effects of the inner loop. |
| 62 | // Note that this works because we know all the blocks of the inner loop are visited |
| 63 | // before the loop header of the outer loop. |
| 64 | UpdateLoopEffects(pre_header->GetLoopInformation(), GetLoopEffects(block)); |
| 65 | } |
| 66 | } else if (block->IsInLoop()) { |
| 67 | // Update the side effects of the loop with the side effects of this block. |
| 68 | UpdateLoopEffects(block->GetLoopInformation(), effects); |
| 69 | } |
| 70 | } |
| 71 | has_run_ = true; |
Aart Bik | 2477320 | 2018-04-26 10:28:51 -0700 | [diff] [blame] | 72 | return true; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | SideEffects SideEffectsAnalysis::GetLoopEffects(HBasicBlock* block) const { |
| 76 | DCHECK(block->IsLoopHeader()); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 77 | return loop_effects_[block->GetBlockId()]; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | SideEffects SideEffectsAnalysis::GetBlockEffects(HBasicBlock* block) const { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 81 | return block_effects_[block->GetBlockId()]; |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void SideEffectsAnalysis::UpdateLoopEffects(HLoopInformation* info, SideEffects effects) { |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 85 | uint32_t id = info->GetHeader()->GetBlockId(); |
Vladimir Marko | 2aaa4b5 | 2015-09-17 17:03:26 +0100 | [diff] [blame] | 86 | loop_effects_[id] = loop_effects_[id].Union(effects); |
Nicolas Geoffray | 827eedb | 2015-01-26 15:18:36 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } // namespace art |