blob: d8fd554de44efdb7248d859770b9cb76ce48306a [file] [log] [blame]
Chris Lattner87fea852002-05-10 05:41:34 +00001//===- PiNodeInsertion.cpp - Insert Pi nodes into a program ---------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner87fea852002-05-10 05:41:34 +00009//
10// PiNodeInsertion - This pass inserts single entry Phi nodes into basic blocks
Misha Brukmancf00c4a2003-10-10 17:57:28 +000011// that are preceded by a conditional branch, where the branch gives
Chris Lattner87fea852002-05-10 05:41:34 +000012// information about the operands of the condition. For example, this C code:
13// if (x == 0) { ... = x + 4;
14// becomes:
15// if (x == 0) {
16// x2 = phi(x); // Node that can hold data flow information about X
17// ... = x2 + 4;
18//
19// Since the direction of the condition branch gives information about X itself
20// (whether or not it is zero), some passes (like value numbering or ABCD) can
21// use the inserted Phi/Pi nodes as a place to attach information, in this case
22// saying that X has a value of 0 in this scope. The power of this analysis
23// information is that "in the scope" translates to "for all uses of x2".
24//
Misha Brukmancf00c4a2003-10-10 17:57:28 +000025// This special form of Phi node is referred to as a Pi node, following the
Chris Lattner87fea852002-05-10 05:41:34 +000026// terminology defined in the "Array Bounds Checks on Demand" paper.
27//
28// As a really trivial example of what the Pi nodes are good for, this pass
29// replaces values compared for equality with direct constants with the constant
30// itself in the branch it's equal to the constant. In the case above, it would
31// change the body to be "... = 0 + 4;" Real value numbering can do much more.
32//
33//===----------------------------------------------------------------------===//
34
35#include "llvm/Transforms/Scalar.h"
36#include "llvm/Analysis/Dominators.h"
37#include "llvm/Pass.h"
38#include "llvm/Function.h"
Chris Lattner87fea852002-05-10 05:41:34 +000039#include "llvm/iTerminators.h"
40#include "llvm/iOperators.h"
41#include "llvm/iPHINode.h"
42#include "llvm/Support/CFG.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000043#include "Support/Statistic.h"
Chris Lattnerd7456022004-01-09 06:02:20 +000044using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000045
Chris Lattner87fea852002-05-10 05:41:34 +000046namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000047 Statistic<> NumInserted("pinodes", "Number of Pi nodes inserted");
48
Chris Lattner87fea852002-05-10 05:41:34 +000049 struct PiNodeInserter : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000050 virtual bool runOnFunction(Function &F);
Chris Lattner87fea852002-05-10 05:41:34 +000051
52 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000053 AU.setPreservesCFG();
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000054 AU.addRequired<DominatorSet>();
Chris Lattner87fea852002-05-10 05:41:34 +000055 }
56
57 // insertPiNodeFor - Insert a Pi node for V in the successors of BB if our
58 // conditions hold. If Rep is not null, fill in a value of 'Rep' instead of
59 // creating a new Pi node itself because we know that the value is a simple
60 // constant.
61 //
62 bool insertPiNodeFor(Value *V, BasicBlock *BB, Value *Rep = 0);
63 };
Chris Lattnerf6293092002-07-23 18:06:35 +000064
Chris Lattnera6275cc2002-07-26 21:12:46 +000065 RegisterOpt<PiNodeInserter> X("pinodes", "Pi Node Insertion");
Chris Lattner87fea852002-05-10 05:41:34 +000066}
67
Chris Lattnerd7456022004-01-09 06:02:20 +000068Pass *llvm::createPiNodeInsertionPass() { return new PiNodeInserter(); }
Chris Lattner87fea852002-05-10 05:41:34 +000069
70
Chris Lattner7e708292002-06-25 16:13:24 +000071bool PiNodeInserter::runOnFunction(Function &F) {
Chris Lattner87fea852002-05-10 05:41:34 +000072 bool Changed = false;
Chris Lattner7e708292002-06-25 16:13:24 +000073 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
74 TerminatorInst *TI = I->getTerminator();
Chris Lattner87fea852002-05-10 05:41:34 +000075
76 // FIXME: Insert PI nodes for switch statements too
77
78 // Look for conditional branch instructions... that branch on a setcc test
79 if (BranchInst *BI = dyn_cast<BranchInst>(TI))
80 if (BI->isConditional())
81 // TODO: we could in theory support logical operations here too...
82 if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition())) {
83 // Calculate replacement values if this is an obvious constant == or
84 // != comparison...
85 Value *TrueRep = 0, *FalseRep = 0;
86
87 // Make sure the the constant is the second operand if there is one...
Chris Lattner065a6162003-09-10 05:29:43 +000088 // This fits with our canonicalization patterns used elsewhere in the
Chris Lattner87fea852002-05-10 05:41:34 +000089 // compiler, without depending on instcombine running before us.
90 //
91 if (isa<Constant>(SCI->getOperand(0)) &&
92 !isa<Constant>(SCI->getOperand(1))) {
93 SCI->swapOperands();
94 Changed = true;
95 }
96
97 if (isa<Constant>(SCI->getOperand(1))) {
98 if (SCI->getOpcode() == Instruction::SetEQ)
99 TrueRep = SCI->getOperand(1);
100 else if (SCI->getOpcode() == Instruction::SetNE)
101 FalseRep = SCI->getOperand(1);
102 }
103
104 BasicBlock *TB = BI->getSuccessor(0); // True block
105 BasicBlock *FB = BI->getSuccessor(1); // False block
106
107 // Insert the Pi nodes for the first operand to the comparison...
108 Changed |= insertPiNodeFor(SCI->getOperand(0), TB, TrueRep);
109 Changed |= insertPiNodeFor(SCI->getOperand(0), FB, FalseRep);
110
111 // Insert the Pi nodes for the second operand to the comparison...
112 Changed |= insertPiNodeFor(SCI->getOperand(1), TB);
113 Changed |= insertPiNodeFor(SCI->getOperand(1), FB);
114 }
115 }
116
117 return Changed;
118}
119
120
Chris Lattner7e708292002-06-25 16:13:24 +0000121// alreadyHasPiNodeFor - Return true if there is already a Pi node in BB for V.
Chris Lattner87fea852002-05-10 05:41:34 +0000122static bool alreadyHasPiNodeFor(Value *V, BasicBlock *BB) {
123 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
124 if (PHINode *PN = dyn_cast<PHINode>(*I))
125 if (PN->getParent() == BB)
126 return true;
127 return false;
128}
129
130
131// insertPiNodeFor - Insert a Pi node for V in the successors of BB if our
132// conditions hold. If Rep is not null, fill in a value of 'Rep' instead of
133// creating a new Pi node itself because we know that the value is a simple
134// constant.
135//
136bool PiNodeInserter::insertPiNodeFor(Value *V, BasicBlock *Succ, Value *Rep) {
137 // Do not insert Pi nodes for constants!
138 if (isa<Constant>(V)) return false;
139
140 // Check to make sure that there is not already a PI node inserted...
141 if (alreadyHasPiNodeFor(V, Succ) && Rep == 0)
142 return false;
143
144 // Insert Pi nodes only into successors that the conditional branch dominates.
145 // In this simple case, we know that BB dominates a successor as long there
146 // are no other incoming edges to the successor.
147 //
148
149 // Check to make sure that the successor only has a single predecessor...
150 pred_iterator PI = pred_begin(Succ);
151 BasicBlock *Pred = *PI;
152 if (++PI != pred_end(Succ)) return false; // Multiple predecessor? Bail...
153
154 // It seems to be safe to insert the Pi node. Do so now...
155
156 // Create the Pi node...
157 Value *Pi = Rep;
Chris Lattner1d608ab2002-09-10 22:38:47 +0000158 if (Rep == 0) // Insert the Pi node in the successor basic block...
159 Pi = new PHINode(V->getType(), V->getName() + ".pi", Succ->begin());
Chris Lattner87fea852002-05-10 05:41:34 +0000160
161 // Loop over all of the uses of V, replacing ones that the Pi node
162 // dominates with references to the Pi node itself.
163 //
164 DominatorSet &DS = getAnalysis<DominatorSet>();
Chris Lattner76590642003-10-16 16:49:12 +0000165 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; )
166 if (Instruction *U = dyn_cast<Instruction>(*I++))
Chris Lattner87fea852002-05-10 05:41:34 +0000167 if (U->getParent()->getParent() == Succ->getParent() &&
168 DS.dominates(Succ, U->getParent())) {
169 // This instruction is dominated by the Pi node, replace reference to V
170 // with a reference to the Pi node.
171 //
172 U->replaceUsesOfWith(V, Pi);
Chris Lattner87fea852002-05-10 05:41:34 +0000173 }
Chris Lattner87fea852002-05-10 05:41:34 +0000174
175 // Set up the incoming value for the Pi node... do this after uses have been
176 // replaced, because we don't want the Pi node to refer to itself.
177 //
178 if (Rep == 0)
179 cast<PHINode>(Pi)->addIncoming(V, Pred);
180
Chris Lattner3dec1f22002-05-10 15:38:35 +0000181
182 ++NumInserted;
Chris Lattner87fea852002-05-10 05:41:34 +0000183 return true;
184}