Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 1 | //===-- LoopIdiomRecognize.cpp - Loop idiom recognition -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass implements an idiom recognizer that transforms simple loops into a |
| 11 | // non-loop form. In cases that this kicks in, it can be a significant |
| 12 | // performance win. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
Chris Lattner | bdce572 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 15 | // |
| 16 | // TODO List: |
| 17 | // |
| 18 | // Future loop memory idioms to recognize: |
| 19 | // memcmp, memmove, strlen, etc. |
| 20 | // Future floating point idioms to recognize in -ffast-math mode: |
| 21 | // fpowi |
| 22 | // Future integer operation idioms to recognize: |
| 23 | // ctpop, ctlz, cttz |
| 24 | // |
| 25 | // Beware that isel's default lowering for ctpop is highly inefficient for |
| 26 | // i64 and larger types when i64 is legal and the value has few bits set. It |
| 27 | // would be good to enhance isel to emit a loop for ctpop in this case. |
| 28 | // |
| 29 | // We should enhance the memset/memcpy recognition to handle multiple stores in |
| 30 | // the loop. This would handle things like: |
| 31 | // void foo(_Complex float *P) |
| 32 | // for (i) { __real__(*P) = 0; __imag__(*P) = 0; } |
| 33 | // this is also "Example 2" from http://blog.regehr.org/archives/320 |
Chris Lattner | 91139cc | 2011-01-02 23:19:45 +0000 | [diff] [blame^] | 34 | // |
| 35 | // This could regognize common matrix multiplies and dot product idioms and |
| 36 | // replace them with calls to BLAS (if linked in??). |
| 37 | // |
Chris Lattner | bdce572 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 38 | //===----------------------------------------------------------------------===// |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 39 | |
| 40 | #define DEBUG_TYPE "loop-idiom" |
| 41 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 2e12f1a | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/LoopPass.h" |
| 44 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/ValueTracking.h" |
| 47 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9f39188 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 48 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 50 | #include "llvm/Support/IRBuilder.h" |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 52 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 53 | using namespace llvm; |
| 54 | |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 55 | STATISTIC(NumMemSet, "Number of memset's formed from loop stores"); |
| 56 | STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores"); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 57 | |
| 58 | namespace { |
| 59 | class LoopIdiomRecognize : public LoopPass { |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 60 | Loop *CurLoop; |
| 61 | const TargetData *TD; |
Chris Lattner | 62c50fd | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 62 | DominatorTree *DT; |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 63 | ScalarEvolution *SE; |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 64 | public: |
| 65 | static char ID; |
| 66 | explicit LoopIdiomRecognize() : LoopPass(ID) { |
| 67 | initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry()); |
| 68 | } |
| 69 | |
| 70 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
Chris Lattner | 62c50fd | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 71 | bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount, |
| 72 | SmallVectorImpl<BasicBlock*> &ExitBlocks); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 74 | bool processLoopStore(StoreInst *SI, const SCEV *BECount); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 75 | |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 76 | bool processLoopStoreOfSplatValue(StoreInst *SI, unsigned StoreSize, |
| 77 | Value *SplatValue, |
| 78 | const SCEVAddRecExpr *Ev, |
| 79 | const SCEV *BECount); |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 80 | bool processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize, |
| 81 | const SCEVAddRecExpr *StoreEv, |
| 82 | const SCEVAddRecExpr *LoadEv, |
| 83 | const SCEV *BECount); |
| 84 | |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 85 | /// This transformation requires natural loop information & requires that |
| 86 | /// loop preheaders be inserted into the CFG. |
| 87 | /// |
| 88 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 89 | AU.addRequired<LoopInfo>(); |
| 90 | AU.addPreserved<LoopInfo>(); |
| 91 | AU.addRequiredID(LoopSimplifyID); |
| 92 | AU.addPreservedID(LoopSimplifyID); |
| 93 | AU.addRequiredID(LCSSAID); |
| 94 | AU.addPreservedID(LCSSAID); |
Chris Lattner | 2e12f1a | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 95 | AU.addRequired<AliasAnalysis>(); |
| 96 | AU.addPreserved<AliasAnalysis>(); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 97 | AU.addRequired<ScalarEvolution>(); |
| 98 | AU.addPreserved<ScalarEvolution>(); |
| 99 | AU.addPreserved<DominatorTree>(); |
Chris Lattner | 62c50fd | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 100 | AU.addRequired<DominatorTree>(); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 101 | } |
| 102 | }; |
| 103 | } |
| 104 | |
| 105 | char LoopIdiomRecognize::ID = 0; |
| 106 | INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms", |
| 107 | false, false) |
| 108 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
Chris Lattner | 62c50fd | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 109 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 110 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 111 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 112 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Chris Lattner | 2e12f1a | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 113 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 114 | INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms", |
| 115 | false, false) |
| 116 | |
| 117 | Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); } |
| 118 | |
Chris Lattner | 9f39188 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 119 | /// DeleteDeadInstruction - Delete this instruction. Before we do, go through |
| 120 | /// and zero out all the operands of this instruction. If any of them become |
| 121 | /// dead, delete them and the computation tree that feeds them. |
| 122 | /// |
| 123 | static void DeleteDeadInstruction(Instruction *I, ScalarEvolution &SE) { |
| 124 | SmallVector<Instruction*, 32> NowDeadInsts; |
| 125 | |
| 126 | NowDeadInsts.push_back(I); |
| 127 | |
| 128 | // Before we touch this instruction, remove it from SE! |
| 129 | do { |
| 130 | Instruction *DeadInst = NowDeadInsts.pop_back_val(); |
| 131 | |
| 132 | // This instruction is dead, zap it, in stages. Start by removing it from |
| 133 | // SCEV. |
| 134 | SE.forgetValue(DeadInst); |
| 135 | |
| 136 | for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) { |
| 137 | Value *Op = DeadInst->getOperand(op); |
| 138 | DeadInst->setOperand(op, 0); |
| 139 | |
| 140 | // If this operand just became dead, add it to the NowDeadInsts list. |
| 141 | if (!Op->use_empty()) continue; |
| 142 | |
| 143 | if (Instruction *OpI = dyn_cast<Instruction>(Op)) |
| 144 | if (isInstructionTriviallyDead(OpI)) |
| 145 | NowDeadInsts.push_back(OpI); |
| 146 | } |
| 147 | |
| 148 | DeadInst->eraseFromParent(); |
| 149 | |
| 150 | } while (!NowDeadInsts.empty()); |
| 151 | } |
| 152 | |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 153 | bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) { |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 154 | CurLoop = L; |
| 155 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 156 | // The trip count of the loop must be analyzable. |
| 157 | SE = &getAnalysis<ScalarEvolution>(); |
| 158 | if (!SE->hasLoopInvariantBackedgeTakenCount(L)) |
| 159 | return false; |
| 160 | const SCEV *BECount = SE->getBackedgeTakenCount(L); |
| 161 | if (isa<SCEVCouldNotCompute>(BECount)) return false; |
| 162 | |
Chris Lattner | 8e08e73 | 2011-01-02 20:24:21 +0000 | [diff] [blame] | 163 | // If this loop executes exactly one time, then it should be peeled, not |
| 164 | // optimized by this pass. |
| 165 | if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount)) |
| 166 | if (BECst->getValue()->getValue() == 0) |
| 167 | return false; |
| 168 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 169 | // We require target data for now. |
| 170 | TD = getAnalysisIfAvailable<TargetData>(); |
| 171 | if (TD == 0) return false; |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 62c50fd | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 173 | DT = &getAnalysis<DominatorTree>(); |
| 174 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 175 | |
| 176 | SmallVector<BasicBlock*, 8> ExitBlocks; |
| 177 | CurLoop->getUniqueExitBlocks(ExitBlocks); |
| 178 | |
Chris Lattner | 63f9c3c | 2011-01-02 21:14:18 +0000 | [diff] [blame] | 179 | DEBUG(dbgs() << "loop-idiom Scanning: F[" |
| 180 | << L->getHeader()->getParent()->getName() |
| 181 | << "] Loop %" << L->getHeader()->getName() << "\n"); |
| 182 | |
Chris Lattner | 62c50fd | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 183 | bool MadeChange = false; |
| 184 | // Scan all the blocks in the loop that are not in subloops. |
| 185 | for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E; |
| 186 | ++BI) { |
| 187 | // Ignore blocks in subloops. |
| 188 | if (LI.getLoopFor(*BI) != CurLoop) |
| 189 | continue; |
| 190 | |
| 191 | MadeChange |= runOnLoopBlock(*BI, BECount, ExitBlocks); |
| 192 | } |
| 193 | return MadeChange; |
| 194 | } |
| 195 | |
| 196 | /// runOnLoopBlock - Process the specified block, which lives in a counted loop |
| 197 | /// with the specified backedge count. This block is known to be in the current |
| 198 | /// loop and not in any subloops. |
| 199 | bool LoopIdiomRecognize::runOnLoopBlock(BasicBlock *BB, const SCEV *BECount, |
| 200 | SmallVectorImpl<BasicBlock*> &ExitBlocks) { |
| 201 | // We can only promote stores in this block if they are unconditionally |
| 202 | // executed in the loop. For a block to be unconditionally executed, it has |
| 203 | // to dominate all the exit blocks of the loop. Verify this now. |
| 204 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) |
| 205 | if (!DT->dominates(BB, ExitBlocks[i])) |
| 206 | return false; |
| 207 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 208 | bool MadeChange = false; |
| 209 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 210 | // Look for store instructions, which may be memsets. |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 211 | StoreInst *SI = dyn_cast<StoreInst>(I++); |
| 212 | if (SI == 0 || SI->isVolatile()) continue; |
| 213 | |
Chris Lattner | 2e12f1a | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 214 | WeakVH InstPtr(SI); |
| 215 | if (!processLoopStore(SI, BECount)) continue; |
| 216 | |
| 217 | MadeChange = true; |
| 218 | |
| 219 | // If processing the store invalidated our iterator, start over from the |
| 220 | // head of the loop. |
| 221 | if (InstPtr == 0) |
| 222 | I = BB->begin(); |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return MadeChange; |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Chris Lattner | 62c50fd | 2011-01-02 19:01:03 +0000 | [diff] [blame] | 228 | |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 229 | /// scanBlock - Look over a block to see if we can promote anything out of it. |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 230 | bool LoopIdiomRecognize::processLoopStore(StoreInst *SI, const SCEV *BECount) { |
| 231 | Value *StoredVal = SI->getValueOperand(); |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 232 | Value *StorePtr = SI->getPointerOperand(); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 95ae676 | 2010-12-28 18:53:48 +0000 | [diff] [blame] | 234 | // Reject stores that are so large that they overflow an unsigned. |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 235 | uint64_t SizeInBits = TD->getTypeSizeInBits(StoredVal->getType()); |
Chris Lattner | 95ae676 | 2010-12-28 18:53:48 +0000 | [diff] [blame] | 236 | if ((SizeInBits & 7) || (SizeInBits >> 32) != 0) |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 237 | return false; |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 239 | // See if the pointer expression is an AddRec like {base,+,1} on the current |
| 240 | // loop, which indicates a strided store. If we have something else, it's a |
| 241 | // random store we can't handle. |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 242 | const SCEVAddRecExpr *StoreEv = |
| 243 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr)); |
| 244 | if (StoreEv == 0 || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine()) |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 245 | return false; |
| 246 | |
| 247 | // Check to see if the stride matches the size of the store. If so, then we |
| 248 | // know that every byte is touched in the loop. |
| 249 | unsigned StoreSize = (unsigned)SizeInBits >> 3; |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 250 | const SCEVConstant *Stride = dyn_cast<SCEVConstant>(StoreEv->getOperand(1)); |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 251 | |
| 252 | // TODO: Could also handle negative stride here someday, that will require the |
| 253 | // validity check in mayLoopModRefLocation to be updated though. |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 254 | if (Stride == 0 || StoreSize != Stride->getValue()->getValue()) |
| 255 | return false; |
| 256 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 257 | // If the stored value is a byte-wise value (like i32 -1), then it may be |
| 258 | // turned into a memset of i8 -1, assuming that all the consequtive bytes |
| 259 | // are stored. A store of i32 0x01020304 can never be turned into a memset. |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 260 | if (Value *SplatValue = isBytewiseValue(StoredVal)) |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 261 | if (processLoopStoreOfSplatValue(SI, StoreSize, SplatValue, StoreEv, |
| 262 | BECount)) |
| 263 | return true; |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 264 | |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 265 | // If the stored value is a strided load in the same loop with the same stride |
| 266 | // this this may be transformable into a memcpy. This kicks in for stuff like |
| 267 | // for (i) A[i] = B[i]; |
| 268 | if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) { |
| 269 | const SCEVAddRecExpr *LoadEv = |
| 270 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getOperand(0))); |
| 271 | if (LoadEv && LoadEv->getLoop() == CurLoop && LoadEv->isAffine() && |
| 272 | StoreEv->getOperand(1) == LoadEv->getOperand(1) && !LI->isVolatile()) |
| 273 | if (processLoopStoreOfLoopLoad(SI, StoreSize, StoreEv, LoadEv, BECount)) |
| 274 | return true; |
| 275 | } |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 276 | //errs() << "UNHANDLED strided store: " << *StoreEv << " - " << *SI << "\n"; |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 277 | |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 278 | return false; |
| 279 | } |
| 280 | |
Chris Lattner | 63f9c3c | 2011-01-02 21:14:18 +0000 | [diff] [blame] | 281 | /// mayLoopAccessLocation - Return true if the specified loop might access the |
| 282 | /// specified pointer location, which is a loop-strided access. The 'Access' |
| 283 | /// argument specifies what the verboten forms of access are (read or write). |
| 284 | static bool mayLoopAccessLocation(Value *Ptr,AliasAnalysis::ModRefResult Access, |
| 285 | Loop *L, const SCEV *BECount, |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 286 | unsigned StoreSize, AliasAnalysis &AA, |
| 287 | StoreInst *IgnoredStore) { |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 288 | // Get the location that may be stored across the loop. Since the access is |
| 289 | // strided positively through memory, we say that the modified location starts |
| 290 | // at the pointer and has infinite size. |
Chris Lattner | a64cbf0 | 2011-01-01 19:54:22 +0000 | [diff] [blame] | 291 | uint64_t AccessSize = AliasAnalysis::UnknownSize; |
| 292 | |
| 293 | // If the loop iterates a fixed number of times, we can refine the access size |
| 294 | // to be exactly the size of the memset, which is (BECount+1)*StoreSize |
| 295 | if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount)) |
| 296 | AccessSize = (BECst->getValue()->getZExtValue()+1)*StoreSize; |
| 297 | |
| 298 | // TODO: For this to be really effective, we have to dive into the pointer |
| 299 | // operand in the store. Store to &A[i] of 100 will always return may alias |
| 300 | // with store of &A[100], we need to StoreLoc to be "A" with size of 100, |
| 301 | // which will then no-alias a store to &A[100]. |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 302 | AliasAnalysis::Location StoreLoc(Ptr, AccessSize); |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 303 | |
| 304 | for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E; |
| 305 | ++BI) |
| 306 | for (BasicBlock::iterator I = (*BI)->begin(), E = (*BI)->end(); I != E; ++I) |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 307 | if (&*I != IgnoredStore && |
Chris Lattner | 63f9c3c | 2011-01-02 21:14:18 +0000 | [diff] [blame] | 308 | (AA.getModRefInfo(I, StoreLoc) & Access)) |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 309 | return true; |
| 310 | |
| 311 | return false; |
| 312 | } |
| 313 | |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 314 | /// processLoopStoreOfSplatValue - We see a strided store of a memsetable value. |
| 315 | /// If we can transform this into a memset in the loop preheader, do so. |
| 316 | bool LoopIdiomRecognize:: |
| 317 | processLoopStoreOfSplatValue(StoreInst *SI, unsigned StoreSize, |
| 318 | Value *SplatValue, |
| 319 | const SCEVAddRecExpr *Ev, const SCEV *BECount) { |
Chris Lattner | bafa117 | 2011-01-01 20:12:04 +0000 | [diff] [blame] | 320 | // Verify that the stored value is loop invariant. If not, we can't promote |
| 321 | // the memset. |
| 322 | if (!CurLoop->isLoopInvariant(SplatValue)) |
| 323 | return false; |
| 324 | |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 325 | // Okay, we have a strided store "p[i]" of a splattable value. We can turn |
| 326 | // this into a memset in the loop preheader now if we want. However, this |
| 327 | // would be unsafe to do if there is anything else in the loop that may read |
| 328 | // or write to the aliased location. Check for an alias. |
Chris Lattner | 63f9c3c | 2011-01-02 21:14:18 +0000 | [diff] [blame] | 329 | if (mayLoopAccessLocation(SI->getPointerOperand(), AliasAnalysis::ModRef, |
| 330 | CurLoop, BECount, |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 331 | StoreSize, getAnalysis<AliasAnalysis>(), SI)) |
| 332 | return false; |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 333 | |
| 334 | // Okay, everything looks good, insert the memset. |
| 335 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); |
| 336 | |
| 337 | IRBuilder<> Builder(Preheader->getTerminator()); |
| 338 | |
| 339 | // The trip count of the loop and the base pointer of the addrec SCEV is |
| 340 | // guaranteed to be loop invariant, which means that it should dominate the |
| 341 | // header. Just insert code for it in the preheader. |
| 342 | SCEVExpander Expander(*SE); |
| 343 | |
| 344 | unsigned AddrSpace = SI->getPointerAddressSpace(); |
| 345 | Value *BasePtr = |
| 346 | Expander.expandCodeFor(Ev->getStart(), Builder.getInt8PtrTy(AddrSpace), |
| 347 | Preheader->getTerminator()); |
| 348 | |
| 349 | // The # stored bytes is (BECount+1)*Size. Expand the trip count out to |
| 350 | // pointer size if it isn't already. |
| 351 | const Type *IntPtr = TD->getIntPtrType(SI->getContext()); |
| 352 | unsigned BESize = SE->getTypeSizeInBits(BECount->getType()); |
| 353 | if (BESize < TD->getPointerSizeInBits()) |
| 354 | BECount = SE->getZeroExtendExpr(BECount, IntPtr); |
| 355 | else if (BESize > TD->getPointerSizeInBits()) |
| 356 | BECount = SE->getTruncateExpr(BECount, IntPtr); |
| 357 | |
| 358 | const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1), |
| 359 | true, true /*nooverflow*/); |
| 360 | if (StoreSize != 1) |
| 361 | NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize), |
| 362 | true, true /*nooverflow*/); |
| 363 | |
| 364 | Value *NumBytes = |
| 365 | Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator()); |
| 366 | |
| 367 | Value *NewCall = |
| 368 | Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, SI->getAlignment()); |
| 369 | |
| 370 | DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n" |
| 371 | << " from store to: " << *Ev << " at: " << *SI << "\n"); |
Duncan Sands | 7922d34 | 2010-12-28 09:41:15 +0000 | [diff] [blame] | 372 | (void)NewCall; |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 373 | |
Chris Lattner | 9f39188 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 374 | // Okay, the memset has been formed. Zap the original store and anything that |
| 375 | // feeds into it. |
| 376 | DeleteDeadInstruction(SI, *SE); |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 377 | ++NumMemSet; |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 378 | return true; |
| 379 | } |
| 380 | |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 381 | /// processLoopStoreOfLoopLoad - We see a strided store whose value is a |
| 382 | /// same-strided load. |
| 383 | bool LoopIdiomRecognize:: |
| 384 | processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize, |
| 385 | const SCEVAddRecExpr *StoreEv, |
| 386 | const SCEVAddRecExpr *LoadEv, |
| 387 | const SCEV *BECount) { |
| 388 | LoadInst *LI = cast<LoadInst>(SI->getValueOperand()); |
| 389 | |
| 390 | // Okay, we have a strided store "p[i]" of a loaded value. We can turn |
Chris Lattner | bdce572 | 2011-01-02 18:32:09 +0000 | [diff] [blame] | 391 | // this into a memcpy in the loop preheader now if we want. However, this |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 392 | // would be unsafe to do if there is anything else in the loop that may read |
Chris Lattner | 63f9c3c | 2011-01-02 21:14:18 +0000 | [diff] [blame] | 393 | // or write to the stored location (including the load feeding the stores). |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 394 | // Check for an alias. |
Chris Lattner | 63f9c3c | 2011-01-02 21:14:18 +0000 | [diff] [blame] | 395 | if (mayLoopAccessLocation(SI->getPointerOperand(), AliasAnalysis::ModRef, |
| 396 | CurLoop, BECount, StoreSize, |
| 397 | getAnalysis<AliasAnalysis>(), SI)) |
| 398 | return false; |
| 399 | |
| 400 | // For a memcpy, we have to make sure that the input array is not being |
| 401 | // mutated by the loop. |
| 402 | if (mayLoopAccessLocation(LI->getPointerOperand(), AliasAnalysis::Mod, |
| 403 | CurLoop, BECount, StoreSize, |
| 404 | getAnalysis<AliasAnalysis>(), SI)) |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 405 | return false; |
| 406 | |
| 407 | // Okay, everything looks good, insert the memcpy. |
| 408 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); |
| 409 | |
| 410 | IRBuilder<> Builder(Preheader->getTerminator()); |
| 411 | |
| 412 | // The trip count of the loop and the base pointer of the addrec SCEV is |
| 413 | // guaranteed to be loop invariant, which means that it should dominate the |
| 414 | // header. Just insert code for it in the preheader. |
| 415 | SCEVExpander Expander(*SE); |
| 416 | |
| 417 | Value *LoadBasePtr = |
| 418 | Expander.expandCodeFor(LoadEv->getStart(), |
| 419 | Builder.getInt8PtrTy(LI->getPointerAddressSpace()), |
| 420 | Preheader->getTerminator()); |
| 421 | Value *StoreBasePtr = |
| 422 | Expander.expandCodeFor(StoreEv->getStart(), |
| 423 | Builder.getInt8PtrTy(SI->getPointerAddressSpace()), |
| 424 | Preheader->getTerminator()); |
| 425 | |
| 426 | // The # stored bytes is (BECount+1)*Size. Expand the trip count out to |
| 427 | // pointer size if it isn't already. |
| 428 | const Type *IntPtr = TD->getIntPtrType(SI->getContext()); |
| 429 | unsigned BESize = SE->getTypeSizeInBits(BECount->getType()); |
| 430 | if (BESize < TD->getPointerSizeInBits()) |
| 431 | BECount = SE->getZeroExtendExpr(BECount, IntPtr); |
| 432 | else if (BESize > TD->getPointerSizeInBits()) |
| 433 | BECount = SE->getTruncateExpr(BECount, IntPtr); |
| 434 | |
| 435 | const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1), |
| 436 | true, true /*nooverflow*/); |
| 437 | if (StoreSize != 1) |
| 438 | NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize), |
| 439 | true, true /*nooverflow*/); |
| 440 | |
| 441 | Value *NumBytes = |
| 442 | Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator()); |
| 443 | |
| 444 | Value *NewCall = |
| 445 | Builder.CreateMemCpy(StoreBasePtr, LoadBasePtr, NumBytes, |
| 446 | std::min(SI->getAlignment(), LI->getAlignment())); |
| 447 | |
| 448 | DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n" |
| 449 | << " from load ptr=" << *LoadEv << " at: " << *LI << "\n" |
| 450 | << " from store ptr=" << *StoreEv << " at: " << *SI << "\n"); |
| 451 | (void)NewCall; |
| 452 | |
| 453 | // Okay, the memset has been formed. Zap the original store and anything that |
| 454 | // feeds into it. |
| 455 | DeleteDeadInstruction(SI, *SE); |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 456 | ++NumMemCpy; |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 457 | return true; |
| 458 | } |