blob: 4e813ddf95c7d44593965775d1f8f1e23bdb36b0 [file] [log] [blame]
Nick Lewycky71502942009-07-03 19:28:36 +00001//===------------------- SSI.cpp - Creates SSI Representation -------------===//
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 pass converts a list of variables to the Static Single Information
11// form. This is a program representation described by Scott Ananian in his
12// Master Thesis: "The Static Single Information Form (1999)".
13// We are building an on-demand representation, that is, we do not convert
14// every single variable in the target function to SSI form. Rather, we receive
15// a list of target variables that must be converted. We also do not
16// completely convert a target variable to the SSI format. Instead, we only
17// change the variable in the points where new information can be attached
18// to its live range, that is, at branch points.
19//
20//===----------------------------------------------------------------------===//
21
22#define DEBUG_TYPE "ssi"
23
24#include "llvm/Transforms/Scalar.h"
25#include "llvm/Transforms/Utils/SSI.h"
Nick Lewycky6ca7f412009-07-09 15:33:14 +000026#include "llvm/ADT/Statistic.h"
Nick Lewycky71502942009-07-03 19:28:36 +000027#include "llvm/Analysis/Dominators.h"
28
29using namespace llvm;
30
31static const std::string SSI_PHI = "SSI_phi";
32static const std::string SSI_SIG = "SSI_sigma";
33
Nick Lewycky6ca7f412009-07-09 15:33:14 +000034STATISTIC(NumSigmaInserted, "Number of sigma functions inserted");
35STATISTIC(NumPhiInserted, "Number of phi functions inserted");
36
Nick Lewycky71502942009-07-03 19:28:36 +000037void SSI::getAnalysisUsage(AnalysisUsage &AU) const {
Owen Anderson7aa3c782009-10-04 17:47:39 +000038 AU.addRequiredTransitive<DominanceFrontier>();
39 AU.addRequiredTransitive<DominatorTree>();
40 AU.setPreservesAll();
Nick Lewycky71502942009-07-03 19:28:36 +000041}
42
43bool SSI::runOnFunction(Function &F) {
44 DT_ = &getAnalysis<DominatorTree>();
45 return false;
46}
47
48/// This methods creates the SSI representation for the list of values
49/// received. It will only create SSI representation if a value is used
Owen Anderson216abab2009-10-04 17:52:13 +000050/// to decide a branch. Repeated values are created only once.
Nick Lewycky71502942009-07-03 19:28:36 +000051///
52void SSI::createSSI(SmallVectorImpl<Instruction *> &value) {
53 init(value);
54
Owen Anderson08993ac2009-10-04 18:49:55 +000055 SmallPtrSet<Instruction*, 4> needConstruction;
56 for (SmallVectorImpl<Instruction*>::iterator I = value.begin(),
57 E = value.end(); I != E; ++I)
58 if (created.insert(*I))
59 needConstruction.insert(*I);
60
61 insertSigmaFunctions(needConstruction);
Nick Lewycky71502942009-07-03 19:28:36 +000062
63 // Test if there is a need to transform to SSI
Owen Anderson08993ac2009-10-04 18:49:55 +000064 if (!needConstruction.empty()) {
65 insertPhiFunctions(needConstruction);
66 renameInit(needConstruction);
Nick Lewycky71502942009-07-03 19:28:36 +000067 rename(DT_->getRoot());
68 fixPhis();
69 }
70
71 clean();
72}
73
74/// Insert sigma functions (a sigma function is a phi function with one
75/// operator)
76///
Owen Anderson08993ac2009-10-04 18:49:55 +000077void SSI::insertSigmaFunctions(SmallPtrSet<Instruction*, 4> &value) {
78 for (SmallPtrSet<Instruction*, 4>::iterator I = value.begin(),
79 E = value.end(); I != E; ++I) {
80 for (Value::use_iterator begin = (*I)->use_begin(),
81 end = (*I)->use_end(); begin != end; ++begin) {
Nick Lewycky71502942009-07-03 19:28:36 +000082 // Test if the Use of the Value is in a comparator
Nick Lewyckyfd5249e2009-09-10 07:02:09 +000083 if (CmpInst *CI = dyn_cast<CmpInst>(begin)) {
84 // Iterates through all uses of CmpInst
Owen Anderson08993ac2009-10-04 18:49:55 +000085 for (Value::use_iterator begin_ci = CI->use_begin(),
86 end_ci = CI->use_end(); begin_ci != end_ci; ++begin_ci) {
Nick Lewyckyfd5249e2009-09-10 07:02:09 +000087 // Test if any use of CmpInst is in a Terminator
88 if (TerminatorInst *TI = dyn_cast<TerminatorInst>(begin_ci)) {
Owen Anderson08993ac2009-10-04 18:49:55 +000089 insertSigma(TI, *I);
Nick Lewycky71502942009-07-03 19:28:36 +000090 }
91 }
92 }
93 }
Nick Lewyckyfd5249e2009-09-10 07:02:09 +000094 }
95}
96
97/// Inserts Sigma Functions in every BasicBlock successor to Terminator
98/// Instruction TI. All inserted Sigma Function are related to Instruction I.
99///
Owen Anderson08993ac2009-10-04 18:49:55 +0000100void SSI::insertSigma(TerminatorInst *TI, Instruction *I) {
Nick Lewyckyfd5249e2009-09-10 07:02:09 +0000101 // Basic Block of the Terminator Instruction
102 BasicBlock *BB = TI->getParent();
103 for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
104 // Next Basic Block
105 BasicBlock *BB_next = TI->getSuccessor(i);
106 if (BB_next != BB &&
107 BB_next->getSinglePredecessor() != NULL &&
108 dominateAny(BB_next, I)) {
109 PHINode *PN = PHINode::Create(I->getType(), SSI_SIG, BB_next->begin());
110 PN->addIncoming(I, BB);
Owen Anderson08993ac2009-10-04 18:49:55 +0000111 sigmas[PN] = I;
Nick Lewyckyfd5249e2009-09-10 07:02:09 +0000112 created.insert(PN);
Owen Anderson08993ac2009-10-04 18:49:55 +0000113 defsites[I].push_back(BB_next);
Nick Lewyckyfd5249e2009-09-10 07:02:09 +0000114 ++NumSigmaInserted;
115 }
Nick Lewycky71502942009-07-03 19:28:36 +0000116 }
117}
118
119/// Insert phi functions when necessary
120///
Owen Anderson08993ac2009-10-04 18:49:55 +0000121void SSI::insertPhiFunctions(SmallPtrSet<Instruction*, 4> &value) {
Nick Lewycky71502942009-07-03 19:28:36 +0000122 DominanceFrontier *DF = &getAnalysis<DominanceFrontier>();
Owen Anderson08993ac2009-10-04 18:49:55 +0000123 for (SmallPtrSet<Instruction*, 4>::iterator I = value.begin(),
124 E = value.end(); I != E; ++I) {
Nick Lewycky71502942009-07-03 19:28:36 +0000125 // Test if there were any sigmas for this variable
Owen Anderson08993ac2009-10-04 18:49:55 +0000126 SmallPtrSet<BasicBlock *, 16> BB_visited;
Nick Lewycky71502942009-07-03 19:28:36 +0000127
Owen Anderson08993ac2009-10-04 18:49:55 +0000128 // Insert phi functions if there is any sigma function
129 while (!defsites[*I].empty()) {
Nick Lewycky71502942009-07-03 19:28:36 +0000130
Owen Anderson08993ac2009-10-04 18:49:55 +0000131 BasicBlock *BB = defsites[*I].back();
Nick Lewycky71502942009-07-03 19:28:36 +0000132
Owen Anderson08993ac2009-10-04 18:49:55 +0000133 defsites[*I].pop_back();
134 DominanceFrontier::iterator DF_BB = DF->find(BB);
Nick Lewycky71502942009-07-03 19:28:36 +0000135
Owen Anderson08993ac2009-10-04 18:49:55 +0000136 // The BB is unreachable. Skip it.
137 if (DF_BB == DF->end())
138 continue;
Nick Lewycky71502942009-07-03 19:28:36 +0000139
Owen Anderson08993ac2009-10-04 18:49:55 +0000140 // Iterates through all the dominance frontier of BB
141 for (std::set<BasicBlock *>::iterator DF_BB_begin =
142 DF_BB->second.begin(), DF_BB_end = DF_BB->second.end();
143 DF_BB_begin != DF_BB_end; ++DF_BB_begin) {
144 BasicBlock *BB_dominated = *DF_BB_begin;
Nick Lewycky071d84e2009-08-15 20:12:18 +0000145
Owen Anderson08993ac2009-10-04 18:49:55 +0000146 // Test if has not yet visited this node and if the
147 // original definition dominates this node
148 if (BB_visited.insert(BB_dominated) &&
149 DT_->properlyDominates(value_original[*I], BB_dominated) &&
150 dominateAny(BB_dominated, *I)) {
151 PHINode *PN = PHINode::Create(
152 (*I)->getType(), SSI_PHI, BB_dominated->begin());
153 phis.insert(std::make_pair(PN, *I));
154 created.insert(PN);
Nick Lewycky71502942009-07-03 19:28:36 +0000155
Owen Anderson08993ac2009-10-04 18:49:55 +0000156 defsites[*I].push_back(BB_dominated);
157 ++NumPhiInserted;
Nick Lewycky71502942009-07-03 19:28:36 +0000158 }
159 }
Nick Lewycky71502942009-07-03 19:28:36 +0000160 }
Owen Anderson08993ac2009-10-04 18:49:55 +0000161 BB_visited.clear();
Nick Lewycky71502942009-07-03 19:28:36 +0000162 }
163}
164
165/// Some initialization for the rename part
166///
Owen Anderson08993ac2009-10-04 18:49:55 +0000167void SSI::renameInit(SmallPtrSet<Instruction*, 4> &value) {
168 for (SmallPtrSet<Instruction*, 4>::iterator I = value.begin(),
169 E = value.end(); I != E; ++I)
170 value_stack[*I].push_back(*I);
Nick Lewycky71502942009-07-03 19:28:36 +0000171}
172
173/// Renames all variables in the specified BasicBlock.
174/// Only variables that need to be rename will be.
175///
176void SSI::rename(BasicBlock *BB) {
Owen Anderson08993ac2009-10-04 18:49:55 +0000177 SmallPtrSet<Instruction*, 8> defined;
Nick Lewycky71502942009-07-03 19:28:36 +0000178
179 // Iterate through instructions and make appropriate renaming.
180 // For SSI_PHI (b = PHI()), store b at value_stack as a new
181 // definition of the variable it represents.
182 // For SSI_SIG (b = PHI(a)), substitute a with the current
183 // value of a, present in the value_stack.
184 // Then store bin the value_stack as the new definition of a.
185 // For all other instructions (b = OP(a, c, d, ...)), we need to substitute
186 // all operands with its current value, present in value_stack.
187 for (BasicBlock::iterator begin = BB->begin(), end = BB->end();
188 begin != end; ++begin) {
189 Instruction *I = begin;
190 if (PHINode *PN = dyn_cast<PHINode>(I)) { // Treat PHI functions
Owen Anderson08993ac2009-10-04 18:49:55 +0000191 Instruction* position;
Nick Lewycky71502942009-07-03 19:28:36 +0000192
193 // Treat SSI_PHI
Owen Anderson08993ac2009-10-04 18:49:55 +0000194 if ((position = getPositionPhi(PN))) {
Nick Lewycky71502942009-07-03 19:28:36 +0000195 value_stack[position].push_back(PN);
Owen Anderson08993ac2009-10-04 18:49:55 +0000196 defined.insert(position);
Nick Lewycky71502942009-07-03 19:28:36 +0000197 // Treat SSI_SIG
Owen Anderson08993ac2009-10-04 18:49:55 +0000198 } else if ((position = getPositionSigma(PN))) {
Nick Lewycky71502942009-07-03 19:28:36 +0000199 substituteUse(I);
200 value_stack[position].push_back(PN);
Owen Anderson08993ac2009-10-04 18:49:55 +0000201 defined.insert(position);
Nick Lewycky71502942009-07-03 19:28:36 +0000202 }
203
204 // Treat all other PHI functions
205 else {
206 substituteUse(I);
207 }
208 }
209
210 // Treat all other functions
211 else {
212 substituteUse(I);
213 }
214 }
215
216 // This loop iterates in all BasicBlocks that are successors of the current
217 // BasicBlock. For each SSI_PHI instruction found, insert an operand.
218 // This operand is the current operand in value_stack for the variable
219 // in "position". And the BasicBlock this operand represents is the current
220 // BasicBlock.
221 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; ++SI) {
222 BasicBlock *BB_succ = *SI;
223
224 for (BasicBlock::iterator begin = BB_succ->begin(),
225 notPhi = BB_succ->getFirstNonPHI(); begin != *notPhi; ++begin) {
226 Instruction *I = begin;
Nick Lewycky08368ce2009-08-19 07:16:57 +0000227 PHINode *PN = dyn_cast<PHINode>(I);
Owen Anderson08993ac2009-10-04 18:49:55 +0000228 Instruction* position;
229 if (PN && ((position = getPositionPhi(PN)))) {
Nick Lewycky71502942009-07-03 19:28:36 +0000230 PN->addIncoming(value_stack[position].back(), BB);
231 }
232 }
233 }
234
235 // This loop calls rename on all children from this block. This time children
236 // refers to a successor block in the dominance tree.
237 DomTreeNode *DTN = DT_->getNode(BB);
238 for (DomTreeNode::iterator begin = DTN->begin(), end = DTN->end();
239 begin != end; ++begin) {
240 DomTreeNodeBase<BasicBlock> *DTN_children = *begin;
241 BasicBlock *BB_children = DTN_children->getBlock();
242 rename(BB_children);
243 }
244
245 // Now we remove all inserted definitions of a variable from the top of
246 // the stack leaving the previous one as the top.
Owen Anderson08993ac2009-10-04 18:49:55 +0000247 for (SmallPtrSet<Instruction*, 8>::iterator DI = defined.begin(),
248 DE = defined.end(); DI != DE; ++DI)
249 value_stack[*DI].pop_back();
Nick Lewycky71502942009-07-03 19:28:36 +0000250}
251
252/// Substitute any use in this instruction for the last definition of
253/// the variable
254///
255void SSI::substituteUse(Instruction *I) {
256 for (unsigned i = 0, e = I->getNumOperands(); i < e; ++i) {
257 Value *operand = I->getOperand(i);
Owen Anderson08993ac2009-10-04 18:49:55 +0000258 for (DenseMap<Instruction*, SmallVector<Instruction*, 1> >::iterator
259 VI = value_stack.begin(), VE = value_stack.end(); VI != VE; ++VI) {
260 if (operand == VI->second.front() &&
261 I != VI->second.back()) {
Nick Lewycky71502942009-07-03 19:28:36 +0000262 PHINode *PN_I = dyn_cast<PHINode>(I);
Owen Anderson08993ac2009-10-04 18:49:55 +0000263 PHINode *PN_vs = dyn_cast<PHINode>(VI->second.back());
Nick Lewycky71502942009-07-03 19:28:36 +0000264
265 // If a phi created in a BasicBlock is used as an operand of another
266 // created in the same BasicBlock, this step marks this second phi,
267 // to fix this issue later. It cannot be fixed now, because the
268 // operands of the first phi are not final yet.
269 if (PN_I && PN_vs &&
Owen Anderson08993ac2009-10-04 18:49:55 +0000270 VI->second.back()->getParent() == I->getParent()) {
Nick Lewycky71502942009-07-03 19:28:36 +0000271
272 phisToFix.insert(PN_I);
273 }
274
Owen Anderson08993ac2009-10-04 18:49:55 +0000275 I->setOperand(i, VI->second.back());
Nick Lewycky71502942009-07-03 19:28:36 +0000276 break;
277 }
278 }
279 }
280}
281
282/// Test if the BasicBlock BB dominates any use or definition of value.
Nick Lewycky89f43a52009-07-09 15:59:27 +0000283/// If it dominates a phi instruction that is on the same BasicBlock,
284/// that does not count.
Nick Lewycky71502942009-07-03 19:28:36 +0000285///
286bool SSI::dominateAny(BasicBlock *BB, Instruction *value) {
287 for (Value::use_iterator begin = value->use_begin(),
288 end = value->use_end(); begin != end; ++begin) {
289 Instruction *I = cast<Instruction>(*begin);
290 BasicBlock *BB_father = I->getParent();
Nick Lewycky89f43a52009-07-09 15:59:27 +0000291 if (BB == BB_father && isa<PHINode>(I))
292 continue;
Nick Lewycky71502942009-07-03 19:28:36 +0000293 if (DT_->dominates(BB, BB_father)) {
294 return true;
295 }
296 }
297 return false;
298}
299
300/// When there is a phi node that is created in a BasicBlock and it is used
301/// as an operand of another phi function used in the same BasicBlock,
302/// LLVM looks this as an error. So on the second phi, the first phi is called
303/// P and the BasicBlock it incomes is B. This P will be replaced by the value
Nick Lewycky08368ce2009-08-19 07:16:57 +0000304/// it has for BasicBlock B. It also includes undef values for predecessors
305/// that were not included in the phi.
Nick Lewycky71502942009-07-03 19:28:36 +0000306///
307void SSI::fixPhis() {
308 for (SmallPtrSet<PHINode *, 1>::iterator begin = phisToFix.begin(),
309 end = phisToFix.end(); begin != end; ++begin) {
310 PHINode *PN = *begin;
311 for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i) {
Nick Lewycky08368ce2009-08-19 07:16:57 +0000312 PHINode *PN_father = dyn_cast<PHINode>(PN->getIncomingValue(i));
313 if (PN_father && PN->getParent() == PN_father->getParent() &&
Nick Lewycky3417e8f2009-08-19 06:24:33 +0000314 !DT_->dominates(PN->getParent(), PN->getIncomingBlock(i))) {
Nick Lewycky71502942009-07-03 19:28:36 +0000315 BasicBlock *BB = PN->getIncomingBlock(i);
316 int pos = PN_father->getBasicBlockIndex(BB);
317 PN->setIncomingValue(i, PN_father->getIncomingValue(pos));
318 }
319 }
320 }
Nick Lewycky08368ce2009-08-19 07:16:57 +0000321
Owen Anderson08993ac2009-10-04 18:49:55 +0000322 for (DenseMapIterator<PHINode *, Instruction*> begin = phis.begin(),
Nick Lewycky08368ce2009-08-19 07:16:57 +0000323 end = phis.end(); begin != end; ++begin) {
324 PHINode *PN = begin->first;
325 BasicBlock *BB = PN->getParent();
326 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
327 SmallVector<BasicBlock*, 8> Preds(PI, PE);
328 for (unsigned size = Preds.size();
329 PI != PE && PN->getNumIncomingValues() != size; ++PI) {
330 bool found = false;
331 for (unsigned i = 0, pn_end = PN->getNumIncomingValues();
332 i < pn_end; ++i) {
333 if (PN->getIncomingBlock(i) == *PI) {
334 found = true;
335 break;
336 }
337 }
338 if (!found) {
339 PN->addIncoming(UndefValue::get(PN->getType()), *PI);
340 }
341 }
342 }
Nick Lewycky71502942009-07-03 19:28:36 +0000343}
344
345/// Return which variable (position on the vector of variables) this phi
346/// represents on the phis list.
347///
Owen Anderson08993ac2009-10-04 18:49:55 +0000348Instruction* SSI::getPositionPhi(PHINode *PN) {
349 DenseMap<PHINode *, Instruction*>::iterator val = phis.find(PN);
Nick Lewycky71502942009-07-03 19:28:36 +0000350 if (val == phis.end())
Owen Anderson08993ac2009-10-04 18:49:55 +0000351 return 0;
Nick Lewycky71502942009-07-03 19:28:36 +0000352 else
353 return val->second;
354}
355
356/// Return which variable (position on the vector of variables) this phi
357/// represents on the sigmas list.
358///
Owen Anderson08993ac2009-10-04 18:49:55 +0000359Instruction* SSI::getPositionSigma(PHINode *PN) {
360 DenseMap<PHINode *, Instruction*>::iterator val = sigmas.find(PN);
Nick Lewycky71502942009-07-03 19:28:36 +0000361 if (val == sigmas.end())
Owen Anderson08993ac2009-10-04 18:49:55 +0000362 return 0;
Nick Lewycky71502942009-07-03 19:28:36 +0000363 else
364 return val->second;
365}
366
Nick Lewycky71502942009-07-03 19:28:36 +0000367/// Initializes
368///
369void SSI::init(SmallVectorImpl<Instruction *> &value) {
Owen Anderson08993ac2009-10-04 18:49:55 +0000370 for (SmallVectorImpl<Instruction *>::iterator I = value.begin(),
371 E = value.end(); I != E; ++I) {
372 value_original[*I] = (*I)->getParent();
373 defsites[*I].push_back((*I)->getParent());
Nick Lewycky71502942009-07-03 19:28:36 +0000374 }
375}
376
377/// Clean all used resources in this creation of SSI
378///
379void SSI::clean() {
Nick Lewycky71502942009-07-03 19:28:36 +0000380 phis.clear();
381 sigmas.clear();
382 phisToFix.clear();
383
384 defsites.clear();
385 value_stack.clear();
386 value_original.clear();
Nick Lewycky71502942009-07-03 19:28:36 +0000387}
388
389/// createSSIPass - The public interface to this file...
390///
391FunctionPass *llvm::createSSIPass() { return new SSI(); }
392
393char SSI::ID = 0;
394static RegisterPass<SSI> X("ssi", "Static Single Information Construction");
395
Nick Lewycky6ca7f412009-07-09 15:33:14 +0000396/// SSIEverything - A pass that runs createSSI on every non-void variable,
397/// intended for debugging.
398namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000399 struct SSIEverything : public FunctionPass {
Nick Lewycky6ca7f412009-07-09 15:33:14 +0000400 static char ID; // Pass identification, replacement for typeid
Dan Gohman1b2d0b82009-08-11 15:15:10 +0000401 SSIEverything() : FunctionPass(&ID) {}
Nick Lewycky6ca7f412009-07-09 15:33:14 +0000402
403 bool runOnFunction(Function &F);
404
405 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
406 AU.addRequired<SSI>();
407 }
408 };
409}
410
411bool SSIEverything::runOnFunction(Function &F) {
412 SmallVector<Instruction *, 16> Insts;
413 SSI &ssi = getAnalysis<SSI>();
414
415 if (F.isDeclaration() || F.isIntrinsic()) return false;
416
417 for (Function::iterator B = F.begin(), BE = F.end(); B != BE; ++B)
418 for (BasicBlock::iterator I = B->begin(), E = B->end(); I != E; ++I)
Benjamin Kramerf0127052010-01-05 13:12:22 +0000419 if (!I->getType()->isVoidTy())
Nick Lewycky6ca7f412009-07-09 15:33:14 +0000420 Insts.push_back(I);
421
422 ssi.createSSI(Insts);
423 return true;
424}
425
426/// createSSIEverythingPass - The public interface to this file...
427///
428FunctionPass *llvm::createSSIEverythingPass() { return new SSIEverything(); }
429
430char SSIEverything::ID = 0;
431static RegisterPass<SSIEverything>
432Y("ssi-everything", "Static Single Information Construction");