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