Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 1 | //===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // This family of functions perform manipulations on basic blocks, and |
| 11 | // instructions contained within basic blocks. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 16 | #include "llvm/Function.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 17 | #include "llvm/Instructions.h" |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 18 | #include "llvm/Constant.h" |
| 19 | #include "llvm/Type.h" |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopInfo.h" |
| 22 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 24 | #include <algorithm> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 25 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame^] | 27 | /// DeleteBlockIfDead - If the specified basic block is trivially dead (has no |
| 28 | /// predecessors and not the entry block), delete it and return true. Otherwise |
| 29 | /// return false. |
| 30 | bool llvm::DeleteBlockIfDead(BasicBlock *BB) { |
| 31 | if (pred_begin(BB) != pred_end(BB) || |
| 32 | BB == &BB->getParent()->getEntryBlock()) |
| 33 | return false; |
| 34 | |
| 35 | TerminatorInst *BBTerm = BB->getTerminator(); |
| 36 | |
| 37 | // Loop through all of our successors and make sure they know that one |
| 38 | // of their predecessors is going away. |
| 39 | for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) |
| 40 | BBTerm->getSuccessor(i)->removePredecessor(BB); |
| 41 | |
| 42 | // Zap all the instructions in the block. |
| 43 | while (!BB->empty()) { |
| 44 | Instruction &I = BB->back(); |
| 45 | // If this instruction is used, replace uses with an arbitrary value. |
| 46 | // Because control flow can't get here, we don't care what we replace the |
| 47 | // value with. Note that since this block is unreachable, and all values |
| 48 | // contained within it must dominate their uses, that all uses will |
| 49 | // eventually be removed (they are themselves dead). |
| 50 | if (!I.use_empty()) |
| 51 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
| 52 | BB->getInstList().pop_back(); |
| 53 | } |
| 54 | |
| 55 | // Zap the block! |
| 56 | BB->eraseFromParent(); |
| 57 | return true; |
| 58 | } |
| 59 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 60 | /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, |
| 61 | /// if possible. The return value indicates success or failure. |
| 62 | bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) { |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 63 | pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 64 | // Can't merge the entry block. |
| 65 | if (pred_begin(BB) == pred_end(BB)) return false; |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 66 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 67 | BasicBlock *PredBB = *PI++; |
| 68 | for (; PI != PE; ++PI) // Search all predecessors, see if they are all same |
| 69 | if (*PI != PredBB) { |
| 70 | PredBB = 0; // There are multiple different predecessors... |
| 71 | break; |
| 72 | } |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 73 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 74 | // Can't merge if there are multiple predecessors. |
| 75 | if (!PredBB) return false; |
Owen Anderson | 3ecaf1b | 2008-07-18 17:46:41 +0000 | [diff] [blame] | 76 | // Don't break self-loops. |
| 77 | if (PredBB == BB) return false; |
| 78 | // Don't break invokes. |
| 79 | if (isa<InvokeInst>(PredBB->getTerminator())) return false; |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 80 | |
| 81 | succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB)); |
| 82 | BasicBlock* OnlySucc = BB; |
| 83 | for (; SI != SE; ++SI) |
| 84 | if (*SI != OnlySucc) { |
| 85 | OnlySucc = 0; // There are multiple distinct successors! |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | // Can't merge if there are multiple successors. |
| 90 | if (!OnlySucc) return false; |
Devang Patel | e435a5d | 2008-09-09 01:06:56 +0000 | [diff] [blame] | 91 | |
| 92 | // Can't merge if there is PHI loop. |
| 93 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) { |
| 94 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 95 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 96 | if (PN->getIncomingValue(i) == PN) |
| 97 | return false; |
| 98 | } else |
| 99 | break; |
| 100 | } |
| 101 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 102 | // Begin by getting rid of unneeded PHIs. |
| 103 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 104 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 105 | BB->getInstList().pop_front(); // Delete the phi node... |
| 106 | } |
| 107 | |
| 108 | // Delete the unconditional branch from the predecessor... |
| 109 | PredBB->getInstList().pop_back(); |
| 110 | |
| 111 | // Move all definitions in the successor to the predecessor... |
| 112 | PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); |
| 113 | |
| 114 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 115 | // source... |
| 116 | BB->replaceAllUsesWith(PredBB); |
| 117 | |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 118 | // Inherit predecessors name if it exists. |
| 119 | if (!PredBB->hasName()) |
| 120 | PredBB->takeName(BB); |
| 121 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 122 | // Finally, erase the old block and update dominator info. |
| 123 | if (P) { |
| 124 | if (DominatorTree* DT = P->getAnalysisToUpdate<DominatorTree>()) { |
| 125 | DomTreeNode* DTN = DT->getNode(BB); |
| 126 | DomTreeNode* PredDTN = DT->getNode(PredBB); |
| 127 | |
| 128 | if (DTN) { |
| 129 | SmallPtrSet<DomTreeNode*, 8> Children(DTN->begin(), DTN->end()); |
| 130 | for (SmallPtrSet<DomTreeNode*, 8>::iterator DI = Children.begin(), |
| 131 | DE = Children.end(); DI != DE; ++DI) |
| 132 | DT->changeImmediateDominator(*DI, PredDTN); |
| 133 | |
| 134 | DT->eraseNode(BB); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | BB->eraseFromParent(); |
| 140 | |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 145 | /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 146 | /// with a value, then remove and delete the original instruction. |
| 147 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 148 | void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 149 | BasicBlock::iterator &BI, Value *V) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 150 | Instruction &I = *BI; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 151 | // Replaces all of the uses of the instruction with uses of the value |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 152 | I.replaceAllUsesWith(V); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 154 | // Make sure to propagate a name if there is one already. |
| 155 | if (I.hasName() && !V->hasName()) |
| 156 | V->takeName(&I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 157 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 158 | // Delete the unnecessary instruction now... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 159 | BI = BIL.erase(BI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 163 | /// ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 164 | /// instruction specified by I. The original instruction is deleted and BI is |
| 165 | /// updated to point to the new instruction. |
| 166 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 167 | void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 168 | BasicBlock::iterator &BI, Instruction *I) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 169 | assert(I->getParent() == 0 && |
| 170 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 171 | |
| 172 | // Insert the new instruction into the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 173 | BasicBlock::iterator New = BIL.insert(BI, I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 174 | |
| 175 | // Replace all uses of the old instruction, and delete it. |
| 176 | ReplaceInstWithValue(BIL, BI, I); |
| 177 | |
| 178 | // Move BI back to point to the newly inserted instruction |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 179 | BI = New; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 182 | /// ReplaceInstWithInst - Replace the instruction specified by From with the |
| 183 | /// instruction specified by To. |
| 184 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 185 | void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 186 | BasicBlock::iterator BI(From); |
| 187 | ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 188 | } |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 190 | /// RemoveSuccessor - Change the specified terminator instruction such that its |
Reid Spencer | bc2eba1 | 2006-05-19 19:09:46 +0000 | [diff] [blame] | 191 | /// successor SuccNum no longer exists. Because this reduces the outgoing |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 192 | /// degree of the current basic block, the actual terminator instruction itself |
Reid Spencer | bc2eba1 | 2006-05-19 19:09:46 +0000 | [diff] [blame] | 193 | /// may have to be changed. In the case where the last successor of the block |
| 194 | /// is deleted, a return instruction is inserted in its place which can cause a |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 195 | /// surprising change in program behavior if it is not expected. |
| 196 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 197 | void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) { |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 198 | assert(SuccNum < TI->getNumSuccessors() && |
| 199 | "Trying to remove a nonexistant successor!"); |
| 200 | |
| 201 | // If our old successor block contains any PHI nodes, remove the entry in the |
| 202 | // PHI nodes that comes from this branch... |
| 203 | // |
| 204 | BasicBlock *BB = TI->getParent(); |
| 205 | TI->getSuccessor(SuccNum)->removePredecessor(BB); |
| 206 | |
| 207 | TerminatorInst *NewTI = 0; |
| 208 | switch (TI->getOpcode()) { |
| 209 | case Instruction::Br: |
| 210 | // If this is a conditional branch... convert to unconditional branch. |
| 211 | if (TI->getNumSuccessors() == 2) { |
| 212 | cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum)); |
| 213 | } else { // Otherwise convert to a return instruction... |
| 214 | Value *RetVal = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 215 | |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 216 | // Create a value to return... if the function doesn't return null... |
| 217 | if (BB->getParent()->getReturnType() != Type::VoidTy) |
| 218 | RetVal = Constant::getNullValue(BB->getParent()->getReturnType()); |
| 219 | |
| 220 | // Create the return... |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 221 | NewTI = ReturnInst::Create(RetVal); |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 222 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 223 | break; |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 224 | |
| 225 | case Instruction::Invoke: // Should convert to call |
| 226 | case Instruction::Switch: // Should remove entry |
| 227 | default: |
| 228 | case Instruction::Ret: // Cannot happen, has no successors! |
| 229 | assert(0 && "Unhandled terminator instruction type in RemoveSuccessor!"); |
| 230 | abort(); |
| 231 | } |
| 232 | |
| 233 | if (NewTI) // If it's a different instruction, replace. |
| 234 | ReplaceInstWithInst(TI, NewTI); |
| 235 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 236 | |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 237 | /// SplitEdge - Split the edge connecting specified block. Pass P must |
| 238 | /// not be NULL. |
| 239 | BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) { |
| 240 | TerminatorInst *LatchTerm = BB->getTerminator(); |
| 241 | unsigned SuccNum = 0; |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 242 | #ifndef NDEBUG |
| 243 | unsigned e = LatchTerm->getNumSuccessors(); |
| 244 | #endif |
| 245 | for (unsigned i = 0; ; ++i) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 246 | assert(i != e && "Didn't find edge?"); |
| 247 | if (LatchTerm->getSuccessor(i) == Succ) { |
| 248 | SuccNum = i; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // If this is a critical edge, let SplitCriticalEdge do it. |
| 254 | if (SplitCriticalEdge(BB->getTerminator(), SuccNum, P)) |
| 255 | return LatchTerm->getSuccessor(SuccNum); |
| 256 | |
| 257 | // If the edge isn't critical, then BB has a single successor or Succ has a |
| 258 | // single pred. Split the block. |
| 259 | BasicBlock::iterator SplitPoint; |
| 260 | if (BasicBlock *SP = Succ->getSinglePredecessor()) { |
| 261 | // If the successor only has a single pred, split the top of the successor |
| 262 | // block. |
| 263 | assert(SP == BB && "CFG broken"); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 264 | SP = NULL; |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 265 | return SplitBlock(Succ, Succ->begin(), P); |
| 266 | } else { |
| 267 | // Otherwise, if BB has a single successor, split it at the bottom of the |
| 268 | // block. |
| 269 | assert(BB->getTerminator()->getNumSuccessors() == 1 && |
| 270 | "Should have a single succ!"); |
| 271 | return SplitBlock(BB, BB->getTerminator(), P); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /// SplitBlock - Split the specified block at the specified instruction - every |
| 276 | /// thing before SplitPt stays in Old and everything starting with SplitPt moves |
| 277 | /// to a new block. The two blocks are joined by an unconditional branch and |
| 278 | /// the loop info is updated. |
| 279 | /// |
| 280 | BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 281 | BasicBlock::iterator SplitIt = SplitPt; |
| 282 | while (isa<PHINode>(SplitIt)) |
| 283 | ++SplitIt; |
| 284 | BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split"); |
| 285 | |
| 286 | // The new block lives in whichever loop the old one did. |
Owen Anderson | a90793b | 2008-10-03 06:55:35 +0000 | [diff] [blame] | 287 | if (LoopInfo* LI = P->getAnalysisToUpdate<LoopInfo>()) |
| 288 | if (Loop *L = LI->getLoopFor(Old)) |
| 289 | L->addBasicBlockToLoop(New, LI->getBase()); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 290 | |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 291 | if (DominatorTree *DT = P->getAnalysisToUpdate<DominatorTree>()) |
| 292 | { |
| 293 | // Old dominates New. New node domiantes all other nodes dominated by Old. |
| 294 | DomTreeNode *OldNode = DT->getNode(Old); |
| 295 | std::vector<DomTreeNode *> Children; |
| 296 | for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end(); |
| 297 | I != E; ++I) |
| 298 | Children.push_back(*I); |
| 299 | |
| 300 | DomTreeNode *NewNode = DT->addNewBlock(New,Old); |
| 301 | |
| 302 | for (std::vector<DomTreeNode *>::iterator I = Children.begin(), |
| 303 | E = Children.end(); I != E; ++I) |
| 304 | DT->changeImmediateDominator(*I, NewNode); |
| 305 | } |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 306 | |
| 307 | if (DominanceFrontier *DF = P->getAnalysisToUpdate<DominanceFrontier>()) |
| 308 | DF->splitBlock(Old); |
| 309 | |
| 310 | return New; |
| 311 | } |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 312 | |
| 313 | |
| 314 | /// SplitBlockPredecessors - This method transforms BB by introducing a new |
| 315 | /// basic block into the function, and moving some of the predecessors of BB to |
| 316 | /// be predecessors of the new block. The new predecessors are indicated by the |
| 317 | /// Preds array, which has NumPreds elements in it. The new block is given a |
| 318 | /// suffix of 'Suffix'. |
| 319 | /// |
| 320 | /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree and |
| 321 | /// DominanceFrontier, but no other analyses. |
| 322 | BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, |
| 323 | BasicBlock *const *Preds, |
| 324 | unsigned NumPreds, const char *Suffix, |
| 325 | Pass *P) { |
| 326 | // Create new basic block, insert right before the original block. |
| 327 | BasicBlock *NewBB = |
| 328 | BasicBlock::Create(BB->getName()+Suffix, BB->getParent(), BB); |
| 329 | |
| 330 | // The new block unconditionally branches to the old block. |
| 331 | BranchInst *BI = BranchInst::Create(BB, NewBB); |
| 332 | |
| 333 | // Move the edges from Preds to point to NewBB instead of BB. |
Nick Lewycky | 280a6e6 | 2008-04-25 16:53:59 +0000 | [diff] [blame] | 334 | for (unsigned i = 0; i != NumPreds; ++i) |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 335 | Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 336 | |
| 337 | // Update dominator tree and dominator frontier if available. |
| 338 | DominatorTree *DT = P ? P->getAnalysisToUpdate<DominatorTree>() : 0; |
| 339 | if (DT) |
| 340 | DT->splitBlock(NewBB); |
| 341 | if (DominanceFrontier *DF = P ? P->getAnalysisToUpdate<DominanceFrontier>():0) |
| 342 | DF->splitBlock(NewBB); |
| 343 | AliasAnalysis *AA = P ? P->getAnalysisToUpdate<AliasAnalysis>() : 0; |
| 344 | |
| 345 | |
| 346 | // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI |
| 347 | // node becomes an incoming value for BB's phi node. However, if the Preds |
| 348 | // list is empty, we need to insert dummy entries into the PHI nodes in BB to |
| 349 | // account for the newly created predecessor. |
| 350 | if (NumPreds == 0) { |
| 351 | // Insert dummy values as the incoming value. |
| 352 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) |
| 353 | cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); |
| 354 | return NewBB; |
| 355 | } |
| 356 | |
| 357 | // Otherwise, create a new PHI node in NewBB for each PHI node in BB. |
| 358 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ) { |
| 359 | PHINode *PN = cast<PHINode>(I++); |
| 360 | |
| 361 | // Check to see if all of the values coming in are the same. If so, we |
| 362 | // don't need to create a new PHI node. |
| 363 | Value *InVal = PN->getIncomingValueForBlock(Preds[0]); |
| 364 | for (unsigned i = 1; i != NumPreds; ++i) |
| 365 | if (InVal != PN->getIncomingValueForBlock(Preds[i])) { |
| 366 | InVal = 0; |
| 367 | break; |
| 368 | } |
| 369 | |
| 370 | if (InVal) { |
| 371 | // If all incoming values for the new PHI would be the same, just don't |
| 372 | // make a new PHI. Instead, just remove the incoming values from the old |
| 373 | // PHI. |
| 374 | for (unsigned i = 0; i != NumPreds; ++i) |
| 375 | PN->removeIncomingValue(Preds[i], false); |
| 376 | } else { |
| 377 | // If the values coming into the block are not the same, we need a PHI. |
| 378 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 379 | PHINode *NewPHI = |
| 380 | PHINode::Create(PN->getType(), PN->getName()+".ph", BI); |
| 381 | if (AA) AA->copyValue(PN, NewPHI); |
| 382 | |
| 383 | // Move all of the PHI values for 'Preds' to the new PHI. |
| 384 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 385 | Value *V = PN->removeIncomingValue(Preds[i], false); |
| 386 | NewPHI->addIncoming(V, Preds[i]); |
| 387 | } |
| 388 | InVal = NewPHI; |
| 389 | } |
| 390 | |
| 391 | // Add an incoming value to the PHI node in the loop for the preheader |
| 392 | // edge. |
| 393 | PN->addIncoming(InVal, NewBB); |
| 394 | |
| 395 | // Check to see if we can eliminate this phi node. |
| 396 | if (Value *V = PN->hasConstantValue(DT != 0)) { |
| 397 | Instruction *I = dyn_cast<Instruction>(V); |
| 398 | if (!I || DT == 0 || DT->dominates(I, PN)) { |
| 399 | PN->replaceAllUsesWith(V); |
| 400 | if (AA) AA->deleteValue(PN); |
| 401 | PN->eraseFromParent(); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return NewBB; |
| 407 | } |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 409 | /// AreEquivalentAddressValues - Test if A and B will obviously have the same |
| 410 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 411 | /// value in code like this: |
| 412 | /// %t0 = getelementptr @a, 0, 3 |
| 413 | /// store i32 0, i32* %t0 |
| 414 | /// %t1 = getelementptr @a, 0, 3 |
| 415 | /// %t2 = load i32* %t1 |
| 416 | /// |
| 417 | static bool AreEquivalentAddressValues(const Value *A, const Value *B) { |
| 418 | // Test if the values are trivially equivalent. |
| 419 | if (A == B) return true; |
| 420 | |
| 421 | // Test if the values come form identical arithmetic instructions. |
| 422 | if (isa<BinaryOperator>(A) || isa<CastInst>(A) || |
| 423 | isa<PHINode>(A) || isa<GetElementPtrInst>(A)) |
| 424 | if (const Instruction *BI = dyn_cast<Instruction>(B)) |
| 425 | if (cast<Instruction>(A)->isIdenticalTo(BI)) |
| 426 | return true; |
| 427 | |
| 428 | // Otherwise they may not be equivalent. |
| 429 | return false; |
| 430 | } |
| 431 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 432 | /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the |
| 433 | /// instruction before ScanFrom) checking to see if we have the value at the |
| 434 | /// memory address *Ptr locally available within a small number of instructions. |
| 435 | /// If the value is available, return it. |
| 436 | /// |
| 437 | /// If not, return the iterator for the last validated instruction that the |
| 438 | /// value would be live through. If we scanned the entire block and didn't find |
| 439 | /// something that invalidates *Ptr or provides it, ScanFrom would be left at |
| 440 | /// begin() and this returns null. ScanFrom could also be left |
| 441 | /// |
| 442 | /// MaxInstsToScan specifies the maximum instructions to scan in the block. If |
| 443 | /// it is set to 0, it will scan the whole block. You can also optionally |
| 444 | /// specify an alias analysis implementation, which makes this more precise. |
| 445 | Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, |
| 446 | BasicBlock::iterator &ScanFrom, |
| 447 | unsigned MaxInstsToScan, |
| 448 | AliasAnalysis *AA) { |
| 449 | if (MaxInstsToScan == 0) MaxInstsToScan = ~0U; |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 450 | |
| 451 | // If we're using alias analysis to disambiguate get the size of *Ptr. |
| 452 | unsigned AccessSize = 0; |
| 453 | if (AA) { |
| 454 | const Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType(); |
| 455 | AccessSize = AA->getTargetData().getTypeStoreSizeInBits(AccessTy); |
| 456 | } |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 457 | |
| 458 | while (ScanFrom != ScanBB->begin()) { |
| 459 | // Don't scan huge blocks. |
| 460 | if (MaxInstsToScan-- == 0) return 0; |
| 461 | |
| 462 | Instruction *Inst = --ScanFrom; |
| 463 | |
| 464 | // If this is a load of Ptr, the loaded value is available. |
| 465 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 466 | if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 467 | return LI; |
| 468 | |
| 469 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 470 | // If this is a store through Ptr, the value is available! |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 471 | if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 472 | return SI->getOperand(0); |
| 473 | |
| 474 | // If Ptr is an alloca and this is a store to a different alloca, ignore |
| 475 | // the store. This is a trivial form of alias analysis that is important |
| 476 | // for reg2mem'd code. |
| 477 | if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) && |
| 478 | (isa<AllocaInst>(SI->getOperand(1)) || |
| 479 | isa<GlobalVariable>(SI->getOperand(1)))) |
| 480 | continue; |
| 481 | |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 482 | // If we have alias analysis and it says the store won't modify the loaded |
| 483 | // value, ignore the store. |
| 484 | if (AA && |
| 485 | (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) |
| 486 | continue; |
| 487 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 488 | // Otherwise the store that may or may not alias the pointer, bail out. |
| 489 | ++ScanFrom; |
| 490 | return 0; |
| 491 | } |
| 492 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 493 | // If this is some other instruction that may clobber Ptr, bail out. |
| 494 | if (Inst->mayWriteToMemory()) { |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 495 | // If alias analysis claims that it really won't modify the load, |
| 496 | // ignore it. |
| 497 | if (AA && |
| 498 | (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) |
| 499 | continue; |
| 500 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 501 | // May modify the pointer, bail out. |
| 502 | ++ScanFrom; |
| 503 | return 0; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | // Got to the start of the block, we didn't find it, but are done for this |
| 508 | // block. |
| 509 | return 0; |
| 510 | } |