blob: a226865262edf238b14c6ef8f8294b3293756f7b [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//
Chris Lattner02a97762011-01-03 01:10:08 +000029// This could recognize common matrix multiplies and dot product idioms and
Chris Lattner8fac5db2011-01-02 23:19:45 +000030// replace them with calls to BLAS (if linked in??).
31//
Chris Lattner0469e012011-01-02 18:32:09 +000032//===----------------------------------------------------------------------===//
Chris Lattner81ae3f22010-12-26 19:39:38 +000033
Chris Lattner81ae3f22010-12-26 19:39:38 +000034#include "llvm/Transforms/Scalar.h"
Haicheng Wuf1c00a22016-01-26 02:27:47 +000035#include "llvm/ADT/MapVector.h"
36#include "llvm/ADT/SetVector.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000037#include "llvm/ADT/Statistic.h"
Chris Lattnercb18bfa2010-12-27 18:39:08 +000038#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000039#include "llvm/Analysis/BasicAliasAnalysis.h"
40#include "llvm/Analysis/GlobalsModRef.h"
Chris Lattner81ae3f22010-12-26 19:39:38 +000041#include "llvm/Analysis/LoopPass.h"
Haicheng Wuf1c00a22016-01-26 02:27:47 +000042#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000043#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chad Rosiera15b4b62015-11-23 21:09:13 +000044#include "llvm/Analysis/ScalarEvolutionExpander.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000045#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Benjamin Kramer799003b2015-03-23 19:32:43 +000046#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000047#include "llvm/Analysis/TargetTransformInfo.h"
Chris Lattner7c5f9c32010-12-26 20:45:45 +000048#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000049#include "llvm/IR/DataLayout.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000050#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000051#include "llvm/IR/IRBuilder.h"
52#include "llvm/IR/IntrinsicInst.h"
53#include "llvm/IR/Module.h"
Chandler Carruthaafe0912012-06-29 12:38:19 +000054#include "llvm/Support/Debug.h"
55#include "llvm/Support/raw_ostream.h"
Ahmed Bougachaace97c12016-04-27 19:04:50 +000056#include "llvm/Transforms/Utils/BuildLibCalls.h"
Chris Lattnerb9fe6852010-12-27 00:03:23 +000057#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth31088a92016-02-19 10:45:18 +000058#include "llvm/Transforms/Utils/LoopUtils.h"
Chris Lattner81ae3f22010-12-26 19:39:38 +000059using namespace llvm;
60
Chandler Carruth964daaa2014-04-22 02:55:47 +000061#define DEBUG_TYPE "loop-idiom"
62
Chandler Carruth099f5cb02012-11-02 08:33:25 +000063STATISTIC(NumMemSet, "Number of memset's formed from loop stores");
64STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores");
Chris Lattner81ae3f22010-12-26 19:39:38 +000065
66namespace {
Shuxin Yang95de7c32012-12-09 03:12:46 +000067
Chandler Carruthbad690e2015-08-12 23:06:37 +000068class LoopIdiomRecognize : public LoopPass {
69 Loop *CurLoop;
Chandler Carruthbf143e22015-08-14 00:21:10 +000070 AliasAnalysis *AA;
Chandler Carruthbad690e2015-08-12 23:06:37 +000071 DominatorTree *DT;
Chandler Carruth18c26692015-08-13 09:27:01 +000072 LoopInfo *LI;
Chandler Carruthbad690e2015-08-12 23:06:37 +000073 ScalarEvolution *SE;
74 TargetLibraryInfo *TLI;
75 const TargetTransformInfo *TTI;
Chad Rosier43f9b482015-11-06 16:33:57 +000076 const DataLayout *DL;
Chris Lattner81ae3f22010-12-26 19:39:38 +000077
Chandler Carruthbad690e2015-08-12 23:06:37 +000078public:
79 static char ID;
80 explicit LoopIdiomRecognize() : LoopPass(ID) {
81 initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
Chandler Carruthbad690e2015-08-12 23:06:37 +000082 }
Chris Lattner81ae3f22010-12-26 19:39:38 +000083
Chandler Carruthbad690e2015-08-12 23:06:37 +000084 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
Shuxin Yang95de7c32012-12-09 03:12:46 +000085
Chandler Carruthbad690e2015-08-12 23:06:37 +000086 /// This transformation requires natural loop information & requires that
87 /// loop preheaders be inserted into the CFG.
88 ///
89 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruthbad690e2015-08-12 23:06:37 +000090 AU.addRequired<TargetLibraryInfoWrapperPass>();
91 AU.addRequired<TargetTransformInfoWrapperPass>();
Chandler Carruth31088a92016-02-19 10:45:18 +000092 getLoopAnalysisUsage(AU);
Chandler Carruthbad690e2015-08-12 23:06:37 +000093 }
Shuxin Yang95de7c32012-12-09 03:12:46 +000094
Chandler Carruthbad690e2015-08-12 23:06:37 +000095private:
Chad Rosiercc9030b2015-11-11 23:00:59 +000096 typedef SmallVector<StoreInst *, 8> StoreList;
Haicheng Wuf1c00a22016-01-26 02:27:47 +000097 typedef MapVector<Value *, StoreList> StoreListMap;
98 StoreListMap StoreRefsForMemset;
99 StoreListMap StoreRefsForMemsetPattern;
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000100 StoreList StoreRefsForMemcpy;
101 bool HasMemset;
102 bool HasMemsetPattern;
103 bool HasMemcpy;
Chad Rosiercc9030b2015-11-11 23:00:59 +0000104
Chandler Carruthd9c60702015-08-13 00:10:03 +0000105 /// \name Countable Loop Idiom Handling
106 /// @{
107
Chandler Carruthbad690e2015-08-12 23:06:37 +0000108 bool runOnCountableLoop();
Chandler Carruthd9c60702015-08-13 00:10:03 +0000109 bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
110 SmallVectorImpl<BasicBlock *> &ExitBlocks);
111
Chad Rosiercc9030b2015-11-11 23:00:59 +0000112 void collectStores(BasicBlock *BB);
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000113 bool isLegalStore(StoreInst *SI, bool &ForMemset, bool &ForMemsetPattern,
114 bool &ForMemcpy);
115 bool processLoopStores(SmallVectorImpl<StoreInst *> &SL, const SCEV *BECount,
116 bool ForMemset);
Chandler Carruthd9c60702015-08-13 00:10:03 +0000117 bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount);
118
119 bool processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000120 unsigned StoreAlignment, Value *StoredVal,
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000121 Instruction *TheStore,
122 SmallPtrSetImpl<Instruction *> &Stores,
123 const SCEVAddRecExpr *Ev, const SCEV *BECount,
124 bool NegStride);
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000125 bool processLoopStoreOfLoopLoad(StoreInst *SI, const SCEV *BECount);
Chandler Carruthd9c60702015-08-13 00:10:03 +0000126
127 /// @}
128 /// \name Noncountable Loop Idiom Handling
129 /// @{
130
131 bool runOnNoncountableLoop();
132
Chandler Carruth8219a502015-08-13 00:44:29 +0000133 bool recognizePopcount();
134 void transformLoopToPopcount(BasicBlock *PreCondBB, Instruction *CntInst,
135 PHINode *CntPhi, Value *Var);
136
Chandler Carruthd9c60702015-08-13 00:10:03 +0000137 /// @}
Chandler Carruthbad690e2015-08-12 23:06:37 +0000138};
139
140} // End anonymous namespace.
Chris Lattner81ae3f22010-12-26 19:39:38 +0000141
142char LoopIdiomRecognize::ID = 0;
143INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
144 false, false)
Chandler Carruth31088a92016-02-19 10:45:18 +0000145INITIALIZE_PASS_DEPENDENCY(LoopPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000146INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Chandler Carruth705b1852015-01-31 03:43:40 +0000147INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
Chris Lattner81ae3f22010-12-26 19:39:38 +0000148INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
149 false, false)
150
151Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); }
152
David Majnemerc5601df2016-06-20 16:03:25 +0000153static void deleteDeadInstruction(Instruction *I) {
Benjamin Kramerf094d772015-02-07 21:37:08 +0000154 I->replaceAllUsesWith(UndefValue::get(I->getType()));
155 I->eraseFromParent();
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000156}
157
Shuxin Yang95de7c32012-12-09 03:12:46 +0000158//===----------------------------------------------------------------------===//
159//
Shuxin Yang95de7c32012-12-09 03:12:46 +0000160// Implementation of LoopIdiomRecognize
161//
162//===----------------------------------------------------------------------===//
163
Chandler Carruthd9c60702015-08-13 00:10:03 +0000164bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000165 if (skipLoop(L))
Chandler Carruthd9c60702015-08-13 00:10:03 +0000166 return false;
167
168 CurLoop = L;
Chandler Carruthd9c60702015-08-13 00:10:03 +0000169 // If the loop could not be converted to canonical form, it must have an
170 // indirectbr in it, just give up.
171 if (!L->getLoopPreheader())
172 return false;
173
174 // Disable loop idiom recognition if the function's name is a common idiom.
175 StringRef Name = L->getHeader()->getParent()->getName();
176 if (Name == "memset" || Name == "memcpy")
177 return false;
178
Chandler Carruth7b560d42015-09-09 17:55:00 +0000179 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
Chandler Carruthdc298322015-08-13 01:03:26 +0000180 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Chandler Carruth18c26692015-08-13 09:27:01 +0000181 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Chandler Carruth2f1fd162015-08-17 02:08:17 +0000182 SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
Chandler Carruthdc298322015-08-13 01:03:26 +0000183 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
184 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
185 *CurLoop->getHeader()->getParent());
Chad Rosier43f9b482015-11-06 16:33:57 +0000186 DL = &CurLoop->getHeader()->getModule()->getDataLayout();
Chandler Carruthdc298322015-08-13 01:03:26 +0000187
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000188 HasMemset = TLI->has(LibFunc::memset);
189 HasMemsetPattern = TLI->has(LibFunc::memset_pattern16);
190 HasMemcpy = TLI->has(LibFunc::memcpy);
191
192 if (HasMemset || HasMemsetPattern || HasMemcpy)
193 if (SE->hasLoopInvariantBackedgeTakenCount(L))
194 return runOnCountableLoop();
Chandler Carruthdc298322015-08-13 01:03:26 +0000195
Chandler Carruthd9c60702015-08-13 00:10:03 +0000196 return runOnNoncountableLoop();
197}
198
Shuxin Yang95de7c32012-12-09 03:12:46 +0000199bool LoopIdiomRecognize::runOnCountableLoop() {
200 const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop);
Davide Italiano8ed04462015-05-11 21:02:34 +0000201 assert(!isa<SCEVCouldNotCompute>(BECount) &&
Chandler Carruthbad690e2015-08-12 23:06:37 +0000202 "runOnCountableLoop() called on a loop without a predictable"
203 "backedge-taken count");
Shuxin Yang95de7c32012-12-09 03:12:46 +0000204
205 // If this loop executes exactly one time, then it should be peeled, not
206 // optimized by this pass.
207 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
Sanjoy Das0de2fec2015-12-17 20:28:46 +0000208 if (BECst->getAPInt() == 0)
Shuxin Yang95de7c32012-12-09 03:12:46 +0000209 return false;
210
Chandler Carruthbad690e2015-08-12 23:06:37 +0000211 SmallVector<BasicBlock *, 8> ExitBlocks;
Shuxin Yang95de7c32012-12-09 03:12:46 +0000212 CurLoop->getUniqueExitBlocks(ExitBlocks);
213
214 DEBUG(dbgs() << "loop-idiom Scanning: F["
Chandler Carruthbad690e2015-08-12 23:06:37 +0000215 << CurLoop->getHeader()->getParent()->getName() << "] Loop %"
216 << CurLoop->getHeader()->getName() << "\n");
Shuxin Yang95de7c32012-12-09 03:12:46 +0000217
218 bool MadeChange = false;
219 // Scan all the blocks in the loop that are not in subloops.
Davide Italiano95a77e82015-05-14 21:52:12 +0000220 for (auto *BB : CurLoop->getBlocks()) {
Shuxin Yang95de7c32012-12-09 03:12:46 +0000221 // Ignore blocks in subloops.
Chandler Carruth18c26692015-08-13 09:27:01 +0000222 if (LI->getLoopFor(BB) != CurLoop)
Shuxin Yang95de7c32012-12-09 03:12:46 +0000223 continue;
224
Davide Italiano80625af2015-05-13 19:51:21 +0000225 MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks);
Shuxin Yang95de7c32012-12-09 03:12:46 +0000226 }
227 return MadeChange;
228}
229
Chad Rosiera548fe52015-11-12 19:09:16 +0000230static unsigned getStoreSizeInBytes(StoreInst *SI, const DataLayout *DL) {
231 uint64_t SizeInBits = DL->getTypeSizeInBits(SI->getValueOperand()->getType());
232 assert(((SizeInBits & 7) || (SizeInBits >> 32) == 0) &&
233 "Don't overflow unsigned.");
234 return (unsigned)SizeInBits >> 3;
235}
236
Chad Rosier4acff962016-02-12 19:05:27 +0000237static APInt getStoreStride(const SCEVAddRecExpr *StoreEv) {
Chad Rosiera548fe52015-11-12 19:09:16 +0000238 const SCEVConstant *ConstStride = cast<SCEVConstant>(StoreEv->getOperand(1));
Chad Rosier4acff962016-02-12 19:05:27 +0000239 return ConstStride->getAPInt();
Chad Rosiera548fe52015-11-12 19:09:16 +0000240}
241
Chad Rosier94274fb2015-12-21 14:49:32 +0000242/// getMemSetPatternValue - If a strided store of the specified value is safe to
243/// turn into a memset_pattern16, return a ConstantArray of 16 bytes that should
244/// be passed in. Otherwise, return null.
245///
246/// Note that we don't ever attempt to use memset_pattern8 or 4, because these
247/// just replicate their input array and then pass on to memset_pattern16.
248static Constant *getMemSetPatternValue(Value *V, const DataLayout *DL) {
249 // If the value isn't a constant, we can't promote it to being in a constant
250 // array. We could theoretically do a store to an alloca or something, but
251 // that doesn't seem worthwhile.
252 Constant *C = dyn_cast<Constant>(V);
253 if (!C)
254 return nullptr;
255
256 // Only handle simple values that are a power of two bytes in size.
257 uint64_t Size = DL->getTypeSizeInBits(V->getType());
258 if (Size == 0 || (Size & 7) || (Size & (Size - 1)))
259 return nullptr;
260
261 // Don't care enough about darwin/ppc to implement this.
262 if (DL->isBigEndian())
263 return nullptr;
264
265 // Convert to size in bytes.
266 Size /= 8;
267
268 // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see
269 // if the top and bottom are the same (e.g. for vectors and large integers).
270 if (Size > 16)
271 return nullptr;
272
273 // If the constant is exactly 16 bytes, just use it.
274 if (Size == 16)
275 return C;
276
277 // Otherwise, we'll use an array of the constants.
278 unsigned ArraySize = 16 / Size;
279 ArrayType *AT = ArrayType::get(V->getType(), ArraySize);
280 return ConstantArray::get(AT, std::vector<Constant *>(ArraySize, C));
281}
282
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000283bool LoopIdiomRecognize::isLegalStore(StoreInst *SI, bool &ForMemset,
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000284 bool &ForMemsetPattern, bool &ForMemcpy) {
Chad Rosier869962f2015-12-01 14:26:35 +0000285 // Don't touch volatile stores.
286 if (!SI->isSimple())
287 return false;
288
Haicheng Wu57e1a3e2016-02-17 21:00:06 +0000289 // Avoid merging nontemporal stores.
290 if (SI->getMetadata(LLVMContext::MD_nontemporal))
291 return false;
292
Chad Rosiera548fe52015-11-12 19:09:16 +0000293 Value *StoredVal = SI->getValueOperand();
294 Value *StorePtr = SI->getPointerOperand();
295
296 // Reject stores that are so large that they overflow an unsigned.
297 uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
298 if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
299 return false;
300
301 // See if the pointer expression is an AddRec like {base,+,1} on the current
302 // loop, which indicates a strided store. If we have something else, it's a
303 // random store we can't handle.
304 const SCEVAddRecExpr *StoreEv =
305 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
306 if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
307 return false;
308
309 // Check to see if we have a constant stride.
310 if (!isa<SCEVConstant>(StoreEv->getOperand(1)))
311 return false;
312
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000313 // See if the store can be turned into a memset.
314
315 // If the stored value is a byte-wise value (like i32 -1), then it may be
316 // turned into a memset of i8 -1, assuming that all the consecutive bytes
317 // are stored. A store of i32 0x01020304 can never be turned into a memset,
318 // but it can be turned into memset_pattern if the target supports it.
319 Value *SplatValue = isBytewiseValue(StoredVal);
320 Constant *PatternValue = nullptr;
321
322 // If we're allowed to form a memset, and the stored value would be
323 // acceptable for memset, use it.
324 if (HasMemset && SplatValue &&
325 // Verify that the stored value is loop invariant. If not, we can't
326 // promote the memset.
327 CurLoop->isLoopInvariant(SplatValue)) {
328 // It looks like we can use SplatValue.
329 ForMemset = true;
330 return true;
331 } else if (HasMemsetPattern &&
332 // Don't create memset_pattern16s with address spaces.
333 StorePtr->getType()->getPointerAddressSpace() == 0 &&
334 (PatternValue = getMemSetPatternValue(StoredVal, DL))) {
335 // It looks like we can use PatternValue!
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000336 ForMemsetPattern = true;
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000337 return true;
338 }
339
340 // Otherwise, see if the store can be turned into a memcpy.
341 if (HasMemcpy) {
342 // Check to see if the stride matches the size of the store. If so, then we
343 // know that every byte is touched in the loop.
Chad Rosier4acff962016-02-12 19:05:27 +0000344 APInt Stride = getStoreStride(StoreEv);
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000345 unsigned StoreSize = getStoreSizeInBytes(SI, DL);
346 if (StoreSize != Stride && StoreSize != -Stride)
347 return false;
348
349 // The store must be feeding a non-volatile load.
350 LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand());
351 if (!LI || !LI->isSimple())
352 return false;
353
354 // See if the pointer expression is an AddRec like {base,+,1} on the current
355 // loop, which indicates a strided load. If we have something else, it's a
356 // random load we can't handle.
357 const SCEVAddRecExpr *LoadEv =
358 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
359 if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
360 return false;
361
362 // The store and load must share the same stride.
363 if (StoreEv->getOperand(1) != LoadEv->getOperand(1))
364 return false;
365
366 // Success. This store can be converted into a memcpy.
367 ForMemcpy = true;
368 return true;
369 }
370 // This store can't be transformed into a memset/memcpy.
371 return false;
Chad Rosiera548fe52015-11-12 19:09:16 +0000372}
373
Chad Rosiercc9030b2015-11-11 23:00:59 +0000374void LoopIdiomRecognize::collectStores(BasicBlock *BB) {
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000375 StoreRefsForMemset.clear();
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000376 StoreRefsForMemsetPattern.clear();
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000377 StoreRefsForMemcpy.clear();
Chad Rosiercc9030b2015-11-11 23:00:59 +0000378 for (Instruction &I : *BB) {
379 StoreInst *SI = dyn_cast<StoreInst>(&I);
380 if (!SI)
381 continue;
382
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000383 bool ForMemset = false;
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000384 bool ForMemsetPattern = false;
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000385 bool ForMemcpy = false;
Chad Rosiera548fe52015-11-12 19:09:16 +0000386 // Make sure this is a strided store with a constant stride.
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000387 if (!isLegalStore(SI, ForMemset, ForMemsetPattern, ForMemcpy))
Chad Rosiera548fe52015-11-12 19:09:16 +0000388 continue;
389
Chad Rosiercc9030b2015-11-11 23:00:59 +0000390 // Save the store locations.
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000391 if (ForMemset) {
392 // Find the base pointer.
393 Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), *DL);
394 StoreRefsForMemset[Ptr].push_back(SI);
395 } else if (ForMemsetPattern) {
396 // Find the base pointer.
397 Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), *DL);
398 StoreRefsForMemsetPattern[Ptr].push_back(SI);
399 } else if (ForMemcpy)
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000400 StoreRefsForMemcpy.push_back(SI);
Chad Rosiercc9030b2015-11-11 23:00:59 +0000401 }
402}
403
Chris Lattner8455b6e2011-01-02 19:01:03 +0000404/// runOnLoopBlock - Process the specified block, which lives in a counted loop
405/// with the specified backedge count. This block is known to be in the current
406/// loop and not in any subloops.
Chandler Carruthbad690e2015-08-12 23:06:37 +0000407bool LoopIdiomRecognize::runOnLoopBlock(
408 BasicBlock *BB, const SCEV *BECount,
409 SmallVectorImpl<BasicBlock *> &ExitBlocks) {
Chris Lattner8455b6e2011-01-02 19:01:03 +0000410 // We can only promote stores in this block if they are unconditionally
411 // executed in the loop. For a block to be unconditionally executed, it has
412 // to dominate all the exit blocks of the loop. Verify this now.
413 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
414 if (!DT->dominates(BB, ExitBlocks[i]))
415 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000416
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000417 bool MadeChange = false;
Chad Rosiercc9030b2015-11-11 23:00:59 +0000418 // Look for store instructions, which may be optimized to memset/memcpy.
419 collectStores(BB);
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000420
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000421 // Look for a single store or sets of stores with a common base, which can be
422 // optimized into a memset (memset_pattern). The latter most commonly happens
423 // with structs and handunrolled loops.
424 for (auto &SL : StoreRefsForMemset)
425 MadeChange |= processLoopStores(SL.second, BECount, true);
426
427 for (auto &SL : StoreRefsForMemsetPattern)
428 MadeChange |= processLoopStores(SL.second, BECount, false);
Chad Rosiercc9030b2015-11-11 23:00:59 +0000429
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000430 // Optimize the store into a memcpy, if it feeds an similarly strided load.
431 for (auto &SI : StoreRefsForMemcpy)
432 MadeChange |= processLoopStoreOfLoopLoad(SI, BECount);
433
Chandler Carruthbad690e2015-08-12 23:06:37 +0000434 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000435 Instruction *Inst = &*I++;
Chris Lattner86438102011-01-04 07:46:33 +0000436 // Look for memset instructions, which may be optimized to a larger memset.
Chandler Carruthbad690e2015-08-12 23:06:37 +0000437 if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +0000438 WeakVH InstPtr(&*I);
Chandler Carruthbad690e2015-08-12 23:06:37 +0000439 if (!processLoopMemSet(MSI, BECount))
440 continue;
Chris Lattner86438102011-01-04 07:46:33 +0000441 MadeChange = true;
Andrew Trick328b2232011-03-14 16:48:10 +0000442
Chris Lattner86438102011-01-04 07:46:33 +0000443 // If processing the memset invalidated our iterator, start over from the
444 // top of the block.
Craig Topperf40110f2014-04-25 05:29:35 +0000445 if (!InstPtr)
Chris Lattner86438102011-01-04 07:46:33 +0000446 I = BB->begin();
447 continue;
448 }
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000449 }
Andrew Trick328b2232011-03-14 16:48:10 +0000450
Chris Lattner7c5f9c32010-12-26 20:45:45 +0000451 return MadeChange;
Chris Lattner81ae3f22010-12-26 19:39:38 +0000452}
453
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000454/// processLoopStores - See if this store(s) can be promoted to a memset.
455bool LoopIdiomRecognize::processLoopStores(SmallVectorImpl<StoreInst *> &SL,
456 const SCEV *BECount,
457 bool ForMemset) {
458 // Try to find consecutive stores that can be transformed into memsets.
459 SetVector<StoreInst *> Heads, Tails;
460 SmallDenseMap<StoreInst *, StoreInst *> ConsecutiveChain;
Chris Lattner86438102011-01-04 07:46:33 +0000461
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000462 // Do a quadratic search on all of the given stores and find
463 // all of the pairs of stores that follow each other.
464 SmallVector<unsigned, 16> IndexQueue;
465 for (unsigned i = 0, e = SL.size(); i < e; ++i) {
466 assert(SL[i]->isSimple() && "Expected only non-volatile stores.");
Andrew Trick328b2232011-03-14 16:48:10 +0000467
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000468 Value *FirstStoredVal = SL[i]->getValueOperand();
469 Value *FirstStorePtr = SL[i]->getPointerOperand();
470 const SCEVAddRecExpr *FirstStoreEv =
471 cast<SCEVAddRecExpr>(SE->getSCEV(FirstStorePtr));
Chad Rosier4acff962016-02-12 19:05:27 +0000472 APInt FirstStride = getStoreStride(FirstStoreEv);
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000473 unsigned FirstStoreSize = getStoreSizeInBytes(SL[i], DL);
Chad Rosier79676142015-10-28 14:38:49 +0000474
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000475 // See if we can optimize just this store in isolation.
Chad Rosier4acff962016-02-12 19:05:27 +0000476 if (FirstStride == FirstStoreSize || -FirstStride == FirstStoreSize) {
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000477 Heads.insert(SL[i]);
478 continue;
479 }
Chris Lattner0f4a6402011-02-19 19:31:39 +0000480
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000481 Value *FirstSplatValue = nullptr;
482 Constant *FirstPatternValue = nullptr;
483
484 if (ForMemset)
485 FirstSplatValue = isBytewiseValue(FirstStoredVal);
486 else
487 FirstPatternValue = getMemSetPatternValue(FirstStoredVal, DL);
488
489 assert((FirstSplatValue || FirstPatternValue) &&
490 "Expected either splat value or pattern value.");
491
492 IndexQueue.clear();
493 // If a store has multiple consecutive store candidates, search Stores
494 // array according to the sequence: from i+1 to e, then from i-1 to 0.
495 // This is because usually pairing with immediate succeeding or preceding
496 // candidate create the best chance to find memset opportunity.
497 unsigned j = 0;
498 for (j = i + 1; j < e; ++j)
499 IndexQueue.push_back(j);
500 for (j = i; j > 0; --j)
501 IndexQueue.push_back(j - 1);
502
503 for (auto &k : IndexQueue) {
504 assert(SL[k]->isSimple() && "Expected only non-volatile stores.");
505 Value *SecondStorePtr = SL[k]->getPointerOperand();
506 const SCEVAddRecExpr *SecondStoreEv =
507 cast<SCEVAddRecExpr>(SE->getSCEV(SecondStorePtr));
Chad Rosier4acff962016-02-12 19:05:27 +0000508 APInt SecondStride = getStoreStride(SecondStoreEv);
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000509
510 if (FirstStride != SecondStride)
511 continue;
512
513 Value *SecondStoredVal = SL[k]->getValueOperand();
514 Value *SecondSplatValue = nullptr;
515 Constant *SecondPatternValue = nullptr;
516
517 if (ForMemset)
518 SecondSplatValue = isBytewiseValue(SecondStoredVal);
519 else
520 SecondPatternValue = getMemSetPatternValue(SecondStoredVal, DL);
521
522 assert((SecondSplatValue || SecondPatternValue) &&
523 "Expected either splat value or pattern value.");
524
525 if (isConsecutiveAccess(SL[i], SL[k], *DL, *SE, false)) {
526 if (ForMemset) {
527 if (FirstSplatValue != SecondSplatValue)
528 continue;
529 } else {
530 if (FirstPatternValue != SecondPatternValue)
531 continue;
532 }
533 Tails.insert(SL[k]);
534 Heads.insert(SL[i]);
535 ConsecutiveChain[SL[i]] = SL[k];
536 break;
537 }
538 }
539 }
540
541 // We may run into multiple chains that merge into a single chain. We mark the
542 // stores that we transformed so that we don't visit the same store twice.
543 SmallPtrSet<Value *, 16> TransformedStores;
544 bool Changed = false;
545
546 // For stores that start but don't end a link in the chain:
547 for (SetVector<StoreInst *>::iterator it = Heads.begin(), e = Heads.end();
548 it != e; ++it) {
549 if (Tails.count(*it))
550 continue;
551
552 // We found a store instr that starts a chain. Now follow the chain and try
553 // to transform it.
554 SmallPtrSet<Instruction *, 8> AdjacentStores;
555 StoreInst *I = *it;
556
557 StoreInst *HeadStore = I;
558 unsigned StoreSize = 0;
559
560 // Collect the chain into a list.
561 while (Tails.count(I) || Heads.count(I)) {
562 if (TransformedStores.count(I))
563 break;
564 AdjacentStores.insert(I);
565
566 StoreSize += getStoreSizeInBytes(I, DL);
567 // Move to the next value in the chain.
568 I = ConsecutiveChain[I];
569 }
570
571 Value *StoredVal = HeadStore->getValueOperand();
572 Value *StorePtr = HeadStore->getPointerOperand();
573 const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
Chad Rosier4acff962016-02-12 19:05:27 +0000574 APInt Stride = getStoreStride(StoreEv);
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000575
576 // Check to see if the stride matches the size of the stores. If so, then
577 // we know that every byte is touched in the loop.
578 if (StoreSize != Stride && StoreSize != -Stride)
579 continue;
580
581 bool NegStride = StoreSize == -Stride;
582
583 if (processLoopStridedStore(StorePtr, StoreSize, HeadStore->getAlignment(),
584 StoredVal, HeadStore, AdjacentStores, StoreEv,
585 BECount, NegStride)) {
586 TransformedStores.insert(AdjacentStores.begin(), AdjacentStores.end());
587 Changed = true;
588 }
589 }
590
591 return Changed;
Chris Lattner81ae3f22010-12-26 19:39:38 +0000592}
593
Chris Lattner86438102011-01-04 07:46:33 +0000594/// processLoopMemSet - See if this memset can be promoted to a large memset.
Chandler Carruthbad690e2015-08-12 23:06:37 +0000595bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
596 const SCEV *BECount) {
Chris Lattner86438102011-01-04 07:46:33 +0000597 // We can only handle non-volatile memsets with a constant size.
Chandler Carruthbad690e2015-08-12 23:06:37 +0000598 if (MSI->isVolatile() || !isa<ConstantInt>(MSI->getLength()))
599 return false;
Chris Lattner86438102011-01-04 07:46:33 +0000600
Chris Lattnere6b261f2011-02-18 22:22:15 +0000601 // If we're not allowed to hack on memset, we fail.
Ahmed Bougacha7f971932016-04-27 19:04:46 +0000602 if (!HasMemset)
Chris Lattnere6b261f2011-02-18 22:22:15 +0000603 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000604
Chris Lattner86438102011-01-04 07:46:33 +0000605 Value *Pointer = MSI->getDest();
Andrew Trick328b2232011-03-14 16:48:10 +0000606
Chris Lattner86438102011-01-04 07:46:33 +0000607 // See if the pointer expression is an AddRec like {base,+,1} on the current
608 // loop, which indicates a strided store. If we have something else, it's a
609 // random store we can't handle.
610 const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer));
Craig Topperf40110f2014-04-25 05:29:35 +0000611 if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine())
Chris Lattner86438102011-01-04 07:46:33 +0000612 return false;
613
614 // Reject memsets that are so large that they overflow an unsigned.
615 uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
616 if ((SizeInBytes >> 32) != 0)
617 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000618
Chris Lattner86438102011-01-04 07:46:33 +0000619 // Check to see if the stride matches the size of the memset. If so, then we
620 // know that every byte is touched in the loop.
Chad Rosier81362a82016-02-12 21:03:23 +0000621 const SCEVConstant *ConstStride = dyn_cast<SCEVConstant>(Ev->getOperand(1));
622 if (!ConstStride)
623 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000624
Chad Rosier81362a82016-02-12 21:03:23 +0000625 APInt Stride = ConstStride->getAPInt();
626 if (SizeInBytes != Stride && SizeInBytes != -Stride)
Chris Lattner86438102011-01-04 07:46:33 +0000627 return false;
Andrew Trick328b2232011-03-14 16:48:10 +0000628
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000629 // Verify that the memset value is loop invariant. If not, we can't promote
630 // the memset.
631 Value *SplatValue = MSI->getValue();
632 if (!SplatValue || !CurLoop->isLoopInvariant(SplatValue))
633 return false;
634
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000635 SmallPtrSet<Instruction *, 1> MSIs;
636 MSIs.insert(MSI);
Chad Rosier81362a82016-02-12 21:03:23 +0000637 bool NegStride = SizeInBytes == -Stride;
Chris Lattner0f4a6402011-02-19 19:31:39 +0000638 return processLoopStridedStore(Pointer, (unsigned)SizeInBytes,
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000639 MSI->getAlignment(), SplatValue, MSI, MSIs, Ev,
Chad Rosier81362a82016-02-12 21:03:23 +0000640 BECount, NegStride);
Chris Lattner86438102011-01-04 07:46:33 +0000641}
642
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000643/// mayLoopAccessLocation - Return true if the specified loop might access the
644/// specified pointer location, which is a loop-strided access. The 'Access'
645/// argument specifies what the verboten forms of access are (read or write).
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000646static bool
647mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L,
648 const SCEV *BECount, unsigned StoreSize,
649 AliasAnalysis &AA,
650 SmallPtrSetImpl<Instruction *> &IgnoredStores) {
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000651 // Get the location that may be stored across the loop. Since the access is
652 // strided positively through memory, we say that the modified location starts
653 // at the pointer and has infinite size.
Chandler Carruthecbd1682015-06-17 07:21:38 +0000654 uint64_t AccessSize = MemoryLocation::UnknownSize;
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000655
656 // If the loop iterates a fixed number of times, we can refine the access size
657 // to be exactly the size of the memset, which is (BECount+1)*StoreSize
658 if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
Chandler Carruthbad690e2015-08-12 23:06:37 +0000659 AccessSize = (BECst->getValue()->getZExtValue() + 1) * StoreSize;
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000660
661 // TODO: For this to be really effective, we have to dive into the pointer
662 // operand in the store. Store to &A[i] of 100 will always return may alias
663 // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
664 // which will then no-alias a store to &A[100].
Chandler Carruthac80dc72015-06-17 07:18:54 +0000665 MemoryLocation StoreLoc(Ptr, AccessSize);
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000666
667 for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E;
668 ++BI)
Benjamin Kramer135f7352016-06-26 12:28:59 +0000669 for (Instruction &I : **BI)
670 if (IgnoredStores.count(&I) == 0 &&
671 (AA.getModRefInfo(&I, StoreLoc) & Access))
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000672 return true;
673
674 return false;
675}
676
Chad Rosiered0c7d12015-11-13 19:11:07 +0000677// If we have a negative stride, Start refers to the end of the memory location
678// we're trying to memset. Therefore, we need to recompute the base pointer,
679// which is just Start - BECount*Size.
680static const SCEV *getStartForNegStride(const SCEV *Start, const SCEV *BECount,
681 Type *IntPtr, unsigned StoreSize,
682 ScalarEvolution *SE) {
683 const SCEV *Index = SE->getTruncateOrZeroExtend(BECount, IntPtr);
684 if (StoreSize != 1)
685 Index = SE->getMulExpr(Index, SE->getConstant(IntPtr, StoreSize),
686 SCEV::FlagNUW);
687 return SE->getMinusSCEV(Start, Index);
688}
689
Chris Lattner0f4a6402011-02-19 19:31:39 +0000690/// processLoopStridedStore - We see a strided store of some value. If we can
691/// transform this into a memset or memset_pattern in the loop preheader, do so.
Chandler Carruthbad690e2015-08-12 23:06:37 +0000692bool LoopIdiomRecognize::processLoopStridedStore(
693 Value *DestPtr, unsigned StoreSize, unsigned StoreAlignment,
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000694 Value *StoredVal, Instruction *TheStore,
695 SmallPtrSetImpl<Instruction *> &Stores, const SCEVAddRecExpr *Ev,
Chad Rosier79676142015-10-28 14:38:49 +0000696 const SCEV *BECount, bool NegStride) {
Chris Lattner0f4a6402011-02-19 19:31:39 +0000697 Value *SplatValue = isBytewiseValue(StoredVal);
Craig Topperf40110f2014-04-25 05:29:35 +0000698 Constant *PatternValue = nullptr;
Matt Arsenault009faed2013-09-11 05:09:42 +0000699
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000700 if (!SplatValue)
701 PatternValue = getMemSetPatternValue(StoredVal, DL);
702
703 assert((SplatValue || PatternValue) &&
704 "Expected either splat value or pattern value.");
Andrew Trick328b2232011-03-14 16:48:10 +0000705
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +0000706 // The trip count of the loop and the base pointer of the addrec SCEV is
707 // guaranteed to be loop invariant, which means that it should dominate the
708 // header. This allows us to insert code for it in the preheader.
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000709 unsigned DestAS = DestPtr->getType()->getPointerAddressSpace();
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +0000710 BasicBlock *Preheader = CurLoop->getLoopPreheader();
711 IRBuilder<> Builder(Preheader->getTerminator());
Chad Rosier43f9b482015-11-06 16:33:57 +0000712 SCEVExpander Expander(*SE, *DL, "loop-idiom");
Andrew Trick60ab3ef2011-06-28 05:04:16 +0000713
Matt Arsenault009faed2013-09-11 05:09:42 +0000714 Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS);
Chad Rosier43f9b482015-11-06 16:33:57 +0000715 Type *IntPtr = Builder.getIntPtrTy(*DL, DestAS);
Chad Rosier79676142015-10-28 14:38:49 +0000716
717 const SCEV *Start = Ev->getStart();
Chad Rosier2fa50a72015-11-13 19:13:40 +0000718 // Handle negative strided loops.
Chad Rosiered0c7d12015-11-13 19:11:07 +0000719 if (NegStride)
720 Start = getStartForNegStride(Start, BECount, IntPtr, StoreSize, SE);
Matt Arsenault009faed2013-09-11 05:09:42 +0000721
Chris Lattner29e14ed2010-12-26 23:42:51 +0000722 // Okay, we have a strided store "p[i]" of a splattable value. We can turn
Benjamin Kramerf77f2242012-10-21 19:31:16 +0000723 // this into a memset in the loop preheader now if we want. However, this
724 // would be unsafe to do if there is anything else in the loop that may read
Chandler Carruth7ec50852012-11-01 08:07:29 +0000725 // or write to the aliased location. Check for any overlap by generating the
726 // base pointer and checking the region.
Chad Rosier79676142015-10-28 14:38:49 +0000727 Value *BasePtr =
728 Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->getTerminator());
Chandler Carruth194f59c2015-07-22 23:15:57 +0000729 if (mayLoopAccessLocation(BasePtr, MRI_ModRef, CurLoop, BECount, StoreSize,
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000730 *AA, Stores)) {
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000731 Expander.clear();
732 // If we generated new code for the base pointer, clean up.
Benjamin Kramerf094d772015-02-07 21:37:08 +0000733 RecursivelyDeleteTriviallyDeadInstructions(BasePtr, TLI);
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000734 return false;
735 }
736
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +0000737 // Okay, everything looks good, insert the memset.
738
Chris Lattner29e14ed2010-12-26 23:42:51 +0000739 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
740 // pointer size if it isn't already.
Chris Lattner0ba473c2011-01-04 00:06:55 +0000741 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr);
Andrew Trick328b2232011-03-14 16:48:10 +0000742
Chandler Carruthbad690e2015-08-12 23:06:37 +0000743 const SCEV *NumBytesS =
Sanjoy Das2aacc0e2015-09-23 01:59:04 +0000744 SE->getAddExpr(BECount, SE->getOne(IntPtr), SCEV::FlagNUW);
Matt Arsenault5df49bd2013-09-11 05:09:35 +0000745 if (StoreSize != 1) {
Chris Lattner29e14ed2010-12-26 23:42:51 +0000746 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize),
Andrew Trick8b55b732011-03-14 16:50:06 +0000747 SCEV::FlagNUW);
Matt Arsenault5df49bd2013-09-11 05:09:35 +0000748 }
Andrew Trick328b2232011-03-14 16:48:10 +0000749
750 Value *NumBytes =
Chandler Carruthbad690e2015-08-12 23:06:37 +0000751 Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator());
Andrew Trick328b2232011-03-14 16:48:10 +0000752
Devang Pateld00c6282011-03-07 22:43:45 +0000753 CallInst *NewCall;
Matt Arsenault5df49bd2013-09-11 05:09:35 +0000754 if (SplatValue) {
Chandler Carruthbad690e2015-08-12 23:06:37 +0000755 NewCall =
756 Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, StoreAlignment);
Matt Arsenault5df49bd2013-09-11 05:09:35 +0000757 } else {
Matt Arsenault009faed2013-09-11 05:09:42 +0000758 // Everything is emitted in default address space
759 Type *Int8PtrTy = DestInt8PtrTy;
760
Sanjay Patelaf674fb2015-12-14 17:24:23 +0000761 Module *M = TheStore->getModule();
Chandler Carruthbad690e2015-08-12 23:06:37 +0000762 Value *MSP =
763 M->getOrInsertFunction("memset_pattern16", Builder.getVoidTy(),
764 Int8PtrTy, Int8PtrTy, IntPtr, (void *)nullptr);
Ahmed Bougachaace97c12016-04-27 19:04:50 +0000765 inferLibFuncAttributes(*M->getFunction("memset_pattern16"), *TLI);
Andrew Trick328b2232011-03-14 16:48:10 +0000766
Chris Lattner0f4a6402011-02-19 19:31:39 +0000767 // Otherwise we should form a memset_pattern16. PatternValue is known to be
768 // an constant array of 16-bytes. Plop the value into a mergable global.
769 GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true,
Benjamin Kramer838752d2015-03-03 00:17:09 +0000770 GlobalValue::PrivateLinkage,
Chris Lattner0f4a6402011-02-19 19:31:39 +0000771 PatternValue, ".memset_pattern");
Peter Collingbourne96efdd62016-06-14 21:01:22 +0000772 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); // Ok to merge these.
Chris Lattner0f4a6402011-02-19 19:31:39 +0000773 GV->setAlignment(16);
Matt Arsenault009faed2013-09-11 05:09:42 +0000774 Value *PatternPtr = ConstantExpr::getBitCast(GV, Int8PtrTy);
David Blaikieff6409d2015-05-18 22:13:54 +0000775 NewCall = Builder.CreateCall(MSP, {BasePtr, PatternPtr, NumBytes});
Chris Lattner0f4a6402011-02-19 19:31:39 +0000776 }
Andrew Trick328b2232011-03-14 16:48:10 +0000777
Chris Lattner29e14ed2010-12-26 23:42:51 +0000778 DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n"
Chris Lattner86438102011-01-04 07:46:33 +0000779 << " from store to: " << *Ev << " at: " << *TheStore << "\n");
Devang Pateld00c6282011-03-07 22:43:45 +0000780 NewCall->setDebugLoc(TheStore->getDebugLoc());
Andrew Trick328b2232011-03-14 16:48:10 +0000781
Chris Lattnerb9fe6852010-12-27 00:03:23 +0000782 // Okay, the memset has been formed. Zap the original store and anything that
783 // feeds into it.
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000784 for (auto *I : Stores)
David Majnemer41ff4fd2016-06-20 16:07:38 +0000785 deleteDeadInstruction(I);
Chris Lattner12f91be2011-01-02 07:36:44 +0000786 ++NumMemSet;
Chris Lattner29e14ed2010-12-26 23:42:51 +0000787 return true;
788}
789
Chad Rosier1cd3da12015-11-19 21:33:07 +0000790/// If the stored value is a strided load in the same loop with the same stride
791/// this may be transformable into a memcpy. This kicks in for stuff like
792/// for (i) A[i] = B[i];
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000793bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
794 const SCEV *BECount) {
795 assert(SI->isSimple() && "Expected only non-volatile stores.");
796
797 Value *StorePtr = SI->getPointerOperand();
798 const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
Chad Rosier4acff962016-02-12 19:05:27 +0000799 APInt Stride = getStoreStride(StoreEv);
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000800 unsigned StoreSize = getStoreSizeInBytes(SI, DL);
801 bool NegStride = StoreSize == -Stride;
Andrew Trick328b2232011-03-14 16:48:10 +0000802
Chad Rosierfddc01f2015-11-19 18:22:21 +0000803 // The store must be feeding a non-volatile load.
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000804 LoadInst *LI = cast<LoadInst>(SI->getValueOperand());
805 assert(LI->isSimple() && "Expected only non-volatile stores.");
Chad Rosierfddc01f2015-11-19 18:22:21 +0000806
807 // See if the pointer expression is an AddRec like {base,+,1} on the current
808 // loop, which indicates a strided load. If we have something else, it's a
809 // random load we can't handle.
Chad Rosier3ecc8d82015-11-19 18:25:11 +0000810 const SCEVAddRecExpr *LoadEv =
Haicheng Wu9d6c9402016-01-04 21:43:14 +0000811 cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
Andrew Trick328b2232011-03-14 16:48:10 +0000812
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +0000813 // The trip count of the loop and the base pointer of the addrec SCEV is
814 // guaranteed to be loop invariant, which means that it should dominate the
815 // header. This allows us to insert code for it in the preheader.
816 BasicBlock *Preheader = CurLoop->getLoopPreheader();
817 IRBuilder<> Builder(Preheader->getTerminator());
Chad Rosier43f9b482015-11-06 16:33:57 +0000818 SCEVExpander Expander(*SE, *DL, "loop-idiom");
Andrew Trick60ab3ef2011-06-28 05:04:16 +0000819
Chad Rosiercc299b62015-11-13 21:51:02 +0000820 const SCEV *StrStart = StoreEv->getStart();
821 unsigned StrAS = SI->getPointerAddressSpace();
822 Type *IntPtrTy = Builder.getIntPtrTy(*DL, StrAS);
823
824 // Handle negative strided loops.
825 if (NegStride)
826 StrStart = getStartForNegStride(StrStart, BECount, IntPtrTy, StoreSize, SE);
827
Chris Lattner85b6d812011-01-02 03:37:56 +0000828 // Okay, we have a strided store "p[i]" of a loaded value. We can turn
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000829 // this into a memcpy in the loop preheader now if we want. However, this
830 // would be unsafe to do if there is anything else in the loop that may read
831 // or write the memory region we're storing to. This includes the load that
832 // feeds the stores. Check for an alias by generating the base address and
833 // checking everything.
Chandler Carruthbad690e2015-08-12 23:06:37 +0000834 Value *StoreBasePtr = Expander.expandCodeFor(
Chad Rosiercc299b62015-11-13 21:51:02 +0000835 StrStart, Builder.getInt8PtrTy(StrAS), Preheader->getTerminator());
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000836
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000837 SmallPtrSet<Instruction *, 1> Stores;
838 Stores.insert(SI);
Chandler Carruth194f59c2015-07-22 23:15:57 +0000839 if (mayLoopAccessLocation(StoreBasePtr, MRI_ModRef, CurLoop, BECount,
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000840 StoreSize, *AA, Stores)) {
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000841 Expander.clear();
842 // If we generated new code for the base pointer, clean up.
Benjamin Kramerf094d772015-02-07 21:37:08 +0000843 RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000844 return false;
845 }
846
Chad Rosiercc299b62015-11-13 21:51:02 +0000847 const SCEV *LdStart = LoadEv->getStart();
848 unsigned LdAS = LI->getPointerAddressSpace();
849
850 // Handle negative strided loops.
851 if (NegStride)
852 LdStart = getStartForNegStride(LdStart, BECount, IntPtrTy, StoreSize, SE);
853
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000854 // For a memcpy, we have to make sure that the input array is not being
855 // mutated by the loop.
Chandler Carruthbad690e2015-08-12 23:06:37 +0000856 Value *LoadBasePtr = Expander.expandCodeFor(
Chad Rosiercc299b62015-11-13 21:51:02 +0000857 LdStart, Builder.getInt8PtrTy(LdAS), Preheader->getTerminator());
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +0000858
Chandler Carruth194f59c2015-07-22 23:15:57 +0000859 if (mayLoopAccessLocation(LoadBasePtr, MRI_Mod, CurLoop, BECount, StoreSize,
Haicheng Wuf1c00a22016-01-26 02:27:47 +0000860 *AA, Stores)) {
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000861 Expander.clear();
862 // If we generated new code for the base pointer, clean up.
Benjamin Kramerf094d772015-02-07 21:37:08 +0000863 RecursivelyDeleteTriviallyDeadInstructions(LoadBasePtr, TLI);
864 RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000865 return false;
866 }
867
Chris Lattnerc4ca7ab2011-05-22 17:39:56 +0000868 // Okay, everything is safe, we can transform this!
Andrew Trick60ab3ef2011-06-28 05:04:16 +0000869
Chris Lattner85b6d812011-01-02 03:37:56 +0000870 // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
871 // pointer size if it isn't already.
Matt Arsenault009faed2013-09-11 05:09:42 +0000872 BECount = SE->getTruncateOrZeroExtend(BECount, IntPtrTy);
Andrew Trick328b2232011-03-14 16:48:10 +0000873
Chandler Carruthbad690e2015-08-12 23:06:37 +0000874 const SCEV *NumBytesS =
Sanjoy Das2aacc0e2015-09-23 01:59:04 +0000875 SE->getAddExpr(BECount, SE->getOne(IntPtrTy), SCEV::FlagNUW);
Chris Lattner85b6d812011-01-02 03:37:56 +0000876 if (StoreSize != 1)
Matt Arsenault009faed2013-09-11 05:09:42 +0000877 NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtrTy, StoreSize),
Andrew Trick8b55b732011-03-14 16:50:06 +0000878 SCEV::FlagNUW);
Andrew Trick328b2232011-03-14 16:48:10 +0000879
Chris Lattner85b6d812011-01-02 03:37:56 +0000880 Value *NumBytes =
Chandler Carruthbad690e2015-08-12 23:06:37 +0000881 Expander.expandCodeFor(NumBytesS, IntPtrTy, Preheader->getTerminator());
Andrew Trick328b2232011-03-14 16:48:10 +0000882
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000883 CallInst *NewCall =
Chandler Carruthbad690e2015-08-12 23:06:37 +0000884 Builder.CreateMemCpy(StoreBasePtr, LoadBasePtr, NumBytes,
Pete Cooper67cf9a72015-11-19 05:56:52 +0000885 std::min(SI->getAlignment(), LI->getAlignment()));
Devang Patel0daa07e2011-05-04 21:37:05 +0000886 NewCall->setDebugLoc(SI->getDebugLoc());
Andrew Trick328b2232011-03-14 16:48:10 +0000887
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000888 DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n"
Chris Lattner85b6d812011-01-02 03:37:56 +0000889 << " from load ptr=" << *LoadEv << " at: " << *LI << "\n"
890 << " from store ptr=" << *StoreEv << " at: " << *SI << "\n");
Andrew Trick60ab3ef2011-06-28 05:04:16 +0000891
Chad Rosier7f08d802015-10-13 20:59:16 +0000892 // Okay, the memcpy has been formed. Zap the original store and anything that
Chris Lattner85b6d812011-01-02 03:37:56 +0000893 // feeds into it.
David Majnemer41ff4fd2016-06-20 16:07:38 +0000894 deleteDeadInstruction(SI);
Chandler Carruth099f5cb02012-11-02 08:33:25 +0000895 ++NumMemCpy;
Chris Lattner85b6d812011-01-02 03:37:56 +0000896 return true;
897}
Chandler Carruthd9c60702015-08-13 00:10:03 +0000898
899bool LoopIdiomRecognize::runOnNoncountableLoop() {
Chad Rosier19dc92d2015-11-09 16:56:06 +0000900 return recognizePopcount();
Chandler Carruthd9c60702015-08-13 00:10:03 +0000901}
Chandler Carruth8219a502015-08-13 00:44:29 +0000902
903/// Check if the given conditional branch is based on the comparison between
904/// a variable and zero, and if the variable is non-zero, the control yields to
905/// the loop entry. If the branch matches the behavior, the variable involved
906/// in the comparion is returned. This function will be called to see if the
907/// precondition and postcondition of the loop are in desirable form.
908static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry) {
909 if (!BI || !BI->isConditional())
910 return nullptr;
911
912 ICmpInst *Cond = dyn_cast<ICmpInst>(BI->getCondition());
913 if (!Cond)
914 return nullptr;
915
916 ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
917 if (!CmpZero || !CmpZero->isZero())
918 return nullptr;
919
920 ICmpInst::Predicate Pred = Cond->getPredicate();
921 if ((Pred == ICmpInst::ICMP_NE && BI->getSuccessor(0) == LoopEntry) ||
922 (Pred == ICmpInst::ICMP_EQ && BI->getSuccessor(1) == LoopEntry))
923 return Cond->getOperand(0);
924
925 return nullptr;
926}
927
928/// Return true iff the idiom is detected in the loop.
929///
930/// Additionally:
931/// 1) \p CntInst is set to the instruction counting the population bit.
932/// 2) \p CntPhi is set to the corresponding phi node.
933/// 3) \p Var is set to the value whose population bits are being counted.
934///
935/// The core idiom we are trying to detect is:
936/// \code
937/// if (x0 != 0)
938/// goto loop-exit // the precondition of the loop
939/// cnt0 = init-val;
940/// do {
941/// x1 = phi (x0, x2);
942/// cnt1 = phi(cnt0, cnt2);
943///
944/// cnt2 = cnt1 + 1;
945/// ...
946/// x2 = x1 & (x1 - 1);
947/// ...
948/// } while(x != 0);
949///
950/// loop-exit:
951/// \endcode
952static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB,
953 Instruction *&CntInst, PHINode *&CntPhi,
954 Value *&Var) {
955 // step 1: Check to see if the look-back branch match this pattern:
956 // "if (a!=0) goto loop-entry".
957 BasicBlock *LoopEntry;
958 Instruction *DefX2, *CountInst;
959 Value *VarX1, *VarX0;
960 PHINode *PhiX, *CountPhi;
961
962 DefX2 = CountInst = nullptr;
963 VarX1 = VarX0 = nullptr;
964 PhiX = CountPhi = nullptr;
965 LoopEntry = *(CurLoop->block_begin());
966
967 // step 1: Check if the loop-back branch is in desirable form.
968 {
969 if (Value *T = matchCondition(
970 dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry))
971 DefX2 = dyn_cast<Instruction>(T);
972 else
973 return false;
974 }
975
976 // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)"
977 {
978 if (!DefX2 || DefX2->getOpcode() != Instruction::And)
979 return false;
980
981 BinaryOperator *SubOneOp;
982
983 if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0))))
984 VarX1 = DefX2->getOperand(1);
985 else {
986 VarX1 = DefX2->getOperand(0);
987 SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1));
988 }
989 if (!SubOneOp)
990 return false;
991
992 Instruction *SubInst = cast<Instruction>(SubOneOp);
993 ConstantInt *Dec = dyn_cast<ConstantInt>(SubInst->getOperand(1));
994 if (!Dec ||
995 !((SubInst->getOpcode() == Instruction::Sub && Dec->isOne()) ||
996 (SubInst->getOpcode() == Instruction::Add &&
997 Dec->isAllOnesValue()))) {
998 return false;
999 }
1000 }
1001
1002 // step 3: Check the recurrence of variable X
1003 {
1004 PhiX = dyn_cast<PHINode>(VarX1);
1005 if (!PhiX ||
1006 (PhiX->getOperand(0) != DefX2 && PhiX->getOperand(1) != DefX2)) {
1007 return false;
1008 }
1009 }
1010
1011 // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1
1012 {
1013 CountInst = nullptr;
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001014 for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(),
Chandler Carruth8219a502015-08-13 00:44:29 +00001015 IterE = LoopEntry->end();
1016 Iter != IterE; Iter++) {
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001017 Instruction *Inst = &*Iter;
Chandler Carruth8219a502015-08-13 00:44:29 +00001018 if (Inst->getOpcode() != Instruction::Add)
1019 continue;
1020
1021 ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
1022 if (!Inc || !Inc->isOne())
1023 continue;
1024
1025 PHINode *Phi = dyn_cast<PHINode>(Inst->getOperand(0));
1026 if (!Phi || Phi->getParent() != LoopEntry)
1027 continue;
1028
1029 // Check if the result of the instruction is live of the loop.
1030 bool LiveOutLoop = false;
1031 for (User *U : Inst->users()) {
1032 if ((cast<Instruction>(U))->getParent() != LoopEntry) {
1033 LiveOutLoop = true;
1034 break;
1035 }
1036 }
1037
1038 if (LiveOutLoop) {
1039 CountInst = Inst;
1040 CountPhi = Phi;
1041 break;
1042 }
1043 }
1044
1045 if (!CountInst)
1046 return false;
1047 }
1048
1049 // step 5: check if the precondition is in this form:
1050 // "if (x != 0) goto loop-head ; else goto somewhere-we-don't-care;"
1051 {
1052 auto *PreCondBr = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1053 Value *T = matchCondition(PreCondBr, CurLoop->getLoopPreheader());
1054 if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1))
1055 return false;
1056
1057 CntInst = CountInst;
1058 CntPhi = CountPhi;
1059 Var = T;
1060 }
1061
1062 return true;
1063}
1064
1065/// Recognizes a population count idiom in a non-countable loop.
1066///
1067/// If detected, transforms the relevant code to issue the popcount intrinsic
1068/// function call, and returns true; otherwise, returns false.
1069bool LoopIdiomRecognize::recognizePopcount() {
Chandler Carruth8219a502015-08-13 00:44:29 +00001070 if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware)
1071 return false;
1072
1073 // Counting population are usually conducted by few arithmetic instructions.
Nick Lewycky06b0ea22015-08-18 22:41:58 +00001074 // Such instructions can be easily "absorbed" by vacant slots in a
Chandler Carruth8219a502015-08-13 00:44:29 +00001075 // non-compact loop. Therefore, recognizing popcount idiom only makes sense
1076 // in a compact loop.
1077
Renato Golin655348f2015-08-13 11:25:38 +00001078 // Give up if the loop has multiple blocks or multiple backedges.
1079 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
Chandler Carruth8219a502015-08-13 00:44:29 +00001080 return false;
1081
Renato Golin655348f2015-08-13 11:25:38 +00001082 BasicBlock *LoopBody = *(CurLoop->block_begin());
1083 if (LoopBody->size() >= 20) {
1084 // The loop is too big, bail out.
Chandler Carruth8219a502015-08-13 00:44:29 +00001085 return false;
Renato Golin655348f2015-08-13 11:25:38 +00001086 }
Chandler Carruth8219a502015-08-13 00:44:29 +00001087
1088 // It should have a preheader containing nothing but an unconditional branch.
Renato Golin655348f2015-08-13 11:25:38 +00001089 BasicBlock *PH = CurLoop->getLoopPreheader();
1090 if (!PH)
Chandler Carruth8219a502015-08-13 00:44:29 +00001091 return false;
Renato Golin655348f2015-08-13 11:25:38 +00001092 if (&PH->front() != PH->getTerminator())
1093 return false;
1094 auto *EntryBI = dyn_cast<BranchInst>(PH->getTerminator());
Chandler Carruth8219a502015-08-13 00:44:29 +00001095 if (!EntryBI || EntryBI->isConditional())
1096 return false;
1097
1098 // It should have a precondition block where the generated popcount instrinsic
1099 // function can be inserted.
Renato Golin655348f2015-08-13 11:25:38 +00001100 auto *PreCondBB = PH->getSinglePredecessor();
Chandler Carruth8219a502015-08-13 00:44:29 +00001101 if (!PreCondBB)
1102 return false;
1103 auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1104 if (!PreCondBI || PreCondBI->isUnconditional())
1105 return false;
1106
1107 Instruction *CntInst;
1108 PHINode *CntPhi;
1109 Value *Val;
1110 if (!detectPopcountIdiom(CurLoop, PreCondBB, CntInst, CntPhi, Val))
1111 return false;
1112
1113 transformLoopToPopcount(PreCondBB, CntInst, CntPhi, Val);
1114 return true;
1115}
1116
1117static CallInst *createPopcntIntrinsic(IRBuilder<> &IRBuilder, Value *Val,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001118 const DebugLoc &DL) {
Chandler Carruth8219a502015-08-13 00:44:29 +00001119 Value *Ops[] = {Val};
1120 Type *Tys[] = {Val->getType()};
1121
1122 Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent();
1123 Value *Func = Intrinsic::getDeclaration(M, Intrinsic::ctpop, Tys);
1124 CallInst *CI = IRBuilder.CreateCall(Func, Ops);
1125 CI->setDebugLoc(DL);
1126
1127 return CI;
1128}
1129
1130void LoopIdiomRecognize::transformLoopToPopcount(BasicBlock *PreCondBB,
1131 Instruction *CntInst,
1132 PHINode *CntPhi, Value *Var) {
1133 BasicBlock *PreHead = CurLoop->getLoopPreheader();
1134 auto *PreCondBr = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1135 const DebugLoc DL = CntInst->getDebugLoc();
1136
1137 // Assuming before transformation, the loop is following:
1138 // if (x) // the precondition
1139 // do { cnt++; x &= x - 1; } while(x);
1140
1141 // Step 1: Insert the ctpop instruction at the end of the precondition block
1142 IRBuilder<> Builder(PreCondBr);
1143 Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
1144 {
1145 PopCnt = createPopcntIntrinsic(Builder, Var, DL);
1146 NewCount = PopCntZext =
1147 Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType()));
1148
1149 if (NewCount != PopCnt)
1150 (cast<Instruction>(NewCount))->setDebugLoc(DL);
1151
1152 // TripCnt is exactly the number of iterations the loop has
1153 TripCnt = NewCount;
1154
1155 // If the population counter's initial value is not zero, insert Add Inst.
1156 Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead);
1157 ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
1158 if (!InitConst || !InitConst->isZero()) {
1159 NewCount = Builder.CreateAdd(NewCount, CntInitVal);
1160 (cast<Instruction>(NewCount))->setDebugLoc(DL);
1161 }
1162 }
1163
Nick Lewycky2c852542015-08-19 06:22:33 +00001164 // Step 2: Replace the precondition from "if (x == 0) goto loop-exit" to
Nick Lewycky1098e492015-08-19 06:25:30 +00001165 // "if (NewCount == 0) loop-exit". Without this change, the intrinsic
Chandler Carruth8219a502015-08-13 00:44:29 +00001166 // function would be partial dead code, and downstream passes will drag
1167 // it back from the precondition block to the preheader.
1168 {
1169 ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition());
1170
1171 Value *Opnd0 = PopCntZext;
1172 Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0);
1173 if (PreCond->getOperand(0) != Var)
1174 std::swap(Opnd0, Opnd1);
1175
1176 ICmpInst *NewPreCond = cast<ICmpInst>(
1177 Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1));
1178 PreCondBr->setCondition(NewPreCond);
1179
1180 RecursivelyDeleteTriviallyDeadInstructions(PreCond, TLI);
1181 }
1182
1183 // Step 3: Note that the population count is exactly the trip count of the
Nick Lewycky1098e492015-08-19 06:25:30 +00001184 // loop in question, which enable us to to convert the loop from noncountable
Chandler Carruth8219a502015-08-13 00:44:29 +00001185 // loop into a countable one. The benefit is twofold:
1186 //
Nick Lewycky2c852542015-08-19 06:22:33 +00001187 // - If the loop only counts population, the entire loop becomes dead after
1188 // the transformation. It is a lot easier to prove a countable loop dead
1189 // than to prove a noncountable one. (In some C dialects, an infinite loop
Chandler Carruth8219a502015-08-13 00:44:29 +00001190 // isn't dead even if it computes nothing useful. In general, DCE needs
1191 // to prove a noncountable loop finite before safely delete it.)
1192 //
1193 // - If the loop also performs something else, it remains alive.
1194 // Since it is transformed to countable form, it can be aggressively
1195 // optimized by some optimizations which are in general not applicable
1196 // to a noncountable loop.
1197 //
1198 // After this step, this loop (conceptually) would look like following:
1199 // newcnt = __builtin_ctpop(x);
1200 // t = newcnt;
1201 // if (x)
1202 // do { cnt++; x &= x-1; t--) } while (t > 0);
1203 BasicBlock *Body = *(CurLoop->block_begin());
1204 {
1205 auto *LbBr = dyn_cast<BranchInst>(Body->getTerminator());
1206 ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
1207 Type *Ty = TripCnt->getType();
1208
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +00001209 PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front());
Chandler Carruth8219a502015-08-13 00:44:29 +00001210
1211 Builder.SetInsertPoint(LbCond);
Chandler Carruth8219a502015-08-13 00:44:29 +00001212 Instruction *TcDec = cast<Instruction>(
Nick Lewycky1098e492015-08-19 06:25:30 +00001213 Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1),
1214 "tcdec", false, true));
Chandler Carruth8219a502015-08-13 00:44:29 +00001215
1216 TcPhi->addIncoming(TripCnt, PreHead);
1217 TcPhi->addIncoming(TcDec, Body);
1218
1219 CmpInst::Predicate Pred =
1220 (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_UGT : CmpInst::ICMP_SLE;
1221 LbCond->setPredicate(Pred);
1222 LbCond->setOperand(0, TcDec);
Nick Lewycky2c852542015-08-19 06:22:33 +00001223 LbCond->setOperand(1, ConstantInt::get(Ty, 0));
Chandler Carruth8219a502015-08-13 00:44:29 +00001224 }
1225
1226 // Step 4: All the references to the original population counter outside
1227 // the loop are replaced with the NewCount -- the value returned from
1228 // __builtin_ctpop().
1229 CntInst->replaceUsesOutsideBlock(NewCount, Body);
1230
1231 // step 5: Forget the "non-computable" trip-count SCEV associated with the
1232 // loop. The loop would otherwise not be deleted even if it becomes empty.
1233 SE->forgetLoop(CurLoop);
1234}