blob: 14984a9a587f24b83f37a35184dafff01e721422 [file] [log] [blame]
Chris Lattner81ae3f22010-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 Lattner0469e012011-01-02 18:32:09 +000015//
16// TODO List:
17//
18// Future loop memory idioms to recognize:
Chandler Carruth099f5cb02012-11-02 08:33:25 +000019// memcmp, memmove, strlen, etc.
Chris Lattner0469e012011-01-02 18:32:09 +000020// 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 Lattner8fac5db2011-01-02 23:19:45 +000033//
Chris Lattnerbc661d62011-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 Lattner02a97762011-01-03 01:10:08 +000039// This could recognize common matrix multiplies and dot product idioms and
Chris Lattner8fac5db2011-01-02 23:19:45 +000040// replace them with calls to BLAS (if linked in??).
41//
Chris Lattner0469e012011-01-02 18:32:09 +000042//===----------------------------------------------------------------------===//
Chris Lattner81ae3f22010-12-26 19:39:38 +000043
Chris Lattner81ae3f22010-12-26 19:39:38 +000044#include "llvm/Transforms/Scalar.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000045#include "llvm/ADT/Statistic.h"
Chris Lattnercb18bfa2010-12-27 18:39:08 +000046#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner81ae3f22010-12-26 19:39:38 +000047#include "llvm/Analysis/LoopPass.h"
Chris Lattner29e14ed2010-12-26 23:42:51 +000048#include "llvm/Analysis/ScalarEvolutionExpander.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000049#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000050#include "llvm/Analysis/TargetTransformInfo.h"
Chris Lattner7c5f9c32010-12-26 20:45:45 +000051#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000052#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000053#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000054#include "llvm/IR/IRBuilder.h"
55#include "llvm/IR/IntrinsicInst.h"
56#include "llvm/IR/Module.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000057#include "llvm/Support/Debug.h"
58#include "llvm/Support/raw_ostream.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000059#include "llvm/Analysis/TargetLibraryInfo.h"
Chris Lattnerb9fe6852010-12-27 00:03:23 +000060#include "llvm/Transforms/Utils/Local.h"
Chris Lattner81ae3f22010-12-26 19:39:38 +000061using namespace llvm;
62
Chandler Carruth964daaa2014-04-22 02:55:47 +000063#define DEBUG_TYPE "loop-idiom"
64
Chandler Carruth099f5cb02012-11-02 08:33:25 +000065STATISTIC(NumMemSet, "Number of memset's formed from loop stores");
66STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores");
Chris Lattner81ae3f22010-12-26 19:39:38 +000067
68namespace {
Shuxin Yang95de7c32012-12-09 03:12:46 +000069
70 class LoopIdiomRecognize;
71
72 /// This class defines some utility functions for loop idiom recognization.
73 class LIRUtil {
74 public:
75 /// Return true iff the block contains nothing but an uncondition branch
76 /// (aka goto instruction).
77 static bool isAlmostEmpty(BasicBlock *);
78
79 static BranchInst *getBranch(BasicBlock *BB) {
80 return dyn_cast<BranchInst>(BB->getTerminator());
81 }
82
Matt Arsenaultfb183232013-07-22 18:59:58 +000083 /// Derive the precondition block (i.e the block that guards the loop
Shuxin Yang95de7c32012-12-09 03:12:46 +000084 /// preheader) from the given preheader.
85 static BasicBlock *getPrecondBb(BasicBlock *PreHead);
86 };
87
88 /// This class is to recoginize idioms of population-count conducted in
89 /// a noncountable loop. Currently it only recognizes this pattern:
90 /// \code
91 /// while(x) {cnt++; ...; x &= x - 1; ...}
92 /// \endcode
93 class NclPopcountRecognize {
94 LoopIdiomRecognize &LIR;
95 Loop *CurLoop;
96 BasicBlock *PreCondBB;
97
98 typedef IRBuilder<> IRBuilderTy;
99
100 public:
101 explicit NclPopcountRecognize(LoopIdiomRecognize &TheLIR);
102 bool recognize();
103
104 private:
105 /// Take a glimpse of the loop to see if we need to go ahead recoginizing
106 /// the idiom.
107 bool preliminaryScreen();
108
109 /// Check if the given conditional branch is based on the comparison
Alp Tokercb402912014-01-24 17:20:08 +0000110 /// between a variable and zero, and if the variable is non-zero, the
111 /// control yields to the loop entry. If the branch matches the behavior,
Shuxin Yang95de7c32012-12-09 03:12:46 +0000112 /// the variable involved in the comparion is returned. This function will
Matt Arsenaultfb183232013-07-22 18:59:58 +0000113 /// be called to see if the precondition and postcondition of the loop
Shuxin Yang95de7c32012-12-09 03:12:46 +0000114 /// are in desirable form.
Nick Lewyckyb06a7962014-06-14 03:48:29 +0000115 Value *matchCondition(BranchInst *Br, BasicBlock *NonZeroTarget) const;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000116
117 /// Return true iff the idiom is detected in the loop. and 1) \p CntInst
Jim Grosbach4a7d4962014-04-29 22:41:55 +0000118 /// is set to the instruction counting the population bit. 2) \p CntPhi
Shuxin Yang95de7c32012-12-09 03:12:46 +0000119 /// is set to the corresponding phi node. 3) \p Var is set to the value
120 /// whose population bits are being counted.
121 bool detectIdiom
122 (Instruction *&CntInst, PHINode *&CntPhi, Value *&Var) const;
123
124 /// Insert ctpop intrinsic function and some obviously dead instructions.
Nick Lewyckyb06a7962014-06-14 03:48:29 +0000125 void transform(Instruction *CntInst, PHINode *CntPhi, Value *Var);
Shuxin Yang95de7c32012-12-09 03:12:46 +0000126
127 /// Create llvm.ctpop.* intrinsic function.
128 CallInst *createPopcntIntrinsic(IRBuilderTy &IRB, Value *Val, DebugLoc DL);
129 };
130
Chris Lattner81ae3f22010-12-26 19:39:38 +0000131 class LoopIdiomRecognize : public LoopPass {
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000132 Loop *CurLoop;
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000133 const DataLayout *DL;
Chris Lattner8455b6e2011-01-02 19:01:03 +0000134 DominatorTree *DT;
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000135 ScalarEvolution *SE;
Chris Lattnere6b261f2011-02-18 22:22:15 +0000136 TargetLibraryInfo *TLI;
Chandler Carruth6fe147f2013-01-05 10:00:09 +0000137 const TargetTransformInfo *TTI;
Chris Lattner81ae3f22010-12-26 19:39:38 +0000138 public:
139 static char ID;
140 explicit LoopIdiomRecognize() : LoopPass(ID) {
141 initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
Craig Topperf40110f2014-04-25 05:29:35 +0000142 DL = nullptr; DT = nullptr; SE = nullptr; TLI = nullptr; TTI = nullptr;
Chris Lattner81ae3f22010-12-26 19:39:38 +0000143 }
144
Craig Topper3e4c6972014-03-05 09:10:37 +0000145 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
Chris Lattner8455b6e2011-01-02 19:01:03 +0000146 bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
147 SmallVectorImpl<BasicBlock*> &ExitBlocks);
Chris Lattner81ae3f22010-12-26 19:39:38 +0000148
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000149 bool processLoopStore(StoreInst *SI, const SCEV *BECount);
Chris Lattner86438102011-01-04 07:46:33 +0000150 bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount);
Andrew Trick328b2232011-03-14 16:48:10 +0000151
Chris Lattner0f4a6402011-02-19 19:31:39 +0000152 bool processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
153 unsigned StoreAlignment,
154 Value *SplatValue, Instruction *TheStore,
155 const SCEVAddRecExpr *Ev,
156 const SCEV *BECount);
Chris Lattner85b6d812011-01-02 03:37:56 +0000157 bool processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize,
158 const SCEVAddRecExpr *StoreEv,
159 const SCEVAddRecExpr *LoadEv,
160 const SCEV *BECount);
Andrew Trick328b2232011-03-14 16:48:10 +0000161
Chris Lattner81ae3f22010-12-26 19:39:38 +0000162 /// This transformation requires natural loop information & requires that
163 /// loop preheaders be inserted into the CFG.
164 ///
Craig Topper3e4c6972014-03-05 09:10:37 +0000165 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000166 AU.addRequired<LoopInfoWrapperPass>();
167 AU.addPreserved<LoopInfoWrapperPass>();
Chris Lattner81ae3f22010-12-26 19:39:38 +0000168 AU.addRequiredID(LoopSimplifyID);
169 AU.addPreservedID(LoopSimplifyID);
170 AU.addRequiredID(LCSSAID);
171 AU.addPreservedID(LCSSAID);
Chris Lattnercb18bfa2010-12-27 18:39:08 +0000172 AU.addRequired<AliasAnalysis>();
173 AU.addPreserved<AliasAnalysis>();
Chris Lattner81ae3f22010-12-26 19:39:38 +0000174 AU.addRequired<ScalarEvolution>();
175 AU.addPreserved<ScalarEvolution>();
Chandler Carruth73523022014-01-13 13:07:17 +0000176 AU.addPreserved<DominatorTreeWrapperPass>();
177 AU.addRequired<DominatorTreeWrapperPass>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000178 AU.addRequired<TargetLibraryInfoWrapperPass>();
Chandler Carruth705b1852015-01-31 03:43:40 +0000179 AU.addRequired<TargetTransformInfoWrapperPass>();
Chris Lattner81ae3f22010-12-26 19:39:38 +0000180 }
Shuxin Yang95de7c32012-12-09 03:12:46 +0000181
182 const DataLayout *getDataLayout() {
Rafael Espindola93512512014-02-25 17:30:31 +0000183 if (DL)
184 return DL;
185 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
Craig Topperf40110f2014-04-25 05:29:35 +0000186 DL = DLP ? &DLP->getDataLayout() : nullptr;
Rafael Espindola93512512014-02-25 17:30:31 +0000187 return DL;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000188 }
189
190 DominatorTree *getDominatorTree() {
Chandler Carruth73523022014-01-13 13:07:17 +0000191 return DT ? DT
192 : (DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree());
Shuxin Yang95de7c32012-12-09 03:12:46 +0000193 }
194
195 ScalarEvolution *getScalarEvolution() {
196 return SE ? SE : (SE = &getAnalysis<ScalarEvolution>());
197 }
198
199 TargetLibraryInfo *getTargetLibraryInfo() {
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000200 if (!TLI)
201 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
202
203 return TLI;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000204 }
205
Chandler Carruth6fe147f2013-01-05 10:00:09 +0000206 const TargetTransformInfo *getTargetTransformInfo() {
Chandler Carruthfdb9c572015-02-01 12:01:35 +0000207 return TTI ? TTI
208 : (TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
209 *CurLoop->getHeader()->getParent()));
Shuxin Yang95de7c32012-12-09 03:12:46 +0000210 }
211
212 Loop *getLoop() const { return CurLoop; }
213
214 private:
215 bool runOnNoncountableLoop();
216 bool runOnCountableLoop();
Chris Lattner81ae3f22010-12-26 19:39:38 +0000217 };
218}
219
220char LoopIdiomRecognize::ID = 0;
221INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
222 false, false)
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000223INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
Chandler Carruth73523022014-01-13 13:07:17 +0000224INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
Chris Lattner81ae3f22010-12-26 19:39:38 +0000225INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
226INITIALIZE_PASS_DEPENDENCY(LCSSA)
227INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000228INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chris Lattnercb18bfa2010-12-27 18:39:08 +0000229INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Chandler Carruth705b1852015-01-31 03:43:40 +0000230INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Chris Lattner81ae3f22010-12-26 19:39:38 +0000231INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
232 false, false)
233
234Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); }
235
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +0000236/// deleteDeadInstruction - Delete this instruction. Before we do, go through
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000237/// and zero out all the operands of this instruction. If any of them become
238/// dead, delete them and the computation tree that feeds them.
239///
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000240static void deleteDeadInstruction(Instruction *I, ScalarEvolution &SE,
241 const TargetLibraryInfo *TLI) {
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000242 SmallVector<Instruction*, 32> NowDeadInsts;
Andrew Trick328b2232011-03-14 16:48:10 +0000243
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000244 NowDeadInsts.push_back(I);
Andrew Trick328b2232011-03-14 16:48:10 +0000245
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000246 // Before we touch this instruction, remove it from SE!
247 do {
248 Instruction *DeadInst = NowDeadInsts.pop_back_val();
Andrew Trick328b2232011-03-14 16:48:10 +0000249
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000250 // This instruction is dead, zap it, in stages. Start by removing it from
251 // SCEV.
252 SE.forgetValue(DeadInst);
Andrew Trick328b2232011-03-14 16:48:10 +0000253
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000254 for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
255 Value *Op = DeadInst->getOperand(op);
Craig Topperf40110f2014-04-25 05:29:35 +0000256 DeadInst->setOperand(op, nullptr);
Andrew Trick328b2232011-03-14 16:48:10 +0000257
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000258 // If this operand just became dead, add it to the NowDeadInsts list.
259 if (!Op->use_empty()) continue;
Andrew Trick328b2232011-03-14 16:48:10 +0000260
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000261 if (Instruction *OpI = dyn_cast<Instruction>(Op))
Benjamin Kramer8bcc9712012-08-29 15:32:21 +0000262 if (isInstructionTriviallyDead(OpI, TLI))
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000263 NowDeadInsts.push_back(OpI);
264 }
Andrew Trick328b2232011-03-14 16:48:10 +0000265
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000266 DeadInst->eraseFromParent();
Andrew Trick328b2232011-03-14 16:48:10 +0000267
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000268 } while (!NowDeadInsts.empty());
269}
270
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000271/// deleteIfDeadInstruction - If the specified value is a dead instruction,
272/// delete it and any recursively used instructions.
273static void deleteIfDeadInstruction(Value *V, ScalarEvolution &SE,
274 const TargetLibraryInfo *TLI) {
275 if (Instruction *I = dyn_cast<Instruction>(V))
276 if (isInstructionTriviallyDead(I, TLI))
277 deleteDeadInstruction(I, SE, TLI);
278}
279
Shuxin Yang95de7c32012-12-09 03:12:46 +0000280//===----------------------------------------------------------------------===//
281//
282// Implementation of LIRUtil
283//
284//===----------------------------------------------------------------------===//
285
Matt Arsenaultfb183232013-07-22 18:59:58 +0000286// This function will return true iff the given block contains nothing but goto.
287// A typical usage of this function is to check if the preheader function is
288// "almost" empty such that generated intrinsic functions can be moved across
289// the preheader and be placed at the end of the precondition block without
290// the concern of breaking data dependence.
Shuxin Yang95de7c32012-12-09 03:12:46 +0000291bool LIRUtil::isAlmostEmpty(BasicBlock *BB) {
292 if (BranchInst *Br = getBranch(BB)) {
293 return Br->isUnconditional() && BB->size() == 1;
294 }
295 return false;
296}
297
Shuxin Yang95de7c32012-12-09 03:12:46 +0000298BasicBlock *LIRUtil::getPrecondBb(BasicBlock *PreHead) {
299 if (BasicBlock *BB = PreHead->getSinglePredecessor()) {
300 BranchInst *Br = getBranch(BB);
Craig Topperf40110f2014-04-25 05:29:35 +0000301 return Br && Br->isConditional() ? BB : nullptr;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000302 }
Craig Topperf40110f2014-04-25 05:29:35 +0000303 return nullptr;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000304}
305
306//===----------------------------------------------------------------------===//
307//
308// Implementation of NclPopcountRecognize
309//
310//===----------------------------------------------------------------------===//
311
312NclPopcountRecognize::NclPopcountRecognize(LoopIdiomRecognize &TheLIR):
Craig Topperf40110f2014-04-25 05:29:35 +0000313 LIR(TheLIR), CurLoop(TheLIR.getLoop()), PreCondBB(nullptr) {
Shuxin Yang95de7c32012-12-09 03:12:46 +0000314}
315
316bool NclPopcountRecognize::preliminaryScreen() {
Chandler Carruth6fe147f2013-01-05 10:00:09 +0000317 const TargetTransformInfo *TTI = LIR.getTargetTransformInfo();
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000318 if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware)
Shuxin Yang95de7c32012-12-09 03:12:46 +0000319 return false;
320
Robert Wilhelm2788d3e2013-09-28 13:42:22 +0000321 // Counting population are usually conducted by few arithmetic instructions.
Shuxin Yang95de7c32012-12-09 03:12:46 +0000322 // Such instructions can be easilly "absorbed" by vacant slots in a
323 // non-compact loop. Therefore, recognizing popcount idiom only makes sense
324 // in a compact loop.
325
326 // Give up if the loop has multiple blocks or multiple backedges.
327 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
328 return false;
329
330 BasicBlock *LoopBody = *(CurLoop->block_begin());
331 if (LoopBody->size() >= 20) {
332 // The loop is too big, bail out.
333 return false;
334 }
335
336 // It should have a preheader containing nothing but a goto instruction.
337 BasicBlock *PreHead = CurLoop->getLoopPreheader();
338 if (!PreHead || !LIRUtil::isAlmostEmpty(PreHead))
339 return false;
340
341 // It should have a precondition block where the generated popcount instrinsic
342 // function will be inserted.
343 PreCondBB = LIRUtil::getPrecondBb(PreHead);
344 if (!PreCondBB)
345 return false;
Matt Arsenaultfb183232013-07-22 18:59:58 +0000346
Shuxin Yang95de7c32012-12-09 03:12:46 +0000347 return true;
348}
349
Jim Grosbach708f80f2014-04-29 22:41:58 +0000350Value *NclPopcountRecognize::matchCondition(BranchInst *Br,
351 BasicBlock *LoopEntry) const {
Shuxin Yang95de7c32012-12-09 03:12:46 +0000352 if (!Br || !Br->isConditional())
Craig Topperf40110f2014-04-25 05:29:35 +0000353 return nullptr;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000354
355 ICmpInst *Cond = dyn_cast<ICmpInst>(Br->getCondition());
356 if (!Cond)
Craig Topperf40110f2014-04-25 05:29:35 +0000357 return nullptr;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000358
359 ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
360 if (!CmpZero || !CmpZero->isZero())
Craig Topperf40110f2014-04-25 05:29:35 +0000361 return nullptr;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000362
363 ICmpInst::Predicate Pred = Cond->getPredicate();
364 if ((Pred == ICmpInst::ICMP_NE && Br->getSuccessor(0) == LoopEntry) ||
365 (Pred == ICmpInst::ICMP_EQ && Br->getSuccessor(1) == LoopEntry))
366 return Cond->getOperand(0);
367
Craig Topperf40110f2014-04-25 05:29:35 +0000368 return nullptr;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000369}
370
371bool NclPopcountRecognize::detectIdiom(Instruction *&CntInst,
372 PHINode *&CntPhi,
373 Value *&Var) const {
374 // Following code tries to detect this idiom:
375 //
376 // if (x0 != 0)
377 // goto loop-exit // the precondition of the loop
378 // cnt0 = init-val;
379 // do {
380 // x1 = phi (x0, x2);
381 // cnt1 = phi(cnt0, cnt2);
382 //
383 // cnt2 = cnt1 + 1;
384 // ...
385 // x2 = x1 & (x1 - 1);
386 // ...
387 // } while(x != 0);
388 //
389 // loop-exit:
390 //
391
392 // step 1: Check to see if the look-back branch match this pattern:
393 // "if (a!=0) goto loop-entry".
394 BasicBlock *LoopEntry;
395 Instruction *DefX2, *CountInst;
396 Value *VarX1, *VarX0;
397 PHINode *PhiX, *CountPhi;
398
Craig Topperf40110f2014-04-25 05:29:35 +0000399 DefX2 = CountInst = nullptr;
400 VarX1 = VarX0 = nullptr;
401 PhiX = CountPhi = nullptr;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000402 LoopEntry = *(CurLoop->block_begin());
403
404 // step 1: Check if the loop-back branch is in desirable form.
405 {
406 if (Value *T = matchCondition (LIRUtil::getBranch(LoopEntry), LoopEntry))
407 DefX2 = dyn_cast<Instruction>(T);
408 else
409 return false;
410 }
411
412 // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)"
413 {
Shuxin Yangc5c730b2013-01-10 23:32:01 +0000414 if (!DefX2 || DefX2->getOpcode() != Instruction::And)
Shuxin Yang95de7c32012-12-09 03:12:46 +0000415 return false;
416
417 BinaryOperator *SubOneOp;
418
419 if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0))))
420 VarX1 = DefX2->getOperand(1);
421 else {
422 VarX1 = DefX2->getOperand(0);
423 SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1));
424 }
425 if (!SubOneOp)
426 return false;
427
428 Instruction *SubInst = cast<Instruction>(SubOneOp);
429 ConstantInt *Dec = dyn_cast<ConstantInt>(SubInst->getOperand(1));
430 if (!Dec ||
431 !((SubInst->getOpcode() == Instruction::Sub && Dec->isOne()) ||
432 (SubInst->getOpcode() == Instruction::Add && Dec->isAllOnesValue()))) {
433 return false;
434 }
435 }
436
437 // step 3: Check the recurrence of variable X
438 {
439 PhiX = dyn_cast<PHINode>(VarX1);
440 if (!PhiX ||
441 (PhiX->getOperand(0) != DefX2 && PhiX->getOperand(1) != DefX2)) {
442 return false;
443 }
444 }
445
446 // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1
447 {
Craig Topperf40110f2014-04-25 05:29:35 +0000448 CountInst = nullptr;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000449 for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI(),
450 IterE = LoopEntry->end(); Iter != IterE; Iter++) {
451 Instruction *Inst = Iter;
452 if (Inst->getOpcode() != Instruction::Add)
453 continue;
454
455 ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
456 if (!Inc || !Inc->isOne())
457 continue;
458
459 PHINode *Phi = dyn_cast<PHINode>(Inst->getOperand(0));
460 if (!Phi || Phi->getParent() != LoopEntry)
461 continue;
462
463 // Check if the result of the instruction is live of the loop.
464 bool LiveOutLoop = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000465 for (User *U : Inst->users()) {
466 if ((cast<Instruction>(U))->getParent() != LoopEntry) {
Shuxin Yang95de7c32012-12-09 03:12:46 +0000467 LiveOutLoop = true; break;
468 }
469 }
470
471 if (LiveOutLoop) {
472 CountInst = Inst;
473 CountPhi = Phi;
474 break;
475 }
476 }
477
478 if (!CountInst)
479 return false;
480 }
481
482 // step 5: check if the precondition is in this form:
483 // "if (x != 0) goto loop-head ; else goto somewhere-we-don't-care;"
484 {
485 BranchInst *PreCondBr = LIRUtil::getBranch(PreCondBB);
486 Value *T = matchCondition (PreCondBr, CurLoop->getLoopPreheader());
487 if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1))
488 return false;
489
490 CntInst = CountInst;
491 CntPhi = CountPhi;
492 Var = T;
493 }
494
495 return true;
496}
497
498void NclPopcountRecognize::transform(Instruction *CntInst,
499 PHINode *CntPhi, Value *Var) {
500
501 ScalarEvolution *SE = LIR.getScalarEvolution();
502 TargetLibraryInfo *TLI = LIR.getTargetLibraryInfo();
503 BasicBlock *PreHead = CurLoop->getLoopPreheader();
504 BranchInst *PreCondBr = LIRUtil::getBranch(PreCondBB);
505 const DebugLoc DL = CntInst->getDebugLoc();
506
507 // Assuming before transformation, the loop is following:
508 // if (x) // the precondition
509 // do { cnt++; x &= x - 1; } while(x);
Matt Arsenaultfb183232013-07-22 18:59:58 +0000510
Shuxin Yang95de7c32012-12-09 03:12:46 +0000511 // Step 1: Insert the ctpop instruction at the end of the precondition block
512 IRBuilderTy Builder(PreCondBr);
513 Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
514 {
515 PopCnt = createPopcntIntrinsic(Builder, Var, DL);
516 NewCount = PopCntZext =
517 Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType()));
518
519 if (NewCount != PopCnt)
520 (cast<Instruction>(NewCount))->setDebugLoc(DL);
521
522 // TripCnt is exactly the number of iterations the loop has
523 TripCnt = NewCount;
524
Alp Tokercb402912014-01-24 17:20:08 +0000525 // If the population counter's initial value is not zero, insert Add Inst.
Shuxin Yang95de7c32012-12-09 03:12:46 +0000526 Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead);
527 ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
528 if (!InitConst || !InitConst->isZero()) {
529 NewCount = Builder.CreateAdd(NewCount, CntInitVal);
530 (cast<Instruction>(NewCount))->setDebugLoc(DL);
531 }
532 }
533
534 // Step 2: Replace the precondition from "if(x == 0) goto loop-exit" to
535 // "if(NewCount == 0) loop-exit". Withtout this change, the intrinsic
536 // function would be partial dead code, and downstream passes will drag
537 // it back from the precondition block to the preheader.
538 {
539 ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition());
540
541 Value *Opnd0 = PopCntZext;
542 Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0);
543 if (PreCond->getOperand(0) != Var)
544 std::swap(Opnd0, Opnd1);
545
546 ICmpInst *NewPreCond =
547 cast<ICmpInst>(Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1));
548 PreCond->replaceAllUsesWith(NewPreCond);
549
550 deleteDeadInstruction(PreCond, *SE, TLI);
551 }
552
553 // Step 3: Note that the population count is exactly the trip count of the
554 // loop in question, which enble us to to convert the loop from noncountable
555 // loop into a countable one. The benefit is twofold:
556 //
557 // - If the loop only counts population, the entire loop become dead after
558 // the transformation. It is lots easier to prove a countable loop dead
559 // than to prove a noncountable one. (In some C dialects, a infite loop
560 // isn't dead even if it computes nothing useful. In general, DCE needs
561 // to prove a noncountable loop finite before safely delete it.)
562 //
563 // - If the loop also performs something else, it remains alive.
564 // Since it is transformed to countable form, it can be aggressively
565 // optimized by some optimizations which are in general not applicable
566 // to a noncountable loop.
567 //
568 // After this step, this loop (conceptually) would look like following:
569 // newcnt = __builtin_ctpop(x);
570 // t = newcnt;
571 // if (x)
572 // do { cnt++; x &= x-1; t--) } while (t > 0);
573 BasicBlock *Body = *(CurLoop->block_begin());
574 {
575 BranchInst *LbBr = LIRUtil::getBranch(Body);
576 ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
577 Type *Ty = TripCnt->getType();
578
579 PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", Body->begin());
580
581 Builder.SetInsertPoint(LbCond);
582 Value *Opnd1 = cast<Value>(TcPhi);
583 Value *Opnd2 = cast<Value>(ConstantInt::get(Ty, 1));
584 Instruction *TcDec =
585 cast<Instruction>(Builder.CreateSub(Opnd1, Opnd2, "tcdec", false, true));
586
587 TcPhi->addIncoming(TripCnt, PreHead);
588 TcPhi->addIncoming(TcDec, Body);
589
590 CmpInst::Predicate Pred = (LbBr->getSuccessor(0) == Body) ?
591 CmpInst::ICMP_UGT : CmpInst::ICMP_SLE;
592 LbCond->setPredicate(Pred);
593 LbCond->setOperand(0, TcDec);
594 LbCond->setOperand(1, cast<Value>(ConstantInt::get(Ty, 0)));
595 }
596
597 // Step 4: All the references to the original population counter outside
598 // the loop are replaced with the NewCount -- the value returned from
599 // __builtin_ctpop().
600 {
601 SmallVector<Value *, 4> CntUses;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000602 for (User *U : CntInst->users())
603 if (cast<Instruction>(U)->getParent() != Body)
604 CntUses.push_back(U);
Shuxin Yang95de7c32012-12-09 03:12:46 +0000605 for (unsigned Idx = 0; Idx < CntUses.size(); Idx++) {
606 (cast<Instruction>(CntUses[Idx]))->replaceUsesOfWith(CntInst, NewCount);
607 }
608 }
609
610 // step 5: Forget the "non-computable" trip-count SCEV associated with the
611 // loop. The loop would otherwise not be deleted even if it becomes empty.
612 SE->forgetLoop(CurLoop);
613}
614
Matt Arsenaultfb183232013-07-22 18:59:58 +0000615CallInst *NclPopcountRecognize::createPopcntIntrinsic(IRBuilderTy &IRBuilder,
Shuxin Yang95de7c32012-12-09 03:12:46 +0000616 Value *Val, DebugLoc DL) {
617 Value *Ops[] = { Val };
618 Type *Tys[] = { Val->getType() };
619
620 Module *M = (*(CurLoop->block_begin()))->getParent()->getParent();
621 Value *Func = Intrinsic::getDeclaration(M, Intrinsic::ctpop, Tys);
622 CallInst *CI = IRBuilder.CreateCall(Func, Ops);
623 CI->setDebugLoc(DL);
624
625 return CI;
626}
627
628/// recognize - detect population count idiom in a non-countable loop. If
629/// detected, transform the relevant code to popcount intrinsic function
630/// call, and return true; otherwise, return false.
631bool NclPopcountRecognize::recognize() {
632
Chandler Carruth6fe147f2013-01-05 10:00:09 +0000633 if (!LIR.getTargetTransformInfo())
Shuxin Yang95de7c32012-12-09 03:12:46 +0000634 return false;
635
636 LIR.getScalarEvolution();
637
638 if (!preliminaryScreen())
639 return false;
640
641 Instruction *CntInst;
642 PHINode *CntPhi;
643 Value *Val;
644 if (!detectIdiom(CntInst, CntPhi, Val))
645 return false;
646
647 transform(CntInst, CntPhi, Val);
648 return true;
649}
650
651//===----------------------------------------------------------------------===//
652//
653// Implementation of LoopIdiomRecognize
654//
655//===----------------------------------------------------------------------===//
656
657bool LoopIdiomRecognize::runOnCountableLoop() {
658 const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop);
659 if (isa<SCEVCouldNotCompute>(BECount)) return false;
660
661 // If this loop executes exactly one time, then it should be peeled, not
662 // optimized by this pass.
663 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
664 if (BECst->getValue()->getValue() == 0)
665 return false;
666
667 // We require target data for now.
668 if (!getDataLayout())
669 return false;
670
Matt Arsenaultfb183232013-07-22 18:59:58 +0000671 // set DT
Shuxin Yang98c844f2013-01-02 18:26:31 +0000672 (void)getDominatorTree();
Shuxin Yang95de7c32012-12-09 03:12:46 +0000673
Chandler Carruth4f8f3072015-01-17 14:16:18 +0000674 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000675 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Shuxin Yang95de7c32012-12-09 03:12:46 +0000676
Matt Arsenaultfb183232013-07-22 18:59:58 +0000677 // set TLI
Shuxin Yang98c844f2013-01-02 18:26:31 +0000678 (void)getTargetLibraryInfo();
Shuxin Yang95de7c32012-12-09 03:12:46 +0000679
680 SmallVector<BasicBlock*, 8> ExitBlocks;
681 CurLoop->getUniqueExitBlocks(ExitBlocks);
682
683 DEBUG(dbgs() << "loop-idiom Scanning: F["
684 << CurLoop->getHeader()->getParent()->getName()
685 << "] Loop %" << CurLoop->getHeader()->getName() << "\n");
686
687 bool MadeChange = false;
688 // Scan all the blocks in the loop that are not in subloops.
689 for (Loop::block_iterator BI = CurLoop->block_begin(),
690 E = CurLoop->block_end(); BI != E; ++BI) {
691 // Ignore blocks in subloops.
692 if (LI.getLoopFor(*BI) != CurLoop)
693 continue;
694
695 MadeChange |= runOnLoopBlock(*BI, BECount, ExitBlocks);
696 }
697 return MadeChange;
698}
699
700bool LoopIdiomRecognize::runOnNoncountableLoop() {
701 NclPopcountRecognize Popcount(*this);
702 if (Popcount.recognize())
703 return true;
704
705 return false;
706}
707
Chris Lattner81ae3f22010-12-26 19:39:38 +0000708bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000709 if (skipOptnoneFunction(L))
710 return false;
711
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000712 CurLoop = L;
Andrew Trick328b2232011-03-14 16:48:10 +0000713
Benjamin Kramereba9aca2012-09-21 17:27:23 +0000714 // If the loop could not be converted to canonical form, it must have an
715 // indirectbr in it, just give up.
716 if (!L->getLoopPreheader())
717 return false;
718
Nadav Rotem465834c2012-07-24 10:51:42 +0000719 // Disable loop idiom recognition if the function's name is a common idiom.
Chad Rosiera7ff5432011-07-15 18:25:04 +0000720 StringRef Name = L->getHeader()->getParent()->getName();
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000721 if (Name == "memset" || Name == "memcpy")
Chad Rosiera7ff5432011-07-15 18:25:04 +0000722 return false;
723
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000724 SE = &getAnalysis<ScalarEvolution>();
Shuxin Yang95de7c32012-12-09 03:12:46 +0000725 if (SE->hasLoopInvariantBackedgeTakenCount(L))
726 return runOnCountableLoop();
727 return runOnNoncountableLoop();
Chris Lattner8455b6e2011-01-02 19:01:03 +0000728}
729
730/// runOnLoopBlock - Process the specified block, which lives in a counted loop
731/// with the specified backedge count. This block is known to be in the current
732/// loop and not in any subloops.
733bool LoopIdiomRecognize::runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
734 SmallVectorImpl<BasicBlock*> &ExitBlocks) {
735 // We can only promote stores in this block if they are unconditionally
736 // executed in the loop. For a block to be unconditionally executed, it has
737 // to dominate all the exit blocks of the loop. Verify this now.
738 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
739 if (!DT->dominates(BB, ExitBlocks[i]))
740 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000741
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000742 bool MadeChange = false;
743 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Chris Lattnera62b01d2011-01-04 07:27:30 +0000744 Instruction *Inst = I++;
745 // Look for store instructions, which may be optimized to memset/memcpy.
746 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
Chris Lattnera62b01d2011-01-04 07:27:30 +0000747 WeakVH InstPtr(I);
748 if (!processLoopStore(SI, BECount)) continue;
749 MadeChange = true;
Andrew Trick328b2232011-03-14 16:48:10 +0000750
Chris Lattnera62b01d2011-01-04 07:27:30 +0000751 // If processing the store invalidated our iterator, start over from the
Chris Lattner86438102011-01-04 07:46:33 +0000752 // top of the block.
Craig Topperf40110f2014-04-25 05:29:35 +0000753 if (!InstPtr)
Chris Lattnera62b01d2011-01-04 07:27:30 +0000754 I = BB->begin();
755 continue;
756 }
Andrew Trick328b2232011-03-14 16:48:10 +0000757
Chris Lattner86438102011-01-04 07:46:33 +0000758 // Look for memset instructions, which may be optimized to a larger memset.
759 if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) {
760 WeakVH InstPtr(I);
761 if (!processLoopMemSet(MSI, BECount)) continue;
762 MadeChange = true;
Andrew Trick328b2232011-03-14 16:48:10 +0000763
Chris Lattner86438102011-01-04 07:46:33 +0000764 // If processing the memset invalidated our iterator, start over from the
765 // top of the block.
Craig Topperf40110f2014-04-25 05:29:35 +0000766 if (!InstPtr)
Chris Lattner86438102011-01-04 07:46:33 +0000767 I = BB->begin();
768 continue;
769 }
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000770 }
Andrew Trick328b2232011-03-14 16:48:10 +0000771
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000772 return MadeChange;
Chris Lattner81ae3f22010-12-26 19:39:38 +0000773}
774
Chris Lattner8455b6e2011-01-02 19:01:03 +0000775
Chris Lattner86438102011-01-04 07:46:33 +0000776/// processLoopStore - See if this store can be promoted to a memset or memcpy.
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000777bool LoopIdiomRecognize::processLoopStore(StoreInst *SI, const SCEV *BECount) {
Eli Friedman7c5dc122011-09-12 20:23:13 +0000778 if (!SI->isSimple()) return false;
Chris Lattner86438102011-01-04 07:46:33 +0000779
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000780 Value *StoredVal = SI->getValueOperand();
Chris Lattner29e14ed2010-12-26 23:42:51 +0000781 Value *StorePtr = SI->getPointerOperand();
Andrew Trick328b2232011-03-14 16:48:10 +0000782
Chris Lattner65a699d2010-12-28 18:53:48 +0000783 // Reject stores that are so large that they overflow an unsigned.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000784 uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
Chris Lattner65a699d2010-12-28 18:53:48 +0000785 if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000786 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000787
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000788 // See if the pointer expression is an AddRec like {base,+,1} on the current
789 // loop, which indicates a strided store. If we have something else, it's a
790 // random store we can't handle.
Chris Lattner85b6d812011-01-02 03:37:56 +0000791 const SCEVAddRecExpr *StoreEv =
792 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
Craig Topperf40110f2014-04-25 05:29:35 +0000793 if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000794 return false;
795
796 // Check to see if the stride matches the size of the store. If so, then we
797 // know that every byte is touched in the loop.
Andrew Trick328b2232011-03-14 16:48:10 +0000798 unsigned StoreSize = (unsigned)SizeInBits >> 3;
Chris Lattner85b6d812011-01-02 03:37:56 +0000799 const SCEVConstant *Stride = dyn_cast<SCEVConstant>(StoreEv->getOperand(1));
Andrew Trick328b2232011-03-14 16:48:10 +0000800
Craig Topperf40110f2014-04-25 05:29:35 +0000801 if (!Stride || StoreSize != Stride->getValue()->getValue()) {
Chris Lattnerbc661d62011-02-21 02:08:54 +0000802 // TODO: Could also handle negative stride here someday, that will require
803 // the validity check in mayLoopAccessLocation to be updated though.
804 // Enable this to print exact negative strides.
Chris Lattner2333ac22011-02-21 17:02:55 +0000805 if (0 && Stride && StoreSize == -Stride->getValue()->getValue()) {
Chris Lattnerbc661d62011-02-21 02:08:54 +0000806 dbgs() << "NEGATIVE STRIDE: " << *SI << "\n";
807 dbgs() << "BB: " << *SI->getParent();
808 }
Andrew Trick328b2232011-03-14 16:48:10 +0000809
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000810 return false;
Chris Lattnerbc661d62011-02-21 02:08:54 +0000811 }
Chris Lattner0f4a6402011-02-19 19:31:39 +0000812
813 // See if we can optimize just this store in isolation.
814 if (processLoopStridedStore(StorePtr, StoreSize, SI->getAlignment(),
815 StoredVal, SI, StoreEv, BECount))
816 return true;
Chris Lattner29e14ed2010-12-26 23:42:51 +0000817
Chris Lattner85b6d812011-01-02 03:37:56 +0000818 // If the stored value is a strided load in the same loop with the same stride
819 // this this may be transformable into a memcpy. This kicks in for stuff like
820 // for (i) A[i] = B[i];
821 if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
822 const SCEVAddRecExpr *LoadEv =
823 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getOperand(0)));
824 if (LoadEv && LoadEv->getLoop() == CurLoop && LoadEv->isAffine() &&
Eli Friedman7c5dc122011-09-12 20:23:13 +0000825 StoreEv->getOperand(1) == LoadEv->getOperand(1) && LI->isSimple())
Chris Lattner85b6d812011-01-02 03:37:56 +0000826 if (processLoopStoreOfLoopLoad(SI, StoreSize, StoreEv, LoadEv, BECount))
827 return true;
828 }
Chris Lattner12f91be2011-01-02 07:36:44 +0000829 //errs() << "UNHANDLED strided store: " << *StoreEv << " - " << *SI << "\n";
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000830
Chris Lattner81ae3f22010-12-26 19:39:38 +0000831 return false;
832}
833
Chris Lattner86438102011-01-04 07:46:33 +0000834/// processLoopMemSet - See if this memset can be promoted to a large memset.
835bool LoopIdiomRecognize::
836processLoopMemSet(MemSetInst *MSI, const SCEV *BECount) {
837 // We can only handle non-volatile memsets with a constant size.
838 if (MSI->isVolatile() || !isa<ConstantInt>(MSI->getLength())) return false;
839
Chris Lattnere6b261f2011-02-18 22:22:15 +0000840 // If we're not allowed to hack on memset, we fail.
841 if (!TLI->has(LibFunc::memset))
842 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000843
Chris Lattner86438102011-01-04 07:46:33 +0000844 Value *Pointer = MSI->getDest();
Andrew Trick328b2232011-03-14 16:48:10 +0000845
Chris Lattner86438102011-01-04 07:46:33 +0000846 // See if the pointer expression is an AddRec like {base,+,1} on the current
847 // loop, which indicates a strided store. If we have something else, it's a
848 // random store we can't handle.
849 const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer));
Craig Topperf40110f2014-04-25 05:29:35 +0000850 if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine())
Chris Lattner86438102011-01-04 07:46:33 +0000851 return false;
852
853 // Reject memsets that are so large that they overflow an unsigned.
854 uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
855 if ((SizeInBytes >> 32) != 0)
856 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000857
Chris Lattner86438102011-01-04 07:46:33 +0000858 // Check to see if the stride matches the size of the memset. If so, then we
859 // know that every byte is touched in the loop.
860 const SCEVConstant *Stride = dyn_cast<SCEVConstant>(Ev->getOperand(1));
Andrew Trick328b2232011-03-14 16:48:10 +0000861
Chris Lattner86438102011-01-04 07:46:33 +0000862 // TODO: Could also handle negative stride here someday, that will require the
863 // validity check in mayLoopAccessLocation to be updated though.
Craig Topperf40110f2014-04-25 05:29:35 +0000864 if (!Stride || MSI->getLength() != Stride->getValue())
Chris Lattner86438102011-01-04 07:46:33 +0000865 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000866
Chris Lattner0f4a6402011-02-19 19:31:39 +0000867 return processLoopStridedStore(Pointer, (unsigned)SizeInBytes,
868 MSI->getAlignment(), MSI->getValue(),
869 MSI, Ev, BECount);
Chris Lattner86438102011-01-04 07:46:33 +0000870}
871
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000872
873/// mayLoopAccessLocation - Return true if the specified loop might access the
874/// specified pointer location, which is a loop-strided access. The 'Access'
875/// argument specifies what the verboten forms of access are (read or write).
876static bool mayLoopAccessLocation(Value *Ptr,AliasAnalysis::ModRefResult Access,
877 Loop *L, const SCEV *BECount,
878 unsigned StoreSize, AliasAnalysis &AA,
879 Instruction *IgnoredStore) {
880 // Get the location that may be stored across the loop. Since the access is
881 // strided positively through memory, we say that the modified location starts
882 // at the pointer and has infinite size.
883 uint64_t AccessSize = AliasAnalysis::UnknownSize;
884
885 // If the loop iterates a fixed number of times, we can refine the access size
886 // to be exactly the size of the memset, which is (BECount+1)*StoreSize
887 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
888 AccessSize = (BECst->getValue()->getZExtValue()+1)*StoreSize;
889
890 // TODO: For this to be really effective, we have to dive into the pointer
891 // operand in the store. Store to &A[i] of 100 will always return may alias
892 // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
893 // which will then no-alias a store to &A[100].
894 AliasAnalysis::Location StoreLoc(Ptr, AccessSize);
895
896 for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E;
897 ++BI)
898 for (BasicBlock::iterator I = (*BI)->begin(), E = (*BI)->end(); I != E; ++I)
899 if (&*I != IgnoredStore &&
900 (AA.getModRefInfo(I, StoreLoc) & Access))
901 return true;
902
903 return false;
904}
905
Chris Lattner0f4a6402011-02-19 19:31:39 +0000906/// getMemSetPatternValue - If a strided store of the specified value is safe to
907/// turn into a memset_pattern16, return a ConstantArray of 16 bytes that should
908/// be passed in. Otherwise, return null.
909///
910/// Note that we don't ever attempt to use memset_pattern8 or 4, because these
911/// just replicate their input array and then pass on to memset_pattern16.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000912static Constant *getMemSetPatternValue(Value *V, const DataLayout &DL) {
Chris Lattner0f4a6402011-02-19 19:31:39 +0000913 // If the value isn't a constant, we can't promote it to being in a constant
914 // array. We could theoretically do a store to an alloca or something, but
915 // that doesn't seem worthwhile.
916 Constant *C = dyn_cast<Constant>(V);
Craig Topperf40110f2014-04-25 05:29:35 +0000917 if (!C) return nullptr;
Andrew Trick328b2232011-03-14 16:48:10 +0000918
Chris Lattner0f4a6402011-02-19 19:31:39 +0000919 // Only handle simple values that are a power of two bytes in size.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000920 uint64_t Size = DL.getTypeSizeInBits(V->getType());
Chris Lattner0f4a6402011-02-19 19:31:39 +0000921 if (Size == 0 || (Size & 7) || (Size & (Size-1)))
Craig Topperf40110f2014-04-25 05:29:35 +0000922 return nullptr;
Andrew Trick328b2232011-03-14 16:48:10 +0000923
Chris Lattner72a35fb2011-02-19 19:56:44 +0000924 // Don't care enough about darwin/ppc to implement this.
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000925 if (DL.isBigEndian())
Craig Topperf40110f2014-04-25 05:29:35 +0000926 return nullptr;
Chris Lattner0f4a6402011-02-19 19:31:39 +0000927
928 // Convert to size in bytes.
929 Size /= 8;
Chris Lattner0f4a6402011-02-19 19:31:39 +0000930
Chris Lattner0f4a6402011-02-19 19:31:39 +0000931 // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see
Chris Lattner72a35fb2011-02-19 19:56:44 +0000932 // if the top and bottom are the same (e.g. for vectors and large integers).
Craig Topperf40110f2014-04-25 05:29:35 +0000933 if (Size > 16) return nullptr;
Andrew Trick328b2232011-03-14 16:48:10 +0000934
Chris Lattner72a35fb2011-02-19 19:56:44 +0000935 // If the constant is exactly 16 bytes, just use it.
936 if (Size == 16) return C;
Chris Lattner0f4a6402011-02-19 19:31:39 +0000937
Chris Lattner72a35fb2011-02-19 19:56:44 +0000938 // Otherwise, we'll use an array of the constants.
939 unsigned ArraySize = 16/Size;
940 ArrayType *AT = ArrayType::get(V->getType(), ArraySize);
941 return ConstantArray::get(AT, std::vector<Constant*>(ArraySize, C));
Chris Lattner0f4a6402011-02-19 19:31:39 +0000942}
943
944
945/// processLoopStridedStore - We see a strided store of some value. If we can
946/// transform this into a memset or memset_pattern in the loop preheader, do so.
947bool LoopIdiomRecognize::
948processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
949 unsigned StoreAlignment, Value *StoredVal,
950 Instruction *TheStore, const SCEVAddRecExpr *Ev,
951 const SCEV *BECount) {
Andrew Trick328b2232011-03-14 16:48:10 +0000952
Chris Lattner0f4a6402011-02-19 19:31:39 +0000953 // If the stored value is a byte-wise value (like i32 -1), then it may be
954 // turned into a memset of i8 -1, assuming that all the consecutive bytes
955 // are stored. A store of i32 0x01020304 can never be turned into a memset,
956 // but it can be turned into memset_pattern if the target supports it.
957 Value *SplatValue = isBytewiseValue(StoredVal);
Craig Topperf40110f2014-04-25 05:29:35 +0000958 Constant *PatternValue = nullptr;
Andrew Trick328b2232011-03-14 16:48:10 +0000959
Matt Arsenault009faed2013-09-11 05:09:42 +0000960 unsigned DestAS = DestPtr->getType()->getPointerAddressSpace();
961
Chris Lattner0f4a6402011-02-19 19:31:39 +0000962 // If we're allowed to form a memset, and the stored value would be acceptable
963 // for memset, use it.
964 if (SplatValue && TLI->has(LibFunc::memset) &&
965 // Verify that the stored value is loop invariant. If not, we can't
966 // promote the memset.
967 CurLoop->isLoopInvariant(SplatValue)) {
968 // Keep and use SplatValue.
Craig Topperf40110f2014-04-25 05:29:35 +0000969 PatternValue = nullptr;
Matt Arsenault009faed2013-09-11 05:09:42 +0000970 } else if (DestAS == 0 &&
971 TLI->has(LibFunc::memset_pattern16) &&
Rafael Espindola37dc9e12014-02-21 00:06:31 +0000972 (PatternValue = getMemSetPatternValue(StoredVal, *DL))) {
Matt Arsenault009faed2013-09-11 05:09:42 +0000973 // Don't create memset_pattern16s with address spaces.
Chris Lattner0f4a6402011-02-19 19:31:39 +0000974 // It looks like we can use PatternValue!
Craig Topperf40110f2014-04-25 05:29:35 +0000975 SplatValue = nullptr;
Chris Lattner0f4a6402011-02-19 19:31:39 +0000976 } else {
977 // Otherwise, this isn't an idiom we can transform. For example, we can't
Eli Friedmana93ab132011-09-13 00:44:16 +0000978 // do anything with a 3-byte store.
Chris Lattnera3514442011-01-01 20:12:04 +0000979 return false;
Chris Lattner0f4a6402011-02-19 19:31:39 +0000980 }
Andrew Trick328b2232011-03-14 16:48:10 +0000981
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +0000982 // The trip count of the loop and the base pointer of the addrec SCEV is
983 // guaranteed to be loop invariant, which means that it should dominate the
984 // header. This allows us to insert code for it in the preheader.
985 BasicBlock *Preheader = CurLoop->getLoopPreheader();
986 IRBuilder<> Builder(Preheader->getTerminator());
Andrew Trick411daa52011-06-28 05:07:32 +0000987 SCEVExpander Expander(*SE, "loop-idiom");
Andrew Trick60ab3ef2011-06-28 05:04:16 +0000988
Matt Arsenault009faed2013-09-11 05:09:42 +0000989 Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS);
990
Chris Lattner29e14ed2010-12-26 23:42:51 +0000991 // Okay, we have a strided store "p[i]" of a splattable value. We can turn
Benjamin Kramerf77f2242012-10-21 19:31:16 +0000992 // this into a memset in the loop preheader now if we want. However, this
993 // would be unsafe to do if there is anything else in the loop that may read
Chandler Carruth7ec50852012-11-01 08:07:29 +0000994 // or write to the aliased location. Check for any overlap by generating the
995 // base pointer and checking the region.
Andrew Trick328b2232011-03-14 16:48:10 +0000996 Value *BasePtr =
Matt Arsenault009faed2013-09-11 05:09:42 +0000997 Expander.expandCodeFor(Ev->getStart(), DestInt8PtrTy,
Chris Lattner29e14ed2010-12-26 23:42:51 +0000998 Preheader->getTerminator());
Andrew Trick328b2232011-03-14 16:48:10 +0000999
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001000 if (mayLoopAccessLocation(BasePtr, AliasAnalysis::ModRef,
1001 CurLoop, BECount,
Matt Arsenault5df49bd2013-09-11 05:09:35 +00001002 StoreSize, getAnalysis<AliasAnalysis>(), TheStore)) {
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001003 Expander.clear();
1004 // If we generated new code for the base pointer, clean up.
1005 deleteIfDeadInstruction(BasePtr, *SE, TLI);
1006 return false;
1007 }
1008
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +00001009 // Okay, everything looks good, insert the memset.
1010
Chris Lattner29e14ed2010-12-26 23:42:51 +00001011 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
1012 // pointer size if it isn't already.
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001013 Type *IntPtr = Builder.getIntPtrTy(DL, DestAS);
Chris Lattner0ba473c2011-01-04 00:06:55 +00001014 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr);
Andrew Trick328b2232011-03-14 16:48:10 +00001015
Chris Lattner29e14ed2010-12-26 23:42:51 +00001016 const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1),
Andrew Trick8b55b732011-03-14 16:50:06 +00001017 SCEV::FlagNUW);
Matt Arsenault5df49bd2013-09-11 05:09:35 +00001018 if (StoreSize != 1) {
Chris Lattner29e14ed2010-12-26 23:42:51 +00001019 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize),
Andrew Trick8b55b732011-03-14 16:50:06 +00001020 SCEV::FlagNUW);
Matt Arsenault5df49bd2013-09-11 05:09:35 +00001021 }
Andrew Trick328b2232011-03-14 16:48:10 +00001022
1023 Value *NumBytes =
Chris Lattner29e14ed2010-12-26 23:42:51 +00001024 Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator());
Andrew Trick328b2232011-03-14 16:48:10 +00001025
Devang Pateld00c6282011-03-07 22:43:45 +00001026 CallInst *NewCall;
Matt Arsenault5df49bd2013-09-11 05:09:35 +00001027 if (SplatValue) {
1028 NewCall = Builder.CreateMemSet(BasePtr,
1029 SplatValue,
1030 NumBytes,
1031 StoreAlignment);
1032 } else {
Matt Arsenault009faed2013-09-11 05:09:42 +00001033 // Everything is emitted in default address space
1034 Type *Int8PtrTy = DestInt8PtrTy;
1035
Chris Lattner0f4a6402011-02-19 19:31:39 +00001036 Module *M = TheStore->getParent()->getParent()->getParent();
1037 Value *MSP = M->getOrInsertFunction("memset_pattern16",
1038 Builder.getVoidTy(),
Matt Arsenault009faed2013-09-11 05:09:42 +00001039 Int8PtrTy,
1040 Int8PtrTy,
1041 IntPtr,
Craig Topperf40110f2014-04-25 05:29:35 +00001042 (void*)nullptr);
Andrew Trick328b2232011-03-14 16:48:10 +00001043
Chris Lattner0f4a6402011-02-19 19:31:39 +00001044 // Otherwise we should form a memset_pattern16. PatternValue is known to be
1045 // an constant array of 16-bytes. Plop the value into a mergable global.
1046 GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true,
1047 GlobalValue::InternalLinkage,
1048 PatternValue, ".memset_pattern");
1049 GV->setUnnamedAddr(true); // Ok to merge these.
1050 GV->setAlignment(16);
Matt Arsenault009faed2013-09-11 05:09:42 +00001051 Value *PatternPtr = ConstantExpr::getBitCast(GV, Int8PtrTy);
Chris Lattner0f4a6402011-02-19 19:31:39 +00001052 NewCall = Builder.CreateCall3(MSP, BasePtr, PatternPtr, NumBytes);
1053 }
Andrew Trick328b2232011-03-14 16:48:10 +00001054
Chris Lattner29e14ed2010-12-26 23:42:51 +00001055 DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n"
Chris Lattner86438102011-01-04 07:46:33 +00001056 << " from store to: " << *Ev << " at: " << *TheStore << "\n");
Devang Pateld00c6282011-03-07 22:43:45 +00001057 NewCall->setDebugLoc(TheStore->getDebugLoc());
Andrew Trick328b2232011-03-14 16:48:10 +00001058
Chris Lattnerb9fe6852010-12-27 00:03:23 +00001059 // Okay, the memset has been formed. Zap the original store and anything that
1060 // feeds into it.
Benjamin Kramer8bcc9712012-08-29 15:32:21 +00001061 deleteDeadInstruction(TheStore, *SE, TLI);
Chris Lattner12f91be2011-01-02 07:36:44 +00001062 ++NumMemSet;
Chris Lattner29e14ed2010-12-26 23:42:51 +00001063 return true;
1064}
1065
Chris Lattner85b6d812011-01-02 03:37:56 +00001066/// processLoopStoreOfLoopLoad - We see a strided store whose value is a
1067/// same-strided load.
1068bool LoopIdiomRecognize::
1069processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize,
1070 const SCEVAddRecExpr *StoreEv,
1071 const SCEVAddRecExpr *LoadEv,
1072 const SCEV *BECount) {
Chris Lattnere6b261f2011-02-18 22:22:15 +00001073 // If we're not allowed to form memcpy, we fail.
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001074 if (!TLI->has(LibFunc::memcpy))
Chris Lattnere6b261f2011-02-18 22:22:15 +00001075 return false;
Andrew Trick328b2232011-03-14 16:48:10 +00001076
Chris Lattner85b6d812011-01-02 03:37:56 +00001077 LoadInst *LI = cast<LoadInst>(SI->getValueOperand());
Andrew Trick328b2232011-03-14 16:48:10 +00001078
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +00001079 // The trip count of the loop and the base pointer of the addrec SCEV is
1080 // guaranteed to be loop invariant, which means that it should dominate the
1081 // header. This allows us to insert code for it in the preheader.
1082 BasicBlock *Preheader = CurLoop->getLoopPreheader();
1083 IRBuilder<> Builder(Preheader->getTerminator());
Andrew Trick411daa52011-06-28 05:07:32 +00001084 SCEVExpander Expander(*SE, "loop-idiom");
Andrew Trick60ab3ef2011-06-28 05:04:16 +00001085
Chris Lattner85b6d812011-01-02 03:37:56 +00001086 // Okay, we have a strided store "p[i]" of a loaded value. We can turn
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001087 // this into a memcpy in the loop preheader now if we want. However, this
1088 // would be unsafe to do if there is anything else in the loop that may read
1089 // or write the memory region we're storing to. This includes the load that
1090 // feeds the stores. Check for an alias by generating the base address and
1091 // checking everything.
Andrew Trick328b2232011-03-14 16:48:10 +00001092 Value *StoreBasePtr =
Chris Lattner85b6d812011-01-02 03:37:56 +00001093 Expander.expandCodeFor(StoreEv->getStart(),
1094 Builder.getInt8PtrTy(SI->getPointerAddressSpace()),
1095 Preheader->getTerminator());
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001096
1097 if (mayLoopAccessLocation(StoreBasePtr, AliasAnalysis::ModRef,
1098 CurLoop, BECount, StoreSize,
1099 getAnalysis<AliasAnalysis>(), SI)) {
1100 Expander.clear();
1101 // If we generated new code for the base pointer, clean up.
1102 deleteIfDeadInstruction(StoreBasePtr, *SE, TLI);
1103 return false;
1104 }
1105
1106 // For a memcpy, we have to make sure that the input array is not being
1107 // mutated by the loop.
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +00001108 Value *LoadBasePtr =
1109 Expander.expandCodeFor(LoadEv->getStart(),
1110 Builder.getInt8PtrTy(LI->getPointerAddressSpace()),
1111 Preheader->getTerminator());
1112
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001113 if (mayLoopAccessLocation(LoadBasePtr, AliasAnalysis::Mod, CurLoop, BECount,
1114 StoreSize, getAnalysis<AliasAnalysis>(), SI)) {
1115 Expander.clear();
1116 // If we generated new code for the base pointer, clean up.
1117 deleteIfDeadInstruction(LoadBasePtr, *SE, TLI);
1118 deleteIfDeadInstruction(StoreBasePtr, *SE, TLI);
1119 return false;
1120 }
1121
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +00001122 // Okay, everything is safe, we can transform this!
Andrew Trick60ab3ef2011-06-28 05:04:16 +00001123
Andrew Trick328b2232011-03-14 16:48:10 +00001124
Chris Lattner85b6d812011-01-02 03:37:56 +00001125 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
1126 // pointer size if it isn't already.
Rafael Espindola37dc9e12014-02-21 00:06:31 +00001127 Type *IntPtrTy = Builder.getIntPtrTy(DL, SI->getPointerAddressSpace());
Matt Arsenault009faed2013-09-11 05:09:42 +00001128 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtrTy);
Andrew Trick328b2232011-03-14 16:48:10 +00001129
Matt Arsenault009faed2013-09-11 05:09:42 +00001130 const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtrTy, 1),
Andrew Trick8b55b732011-03-14 16:50:06 +00001131 SCEV::FlagNUW);
Chris Lattner85b6d812011-01-02 03:37:56 +00001132 if (StoreSize != 1)
Matt Arsenault009faed2013-09-11 05:09:42 +00001133 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtrTy, StoreSize),
Andrew Trick8b55b732011-03-14 16:50:06 +00001134 SCEV::FlagNUW);
Andrew Trick328b2232011-03-14 16:48:10 +00001135
Chris Lattner85b6d812011-01-02 03:37:56 +00001136 Value *NumBytes =
Matt Arsenault009faed2013-09-11 05:09:42 +00001137 Expander.expandCodeFor(NumBytesS, IntPtrTy, Preheader->getTerminator());
Andrew Trick328b2232011-03-14 16:48:10 +00001138
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001139 CallInst *NewCall =
1140 Builder.CreateMemCpy(StoreBasePtr, LoadBasePtr, NumBytes,
1141 std::min(SI->getAlignment(), LI->getAlignment()));
Devang Patel0daa07e2011-05-04 21:37:05 +00001142 NewCall->setDebugLoc(SI->getDebugLoc());
Andrew Trick328b2232011-03-14 16:48:10 +00001143
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001144 DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n"
Chris Lattner85b6d812011-01-02 03:37:56 +00001145 << " from load ptr=" << *LoadEv << " at: " << *LI << "\n"
1146 << " from store ptr=" << *StoreEv << " at: " << *SI << "\n");
Andrew Trick60ab3ef2011-06-28 05:04:16 +00001147
Andrew Trick328b2232011-03-14 16:48:10 +00001148
Chris Lattner85b6d812011-01-02 03:37:56 +00001149 // Okay, the memset has been formed. Zap the original store and anything that
1150 // feeds into it.
Benjamin Kramer8bcc9712012-08-29 15:32:21 +00001151 deleteDeadInstruction(SI, *SE, TLI);
Chandler Carruth099f5cb02012-11-02 08:33:25 +00001152 ++NumMemCpy;
Chris Lattner85b6d812011-01-02 03:37:56 +00001153 return true;
1154}