blob: b71ac0de661626bc04dcfc5d0f6cf4ad53811ff8 [file] [log] [blame]
Dan Gohman3751f562009-03-19 17:29:04 +00001//===- LiveValues.cpp - Liveness information for LLVM IR Values. ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the implementation for the LLVM IR Value liveness
11// analysis pass.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/LiveValues.h"
16#include "llvm/Analysis/Dominators.h"
17#include "llvm/Analysis/LoopInfo.h"
18using namespace llvm;
19
Chris Lattner68cf6042009-11-11 00:21:21 +000020namespace llvm {
21 FunctionPass *createLiveValuesPass() { return new LiveValues(); }
22}
Dan Gohman3751f562009-03-19 17:29:04 +000023
24char LiveValues::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000025INITIALIZE_PASS_BEGIN(LiveValues, "live-values",
26 "Value Liveness Analysis", false, true)
27INITIALIZE_PASS_DEPENDENCY(DominatorTree)
28INITIALIZE_PASS_DEPENDENCY(LoopInfo)
29INITIALIZE_PASS_END(LiveValues, "live-values",
Owen Andersonce665bd2010-10-07 22:25:06 +000030 "Value Liveness Analysis", false, true)
Dan Gohman3751f562009-03-19 17:29:04 +000031
Owen Anderson90c579d2010-08-06 18:33:48 +000032LiveValues::LiveValues() : FunctionPass(ID) {}
Dan Gohman3751f562009-03-19 17:29:04 +000033
34void LiveValues::getAnalysisUsage(AnalysisUsage &AU) const {
35 AU.addRequired<DominatorTree>();
36 AU.addRequired<LoopInfo>();
37 AU.setPreservesAll();
38}
39
40bool LiveValues::runOnFunction(Function &F) {
41 DT = &getAnalysis<DominatorTree>();
42 LI = &getAnalysis<LoopInfo>();
43
44 // This pass' values are computed lazily, so there's nothing to do here.
45
46 return false;
47}
48
49void LiveValues::releaseMemory() {
50 Memos.clear();
51}
52
53/// isUsedInBlock - Test if the given value is used in the given block.
54///
55bool LiveValues::isUsedInBlock(const Value *V, const BasicBlock *BB) {
56 Memo &M = getMemo(V);
57 return M.Used.count(BB);
58}
59
60/// isLiveThroughBlock - Test if the given value is known to be
61/// live-through the given block, meaning that the block is properly
62/// dominated by the value's definition, and there exists a block
63/// reachable from it that contains a use. This uses a conservative
64/// approximation that errs on the side of returning false.
65///
66bool LiveValues::isLiveThroughBlock(const Value *V,
67 const BasicBlock *BB) {
68 Memo &M = getMemo(V);
69 return M.LiveThrough.count(BB);
70}
71
72/// isKilledInBlock - Test if the given value is known to be killed in
73/// the given block, meaning that the block contains a use of the value,
74/// and no blocks reachable from the block contain a use. This uses a
75/// conservative approximation that errs on the side of returning false.
76///
77bool LiveValues::isKilledInBlock(const Value *V, const BasicBlock *BB) {
78 Memo &M = getMemo(V);
79 return M.Killed.count(BB);
80}
81
82/// getMemo - Retrieve an existing Memo for the given value if one
83/// is available, otherwise compute a new one.
84///
85LiveValues::Memo &LiveValues::getMemo(const Value *V) {
86 DenseMap<const Value *, Memo>::iterator I = Memos.find(V);
87 if (I != Memos.end())
88 return I->second;
89 return compute(V);
90}
91
92/// getImmediateDominator - A handy utility for the specific DominatorTree
93/// query that we need here.
94///
95static const BasicBlock *getImmediateDominator(const BasicBlock *BB,
96 const DominatorTree *DT) {
97 DomTreeNode *Node = DT->getNode(const_cast<BasicBlock *>(BB))->getIDom();
98 return Node ? Node->getBlock() : 0;
99}
100
101/// compute - Compute a new Memo for the given value.
102///
103LiveValues::Memo &LiveValues::compute(const Value *V) {
104 Memo &M = Memos[V];
105
106 // Determine the block containing the definition.
107 const BasicBlock *DefBB;
108 // Instructions define values with meaningful live ranges.
109 if (const Instruction *I = dyn_cast<Instruction>(V))
110 DefBB = I->getParent();
111 // Arguments can be analyzed as values defined in the entry block.
112 else if (const Argument *A = dyn_cast<Argument>(V))
113 DefBB = &A->getParent()->getEntryBlock();
114 // Constants and other things aren't meaningful here, so just
115 // return having computed an empty Memo so that we don't come
116 // here again. The assumption here is that client code won't
117 // be asking about such values very often.
118 else
119 return M;
120
121 // Determine if the value is defined inside a loop. This is used
122 // to track whether the value is ever used outside the loop, so
123 // it'll be set to null if the value is either not defined in a
124 // loop or used outside the loop in which it is defined.
125 const Loop *L = LI->getLoopFor(DefBB);
126
127 // Track whether the value is used anywhere outside of the block
128 // in which it is defined.
129 bool LiveOutOfDefBB = false;
130
131 // Examine each use of the value.
Gabor Greif60ad7812010-03-25 23:06:16 +0000132 for (Value::const_use_iterator I = V->use_begin(), E = V->use_end();
Dan Gohman3751f562009-03-19 17:29:04 +0000133 I != E; ++I) {
134 const User *U = *I;
135 const BasicBlock *UseBB = cast<Instruction>(U)->getParent();
136
137 // Note the block in which this use occurs.
138 M.Used.insert(UseBB);
139
Dan Gohman654c98c2009-03-20 01:28:21 +0000140 // If the use block doesn't have successors, the value can be
141 // considered killed.
142 if (succ_begin(UseBB) == succ_end(UseBB))
143 M.Killed.insert(UseBB);
144
Dan Gohman3751f562009-03-19 17:29:04 +0000145 // Observe whether the value is used outside of the loop in which
146 // it is defined. Switch to an enclosing loop if necessary.
147 for (; L; L = L->getParentLoop())
148 if (L->contains(UseBB))
149 break;
150
Dan Gohmana9d71e12009-03-23 15:49:37 +0000151 // Search for live-through blocks.
152 const BasicBlock *BB;
153 if (const PHINode *PHI = dyn_cast<PHINode>(U)) {
154 // For PHI nodes, start the search at the incoming block paired with the
155 // incoming value, which must be dominated by the definition.
156 unsigned Num = PHI->getIncomingValueNumForOperand(I.getOperandNo());
157 BB = PHI->getIncomingBlock(Num);
Dan Gohman3751f562009-03-19 17:29:04 +0000158
Dan Gohmana9d71e12009-03-23 15:49:37 +0000159 // A PHI-node use means the value is live-out of it's defining block
160 // even if that block also contains the only use.
161 LiveOutOfDefBB = true;
162 } else {
163 // Otherwise just start the search at the use.
164 BB = UseBB;
165
166 // Note if the use is outside the defining block.
167 LiveOutOfDefBB |= UseBB != DefBB;
168 }
169
170 // Climb the immediate dominator tree from the use to the definition
Dan Gohman18a69c52009-05-31 16:18:57 +0000171 // and mark all intermediate blocks as live-through.
Dan Gohmana9d71e12009-03-23 15:49:37 +0000172 for (; BB != DefBB; BB = getImmediateDominator(BB, DT)) {
173 if (BB != UseBB && !M.LiveThrough.insert(BB))
174 break;
Dan Gohman3751f562009-03-19 17:29:04 +0000175 }
176 }
177
178 // If the value is defined inside a loop and is not live outside
179 // the loop, then each exit block of the loop in which the value
180 // is used is a kill block.
181 if (L) {
182 SmallVector<BasicBlock *, 4> ExitingBlocks;
183 L->getExitingBlocks(ExitingBlocks);
184 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
185 const BasicBlock *ExitingBlock = ExitingBlocks[i];
186 if (M.Used.count(ExitingBlock))
187 M.Killed.insert(ExitingBlock);
188 }
189 }
190
Dan Gohmanf451cb82010-02-10 16:03:48 +0000191 // If the value was never used outside the block in which it was
Dan Gohman3751f562009-03-19 17:29:04 +0000192 // defined, it's killed in that block.
193 if (!LiveOutOfDefBB)
194 M.Killed.insert(DefBB);
195
196 return M;
197}