blob: d555df788a115d313723677e6c4a615bda60bda4 [file] [log] [blame]
Chris Lattner87fea852002-05-10 05:41:34 +00001//===- PiNodeInsertion.cpp - Insert Pi nodes into a program ---------------===//
2//
3// PiNodeInsertion - This pass inserts single entry Phi nodes into basic blocks
Misha Brukmancf00c4a2003-10-10 17:57:28 +00004// that are preceded by a conditional branch, where the branch gives
Chris Lattner87fea852002-05-10 05:41:34 +00005// information about the operands of the condition. For example, this C code:
6// if (x == 0) { ... = x + 4;
7// becomes:
8// if (x == 0) {
9// x2 = phi(x); // Node that can hold data flow information about X
10// ... = x2 + 4;
11//
12// Since the direction of the condition branch gives information about X itself
13// (whether or not it is zero), some passes (like value numbering or ABCD) can
14// use the inserted Phi/Pi nodes as a place to attach information, in this case
15// saying that X has a value of 0 in this scope. The power of this analysis
16// information is that "in the scope" translates to "for all uses of x2".
17//
Misha Brukmancf00c4a2003-10-10 17:57:28 +000018// This special form of Phi node is referred to as a Pi node, following the
Chris Lattner87fea852002-05-10 05:41:34 +000019// terminology defined in the "Array Bounds Checks on Demand" paper.
20//
21// As a really trivial example of what the Pi nodes are good for, this pass
22// replaces values compared for equality with direct constants with the constant
23// itself in the branch it's equal to the constant. In the case above, it would
24// change the body to be "... = 0 + 4;" Real value numbering can do much more.
25//
26//===----------------------------------------------------------------------===//
27
28#include "llvm/Transforms/Scalar.h"
29#include "llvm/Analysis/Dominators.h"
30#include "llvm/Pass.h"
31#include "llvm/Function.h"
Chris Lattner87fea852002-05-10 05:41:34 +000032#include "llvm/iTerminators.h"
33#include "llvm/iOperators.h"
34#include "llvm/iPHINode.h"
35#include "llvm/Support/CFG.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000036#include "Support/Statistic.h"
Chris Lattner87fea852002-05-10 05:41:34 +000037
38namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000039 Statistic<> NumInserted("pinodes", "Number of Pi nodes inserted");
40
Chris Lattner87fea852002-05-10 05:41:34 +000041 struct PiNodeInserter : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000042 virtual bool runOnFunction(Function &F);
Chris Lattner87fea852002-05-10 05:41:34 +000043
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000045 AU.setPreservesCFG();
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000046 AU.addRequired<DominatorSet>();
Chris Lattner87fea852002-05-10 05:41:34 +000047 }
48
49 // insertPiNodeFor - Insert a Pi node for V in the successors of BB if our
50 // conditions hold. If Rep is not null, fill in a value of 'Rep' instead of
51 // creating a new Pi node itself because we know that the value is a simple
52 // constant.
53 //
54 bool insertPiNodeFor(Value *V, BasicBlock *BB, Value *Rep = 0);
55 };
Chris Lattnerf6293092002-07-23 18:06:35 +000056
Chris Lattnera6275cc2002-07-26 21:12:46 +000057 RegisterOpt<PiNodeInserter> X("pinodes", "Pi Node Insertion");
Chris Lattner87fea852002-05-10 05:41:34 +000058}
59
60Pass *createPiNodeInsertionPass() { return new PiNodeInserter(); }
61
62
Chris Lattner7e708292002-06-25 16:13:24 +000063bool PiNodeInserter::runOnFunction(Function &F) {
Chris Lattner87fea852002-05-10 05:41:34 +000064 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000065 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
66 TerminatorInst *TI = I->getTerminator();
Chris Lattner87fea852002-05-10 05:41:34 +000067
68 // FIXME: Insert PI nodes for switch statements too
69
70 // Look for conditional branch instructions... that branch on a setcc test
71 if (BranchInst *BI = dyn_cast<BranchInst>(TI))
72 if (BI->isConditional())
73 // TODO: we could in theory support logical operations here too...
74 if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition())) {
75 // Calculate replacement values if this is an obvious constant == or
76 // != comparison...
77 Value *TrueRep = 0, *FalseRep = 0;
78
79 // Make sure the the constant is the second operand if there is one...
Chris Lattner065a6162003-09-10 05:29:43 +000080 // This fits with our canonicalization patterns used elsewhere in the
Chris Lattner87fea852002-05-10 05:41:34 +000081 // compiler, without depending on instcombine running before us.
82 //
83 if (isa<Constant>(SCI->getOperand(0)) &&
84 !isa<Constant>(SCI->getOperand(1))) {
85 SCI->swapOperands();
86 Changed = true;
87 }
88
89 if (isa<Constant>(SCI->getOperand(1))) {
90 if (SCI->getOpcode() == Instruction::SetEQ)
91 TrueRep = SCI->getOperand(1);
92 else if (SCI->getOpcode() == Instruction::SetNE)
93 FalseRep = SCI->getOperand(1);
94 }
95
96 BasicBlock *TB = BI->getSuccessor(0); // True block
97 BasicBlock *FB = BI->getSuccessor(1); // False block
98
99 // Insert the Pi nodes for the first operand to the comparison...
100 Changed |= insertPiNodeFor(SCI->getOperand(0), TB, TrueRep);
101 Changed |= insertPiNodeFor(SCI->getOperand(0), FB, FalseRep);
102
103 // Insert the Pi nodes for the second operand to the comparison...
104 Changed |= insertPiNodeFor(SCI->getOperand(1), TB);
105 Changed |= insertPiNodeFor(SCI->getOperand(1), FB);
106 }
107 }
108
109 return Changed;
110}
111
112
Chris Lattner7e708292002-06-25 16:13:24 +0000113// alreadyHasPiNodeFor - Return true if there is already a Pi node in BB for V.
Chris Lattner87fea852002-05-10 05:41:34 +0000114static bool alreadyHasPiNodeFor(Value *V, BasicBlock *BB) {
115 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
116 if (PHINode *PN = dyn_cast<PHINode>(*I))
117 if (PN->getParent() == BB)
118 return true;
119 return false;
120}
121
122
123// insertPiNodeFor - Insert a Pi node for V in the successors of BB if our
124// conditions hold. If Rep is not null, fill in a value of 'Rep' instead of
125// creating a new Pi node itself because we know that the value is a simple
126// constant.
127//
128bool PiNodeInserter::insertPiNodeFor(Value *V, BasicBlock *Succ, Value *Rep) {
129 // Do not insert Pi nodes for constants!
130 if (isa<Constant>(V)) return false;
131
132 // Check to make sure that there is not already a PI node inserted...
133 if (alreadyHasPiNodeFor(V, Succ) && Rep == 0)
134 return false;
135
136 // Insert Pi nodes only into successors that the conditional branch dominates.
137 // In this simple case, we know that BB dominates a successor as long there
138 // are no other incoming edges to the successor.
139 //
140
141 // Check to make sure that the successor only has a single predecessor...
142 pred_iterator PI = pred_begin(Succ);
143 BasicBlock *Pred = *PI;
144 if (++PI != pred_end(Succ)) return false; // Multiple predecessor? Bail...
145
146 // It seems to be safe to insert the Pi node. Do so now...
147
148 // Create the Pi node...
149 Value *Pi = Rep;
Chris Lattner1d608ab2002-09-10 22:38:47 +0000150 if (Rep == 0) // Insert the Pi node in the successor basic block...
151 Pi = new PHINode(V->getType(), V->getName() + ".pi", Succ->begin());
Chris Lattner87fea852002-05-10 05:41:34 +0000152
153 // Loop over all of the uses of V, replacing ones that the Pi node
154 // dominates with references to the Pi node itself.
155 //
156 DominatorSet &DS = getAnalysis<DominatorSet>();
157 for (unsigned i = 0; i < V->use_size(); ) {
158 if (Instruction *U = dyn_cast<Instruction>(*(V->use_begin()+i)))
159 if (U->getParent()->getParent() == Succ->getParent() &&
160 DS.dominates(Succ, U->getParent())) {
161 // This instruction is dominated by the Pi node, replace reference to V
162 // with a reference to the Pi node.
163 //
164 U->replaceUsesOfWith(V, Pi);
165 continue; // Do not skip the next use...
166 }
167
168 // This use is not dominated by the Pi node, skip it...
169 ++i;
170 }
171
172 // Set up the incoming value for the Pi node... do this after uses have been
173 // replaced, because we don't want the Pi node to refer to itself.
174 //
175 if (Rep == 0)
176 cast<PHINode>(Pi)->addIncoming(V, Pred);
177
Chris Lattner3dec1f22002-05-10 15:38:35 +0000178
179 ++NumInserted;
Chris Lattner87fea852002-05-10 05:41:34 +0000180 return true;
181}
182