1 //===- PiNodeInsertion.cpp - Insert Pi nodes into a program ---------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // PiNodeInsertion - This pass inserts single entry Phi nodes into basic blocks
11 // that are preceded by a conditional branch, where the branch gives
12 // information about the operands of the condition. For example, this C code:
13 // if (x == 0) { ... = x + 4;
16 // x2 = phi(x); // Node that can hold data flow information about X
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".
25 // This special form of Phi node is referred to as a Pi node, following the
26 // terminology defined in the "Array Bounds Checks on Demand" paper.
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.
33 //===----------------------------------------------------------------------===//
35 #include "llvm/Transforms/Scalar.h"
36 #include "llvm/Analysis/Dominators.h"
37 #include "llvm/Pass.h"
38 #include "llvm/Function.h"
39 #include "llvm/Instructions.h"
40 #include "llvm/Support/CFG.h"
41 #include "Support/Statistic.h"
45 Statistic<> NumInserted("pinodes", "Number of Pi nodes inserted");
47 struct PiNodeInserter : public FunctionPass {
48 virtual bool runOnFunction(Function &F);
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.addRequired<DominatorSet>();
55 // insertPiNodeFor - Insert a Pi node for V in the successors of BB if our
56 // conditions hold. If Rep is not null, fill in a value of 'Rep' instead of
57 // creating a new Pi node itself because we know that the value is a simple
60 bool insertPiNodeFor(Value *V, BasicBlock *BB, Value *Rep = 0);
63 RegisterOpt<PiNodeInserter> X("pinodes", "Pi Node Insertion");
66 Pass *llvm::createPiNodeInsertionPass() { return new PiNodeInserter(); }
69 bool PiNodeInserter::runOnFunction(Function &F) {
71 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
72 TerminatorInst *TI = I->getTerminator();
74 // FIXME: Insert PI nodes for switch statements too
76 // Look for conditional branch instructions... that branch on a setcc test
77 if (BranchInst *BI = dyn_cast<BranchInst>(TI))
78 if (BI->isConditional())
79 // TODO: we could in theory support logical operations here too...
80 if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition())) {
81 // Calculate replacement values if this is an obvious constant == or
83 Value *TrueRep = 0, *FalseRep = 0;
85 // Make sure the the constant is the second operand if there is one...
86 // This fits with our canonicalization patterns used elsewhere in the
87 // compiler, without depending on instcombine running before us.
89 if (isa<Constant>(SCI->getOperand(0)) &&
90 !isa<Constant>(SCI->getOperand(1))) {
95 if (isa<Constant>(SCI->getOperand(1))) {
96 if (SCI->getOpcode() == Instruction::SetEQ)
97 TrueRep = SCI->getOperand(1);
98 else if (SCI->getOpcode() == Instruction::SetNE)
99 FalseRep = SCI->getOperand(1);
102 BasicBlock *TB = BI->getSuccessor(0); // True block
103 BasicBlock *FB = BI->getSuccessor(1); // False block
105 // Insert the Pi nodes for the first operand to the comparison...
106 Changed |= insertPiNodeFor(SCI->getOperand(0), TB, TrueRep);
107 Changed |= insertPiNodeFor(SCI->getOperand(0), FB, FalseRep);
109 // Insert the Pi nodes for the second operand to the comparison...
110 Changed |= insertPiNodeFor(SCI->getOperand(1), TB);
111 Changed |= insertPiNodeFor(SCI->getOperand(1), FB);
119 // alreadyHasPiNodeFor - Return true if there is already a Pi node in BB for V.
120 static bool alreadyHasPiNodeFor(Value *V, BasicBlock *BB) {
121 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I)
122 if (PHINode *PN = dyn_cast<PHINode>(*I))
123 if (PN->getParent() == BB)
129 // insertPiNodeFor - Insert a Pi node for V in the successors of BB if our
130 // conditions hold. If Rep is not null, fill in a value of 'Rep' instead of
131 // creating a new Pi node itself because we know that the value is a simple
134 bool PiNodeInserter::insertPiNodeFor(Value *V, BasicBlock *Succ, Value *Rep) {
135 // Do not insert Pi nodes for constants!
136 if (isa<Constant>(V)) return false;
138 // Check to make sure that there is not already a PI node inserted...
139 if (alreadyHasPiNodeFor(V, Succ) && Rep == 0)
142 // Insert Pi nodes only into successors that the conditional branch dominates.
143 // In this simple case, we know that BB dominates a successor as long there
144 // are no other incoming edges to the successor.
147 // Check to make sure that the successor only has a single predecessor...
148 pred_iterator PI = pred_begin(Succ);
149 BasicBlock *Pred = *PI;
150 if (++PI != pred_end(Succ)) return false; // Multiple predecessor? Bail...
152 // It seems to be safe to insert the Pi node. Do so now...
154 // Create the Pi node...
156 if (Rep == 0) // Insert the Pi node in the successor basic block...
157 Pi = new PHINode(V->getType(), V->getName() + ".pi", Succ->begin());
159 // Loop over all of the uses of V, replacing ones that the Pi node
160 // dominates with references to the Pi node itself.
162 DominatorSet &DS = getAnalysis<DominatorSet>();
163 for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; )
164 if (Instruction *U = dyn_cast<Instruction>(*I++))
165 if (U->getParent()->getParent() == Succ->getParent() &&
166 DS.dominates(Succ, U->getParent())) {
167 // This instruction is dominated by the Pi node, replace reference to V
168 // with a reference to the Pi node.
170 U->replaceUsesOfWith(V, Pi);
173 // Set up the incoming value for the Pi node... do this after uses have been
174 // replaced, because we don't want the Pi node to refer to itself.
177 cast<PHINode>(Pi)->addIncoming(V, Pred);