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" |
Dale Johannesen | bd8e650 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 18 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 19 | #include "llvm/Constant.h" |
| 20 | #include "llvm/Type.h" |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopInfo.h" |
| 23 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Scalar.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ValueHandle.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 71af9b0 | 2008-12-03 06:40:52 +0000 | [diff] [blame] | 32 | /// DeleteDeadBlock - Delete the specified block, which must have no |
| 33 | /// predecessors. |
| 34 | void llvm::DeleteDeadBlock(BasicBlock *BB) { |
Chris Lattner | 2973a25 | 2008-12-03 07:45:15 +0000 | [diff] [blame] | 35 | assert((pred_begin(BB) == pred_end(BB) || |
| 36 | // Can delete self loop. |
| 37 | BB->getSinglePredecessor() == BB) && "Block is not dead!"); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 38 | TerminatorInst *BBTerm = BB->getTerminator(); |
Devang Patel | 5622f07 | 2009-02-24 00:05:16 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 40 | // Loop through all of our successors and make sure they know that one |
| 41 | // of their predecessors is going away. |
| 42 | for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) |
| 43 | BBTerm->getSuccessor(i)->removePredecessor(BB); |
| 44 | |
| 45 | // Zap all the instructions in the block. |
| 46 | while (!BB->empty()) { |
| 47 | Instruction &I = BB->back(); |
| 48 | // If this instruction is used, replace uses with an arbitrary value. |
| 49 | // Because control flow can't get here, we don't care what we replace the |
| 50 | // value with. Note that since this block is unreachable, and all values |
| 51 | // contained within it must dominate their uses, that all uses will |
| 52 | // eventually be removed (they are themselves dead). |
| 53 | if (!I.use_empty()) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 54 | I.replaceAllUsesWith(UndefValue::get(I.getType())); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 55 | BB->getInstList().pop_back(); |
| 56 | } |
Devang Patel | 5622f07 | 2009-02-24 00:05:16 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 58 | // Zap the block! |
| 59 | BB->eraseFromParent(); |
Chris Lattner | 2b1ba24 | 2008-12-03 06:37:44 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 62 | /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are |
| 63 | /// any single-entry PHI nodes in it, fold them away. This handles the case |
| 64 | /// when all entries to the PHI nodes in a block are guaranteed equal, such as |
| 65 | /// when the block has exactly one predecessor. |
| 66 | void llvm::FoldSingleEntryPHINodes(BasicBlock *BB) { |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 67 | while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) { |
| 68 | if (PN->getIncomingValue(0) != PN) |
| 69 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 70 | else |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 71 | PN->replaceAllUsesWith(UndefValue::get(PN->getType())); |
Chris Lattner | 29874e0 | 2008-12-03 19:44:02 +0000 | [diff] [blame] | 72 | PN->eraseFromParent(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 77 | /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it |
| 78 | /// is dead. Also recursively delete any operands that become dead as |
| 79 | /// a result. This includes tracing the def-use list from the PHI to see if |
Dan Gohman | 35738ac | 2009-05-04 22:30:44 +0000 | [diff] [blame] | 80 | /// it is ultimately unused or if it reaches an unused cycle. |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 81 | bool llvm::DeleteDeadPHIs(BasicBlock *BB) { |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 82 | // Recursively deleting a PHI may cause multiple PHIs to be deleted |
| 83 | // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete. |
| 84 | SmallVector<WeakVH, 8> PHIs; |
| 85 | for (BasicBlock::iterator I = BB->begin(); |
| 86 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 87 | PHIs.push_back(PN); |
| 88 | |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 89 | bool Changed = false; |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 90 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) |
| 91 | if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*())) |
Dan Gohman | 90fe0bd | 2010-01-05 15:45:31 +0000 | [diff] [blame] | 92 | Changed |= RecursivelyDeleteDeadPHINode(PN); |
| 93 | |
| 94 | return Changed; |
Dan Gohman | afc36a9 | 2009-05-02 18:29:22 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 97 | /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor, |
| 98 | /// if possible. The return value indicates success or failure. |
Chris Lattner | 8820292 | 2009-11-01 04:57:33 +0000 | [diff] [blame] | 99 | bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P) { |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 100 | pred_iterator PI(pred_begin(BB)), PE(pred_end(BB)); |
Chris Lattner | 8820292 | 2009-11-01 04:57:33 +0000 | [diff] [blame] | 101 | // Can't merge the entry block. Don't merge away blocks who have their |
| 102 | // address taken: this is a bug if the predecessor block is the entry node |
| 103 | // (because we'd end up taking the address of the entry) and undesirable in |
| 104 | // any case. |
| 105 | if (pred_begin(BB) == pred_end(BB) || |
| 106 | BB->hasAddressTaken()) return false; |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 107 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 108 | BasicBlock *PredBB = *PI++; |
| 109 | for (; PI != PE; ++PI) // Search all predecessors, see if they are all same |
| 110 | if (*PI != PredBB) { |
| 111 | PredBB = 0; // There are multiple different predecessors... |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | // Can't merge if there are multiple predecessors. |
| 116 | if (!PredBB) return false; |
| 117 | // Don't break self-loops. |
| 118 | if (PredBB == BB) return false; |
| 119 | // Don't break invokes. |
| 120 | if (isa<InvokeInst>(PredBB->getTerminator())) return false; |
| 121 | |
| 122 | succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB)); |
| 123 | BasicBlock* OnlySucc = BB; |
| 124 | for (; SI != SE; ++SI) |
| 125 | if (*SI != OnlySucc) { |
| 126 | OnlySucc = 0; // There are multiple distinct successors! |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | // Can't merge if there are multiple successors. |
| 131 | if (!OnlySucc) return false; |
Devang Patel | e435a5d | 2008-09-09 01:06:56 +0000 | [diff] [blame] | 132 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 133 | // Can't merge if there is PHI loop. |
| 134 | for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) { |
| 135 | if (PHINode *PN = dyn_cast<PHINode>(BI)) { |
| 136 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 137 | if (PN->getIncomingValue(i) == PN) |
| 138 | return false; |
| 139 | } else |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | // Begin by getting rid of unneeded PHIs. |
| 144 | while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) { |
| 145 | PN->replaceAllUsesWith(PN->getIncomingValue(0)); |
| 146 | BB->getInstList().pop_front(); // Delete the phi node... |
| 147 | } |
| 148 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 149 | // Delete the unconditional branch from the predecessor... |
| 150 | PredBB->getInstList().pop_back(); |
| 151 | |
| 152 | // Move all definitions in the successor to the predecessor... |
| 153 | PredBB->getInstList().splice(PredBB->end(), BB->getInstList()); |
| 154 | |
| 155 | // Make all PHI nodes that referred to BB now refer to Pred as their |
| 156 | // source... |
| 157 | BB->replaceAllUsesWith(PredBB); |
| 158 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 159 | // Inherit predecessors name if it exists. |
Owen Anderson | 11f2ec8 | 2008-07-17 19:42:29 +0000 | [diff] [blame] | 160 | if (!PredBB->hasName()) |
| 161 | PredBB->takeName(BB); |
| 162 | |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 163 | // Finally, erase the old block and update dominator info. |
| 164 | if (P) { |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 165 | if (DominatorTree* DT = P->getAnalysisIfAvailable<DominatorTree>()) { |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 166 | DomTreeNode* DTN = DT->getNode(BB); |
| 167 | DomTreeNode* PredDTN = DT->getNode(PredBB); |
| 168 | |
| 169 | if (DTN) { |
| 170 | SmallPtrSet<DomTreeNode*, 8> Children(DTN->begin(), DTN->end()); |
| 171 | for (SmallPtrSet<DomTreeNode*, 8>::iterator DI = Children.begin(), |
| 172 | DE = Children.end(); DI != DE; ++DI) |
| 173 | DT->changeImmediateDominator(*DI, PredDTN); |
| 174 | |
| 175 | DT->eraseNode(BB); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | BB->eraseFromParent(); |
| 181 | |
Dan Gohman | 438b583 | 2009-10-31 17:33:01 +0000 | [diff] [blame] | 182 | |
| 183 | return true; |
Owen Anderson | b31b06d | 2008-07-17 00:01:40 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 186 | /// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI) |
| 187 | /// with a value, then remove and delete the original instruction. |
| 188 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 189 | void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL, |
| 190 | BasicBlock::iterator &BI, Value *V) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 191 | Instruction &I = *BI; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 192 | // 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] | 193 | I.replaceAllUsesWith(V); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 86cc423 | 2007-02-11 01:37:51 +0000 | [diff] [blame] | 195 | // Make sure to propagate a name if there is one already. |
| 196 | if (I.hasName() && !V->hasName()) |
| 197 | V->takeName(&I); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 198 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 199 | // Delete the unnecessary instruction now... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 200 | BI = BIL.erase(BI); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 204 | /// ReplaceInstWithInst - Replace the instruction specified by BI with the |
| 205 | /// instruction specified by I. The original instruction is deleted and BI is |
| 206 | /// updated to point to the new instruction. |
| 207 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 208 | void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL, |
| 209 | BasicBlock::iterator &BI, Instruction *I) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 210 | assert(I->getParent() == 0 && |
| 211 | "ReplaceInstWithInst: Instruction already inserted into basic block!"); |
| 212 | |
| 213 | // Insert the new instruction into the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 214 | BasicBlock::iterator New = BIL.insert(BI, I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 215 | |
| 216 | // Replace all uses of the old instruction, and delete it. |
| 217 | ReplaceInstWithValue(BIL, BI, I); |
| 218 | |
| 219 | // Move BI back to point to the newly inserted instruction |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 220 | BI = New; |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 223 | /// ReplaceInstWithInst - Replace the instruction specified by From with the |
| 224 | /// instruction specified by To. |
| 225 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 226 | void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 227 | BasicBlock::iterator BI(From); |
| 228 | ReplaceInstWithInst(From->getParent()->getInstList(), BI, To); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 229 | } |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 231 | /// RemoveSuccessor - Change the specified terminator instruction such that its |
Reid Spencer | bc2eba1 | 2006-05-19 19:09:46 +0000 | [diff] [blame] | 232 | /// successor SuccNum no longer exists. Because this reduces the outgoing |
Chris Lattner | 0f67dd6 | 2005-04-21 16:04:49 +0000 | [diff] [blame] | 233 | /// degree of the current basic block, the actual terminator instruction itself |
Reid Spencer | bc2eba1 | 2006-05-19 19:09:46 +0000 | [diff] [blame] | 234 | /// may have to be changed. In the case where the last successor of the block |
| 235 | /// 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] | 236 | /// surprising change in program behavior if it is not expected. |
| 237 | /// |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 238 | void llvm::RemoveSuccessor(TerminatorInst *TI, unsigned SuccNum) { |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 239 | assert(SuccNum < TI->getNumSuccessors() && |
| 240 | "Trying to remove a nonexistant successor!"); |
| 241 | |
| 242 | // If our old successor block contains any PHI nodes, remove the entry in the |
| 243 | // PHI nodes that comes from this branch... |
| 244 | // |
| 245 | BasicBlock *BB = TI->getParent(); |
| 246 | TI->getSuccessor(SuccNum)->removePredecessor(BB); |
| 247 | |
| 248 | TerminatorInst *NewTI = 0; |
| 249 | switch (TI->getOpcode()) { |
| 250 | case Instruction::Br: |
| 251 | // If this is a conditional branch... convert to unconditional branch. |
| 252 | if (TI->getNumSuccessors() == 2) { |
| 253 | cast<BranchInst>(TI)->setUnconditionalDest(TI->getSuccessor(1-SuccNum)); |
| 254 | } else { // Otherwise convert to a return instruction... |
| 255 | Value *RetVal = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 256 | |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 257 | // Create a value to return... if the function doesn't return null... |
Benjamin Kramer | f012705 | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 258 | if (!BB->getParent()->getReturnType()->isVoidTy()) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 259 | RetVal = Constant::getNullValue(BB->getParent()->getReturnType()); |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 260 | |
| 261 | // Create the return... |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 262 | NewTI = ReturnInst::Create(TI->getContext(), RetVal); |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 263 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 264 | break; |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 265 | |
| 266 | case Instruction::Invoke: // Should convert to call |
| 267 | case Instruction::Switch: // Should remove entry |
| 268 | default: |
| 269 | case Instruction::Ret: // Cannot happen, has no successors! |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 270 | llvm_unreachable("Unhandled terminator instruction type in RemoveSuccessor!"); |
Chris Lattner | b0f0ef8 | 2002-07-29 22:32:08 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (NewTI) // If it's a different instruction, replace. |
| 274 | ReplaceInstWithInst(TI, NewTI); |
| 275 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 276 | |
Bob Wilson | ae23daf | 2010-02-16 21:06:42 +0000 | [diff] [blame] | 277 | /// GetSuccessorNumber - Search for the specified successor of basic block BB |
| 278 | /// and return its position in the terminator instruction's list of |
| 279 | /// successors. It is an error to call this with a block that is not a |
| 280 | /// successor. |
| 281 | unsigned llvm::GetSuccessorNumber(BasicBlock *BB, BasicBlock *Succ) { |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 282 | TerminatorInst *Term = BB->getTerminator(); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 283 | #ifndef NDEBUG |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 284 | unsigned e = Term->getNumSuccessors(); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 285 | #endif |
| 286 | for (unsigned i = 0; ; ++i) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 287 | assert(i != e && "Didn't find edge?"); |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 288 | if (Term->getSuccessor(i) == Succ) |
| 289 | return i; |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 290 | } |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /// SplitEdge - Split the edge connecting specified block. Pass P must |
| 295 | /// not be NULL. |
| 296 | BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) { |
Bob Wilson | ae23daf | 2010-02-16 21:06:42 +0000 | [diff] [blame] | 297 | unsigned SuccNum = GetSuccessorNumber(BB, Succ); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 298 | |
| 299 | // If this is a critical edge, let SplitCriticalEdge do it. |
Bob Wilson | adb6f22 | 2010-02-16 19:49:17 +0000 | [diff] [blame] | 300 | TerminatorInst *LatchTerm = BB->getTerminator(); |
| 301 | if (SplitCriticalEdge(LatchTerm, SuccNum, P)) |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 302 | return LatchTerm->getSuccessor(SuccNum); |
| 303 | |
| 304 | // If the edge isn't critical, then BB has a single successor or Succ has a |
| 305 | // single pred. Split the block. |
| 306 | BasicBlock::iterator SplitPoint; |
| 307 | if (BasicBlock *SP = Succ->getSinglePredecessor()) { |
| 308 | // If the successor only has a single pred, split the top of the successor |
| 309 | // block. |
| 310 | assert(SP == BB && "CFG broken"); |
Devang Patel | 8a88a14 | 2008-11-03 23:14:09 +0000 | [diff] [blame] | 311 | SP = NULL; |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 312 | return SplitBlock(Succ, Succ->begin(), P); |
| 313 | } else { |
| 314 | // Otherwise, if BB has a single successor, split it at the bottom of the |
| 315 | // block. |
| 316 | assert(BB->getTerminator()->getNumSuccessors() == 1 && |
| 317 | "Should have a single succ!"); |
| 318 | return SplitBlock(BB, BB->getTerminator(), P); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | /// SplitBlock - Split the specified block at the specified instruction - every |
| 323 | /// thing before SplitPt stays in Old and everything starting with SplitPt moves |
| 324 | /// to a new block. The two blocks are joined by an unconditional branch and |
| 325 | /// the loop info is updated. |
| 326 | /// |
| 327 | BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) { |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 328 | BasicBlock::iterator SplitIt = SplitPt; |
| 329 | while (isa<PHINode>(SplitIt)) |
| 330 | ++SplitIt; |
| 331 | BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split"); |
| 332 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 333 | // The new block lives in whichever loop the old one did. This preserves |
| 334 | // LCSSA as well, because we force the split point to be after any PHI nodes. |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 335 | if (LoopInfo* LI = P->getAnalysisIfAvailable<LoopInfo>()) |
Owen Anderson | a90793b | 2008-10-03 06:55:35 +0000 | [diff] [blame] | 336 | if (Loop *L = LI->getLoopFor(Old)) |
| 337 | L->addBasicBlockToLoop(New, LI->getBase()); |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 338 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 339 | if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) |
Devang Patel | a8a8a36 | 2007-07-19 02:29:24 +0000 | [diff] [blame] | 340 | { |
| 341 | // Old dominates New. New node domiantes all other nodes dominated by Old. |
| 342 | DomTreeNode *OldNode = DT->getNode(Old); |
| 343 | std::vector<DomTreeNode *> Children; |
| 344 | for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end(); |
| 345 | I != E; ++I) |
| 346 | Children.push_back(*I); |
| 347 | |
| 348 | DomTreeNode *NewNode = DT->addNewBlock(New,Old); |
| 349 | |
| 350 | for (std::vector<DomTreeNode *>::iterator I = Children.begin(), |
| 351 | E = Children.end(); I != E; ++I) |
| 352 | DT->changeImmediateDominator(*I, NewNode); |
| 353 | } |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 354 | |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 355 | if (DominanceFrontier *DF = P->getAnalysisIfAvailable<DominanceFrontier>()) |
Devang Patel | 8019893 | 2007-07-06 21:39:20 +0000 | [diff] [blame] | 356 | DF->splitBlock(Old); |
| 357 | |
| 358 | return New; |
| 359 | } |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 360 | |
| 361 | |
| 362 | /// SplitBlockPredecessors - This method transforms BB by introducing a new |
| 363 | /// basic block into the function, and moving some of the predecessors of BB to |
| 364 | /// be predecessors of the new block. The new predecessors are indicated by the |
| 365 | /// Preds array, which has NumPreds elements in it. The new block is given a |
| 366 | /// suffix of 'Suffix'. |
| 367 | /// |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 368 | /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree, |
| 369 | /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. |
| 370 | /// In particular, it does not preserve LoopSimplify (because it's |
| 371 | /// complicated to handle the case where one of the edges being split |
| 372 | /// is an exit of a loop with other exits). |
| 373 | /// |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 374 | BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB, |
| 375 | BasicBlock *const *Preds, |
| 376 | unsigned NumPreds, const char *Suffix, |
| 377 | Pass *P) { |
| 378 | // Create new basic block, insert right before the original block. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 379 | BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), BB->getName()+Suffix, |
| 380 | BB->getParent(), BB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 381 | |
| 382 | // The new block unconditionally branches to the old block. |
| 383 | BranchInst *BI = BranchInst::Create(BB, NewBB); |
| 384 | |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 385 | LoopInfo *LI = P ? P->getAnalysisIfAvailable<LoopInfo>() : 0; |
| 386 | Loop *L = LI ? LI->getLoopFor(BB) : 0; |
| 387 | bool PreserveLCSSA = P->mustPreserveAnalysisID(LCSSAID); |
| 388 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 389 | // Move the edges from Preds to point to NewBB instead of BB. |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 390 | // While here, if we need to preserve loop analyses, collect |
| 391 | // some information about how this split will affect loops. |
| 392 | bool HasLoopExit = false; |
| 393 | bool IsLoopEntry = !!L; |
| 394 | bool SplitMakesNewLoopHeader = false; |
| 395 | for (unsigned i = 0; i != NumPreds; ++i) { |
Dan Gohman | b8eb17c | 2009-11-05 18:25:44 +0000 | [diff] [blame] | 396 | // This is slightly more strict than necessary; the minimum requirement |
| 397 | // is that there be no more than one indirectbr branching to BB. And |
| 398 | // all BlockAddress uses would need to be updated. |
| 399 | assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) && |
| 400 | "Cannot split an edge from an IndirectBrInst"); |
| 401 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 402 | Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 403 | |
| 404 | if (LI) { |
| 405 | // If we need to preserve LCSSA, determine if any of |
| 406 | // the preds is a loop exit. |
| 407 | if (PreserveLCSSA) |
| 408 | if (Loop *PL = LI->getLoopFor(Preds[i])) |
| 409 | if (!PL->contains(BB)) |
| 410 | HasLoopExit = true; |
| 411 | // If we need to preserve LoopInfo, note whether any of the |
| 412 | // preds crosses an interesting loop boundary. |
| 413 | if (L) { |
| 414 | if (L->contains(Preds[i])) |
| 415 | IsLoopEntry = false; |
| 416 | else |
| 417 | SplitMakesNewLoopHeader = true; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 422 | // Update dominator tree and dominator frontier if available. |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 423 | DominatorTree *DT = P ? P->getAnalysisIfAvailable<DominatorTree>() : 0; |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 424 | if (DT) |
| 425 | DT->splitBlock(NewBB); |
Duncan Sands | 1465d61 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 426 | if (DominanceFrontier *DF = P ? P->getAnalysisIfAvailable<DominanceFrontier>():0) |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 427 | DF->splitBlock(NewBB); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 428 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 429 | // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI |
| 430 | // node becomes an incoming value for BB's phi node. However, if the Preds |
| 431 | // list is empty, we need to insert dummy entries into the PHI nodes in BB to |
| 432 | // account for the newly created predecessor. |
| 433 | if (NumPreds == 0) { |
| 434 | // Insert dummy values as the incoming value. |
| 435 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I) |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 436 | cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 437 | return NewBB; |
| 438 | } |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 439 | |
| 440 | AliasAnalysis *AA = P ? P->getAnalysisIfAvailable<AliasAnalysis>() : 0; |
| 441 | |
| 442 | if (L) { |
| 443 | if (IsLoopEntry) { |
Dan Gohman | 841a147 | 2009-10-19 16:04:50 +0000 | [diff] [blame] | 444 | // Add the new block to the nearest enclosing loop (and not an |
| 445 | // adjacent loop). To find this, examine each of the predecessors and |
| 446 | // determine which loops enclose them, and select the most-nested loop |
| 447 | // which contains the loop containing the block being split. |
| 448 | Loop *InnermostPredLoop = 0; |
| 449 | for (unsigned i = 0; i != NumPreds; ++i) |
| 450 | if (Loop *PredLoop = LI->getLoopFor(Preds[i])) { |
| 451 | // Seek a loop which actually contains the block being split (to |
| 452 | // avoid adjacent loops). |
| 453 | while (PredLoop && !PredLoop->contains(BB)) |
| 454 | PredLoop = PredLoop->getParentLoop(); |
| 455 | // Select the most-nested of these loops which contains the block. |
| 456 | if (PredLoop && |
| 457 | PredLoop->contains(BB) && |
| 458 | (!InnermostPredLoop || |
| 459 | InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth())) |
| 460 | InnermostPredLoop = PredLoop; |
| 461 | } |
| 462 | if (InnermostPredLoop) |
| 463 | InnermostPredLoop->addBasicBlockToLoop(NewBB, LI->getBase()); |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 464 | } else { |
| 465 | L->addBasicBlockToLoop(NewBB, LI->getBase()); |
| 466 | if (SplitMakesNewLoopHeader) |
| 467 | L->moveToHeader(NewBB); |
| 468 | } |
| 469 | } |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 470 | |
| 471 | // Otherwise, create a new PHI node in NewBB for each PHI node in BB. |
| 472 | for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ) { |
| 473 | PHINode *PN = cast<PHINode>(I++); |
| 474 | |
| 475 | // Check to see if all of the values coming in are the same. If so, we |
Dan Gohman | 5c89b52 | 2009-09-08 15:45:00 +0000 | [diff] [blame] | 476 | // don't need to create a new PHI node, unless it's needed for LCSSA. |
| 477 | Value *InVal = 0; |
| 478 | if (!HasLoopExit) { |
| 479 | InVal = PN->getIncomingValueForBlock(Preds[0]); |
| 480 | for (unsigned i = 1; i != NumPreds; ++i) |
| 481 | if (InVal != PN->getIncomingValueForBlock(Preds[i])) { |
| 482 | InVal = 0; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 487 | if (InVal) { |
| 488 | // If all incoming values for the new PHI would be the same, just don't |
| 489 | // make a new PHI. Instead, just remove the incoming values from the old |
| 490 | // PHI. |
| 491 | for (unsigned i = 0; i != NumPreds; ++i) |
| 492 | PN->removeIncomingValue(Preds[i], false); |
| 493 | } else { |
| 494 | // If the values coming into the block are not the same, we need a PHI. |
| 495 | // Create the new PHI node, insert it into NewBB at the end of the block |
| 496 | PHINode *NewPHI = |
| 497 | PHINode::Create(PN->getType(), PN->getName()+".ph", BI); |
| 498 | if (AA) AA->copyValue(PN, NewPHI); |
| 499 | |
| 500 | // Move all of the PHI values for 'Preds' to the new PHI. |
| 501 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 502 | Value *V = PN->removeIncomingValue(Preds[i], false); |
| 503 | NewPHI->addIncoming(V, Preds[i]); |
| 504 | } |
| 505 | InVal = NewPHI; |
| 506 | } |
| 507 | |
| 508 | // Add an incoming value to the PHI node in the loop for the preheader |
| 509 | // edge. |
| 510 | PN->addIncoming(InVal, NewBB); |
Chris Lattner | 54b9c3b | 2008-04-21 01:28:02 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | return NewBB; |
| 514 | } |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 515 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 516 | /// FindFunctionBackedges - Analyze the specified function to find all of the |
| 517 | /// loop backedges in the function and return them. This is a relatively cheap |
| 518 | /// (compared to computing dominators and loop info) analysis. |
| 519 | /// |
| 520 | /// The output is added to Result, as pairs of <from,to> edge info. |
| 521 | void llvm::FindFunctionBackedges(const Function &F, |
| 522 | SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) { |
| 523 | const BasicBlock *BB = &F.getEntryBlock(); |
| 524 | if (succ_begin(BB) == succ_end(BB)) |
| 525 | return; |
| 526 | |
| 527 | SmallPtrSet<const BasicBlock*, 8> Visited; |
| 528 | SmallVector<std::pair<const BasicBlock*, succ_const_iterator>, 8> VisitStack; |
| 529 | SmallPtrSet<const BasicBlock*, 8> InStack; |
| 530 | |
| 531 | Visited.insert(BB); |
| 532 | VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); |
| 533 | InStack.insert(BB); |
| 534 | do { |
| 535 | std::pair<const BasicBlock*, succ_const_iterator> &Top = VisitStack.back(); |
| 536 | const BasicBlock *ParentBB = Top.first; |
| 537 | succ_const_iterator &I = Top.second; |
| 538 | |
| 539 | bool FoundNew = false; |
| 540 | while (I != succ_end(ParentBB)) { |
| 541 | BB = *I++; |
| 542 | if (Visited.insert(BB)) { |
| 543 | FoundNew = true; |
| 544 | break; |
| 545 | } |
| 546 | // Successor is in VisitStack, it's a back edge. |
| 547 | if (InStack.count(BB)) |
| 548 | Result.push_back(std::make_pair(ParentBB, BB)); |
| 549 | } |
| 550 | |
| 551 | if (FoundNew) { |
| 552 | // Go down one level if there is a unvisited successor. |
| 553 | InStack.insert(BB); |
| 554 | VisitStack.push_back(std::make_pair(BB, succ_begin(BB))); |
| 555 | } else { |
| 556 | // Go up one level. |
| 557 | InStack.erase(VisitStack.pop_back_val().first); |
| 558 | } |
| 559 | } while (!VisitStack.empty()); |
| 560 | |
| 561 | |
| 562 | } |
| 563 | |
| 564 | |
| 565 | |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 566 | /// AreEquivalentAddressValues - Test if A and B will obviously have the same |
| 567 | /// value. This includes recognizing that %t0 and %t1 will have the same |
| 568 | /// value in code like this: |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 569 | /// %t0 = getelementptr \@a, 0, 3 |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 570 | /// store i32 0, i32* %t0 |
Dan Gohman | 0f8b53f | 2009-03-03 02:55:14 +0000 | [diff] [blame] | 571 | /// %t1 = getelementptr \@a, 0, 3 |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 572 | /// %t2 = load i32* %t1 |
| 573 | /// |
| 574 | static bool AreEquivalentAddressValues(const Value *A, const Value *B) { |
| 575 | // Test if the values are trivially equivalent. |
| 576 | if (A == B) return true; |
| 577 | |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 578 | // Test if the values come from identical arithmetic instructions. |
| 579 | // Use isIdenticalToWhenDefined instead of isIdenticalTo because |
| 580 | // this function is only used when one address use dominates the |
| 581 | // other, which means that they'll always either have the same |
| 582 | // value or one of them will have an undefined value. |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 583 | if (isa<BinaryOperator>(A) || isa<CastInst>(A) || |
| 584 | isa<PHINode>(A) || isa<GetElementPtrInst>(A)) |
| 585 | if (const Instruction *BI = dyn_cast<Instruction>(B)) |
Dan Gohman | 58cfa3b | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 586 | if (cast<Instruction>(A)->isIdenticalToWhenDefined(BI)) |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 587 | return true; |
| 588 | |
| 589 | // Otherwise they may not be equivalent. |
| 590 | return false; |
| 591 | } |
| 592 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 593 | /// FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the |
| 594 | /// instruction before ScanFrom) checking to see if we have the value at the |
| 595 | /// memory address *Ptr locally available within a small number of instructions. |
| 596 | /// If the value is available, return it. |
| 597 | /// |
| 598 | /// If not, return the iterator for the last validated instruction that the |
| 599 | /// value would be live through. If we scanned the entire block and didn't find |
| 600 | /// something that invalidates *Ptr or provides it, ScanFrom would be left at |
| 601 | /// begin() and this returns null. ScanFrom could also be left |
| 602 | /// |
| 603 | /// MaxInstsToScan specifies the maximum instructions to scan in the block. If |
| 604 | /// it is set to 0, it will scan the whole block. You can also optionally |
| 605 | /// specify an alias analysis implementation, which makes this more precise. |
| 606 | Value *llvm::FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, |
| 607 | BasicBlock::iterator &ScanFrom, |
| 608 | unsigned MaxInstsToScan, |
| 609 | AliasAnalysis *AA) { |
| 610 | if (MaxInstsToScan == 0) MaxInstsToScan = ~0U; |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 611 | |
| 612 | // If we're using alias analysis to disambiguate get the size of *Ptr. |
| 613 | unsigned AccessSize = 0; |
| 614 | if (AA) { |
| 615 | const Type *AccessTy = cast<PointerType>(Ptr->getType())->getElementType(); |
Dan Gohman | fc2a3ed | 2009-07-25 00:48:42 +0000 | [diff] [blame] | 616 | AccessSize = AA->getTypeStoreSize(AccessTy); |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 617 | } |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 618 | |
| 619 | while (ScanFrom != ScanBB->begin()) { |
Dale Johannesen | bd8e650 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 620 | // We must ignore debug info directives when counting (otherwise they |
| 621 | // would affect codegen). |
| 622 | Instruction *Inst = --ScanFrom; |
| 623 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 624 | continue; |
Dale Johannesen | 4ded40a | 2009-03-03 22:36:47 +0000 | [diff] [blame] | 625 | |
Dale Johannesen | bd8e650 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 626 | // Restore ScanFrom to expected value in case next test succeeds |
| 627 | ScanFrom++; |
| 628 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 629 | // Don't scan huge blocks. |
| 630 | if (MaxInstsToScan-- == 0) return 0; |
| 631 | |
Dale Johannesen | bd8e650 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 632 | --ScanFrom; |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 633 | // If this is a load of Ptr, the loaded value is available. |
| 634 | if (LoadInst *LI = dyn_cast<LoadInst>(Inst)) |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 635 | if (AreEquivalentAddressValues(LI->getOperand(0), Ptr)) |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 636 | return LI; |
| 637 | |
| 638 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 639 | // If this is a store through Ptr, the value is available! |
Chris Lattner | 4aebaee | 2008-11-27 08:56:30 +0000 | [diff] [blame] | 640 | if (AreEquivalentAddressValues(SI->getOperand(1), Ptr)) |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 641 | return SI->getOperand(0); |
| 642 | |
| 643 | // If Ptr is an alloca and this is a store to a different alloca, ignore |
| 644 | // the store. This is a trivial form of alias analysis that is important |
| 645 | // for reg2mem'd code. |
| 646 | if ((isa<AllocaInst>(Ptr) || isa<GlobalVariable>(Ptr)) && |
| 647 | (isa<AllocaInst>(SI->getOperand(1)) || |
| 648 | isa<GlobalVariable>(SI->getOperand(1)))) |
| 649 | continue; |
| 650 | |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 651 | // If we have alias analysis and it says the store won't modify the loaded |
| 652 | // value, ignore the store. |
| 653 | if (AA && |
| 654 | (AA->getModRefInfo(SI, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) |
| 655 | continue; |
| 656 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 657 | // Otherwise the store that may or may not alias the pointer, bail out. |
| 658 | ++ScanFrom; |
| 659 | return 0; |
| 660 | } |
| 661 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 662 | // If this is some other instruction that may clobber Ptr, bail out. |
| 663 | if (Inst->mayWriteToMemory()) { |
Chris Lattner | ee6e10b | 2008-11-27 08:18:12 +0000 | [diff] [blame] | 664 | // If alias analysis claims that it really won't modify the load, |
| 665 | // ignore it. |
| 666 | if (AA && |
| 667 | (AA->getModRefInfo(Inst, Ptr, AccessSize) & AliasAnalysis::Mod) == 0) |
| 668 | continue; |
| 669 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 670 | // May modify the pointer, bail out. |
| 671 | ++ScanFrom; |
| 672 | return 0; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | // Got to the start of the block, we didn't find it, but are done for this |
| 677 | // block. |
| 678 | return 0; |
| 679 | } |
Dale Johannesen | bd8e650 | 2009-03-03 01:09:07 +0000 | [diff] [blame] | 680 | |