1 //===- LoopIdiomRecognize.cpp - Loop idiom recognition --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass implements an idiom recognizer that transforms simple loops into a
10 // non-loop form.  In cases that this kicks in, it can be a significant
11 // performance win.
12 //
13 // If compiling for code size we avoid idiom recognition if the resulting
14 // code could be larger than the code for the original loop. One way this could
15 // happen is if the loop is not removable after idiom recognition due to the
16 // presence of non-idiom instructions. The initial implementation of the
17 // heuristics applies to idioms in multi-block loops.
18 //
19 //===----------------------------------------------------------------------===//
20 //
21 // TODO List:
22 //
23 // Future loop memory idioms to recognize:
24 //   memcmp, strlen, etc.
25 // Future floating point idioms to recognize in -ffast-math mode:
26 //   fpowi
27 // Future integer operation idioms to recognize:
28 //   ctpop
29 //
30 // Beware that isel's default lowering for ctpop is highly inefficient for
31 // i64 and larger types when i64 is legal and the value has few bits set.  It
32 // would be good to enhance isel to emit a loop for ctpop in this case.
33 //
34 // This could recognize common matrix multiplies and dot product idioms and
35 // replace them with calls to BLAS (if linked in??).
36 //
37 //===----------------------------------------------------------------------===//
38 
39 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
40 #include "llvm/ADT/APInt.h"
41 #include "llvm/ADT/ArrayRef.h"
42 #include "llvm/ADT/DenseMap.h"
43 #include "llvm/ADT/MapVector.h"
44 #include "llvm/ADT/SetVector.h"
45 #include "llvm/ADT/SmallPtrSet.h"
46 #include "llvm/ADT/SmallVector.h"
47 #include "llvm/ADT/Statistic.h"
48 #include "llvm/ADT/StringRef.h"
49 #include "llvm/Analysis/AliasAnalysis.h"
50 #include "llvm/Analysis/CmpInstAnalysis.h"
51 #include "llvm/Analysis/LoopAccessAnalysis.h"
52 #include "llvm/Analysis/LoopInfo.h"
53 #include "llvm/Analysis/LoopPass.h"
54 #include "llvm/Analysis/MemoryLocation.h"
55 #include "llvm/Analysis/MemorySSA.h"
56 #include "llvm/Analysis/MemorySSAUpdater.h"
57 #include "llvm/Analysis/MustExecute.h"
58 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
59 #include "llvm/Analysis/ScalarEvolution.h"
60 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
61 #include "llvm/Analysis/TargetLibraryInfo.h"
62 #include "llvm/Analysis/TargetTransformInfo.h"
63 #include "llvm/Analysis/ValueTracking.h"
64 #include "llvm/IR/Attributes.h"
65 #include "llvm/IR/BasicBlock.h"
66 #include "llvm/IR/Constant.h"
67 #include "llvm/IR/Constants.h"
68 #include "llvm/IR/DataLayout.h"
69 #include "llvm/IR/DebugLoc.h"
70 #include "llvm/IR/DerivedTypes.h"
71 #include "llvm/IR/Dominators.h"
72 #include "llvm/IR/GlobalValue.h"
73 #include "llvm/IR/GlobalVariable.h"
74 #include "llvm/IR/IRBuilder.h"
75 #include "llvm/IR/InstrTypes.h"
76 #include "llvm/IR/Instruction.h"
77 #include "llvm/IR/Instructions.h"
78 #include "llvm/IR/IntrinsicInst.h"
79 #include "llvm/IR/Intrinsics.h"
80 #include "llvm/IR/LLVMContext.h"
81 #include "llvm/IR/Module.h"
82 #include "llvm/IR/PassManager.h"
83 #include "llvm/IR/PatternMatch.h"
84 #include "llvm/IR/Type.h"
85 #include "llvm/IR/User.h"
86 #include "llvm/IR/Value.h"
87 #include "llvm/IR/ValueHandle.h"
88 #include "llvm/InitializePasses.h"
89 #include "llvm/Pass.h"
90 #include "llvm/Support/Casting.h"
91 #include "llvm/Support/CommandLine.h"
92 #include "llvm/Support/Debug.h"
93 #include "llvm/Support/InstructionCost.h"
94 #include "llvm/Support/raw_ostream.h"
95 #include "llvm/Transforms/Scalar.h"
96 #include "llvm/Transforms/Utils/BuildLibCalls.h"
97 #include "llvm/Transforms/Utils/Local.h"
98 #include "llvm/Transforms/Utils/LoopUtils.h"
99 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
100 #include <algorithm>
101 #include <cassert>
102 #include <cstdint>
103 #include <utility>
104 #include <vector>
105 
106 using namespace llvm;
107 
108 #define DEBUG_TYPE "loop-idiom"
109 
110 STATISTIC(NumMemSet, "Number of memset's formed from loop stores");
111 STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores");
112 STATISTIC(NumMemMove, "Number of memmove's formed from loop load+stores");
113 STATISTIC(
114     NumShiftUntilBitTest,
115     "Number of uncountable loops recognized as 'shift until bitttest' idiom");
116 STATISTIC(NumShiftUntilZero,
117           "Number of uncountable loops recognized as 'shift until zero' idiom");
118 
119 bool DisableLIRP::All;
120 static cl::opt<bool, true>
121     DisableLIRPAll("disable-" DEBUG_TYPE "-all",
122                    cl::desc("Options to disable Loop Idiom Recognize Pass."),
123                    cl::location(DisableLIRP::All), cl::init(false),
124                    cl::ReallyHidden);
125 
126 bool DisableLIRP::Memset;
127 static cl::opt<bool, true>
128     DisableLIRPMemset("disable-" DEBUG_TYPE "-memset",
129                       cl::desc("Proceed with loop idiom recognize pass, but do "
130                                "not convert loop(s) to memset."),
131                       cl::location(DisableLIRP::Memset), cl::init(false),
132                       cl::ReallyHidden);
133 
134 bool DisableLIRP::Memcpy;
135 static cl::opt<bool, true>
136     DisableLIRPMemcpy("disable-" DEBUG_TYPE "-memcpy",
137                       cl::desc("Proceed with loop idiom recognize pass, but do "
138                                "not convert loop(s) to memcpy."),
139                       cl::location(DisableLIRP::Memcpy), cl::init(false),
140                       cl::ReallyHidden);
141 
142 static cl::opt<bool> UseLIRCodeSizeHeurs(
143     "use-lir-code-size-heurs",
144     cl::desc("Use loop idiom recognition code size heuristics when compiling"
145              "with -Os/-Oz"),
146     cl::init(true), cl::Hidden);
147 
148 namespace {
149 
150 class LoopIdiomRecognize {
151   Loop *CurLoop = nullptr;
152   AliasAnalysis *AA;
153   DominatorTree *DT;
154   LoopInfo *LI;
155   ScalarEvolution *SE;
156   TargetLibraryInfo *TLI;
157   const TargetTransformInfo *TTI;
158   const DataLayout *DL;
159   OptimizationRemarkEmitter &ORE;
160   bool ApplyCodeSizeHeuristics;
161   std::unique_ptr<MemorySSAUpdater> MSSAU;
162 
163 public:
164   explicit LoopIdiomRecognize(AliasAnalysis *AA, DominatorTree *DT,
165                               LoopInfo *LI, ScalarEvolution *SE,
166                               TargetLibraryInfo *TLI,
167                               const TargetTransformInfo *TTI, MemorySSA *MSSA,
168                               const DataLayout *DL,
169                               OptimizationRemarkEmitter &ORE)
170       : AA(AA), DT(DT), LI(LI), SE(SE), TLI(TLI), TTI(TTI), DL(DL), ORE(ORE) {
171     if (MSSA)
172       MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
173   }
174 
175   bool runOnLoop(Loop *L);
176 
177 private:
178   using StoreList = SmallVector<StoreInst *, 8>;
179   using StoreListMap = MapVector<Value *, StoreList>;
180 
181   StoreListMap StoreRefsForMemset;
182   StoreListMap StoreRefsForMemsetPattern;
183   StoreList StoreRefsForMemcpy;
184   bool HasMemset;
185   bool HasMemsetPattern;
186   bool HasMemcpy;
187 
188   /// Return code for isLegalStore()
189   enum LegalStoreKind {
190     None = 0,
191     Memset,
192     MemsetPattern,
193     Memcpy,
194     UnorderedAtomicMemcpy,
195     DontUse // Dummy retval never to be used. Allows catching errors in retval
196             // handling.
197   };
198 
199   /// \name Countable Loop Idiom Handling
200   /// @{
201 
202   bool runOnCountableLoop();
203   bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
204                       SmallVectorImpl<BasicBlock *> &ExitBlocks);
205 
206   void collectStores(BasicBlock *BB);
207   LegalStoreKind isLegalStore(StoreInst *SI);
208   enum class ForMemset { No, Yes };
209   bool processLoopStores(SmallVectorImpl<StoreInst *> &SL, const SCEV *BECount,
210                          ForMemset For);
211 
212   template <typename MemInst>
213   bool processLoopMemIntrinsic(
214       BasicBlock *BB,
215       bool (LoopIdiomRecognize::*Processor)(MemInst *, const SCEV *),
216       const SCEV *BECount);
217   bool processLoopMemCpy(MemCpyInst *MCI, const SCEV *BECount);
218   bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount);
219 
220   bool processLoopStridedStore(Value *DestPtr, const SCEV *StoreSizeSCEV,
221                                MaybeAlign StoreAlignment, Value *StoredVal,
222                                Instruction *TheStore,
223                                SmallPtrSetImpl<Instruction *> &Stores,
224                                const SCEVAddRecExpr *Ev, const SCEV *BECount,
225                                bool IsNegStride, bool IsLoopMemset = false);
226   bool processLoopStoreOfLoopLoad(StoreInst *SI, const SCEV *BECount);
227   bool processLoopStoreOfLoopLoad(Value *DestPtr, Value *SourcePtr,
228                                   const SCEV *StoreSize, MaybeAlign StoreAlign,
229                                   MaybeAlign LoadAlign, Instruction *TheStore,
230                                   Instruction *TheLoad,
231                                   const SCEVAddRecExpr *StoreEv,
232                                   const SCEVAddRecExpr *LoadEv,
233                                   const SCEV *BECount);
234   bool avoidLIRForMultiBlockLoop(bool IsMemset = false,
235                                  bool IsLoopMemset = false);
236 
237   /// @}
238   /// \name Noncountable Loop Idiom Handling
239   /// @{
240 
241   bool runOnNoncountableLoop();
242 
243   bool recognizePopcount();
244   void transformLoopToPopcount(BasicBlock *PreCondBB, Instruction *CntInst,
245                                PHINode *CntPhi, Value *Var);
246   bool recognizeAndInsertFFS();  /// Find First Set: ctlz or cttz
247   void transformLoopToCountable(Intrinsic::ID IntrinID, BasicBlock *PreCondBB,
248                                 Instruction *CntInst, PHINode *CntPhi,
249                                 Value *Var, Instruction *DefX,
250                                 const DebugLoc &DL, bool ZeroCheck,
251                                 bool IsCntPhiUsedOutsideLoop);
252 
253   bool recognizeShiftUntilBitTest();
254   bool recognizeShiftUntilZero();
255 
256   /// @}
257 };
258 
259 class LoopIdiomRecognizeLegacyPass : public LoopPass {
260 public:
261   static char ID;
262 
263   explicit LoopIdiomRecognizeLegacyPass() : LoopPass(ID) {
264     initializeLoopIdiomRecognizeLegacyPassPass(
265         *PassRegistry::getPassRegistry());
266   }
267 
268   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
269     if (DisableLIRP::All)
270       return false;
271 
272     if (skipLoop(L))
273       return false;
274 
275     AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
276     DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
277     LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
278     ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
279     TargetLibraryInfo *TLI =
280         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(
281             *L->getHeader()->getParent());
282     const TargetTransformInfo *TTI =
283         &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
284             *L->getHeader()->getParent());
285     const DataLayout *DL = &L->getHeader()->getModule()->getDataLayout();
286     auto *MSSAAnalysis = getAnalysisIfAvailable<MemorySSAWrapperPass>();
287     MemorySSA *MSSA = nullptr;
288     if (MSSAAnalysis)
289       MSSA = &MSSAAnalysis->getMSSA();
290 
291     // For the old PM, we can't use OptimizationRemarkEmitter as an analysis
292     // pass.  Function analyses need to be preserved across loop transformations
293     // but ORE cannot be preserved (see comment before the pass definition).
294     OptimizationRemarkEmitter ORE(L->getHeader()->getParent());
295 
296     LoopIdiomRecognize LIR(AA, DT, LI, SE, TLI, TTI, MSSA, DL, ORE);
297     return LIR.runOnLoop(L);
298   }
299 
300   /// This transformation requires natural loop information & requires that
301   /// loop preheaders be inserted into the CFG.
302   void getAnalysisUsage(AnalysisUsage &AU) const override {
303     AU.addRequired<TargetLibraryInfoWrapperPass>();
304     AU.addRequired<TargetTransformInfoWrapperPass>();
305     AU.addPreserved<MemorySSAWrapperPass>();
306     getLoopAnalysisUsage(AU);
307   }
308 };
309 
310 // The Folder will fold expressions that are guarded by the loop entry.
311 class SCEVSignToZeroExtentionRewriter
312     : public SCEVRewriteVisitor<SCEVSignToZeroExtentionRewriter> {
313 public:
314   ScalarEvolution &SE;
315   const Loop *CurLoop;
316   SCEVSignToZeroExtentionRewriter(ScalarEvolution &SE, const Loop *CurLoop)
317       : SCEVRewriteVisitor(SE), SE(SE), CurLoop(CurLoop) {}
318 
319   const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
320     // If expression is guarded by CurLoop to be greater or equal to zero
321     // then convert sext to zext. Otherwise return the original expression.
322     if (SE.isLoopEntryGuardedByCond(CurLoop, ICmpInst::ICMP_SGE, Expr,
323                                     SE.getZero(Expr->getType())))
324       return SE.getZeroExtendExpr(visit(Expr->getOperand()), Expr->getType());
325     return Expr;
326   }
327 };
328 
329 } // end anonymous namespace
330 
331 char LoopIdiomRecognizeLegacyPass::ID = 0;
332 
333 PreservedAnalyses LoopIdiomRecognizePass::run(Loop &L, LoopAnalysisManager &AM,
334                                               LoopStandardAnalysisResults &AR,
335                                               LPMUpdater &) {
336   if (DisableLIRP::All)
337     return PreservedAnalyses::all();
338 
339   const auto *DL = &L.getHeader()->getModule()->getDataLayout();
340 
341   // For the new PM, we also can't use OptimizationRemarkEmitter as an analysis
342   // pass.  Function analyses need to be preserved across loop transformations
343   // but ORE cannot be preserved (see comment before the pass definition).
344   OptimizationRemarkEmitter ORE(L.getHeader()->getParent());
345 
346   LoopIdiomRecognize LIR(&AR.AA, &AR.DT, &AR.LI, &AR.SE, &AR.TLI, &AR.TTI,
347                          AR.MSSA, DL, ORE);
348   if (!LIR.runOnLoop(&L))
349     return PreservedAnalyses::all();
350 
351   auto PA = getLoopPassPreservedAnalyses();
352   if (AR.MSSA)
353     PA.preserve<MemorySSAAnalysis>();
354   return PA;
355 }
356 
357 INITIALIZE_PASS_BEGIN(LoopIdiomRecognizeLegacyPass, "loop-idiom",
358                       "Recognize loop idioms", false, false)
359 INITIALIZE_PASS_DEPENDENCY(LoopPass)
360 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
361 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
362 INITIALIZE_PASS_END(LoopIdiomRecognizeLegacyPass, "loop-idiom",
363                     "Recognize loop idioms", false, false)
364 
365 Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognizeLegacyPass(); }
366 
367 static void deleteDeadInstruction(Instruction *I) {
368   I->replaceAllUsesWith(UndefValue::get(I->getType()));
369   I->eraseFromParent();
370 }
371 
372 //===----------------------------------------------------------------------===//
373 //
374 //          Implementation of LoopIdiomRecognize
375 //
376 //===----------------------------------------------------------------------===//
377 
378 bool LoopIdiomRecognize::runOnLoop(Loop *L) {
379   CurLoop = L;
380   // If the loop could not be converted to canonical form, it must have an
381   // indirectbr in it, just give up.
382   if (!L->getLoopPreheader())
383     return false;
384 
385   // Disable loop idiom recognition if the function's name is a common idiom.
386   StringRef Name = L->getHeader()->getParent()->getName();
387   if (Name == "memset" || Name == "memcpy")
388     return false;
389 
390   // Determine if code size heuristics need to be applied.
391   ApplyCodeSizeHeuristics =
392       L->getHeader()->getParent()->hasOptSize() && UseLIRCodeSizeHeurs;
393 
394   HasMemset = TLI->has(LibFunc_memset);
395   HasMemsetPattern = TLI->has(LibFunc_memset_pattern16);
396   HasMemcpy = TLI->has(LibFunc_memcpy);
397 
398   if (HasMemset || HasMemsetPattern || HasMemcpy)
399     if (SE->hasLoopInvariantBackedgeTakenCount(L))
400       return runOnCountableLoop();
401 
402   return runOnNoncountableLoop();
403 }
404 
405 bool LoopIdiomRecognize::runOnCountableLoop() {
406   const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop);
407   assert(!isa<SCEVCouldNotCompute>(BECount) &&
408          "runOnCountableLoop() called on a loop without a predictable"
409          "backedge-taken count");
410 
411   // If this loop executes exactly one time, then it should be peeled, not
412   // optimized by this pass.
413   if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
414     if (BECst->getAPInt() == 0)
415       return false;
416 
417   SmallVector<BasicBlock *, 8> ExitBlocks;
418   CurLoop->getUniqueExitBlocks(ExitBlocks);
419 
420   LLVM_DEBUG(dbgs() << DEBUG_TYPE " Scanning: F["
421                     << CurLoop->getHeader()->getParent()->getName()
422                     << "] Countable Loop %" << CurLoop->getHeader()->getName()
423                     << "\n");
424 
425   // The following transforms hoist stores/memsets into the loop pre-header.
426   // Give up if the loop has instructions that may throw.
427   SimpleLoopSafetyInfo SafetyInfo;
428   SafetyInfo.computeLoopSafetyInfo(CurLoop);
429   if (SafetyInfo.anyBlockMayThrow())
430     return false;
431 
432   bool MadeChange = false;
433 
434   // Scan all the blocks in the loop that are not in subloops.
435   for (auto *BB : CurLoop->getBlocks()) {
436     // Ignore blocks in subloops.
437     if (LI->getLoopFor(BB) != CurLoop)
438       continue;
439 
440     MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks);
441   }
442   return MadeChange;
443 }
444 
445 static APInt getStoreStride(const SCEVAddRecExpr *StoreEv) {
446   const SCEVConstant *ConstStride = cast<SCEVConstant>(StoreEv->getOperand(1));
447   return ConstStride->getAPInt();
448 }
449 
450 /// getMemSetPatternValue - If a strided store of the specified value is safe to
451 /// turn into a memset_pattern16, return a ConstantArray of 16 bytes that should
452 /// be passed in.  Otherwise, return null.
453 ///
454 /// Note that we don't ever attempt to use memset_pattern8 or 4, because these
455 /// just replicate their input array and then pass on to memset_pattern16.
456 static Constant *getMemSetPatternValue(Value *V, const DataLayout *DL) {
457   // FIXME: This could check for UndefValue because it can be merged into any
458   // other valid pattern.
459 
460   // If the value isn't a constant, we can't promote it to being in a constant
461   // array.  We could theoretically do a store to an alloca or something, but
462   // that doesn't seem worthwhile.
463   Constant *C = dyn_cast<Constant>(V);
464   if (!C)
465     return nullptr;
466 
467   // Only handle simple values that are a power of two bytes in size.
468   uint64_t Size = DL->getTypeSizeInBits(V->getType());
469   if (Size == 0 || (Size & 7) || (Size & (Size - 1)))
470     return nullptr;
471 
472   // Don't care enough about darwin/ppc to implement this.
473   if (DL->isBigEndian())
474     return nullptr;
475 
476   // Convert to size in bytes.
477   Size /= 8;
478 
479   // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see
480   // if the top and bottom are the same (e.g. for vectors and large integers).
481   if (Size > 16)
482     return nullptr;
483 
484   // If the constant is exactly 16 bytes, just use it.
485   if (Size == 16)
486     return C;
487 
488   // Otherwise, we'll use an array of the constants.
489   unsigned ArraySize = 16 / Size;
490   ArrayType *AT = ArrayType::get(V->getType(), ArraySize);
491   return ConstantArray::get(AT, std::vector<Constant *>(ArraySize, C));
492 }
493 
494 LoopIdiomRecognize::LegalStoreKind
495 LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
496   // Don't touch volatile stores.
497   if (SI->isVolatile())
498     return LegalStoreKind::None;
499   // We only want simple or unordered-atomic stores.
500   if (!SI->isUnordered())
501     return LegalStoreKind::None;
502 
503   // Avoid merging nontemporal stores.
504   if (SI->getMetadata(LLVMContext::MD_nontemporal))
505     return LegalStoreKind::None;
506 
507   Value *StoredVal = SI->getValueOperand();
508   Value *StorePtr = SI->getPointerOperand();
509 
510   // Don't convert stores of non-integral pointer types to memsets (which stores
511   // integers).
512   if (DL->isNonIntegralPointerType(StoredVal->getType()->getScalarType()))
513     return LegalStoreKind::None;
514 
515   // Reject stores that are so large that they overflow an unsigned.
516   // When storing out scalable vectors we bail out for now, since the code
517   // below currently only works for constant strides.
518   TypeSize SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
519   if (SizeInBits.isScalable() || (SizeInBits.getFixedSize() & 7) ||
520       (SizeInBits.getFixedSize() >> 32) != 0)
521     return LegalStoreKind::None;
522 
523   // See if the pointer expression is an AddRec like {base,+,1} on the current
524   // loop, which indicates a strided store.  If we have something else, it's a
525   // random store we can't handle.
526   const SCEVAddRecExpr *StoreEv =
527       dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
528   if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
529     return LegalStoreKind::None;
530 
531   // Check to see if we have a constant stride.
532   if (!isa<SCEVConstant>(StoreEv->getOperand(1)))
533     return LegalStoreKind::None;
534 
535   // See if the store can be turned into a memset.
536 
537   // If the stored value is a byte-wise value (like i32 -1), then it may be
538   // turned into a memset of i8 -1, assuming that all the consecutive bytes
539   // are stored.  A store of i32 0x01020304 can never be turned into a memset,
540   // but it can be turned into memset_pattern if the target supports it.
541   Value *SplatValue = isBytewiseValue(StoredVal, *DL);
542 
543   // Note: memset and memset_pattern on unordered-atomic is yet not supported
544   bool UnorderedAtomic = SI->isUnordered() && !SI->isSimple();
545 
546   // If we're allowed to form a memset, and the stored value would be
547   // acceptable for memset, use it.
548   if (!UnorderedAtomic && HasMemset && SplatValue && !DisableLIRP::Memset &&
549       // Verify that the stored value is loop invariant.  If not, we can't
550       // promote the memset.
551       CurLoop->isLoopInvariant(SplatValue)) {
552     // It looks like we can use SplatValue.
553     return LegalStoreKind::Memset;
554   }
555   if (!UnorderedAtomic && HasMemsetPattern && !DisableLIRP::Memset &&
556       // Don't create memset_pattern16s with address spaces.
557       StorePtr->getType()->getPointerAddressSpace() == 0 &&
558       getMemSetPatternValue(StoredVal, DL)) {
559     // It looks like we can use PatternValue!
560     return LegalStoreKind::MemsetPattern;
561   }
562 
563   // Otherwise, see if the store can be turned into a memcpy.
564   if (HasMemcpy && !DisableLIRP::Memcpy) {
565     // Check to see if the stride matches the size of the store.  If so, then we
566     // know that every byte is touched in the loop.
567     APInt Stride = getStoreStride(StoreEv);
568     unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
569     if (StoreSize != Stride && StoreSize != -Stride)
570       return LegalStoreKind::None;
571 
572     // The store must be feeding a non-volatile load.
573     LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand());
574 
575     // Only allow non-volatile loads
576     if (!LI || LI->isVolatile())
577       return LegalStoreKind::None;
578     // Only allow simple or unordered-atomic loads
579     if (!LI->isUnordered())
580       return LegalStoreKind::None;
581 
582     // See if the pointer expression is an AddRec like {base,+,1} on the current
583     // loop, which indicates a strided load.  If we have something else, it's a
584     // random load we can't handle.
585     const SCEVAddRecExpr *LoadEv =
586         dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
587     if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
588       return LegalStoreKind::None;
589 
590     // The store and load must share the same stride.
591     if (StoreEv->getOperand(1) != LoadEv->getOperand(1))
592       return LegalStoreKind::None;
593 
594     // Success.  This store can be converted into a memcpy.
595     UnorderedAtomic = UnorderedAtomic || LI->isAtomic();
596     return UnorderedAtomic ? LegalStoreKind::UnorderedAtomicMemcpy
597                            : LegalStoreKind::Memcpy;
598   }
599   // This store can't be transformed into a memset/memcpy.
600   return LegalStoreKind::None;
601 }
602 
603 void LoopIdiomRecognize::collectStores(BasicBlock *BB) {
604   StoreRefsForMemset.clear();
605   StoreRefsForMemsetPattern.clear();
606   StoreRefsForMemcpy.clear();
607   for (Instruction &I : *BB) {
608     StoreInst *SI = dyn_cast<StoreInst>(&I);
609     if (!SI)
610       continue;
611 
612     // Make sure this is a strided store with a constant stride.
613     switch (isLegalStore(SI)) {
614     case LegalStoreKind::None:
615       // Nothing to do
616       break;
617     case LegalStoreKind::Memset: {
618       // Find the base pointer.
619       Value *Ptr = getUnderlyingObject(SI->getPointerOperand());
620       StoreRefsForMemset[Ptr].push_back(SI);
621     } break;
622     case LegalStoreKind::MemsetPattern: {
623       // Find the base pointer.
624       Value *Ptr = getUnderlyingObject(SI->getPointerOperand());
625       StoreRefsForMemsetPattern[Ptr].push_back(SI);
626     } break;
627     case LegalStoreKind::Memcpy:
628     case LegalStoreKind::UnorderedAtomicMemcpy:
629       StoreRefsForMemcpy.push_back(SI);
630       break;
631     default:
632       assert(false && "unhandled return value");
633       break;
634     }
635   }
636 }
637 
638 /// runOnLoopBlock - Process the specified block, which lives in a counted loop
639 /// with the specified backedge count.  This block is known to be in the current
640 /// loop and not in any subloops.
641 bool LoopIdiomRecognize::runOnLoopBlock(
642     BasicBlock *BB, const SCEV *BECount,
643     SmallVectorImpl<BasicBlock *> &ExitBlocks) {
644   // We can only promote stores in this block if they are unconditionally
645   // executed in the loop.  For a block to be unconditionally executed, it has
646   // to dominate all the exit blocks of the loop.  Verify this now.
647   for (BasicBlock *ExitBlock : ExitBlocks)
648     if (!DT->dominates(BB, ExitBlock))
649       return false;
650 
651   bool MadeChange = false;
652   // Look for store instructions, which may be optimized to memset/memcpy.
653   collectStores(BB);
654 
655   // Look for a single store or sets of stores with a common base, which can be
656   // optimized into a memset (memset_pattern).  The latter most commonly happens
657   // with structs and handunrolled loops.
658   for (auto &SL : StoreRefsForMemset)
659     MadeChange |= processLoopStores(SL.second, BECount, ForMemset::Yes);
660 
661   for (auto &SL : StoreRefsForMemsetPattern)
662     MadeChange |= processLoopStores(SL.second, BECount, ForMemset::No);
663 
664   // Optimize the store into a memcpy, if it feeds an similarly strided load.
665   for (auto &SI : StoreRefsForMemcpy)
666     MadeChange |= processLoopStoreOfLoopLoad(SI, BECount);
667 
668   MadeChange |= processLoopMemIntrinsic<MemCpyInst>(
669       BB, &LoopIdiomRecognize::processLoopMemCpy, BECount);
670   MadeChange |= processLoopMemIntrinsic<MemSetInst>(
671       BB, &LoopIdiomRecognize::processLoopMemSet, BECount);
672 
673   return MadeChange;
674 }
675 
676 /// See if this store(s) can be promoted to a memset.
677 bool LoopIdiomRecognize::processLoopStores(SmallVectorImpl<StoreInst *> &SL,
678                                            const SCEV *BECount, ForMemset For) {
679   // Try to find consecutive stores that can be transformed into memsets.
680   SetVector<StoreInst *> Heads, Tails;
681   SmallDenseMap<StoreInst *, StoreInst *> ConsecutiveChain;
682 
683   // Do a quadratic search on all of the given stores and find
684   // all of the pairs of stores that follow each other.
685   SmallVector<unsigned, 16> IndexQueue;
686   for (unsigned i = 0, e = SL.size(); i < e; ++i) {
687     assert(SL[i]->isSimple() && "Expected only non-volatile stores.");
688 
689     Value *FirstStoredVal = SL[i]->getValueOperand();
690     Value *FirstStorePtr = SL[i]->getPointerOperand();
691     const SCEVAddRecExpr *FirstStoreEv =
692         cast<SCEVAddRecExpr>(SE->getSCEV(FirstStorePtr));
693     APInt FirstStride = getStoreStride(FirstStoreEv);
694     unsigned FirstStoreSize = DL->getTypeStoreSize(SL[i]->getValueOperand()->getType());
695 
696     // See if we can optimize just this store in isolation.
697     if (FirstStride == FirstStoreSize || -FirstStride == FirstStoreSize) {
698       Heads.insert(SL[i]);
699       continue;
700     }
701 
702     Value *FirstSplatValue = nullptr;
703     Constant *FirstPatternValue = nullptr;
704 
705     if (For == ForMemset::Yes)
706       FirstSplatValue = isBytewiseValue(FirstStoredVal, *DL);
707     else
708       FirstPatternValue = getMemSetPatternValue(FirstStoredVal, DL);
709 
710     assert((FirstSplatValue || FirstPatternValue) &&
711            "Expected either splat value or pattern value.");
712 
713     IndexQueue.clear();
714     // If a store has multiple consecutive store candidates, search Stores
715     // array according to the sequence: from i+1 to e, then from i-1 to 0.
716     // This is because usually pairing with immediate succeeding or preceding
717     // candidate create the best chance to find memset opportunity.
718     unsigned j = 0;
719     for (j = i + 1; j < e; ++j)
720       IndexQueue.push_back(j);
721     for (j = i; j > 0; --j)
722       IndexQueue.push_back(j - 1);
723 
724     for (auto &k : IndexQueue) {
725       assert(SL[k]->isSimple() && "Expected only non-volatile stores.");
726       Value *SecondStorePtr = SL[k]->getPointerOperand();
727       const SCEVAddRecExpr *SecondStoreEv =
728           cast<SCEVAddRecExpr>(SE->getSCEV(SecondStorePtr));
729       APInt SecondStride = getStoreStride(SecondStoreEv);
730 
731       if (FirstStride != SecondStride)
732         continue;
733 
734       Value *SecondStoredVal = SL[k]->getValueOperand();
735       Value *SecondSplatValue = nullptr;
736       Constant *SecondPatternValue = nullptr;
737 
738       if (For == ForMemset::Yes)
739         SecondSplatValue = isBytewiseValue(SecondStoredVal, *DL);
740       else
741         SecondPatternValue = getMemSetPatternValue(SecondStoredVal, DL);
742 
743       assert((SecondSplatValue || SecondPatternValue) &&
744              "Expected either splat value or pattern value.");
745 
746       if (isConsecutiveAccess(SL[i], SL[k], *DL, *SE, false)) {
747         if (For == ForMemset::Yes) {
748           if (isa<UndefValue>(FirstSplatValue))
749             FirstSplatValue = SecondSplatValue;
750           if (FirstSplatValue != SecondSplatValue)
751             continue;
752         } else {
753           if (isa<UndefValue>(FirstPatternValue))
754             FirstPatternValue = SecondPatternValue;
755           if (FirstPatternValue != SecondPatternValue)
756             continue;
757         }
758         Tails.insert(SL[k]);
759         Heads.insert(SL[i]);
760         ConsecutiveChain[SL[i]] = SL[k];
761         break;
762       }
763     }
764   }
765 
766   // We may run into multiple chains that merge into a single chain. We mark the
767   // stores that we transformed so that we don't visit the same store twice.
768   SmallPtrSet<Value *, 16> TransformedStores;
769   bool Changed = false;
770 
771   // For stores that start but don't end a link in the chain:
772   for (StoreInst *I : Heads) {
773     if (Tails.count(I))
774       continue;
775 
776     // We found a store instr that starts a chain. Now follow the chain and try
777     // to transform it.
778     SmallPtrSet<Instruction *, 8> AdjacentStores;
779     StoreInst *HeadStore = I;
780     unsigned StoreSize = 0;
781 
782     // Collect the chain into a list.
783     while (Tails.count(I) || Heads.count(I)) {
784       if (TransformedStores.count(I))
785         break;
786       AdjacentStores.insert(I);
787 
788       StoreSize += DL->getTypeStoreSize(I->getValueOperand()->getType());
789       // Move to the next value in the chain.
790       I = ConsecutiveChain[I];
791     }
792 
793     Value *StoredVal = HeadStore->getValueOperand();
794     Value *StorePtr = HeadStore->getPointerOperand();
795     const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
796     APInt Stride = getStoreStride(StoreEv);
797 
798     // Check to see if the stride matches the size of the stores.  If so, then
799     // we know that every byte is touched in the loop.
800     if (StoreSize != Stride && StoreSize != -Stride)
801       continue;
802 
803     bool IsNegStride = StoreSize == -Stride;
804 
805     Type *IntIdxTy = DL->getIndexType(StorePtr->getType());
806     const SCEV *StoreSizeSCEV = SE->getConstant(IntIdxTy, StoreSize);
807     if (processLoopStridedStore(StorePtr, StoreSizeSCEV,
808                                 MaybeAlign(HeadStore->getAlignment()),
809                                 StoredVal, HeadStore, AdjacentStores, StoreEv,
810                                 BECount, IsNegStride)) {
811       TransformedStores.insert(AdjacentStores.begin(), AdjacentStores.end());
812       Changed = true;
813     }
814   }
815 
816   return Changed;
817 }
818 
819 /// processLoopMemIntrinsic - Template function for calling different processor
820 /// functions based on mem instrinsic type.
821 template <typename MemInst>
822 bool LoopIdiomRecognize::processLoopMemIntrinsic(
823     BasicBlock *BB,
824     bool (LoopIdiomRecognize::*Processor)(MemInst *, const SCEV *),
825     const SCEV *BECount) {
826   bool MadeChange = false;
827   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
828     Instruction *Inst = &*I++;
829     // Look for memory instructions, which may be optimized to a larger one.
830     if (MemInst *MI = dyn_cast<MemInst>(Inst)) {
831       WeakTrackingVH InstPtr(&*I);
832       if (!(this->*Processor)(MI, BECount))
833         continue;
834       MadeChange = true;
835 
836       // If processing the instruction invalidated our iterator, start over from
837       // the top of the block.
838       if (!InstPtr)
839         I = BB->begin();
840     }
841   }
842   return MadeChange;
843 }
844 
845 /// processLoopMemCpy - See if this memcpy can be promoted to a large memcpy
846 bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
847                                            const SCEV *BECount) {
848   // We can only handle non-volatile memcpys with a constant size.
849   if (MCI->isVolatile() || !isa<ConstantInt>(MCI->getLength()))
850     return false;
851 
852   // If we're not allowed to hack on memcpy, we fail.
853   if ((!HasMemcpy && !isa<MemCpyInlineInst>(MCI)) || DisableLIRP::Memcpy)
854     return false;
855 
856   Value *Dest = MCI->getDest();
857   Value *Source = MCI->getSource();
858   if (!Dest || !Source)
859     return false;
860 
861   // See if the load and store pointer expressions are AddRec like {base,+,1} on
862   // the current loop, which indicates a strided load and store.  If we have
863   // something else, it's a random load or store we can't handle.
864   const SCEVAddRecExpr *StoreEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Dest));
865   if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
866     return false;
867   const SCEVAddRecExpr *LoadEv = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Source));
868   if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
869     return false;
870 
871   // Reject memcpys that are so large that they overflow an unsigned.
872   uint64_t SizeInBytes = cast<ConstantInt>(MCI->getLength())->getZExtValue();
873   if ((SizeInBytes >> 32) != 0)
874     return false;
875 
876   // Check if the stride matches the size of the memcpy. If so, then we know
877   // that every byte is touched in the loop.
878   const SCEVConstant *ConstStoreStride =
879       dyn_cast<SCEVConstant>(StoreEv->getOperand(1));
880   const SCEVConstant *ConstLoadStride =
881       dyn_cast<SCEVConstant>(LoadEv->getOperand(1));
882   if (!ConstStoreStride || !ConstLoadStride)
883     return false;
884 
885   APInt StoreStrideValue = ConstStoreStride->getAPInt();
886   APInt LoadStrideValue = ConstLoadStride->getAPInt();
887   // Huge stride value - give up
888   if (StoreStrideValue.getBitWidth() > 64 || LoadStrideValue.getBitWidth() > 64)
889     return false;
890 
891   if (SizeInBytes != StoreStrideValue && SizeInBytes != -StoreStrideValue) {
892     ORE.emit([&]() {
893       return OptimizationRemarkMissed(DEBUG_TYPE, "SizeStrideUnequal", MCI)
894              << ore::NV("Inst", "memcpy") << " in "
895              << ore::NV("Function", MCI->getFunction())
896              << " function will not be hoisted: "
897              << ore::NV("Reason", "memcpy size is not equal to stride");
898     });
899     return false;
900   }
901 
902   int64_t StoreStrideInt = StoreStrideValue.getSExtValue();
903   int64_t LoadStrideInt = LoadStrideValue.getSExtValue();
904   // Check if the load stride matches the store stride.
905   if (StoreStrideInt != LoadStrideInt)
906     return false;
907 
908   return processLoopStoreOfLoopLoad(
909       Dest, Source, SE->getConstant(Dest->getType(), SizeInBytes),
910       MCI->getDestAlign(), MCI->getSourceAlign(), MCI, MCI, StoreEv, LoadEv,
911       BECount);
912 }
913 
914 /// processLoopMemSet - See if this memset can be promoted to a large memset.
915 bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
916                                            const SCEV *BECount) {
917   // We can only handle non-volatile memsets.
918   if (MSI->isVolatile())
919     return false;
920 
921   // If we're not allowed to hack on memset, we fail.
922   if (!HasMemset || DisableLIRP::Memset)
923     return false;
924 
925   Value *Pointer = MSI->getDest();
926 
927   // See if the pointer expression is an AddRec like {base,+,1} on the current
928   // loop, which indicates a strided store.  If we have something else, it's a
929   // random store we can't handle.
930   const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer));
931   if (!Ev || Ev->getLoop() != CurLoop)
932     return false;
933   if (!Ev->isAffine()) {
934     LLVM_DEBUG(dbgs() << "  Pointer is not affine, abort\n");
935     return false;
936   }
937 
938   const SCEV *PointerStrideSCEV = Ev->getOperand(1);
939   const SCEV *MemsetSizeSCEV = SE->getSCEV(MSI->getLength());
940   if (!PointerStrideSCEV || !MemsetSizeSCEV)
941     return false;
942 
943   bool IsNegStride = false;
944   const bool IsConstantSize = isa<ConstantInt>(MSI->getLength());
945 
946   if (IsConstantSize) {
947     // Memset size is constant.
948     // Check if the pointer stride matches the memset size. If so, then
949     // we know that every byte is touched in the loop.
950     LLVM_DEBUG(dbgs() << "  memset size is constant\n");
951     uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
952     const SCEVConstant *ConstStride = dyn_cast<SCEVConstant>(Ev->getOperand(1));
953     if (!ConstStride)
954       return false;
955 
956     APInt Stride = ConstStride->getAPInt();
957     if (SizeInBytes != Stride && SizeInBytes != -Stride)
958       return false;
959 
960     IsNegStride = SizeInBytes == -Stride;
961   } else {
962     // Memset size is non-constant.
963     // Check if the pointer stride matches the memset size.
964     // To be conservative, the pass would not promote pointers that aren't in
965     // address space zero. Also, the pass only handles memset length and stride
966     // that are invariant for the top level loop.
967     LLVM_DEBUG(dbgs() << "  memset size is non-constant\n");
968     if (Pointer->getType()->getPointerAddressSpace() != 0) {
969       LLVM_DEBUG(dbgs() << "  pointer is not in address space zero, "
970                         << "abort\n");
971       return false;
972     }
973     if (!SE->isLoopInvariant(MemsetSizeSCEV, CurLoop)) {
974       LLVM_DEBUG(dbgs() << "  memset size is not a loop-invariant, "
975                         << "abort\n");
976       return false;
977     }
978 
979     // Compare positive direction PointerStrideSCEV with MemsetSizeSCEV
980     IsNegStride = PointerStrideSCEV->isNonConstantNegative();
981     const SCEV *PositiveStrideSCEV =
982         IsNegStride ? SE->getNegativeSCEV(PointerStrideSCEV)
983                     : PointerStrideSCEV;
984     LLVM_DEBUG(dbgs() << "  MemsetSizeSCEV: " << *MemsetSizeSCEV << "\n"
985                       << "  PositiveStrideSCEV: " << *PositiveStrideSCEV
986                       << "\n");
987 
988     if (PositiveStrideSCEV != MemsetSizeSCEV) {
989       // The folding is to fold an expression that is covered by the loop guard
990       // at loop entry. After the folding, compare again and proceed with
991       // optimization, if equal.
992       SCEVSignToZeroExtentionRewriter Folder(*SE, CurLoop);
993       const SCEV *FoldedPositiveStride = Folder.visit(PositiveStrideSCEV);
994       const SCEV *FoldedMemsetSize = Folder.visit(MemsetSizeSCEV);
995 
996       LLVM_DEBUG(dbgs() << "  Try to fold SCEV based on loop guard\n"
997                         << "    FoldedMemsetSize: " << *FoldedMemsetSize << "\n"
998                         << "    FoldedPositiveStride: " << *FoldedPositiveStride
999                         << "\n");
1000 
1001       if (FoldedPositiveStride != FoldedMemsetSize) {
1002         LLVM_DEBUG(dbgs() << "  SCEV don't match, abort\n");
1003         return false;
1004       }
1005     }
1006   }
1007 
1008   // Verify that the memset value is loop invariant.  If not, we can't promote
1009   // the memset.
1010   Value *SplatValue = MSI->getValue();
1011   if (!SplatValue || !CurLoop->isLoopInvariant(SplatValue))
1012     return false;
1013 
1014   SmallPtrSet<Instruction *, 1> MSIs;
1015   MSIs.insert(MSI);
1016   return processLoopStridedStore(Pointer, SE->getSCEV(MSI->getLength()),
1017                                  MaybeAlign(MSI->getDestAlignment()),
1018                                  SplatValue, MSI, MSIs, Ev, BECount,
1019                                  IsNegStride, /*IsLoopMemset=*/true);
1020 }
1021 
1022 /// mayLoopAccessLocation - Return true if the specified loop might access the
1023 /// specified pointer location, which is a loop-strided access.  The 'Access'
1024 /// argument specifies what the verboten forms of access are (read or write).
1025 static bool
1026 mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L,
1027                       const SCEV *BECount, const SCEV *StoreSizeSCEV,
1028                       AliasAnalysis &AA,
1029                       SmallPtrSetImpl<Instruction *> &IgnoredInsts) {
1030   // Get the location that may be stored across the loop.  Since the access is
1031   // strided positively through memory, we say that the modified location starts
1032   // at the pointer and has infinite size.
1033   LocationSize AccessSize = LocationSize::afterPointer();
1034 
1035   // If the loop iterates a fixed number of times, we can refine the access size
1036   // to be exactly the size of the memset, which is (BECount+1)*StoreSize
1037   const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount);
1038   const SCEVConstant *ConstSize = dyn_cast<SCEVConstant>(StoreSizeSCEV);
1039   if (BECst && ConstSize)
1040     AccessSize = LocationSize::precise((BECst->getValue()->getZExtValue() + 1) *
1041                                        ConstSize->getValue()->getZExtValue());
1042 
1043   // TODO: For this to be really effective, we have to dive into the pointer
1044   // operand in the store.  Store to &A[i] of 100 will always return may alias
1045   // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
1046   // which will then no-alias a store to &A[100].
1047   MemoryLocation StoreLoc(Ptr, AccessSize);
1048 
1049   for (BasicBlock *B : L->blocks())
1050     for (Instruction &I : *B)
1051       if (!IgnoredInsts.contains(&I) &&
1052           isModOrRefSet(
1053               intersectModRef(AA.getModRefInfo(&I, StoreLoc), Access)))
1054         return true;
1055   return false;
1056 }
1057 
1058 // If we have a negative stride, Start refers to the end of the memory location
1059 // we're trying to memset.  Therefore, we need to recompute the base pointer,
1060 // which is just Start - BECount*Size.
1061 static const SCEV *getStartForNegStride(const SCEV *Start, const SCEV *BECount,
1062                                         Type *IntPtr, const SCEV *StoreSizeSCEV,
1063                                         ScalarEvolution *SE) {
1064   const SCEV *Index = SE->getTruncateOrZeroExtend(BECount, IntPtr);
1065   if (!StoreSizeSCEV->isOne()) {
1066     // index = back edge count * store size
1067     Index = SE->getMulExpr(Index,
1068                            SE->getTruncateOrZeroExtend(StoreSizeSCEV, IntPtr),
1069                            SCEV::FlagNUW);
1070   }
1071   // base pointer = start - index * store size
1072   return SE->getMinusSCEV(Start, Index);
1073 }
1074 
1075 /// Compute trip count from the backedge taken count.
1076 static const SCEV *getTripCount(const SCEV *BECount, Type *IntPtr,
1077                                 Loop *CurLoop, const DataLayout *DL,
1078                                 ScalarEvolution *SE) {
1079   const SCEV *TripCountS = nullptr;
1080   // The # stored bytes is (BECount+1).  Expand the trip count out to
1081   // pointer size if it isn't already.
1082   //
1083   // If we're going to need to zero extend the BE count, check if we can add
1084   // one to it prior to zero extending without overflow. Provided this is safe,
1085   // it allows better simplification of the +1.
1086   if (DL->getTypeSizeInBits(BECount->getType()) <
1087           DL->getTypeSizeInBits(IntPtr) &&
1088       SE->isLoopEntryGuardedByCond(
1089           CurLoop, ICmpInst::ICMP_NE, BECount,
1090           SE->getNegativeSCEV(SE->getOne(BECount->getType())))) {
1091     TripCountS = SE->getZeroExtendExpr(
1092         SE->getAddExpr(BECount, SE->getOne(BECount->getType()), SCEV::FlagNUW),
1093         IntPtr);
1094   } else {
1095     TripCountS = SE->getAddExpr(SE->getTruncateOrZeroExtend(BECount, IntPtr),
1096                                 SE->getOne(IntPtr), SCEV::FlagNUW);
1097   }
1098 
1099   return TripCountS;
1100 }
1101 
1102 /// Compute the number of bytes as a SCEV from the backedge taken count.
1103 ///
1104 /// This also maps the SCEV into the provided type and tries to handle the
1105 /// computation in a way that will fold cleanly.
1106 static const SCEV *getNumBytes(const SCEV *BECount, Type *IntPtr,
1107                                const SCEV *StoreSizeSCEV, Loop *CurLoop,
1108                                const DataLayout *DL, ScalarEvolution *SE) {
1109   const SCEV *TripCountSCEV = getTripCount(BECount, IntPtr, CurLoop, DL, SE);
1110 
1111   return SE->getMulExpr(TripCountSCEV,
1112                         SE->getTruncateOrZeroExtend(StoreSizeSCEV, IntPtr),
1113                         SCEV::FlagNUW);
1114 }
1115 
1116 /// processLoopStridedStore - We see a strided store of some value.  If we can
1117 /// transform this into a memset or memset_pattern in the loop preheader, do so.
1118 bool LoopIdiomRecognize::processLoopStridedStore(
1119     Value *DestPtr, const SCEV *StoreSizeSCEV, MaybeAlign StoreAlignment,
1120     Value *StoredVal, Instruction *TheStore,
1121     SmallPtrSetImpl<Instruction *> &Stores, const SCEVAddRecExpr *Ev,
1122     const SCEV *BECount, bool IsNegStride, bool IsLoopMemset) {
1123   Value *SplatValue = isBytewiseValue(StoredVal, *DL);
1124   Constant *PatternValue = nullptr;
1125 
1126   if (!SplatValue)
1127     PatternValue = getMemSetPatternValue(StoredVal, DL);
1128 
1129   assert((SplatValue || PatternValue) &&
1130          "Expected either splat value or pattern value.");
1131 
1132   // The trip count of the loop and the base pointer of the addrec SCEV is
1133   // guaranteed to be loop invariant, which means that it should dominate the
1134   // header.  This allows us to insert code for it in the preheader.
1135   unsigned DestAS = DestPtr->getType()->getPointerAddressSpace();
1136   BasicBlock *Preheader = CurLoop->getLoopPreheader();
1137   IRBuilder<> Builder(Preheader->getTerminator());
1138   SCEVExpander Expander(*SE, *DL, "loop-idiom");
1139   SCEVExpanderCleaner ExpCleaner(Expander, *DT);
1140 
1141   Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS);
1142   Type *IntIdxTy = DL->getIndexType(DestPtr->getType());
1143 
1144   bool Changed = false;
1145   const SCEV *Start = Ev->getStart();
1146   // Handle negative strided loops.
1147   if (IsNegStride)
1148     Start = getStartForNegStride(Start, BECount, IntIdxTy, StoreSizeSCEV, SE);
1149 
1150   // TODO: ideally we should still be able to generate memset if SCEV expander
1151   // is taught to generate the dependencies at the latest point.
1152   if (!isSafeToExpand(Start, *SE))
1153     return Changed;
1154 
1155   // Okay, we have a strided store "p[i]" of a splattable value.  We can turn
1156   // this into a memset in the loop preheader now if we want.  However, this
1157   // would be unsafe to do if there is anything else in the loop that may read
1158   // or write to the aliased location.  Check for any overlap by generating the
1159   // base pointer and checking the region.
1160   Value *BasePtr =
1161       Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->getTerminator());
1162 
1163   // From here on out, conservatively report to the pass manager that we've
1164   // changed the IR, even if we later clean up these added instructions. There
1165   // may be structural differences e.g. in the order of use lists not accounted
1166   // for in just a textual dump of the IR. This is written as a variable, even
1167   // though statically all the places this dominates could be replaced with
1168   // 'true', with the hope that anyone trying to be clever / "more precise" with
1169   // the return value will read this comment, and leave them alone.
1170   Changed = true;
1171 
1172   if (mayLoopAccessLocation(BasePtr, ModRefInfo::ModRef, CurLoop, BECount,
1173                             StoreSizeSCEV, *AA, Stores))
1174     return Changed;
1175 
1176   if (avoidLIRForMultiBlockLoop(/*IsMemset=*/true, IsLoopMemset))
1177     return Changed;
1178 
1179   // Okay, everything looks good, insert the memset.
1180 
1181   const SCEV *NumBytesS =
1182       getNumBytes(BECount, IntIdxTy, StoreSizeSCEV, CurLoop, DL, SE);
1183 
1184   // TODO: ideally we should still be able to generate memset if SCEV expander
1185   // is taught to generate the dependencies at the latest point.
1186   if (!isSafeToExpand(NumBytesS, *SE))
1187     return Changed;
1188 
1189   Value *NumBytes =
1190       Expander.expandCodeFor(NumBytesS, IntIdxTy, Preheader->getTerminator());
1191 
1192   CallInst *NewCall;
1193   if (SplatValue) {
1194     NewCall = Builder.CreateMemSet(BasePtr, SplatValue, NumBytes,
1195                                    MaybeAlign(StoreAlignment));
1196   } else {
1197     // Everything is emitted in default address space
1198     Type *Int8PtrTy = DestInt8PtrTy;
1199 
1200     Module *M = TheStore->getModule();
1201     StringRef FuncName = "memset_pattern16";
1202     FunctionCallee MSP = M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
1203                                                 Int8PtrTy, Int8PtrTy, IntIdxTy);
1204     inferLibFuncAttributes(M, FuncName, *TLI);
1205 
1206     // Otherwise we should form a memset_pattern16.  PatternValue is known to be
1207     // an constant array of 16-bytes.  Plop the value into a mergable global.
1208     GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true,
1209                                             GlobalValue::PrivateLinkage,
1210                                             PatternValue, ".memset_pattern");
1211     GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); // Ok to merge these.
1212     GV->setAlignment(Align(16));
1213     Value *PatternPtr = ConstantExpr::getBitCast(GV, Int8PtrTy);
1214     NewCall = Builder.CreateCall(MSP, {BasePtr, PatternPtr, NumBytes});
1215   }
1216   NewCall->setDebugLoc(TheStore->getDebugLoc());
1217 
1218   if (MSSAU) {
1219     MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB(
1220         NewCall, nullptr, NewCall->getParent(), MemorySSA::BeforeTerminator);
1221     MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true);
1222   }
1223 
1224   LLVM_DEBUG(dbgs() << "  Formed memset: " << *NewCall << "\n"
1225                     << "    from store to: " << *Ev << " at: " << *TheStore
1226                     << "\n");
1227 
1228   ORE.emit([&]() {
1229     OptimizationRemark R(DEBUG_TYPE, "ProcessLoopStridedStore",
1230                          NewCall->getDebugLoc(), Preheader);
1231     R << "Transformed loop-strided store in "
1232       << ore::NV("Function", TheStore->getFunction())
1233       << " function into a call to "
1234       << ore::NV("NewFunction", NewCall->getCalledFunction())
1235       << "() intrinsic";
1236     if (!Stores.empty())
1237       R << ore::setExtraArgs();
1238     for (auto *I : Stores) {
1239       R << ore::NV("FromBlock", I->getParent()->getName())
1240         << ore::NV("ToBlock", Preheader->getName());
1241     }
1242     return R;
1243   });
1244 
1245   // Okay, the memset has been formed.  Zap the original store and anything that
1246   // feeds into it.
1247   for (auto *I : Stores) {
1248     if (MSSAU)
1249       MSSAU->removeMemoryAccess(I, true);
1250     deleteDeadInstruction(I);
1251   }
1252   if (MSSAU && VerifyMemorySSA)
1253     MSSAU->getMemorySSA()->verifyMemorySSA();
1254   ++NumMemSet;
1255   ExpCleaner.markResultUsed();
1256   return true;
1257 }
1258 
1259 /// If the stored value is a strided load in the same loop with the same stride
1260 /// this may be transformable into a memcpy.  This kicks in for stuff like
1261 /// for (i) A[i] = B[i];
1262 bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
1263                                                     const SCEV *BECount) {
1264   assert(SI->isUnordered() && "Expected only non-volatile non-ordered stores.");
1265 
1266   Value *StorePtr = SI->getPointerOperand();
1267   const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
1268   unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
1269 
1270   // The store must be feeding a non-volatile load.
1271   LoadInst *LI = cast<LoadInst>(SI->getValueOperand());
1272   assert(LI->isUnordered() && "Expected only non-volatile non-ordered loads.");
1273 
1274   // See if the pointer expression is an AddRec like {base,+,1} on the current
1275   // loop, which indicates a strided load.  If we have something else, it's a
1276   // random load we can't handle.
1277   Value *LoadPtr = LI->getPointerOperand();
1278   const SCEVAddRecExpr *LoadEv = cast<SCEVAddRecExpr>(SE->getSCEV(LoadPtr));
1279 
1280   const SCEV *StoreSizeSCEV = SE->getConstant(StorePtr->getType(), StoreSize);
1281   return processLoopStoreOfLoopLoad(StorePtr, LoadPtr, StoreSizeSCEV,
1282                                     SI->getAlign(), LI->getAlign(), SI, LI,
1283                                     StoreEv, LoadEv, BECount);
1284 }
1285 
1286 class MemmoveVerifier {
1287 public:
1288   explicit MemmoveVerifier(const Value &LoadBasePtr, const Value &StoreBasePtr,
1289                            const DataLayout &DL)
1290       : DL(DL), LoadOff(0), StoreOff(0),
1291         BP1(llvm::GetPointerBaseWithConstantOffset(
1292             LoadBasePtr.stripPointerCasts(), LoadOff, DL)),
1293         BP2(llvm::GetPointerBaseWithConstantOffset(
1294             StoreBasePtr.stripPointerCasts(), StoreOff, DL)),
1295         IsSameObject(BP1 == BP2) {}
1296 
1297   bool loadAndStoreMayFormMemmove(unsigned StoreSize, bool IsNegStride,
1298                                   const Instruction &TheLoad,
1299                                   bool IsMemCpy) const {
1300     if (IsMemCpy) {
1301       // Ensure that LoadBasePtr is after StoreBasePtr or before StoreBasePtr
1302       // for negative stride.
1303       if ((!IsNegStride && LoadOff <= StoreOff) ||
1304           (IsNegStride && LoadOff >= StoreOff))
1305         return false;
1306     } else {
1307       // Ensure that LoadBasePtr is after StoreBasePtr or before StoreBasePtr
1308       // for negative stride. LoadBasePtr shouldn't overlap with StoreBasePtr.
1309       int64_t LoadSize =
1310           DL.getTypeSizeInBits(TheLoad.getType()).getFixedSize() / 8;
1311       if (BP1 != BP2 || LoadSize != int64_t(StoreSize))
1312         return false;
1313       if ((!IsNegStride && LoadOff < StoreOff + int64_t(StoreSize)) ||
1314           (IsNegStride && LoadOff + LoadSize > StoreOff))
1315         return false;
1316     }
1317     return true;
1318   }
1319 
1320 private:
1321   const DataLayout &DL;
1322   int64_t LoadOff;
1323   int64_t StoreOff;
1324   const Value *BP1;
1325   const Value *BP2;
1326 
1327 public:
1328   const bool IsSameObject;
1329 };
1330 
1331 bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(
1332     Value *DestPtr, Value *SourcePtr, const SCEV *StoreSizeSCEV,
1333     MaybeAlign StoreAlign, MaybeAlign LoadAlign, Instruction *TheStore,
1334     Instruction *TheLoad, const SCEVAddRecExpr *StoreEv,
1335     const SCEVAddRecExpr *LoadEv, const SCEV *BECount) {
1336 
1337   // FIXME: until llvm.memcpy.inline supports dynamic sizes, we need to
1338   // conservatively bail here, since otherwise we may have to transform
1339   // llvm.memcpy.inline into llvm.memcpy which is illegal.
1340   if (isa<MemCpyInlineInst>(TheStore))
1341     return false;
1342 
1343   // The trip count of the loop and the base pointer of the addrec SCEV is
1344   // guaranteed to be loop invariant, which means that it should dominate the
1345   // header.  This allows us to insert code for it in the preheader.
1346   BasicBlock *Preheader = CurLoop->getLoopPreheader();
1347   IRBuilder<> Builder(Preheader->getTerminator());
1348   SCEVExpander Expander(*SE, *DL, "loop-idiom");
1349 
1350   SCEVExpanderCleaner ExpCleaner(Expander, *DT);
1351 
1352   bool Changed = false;
1353   const SCEV *StrStart = StoreEv->getStart();
1354   unsigned StrAS = DestPtr->getType()->getPointerAddressSpace();
1355   Type *IntIdxTy = Builder.getIntNTy(DL->getIndexSizeInBits(StrAS));
1356 
1357   APInt Stride = getStoreStride(StoreEv);
1358   const SCEVConstant *ConstStoreSize = dyn_cast<SCEVConstant>(StoreSizeSCEV);
1359 
1360   // TODO: Deal with non-constant size; Currently expect constant store size
1361   assert(ConstStoreSize && "store size is expected to be a constant");
1362 
1363   int64_t StoreSize = ConstStoreSize->getValue()->getZExtValue();
1364   bool IsNegStride = StoreSize == -Stride;
1365 
1366   // Handle negative strided loops.
1367   if (IsNegStride)
1368     StrStart =
1369         getStartForNegStride(StrStart, BECount, IntIdxTy, StoreSizeSCEV, SE);
1370 
1371   // Okay, we have a strided store "p[i]" of a loaded value.  We can turn
1372   // this into a memcpy in the loop preheader now if we want.  However, this
1373   // would be unsafe to do if there is anything else in the loop that may read
1374   // or write the memory region we're storing to.  This includes the load that
1375   // feeds the stores.  Check for an alias by generating the base address and
1376   // checking everything.
1377   Value *StoreBasePtr = Expander.expandCodeFor(
1378       StrStart, Builder.getInt8PtrTy(StrAS), Preheader->getTerminator());
1379 
1380   // From here on out, conservatively report to the pass manager that we've
1381   // changed the IR, even if we later clean up these added instructions. There
1382   // may be structural differences e.g. in the order of use lists not accounted
1383   // for in just a textual dump of the IR. This is written as a variable, even
1384   // though statically all the places this dominates could be replaced with
1385   // 'true', with the hope that anyone trying to be clever / "more precise" with
1386   // the return value will read this comment, and leave them alone.
1387   Changed = true;
1388 
1389   SmallPtrSet<Instruction *, 2> IgnoredInsts;
1390   IgnoredInsts.insert(TheStore);
1391 
1392   bool IsMemCpy = isa<MemCpyInst>(TheStore);
1393   const StringRef InstRemark = IsMemCpy ? "memcpy" : "load and store";
1394 
1395   bool LoopAccessStore =
1396       mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount,
1397                             StoreSizeSCEV, *AA, IgnoredInsts);
1398   if (LoopAccessStore) {
1399     // For memmove case it's not enough to guarantee that loop doesn't access
1400     // TheStore and TheLoad. Additionally we need to make sure that TheStore is
1401     // the only user of TheLoad.
1402     if (!TheLoad->hasOneUse())
1403       return Changed;
1404     IgnoredInsts.insert(TheLoad);
1405     if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop,
1406                               BECount, StoreSizeSCEV, *AA, IgnoredInsts)) {
1407       ORE.emit([&]() {
1408         return OptimizationRemarkMissed(DEBUG_TYPE, "LoopMayAccessStore",
1409                                         TheStore)
1410                << ore::NV("Inst", InstRemark) << " in "
1411                << ore::NV("Function", TheStore->getFunction())
1412                << " function will not be hoisted: "
1413                << ore::NV("Reason", "The loop may access store location");
1414       });
1415       return Changed;
1416     }
1417     IgnoredInsts.erase(TheLoad);
1418   }
1419 
1420   const SCEV *LdStart = LoadEv->getStart();
1421   unsigned LdAS = SourcePtr->getType()->getPointerAddressSpace();
1422 
1423   // Handle negative strided loops.
1424   if (IsNegStride)
1425     LdStart =
1426         getStartForNegStride(LdStart, BECount, IntIdxTy, StoreSizeSCEV, SE);
1427 
1428   // For a memcpy, we have to make sure that the input array is not being
1429   // mutated by the loop.
1430   Value *LoadBasePtr = Expander.expandCodeFor(
1431       LdStart, Builder.getInt8PtrTy(LdAS), Preheader->getTerminator());
1432 
1433   // If the store is a memcpy instruction, we must check if it will write to
1434   // the load memory locations. So remove it from the ignored stores.
1435   if (IsMemCpy)
1436     IgnoredInsts.erase(TheStore);
1437   MemmoveVerifier Verifier(*LoadBasePtr, *StoreBasePtr, *DL);
1438   if (mayLoopAccessLocation(LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount,
1439                             StoreSizeSCEV, *AA, IgnoredInsts)) {
1440     if (!IsMemCpy) {
1441       ORE.emit([&]() {
1442         return OptimizationRemarkMissed(DEBUG_TYPE, "LoopMayAccessLoad",
1443                                         TheLoad)
1444                << ore::NV("Inst", InstRemark) << " in "
1445                << ore::NV("Function", TheStore->getFunction())
1446                << " function will not be hoisted: "
1447                << ore::NV("Reason", "The loop may access load location");
1448       });
1449       return Changed;
1450     }
1451     // At this point loop may access load only for memcpy in same underlying
1452     // object. If that's not the case bail out.
1453     if (!Verifier.IsSameObject)
1454       return Changed;
1455   }
1456 
1457   bool UseMemMove = IsMemCpy ? Verifier.IsSameObject : LoopAccessStore;
1458   if (UseMemMove)
1459     if (!Verifier.loadAndStoreMayFormMemmove(StoreSize, IsNegStride, *TheLoad,
1460                                              IsMemCpy))
1461       return Changed;
1462 
1463   if (avoidLIRForMultiBlockLoop())
1464     return Changed;
1465 
1466   // Okay, everything is safe, we can transform this!
1467 
1468   const SCEV *NumBytesS =
1469       getNumBytes(BECount, IntIdxTy, StoreSizeSCEV, CurLoop, DL, SE);
1470 
1471   Value *NumBytes =
1472       Expander.expandCodeFor(NumBytesS, IntIdxTy, Preheader->getTerminator());
1473 
1474   CallInst *NewCall = nullptr;
1475   // Check whether to generate an unordered atomic memcpy:
1476   //  If the load or store are atomic, then they must necessarily be unordered
1477   //  by previous checks.
1478   if (!TheStore->isAtomic() && !TheLoad->isAtomic()) {
1479     if (UseMemMove)
1480       NewCall = Builder.CreateMemMove(StoreBasePtr, StoreAlign, LoadBasePtr,
1481                                       LoadAlign, NumBytes);
1482     else
1483       NewCall = Builder.CreateMemCpy(StoreBasePtr, StoreAlign, LoadBasePtr,
1484                                      LoadAlign, NumBytes);
1485   } else {
1486     // For now don't support unordered atomic memmove.
1487     if (UseMemMove)
1488       return Changed;
1489     // We cannot allow unaligned ops for unordered load/store, so reject
1490     // anything where the alignment isn't at least the element size.
1491     assert((StoreAlign.hasValue() && LoadAlign.hasValue()) &&
1492            "Expect unordered load/store to have align.");
1493     if (StoreAlign.getValue() < StoreSize || LoadAlign.getValue() < StoreSize)
1494       return Changed;
1495 
1496     // If the element.atomic memcpy is not lowered into explicit
1497     // loads/stores later, then it will be lowered into an element-size
1498     // specific lib call. If the lib call doesn't exist for our store size, then
1499     // we shouldn't generate the memcpy.
1500     if (StoreSize > TTI->getAtomicMemIntrinsicMaxElementSize())
1501       return Changed;
1502 
1503     // Create the call.
1504     // Note that unordered atomic loads/stores are *required* by the spec to
1505     // have an alignment but non-atomic loads/stores may not.
1506     NewCall = Builder.CreateElementUnorderedAtomicMemCpy(
1507         StoreBasePtr, StoreAlign.getValue(), LoadBasePtr, LoadAlign.getValue(),
1508         NumBytes, StoreSize);
1509   }
1510   NewCall->setDebugLoc(TheStore->getDebugLoc());
1511 
1512   if (MSSAU) {
1513     MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB(
1514         NewCall, nullptr, NewCall->getParent(), MemorySSA::BeforeTerminator);
1515     MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true);
1516   }
1517 
1518   LLVM_DEBUG(dbgs() << "  Formed new call: " << *NewCall << "\n"
1519                     << "    from load ptr=" << *LoadEv << " at: " << *TheLoad
1520                     << "\n"
1521                     << "    from store ptr=" << *StoreEv << " at: " << *TheStore
1522                     << "\n");
1523 
1524   ORE.emit([&]() {
1525     return OptimizationRemark(DEBUG_TYPE, "ProcessLoopStoreOfLoopLoad",
1526                               NewCall->getDebugLoc(), Preheader)
1527            << "Formed a call to "
1528            << ore::NV("NewFunction", NewCall->getCalledFunction())
1529            << "() intrinsic from " << ore::NV("Inst", InstRemark)
1530            << " instruction in " << ore::NV("Function", TheStore->getFunction())
1531            << " function"
1532            << ore::setExtraArgs()
1533            << ore::NV("FromBlock", TheStore->getParent()->getName())
1534            << ore::NV("ToBlock", Preheader->getName());
1535   });
1536 
1537   // Okay, a new call to memcpy/memmove has been formed.  Zap the original store
1538   // and anything that feeds into it.
1539   if (MSSAU)
1540     MSSAU->removeMemoryAccess(TheStore, true);
1541   deleteDeadInstruction(TheStore);
1542   if (MSSAU && VerifyMemorySSA)
1543     MSSAU->getMemorySSA()->verifyMemorySSA();
1544   if (UseMemMove)
1545     ++NumMemMove;
1546   else
1547     ++NumMemCpy;
1548   ExpCleaner.markResultUsed();
1549   return true;
1550 }
1551 
1552 // When compiling for codesize we avoid idiom recognition for a multi-block loop
1553 // unless it is a loop_memset idiom or a memset/memcpy idiom in a nested loop.
1554 //
1555 bool LoopIdiomRecognize::avoidLIRForMultiBlockLoop(bool IsMemset,
1556                                                    bool IsLoopMemset) {
1557   if (ApplyCodeSizeHeuristics && CurLoop->getNumBlocks() > 1) {
1558     if (CurLoop->isOutermost() && (!IsMemset || !IsLoopMemset)) {
1559       LLVM_DEBUG(dbgs() << "  " << CurLoop->getHeader()->getParent()->getName()
1560                         << " : LIR " << (IsMemset ? "Memset" : "Memcpy")
1561                         << " avoided: multi-block top-level loop\n");
1562       return true;
1563     }
1564   }
1565 
1566   return false;
1567 }
1568 
1569 bool LoopIdiomRecognize::runOnNoncountableLoop() {
1570   LLVM_DEBUG(dbgs() << DEBUG_TYPE " Scanning: F["
1571                     << CurLoop->getHeader()->getParent()->getName()
1572                     << "] Noncountable Loop %"
1573                     << CurLoop->getHeader()->getName() << "\n");
1574 
1575   return recognizePopcount() || recognizeAndInsertFFS() ||
1576          recognizeShiftUntilBitTest() || recognizeShiftUntilZero();
1577 }
1578 
1579 /// Check if the given conditional branch is based on the comparison between
1580 /// a variable and zero, and if the variable is non-zero or zero (JmpOnZero is
1581 /// true), the control yields to the loop entry. If the branch matches the
1582 /// behavior, the variable involved in the comparison is returned. This function
1583 /// will be called to see if the precondition and postcondition of the loop are
1584 /// in desirable form.
1585 static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
1586                              bool JmpOnZero = false) {
1587   if (!BI || !BI->isConditional())
1588     return nullptr;
1589 
1590   ICmpInst *Cond = dyn_cast<ICmpInst>(BI->getCondition());
1591   if (!Cond)
1592     return nullptr;
1593 
1594   ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
1595   if (!CmpZero || !CmpZero->isZero())
1596     return nullptr;
1597 
1598   BasicBlock *TrueSucc = BI->getSuccessor(0);
1599   BasicBlock *FalseSucc = BI->getSuccessor(1);
1600   if (JmpOnZero)
1601     std::swap(TrueSucc, FalseSucc);
1602 
1603   ICmpInst::Predicate Pred = Cond->getPredicate();
1604   if ((Pred == ICmpInst::ICMP_NE && TrueSucc == LoopEntry) ||
1605       (Pred == ICmpInst::ICMP_EQ && FalseSucc == LoopEntry))
1606     return Cond->getOperand(0);
1607 
1608   return nullptr;
1609 }
1610 
1611 // Check if the recurrence variable `VarX` is in the right form to create
1612 // the idiom. Returns the value coerced to a PHINode if so.
1613 static PHINode *getRecurrenceVar(Value *VarX, Instruction *DefX,
1614                                  BasicBlock *LoopEntry) {
1615   auto *PhiX = dyn_cast<PHINode>(VarX);
1616   if (PhiX && PhiX->getParent() == LoopEntry &&
1617       (PhiX->getOperand(0) == DefX || PhiX->getOperand(1) == DefX))
1618     return PhiX;
1619   return nullptr;
1620 }
1621 
1622 /// Return true iff the idiom is detected in the loop.
1623 ///
1624 /// Additionally:
1625 /// 1) \p CntInst is set to the instruction counting the population bit.
1626 /// 2) \p CntPhi is set to the corresponding phi node.
1627 /// 3) \p Var is set to the value whose population bits are being counted.
1628 ///
1629 /// The core idiom we are trying to detect is:
1630 /// \code
1631 ///    if (x0 != 0)
1632 ///      goto loop-exit // the precondition of the loop
1633 ///    cnt0 = init-val;
1634 ///    do {
1635 ///       x1 = phi (x0, x2);
1636 ///       cnt1 = phi(cnt0, cnt2);
1637 ///
1638 ///       cnt2 = cnt1 + 1;
1639 ///        ...
1640 ///       x2 = x1 & (x1 - 1);
1641 ///        ...
1642 ///    } while(x != 0);
1643 ///
1644 /// loop-exit:
1645 /// \endcode
1646 static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB,
1647                                 Instruction *&CntInst, PHINode *&CntPhi,
1648                                 Value *&Var) {
1649   // step 1: Check to see if the look-back branch match this pattern:
1650   //    "if (a!=0) goto loop-entry".
1651   BasicBlock *LoopEntry;
1652   Instruction *DefX2, *CountInst;
1653   Value *VarX1, *VarX0;
1654   PHINode *PhiX, *CountPhi;
1655 
1656   DefX2 = CountInst = nullptr;
1657   VarX1 = VarX0 = nullptr;
1658   PhiX = CountPhi = nullptr;
1659   LoopEntry = *(CurLoop->block_begin());
1660 
1661   // step 1: Check if the loop-back branch is in desirable form.
1662   {
1663     if (Value *T = matchCondition(
1664             dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry))
1665       DefX2 = dyn_cast<Instruction>(T);
1666     else
1667       return false;
1668   }
1669 
1670   // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)"
1671   {
1672     if (!DefX2 || DefX2->getOpcode() != Instruction::And)
1673       return false;
1674 
1675     BinaryOperator *SubOneOp;
1676 
1677     if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0))))
1678       VarX1 = DefX2->getOperand(1);
1679     else {
1680       VarX1 = DefX2->getOperand(0);
1681       SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1));
1682     }
1683     if (!SubOneOp || SubOneOp->getOperand(0) != VarX1)
1684       return false;
1685 
1686     ConstantInt *Dec = dyn_cast<ConstantInt>(SubOneOp->getOperand(1));
1687     if (!Dec ||
1688         !((SubOneOp->getOpcode() == Instruction::Sub && Dec->isOne()) ||
1689           (SubOneOp->getOpcode() == Instruction::Add &&
1690            Dec->isMinusOne()))) {
1691       return false;
1692     }
1693   }
1694 
1695   // step 3: Check the recurrence of variable X
1696   PhiX = getRecurrenceVar(VarX1, DefX2, LoopEntry);
1697   if (!PhiX)
1698     return false;
1699 
1700   // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1
1701   {
1702     CountInst = nullptr;
1703     for (Instruction &Inst : llvm::make_range(
1704              LoopEntry->getFirstNonPHI()->getIterator(), LoopEntry->end())) {
1705       if (Inst.getOpcode() != Instruction::Add)
1706         continue;
1707 
1708       ConstantInt *Inc = dyn_cast<ConstantInt>(Inst.getOperand(1));
1709       if (!Inc || !Inc->isOne())
1710         continue;
1711 
1712       PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry);
1713       if (!Phi)
1714         continue;
1715 
1716       // Check if the result of the instruction is live of the loop.
1717       bool LiveOutLoop = false;
1718       for (User *U : Inst.users()) {
1719         if ((cast<Instruction>(U))->getParent() != LoopEntry) {
1720           LiveOutLoop = true;
1721           break;
1722         }
1723       }
1724 
1725       if (LiveOutLoop) {
1726         CountInst = &Inst;
1727         CountPhi = Phi;
1728         break;
1729       }
1730     }
1731 
1732     if (!CountInst)
1733       return false;
1734   }
1735 
1736   // step 5: check if the precondition is in this form:
1737   //   "if (x != 0) goto loop-head ; else goto somewhere-we-don't-care;"
1738   {
1739     auto *PreCondBr = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1740     Value *T = matchCondition(PreCondBr, CurLoop->getLoopPreheader());
1741     if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1))
1742       return false;
1743 
1744     CntInst = CountInst;
1745     CntPhi = CountPhi;
1746     Var = T;
1747   }
1748 
1749   return true;
1750 }
1751 
1752 /// Return true if the idiom is detected in the loop.
1753 ///
1754 /// Additionally:
1755 /// 1) \p CntInst is set to the instruction Counting Leading Zeros (CTLZ)
1756 ///       or nullptr if there is no such.
1757 /// 2) \p CntPhi is set to the corresponding phi node
1758 ///       or nullptr if there is no such.
1759 /// 3) \p Var is set to the value whose CTLZ could be used.
1760 /// 4) \p DefX is set to the instruction calculating Loop exit condition.
1761 ///
1762 /// The core idiom we are trying to detect is:
1763 /// \code
1764 ///    if (x0 == 0)
1765 ///      goto loop-exit // the precondition of the loop
1766 ///    cnt0 = init-val;
1767 ///    do {
1768 ///       x = phi (x0, x.next);   //PhiX
1769 ///       cnt = phi(cnt0, cnt.next);
1770 ///
1771 ///       cnt.next = cnt + 1;
1772 ///        ...
1773 ///       x.next = x >> 1;   // DefX
1774 ///        ...
1775 ///    } while(x.next != 0);
1776 ///
1777 /// loop-exit:
1778 /// \endcode
1779 static bool detectShiftUntilZeroIdiom(Loop *CurLoop, const DataLayout &DL,
1780                                       Intrinsic::ID &IntrinID, Value *&InitX,
1781                                       Instruction *&CntInst, PHINode *&CntPhi,
1782                                       Instruction *&DefX) {
1783   BasicBlock *LoopEntry;
1784   Value *VarX = nullptr;
1785 
1786   DefX = nullptr;
1787   CntInst = nullptr;
1788   CntPhi = nullptr;
1789   LoopEntry = *(CurLoop->block_begin());
1790 
1791   // step 1: Check if the loop-back branch is in desirable form.
1792   if (Value *T = matchCondition(
1793           dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry))
1794     DefX = dyn_cast<Instruction>(T);
1795   else
1796     return false;
1797 
1798   // step 2: detect instructions corresponding to "x.next = x >> 1 or x << 1"
1799   if (!DefX || !DefX->isShift())
1800     return false;
1801   IntrinID = DefX->getOpcode() == Instruction::Shl ? Intrinsic::cttz :
1802                                                      Intrinsic::ctlz;
1803   ConstantInt *Shft = dyn_cast<ConstantInt>(DefX->getOperand(1));
1804   if (!Shft || !Shft->isOne())
1805     return false;
1806   VarX = DefX->getOperand(0);
1807 
1808   // step 3: Check the recurrence of variable X
1809   PHINode *PhiX = getRecurrenceVar(VarX, DefX, LoopEntry);
1810   if (!PhiX)
1811     return false;
1812 
1813   InitX = PhiX->getIncomingValueForBlock(CurLoop->getLoopPreheader());
1814 
1815   // Make sure the initial value can't be negative otherwise the ashr in the
1816   // loop might never reach zero which would make the loop infinite.
1817   if (DefX->getOpcode() == Instruction::AShr && !isKnownNonNegative(InitX, DL))
1818     return false;
1819 
1820   // step 4: Find the instruction which count the CTLZ: cnt.next = cnt + 1
1821   //         or cnt.next = cnt + -1.
1822   // TODO: We can skip the step. If loop trip count is known (CTLZ),
1823   //       then all uses of "cnt.next" could be optimized to the trip count
1824   //       plus "cnt0". Currently it is not optimized.
1825   //       This step could be used to detect POPCNT instruction:
1826   //       cnt.next = cnt + (x.next & 1)
1827   for (Instruction &Inst : llvm::make_range(
1828            LoopEntry->getFirstNonPHI()->getIterator(), LoopEntry->end())) {
1829     if (Inst.getOpcode() != Instruction::Add)
1830       continue;
1831 
1832     ConstantInt *Inc = dyn_cast<ConstantInt>(Inst.getOperand(1));
1833     if (!Inc || (!Inc->isOne() && !Inc->isMinusOne()))
1834       continue;
1835 
1836     PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry);
1837     if (!Phi)
1838       continue;
1839 
1840     CntInst = &Inst;
1841     CntPhi = Phi;
1842     break;
1843   }
1844   if (!CntInst)
1845     return false;
1846 
1847   return true;
1848 }
1849 
1850 /// Recognize CTLZ or CTTZ idiom in a non-countable loop and convert the loop
1851 /// to countable (with CTLZ / CTTZ trip count). If CTLZ / CTTZ inserted as a new
1852 /// trip count returns true; otherwise, returns false.
1853 bool LoopIdiomRecognize::recognizeAndInsertFFS() {
1854   // Give up if the loop has multiple blocks or multiple backedges.
1855   if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
1856     return false;
1857 
1858   Intrinsic::ID IntrinID;
1859   Value *InitX;
1860   Instruction *DefX = nullptr;
1861   PHINode *CntPhi = nullptr;
1862   Instruction *CntInst = nullptr;
1863   // Help decide if transformation is profitable. For ShiftUntilZero idiom,
1864   // this is always 6.
1865   size_t IdiomCanonicalSize = 6;
1866 
1867   if (!detectShiftUntilZeroIdiom(CurLoop, *DL, IntrinID, InitX,
1868                                  CntInst, CntPhi, DefX))
1869     return false;
1870 
1871   bool IsCntPhiUsedOutsideLoop = false;
1872   for (User *U : CntPhi->users())
1873     if (!CurLoop->contains(cast<Instruction>(U))) {
1874       IsCntPhiUsedOutsideLoop = true;
1875       break;
1876     }
1877   bool IsCntInstUsedOutsideLoop = false;
1878   for (User *U : CntInst->users())
1879     if (!CurLoop->contains(cast<Instruction>(U))) {
1880       IsCntInstUsedOutsideLoop = true;
1881       break;
1882     }
1883   // If both CntInst and CntPhi are used outside the loop the profitability
1884   // is questionable.
1885   if (IsCntInstUsedOutsideLoop && IsCntPhiUsedOutsideLoop)
1886     return false;
1887 
1888   // For some CPUs result of CTLZ(X) intrinsic is undefined
1889   // when X is 0. If we can not guarantee X != 0, we need to check this
1890   // when expand.
1891   bool ZeroCheck = false;
1892   // It is safe to assume Preheader exist as it was checked in
1893   // parent function RunOnLoop.
1894   BasicBlock *PH = CurLoop->getLoopPreheader();
1895 
1896   // If we are using the count instruction outside the loop, make sure we
1897   // have a zero check as a precondition. Without the check the loop would run
1898   // one iteration for before any check of the input value. This means 0 and 1
1899   // would have identical behavior in the original loop and thus
1900   if (!IsCntPhiUsedOutsideLoop) {
1901     auto *PreCondBB = PH->getSinglePredecessor();
1902     if (!PreCondBB)
1903       return false;
1904     auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1905     if (!PreCondBI)
1906       return false;
1907     if (matchCondition(PreCondBI, PH) != InitX)
1908       return false;
1909     ZeroCheck = true;
1910   }
1911 
1912   // Check if CTLZ / CTTZ intrinsic is profitable. Assume it is always
1913   // profitable if we delete the loop.
1914 
1915   // the loop has only 6 instructions:
1916   //  %n.addr.0 = phi [ %n, %entry ], [ %shr, %while.cond ]
1917   //  %i.0 = phi [ %i0, %entry ], [ %inc, %while.cond ]
1918   //  %shr = ashr %n.addr.0, 1
1919   //  %tobool = icmp eq %shr, 0
1920   //  %inc = add nsw %i.0, 1
1921   //  br i1 %tobool
1922 
1923   const Value *Args[] = {InitX,
1924                          ConstantInt::getBool(InitX->getContext(), ZeroCheck)};
1925 
1926   // @llvm.dbg doesn't count as they have no semantic effect.
1927   auto InstWithoutDebugIt = CurLoop->getHeader()->instructionsWithoutDebug();
1928   uint32_t HeaderSize =
1929       std::distance(InstWithoutDebugIt.begin(), InstWithoutDebugIt.end());
1930 
1931   IntrinsicCostAttributes Attrs(IntrinID, InitX->getType(), Args);
1932   InstructionCost Cost =
1933     TTI->getIntrinsicInstrCost(Attrs, TargetTransformInfo::TCK_SizeAndLatency);
1934   if (HeaderSize != IdiomCanonicalSize &&
1935       Cost > TargetTransformInfo::TCC_Basic)
1936     return false;
1937 
1938   transformLoopToCountable(IntrinID, PH, CntInst, CntPhi, InitX, DefX,
1939                            DefX->getDebugLoc(), ZeroCheck,
1940                            IsCntPhiUsedOutsideLoop);
1941   return true;
1942 }
1943 
1944 /// Recognizes a population count idiom in a non-countable loop.
1945 ///
1946 /// If detected, transforms the relevant code to issue the popcount intrinsic
1947 /// function call, and returns true; otherwise, returns false.
1948 bool LoopIdiomRecognize::recognizePopcount() {
1949   if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware)
1950     return false;
1951 
1952   // Counting population are usually conducted by few arithmetic instructions.
1953   // Such instructions can be easily "absorbed" by vacant slots in a
1954   // non-compact loop. Therefore, recognizing popcount idiom only makes sense
1955   // in a compact loop.
1956 
1957   // Give up if the loop has multiple blocks or multiple backedges.
1958   if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
1959     return false;
1960 
1961   BasicBlock *LoopBody = *(CurLoop->block_begin());
1962   if (LoopBody->size() >= 20) {
1963     // The loop is too big, bail out.
1964     return false;
1965   }
1966 
1967   // It should have a preheader containing nothing but an unconditional branch.
1968   BasicBlock *PH = CurLoop->getLoopPreheader();
1969   if (!PH || &PH->front() != PH->getTerminator())
1970     return false;
1971   auto *EntryBI = dyn_cast<BranchInst>(PH->getTerminator());
1972   if (!EntryBI || EntryBI->isConditional())
1973     return false;
1974 
1975   // It should have a precondition block where the generated popcount intrinsic
1976   // function can be inserted.
1977   auto *PreCondBB = PH->getSinglePredecessor();
1978   if (!PreCondBB)
1979     return false;
1980   auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1981   if (!PreCondBI || PreCondBI->isUnconditional())
1982     return false;
1983 
1984   Instruction *CntInst;
1985   PHINode *CntPhi;
1986   Value *Val;
1987   if (!detectPopcountIdiom(CurLoop, PreCondBB, CntInst, CntPhi, Val))
1988     return false;
1989 
1990   transformLoopToPopcount(PreCondBB, CntInst, CntPhi, Val);
1991   return true;
1992 }
1993 
1994 static CallInst *createPopcntIntrinsic(IRBuilder<> &IRBuilder, Value *Val,
1995                                        const DebugLoc &DL) {
1996   Value *Ops[] = {Val};
1997   Type *Tys[] = {Val->getType()};
1998 
1999   Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent();
2000   Function *Func = Intrinsic::getDeclaration(M, Intrinsic::ctpop, Tys);
2001   CallInst *CI = IRBuilder.CreateCall(Func, Ops);
2002   CI->setDebugLoc(DL);
2003 
2004   return CI;
2005 }
2006 
2007 static CallInst *createFFSIntrinsic(IRBuilder<> &IRBuilder, Value *Val,
2008                                     const DebugLoc &DL, bool ZeroCheck,
2009                                     Intrinsic::ID IID) {
2010   Value *Ops[] = {Val, IRBuilder.getInt1(ZeroCheck)};
2011   Type *Tys[] = {Val->getType()};
2012 
2013   Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent();
2014   Function *Func = Intrinsic::getDeclaration(M, IID, Tys);
2015   CallInst *CI = IRBuilder.CreateCall(Func, Ops);
2016   CI->setDebugLoc(DL);
2017 
2018   return CI;
2019 }
2020 
2021 /// Transform the following loop (Using CTLZ, CTTZ is similar):
2022 /// loop:
2023 ///   CntPhi = PHI [Cnt0, CntInst]
2024 ///   PhiX = PHI [InitX, DefX]
2025 ///   CntInst = CntPhi + 1
2026 ///   DefX = PhiX >> 1
2027 ///   LOOP_BODY
2028 ///   Br: loop if (DefX != 0)
2029 /// Use(CntPhi) or Use(CntInst)
2030 ///
2031 /// Into:
2032 /// If CntPhi used outside the loop:
2033 ///   CountPrev = BitWidth(InitX) - CTLZ(InitX >> 1)
2034 ///   Count = CountPrev + 1
2035 /// else
2036 ///   Count = BitWidth(InitX) - CTLZ(InitX)
2037 /// loop:
2038 ///   CntPhi = PHI [Cnt0, CntInst]
2039 ///   PhiX = PHI [InitX, DefX]
2040 ///   PhiCount = PHI [Count, Dec]
2041 ///   CntInst = CntPhi + 1
2042 ///   DefX = PhiX >> 1
2043 ///   Dec = PhiCount - 1
2044 ///   LOOP_BODY
2045 ///   Br: loop if (Dec != 0)
2046 /// Use(CountPrev + Cnt0) // Use(CntPhi)
2047 /// or
2048 /// Use(Count + Cnt0) // Use(CntInst)
2049 ///
2050 /// If LOOP_BODY is empty the loop will be deleted.
2051 /// If CntInst and DefX are not used in LOOP_BODY they will be removed.
2052 void LoopIdiomRecognize::transformLoopToCountable(
2053     Intrinsic::ID IntrinID, BasicBlock *Preheader, Instruction *CntInst,
2054     PHINode *CntPhi, Value *InitX, Instruction *DefX, const DebugLoc &DL,
2055     bool ZeroCheck, bool IsCntPhiUsedOutsideLoop) {
2056   BranchInst *PreheaderBr = cast<BranchInst>(Preheader->getTerminator());
2057 
2058   // Step 1: Insert the CTLZ/CTTZ instruction at the end of the preheader block
2059   IRBuilder<> Builder(PreheaderBr);
2060   Builder.SetCurrentDebugLocation(DL);
2061 
2062   // If there are no uses of CntPhi crate:
2063   //   Count = BitWidth - CTLZ(InitX);
2064   //   NewCount = Count;
2065   // If there are uses of CntPhi create:
2066   //   NewCount = BitWidth - CTLZ(InitX >> 1);
2067   //   Count = NewCount + 1;
2068   Value *InitXNext;
2069   if (IsCntPhiUsedOutsideLoop) {
2070     if (DefX->getOpcode() == Instruction::AShr)
2071       InitXNext = Builder.CreateAShr(InitX, 1);
2072     else if (DefX->getOpcode() == Instruction::LShr)
2073       InitXNext = Builder.CreateLShr(InitX, 1);
2074     else if (DefX->getOpcode() == Instruction::Shl) // cttz
2075       InitXNext = Builder.CreateShl(InitX, 1);
2076     else
2077       llvm_unreachable("Unexpected opcode!");
2078   } else
2079     InitXNext = InitX;
2080   Value *Count =
2081       createFFSIntrinsic(Builder, InitXNext, DL, ZeroCheck, IntrinID);
2082   Type *CountTy = Count->getType();
2083   Count = Builder.CreateSub(
2084       ConstantInt::get(CountTy, CountTy->getIntegerBitWidth()), Count);
2085   Value *NewCount = Count;
2086   if (IsCntPhiUsedOutsideLoop)
2087     Count = Builder.CreateAdd(Count, ConstantInt::get(CountTy, 1));
2088 
2089   NewCount = Builder.CreateZExtOrTrunc(NewCount, CntInst->getType());
2090 
2091   Value *CntInitVal = CntPhi->getIncomingValueForBlock(Preheader);
2092   if (cast<ConstantInt>(CntInst->getOperand(1))->isOne()) {
2093     // If the counter was being incremented in the loop, add NewCount to the
2094     // counter's initial value, but only if the initial value is not zero.
2095     ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
2096     if (!InitConst || !InitConst->isZero())
2097       NewCount = Builder.CreateAdd(NewCount, CntInitVal);
2098   } else {
2099     // If the count was being decremented in the loop, subtract NewCount from
2100     // the counter's initial value.
2101     NewCount = Builder.CreateSub(CntInitVal, NewCount);
2102   }
2103 
2104   // Step 2: Insert new IV and loop condition:
2105   // loop:
2106   //   ...
2107   //   PhiCount = PHI [Count, Dec]
2108   //   ...
2109   //   Dec = PhiCount - 1
2110   //   ...
2111   //   Br: loop if (Dec != 0)
2112   BasicBlock *Body = *(CurLoop->block_begin());
2113   auto *LbBr = cast<BranchInst>(Body->getTerminator());
2114   ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
2115 
2116   PHINode *TcPhi = PHINode::Create(CountTy, 2, "tcphi", &Body->front());
2117 
2118   Builder.SetInsertPoint(LbCond);
2119   Instruction *TcDec = cast<Instruction>(Builder.CreateSub(
2120       TcPhi, ConstantInt::get(CountTy, 1), "tcdec", false, true));
2121 
2122   TcPhi->addIncoming(Count, Preheader);
2123   TcPhi->addIncoming(TcDec, Body);
2124 
2125   CmpInst::Predicate Pred =
2126       (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ;
2127   LbCond->setPredicate(Pred);
2128   LbCond->setOperand(0, TcDec);
2129   LbCond->setOperand(1, ConstantInt::get(CountTy, 0));
2130 
2131   // Step 3: All the references to the original counter outside
2132   //  the loop are replaced with the NewCount
2133   if (IsCntPhiUsedOutsideLoop)
2134     CntPhi->replaceUsesOutsideBlock(NewCount, Body);
2135   else
2136     CntInst->replaceUsesOutsideBlock(NewCount, Body);
2137 
2138   // step 4: Forget the "non-computable" trip-count SCEV associated with the
2139   //   loop. The loop would otherwise not be deleted even if it becomes empty.
2140   SE->forgetLoop(CurLoop);
2141 }
2142 
2143 void LoopIdiomRecognize::transformLoopToPopcount(BasicBlock *PreCondBB,
2144                                                  Instruction *CntInst,
2145                                                  PHINode *CntPhi, Value *Var) {
2146   BasicBlock *PreHead = CurLoop->getLoopPreheader();
2147   auto *PreCondBr = cast<BranchInst>(PreCondBB->getTerminator());
2148   const DebugLoc &DL = CntInst->getDebugLoc();
2149 
2150   // Assuming before transformation, the loop is following:
2151   //  if (x) // the precondition
2152   //     do { cnt++; x &= x - 1; } while(x);
2153 
2154   // Step 1: Insert the ctpop instruction at the end of the precondition block
2155   IRBuilder<> Builder(PreCondBr);
2156   Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
2157   {
2158     PopCnt = createPopcntIntrinsic(Builder, Var, DL);
2159     NewCount = PopCntZext =
2160         Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType()));
2161 
2162     if (NewCount != PopCnt)
2163       (cast<Instruction>(NewCount))->setDebugLoc(DL);
2164 
2165     // TripCnt is exactly the number of iterations the loop has
2166     TripCnt = NewCount;
2167 
2168     // If the population counter's initial value is not zero, insert Add Inst.
2169     Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead);
2170     ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
2171     if (!InitConst || !InitConst->isZero()) {
2172       NewCount = Builder.CreateAdd(NewCount, CntInitVal);
2173       (cast<Instruction>(NewCount))->setDebugLoc(DL);
2174     }
2175   }
2176 
2177   // Step 2: Replace the precondition from "if (x == 0) goto loop-exit" to
2178   //   "if (NewCount == 0) loop-exit". Without this change, the intrinsic
2179   //   function would be partial dead code, and downstream passes will drag
2180   //   it back from the precondition block to the preheader.
2181   {
2182     ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition());
2183 
2184     Value *Opnd0 = PopCntZext;
2185     Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0);
2186     if (PreCond->getOperand(0) != Var)
2187       std::swap(Opnd0, Opnd1);
2188 
2189     ICmpInst *NewPreCond = cast<ICmpInst>(
2190         Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1));
2191     PreCondBr->setCondition(NewPreCond);
2192 
2193     RecursivelyDeleteTriviallyDeadInstructions(PreCond, TLI);
2194   }
2195 
2196   // Step 3: Note that the population count is exactly the trip count of the
2197   // loop in question, which enable us to convert the loop from noncountable
2198   // loop into a countable one. The benefit is twofold:
2199   //
2200   //  - If the loop only counts population, the entire loop becomes dead after
2201   //    the transformation. It is a lot easier to prove a countable loop dead
2202   //    than to prove a noncountable one. (In some C dialects, an infinite loop
2203   //    isn't dead even if it computes nothing useful. In general, DCE needs
2204   //    to prove a noncountable loop finite before safely delete it.)
2205   //
2206   //  - If the loop also performs something else, it remains alive.
2207   //    Since it is transformed to countable form, it can be aggressively
2208   //    optimized by some optimizations which are in general not applicable
2209   //    to a noncountable loop.
2210   //
2211   // After this step, this loop (conceptually) would look like following:
2212   //   newcnt = __builtin_ctpop(x);
2213   //   t = newcnt;
2214   //   if (x)
2215   //     do { cnt++; x &= x-1; t--) } while (t > 0);
2216   BasicBlock *Body = *(CurLoop->block_begin());
2217   {
2218     auto *LbBr = cast<BranchInst>(Body->getTerminator());
2219     ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
2220     Type *Ty = TripCnt->getType();
2221 
2222     PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front());
2223 
2224     Builder.SetInsertPoint(LbCond);
2225     Instruction *TcDec = cast<Instruction>(
2226         Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1),
2227                           "tcdec", false, true));
2228 
2229     TcPhi->addIncoming(TripCnt, PreHead);
2230     TcPhi->addIncoming(TcDec, Body);
2231 
2232     CmpInst::Predicate Pred =
2233         (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_UGT : CmpInst::ICMP_SLE;
2234     LbCond->setPredicate(Pred);
2235     LbCond->setOperand(0, TcDec);
2236     LbCond->setOperand(1, ConstantInt::get(Ty, 0));
2237   }
2238 
2239   // Step 4: All the references to the original population counter outside
2240   //  the loop are replaced with the NewCount -- the value returned from
2241   //  __builtin_ctpop().
2242   CntInst->replaceUsesOutsideBlock(NewCount, Body);
2243 
2244   // step 5: Forget the "non-computable" trip-count SCEV associated with the
2245   //   loop. The loop would otherwise not be deleted even if it becomes empty.
2246   SE->forgetLoop(CurLoop);
2247 }
2248 
2249 /// Match loop-invariant value.
2250 template <typename SubPattern_t> struct match_LoopInvariant {
2251   SubPattern_t SubPattern;
2252   const Loop *L;
2253 
2254   match_LoopInvariant(const SubPattern_t &SP, const Loop *L)
2255       : SubPattern(SP), L(L) {}
2256 
2257   template <typename ITy> bool match(ITy *V) {
2258     return L->isLoopInvariant(V) && SubPattern.match(V);
2259   }
2260 };
2261 
2262 /// Matches if the value is loop-invariant.
2263 template <typename Ty>
2264 inline match_LoopInvariant<Ty> m_LoopInvariant(const Ty &M, const Loop *L) {
2265   return match_LoopInvariant<Ty>(M, L);
2266 }
2267 
2268 /// Return true if the idiom is detected in the loop.
2269 ///
2270 /// The core idiom we are trying to detect is:
2271 /// \code
2272 ///   entry:
2273 ///     <...>
2274 ///     %bitmask = shl i32 1, %bitpos
2275 ///     br label %loop
2276 ///
2277 ///   loop:
2278 ///     %x.curr = phi i32 [ %x, %entry ], [ %x.next, %loop ]
2279 ///     %x.curr.bitmasked = and i32 %x.curr, %bitmask
2280 ///     %x.curr.isbitunset = icmp eq i32 %x.curr.bitmasked, 0
2281 ///     %x.next = shl i32 %x.curr, 1
2282 ///     <...>
2283 ///     br i1 %x.curr.isbitunset, label %loop, label %end
2284 ///
2285 ///   end:
2286 ///     %x.curr.res = phi i32 [ %x.curr, %loop ] <...>
2287 ///     %x.next.res = phi i32 [ %x.next, %loop ] <...>
2288 ///     <...>
2289 /// \endcode
2290 static bool detectShiftUntilBitTestIdiom(Loop *CurLoop, Value *&BaseX,
2291                                          Value *&BitMask, Value *&BitPos,
2292                                          Value *&CurrX, Instruction *&NextX) {
2293   LLVM_DEBUG(dbgs() << DEBUG_TYPE
2294              " Performing shift-until-bittest idiom detection.\n");
2295 
2296   // Give up if the loop has multiple blocks or multiple backedges.
2297   if (CurLoop->getNumBlocks() != 1 || CurLoop->getNumBackEdges() != 1) {
2298     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad block/backedge count.\n");
2299     return false;
2300   }
2301 
2302   BasicBlock *LoopHeaderBB = CurLoop->getHeader();
2303   BasicBlock *LoopPreheaderBB = CurLoop->getLoopPreheader();
2304   assert(LoopPreheaderBB && "There is always a loop preheader.");
2305 
2306   using namespace PatternMatch;
2307 
2308   // Step 1: Check if the loop backedge is in desirable form.
2309 
2310   ICmpInst::Predicate Pred;
2311   Value *CmpLHS, *CmpRHS;
2312   BasicBlock *TrueBB, *FalseBB;
2313   if (!match(LoopHeaderBB->getTerminator(),
2314              m_Br(m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)),
2315                   m_BasicBlock(TrueBB), m_BasicBlock(FalseBB)))) {
2316     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge structure.\n");
2317     return false;
2318   }
2319 
2320   // Step 2: Check if the backedge's condition is in desirable form.
2321 
2322   auto MatchVariableBitMask = [&]() {
2323     return ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero()) &&
2324            match(CmpLHS,
2325                  m_c_And(m_Value(CurrX),
2326                          m_CombineAnd(
2327                              m_Value(BitMask),
2328                              m_LoopInvariant(m_Shl(m_One(), m_Value(BitPos)),
2329                                              CurLoop))));
2330   };
2331   auto MatchConstantBitMask = [&]() {
2332     return ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero()) &&
2333            match(CmpLHS, m_And(m_Value(CurrX),
2334                                m_CombineAnd(m_Value(BitMask), m_Power2()))) &&
2335            (BitPos = ConstantExpr::getExactLogBase2(cast<Constant>(BitMask)));
2336   };
2337   auto MatchDecomposableConstantBitMask = [&]() {
2338     APInt Mask;
2339     return llvm::decomposeBitTestICmp(CmpLHS, CmpRHS, Pred, CurrX, Mask) &&
2340            ICmpInst::isEquality(Pred) && Mask.isPowerOf2() &&
2341            (BitMask = ConstantInt::get(CurrX->getType(), Mask)) &&
2342            (BitPos = ConstantInt::get(CurrX->getType(), Mask.logBase2()));
2343   };
2344 
2345   if (!MatchVariableBitMask() && !MatchConstantBitMask() &&
2346       !MatchDecomposableConstantBitMask()) {
2347     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge comparison.\n");
2348     return false;
2349   }
2350 
2351   // Step 3: Check if the recurrence is in desirable form.
2352   auto *CurrXPN = dyn_cast<PHINode>(CurrX);
2353   if (!CurrXPN || CurrXPN->getParent() != LoopHeaderBB) {
2354     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Not an expected PHI node.\n");
2355     return false;
2356   }
2357 
2358   BaseX = CurrXPN->getIncomingValueForBlock(LoopPreheaderBB);
2359   NextX =
2360       dyn_cast<Instruction>(CurrXPN->getIncomingValueForBlock(LoopHeaderBB));
2361 
2362   assert(CurLoop->isLoopInvariant(BaseX) &&
2363          "Expected BaseX to be avaliable in the preheader!");
2364 
2365   if (!NextX || !match(NextX, m_Shl(m_Specific(CurrX), m_One()))) {
2366     // FIXME: support right-shift?
2367     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad recurrence.\n");
2368     return false;
2369   }
2370 
2371   // Step 4: Check if the backedge's destinations are in desirable form.
2372 
2373   assert(ICmpInst::isEquality(Pred) &&
2374          "Should only get equality predicates here.");
2375 
2376   // cmp-br is commutative, so canonicalize to a single variant.
2377   if (Pred != ICmpInst::Predicate::ICMP_EQ) {
2378     Pred = ICmpInst::getInversePredicate(Pred);
2379     std::swap(TrueBB, FalseBB);
2380   }
2381 
2382   // We expect to exit loop when comparison yields false,
2383   // so when it yields true we should branch back to loop header.
2384   if (TrueBB != LoopHeaderBB) {
2385     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge flow.\n");
2386     return false;
2387   }
2388 
2389   // Okay, idiom checks out.
2390   return true;
2391 }
2392 
2393 /// Look for the following loop:
2394 /// \code
2395 ///   entry:
2396 ///     <...>
2397 ///     %bitmask = shl i32 1, %bitpos
2398 ///     br label %loop
2399 ///
2400 ///   loop:
2401 ///     %x.curr = phi i32 [ %x, %entry ], [ %x.next, %loop ]
2402 ///     %x.curr.bitmasked = and i32 %x.curr, %bitmask
2403 ///     %x.curr.isbitunset = icmp eq i32 %x.curr.bitmasked, 0
2404 ///     %x.next = shl i32 %x.curr, 1
2405 ///     <...>
2406 ///     br i1 %x.curr.isbitunset, label %loop, label %end
2407 ///
2408 ///   end:
2409 ///     %x.curr.res = phi i32 [ %x.curr, %loop ] <...>
2410 ///     %x.next.res = phi i32 [ %x.next, %loop ] <...>
2411 ///     <...>
2412 /// \endcode
2413 ///
2414 /// And transform it into:
2415 /// \code
2416 ///   entry:
2417 ///     %bitmask = shl i32 1, %bitpos
2418 ///     %lowbitmask = add i32 %bitmask, -1
2419 ///     %mask = or i32 %lowbitmask, %bitmask
2420 ///     %x.masked = and i32 %x, %mask
2421 ///     %x.masked.numleadingzeros = call i32 @llvm.ctlz.i32(i32 %x.masked,
2422 ///                                                         i1 true)
2423 ///     %x.masked.numactivebits = sub i32 32, %x.masked.numleadingzeros
2424 ///     %x.masked.leadingonepos = add i32 %x.masked.numactivebits, -1
2425 ///     %backedgetakencount = sub i32 %bitpos, %x.masked.leadingonepos
2426 ///     %tripcount = add i32 %backedgetakencount, 1
2427 ///     %x.curr = shl i32 %x, %backedgetakencount
2428 ///     %x.next = shl i32 %x, %tripcount
2429 ///     br label %loop
2430 ///
2431 ///   loop:
2432 ///     %loop.iv = phi i32 [ 0, %entry ], [ %loop.iv.next, %loop ]
2433 ///     %loop.iv.next = add nuw i32 %loop.iv, 1
2434 ///     %loop.ivcheck = icmp eq i32 %loop.iv.next, %tripcount
2435 ///     <...>
2436 ///     br i1 %loop.ivcheck, label %end, label %loop
2437 ///
2438 ///   end:
2439 ///     %x.curr.res = phi i32 [ %x.curr, %loop ] <...>
2440 ///     %x.next.res = phi i32 [ %x.next, %loop ] <...>
2441 ///     <...>
2442 /// \endcode
2443 bool LoopIdiomRecognize::recognizeShiftUntilBitTest() {
2444   bool MadeChange = false;
2445 
2446   Value *X, *BitMask, *BitPos, *XCurr;
2447   Instruction *XNext;
2448   if (!detectShiftUntilBitTestIdiom(CurLoop, X, BitMask, BitPos, XCurr,
2449                                     XNext)) {
2450     LLVM_DEBUG(dbgs() << DEBUG_TYPE
2451                " shift-until-bittest idiom detection failed.\n");
2452     return MadeChange;
2453   }
2454   LLVM_DEBUG(dbgs() << DEBUG_TYPE " shift-until-bittest idiom detected!\n");
2455 
2456   // Ok, it is the idiom we were looking for, we *could* transform this loop,
2457   // but is it profitable to transform?
2458 
2459   BasicBlock *LoopHeaderBB = CurLoop->getHeader();
2460   BasicBlock *LoopPreheaderBB = CurLoop->getLoopPreheader();
2461   assert(LoopPreheaderBB && "There is always a loop preheader.");
2462 
2463   BasicBlock *SuccessorBB = CurLoop->getExitBlock();
2464   assert(SuccessorBB && "There is only a single successor.");
2465 
2466   IRBuilder<> Builder(LoopPreheaderBB->getTerminator());
2467   Builder.SetCurrentDebugLocation(cast<Instruction>(XCurr)->getDebugLoc());
2468 
2469   Intrinsic::ID IntrID = Intrinsic::ctlz;
2470   Type *Ty = X->getType();
2471   unsigned Bitwidth = Ty->getScalarSizeInBits();
2472 
2473   TargetTransformInfo::TargetCostKind CostKind =
2474       TargetTransformInfo::TCK_SizeAndLatency;
2475 
2476   // The rewrite is considered to be unprofitable iff and only iff the
2477   // intrinsic/shift we'll use are not cheap. Note that we are okay with *just*
2478   // making the loop countable, even if nothing else changes.
2479   IntrinsicCostAttributes Attrs(
2480       IntrID, Ty, {UndefValue::get(Ty), /*is_zero_undef=*/Builder.getTrue()});
2481   InstructionCost Cost = TTI->getIntrinsicInstrCost(Attrs, CostKind);
2482   if (Cost > TargetTransformInfo::TCC_Basic) {
2483     LLVM_DEBUG(dbgs() << DEBUG_TYPE
2484                " Intrinsic is too costly, not beneficial\n");
2485     return MadeChange;
2486   }
2487   if (TTI->getArithmeticInstrCost(Instruction::Shl, Ty, CostKind) >
2488       TargetTransformInfo::TCC_Basic) {
2489     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Shift is too costly, not beneficial\n");
2490     return MadeChange;
2491   }
2492 
2493   // Ok, transform appears worthwhile.
2494   MadeChange = true;
2495 
2496   // Step 1: Compute the loop trip count.
2497 
2498   Value *LowBitMask = Builder.CreateAdd(BitMask, Constant::getAllOnesValue(Ty),
2499                                         BitPos->getName() + ".lowbitmask");
2500   Value *Mask =
2501       Builder.CreateOr(LowBitMask, BitMask, BitPos->getName() + ".mask");
2502   Value *XMasked = Builder.CreateAnd(X, Mask, X->getName() + ".masked");
2503   CallInst *XMaskedNumLeadingZeros = Builder.CreateIntrinsic(
2504       IntrID, Ty, {XMasked, /*is_zero_undef=*/Builder.getTrue()},
2505       /*FMFSource=*/nullptr, XMasked->getName() + ".numleadingzeros");
2506   Value *XMaskedNumActiveBits = Builder.CreateSub(
2507       ConstantInt::get(Ty, Ty->getScalarSizeInBits()), XMaskedNumLeadingZeros,
2508       XMasked->getName() + ".numactivebits", /*HasNUW=*/true,
2509       /*HasNSW=*/Bitwidth != 2);
2510   Value *XMaskedLeadingOnePos =
2511       Builder.CreateAdd(XMaskedNumActiveBits, Constant::getAllOnesValue(Ty),
2512                         XMasked->getName() + ".leadingonepos", /*HasNUW=*/false,
2513                         /*HasNSW=*/Bitwidth > 2);
2514 
2515   Value *LoopBackedgeTakenCount = Builder.CreateSub(
2516       BitPos, XMaskedLeadingOnePos, CurLoop->getName() + ".backedgetakencount",
2517       /*HasNUW=*/true, /*HasNSW=*/true);
2518   // We know loop's backedge-taken count, but what's loop's trip count?
2519   // Note that while NUW is always safe, while NSW is only for bitwidths != 2.
2520   Value *LoopTripCount =
2521       Builder.CreateAdd(LoopBackedgeTakenCount, ConstantInt::get(Ty, 1),
2522                         CurLoop->getName() + ".tripcount", /*HasNUW=*/true,
2523                         /*HasNSW=*/Bitwidth != 2);
2524 
2525   // Step 2: Compute the recurrence's final value without a loop.
2526 
2527   // NewX is always safe to compute, because `LoopBackedgeTakenCount`
2528   // will always be smaller than `bitwidth(X)`, i.e. we never get poison.
2529   Value *NewX = Builder.CreateShl(X, LoopBackedgeTakenCount);
2530   NewX->takeName(XCurr);
2531   if (auto *I = dyn_cast<Instruction>(NewX))
2532     I->copyIRFlags(XNext, /*IncludeWrapFlags=*/true);
2533 
2534   Value *NewXNext;
2535   // Rewriting XNext is more complicated, however, because `X << LoopTripCount`
2536   // will be poison iff `LoopTripCount == bitwidth(X)` (which will happen
2537   // iff `BitPos` is `bitwidth(x) - 1` and `X` is `1`). So unless we know
2538   // that isn't the case, we'll need to emit an alternative, safe IR.
2539   if (XNext->hasNoSignedWrap() || XNext->hasNoUnsignedWrap() ||
2540       PatternMatch::match(
2541           BitPos, PatternMatch::m_SpecificInt_ICMP(
2542                       ICmpInst::ICMP_NE, APInt(Ty->getScalarSizeInBits(),
2543                                                Ty->getScalarSizeInBits() - 1))))
2544     NewXNext = Builder.CreateShl(X, LoopTripCount);
2545   else {
2546     // Otherwise, just additionally shift by one. It's the smallest solution,
2547     // alternatively, we could check that NewX is INT_MIN (or BitPos is )
2548     // and select 0 instead.
2549     NewXNext = Builder.CreateShl(NewX, ConstantInt::get(Ty, 1));
2550   }
2551 
2552   NewXNext->takeName(XNext);
2553   if (auto *I = dyn_cast<Instruction>(NewXNext))
2554     I->copyIRFlags(XNext, /*IncludeWrapFlags=*/true);
2555 
2556   // Step 3: Adjust the successor basic block to recieve the computed
2557   //         recurrence's final value instead of the recurrence itself.
2558 
2559   XCurr->replaceUsesOutsideBlock(NewX, LoopHeaderBB);
2560   XNext->replaceUsesOutsideBlock(NewXNext, LoopHeaderBB);
2561 
2562   // Step 4: Rewrite the loop into a countable form, with canonical IV.
2563 
2564   // The new canonical induction variable.
2565   Builder.SetInsertPoint(&LoopHeaderBB->front());
2566   auto *IV = Builder.CreatePHI(Ty, 2, CurLoop->getName() + ".iv");
2567 
2568   // The induction itself.
2569   // Note that while NUW is always safe, while NSW is only for bitwidths != 2.
2570   Builder.SetInsertPoint(LoopHeaderBB->getTerminator());
2571   auto *IVNext =
2572       Builder.CreateAdd(IV, ConstantInt::get(Ty, 1), IV->getName() + ".next",
2573                         /*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2);
2574 
2575   // The loop trip count check.
2576   auto *IVCheck = Builder.CreateICmpEQ(IVNext, LoopTripCount,
2577                                        CurLoop->getName() + ".ivcheck");
2578   Builder.CreateCondBr(IVCheck, SuccessorBB, LoopHeaderBB);
2579   LoopHeaderBB->getTerminator()->eraseFromParent();
2580 
2581   // Populate the IV PHI.
2582   IV->addIncoming(ConstantInt::get(Ty, 0), LoopPreheaderBB);
2583   IV->addIncoming(IVNext, LoopHeaderBB);
2584 
2585   // Step 5: Forget the "non-computable" trip-count SCEV associated with the
2586   //   loop. The loop would otherwise not be deleted even if it becomes empty.
2587 
2588   SE->forgetLoop(CurLoop);
2589 
2590   // Other passes will take care of actually deleting the loop if possible.
2591 
2592   LLVM_DEBUG(dbgs() << DEBUG_TYPE " shift-until-bittest idiom optimized!\n");
2593 
2594   ++NumShiftUntilBitTest;
2595   return MadeChange;
2596 }
2597 
2598 /// Return true if the idiom is detected in the loop.
2599 ///
2600 /// The core idiom we are trying to detect is:
2601 /// \code
2602 ///   entry:
2603 ///     <...>
2604 ///     %start = <...>
2605 ///     %extraoffset = <...>
2606 ///     <...>
2607 ///     br label %for.cond
2608 ///
2609 ///   loop:
2610 ///     %iv = phi i8 [ %start, %entry ], [ %iv.next, %for.cond ]
2611 ///     %nbits = add nsw i8 %iv, %extraoffset
2612 ///     %val.shifted = {{l,a}shr,shl} i8 %val, %nbits
2613 ///     %val.shifted.iszero = icmp eq i8 %val.shifted, 0
2614 ///     %iv.next = add i8 %iv, 1
2615 ///     <...>
2616 ///     br i1 %val.shifted.iszero, label %end, label %loop
2617 ///
2618 ///   end:
2619 ///     %iv.res = phi i8 [ %iv, %loop ] <...>
2620 ///     %nbits.res = phi i8 [ %nbits, %loop ] <...>
2621 ///     %val.shifted.res = phi i8 [ %val.shifted, %loop ] <...>
2622 ///     %val.shifted.iszero.res = phi i1 [ %val.shifted.iszero, %loop ] <...>
2623 ///     %iv.next.res = phi i8 [ %iv.next, %loop ] <...>
2624 ///     <...>
2625 /// \endcode
2626 static bool detectShiftUntilZeroIdiom(Loop *CurLoop, ScalarEvolution *SE,
2627                                       Instruction *&ValShiftedIsZero,
2628                                       Intrinsic::ID &IntrinID, Instruction *&IV,
2629                                       Value *&Start, Value *&Val,
2630                                       const SCEV *&ExtraOffsetExpr,
2631                                       bool &InvertedCond) {
2632   LLVM_DEBUG(dbgs() << DEBUG_TYPE
2633              " Performing shift-until-zero idiom detection.\n");
2634 
2635   // Give up if the loop has multiple blocks or multiple backedges.
2636   if (CurLoop->getNumBlocks() != 1 || CurLoop->getNumBackEdges() != 1) {
2637     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad block/backedge count.\n");
2638     return false;
2639   }
2640 
2641   Instruction *ValShifted, *NBits, *IVNext;
2642   Value *ExtraOffset;
2643 
2644   BasicBlock *LoopHeaderBB = CurLoop->getHeader();
2645   BasicBlock *LoopPreheaderBB = CurLoop->getLoopPreheader();
2646   assert(LoopPreheaderBB && "There is always a loop preheader.");
2647 
2648   using namespace PatternMatch;
2649 
2650   // Step 1: Check if the loop backedge, condition is in desirable form.
2651 
2652   ICmpInst::Predicate Pred;
2653   BasicBlock *TrueBB, *FalseBB;
2654   if (!match(LoopHeaderBB->getTerminator(),
2655              m_Br(m_Instruction(ValShiftedIsZero), m_BasicBlock(TrueBB),
2656                   m_BasicBlock(FalseBB))) ||
2657       !match(ValShiftedIsZero,
2658              m_ICmp(Pred, m_Instruction(ValShifted), m_Zero())) ||
2659       !ICmpInst::isEquality(Pred)) {
2660     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge structure.\n");
2661     return false;
2662   }
2663 
2664   // Step 2: Check if the comparison's operand is in desirable form.
2665   // FIXME: Val could be a one-input PHI node, which we should look past.
2666   if (!match(ValShifted, m_Shift(m_LoopInvariant(m_Value(Val), CurLoop),
2667                                  m_Instruction(NBits)))) {
2668     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad comparisons value computation.\n");
2669     return false;
2670   }
2671   IntrinID = ValShifted->getOpcode() == Instruction::Shl ? Intrinsic::cttz
2672                                                          : Intrinsic::ctlz;
2673 
2674   // Step 3: Check if the shift amount is in desirable form.
2675 
2676   if (match(NBits, m_c_Add(m_Instruction(IV),
2677                            m_LoopInvariant(m_Value(ExtraOffset), CurLoop))) &&
2678       (NBits->hasNoSignedWrap() || NBits->hasNoUnsignedWrap()))
2679     ExtraOffsetExpr = SE->getNegativeSCEV(SE->getSCEV(ExtraOffset));
2680   else if (match(NBits,
2681                  m_Sub(m_Instruction(IV),
2682                        m_LoopInvariant(m_Value(ExtraOffset), CurLoop))) &&
2683            NBits->hasNoSignedWrap())
2684     ExtraOffsetExpr = SE->getSCEV(ExtraOffset);
2685   else {
2686     IV = NBits;
2687     ExtraOffsetExpr = SE->getZero(NBits->getType());
2688   }
2689 
2690   // Step 4: Check if the recurrence is in desirable form.
2691   auto *IVPN = dyn_cast<PHINode>(IV);
2692   if (!IVPN || IVPN->getParent() != LoopHeaderBB) {
2693     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Not an expected PHI node.\n");
2694     return false;
2695   }
2696 
2697   Start = IVPN->getIncomingValueForBlock(LoopPreheaderBB);
2698   IVNext = dyn_cast<Instruction>(IVPN->getIncomingValueForBlock(LoopHeaderBB));
2699 
2700   if (!IVNext || !match(IVNext, m_Add(m_Specific(IVPN), m_One()))) {
2701     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad recurrence.\n");
2702     return false;
2703   }
2704 
2705   // Step 4: Check if the backedge's destinations are in desirable form.
2706 
2707   assert(ICmpInst::isEquality(Pred) &&
2708          "Should only get equality predicates here.");
2709 
2710   // cmp-br is commutative, so canonicalize to a single variant.
2711   InvertedCond = Pred != ICmpInst::Predicate::ICMP_EQ;
2712   if (InvertedCond) {
2713     Pred = ICmpInst::getInversePredicate(Pred);
2714     std::swap(TrueBB, FalseBB);
2715   }
2716 
2717   // We expect to exit loop when comparison yields true,
2718   // so when it yields false we should branch back to loop header.
2719   if (FalseBB != LoopHeaderBB) {
2720     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge flow.\n");
2721     return false;
2722   }
2723 
2724   // The new, countable, loop will certainly only run a known number of
2725   // iterations, It won't be infinite. But the old loop might be infinite
2726   // under certain conditions. For logical shifts, the value will become zero
2727   // after at most bitwidth(%Val) loop iterations. However, for arithmetic
2728   // right-shift, iff the sign bit was set, the value will never become zero,
2729   // and the loop may never finish.
2730   if (ValShifted->getOpcode() == Instruction::AShr &&
2731       !isMustProgress(CurLoop) && !SE->isKnownNonNegative(SE->getSCEV(Val))) {
2732     LLVM_DEBUG(dbgs() << DEBUG_TYPE " Can not prove the loop is finite.\n");
2733     return false;
2734   }
2735 
2736   // Okay, idiom checks out.
2737   return true;
2738 }
2739 
2740 /// Look for the following loop:
2741 /// \code
2742 ///   entry:
2743 ///     <...>
2744 ///     %start = <...>
2745 ///     %extraoffset = <...>
2746 ///     <...>
2747 ///     br label %for.cond
2748 ///
2749 ///   loop:
2750 ///     %iv = phi i8 [ %start, %entry ], [ %iv.next, %for.cond ]
2751 ///     %nbits = add nsw i8 %iv, %extraoffset
2752 ///     %val.shifted = {{l,a}shr,shl} i8 %val, %nbits
2753 ///     %val.shifted.iszero = icmp eq i8 %val.shifted, 0
2754 ///     %iv.next = add i8 %iv, 1
2755 ///     <...>
2756 ///     br i1 %val.shifted.iszero, label %end, label %loop
2757 ///
2758 ///   end:
2759 ///     %iv.res = phi i8 [ %iv, %loop ] <...>
2760 ///     %nbits.res = phi i8 [ %nbits, %loop ] <...>
2761 ///     %val.shifted.res = phi i8 [ %val.shifted, %loop ] <...>
2762 ///     %val.shifted.iszero.res = phi i1 [ %val.shifted.iszero, %loop ] <...>
2763 ///     %iv.next.res = phi i8 [ %iv.next, %loop ] <...>
2764 ///     <...>
2765 /// \endcode
2766 ///
2767 /// And transform it into:
2768 /// \code
2769 ///   entry:
2770 ///     <...>
2771 ///     %start = <...>
2772 ///     %extraoffset = <...>
2773 ///     <...>
2774 ///     %val.numleadingzeros = call i8 @llvm.ct{l,t}z.i8(i8 %val, i1 0)
2775 ///     %val.numactivebits = sub i8 8, %val.numleadingzeros
2776 ///     %extraoffset.neg = sub i8 0, %extraoffset
2777 ///     %tmp = add i8 %val.numactivebits, %extraoffset.neg
2778 ///     %iv.final = call i8 @llvm.smax.i8(i8 %tmp, i8 %start)
2779 ///     %loop.tripcount = sub i8 %iv.final, %start
2780 ///     br label %loop
2781 ///
2782 ///   loop:
2783 ///     %loop.iv = phi i8 [ 0, %entry ], [ %loop.iv.next, %loop ]
2784 ///     %loop.iv.next = add i8 %loop.iv, 1
2785 ///     %loop.ivcheck = icmp eq i8 %loop.iv.next, %loop.tripcount
2786 ///     %iv = add i8 %loop.iv, %start
2787 ///     <...>
2788 ///     br i1 %loop.ivcheck, label %end, label %loop
2789 ///
2790 ///   end:
2791 ///     %iv.res = phi i8 [ %iv.final, %loop ] <...>
2792 ///     <...>
2793 /// \endcode
2794 bool LoopIdiomRecognize::recognizeShiftUntilZero() {
2795   bool MadeChange = false;
2796 
2797   Instruction *ValShiftedIsZero;
2798   Intrinsic::ID IntrID;
2799   Instruction *IV;
2800   Value *Start, *Val;
2801   const SCEV *ExtraOffsetExpr;
2802   bool InvertedCond;
2803   if (!detectShiftUntilZeroIdiom(CurLoop, SE, ValShiftedIsZero, IntrID, IV,
2804                                  Start, Val, ExtraOffsetExpr, InvertedCond)) {
2805     LLVM_DEBUG(dbgs() << DEBUG_TYPE
2806                " shift-until-zero idiom detection failed.\n");
2807     return MadeChange;
2808   }
2809   LLVM_DEBUG(dbgs() << DEBUG_TYPE " shift-until-zero idiom detected!\n");
2810 
2811   // Ok, it is the idiom we were looking for, we *could* transform this loop,
2812   // but is it profitable to transform?
2813 
2814   BasicBlock *LoopHeaderBB = CurLoop->getHeader();
2815   BasicBlock *LoopPreheaderBB = CurLoop->getLoopPreheader();
2816   assert(LoopPreheaderBB && "There is always a loop preheader.");
2817 
2818   BasicBlock *SuccessorBB = CurLoop->getExitBlock();
2819   assert(SuccessorBB && "There is only a single successor.");
2820 
2821   IRBuilder<> Builder(LoopPreheaderBB->getTerminator());
2822   Builder.SetCurrentDebugLocation(IV->getDebugLoc());
2823 
2824   Type *Ty = Val->getType();
2825   unsigned Bitwidth = Ty->getScalarSizeInBits();
2826 
2827   TargetTransformInfo::TargetCostKind CostKind =
2828       TargetTransformInfo::TCK_SizeAndLatency;
2829 
2830   // The rewrite is considered to be unprofitable iff and only iff the
2831   // intrinsic we'll use are not cheap. Note that we are okay with *just*
2832   // making the loop countable, even if nothing else changes.
2833   IntrinsicCostAttributes Attrs(
2834       IntrID, Ty, {UndefValue::get(Ty), /*is_zero_undef=*/Builder.getFalse()});
2835   InstructionCost Cost = TTI->getIntrinsicInstrCost(Attrs, CostKind);
2836   if (Cost > TargetTransformInfo::TCC_Basic) {
2837     LLVM_DEBUG(dbgs() << DEBUG_TYPE
2838                " Intrinsic is too costly, not beneficial\n");
2839     return MadeChange;
2840   }
2841 
2842   // Ok, transform appears worthwhile.
2843   MadeChange = true;
2844 
2845   bool OffsetIsZero = false;
2846   if (auto *ExtraOffsetExprC = dyn_cast<SCEVConstant>(ExtraOffsetExpr))
2847     OffsetIsZero = ExtraOffsetExprC->isZero();
2848 
2849   // Step 1: Compute the loop's final IV value / trip count.
2850 
2851   CallInst *ValNumLeadingZeros = Builder.CreateIntrinsic(
2852       IntrID, Ty, {Val, /*is_zero_undef=*/Builder.getFalse()},
2853       /*FMFSource=*/nullptr, Val->getName() + ".numleadingzeros");
2854   Value *ValNumActiveBits = Builder.CreateSub(
2855       ConstantInt::get(Ty, Ty->getScalarSizeInBits()), ValNumLeadingZeros,
2856       Val->getName() + ".numactivebits", /*HasNUW=*/true,
2857       /*HasNSW=*/Bitwidth != 2);
2858 
2859   SCEVExpander Expander(*SE, *DL, "loop-idiom");
2860   Expander.setInsertPoint(&*Builder.GetInsertPoint());
2861   Value *ExtraOffset = Expander.expandCodeFor(ExtraOffsetExpr);
2862 
2863   Value *ValNumActiveBitsOffset = Builder.CreateAdd(
2864       ValNumActiveBits, ExtraOffset, ValNumActiveBits->getName() + ".offset",
2865       /*HasNUW=*/OffsetIsZero, /*HasNSW=*/true);
2866   Value *IVFinal = Builder.CreateIntrinsic(Intrinsic::smax, {Ty},
2867                                            {ValNumActiveBitsOffset, Start},
2868                                            /*FMFSource=*/nullptr, "iv.final");
2869 
2870   auto *LoopBackedgeTakenCount = cast<Instruction>(Builder.CreateSub(
2871       IVFinal, Start, CurLoop->getName() + ".backedgetakencount",
2872       /*HasNUW=*/OffsetIsZero, /*HasNSW=*/true));
2873   // FIXME: or when the offset was `add nuw`
2874 
2875   // We know loop's backedge-taken count, but what's loop's trip count?
2876   Value *LoopTripCount =
2877       Builder.CreateAdd(LoopBackedgeTakenCount, ConstantInt::get(Ty, 1),
2878                         CurLoop->getName() + ".tripcount", /*HasNUW=*/true,
2879                         /*HasNSW=*/Bitwidth != 2);
2880 
2881   // Step 2: Adjust the successor basic block to recieve the original
2882   //         induction variable's final value instead of the orig. IV itself.
2883 
2884   IV->replaceUsesOutsideBlock(IVFinal, LoopHeaderBB);
2885 
2886   // Step 3: Rewrite the loop into a countable form, with canonical IV.
2887 
2888   // The new canonical induction variable.
2889   Builder.SetInsertPoint(&LoopHeaderBB->front());
2890   auto *CIV = Builder.CreatePHI(Ty, 2, CurLoop->getName() + ".iv");
2891 
2892   // The induction itself.
2893   Builder.SetInsertPoint(LoopHeaderBB->getFirstNonPHI());
2894   auto *CIVNext =
2895       Builder.CreateAdd(CIV, ConstantInt::get(Ty, 1), CIV->getName() + ".next",
2896                         /*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2);
2897 
2898   // The loop trip count check.
2899   auto *CIVCheck = Builder.CreateICmpEQ(CIVNext, LoopTripCount,
2900                                         CurLoop->getName() + ".ivcheck");
2901   auto *NewIVCheck = CIVCheck;
2902   if (InvertedCond) {
2903     NewIVCheck = Builder.CreateNot(CIVCheck);
2904     NewIVCheck->takeName(ValShiftedIsZero);
2905   }
2906 
2907   // The original IV, but rebased to be an offset to the CIV.
2908   auto *IVDePHId = Builder.CreateAdd(CIV, Start, "", /*HasNUW=*/false,
2909                                      /*HasNSW=*/true); // FIXME: what about NUW?
2910   IVDePHId->takeName(IV);
2911 
2912   // The loop terminator.
2913   Builder.SetInsertPoint(LoopHeaderBB->getTerminator());
2914   Builder.CreateCondBr(CIVCheck, SuccessorBB, LoopHeaderBB);
2915   LoopHeaderBB->getTerminator()->eraseFromParent();
2916 
2917   // Populate the IV PHI.
2918   CIV->addIncoming(ConstantInt::get(Ty, 0), LoopPreheaderBB);
2919   CIV->addIncoming(CIVNext, LoopHeaderBB);
2920 
2921   // Step 4: Forget the "non-computable" trip-count SCEV associated with the
2922   //   loop. The loop would otherwise not be deleted even if it becomes empty.
2923 
2924   SE->forgetLoop(CurLoop);
2925 
2926   // Step 5: Try to cleanup the loop's body somewhat.
2927   IV->replaceAllUsesWith(IVDePHId);
2928   IV->eraseFromParent();
2929 
2930   ValShiftedIsZero->replaceAllUsesWith(NewIVCheck);
2931   ValShiftedIsZero->eraseFromParent();
2932 
2933   // Other passes will take care of actually deleting the loop if possible.
2934 
2935   LLVM_DEBUG(dbgs() << DEBUG_TYPE " shift-until-zero idiom optimized!\n");
2936 
2937   ++NumShiftUntilZero;
2938   return MadeChange;
2939 }
2940