Chris Lattner | 87fea85 | 2002-05-10 05:41:34 +0000 | [diff] [blame] | 1 | //===- PiNodeInsertion.cpp - Insert Pi nodes into a program ---------------===// |
| 2 | // |
| 3 | // PiNodeInsertion - This pass inserts single entry Phi nodes into basic blocks |
| 4 | // that are preceeded by a conditional branch, where the branch gives |
| 5 | // 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 | // |
| 18 | // This special form of Phi node is refered to as a Pi node, following the |
| 19 | // 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" |
| 32 | #include "llvm/BasicBlock.h" |
| 33 | #include "llvm/iTerminators.h" |
| 34 | #include "llvm/iOperators.h" |
| 35 | #include "llvm/iPHINode.h" |
| 36 | #include "llvm/Support/CFG.h" |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 37 | #include "Support/StatisticReporter.h" |
| 38 | |
| 39 | static Statistic<> NumInserted("pinodes\t\t- Number of Pi nodes inserted"); |
Chris Lattner | 87fea85 | 2002-05-10 05:41:34 +0000 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | struct PiNodeInserter : public FunctionPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 43 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 87fea85 | 2002-05-10 05:41:34 +0000 | [diff] [blame] | 44 | |
| 45 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 46 | AU.preservesCFG(); |
| 47 | AU.addRequired(DominatorSet::ID); |
| 48 | } |
| 49 | |
| 50 | // insertPiNodeFor - Insert a Pi node for V in the successors of BB if our |
| 51 | // conditions hold. If Rep is not null, fill in a value of 'Rep' instead of |
| 52 | // creating a new Pi node itself because we know that the value is a simple |
| 53 | // constant. |
| 54 | // |
| 55 | bool insertPiNodeFor(Value *V, BasicBlock *BB, Value *Rep = 0); |
| 56 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 57 | |
Chris Lattner | a6275cc | 2002-07-26 21:12:46 +0000 | [diff] [blame^] | 58 | RegisterOpt<PiNodeInserter> X("pinodes", "Pi Node Insertion"); |
Chris Lattner | 87fea85 | 2002-05-10 05:41:34 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | Pass *createPiNodeInsertionPass() { return new PiNodeInserter(); } |
| 62 | |
| 63 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 64 | bool PiNodeInserter::runOnFunction(Function &F) { |
Chris Lattner | 87fea85 | 2002-05-10 05:41:34 +0000 | [diff] [blame] | 65 | bool Changed = false; |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 66 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 67 | TerminatorInst *TI = I->getTerminator(); |
Chris Lattner | 87fea85 | 2002-05-10 05:41:34 +0000 | [diff] [blame] | 68 | |
| 69 | // FIXME: Insert PI nodes for switch statements too |
| 70 | |
| 71 | // Look for conditional branch instructions... that branch on a setcc test |
| 72 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) |
| 73 | if (BI->isConditional()) |
| 74 | // TODO: we could in theory support logical operations here too... |
| 75 | if (SetCondInst *SCI = dyn_cast<SetCondInst>(BI->getCondition())) { |
| 76 | // Calculate replacement values if this is an obvious constant == or |
| 77 | // != comparison... |
| 78 | Value *TrueRep = 0, *FalseRep = 0; |
| 79 | |
| 80 | // Make sure the the constant is the second operand if there is one... |
| 81 | // This fits with our cannonicalization patterns used elsewhere in the |
| 82 | // compiler, without depending on instcombine running before us. |
| 83 | // |
| 84 | if (isa<Constant>(SCI->getOperand(0)) && |
| 85 | !isa<Constant>(SCI->getOperand(1))) { |
| 86 | SCI->swapOperands(); |
| 87 | Changed = true; |
| 88 | } |
| 89 | |
| 90 | if (isa<Constant>(SCI->getOperand(1))) { |
| 91 | if (SCI->getOpcode() == Instruction::SetEQ) |
| 92 | TrueRep = SCI->getOperand(1); |
| 93 | else if (SCI->getOpcode() == Instruction::SetNE) |
| 94 | FalseRep = SCI->getOperand(1); |
| 95 | } |
| 96 | |
| 97 | BasicBlock *TB = BI->getSuccessor(0); // True block |
| 98 | BasicBlock *FB = BI->getSuccessor(1); // False block |
| 99 | |
| 100 | // Insert the Pi nodes for the first operand to the comparison... |
| 101 | Changed |= insertPiNodeFor(SCI->getOperand(0), TB, TrueRep); |
| 102 | Changed |= insertPiNodeFor(SCI->getOperand(0), FB, FalseRep); |
| 103 | |
| 104 | // Insert the Pi nodes for the second operand to the comparison... |
| 105 | Changed |= insertPiNodeFor(SCI->getOperand(1), TB); |
| 106 | Changed |= insertPiNodeFor(SCI->getOperand(1), FB); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return Changed; |
| 111 | } |
| 112 | |
| 113 | |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 114 | // alreadyHasPiNodeFor - Return true if there is already a Pi node in BB for V. |
Chris Lattner | 87fea85 | 2002-05-10 05:41:34 +0000 | [diff] [blame] | 115 | static bool alreadyHasPiNodeFor(Value *V, BasicBlock *BB) { |
| 116 | for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) |
| 117 | if (PHINode *PN = dyn_cast<PHINode>(*I)) |
| 118 | if (PN->getParent() == BB) |
| 119 | return true; |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | // insertPiNodeFor - Insert a Pi node for V in the successors of BB if our |
| 125 | // conditions hold. If Rep is not null, fill in a value of 'Rep' instead of |
| 126 | // creating a new Pi node itself because we know that the value is a simple |
| 127 | // constant. |
| 128 | // |
| 129 | bool PiNodeInserter::insertPiNodeFor(Value *V, BasicBlock *Succ, Value *Rep) { |
| 130 | // Do not insert Pi nodes for constants! |
| 131 | if (isa<Constant>(V)) return false; |
| 132 | |
| 133 | // Check to make sure that there is not already a PI node inserted... |
| 134 | if (alreadyHasPiNodeFor(V, Succ) && Rep == 0) |
| 135 | return false; |
| 136 | |
| 137 | // Insert Pi nodes only into successors that the conditional branch dominates. |
| 138 | // In this simple case, we know that BB dominates a successor as long there |
| 139 | // are no other incoming edges to the successor. |
| 140 | // |
| 141 | |
| 142 | // Check to make sure that the successor only has a single predecessor... |
| 143 | pred_iterator PI = pred_begin(Succ); |
| 144 | BasicBlock *Pred = *PI; |
| 145 | if (++PI != pred_end(Succ)) return false; // Multiple predecessor? Bail... |
| 146 | |
| 147 | // It seems to be safe to insert the Pi node. Do so now... |
| 148 | |
| 149 | // Create the Pi node... |
| 150 | Value *Pi = Rep; |
| 151 | if (Rep == 0) { |
| 152 | PHINode *Phi = new PHINode(V->getType(), V->getName() + ".pi"); |
| 153 | |
| 154 | // Insert the Pi node in the successor basic block... |
| 155 | Succ->getInstList().push_front(Phi); |
| 156 | Pi = Phi; |
| 157 | } |
| 158 | |
| 159 | // Loop over all of the uses of V, replacing ones that the Pi node |
| 160 | // dominates with references to the Pi node itself. |
| 161 | // |
| 162 | DominatorSet &DS = getAnalysis<DominatorSet>(); |
| 163 | for (unsigned i = 0; i < V->use_size(); ) { |
| 164 | if (Instruction *U = dyn_cast<Instruction>(*(V->use_begin()+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. |
| 169 | // |
| 170 | U->replaceUsesOfWith(V, Pi); |
| 171 | continue; // Do not skip the next use... |
| 172 | } |
| 173 | |
| 174 | // This use is not dominated by the Pi node, skip it... |
| 175 | ++i; |
| 176 | } |
| 177 | |
| 178 | // Set up the incoming value for the Pi node... do this after uses have been |
| 179 | // replaced, because we don't want the Pi node to refer to itself. |
| 180 | // |
| 181 | if (Rep == 0) |
| 182 | cast<PHINode>(Pi)->addIncoming(V, Pred); |
| 183 | |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 184 | |
| 185 | ++NumInserted; |
Chris Lattner | 87fea85 | 2002-05-10 05:41:34 +0000 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | |