blob: b8695ba23cb24c354afca0727df06a2b5737496f [file] [log] [blame]
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001/*
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#ifndef ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_
18#define ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_
19
20#include "nodes.h"
21
22namespace art {
23
24class BlockInfo : public ArenaObject {
25 public:
26 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
27 : block_(block),
28 live_in_(allocator, number_of_ssa_values, false),
29 live_out_(allocator, number_of_ssa_values, false),
30 kill_(allocator, number_of_ssa_values, false) {
31 live_in_.ClearAllBits();
32 live_out_.ClearAllBits();
33 kill_.ClearAllBits();
34 }
35
36 private:
37 const HBasicBlock& block_;
38 ArenaBitVector live_in_;
39 ArenaBitVector live_out_;
40 ArenaBitVector kill_;
41
42 friend class SsaLivenessAnalysis;
43
44 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
45};
46
47class SsaLivenessAnalysis : public ValueObject {
48 public:
49 explicit SsaLivenessAnalysis(const HGraph& graph)
50 : graph_(graph),
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010051 linear_post_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +010052 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
53 number_of_ssa_values_(0) {
54 block_infos_.SetSize(graph.GetBlocks().Size());
55 }
56
57 void Analyze();
58
59 BitVector* GetLiveInSet(const HBasicBlock& block) const {
60 return &block_infos_.Get(block.GetBlockId())->live_in_;
61 }
62
63 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
64 return &block_infos_.Get(block.GetBlockId())->live_out_;
65 }
66
67 BitVector* GetKillSet(const HBasicBlock& block) const {
68 return &block_infos_.Get(block.GetBlockId())->kill_;
69 }
70
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010071 const GrowableArray<HBasicBlock*>& GetLinearPostOrder() const {
72 return linear_post_order_;
73 }
74
Nicolas Geoffray804d0932014-05-02 08:46:00 +010075 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010076 // Linearize the graph so that:
77 // (1): a block is always after its dominator,
78 // (2): blocks of loops are contiguous.
79 // This creates a natural and efficient ordering when visualizing live ranges.
80 void LinearizeGraph();
81
Nicolas Geoffray804d0932014-05-02 08:46:00 +010082 // Give an SSA number to each instruction that defines a value used by another instruction.
83 void NumberInstructions();
84
85 // Compute live_in, live_out and kill sets.
86 void ComputeSets();
87
88 // Compute the initial live_in, live_out and kill sets, without analyzing
89 // backward branches.
90 void ComputeInitialSets();
91
92 // After computing the initial sets, this method does a fixed point
93 // calculation over the live_in and live_out set to take into account
94 // backwards branches.
95 void ComputeLiveInAndLiveOutSets();
96
97 // Update the live_in set of the block and returns whether it has changed.
98 bool UpdateLiveIn(const HBasicBlock& block);
99
100 // Update the live_out set of the block and returns whether it has changed.
101 bool UpdateLiveOut(const HBasicBlock& block);
102
103 const HGraph& graph_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100104 GrowableArray<HBasicBlock*> linear_post_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100105 GrowableArray<BlockInfo*> block_infos_;
106 size_t number_of_ssa_values_;
107
108 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
109};
110
111} // namespace art
112
113#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_