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