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 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "loop-idiom" |
| 17 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 2e12f1a | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/LoopPass.h" |
| 20 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ValueTracking.h" |
| 23 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9f39188 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 26 | #include "llvm/Support/IRBuilder.h" |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | // TODO: Recognize "N" size array multiplies: replace with call to blas or |
| 32 | // something. |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 33 | STATISTIC(NumMemSet, "Number of memset's formed from loop stores"); |
| 34 | STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores"); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | class LoopIdiomRecognize : public LoopPass { |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 38 | Loop *CurLoop; |
| 39 | const TargetData *TD; |
| 40 | ScalarEvolution *SE; |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 41 | public: |
| 42 | static char ID; |
| 43 | explicit LoopIdiomRecognize() : LoopPass(ID) { |
| 44 | initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry()); |
| 45 | } |
| 46 | |
| 47 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
| 48 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 49 | bool processLoopStore(StoreInst *SI, const SCEV *BECount); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 50 | |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 51 | bool processLoopStoreOfSplatValue(StoreInst *SI, unsigned StoreSize, |
| 52 | Value *SplatValue, |
| 53 | const SCEVAddRecExpr *Ev, |
| 54 | const SCEV *BECount); |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 55 | bool processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize, |
| 56 | const SCEVAddRecExpr *StoreEv, |
| 57 | const SCEVAddRecExpr *LoadEv, |
| 58 | const SCEV *BECount); |
| 59 | |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 60 | /// This transformation requires natural loop information & requires that |
| 61 | /// loop preheaders be inserted into the CFG. |
| 62 | /// |
| 63 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 64 | AU.addRequired<LoopInfo>(); |
| 65 | AU.addPreserved<LoopInfo>(); |
| 66 | AU.addRequiredID(LoopSimplifyID); |
| 67 | AU.addPreservedID(LoopSimplifyID); |
| 68 | AU.addRequiredID(LCSSAID); |
| 69 | AU.addPreservedID(LCSSAID); |
Chris Lattner | 2e12f1a | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 70 | AU.addRequired<AliasAnalysis>(); |
| 71 | AU.addPreserved<AliasAnalysis>(); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 72 | AU.addRequired<ScalarEvolution>(); |
| 73 | AU.addPreserved<ScalarEvolution>(); |
| 74 | AU.addPreserved<DominatorTree>(); |
| 75 | } |
| 76 | }; |
| 77 | } |
| 78 | |
| 79 | char LoopIdiomRecognize::ID = 0; |
| 80 | INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms", |
| 81 | false, false) |
| 82 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 83 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 84 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 85 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
Chris Lattner | 2e12f1a | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 86 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 87 | INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms", |
| 88 | false, false) |
| 89 | |
| 90 | Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); } |
| 91 | |
Chris Lattner | 9f39188 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 92 | /// DeleteDeadInstruction - Delete this instruction. Before we do, go through |
| 93 | /// and zero out all the operands of this instruction. If any of them become |
| 94 | /// dead, delete them and the computation tree that feeds them. |
| 95 | /// |
| 96 | static void DeleteDeadInstruction(Instruction *I, ScalarEvolution &SE) { |
| 97 | SmallVector<Instruction*, 32> NowDeadInsts; |
| 98 | |
| 99 | NowDeadInsts.push_back(I); |
| 100 | |
| 101 | // Before we touch this instruction, remove it from SE! |
| 102 | do { |
| 103 | Instruction *DeadInst = NowDeadInsts.pop_back_val(); |
| 104 | |
| 105 | // This instruction is dead, zap it, in stages. Start by removing it from |
| 106 | // SCEV. |
| 107 | SE.forgetValue(DeadInst); |
| 108 | |
| 109 | for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) { |
| 110 | Value *Op = DeadInst->getOperand(op); |
| 111 | DeadInst->setOperand(op, 0); |
| 112 | |
| 113 | // If this operand just became dead, add it to the NowDeadInsts list. |
| 114 | if (!Op->use_empty()) continue; |
| 115 | |
| 116 | if (Instruction *OpI = dyn_cast<Instruction>(Op)) |
| 117 | if (isInstructionTriviallyDead(OpI)) |
| 118 | NowDeadInsts.push_back(OpI); |
| 119 | } |
| 120 | |
| 121 | DeadInst->eraseFromParent(); |
| 122 | |
| 123 | } while (!NowDeadInsts.empty()); |
| 124 | } |
| 125 | |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 126 | bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) { |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 127 | CurLoop = L; |
| 128 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 129 | // The trip count of the loop must be analyzable. |
| 130 | SE = &getAnalysis<ScalarEvolution>(); |
| 131 | if (!SE->hasLoopInvariantBackedgeTakenCount(L)) |
| 132 | return false; |
| 133 | const SCEV *BECount = SE->getBackedgeTakenCount(L); |
| 134 | if (isa<SCEVCouldNotCompute>(BECount)) return false; |
| 135 | |
| 136 | // We require target data for now. |
| 137 | TD = getAnalysisIfAvailable<TargetData>(); |
| 138 | if (TD == 0) return false; |
| 139 | |
Chris Lattner | cf078f2 | 2011-01-02 07:58:36 +0000 | [diff] [blame] | 140 | // TODO: We currently only scan the header of the loop, because it is the only |
| 141 | // part that is known to execute and we don't want to make a conditional store |
| 142 | // into an unconditional one in the preheader. However, there can be diamonds |
| 143 | // and other things in the loop that would make other blocks "always executed" |
| 144 | // we should get the full set and scan each block. |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 145 | BasicBlock *BB = L->getHeader(); |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 146 | DEBUG(dbgs() << "loop-idiom Scanning: F[" << BB->getParent()->getName() |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 147 | << "] Loop %" << BB->getName() << "\n"); |
| 148 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 149 | bool MadeChange = false; |
| 150 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 151 | // Look for store instructions, which may be memsets. |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 152 | StoreInst *SI = dyn_cast<StoreInst>(I++); |
| 153 | if (SI == 0 || SI->isVolatile()) continue; |
| 154 | |
Chris Lattner | 2e12f1a | 2010-12-27 18:39:08 +0000 | [diff] [blame] | 155 | WeakVH InstPtr(SI); |
| 156 | if (!processLoopStore(SI, BECount)) continue; |
| 157 | |
| 158 | MadeChange = true; |
| 159 | |
| 160 | // If processing the store invalidated our iterator, start over from the |
| 161 | // head of the loop. |
| 162 | if (InstPtr == 0) |
| 163 | I = BB->begin(); |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return MadeChange; |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /// 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] | 170 | bool LoopIdiomRecognize::processLoopStore(StoreInst *SI, const SCEV *BECount) { |
| 171 | Value *StoredVal = SI->getValueOperand(); |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 172 | Value *StorePtr = SI->getPointerOperand(); |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 95ae676 | 2010-12-28 18:53:48 +0000 | [diff] [blame] | 174 | // Reject stores that are so large that they overflow an unsigned. |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 175 | uint64_t SizeInBits = TD->getTypeSizeInBits(StoredVal->getType()); |
Chris Lattner | 95ae676 | 2010-12-28 18:53:48 +0000 | [diff] [blame] | 176 | if ((SizeInBits & 7) || (SizeInBits >> 32) != 0) |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 177 | return false; |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 179 | // See if the pointer expression is an AddRec like {base,+,1} on the current |
| 180 | // loop, which indicates a strided store. If we have something else, it's a |
| 181 | // random store we can't handle. |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 182 | const SCEVAddRecExpr *StoreEv = |
| 183 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr)); |
| 184 | if (StoreEv == 0 || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine()) |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 185 | return false; |
| 186 | |
| 187 | // Check to see if the stride matches the size of the store. If so, then we |
| 188 | // know that every byte is touched in the loop. |
| 189 | unsigned StoreSize = (unsigned)SizeInBits >> 3; |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 190 | const SCEVConstant *Stride = dyn_cast<SCEVConstant>(StoreEv->getOperand(1)); |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 191 | |
| 192 | // TODO: Could also handle negative stride here someday, that will require the |
| 193 | // validity check in mayLoopModRefLocation to be updated though. |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 194 | if (Stride == 0 || StoreSize != Stride->getValue()->getValue()) |
| 195 | return false; |
| 196 | |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 197 | // If the stored value is a byte-wise value (like i32 -1), then it may be |
| 198 | // turned into a memset of i8 -1, assuming that all the consequtive bytes |
| 199 | // 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] | 200 | if (Value *SplatValue = isBytewiseValue(StoredVal)) |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 201 | if (processLoopStoreOfSplatValue(SI, StoreSize, SplatValue, StoreEv, |
| 202 | BECount)) |
| 203 | return true; |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 204 | |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 205 | // If the stored value is a strided load in the same loop with the same stride |
| 206 | // this this may be transformable into a memcpy. This kicks in for stuff like |
| 207 | // for (i) A[i] = B[i]; |
| 208 | if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) { |
| 209 | const SCEVAddRecExpr *LoadEv = |
| 210 | dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getOperand(0))); |
| 211 | if (LoadEv && LoadEv->getLoop() == CurLoop && LoadEv->isAffine() && |
| 212 | StoreEv->getOperand(1) == LoadEv->getOperand(1) && !LI->isVolatile()) |
| 213 | if (processLoopStoreOfLoopLoad(SI, StoreSize, StoreEv, LoadEv, BECount)) |
| 214 | return true; |
| 215 | } |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 216 | //errs() << "UNHANDLED strided store: " << *StoreEv << " - " << *SI << "\n"; |
Chris Lattner | 22920b5 | 2010-12-26 20:45:45 +0000 | [diff] [blame] | 217 | |
Chris Lattner | e6bb649 | 2010-12-26 19:39:38 +0000 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 221 | /// mayLoopModRefLocation - Return true if the specified loop might do a load or |
| 222 | /// store to the same location that the specified store could store to, which is |
| 223 | /// a loop-strided access. |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 224 | static bool mayLoopModRefLocation(Value *Ptr, Loop *L, const SCEV *BECount, |
| 225 | unsigned StoreSize, AliasAnalysis &AA, |
| 226 | StoreInst *IgnoredStore) { |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 227 | // Get the location that may be stored across the loop. Since the access is |
| 228 | // strided positively through memory, we say that the modified location starts |
| 229 | // at the pointer and has infinite size. |
Chris Lattner | a64cbf0 | 2011-01-01 19:54:22 +0000 | [diff] [blame] | 230 | uint64_t AccessSize = AliasAnalysis::UnknownSize; |
| 231 | |
| 232 | // If the loop iterates a fixed number of times, we can refine the access size |
| 233 | // to be exactly the size of the memset, which is (BECount+1)*StoreSize |
| 234 | if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount)) |
| 235 | AccessSize = (BECst->getValue()->getZExtValue()+1)*StoreSize; |
| 236 | |
| 237 | // TODO: For this to be really effective, we have to dive into the pointer |
| 238 | // operand in the store. Store to &A[i] of 100 will always return may alias |
| 239 | // with store of &A[100], we need to StoreLoc to be "A" with size of 100, |
| 240 | // which will then no-alias a store to &A[100]. |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 241 | AliasAnalysis::Location StoreLoc(Ptr, AccessSize); |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 242 | |
| 243 | for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E; |
| 244 | ++BI) |
| 245 | for (BasicBlock::iterator I = (*BI)->begin(), E = (*BI)->end(); I != E; ++I) |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 246 | if (&*I != IgnoredStore && |
| 247 | AA.getModRefInfo(I, StoreLoc) != AliasAnalysis::NoModRef) |
Chris Lattner | 30980b6 | 2011-01-01 19:39:01 +0000 | [diff] [blame] | 248 | return true; |
| 249 | |
| 250 | return false; |
| 251 | } |
| 252 | |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 253 | /// processLoopStoreOfSplatValue - We see a strided store of a memsetable value. |
| 254 | /// If we can transform this into a memset in the loop preheader, do so. |
| 255 | bool LoopIdiomRecognize:: |
| 256 | processLoopStoreOfSplatValue(StoreInst *SI, unsigned StoreSize, |
| 257 | Value *SplatValue, |
| 258 | const SCEVAddRecExpr *Ev, const SCEV *BECount) { |
Chris Lattner | bafa117 | 2011-01-01 20:12:04 +0000 | [diff] [blame] | 259 | // Verify that the stored value is loop invariant. If not, we can't promote |
| 260 | // the memset. |
| 261 | if (!CurLoop->isLoopInvariant(SplatValue)) |
| 262 | return false; |
| 263 | |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 264 | // Okay, we have a strided store "p[i]" of a splattable value. We can turn |
| 265 | // this into a memset in the loop preheader now if we want. However, this |
| 266 | // would be unsafe to do if there is anything else in the loop that may read |
| 267 | // or write to the aliased location. Check for an alias. |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 268 | if (mayLoopModRefLocation(SI->getPointerOperand(), CurLoop, BECount, |
| 269 | StoreSize, getAnalysis<AliasAnalysis>(), SI)) |
| 270 | return false; |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 271 | |
| 272 | // Okay, everything looks good, insert the memset. |
| 273 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); |
| 274 | |
| 275 | IRBuilder<> Builder(Preheader->getTerminator()); |
| 276 | |
| 277 | // The trip count of the loop and the base pointer of the addrec SCEV is |
| 278 | // guaranteed to be loop invariant, which means that it should dominate the |
| 279 | // header. Just insert code for it in the preheader. |
| 280 | SCEVExpander Expander(*SE); |
| 281 | |
| 282 | unsigned AddrSpace = SI->getPointerAddressSpace(); |
| 283 | Value *BasePtr = |
| 284 | Expander.expandCodeFor(Ev->getStart(), Builder.getInt8PtrTy(AddrSpace), |
| 285 | Preheader->getTerminator()); |
| 286 | |
| 287 | // The # stored bytes is (BECount+1)*Size. Expand the trip count out to |
| 288 | // pointer size if it isn't already. |
| 289 | const Type *IntPtr = TD->getIntPtrType(SI->getContext()); |
| 290 | unsigned BESize = SE->getTypeSizeInBits(BECount->getType()); |
| 291 | if (BESize < TD->getPointerSizeInBits()) |
| 292 | BECount = SE->getZeroExtendExpr(BECount, IntPtr); |
| 293 | else if (BESize > TD->getPointerSizeInBits()) |
| 294 | BECount = SE->getTruncateExpr(BECount, IntPtr); |
| 295 | |
| 296 | const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1), |
| 297 | true, true /*nooverflow*/); |
| 298 | if (StoreSize != 1) |
| 299 | NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize), |
| 300 | true, true /*nooverflow*/); |
| 301 | |
| 302 | Value *NumBytes = |
| 303 | Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator()); |
| 304 | |
| 305 | Value *NewCall = |
| 306 | Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, SI->getAlignment()); |
| 307 | |
| 308 | DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n" |
| 309 | << " from store to: " << *Ev << " at: " << *SI << "\n"); |
Duncan Sands | 7922d34 | 2010-12-28 09:41:15 +0000 | [diff] [blame] | 310 | (void)NewCall; |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 9f39188 | 2010-12-27 00:03:23 +0000 | [diff] [blame] | 312 | // Okay, the memset has been formed. Zap the original store and anything that |
| 313 | // feeds into it. |
| 314 | DeleteDeadInstruction(SI, *SE); |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 315 | ++NumMemSet; |
Chris Lattner | a92ff91 | 2010-12-26 23:42:51 +0000 | [diff] [blame] | 316 | return true; |
| 317 | } |
| 318 | |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 319 | /// processLoopStoreOfLoopLoad - We see a strided store whose value is a |
| 320 | /// same-strided load. |
| 321 | bool LoopIdiomRecognize:: |
| 322 | processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize, |
| 323 | const SCEVAddRecExpr *StoreEv, |
| 324 | const SCEVAddRecExpr *LoadEv, |
| 325 | const SCEV *BECount) { |
| 326 | LoadInst *LI = cast<LoadInst>(SI->getValueOperand()); |
| 327 | |
| 328 | // Okay, we have a strided store "p[i]" of a loaded value. We can turn |
| 329 | // this into a memcmp in the loop preheader now if we want. However, this |
| 330 | // would be unsafe to do if there is anything else in the loop that may read |
| 331 | // or write to the aliased location (including the load feeding the stores). |
| 332 | // Check for an alias. |
| 333 | if (mayLoopModRefLocation(SI->getPointerOperand(), CurLoop, BECount, |
| 334 | StoreSize, getAnalysis<AliasAnalysis>(), SI)) |
| 335 | return false; |
| 336 | |
| 337 | // Okay, everything looks good, insert the memcpy. |
| 338 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); |
| 339 | |
| 340 | IRBuilder<> Builder(Preheader->getTerminator()); |
| 341 | |
| 342 | // The trip count of the loop and the base pointer of the addrec SCEV is |
| 343 | // guaranteed to be loop invariant, which means that it should dominate the |
| 344 | // header. Just insert code for it in the preheader. |
| 345 | SCEVExpander Expander(*SE); |
| 346 | |
| 347 | Value *LoadBasePtr = |
| 348 | Expander.expandCodeFor(LoadEv->getStart(), |
| 349 | Builder.getInt8PtrTy(LI->getPointerAddressSpace()), |
| 350 | Preheader->getTerminator()); |
| 351 | Value *StoreBasePtr = |
| 352 | Expander.expandCodeFor(StoreEv->getStart(), |
| 353 | Builder.getInt8PtrTy(SI->getPointerAddressSpace()), |
| 354 | Preheader->getTerminator()); |
| 355 | |
| 356 | // The # stored bytes is (BECount+1)*Size. Expand the trip count out to |
| 357 | // pointer size if it isn't already. |
| 358 | const Type *IntPtr = TD->getIntPtrType(SI->getContext()); |
| 359 | unsigned BESize = SE->getTypeSizeInBits(BECount->getType()); |
| 360 | if (BESize < TD->getPointerSizeInBits()) |
| 361 | BECount = SE->getZeroExtendExpr(BECount, IntPtr); |
| 362 | else if (BESize > TD->getPointerSizeInBits()) |
| 363 | BECount = SE->getTruncateExpr(BECount, IntPtr); |
| 364 | |
| 365 | const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1), |
| 366 | true, true /*nooverflow*/); |
| 367 | if (StoreSize != 1) |
| 368 | NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize), |
| 369 | true, true /*nooverflow*/); |
| 370 | |
| 371 | Value *NumBytes = |
| 372 | Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator()); |
| 373 | |
| 374 | Value *NewCall = |
| 375 | Builder.CreateMemCpy(StoreBasePtr, LoadBasePtr, NumBytes, |
| 376 | std::min(SI->getAlignment(), LI->getAlignment())); |
| 377 | |
| 378 | DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n" |
| 379 | << " from load ptr=" << *LoadEv << " at: " << *LI << "\n" |
| 380 | << " from store ptr=" << *StoreEv << " at: " << *SI << "\n"); |
| 381 | (void)NewCall; |
| 382 | |
| 383 | // Okay, the memset has been formed. Zap the original store and anything that |
| 384 | // feeds into it. |
| 385 | DeleteDeadInstruction(SI, *SE); |
Chris Lattner | 4ce31fb | 2011-01-02 07:36:44 +0000 | [diff] [blame] | 386 | ++NumMemCpy; |
Chris Lattner | e2c4392 | 2011-01-02 03:37:56 +0000 | [diff] [blame] | 387 | return true; |
| 388 | } |