blob: a0e41d9a977216dc3cdf101c4f9126018c5c2e96 [file] [log] [blame]
Chris Lattnere6bb6492010-12-26 19:39:38 +00001//===-- 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 Lattnerbdce5722011-01-02 18:32:09 +000015//
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; }
Chris Lattner91139cc2011-01-02 23:19:45 +000033//
Chris Lattner408b5342011-02-21 02:08:54 +000034// We should enhance this to handle negative strides through memory.
35// Alternatively (and perhaps better) we could rely on an earlier pass to force
36// forward iteration through memory, which is generally better for cache
37// behavior. Negative strides *do* happen for memset/memcpy loops.
38//
Chris Lattnerd957c712011-01-03 01:10:08 +000039// This could recognize common matrix multiplies and dot product idioms and
Chris Lattner91139cc2011-01-02 23:19:45 +000040// replace them with calls to BLAS (if linked in??).
41//
Chris Lattnerbdce5722011-01-02 18:32:09 +000042//===----------------------------------------------------------------------===//
Chris Lattnere6bb6492010-12-26 19:39:38 +000043
44#define DEBUG_TYPE "loop-idiom"
45#include "llvm/Transforms/Scalar.h"
Chris Lattnere41d3c02011-01-04 07:46:33 +000046#include "llvm/IntrinsicInst.h"
Chris Lattner3a393722011-02-19 19:31:39 +000047#include "llvm/Module.h"
Chris Lattner2e12f1a2010-12-27 18:39:08 +000048#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattnere6bb6492010-12-26 19:39:38 +000049#include "llvm/Analysis/LoopPass.h"
50#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnera92ff912010-12-26 23:42:51 +000051#include "llvm/Analysis/ScalarEvolutionExpander.h"
Chris Lattner22920b52010-12-26 20:45:45 +000052#include "llvm/Analysis/ValueTracking.h"
53#include "llvm/Target/TargetData.h"
Chris Lattnerc19175c2011-02-18 22:22:15 +000054#include "llvm/Target/TargetLibraryInfo.h"
Chris Lattner9f391882010-12-27 00:03:23 +000055#include "llvm/Transforms/Utils/Local.h"
Chris Lattnere6bb6492010-12-26 19:39:38 +000056#include "llvm/Support/Debug.h"
Chris Lattnera92ff912010-12-26 23:42:51 +000057#include "llvm/Support/IRBuilder.h"
Chris Lattnere6bb6492010-12-26 19:39:38 +000058#include "llvm/Support/raw_ostream.h"
Chris Lattner4ce31fb2011-01-02 07:36:44 +000059#include "llvm/ADT/Statistic.h"
Chris Lattnere6bb6492010-12-26 19:39:38 +000060using namespace llvm;
61
Chris Lattner4ce31fb2011-01-02 07:36:44 +000062STATISTIC(NumMemSet, "Number of memset's formed from loop stores");
63STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores");
Chris Lattnere6bb6492010-12-26 19:39:38 +000064
65namespace {
66 class LoopIdiomRecognize : public LoopPass {
Chris Lattner22920b52010-12-26 20:45:45 +000067 Loop *CurLoop;
68 const TargetData *TD;
Chris Lattner62c50fd2011-01-02 19:01:03 +000069 DominatorTree *DT;
Chris Lattner22920b52010-12-26 20:45:45 +000070 ScalarEvolution *SE;
Chris Lattnerc19175c2011-02-18 22:22:15 +000071 TargetLibraryInfo *TLI;
Chris Lattnere6bb6492010-12-26 19:39:38 +000072 public:
73 static char ID;
74 explicit LoopIdiomRecognize() : LoopPass(ID) {
75 initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
76 }
77
78 bool runOnLoop(Loop *L, LPPassManager &LPM);
Chris Lattner62c50fd2011-01-02 19:01:03 +000079 bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
80 SmallVectorImpl<BasicBlock*> &ExitBlocks);
Chris Lattnere6bb6492010-12-26 19:39:38 +000081
Chris Lattner22920b52010-12-26 20:45:45 +000082 bool processLoopStore(StoreInst *SI, const SCEV *BECount);
Chris Lattnere41d3c02011-01-04 07:46:33 +000083 bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount);
Andrew Trickd99b39e2011-03-14 16:48:10 +000084
Chris Lattner3a393722011-02-19 19:31:39 +000085 bool processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
86 unsigned StoreAlignment,
87 Value *SplatValue, Instruction *TheStore,
88 const SCEVAddRecExpr *Ev,
89 const SCEV *BECount);
Chris Lattnere2c43922011-01-02 03:37:56 +000090 bool processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize,
91 const SCEVAddRecExpr *StoreEv,
92 const SCEVAddRecExpr *LoadEv,
93 const SCEV *BECount);
Andrew Trickd99b39e2011-03-14 16:48:10 +000094
Chris Lattnere6bb6492010-12-26 19:39:38 +000095 /// This transformation requires natural loop information & requires that
96 /// loop preheaders be inserted into the CFG.
97 ///
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
99 AU.addRequired<LoopInfo>();
100 AU.addPreserved<LoopInfo>();
101 AU.addRequiredID(LoopSimplifyID);
102 AU.addPreservedID(LoopSimplifyID);
103 AU.addRequiredID(LCSSAID);
104 AU.addPreservedID(LCSSAID);
Chris Lattner2e12f1a2010-12-27 18:39:08 +0000105 AU.addRequired<AliasAnalysis>();
106 AU.addPreserved<AliasAnalysis>();
Chris Lattnere6bb6492010-12-26 19:39:38 +0000107 AU.addRequired<ScalarEvolution>();
108 AU.addPreserved<ScalarEvolution>();
109 AU.addPreserved<DominatorTree>();
Chris Lattner62c50fd2011-01-02 19:01:03 +0000110 AU.addRequired<DominatorTree>();
Chris Lattnerc19175c2011-02-18 22:22:15 +0000111 AU.addRequired<TargetLibraryInfo>();
Chris Lattnere6bb6492010-12-26 19:39:38 +0000112 }
113 };
114}
115
116char LoopIdiomRecognize::ID = 0;
117INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
118 false, false)
119INITIALIZE_PASS_DEPENDENCY(LoopInfo)
Chris Lattner62c50fd2011-01-02 19:01:03 +0000120INITIALIZE_PASS_DEPENDENCY(DominatorTree)
Chris Lattnere6bb6492010-12-26 19:39:38 +0000121INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
122INITIALIZE_PASS_DEPENDENCY(LCSSA)
123INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
Chris Lattnerc19175c2011-02-18 22:22:15 +0000124INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Chris Lattner2e12f1a2010-12-27 18:39:08 +0000125INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Chris Lattnere6bb6492010-12-26 19:39:38 +0000126INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
127 false, false)
128
129Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); }
130
Chris Lattner4f81b542011-05-22 17:39:56 +0000131/// deleteDeadInstruction - Delete this instruction. Before we do, go through
Chris Lattner9f391882010-12-27 00:03:23 +0000132/// and zero out all the operands of this instruction. If any of them become
133/// dead, delete them and the computation tree that feeds them.
134///
Chris Lattner4f81b542011-05-22 17:39:56 +0000135static void deleteDeadInstruction(Instruction *I, ScalarEvolution &SE) {
Chris Lattner9f391882010-12-27 00:03:23 +0000136 SmallVector<Instruction*, 32> NowDeadInsts;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000137
Chris Lattner9f391882010-12-27 00:03:23 +0000138 NowDeadInsts.push_back(I);
Andrew Trickd99b39e2011-03-14 16:48:10 +0000139
Chris Lattner9f391882010-12-27 00:03:23 +0000140 // Before we touch this instruction, remove it from SE!
141 do {
142 Instruction *DeadInst = NowDeadInsts.pop_back_val();
Andrew Trickd99b39e2011-03-14 16:48:10 +0000143
Chris Lattner9f391882010-12-27 00:03:23 +0000144 // This instruction is dead, zap it, in stages. Start by removing it from
145 // SCEV.
146 SE.forgetValue(DeadInst);
Andrew Trickd99b39e2011-03-14 16:48:10 +0000147
Chris Lattner9f391882010-12-27 00:03:23 +0000148 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
149 Value *Op = DeadInst->getOperand(op);
150 DeadInst->setOperand(op, 0);
Andrew Trickd99b39e2011-03-14 16:48:10 +0000151
Chris Lattner9f391882010-12-27 00:03:23 +0000152 // If this operand just became dead, add it to the NowDeadInsts list.
153 if (!Op->use_empty()) continue;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000154
Chris Lattner9f391882010-12-27 00:03:23 +0000155 if (Instruction *OpI = dyn_cast<Instruction>(Op))
156 if (isInstructionTriviallyDead(OpI))
157 NowDeadInsts.push_back(OpI);
158 }
Andrew Trickd99b39e2011-03-14 16:48:10 +0000159
Chris Lattner9f391882010-12-27 00:03:23 +0000160 DeadInst->eraseFromParent();
Andrew Trickd99b39e2011-03-14 16:48:10 +0000161
Chris Lattner9f391882010-12-27 00:03:23 +0000162 } while (!NowDeadInsts.empty());
163}
164
Chris Lattner4f81b542011-05-22 17:39:56 +0000165/// deleteIfDeadInstruction - If the specified value is a dead instruction,
166/// delete it and any recursively used instructions.
167static void deleteIfDeadInstruction(Value *V, ScalarEvolution &SE) {
168 if (Instruction *I = dyn_cast<Instruction>(V))
169 if (isInstructionTriviallyDead(I))
Andrew Tricka5d950f2011-06-28 05:04:16 +0000170 deleteDeadInstruction(I, SE);
Chris Lattner4f81b542011-05-22 17:39:56 +0000171}
172
Chris Lattnere6bb6492010-12-26 19:39:38 +0000173bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
Chris Lattner22920b52010-12-26 20:45:45 +0000174 CurLoop = L;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000175
Chad Rosier71400b62011-07-15 18:25:04 +0000176 // Disable loop idiom recognition if the function's name is a common idiom.
177 StringRef Name = L->getHeader()->getParent()->getName();
178 if (Name == "memset" || Name == "memcpy")
179 return false;
180
Chris Lattner22920b52010-12-26 20:45:45 +0000181 // The trip count of the loop must be analyzable.
182 SE = &getAnalysis<ScalarEvolution>();
183 if (!SE->hasLoopInvariantBackedgeTakenCount(L))
184 return false;
185 const SCEV *BECount = SE->getBackedgeTakenCount(L);
186 if (isa<SCEVCouldNotCompute>(BECount)) return false;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000187
Chris Lattner8e08e732011-01-02 20:24:21 +0000188 // If this loop executes exactly one time, then it should be peeled, not
189 // optimized by this pass.
190 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
191 if (BECst->getValue()->getValue() == 0)
192 return false;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000193
Chris Lattner22920b52010-12-26 20:45:45 +0000194 // We require target data for now.
195 TD = getAnalysisIfAvailable<TargetData>();
196 if (TD == 0) return false;
Chris Lattnere6bb6492010-12-26 19:39:38 +0000197
Chris Lattner62c50fd2011-01-02 19:01:03 +0000198 DT = &getAnalysis<DominatorTree>();
199 LoopInfo &LI = getAnalysis<LoopInfo>();
Chris Lattnerc19175c2011-02-18 22:22:15 +0000200 TLI = &getAnalysis<TargetLibraryInfo>();
Andrew Trickd99b39e2011-03-14 16:48:10 +0000201
Chris Lattner62c50fd2011-01-02 19:01:03 +0000202 SmallVector<BasicBlock*, 8> ExitBlocks;
203 CurLoop->getUniqueExitBlocks(ExitBlocks);
204
Chris Lattner63f9c3c2011-01-02 21:14:18 +0000205 DEBUG(dbgs() << "loop-idiom Scanning: F["
206 << L->getHeader()->getParent()->getName()
207 << "] Loop %" << L->getHeader()->getName() << "\n");
Andrew Trickd99b39e2011-03-14 16:48:10 +0000208
Chris Lattner62c50fd2011-01-02 19:01:03 +0000209 bool MadeChange = false;
210 // Scan all the blocks in the loop that are not in subloops.
211 for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E;
212 ++BI) {
213 // Ignore blocks in subloops.
214 if (LI.getLoopFor(*BI) != CurLoop)
215 continue;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000216
Chris Lattner62c50fd2011-01-02 19:01:03 +0000217 MadeChange |= runOnLoopBlock(*BI, BECount, ExitBlocks);
218 }
219 return MadeChange;
220}
221
222/// runOnLoopBlock - Process the specified block, which lives in a counted loop
223/// with the specified backedge count. This block is known to be in the current
224/// loop and not in any subloops.
225bool LoopIdiomRecognize::runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
226 SmallVectorImpl<BasicBlock*> &ExitBlocks) {
227 // We can only promote stores in this block if they are unconditionally
228 // executed in the loop. For a block to be unconditionally executed, it has
229 // to dominate all the exit blocks of the loop. Verify this now.
230 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
231 if (!DT->dominates(BB, ExitBlocks[i]))
232 return false;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000233
Chris Lattner22920b52010-12-26 20:45:45 +0000234 bool MadeChange = false;
235 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Chris Lattnerb7e9ef02011-01-04 07:27:30 +0000236 Instruction *Inst = I++;
237 // Look for store instructions, which may be optimized to memset/memcpy.
238 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattnerb7e9ef02011-01-04 07:27:30 +0000239 WeakVH InstPtr(I);
240 if (!processLoopStore(SI, BECount)) continue;
241 MadeChange = true;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000242
Chris Lattnerb7e9ef02011-01-04 07:27:30 +0000243 // If processing the store invalidated our iterator, start over from the
Chris Lattnere41d3c02011-01-04 07:46:33 +0000244 // top of the block.
Chris Lattnerb7e9ef02011-01-04 07:27:30 +0000245 if (InstPtr == 0)
246 I = BB->begin();
247 continue;
248 }
Andrew Trickd99b39e2011-03-14 16:48:10 +0000249
Chris Lattnere41d3c02011-01-04 07:46:33 +0000250 // Look for memset instructions, which may be optimized to a larger memset.
251 if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) {
252 WeakVH InstPtr(I);
253 if (!processLoopMemSet(MSI, BECount)) continue;
254 MadeChange = true;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000255
Chris Lattnere41d3c02011-01-04 07:46:33 +0000256 // If processing the memset invalidated our iterator, start over from the
257 // top of the block.
258 if (InstPtr == 0)
259 I = BB->begin();
260 continue;
261 }
Chris Lattner22920b52010-12-26 20:45:45 +0000262 }
Andrew Trickd99b39e2011-03-14 16:48:10 +0000263
Chris Lattner22920b52010-12-26 20:45:45 +0000264 return MadeChange;
Chris Lattnere6bb6492010-12-26 19:39:38 +0000265}
266
Chris Lattner62c50fd2011-01-02 19:01:03 +0000267
Chris Lattnere41d3c02011-01-04 07:46:33 +0000268/// processLoopStore - See if this store can be promoted to a memset or memcpy.
Chris Lattner22920b52010-12-26 20:45:45 +0000269bool LoopIdiomRecognize::processLoopStore(StoreInst *SI, const SCEV *BECount) {
Chris Lattnere41d3c02011-01-04 07:46:33 +0000270 if (SI->isVolatile()) return false;
271
Chris Lattner22920b52010-12-26 20:45:45 +0000272 Value *StoredVal = SI->getValueOperand();
Chris Lattnera92ff912010-12-26 23:42:51 +0000273 Value *StorePtr = SI->getPointerOperand();
Andrew Trickd99b39e2011-03-14 16:48:10 +0000274
Chris Lattner95ae6762010-12-28 18:53:48 +0000275 // Reject stores that are so large that they overflow an unsigned.
Chris Lattner22920b52010-12-26 20:45:45 +0000276 uint64_t SizeInBits = TD->getTypeSizeInBits(StoredVal->getType());
Chris Lattner95ae6762010-12-28 18:53:48 +0000277 if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
Chris Lattner22920b52010-12-26 20:45:45 +0000278 return false;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000279
Chris Lattner22920b52010-12-26 20:45:45 +0000280 // See if the pointer expression is an AddRec like {base,+,1} on the current
281 // loop, which indicates a strided store. If we have something else, it's a
282 // random store we can't handle.
Chris Lattnere2c43922011-01-02 03:37:56 +0000283 const SCEVAddRecExpr *StoreEv =
284 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
285 if (StoreEv == 0 || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
Chris Lattner22920b52010-12-26 20:45:45 +0000286 return false;
287
288 // Check to see if the stride matches the size of the store. If so, then we
289 // know that every byte is touched in the loop.
Andrew Trickd99b39e2011-03-14 16:48:10 +0000290 unsigned StoreSize = (unsigned)SizeInBits >> 3;
Chris Lattnere2c43922011-01-02 03:37:56 +0000291 const SCEVConstant *Stride = dyn_cast<SCEVConstant>(StoreEv->getOperand(1));
Andrew Trickd99b39e2011-03-14 16:48:10 +0000292
Chris Lattner408b5342011-02-21 02:08:54 +0000293 if (Stride == 0 || StoreSize != Stride->getValue()->getValue()) {
294 // TODO: Could also handle negative stride here someday, that will require
295 // the validity check in mayLoopAccessLocation to be updated though.
296 // Enable this to print exact negative strides.
Chris Lattner0e68cee2011-02-21 17:02:55 +0000297 if (0 && Stride && StoreSize == -Stride->getValue()->getValue()) {
Chris Lattner408b5342011-02-21 02:08:54 +0000298 dbgs() << "NEGATIVE STRIDE: " << *SI << "\n";
299 dbgs() << "BB: " << *SI->getParent();
300 }
Andrew Trickd99b39e2011-03-14 16:48:10 +0000301
Chris Lattner22920b52010-12-26 20:45:45 +0000302 return false;
Chris Lattner408b5342011-02-21 02:08:54 +0000303 }
Chris Lattner3a393722011-02-19 19:31:39 +0000304
305 // See if we can optimize just this store in isolation.
306 if (processLoopStridedStore(StorePtr, StoreSize, SI->getAlignment(),
307 StoredVal, SI, StoreEv, BECount))
308 return true;
Chris Lattnera92ff912010-12-26 23:42:51 +0000309
Chris Lattnere2c43922011-01-02 03:37:56 +0000310 // If the stored value is a strided load in the same loop with the same stride
311 // this this may be transformable into a memcpy. This kicks in for stuff like
312 // for (i) A[i] = B[i];
313 if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
314 const SCEVAddRecExpr *LoadEv =
315 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getOperand(0)));
316 if (LoadEv && LoadEv->getLoop() == CurLoop && LoadEv->isAffine() &&
317 StoreEv->getOperand(1) == LoadEv->getOperand(1) && !LI->isVolatile())
318 if (processLoopStoreOfLoopLoad(SI, StoreSize, StoreEv, LoadEv, BECount))
319 return true;
320 }
Chris Lattner4ce31fb2011-01-02 07:36:44 +0000321 //errs() << "UNHANDLED strided store: " << *StoreEv << " - " << *SI << "\n";
Chris Lattner22920b52010-12-26 20:45:45 +0000322
Chris Lattnere6bb6492010-12-26 19:39:38 +0000323 return false;
324}
325
Chris Lattnere41d3c02011-01-04 07:46:33 +0000326/// processLoopMemSet - See if this memset can be promoted to a large memset.
327bool LoopIdiomRecognize::
328processLoopMemSet(MemSetInst *MSI, const SCEV *BECount) {
329 // We can only handle non-volatile memsets with a constant size.
330 if (MSI->isVolatile() || !isa<ConstantInt>(MSI->getLength())) return false;
331
Chris Lattnerc19175c2011-02-18 22:22:15 +0000332 // If we're not allowed to hack on memset, we fail.
333 if (!TLI->has(LibFunc::memset))
334 return false;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000335
Chris Lattnere41d3c02011-01-04 07:46:33 +0000336 Value *Pointer = MSI->getDest();
Andrew Trickd99b39e2011-03-14 16:48:10 +0000337
Chris Lattnere41d3c02011-01-04 07:46:33 +0000338 // See if the pointer expression is an AddRec like {base,+,1} on the current
339 // loop, which indicates a strided store. If we have something else, it's a
340 // random store we can't handle.
341 const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer));
342 if (Ev == 0 || Ev->getLoop() != CurLoop || !Ev->isAffine())
343 return false;
344
345 // Reject memsets that are so large that they overflow an unsigned.
346 uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
347 if ((SizeInBytes >> 32) != 0)
348 return false;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000349
Chris Lattnere41d3c02011-01-04 07:46:33 +0000350 // Check to see if the stride matches the size of the memset. If so, then we
351 // know that every byte is touched in the loop.
352 const SCEVConstant *Stride = dyn_cast<SCEVConstant>(Ev->getOperand(1));
Andrew Trickd99b39e2011-03-14 16:48:10 +0000353
Chris Lattnere41d3c02011-01-04 07:46:33 +0000354 // TODO: Could also handle negative stride here someday, that will require the
355 // validity check in mayLoopAccessLocation to be updated though.
356 if (Stride == 0 || MSI->getLength() != Stride->getValue())
357 return false;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000358
Chris Lattner3a393722011-02-19 19:31:39 +0000359 return processLoopStridedStore(Pointer, (unsigned)SizeInBytes,
360 MSI->getAlignment(), MSI->getValue(),
361 MSI, Ev, BECount);
Chris Lattnere41d3c02011-01-04 07:46:33 +0000362}
363
364
Chris Lattner63f9c3c2011-01-02 21:14:18 +0000365/// mayLoopAccessLocation - Return true if the specified loop might access the
366/// specified pointer location, which is a loop-strided access. The 'Access'
367/// argument specifies what the verboten forms of access are (read or write).
368static bool mayLoopAccessLocation(Value *Ptr,AliasAnalysis::ModRefResult Access,
369 Loop *L, const SCEV *BECount,
Chris Lattnere2c43922011-01-02 03:37:56 +0000370 unsigned StoreSize, AliasAnalysis &AA,
Chris Lattnere41d3c02011-01-04 07:46:33 +0000371 Instruction *IgnoredStore) {
Chris Lattner30980b62011-01-01 19:39:01 +0000372 // Get the location that may be stored across the loop. Since the access is
373 // strided positively through memory, we say that the modified location starts
374 // at the pointer and has infinite size.
Chris Lattnera64cbf02011-01-01 19:54:22 +0000375 uint64_t AccessSize = AliasAnalysis::UnknownSize;
376
377 // If the loop iterates a fixed number of times, we can refine the access size
378 // to be exactly the size of the memset, which is (BECount+1)*StoreSize
379 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
380 AccessSize = (BECst->getValue()->getZExtValue()+1)*StoreSize;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000381
Chris Lattnera64cbf02011-01-01 19:54:22 +0000382 // TODO: For this to be really effective, we have to dive into the pointer
383 // operand in the store. Store to &A[i] of 100 will always return may alias
384 // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
385 // which will then no-alias a store to &A[100].
Chris Lattnere2c43922011-01-02 03:37:56 +0000386 AliasAnalysis::Location StoreLoc(Ptr, AccessSize);
Chris Lattner30980b62011-01-01 19:39:01 +0000387
388 for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E;
389 ++BI)
390 for (BasicBlock::iterator I = (*BI)->begin(), E = (*BI)->end(); I != E; ++I)
Chris Lattnere2c43922011-01-02 03:37:56 +0000391 if (&*I != IgnoredStore &&
Chris Lattner63f9c3c2011-01-02 21:14:18 +0000392 (AA.getModRefInfo(I, StoreLoc) & Access))
Chris Lattner30980b62011-01-01 19:39:01 +0000393 return true;
394
395 return false;
396}
397
Chris Lattner3a393722011-02-19 19:31:39 +0000398/// getMemSetPatternValue - If a strided store of the specified value is safe to
399/// turn into a memset_pattern16, return a ConstantArray of 16 bytes that should
400/// be passed in. Otherwise, return null.
401///
402/// Note that we don't ever attempt to use memset_pattern8 or 4, because these
403/// just replicate their input array and then pass on to memset_pattern16.
404static Constant *getMemSetPatternValue(Value *V, const TargetData &TD) {
405 // If the value isn't a constant, we can't promote it to being in a constant
406 // array. We could theoretically do a store to an alloca or something, but
407 // that doesn't seem worthwhile.
408 Constant *C = dyn_cast<Constant>(V);
409 if (C == 0) return 0;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000410
Chris Lattner3a393722011-02-19 19:31:39 +0000411 // Only handle simple values that are a power of two bytes in size.
412 uint64_t Size = TD.getTypeSizeInBits(V->getType());
413 if (Size == 0 || (Size & 7) || (Size & (Size-1)))
414 return 0;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000415
Chris Lattner80e8b502011-02-19 19:56:44 +0000416 // Don't care enough about darwin/ppc to implement this.
417 if (TD.isBigEndian())
418 return 0;
Chris Lattner3a393722011-02-19 19:31:39 +0000419
420 // Convert to size in bytes.
421 Size /= 8;
Chris Lattner3a393722011-02-19 19:31:39 +0000422
Chris Lattner3a393722011-02-19 19:31:39 +0000423 // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see
Chris Lattner80e8b502011-02-19 19:56:44 +0000424 // if the top and bottom are the same (e.g. for vectors and large integers).
Chris Lattner3a393722011-02-19 19:31:39 +0000425 if (Size > 16) return 0;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000426
Chris Lattner80e8b502011-02-19 19:56:44 +0000427 // If the constant is exactly 16 bytes, just use it.
428 if (Size == 16) return C;
Chris Lattner3a393722011-02-19 19:31:39 +0000429
Chris Lattner80e8b502011-02-19 19:56:44 +0000430 // Otherwise, we'll use an array of the constants.
431 unsigned ArraySize = 16/Size;
432 ArrayType *AT = ArrayType::get(V->getType(), ArraySize);
433 return ConstantArray::get(AT, std::vector<Constant*>(ArraySize, C));
Chris Lattner3a393722011-02-19 19:31:39 +0000434}
435
436
437/// processLoopStridedStore - We see a strided store of some value. If we can
438/// transform this into a memset or memset_pattern in the loop preheader, do so.
439bool LoopIdiomRecognize::
440processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
441 unsigned StoreAlignment, Value *StoredVal,
442 Instruction *TheStore, const SCEVAddRecExpr *Ev,
443 const SCEV *BECount) {
Andrew Trickd99b39e2011-03-14 16:48:10 +0000444
Chris Lattner3a393722011-02-19 19:31:39 +0000445 // If the stored value is a byte-wise value (like i32 -1), then it may be
446 // turned into a memset of i8 -1, assuming that all the consecutive bytes
447 // are stored. A store of i32 0x01020304 can never be turned into a memset,
448 // but it can be turned into memset_pattern if the target supports it.
449 Value *SplatValue = isBytewiseValue(StoredVal);
450 Constant *PatternValue = 0;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000451
Chris Lattner3a393722011-02-19 19:31:39 +0000452 // If we're allowed to form a memset, and the stored value would be acceptable
453 // for memset, use it.
454 if (SplatValue && TLI->has(LibFunc::memset) &&
455 // Verify that the stored value is loop invariant. If not, we can't
456 // promote the memset.
457 CurLoop->isLoopInvariant(SplatValue)) {
458 // Keep and use SplatValue.
459 PatternValue = 0;
460 } else if (TLI->has(LibFunc::memset_pattern16) &&
461 (PatternValue = getMemSetPatternValue(StoredVal, *TD))) {
462 // It looks like we can use PatternValue!
463 SplatValue = 0;
464 } else {
465 // Otherwise, this isn't an idiom we can transform. For example, we can't
466 // do anything with a 3-byte store, for example.
Chris Lattnerbafa1172011-01-01 20:12:04 +0000467 return false;
Chris Lattner3a393722011-02-19 19:31:39 +0000468 }
Andrew Trickd99b39e2011-03-14 16:48:10 +0000469
Chris Lattner4f81b542011-05-22 17:39:56 +0000470 // The trip count of the loop and the base pointer of the addrec SCEV is
471 // guaranteed to be loop invariant, which means that it should dominate the
472 // header. This allows us to insert code for it in the preheader.
473 BasicBlock *Preheader = CurLoop->getLoopPreheader();
474 IRBuilder<> Builder(Preheader->getTerminator());
Andrew Trick5e7645b2011-06-28 05:07:32 +0000475 SCEVExpander Expander(*SE, "loop-idiom");
Andrew Tricka5d950f2011-06-28 05:04:16 +0000476
Chris Lattnera92ff912010-12-26 23:42:51 +0000477 // Okay, we have a strided store "p[i]" of a splattable value. We can turn
478 // this into a memset in the loop preheader now if we want. However, this
479 // would be unsafe to do if there is anything else in the loop that may read
Chris Lattner4f81b542011-05-22 17:39:56 +0000480 // or write to the aliased location. Check for any overlap by generating the
481 // base pointer and checking the region.
Chris Lattnere41d3c02011-01-04 07:46:33 +0000482 unsigned AddrSpace = cast<PointerType>(DestPtr->getType())->getAddressSpace();
Andrew Trickd99b39e2011-03-14 16:48:10 +0000483 Value *BasePtr =
Chris Lattnera92ff912010-12-26 23:42:51 +0000484 Expander.expandCodeFor(Ev->getStart(), Builder.getInt8PtrTy(AddrSpace),
485 Preheader->getTerminator());
Andrew Trickd99b39e2011-03-14 16:48:10 +0000486
Chris Lattner4f81b542011-05-22 17:39:56 +0000487
488 if (mayLoopAccessLocation(BasePtr, AliasAnalysis::ModRef,
489 CurLoop, BECount,
490 StoreSize, getAnalysis<AliasAnalysis>(), TheStore)){
491 Expander.clear();
492 // If we generated new code for the base pointer, clean up.
493 deleteIfDeadInstruction(BasePtr, *SE);
494 return false;
495 }
Andrew Tricka5d950f2011-06-28 05:04:16 +0000496
Chris Lattner4f81b542011-05-22 17:39:56 +0000497 // Okay, everything looks good, insert the memset.
498
Chris Lattnera92ff912010-12-26 23:42:51 +0000499 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
500 // pointer size if it isn't already.
Chris Lattner3a393722011-02-19 19:31:39 +0000501 const Type *IntPtr = TD->getIntPtrType(DestPtr->getContext());
Chris Lattner7c90b902011-01-04 00:06:55 +0000502 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr);
Andrew Trickd99b39e2011-03-14 16:48:10 +0000503
Chris Lattnera92ff912010-12-26 23:42:51 +0000504 const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1),
Andrew Trick3228cc22011-03-14 16:50:06 +0000505 SCEV::FlagNUW);
Chris Lattnera92ff912010-12-26 23:42:51 +0000506 if (StoreSize != 1)
507 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize),
Andrew Trick3228cc22011-03-14 16:50:06 +0000508 SCEV::FlagNUW);
Andrew Trickd99b39e2011-03-14 16:48:10 +0000509
510 Value *NumBytes =
Chris Lattnera92ff912010-12-26 23:42:51 +0000511 Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator());
Andrew Trickd99b39e2011-03-14 16:48:10 +0000512
Devang Patelcd77a502011-03-07 22:43:45 +0000513 CallInst *NewCall;
Chris Lattner3a393722011-02-19 19:31:39 +0000514 if (SplatValue)
515 NewCall = Builder.CreateMemSet(BasePtr, SplatValue,NumBytes,StoreAlignment);
516 else {
517 Module *M = TheStore->getParent()->getParent()->getParent();
518 Value *MSP = M->getOrInsertFunction("memset_pattern16",
519 Builder.getVoidTy(),
Andrew Trickd99b39e2011-03-14 16:48:10 +0000520 Builder.getInt8PtrTy(),
Chris Lattner3a393722011-02-19 19:31:39 +0000521 Builder.getInt8PtrTy(), IntPtr,
522 (void*)0);
Andrew Trickd99b39e2011-03-14 16:48:10 +0000523
Chris Lattner3a393722011-02-19 19:31:39 +0000524 // Otherwise we should form a memset_pattern16. PatternValue is known to be
525 // an constant array of 16-bytes. Plop the value into a mergable global.
526 GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true,
527 GlobalValue::InternalLinkage,
528 PatternValue, ".memset_pattern");
529 GV->setUnnamedAddr(true); // Ok to merge these.
530 GV->setAlignment(16);
Chris Lattner80e8b502011-02-19 19:56:44 +0000531 Value *PatternPtr = ConstantExpr::getBitCast(GV, Builder.getInt8PtrTy());
Chris Lattner3a393722011-02-19 19:31:39 +0000532 NewCall = Builder.CreateCall3(MSP, BasePtr, PatternPtr, NumBytes);
533 }
Andrew Trickd99b39e2011-03-14 16:48:10 +0000534
Chris Lattnera92ff912010-12-26 23:42:51 +0000535 DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n"
Chris Lattnere41d3c02011-01-04 07:46:33 +0000536 << " from store to: " << *Ev << " at: " << *TheStore << "\n");
Devang Patelcd77a502011-03-07 22:43:45 +0000537 NewCall->setDebugLoc(TheStore->getDebugLoc());
Andrew Trickd99b39e2011-03-14 16:48:10 +0000538
Chris Lattner9f391882010-12-27 00:03:23 +0000539 // Okay, the memset has been formed. Zap the original store and anything that
540 // feeds into it.
Chris Lattner4f81b542011-05-22 17:39:56 +0000541 deleteDeadInstruction(TheStore, *SE);
Chris Lattner4ce31fb2011-01-02 07:36:44 +0000542 ++NumMemSet;
Chris Lattnera92ff912010-12-26 23:42:51 +0000543 return true;
544}
545
Chris Lattnere2c43922011-01-02 03:37:56 +0000546/// processLoopStoreOfLoopLoad - We see a strided store whose value is a
547/// same-strided load.
548bool LoopIdiomRecognize::
549processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize,
550 const SCEVAddRecExpr *StoreEv,
551 const SCEVAddRecExpr *LoadEv,
552 const SCEV *BECount) {
Chris Lattnerc19175c2011-02-18 22:22:15 +0000553 // If we're not allowed to form memcpy, we fail.
554 if (!TLI->has(LibFunc::memcpy))
555 return false;
Andrew Trickd99b39e2011-03-14 16:48:10 +0000556
Chris Lattnere2c43922011-01-02 03:37:56 +0000557 LoadInst *LI = cast<LoadInst>(SI->getValueOperand());
Andrew Trickd99b39e2011-03-14 16:48:10 +0000558
Chris Lattner4f81b542011-05-22 17:39:56 +0000559 // The trip count of the loop and the base pointer of the addrec SCEV is
560 // guaranteed to be loop invariant, which means that it should dominate the
561 // header. This allows us to insert code for it in the preheader.
562 BasicBlock *Preheader = CurLoop->getLoopPreheader();
563 IRBuilder<> Builder(Preheader->getTerminator());
Andrew Trick5e7645b2011-06-28 05:07:32 +0000564 SCEVExpander Expander(*SE, "loop-idiom");
Andrew Tricka5d950f2011-06-28 05:04:16 +0000565
Chris Lattnere2c43922011-01-02 03:37:56 +0000566 // Okay, we have a strided store "p[i]" of a loaded value. We can turn
Chris Lattnerbdce5722011-01-02 18:32:09 +0000567 // this into a memcpy in the loop preheader now if we want. However, this
Chris Lattnere2c43922011-01-02 03:37:56 +0000568 // would be unsafe to do if there is anything else in the loop that may read
Chris Lattner4f81b542011-05-22 17:39:56 +0000569 // or write the memory region we're storing to. This includes the load that
570 // feeds the stores. Check for an alias by generating the base address and
571 // checking everything.
Andrew Trickd99b39e2011-03-14 16:48:10 +0000572 Value *StoreBasePtr =
Chris Lattnere2c43922011-01-02 03:37:56 +0000573 Expander.expandCodeFor(StoreEv->getStart(),
574 Builder.getInt8PtrTy(SI->getPointerAddressSpace()),
575 Preheader->getTerminator());
Andrew Tricka5d950f2011-06-28 05:04:16 +0000576
Chris Lattner4f81b542011-05-22 17:39:56 +0000577 if (mayLoopAccessLocation(StoreBasePtr, AliasAnalysis::ModRef,
578 CurLoop, BECount, StoreSize,
579 getAnalysis<AliasAnalysis>(), SI)) {
580 Expander.clear();
581 // If we generated new code for the base pointer, clean up.
582 deleteIfDeadInstruction(StoreBasePtr, *SE);
583 return false;
584 }
585
586 // For a memcpy, we have to make sure that the input array is not being
587 // mutated by the loop.
588 Value *LoadBasePtr =
589 Expander.expandCodeFor(LoadEv->getStart(),
590 Builder.getInt8PtrTy(LI->getPointerAddressSpace()),
591 Preheader->getTerminator());
592
593 if (mayLoopAccessLocation(LoadBasePtr, AliasAnalysis::Mod, CurLoop, BECount,
594 StoreSize, getAnalysis<AliasAnalysis>(), SI)) {
595 Expander.clear();
596 // If we generated new code for the base pointer, clean up.
597 deleteIfDeadInstruction(LoadBasePtr, *SE);
598 deleteIfDeadInstruction(StoreBasePtr, *SE);
599 return false;
600 }
Andrew Tricka5d950f2011-06-28 05:04:16 +0000601
Chris Lattner4f81b542011-05-22 17:39:56 +0000602 // Okay, everything is safe, we can transform this!
Andrew Tricka5d950f2011-06-28 05:04:16 +0000603
Andrew Trickd99b39e2011-03-14 16:48:10 +0000604
Chris Lattnere2c43922011-01-02 03:37:56 +0000605 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
606 // pointer size if it isn't already.
607 const Type *IntPtr = TD->getIntPtrType(SI->getContext());
Chris Lattner7c90b902011-01-04 00:06:55 +0000608 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr);
Andrew Trickd99b39e2011-03-14 16:48:10 +0000609
Chris Lattnere2c43922011-01-02 03:37:56 +0000610 const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1),
Andrew Trick3228cc22011-03-14 16:50:06 +0000611 SCEV::FlagNUW);
Chris Lattnere2c43922011-01-02 03:37:56 +0000612 if (StoreSize != 1)
613 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize),
Andrew Trick3228cc22011-03-14 16:50:06 +0000614 SCEV::FlagNUW);
Andrew Trickd99b39e2011-03-14 16:48:10 +0000615
Chris Lattnere2c43922011-01-02 03:37:56 +0000616 Value *NumBytes =
617 Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator());
Andrew Trickd99b39e2011-03-14 16:48:10 +0000618
Devang Patelaf358412011-05-04 21:37:05 +0000619 CallInst *NewCall =
Chris Lattnere2c43922011-01-02 03:37:56 +0000620 Builder.CreateMemCpy(StoreBasePtr, LoadBasePtr, NumBytes,
621 std::min(SI->getAlignment(), LI->getAlignment()));
Devang Patelaf358412011-05-04 21:37:05 +0000622 NewCall->setDebugLoc(SI->getDebugLoc());
Andrew Trickd99b39e2011-03-14 16:48:10 +0000623
Chris Lattnere2c43922011-01-02 03:37:56 +0000624 DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n"
625 << " from load ptr=" << *LoadEv << " at: " << *LI << "\n"
626 << " from store ptr=" << *StoreEv << " at: " << *SI << "\n");
Andrew Tricka5d950f2011-06-28 05:04:16 +0000627
Andrew Trickd99b39e2011-03-14 16:48:10 +0000628
Chris Lattnere2c43922011-01-02 03:37:56 +0000629 // Okay, the memset has been formed. Zap the original store and anything that
630 // feeds into it.
Chris Lattner4f81b542011-05-22 17:39:56 +0000631 deleteDeadInstruction(SI, *SE);
Chris Lattner4ce31fb2011-01-02 07:36:44 +0000632 ++NumMemCpy;
Chris Lattnere2c43922011-01-02 03:37:56 +0000633 return true;
634}