blob: 6a901d1c04737a5bcb2c368b972896e53fe8c425 [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),
51 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
52 number_of_ssa_values_(0) {
53 block_infos_.SetSize(graph.GetBlocks().Size());
54 }
55
56 void Analyze();
57
58 BitVector* GetLiveInSet(const HBasicBlock& block) const {
59 return &block_infos_.Get(block.GetBlockId())->live_in_;
60 }
61
62 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
63 return &block_infos_.Get(block.GetBlockId())->live_out_;
64 }
65
66 BitVector* GetKillSet(const HBasicBlock& block) const {
67 return &block_infos_.Get(block.GetBlockId())->kill_;
68 }
69
70 private:
71 // Give an SSA number to each instruction that defines a value used by another instruction.
72 void NumberInstructions();
73
74 // Compute live_in, live_out and kill sets.
75 void ComputeSets();
76
77 // Compute the initial live_in, live_out and kill sets, without analyzing
78 // backward branches.
79 void ComputeInitialSets();
80
81 // After computing the initial sets, this method does a fixed point
82 // calculation over the live_in and live_out set to take into account
83 // backwards branches.
84 void ComputeLiveInAndLiveOutSets();
85
86 // Update the live_in set of the block and returns whether it has changed.
87 bool UpdateLiveIn(const HBasicBlock& block);
88
89 // Update the live_out set of the block and returns whether it has changed.
90 bool UpdateLiveOut(const HBasicBlock& block);
91
92 const HGraph& graph_;
93 GrowableArray<BlockInfo*> block_infos_;
94 size_t number_of_ssa_values_;
95
96 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
97};
98
99} // namespace art
100
101#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_