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