LLVM  9.0.0svn
LoopIdiomRecognize.cpp
Go to the documentation of this file.
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, memmove, 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/ADT/APInt.h"
40 #include "llvm/ADT/ArrayRef.h"
41 #include "llvm/ADT/DenseMap.h"
42 #include "llvm/ADT/MapVector.h"
43 #include "llvm/ADT/SetVector.h"
44 #include "llvm/ADT/SmallPtrSet.h"
45 #include "llvm/ADT/SmallVector.h"
46 #include "llvm/ADT/Statistic.h"
47 #include "llvm/ADT/StringRef.h"
50 #include "llvm/Analysis/LoopInfo.h"
51 #include "llvm/Analysis/LoopPass.h"
60 #include "llvm/IR/Attributes.h"
61 #include "llvm/IR/BasicBlock.h"
62 #include "llvm/IR/Constant.h"
63 #include "llvm/IR/Constants.h"
64 #include "llvm/IR/DataLayout.h"
65 #include "llvm/IR/DebugLoc.h"
66 #include "llvm/IR/DerivedTypes.h"
67 #include "llvm/IR/Dominators.h"
68 #include "llvm/IR/GlobalValue.h"
69 #include "llvm/IR/GlobalVariable.h"
70 #include "llvm/IR/IRBuilder.h"
71 #include "llvm/IR/InstrTypes.h"
72 #include "llvm/IR/Instruction.h"
73 #include "llvm/IR/Instructions.h"
74 #include "llvm/IR/IntrinsicInst.h"
75 #include "llvm/IR/Intrinsics.h"
76 #include "llvm/IR/LLVMContext.h"
77 #include "llvm/IR/Module.h"
78 #include "llvm/IR/PassManager.h"
79 #include "llvm/IR/Type.h"
80 #include "llvm/IR/User.h"
81 #include "llvm/IR/Value.h"
82 #include "llvm/IR/ValueHandle.h"
83 #include "llvm/Pass.h"
84 #include "llvm/Support/Casting.h"
86 #include "llvm/Support/Debug.h"
88 #include "llvm/Transforms/Scalar.h"
92 #include <algorithm>
93 #include <cassert>
94 #include <cstdint>
95 #include <utility>
96 #include <vector>
97 
98 using namespace llvm;
99 
100 #define DEBUG_TYPE "loop-idiom"
101 
102 STATISTIC(NumMemSet, "Number of memset's formed from loop stores");
103 STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores");
104 
106  "use-lir-code-size-heurs",
107  cl::desc("Use loop idiom recognition code size heuristics when compiling"
108  "with -Os/-Oz"),
109  cl::init(true), cl::Hidden);
110 
111 namespace {
112 
113 class LoopIdiomRecognize {
114  Loop *CurLoop = nullptr;
115  AliasAnalysis *AA;
116  DominatorTree *DT;
117  LoopInfo *LI;
118  ScalarEvolution *SE;
119  TargetLibraryInfo *TLI;
120  const TargetTransformInfo *TTI;
121  const DataLayout *DL;
122  bool ApplyCodeSizeHeuristics;
123 
124 public:
125  explicit LoopIdiomRecognize(AliasAnalysis *AA, DominatorTree *DT,
126  LoopInfo *LI, ScalarEvolution *SE,
127  TargetLibraryInfo *TLI,
128  const TargetTransformInfo *TTI,
129  const DataLayout *DL)
130  : AA(AA), DT(DT), LI(LI), SE(SE), TLI(TLI), TTI(TTI), DL(DL) {}
131 
132  bool runOnLoop(Loop *L);
133 
134 private:
135  using StoreList = SmallVector<StoreInst *, 8>;
136  using StoreListMap = MapVector<Value *, StoreList>;
137 
138  StoreListMap StoreRefsForMemset;
139  StoreListMap StoreRefsForMemsetPattern;
140  StoreList StoreRefsForMemcpy;
141  bool HasMemset;
142  bool HasMemsetPattern;
143  bool HasMemcpy;
144 
145  /// Return code for isLegalStore()
146  enum LegalStoreKind {
147  None = 0,
148  Memset,
149  MemsetPattern,
150  Memcpy,
151  UnorderedAtomicMemcpy,
152  DontUse // Dummy retval never to be used. Allows catching errors in retval
153  // handling.
154  };
155 
156  /// \name Countable Loop Idiom Handling
157  /// @{
158 
159  bool runOnCountableLoop();
160  bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
161  SmallVectorImpl<BasicBlock *> &ExitBlocks);
162 
163  void collectStores(BasicBlock *BB);
164  LegalStoreKind isLegalStore(StoreInst *SI);
165  enum class ForMemset { No, Yes };
166  bool processLoopStores(SmallVectorImpl<StoreInst *> &SL, const SCEV *BECount,
167  ForMemset For);
168  bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount);
169 
170  bool processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
171  unsigned StoreAlignment, Value *StoredVal,
172  Instruction *TheStore,
174  const SCEVAddRecExpr *Ev, const SCEV *BECount,
175  bool NegStride, bool IsLoopMemset = false);
176  bool processLoopStoreOfLoopLoad(StoreInst *SI, const SCEV *BECount);
177  bool avoidLIRForMultiBlockLoop(bool IsMemset = false,
178  bool IsLoopMemset = false);
179 
180  /// @}
181  /// \name Noncountable Loop Idiom Handling
182  /// @{
183 
184  bool runOnNoncountableLoop();
185 
186  bool recognizePopcount();
187  void transformLoopToPopcount(BasicBlock *PreCondBB, Instruction *CntInst,
188  PHINode *CntPhi, Value *Var);
189  bool recognizeAndInsertFFS(); /// Find First Set: ctlz or cttz
190  void transformLoopToCountable(Intrinsic::ID IntrinID, BasicBlock *PreCondBB,
191  Instruction *CntInst, PHINode *CntPhi,
192  Value *Var, Instruction *DefX,
193  const DebugLoc &DL, bool ZeroCheck,
194  bool IsCntPhiUsedOutsideLoop);
195 
196  /// @}
197 };
198 
199 class LoopIdiomRecognizeLegacyPass : public LoopPass {
200 public:
201  static char ID;
202 
203  explicit LoopIdiomRecognizeLegacyPass() : LoopPass(ID) {
206  }
207 
208  bool runOnLoop(Loop *L, LPPassManager &LPM) override {
209  if (skipLoop(L))
210  return false;
211 
212  AliasAnalysis *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
213  DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
214  LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
215  ScalarEvolution *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
216  TargetLibraryInfo *TLI =
217  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
218  const TargetTransformInfo *TTI =
219  &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
220  *L->getHeader()->getParent());
221  const DataLayout *DL = &L->getHeader()->getModule()->getDataLayout();
222 
223  LoopIdiomRecognize LIR(AA, DT, LI, SE, TLI, TTI, DL);
224  return LIR.runOnLoop(L);
225  }
226 
227  /// This transformation requires natural loop information & requires that
228  /// loop preheaders be inserted into the CFG.
229  void getAnalysisUsage(AnalysisUsage &AU) const override {
233  }
234 };
235 
236 } // end anonymous namespace
237 
239 
242  LPMUpdater &) {
243  const auto *DL = &L.getHeader()->getModule()->getDataLayout();
244 
245  LoopIdiomRecognize LIR(&AR.AA, &AR.DT, &AR.LI, &AR.SE, &AR.TLI, &AR.TTI, DL);
246  if (!LIR.runOnLoop(&L))
247  return PreservedAnalyses::all();
248 
250 }
251 
252 INITIALIZE_PASS_BEGIN(LoopIdiomRecognizeLegacyPass, "loop-idiom",
253  "Recognize loop idioms", false, false)
257 INITIALIZE_PASS_END(LoopIdiomRecognizeLegacyPass, "loop-idiom",
258  "Recognize loop idioms", false, false)
259 
260 Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognizeLegacyPass(); }
261 
264  I->eraseFromParent();
265 }
266 
267 //===----------------------------------------------------------------------===//
268 //
269 // Implementation of LoopIdiomRecognize
270 //
271 //===----------------------------------------------------------------------===//
272 
273 bool LoopIdiomRecognize::runOnLoop(Loop *L) {
274  CurLoop = L;
275  // If the loop could not be converted to canonical form, it must have an
276  // indirectbr in it, just give up.
277  if (!L->getLoopPreheader())
278  return false;
279 
280  // Disable loop idiom recognition if the function's name is a common idiom.
282  if (Name == "memset" || Name == "memcpy")
283  return false;
284 
285  // Determine if code size heuristics need to be applied.
286  ApplyCodeSizeHeuristics =
288 
289  HasMemset = TLI->has(LibFunc_memset);
290  HasMemsetPattern = TLI->has(LibFunc_memset_pattern16);
291  HasMemcpy = TLI->has(LibFunc_memcpy);
292 
293  if (HasMemset || HasMemsetPattern || HasMemcpy)
295  return runOnCountableLoop();
296 
297  return runOnNoncountableLoop();
298 }
299 
300 bool LoopIdiomRecognize::runOnCountableLoop() {
301  const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop);
302  assert(!isa<SCEVCouldNotCompute>(BECount) &&
303  "runOnCountableLoop() called on a loop without a predictable"
304  "backedge-taken count");
305 
306  // If this loop executes exactly one time, then it should be peeled, not
307  // optimized by this pass.
308  if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
309  if (BECst->getAPInt() == 0)
310  return false;
311 
312  SmallVector<BasicBlock *, 8> ExitBlocks;
313  CurLoop->getUniqueExitBlocks(ExitBlocks);
314 
315  LLVM_DEBUG(dbgs() << "loop-idiom Scanning: F["
316  << CurLoop->getHeader()->getParent()->getName()
317  << "] Loop %" << CurLoop->getHeader()->getName() << "\n");
318 
319  bool MadeChange = false;
320 
321  // The following transforms hoist stores/memsets into the loop pre-header.
322  // Give up if the loop has instructions may throw.
323  SimpleLoopSafetyInfo SafetyInfo;
324  SafetyInfo.computeLoopSafetyInfo(CurLoop);
325  if (SafetyInfo.anyBlockMayThrow())
326  return MadeChange;
327 
328  // Scan all the blocks in the loop that are not in subloops.
329  for (auto *BB : CurLoop->getBlocks()) {
330  // Ignore blocks in subloops.
331  if (LI->getLoopFor(BB) != CurLoop)
332  continue;
333 
334  MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks);
335  }
336  return MadeChange;
337 }
338 
339 static APInt getStoreStride(const SCEVAddRecExpr *StoreEv) {
340  const SCEVConstant *ConstStride = cast<SCEVConstant>(StoreEv->getOperand(1));
341  return ConstStride->getAPInt();
342 }
343 
344 /// getMemSetPatternValue - If a strided store of the specified value is safe to
345 /// turn into a memset_pattern16, return a ConstantArray of 16 bytes that should
346 /// be passed in. Otherwise, return null.
347 ///
348 /// Note that we don't ever attempt to use memset_pattern8 or 4, because these
349 /// just replicate their input array and then pass on to memset_pattern16.
351  // FIXME: This could check for UndefValue because it can be merged into any
352  // other valid pattern.
353 
354  // If the value isn't a constant, we can't promote it to being in a constant
355  // array. We could theoretically do a store to an alloca or something, but
356  // that doesn't seem worthwhile.
357  Constant *C = dyn_cast<Constant>(V);
358  if (!C)
359  return nullptr;
360 
361  // Only handle simple values that are a power of two bytes in size.
362  uint64_t Size = DL->getTypeSizeInBits(V->getType());
363  if (Size == 0 || (Size & 7) || (Size & (Size - 1)))
364  return nullptr;
365 
366  // Don't care enough about darwin/ppc to implement this.
367  if (DL->isBigEndian())
368  return nullptr;
369 
370  // Convert to size in bytes.
371  Size /= 8;
372 
373  // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see
374  // if the top and bottom are the same (e.g. for vectors and large integers).
375  if (Size > 16)
376  return nullptr;
377 
378  // If the constant is exactly 16 bytes, just use it.
379  if (Size == 16)
380  return C;
381 
382  // Otherwise, we'll use an array of the constants.
383  unsigned ArraySize = 16 / Size;
384  ArrayType *AT = ArrayType::get(V->getType(), ArraySize);
385  return ConstantArray::get(AT, std::vector<Constant *>(ArraySize, C));
386 }
387 
388 LoopIdiomRecognize::LegalStoreKind
389 LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
390  // Don't touch volatile stores.
391  if (SI->isVolatile())
392  return LegalStoreKind::None;
393  // We only want simple or unordered-atomic stores.
394  if (!SI->isUnordered())
395  return LegalStoreKind::None;
396 
397  // Don't convert stores of non-integral pointer types to memsets (which stores
398  // integers).
399  if (DL->isNonIntegralPointerType(SI->getValueOperand()->getType()))
400  return LegalStoreKind::None;
401 
402  // Avoid merging nontemporal stores.
404  return LegalStoreKind::None;
405 
406  Value *StoredVal = SI->getValueOperand();
407  Value *StorePtr = SI->getPointerOperand();
408 
409  // Reject stores that are so large that they overflow an unsigned.
410  uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
411  if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
412  return LegalStoreKind::None;
413 
414  // See if the pointer expression is an AddRec like {base,+,1} on the current
415  // loop, which indicates a strided store. If we have something else, it's a
416  // random store we can't handle.
417  const SCEVAddRecExpr *StoreEv =
418  dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
419  if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
420  return LegalStoreKind::None;
421 
422  // Check to see if we have a constant stride.
423  if (!isa<SCEVConstant>(StoreEv->getOperand(1)))
424  return LegalStoreKind::None;
425 
426  // See if the store can be turned into a memset.
427 
428  // If the stored value is a byte-wise value (like i32 -1), then it may be
429  // turned into a memset of i8 -1, assuming that all the consecutive bytes
430  // are stored. A store of i32 0x01020304 can never be turned into a memset,
431  // but it can be turned into memset_pattern if the target supports it.
432  Value *SplatValue = isBytewiseValue(StoredVal);
433  Constant *PatternValue = nullptr;
434 
435  // Note: memset and memset_pattern on unordered-atomic is yet not supported
436  bool UnorderedAtomic = SI->isUnordered() && !SI->isSimple();
437 
438  // If we're allowed to form a memset, and the stored value would be
439  // acceptable for memset, use it.
440  if (!UnorderedAtomic && HasMemset && SplatValue &&
441  // Verify that the stored value is loop invariant. If not, we can't
442  // promote the memset.
443  CurLoop->isLoopInvariant(SplatValue)) {
444  // It looks like we can use SplatValue.
445  return LegalStoreKind::Memset;
446  } else if (!UnorderedAtomic && HasMemsetPattern &&
447  // Don't create memset_pattern16s with address spaces.
448  StorePtr->getType()->getPointerAddressSpace() == 0 &&
449  (PatternValue = getMemSetPatternValue(StoredVal, DL))) {
450  // It looks like we can use PatternValue!
451  return LegalStoreKind::MemsetPattern;
452  }
453 
454  // Otherwise, see if the store can be turned into a memcpy.
455  if (HasMemcpy) {
456  // Check to see if the stride matches the size of the store. If so, then we
457  // know that every byte is touched in the loop.
458  APInt Stride = getStoreStride(StoreEv);
459  unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
460  if (StoreSize != Stride && StoreSize != -Stride)
461  return LegalStoreKind::None;
462 
463  // The store must be feeding a non-volatile load.
465 
466  // Only allow non-volatile loads
467  if (!LI || LI->isVolatile())
468  return LegalStoreKind::None;
469  // Only allow simple or unordered-atomic loads
470  if (!LI->isUnordered())
471  return LegalStoreKind::None;
472 
473  // See if the pointer expression is an AddRec like {base,+,1} on the current
474  // loop, which indicates a strided load. If we have something else, it's a
475  // random load we can't handle.
476  const SCEVAddRecExpr *LoadEv =
478  if (!LoadEv || LoadEv->getLoop() != CurLoop || !LoadEv->isAffine())
479  return LegalStoreKind::None;
480 
481  // The store and load must share the same stride.
482  if (StoreEv->getOperand(1) != LoadEv->getOperand(1))
483  return LegalStoreKind::None;
484 
485  // Success. This store can be converted into a memcpy.
486  UnorderedAtomic = UnorderedAtomic || LI->isAtomic();
487  return UnorderedAtomic ? LegalStoreKind::UnorderedAtomicMemcpy
488  : LegalStoreKind::Memcpy;
489  }
490  // This store can't be transformed into a memset/memcpy.
491  return LegalStoreKind::None;
492 }
493 
494 void LoopIdiomRecognize::collectStores(BasicBlock *BB) {
495  StoreRefsForMemset.clear();
496  StoreRefsForMemsetPattern.clear();
497  StoreRefsForMemcpy.clear();
498  for (Instruction &I : *BB) {
499  StoreInst *SI = dyn_cast<StoreInst>(&I);
500  if (!SI)
501  continue;
502 
503  // Make sure this is a strided store with a constant stride.
504  switch (isLegalStore(SI)) {
506  // Nothing to do
507  break;
508  case LegalStoreKind::Memset: {
509  // Find the base pointer.
510  Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), *DL);
511  StoreRefsForMemset[Ptr].push_back(SI);
512  } break;
513  case LegalStoreKind::MemsetPattern: {
514  // Find the base pointer.
515  Value *Ptr = GetUnderlyingObject(SI->getPointerOperand(), *DL);
516  StoreRefsForMemsetPattern[Ptr].push_back(SI);
517  } break;
518  case LegalStoreKind::Memcpy:
519  case LegalStoreKind::UnorderedAtomicMemcpy:
520  StoreRefsForMemcpy.push_back(SI);
521  break;
522  default:
523  assert(false && "unhandled return value");
524  break;
525  }
526  }
527 }
528 
529 /// runOnLoopBlock - Process the specified block, which lives in a counted loop
530 /// with the specified backedge count. This block is known to be in the current
531 /// loop and not in any subloops.
532 bool LoopIdiomRecognize::runOnLoopBlock(
533  BasicBlock *BB, const SCEV *BECount,
534  SmallVectorImpl<BasicBlock *> &ExitBlocks) {
535  // We can only promote stores in this block if they are unconditionally
536  // executed in the loop. For a block to be unconditionally executed, it has
537  // to dominate all the exit blocks of the loop. Verify this now.
538  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
539  if (!DT->dominates(BB, ExitBlocks[i]))
540  return false;
541 
542  bool MadeChange = false;
543  // Look for store instructions, which may be optimized to memset/memcpy.
544  collectStores(BB);
545 
546  // Look for a single store or sets of stores with a common base, which can be
547  // optimized into a memset (memset_pattern). The latter most commonly happens
548  // with structs and handunrolled loops.
549  for (auto &SL : StoreRefsForMemset)
550  MadeChange |= processLoopStores(SL.second, BECount, ForMemset::Yes);
551 
552  for (auto &SL : StoreRefsForMemsetPattern)
553  MadeChange |= processLoopStores(SL.second, BECount, ForMemset::No);
554 
555  // Optimize the store into a memcpy, if it feeds an similarly strided load.
556  for (auto &SI : StoreRefsForMemcpy)
557  MadeChange |= processLoopStoreOfLoopLoad(SI, BECount);
558 
559  for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
560  Instruction *Inst = &*I++;
561  // Look for memset instructions, which may be optimized to a larger memset.
562  if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) {
563  WeakTrackingVH InstPtr(&*I);
564  if (!processLoopMemSet(MSI, BECount))
565  continue;
566  MadeChange = true;
567 
568  // If processing the memset invalidated our iterator, start over from the
569  // top of the block.
570  if (!InstPtr)
571  I = BB->begin();
572  continue;
573  }
574  }
575 
576  return MadeChange;
577 }
578 
579 /// See if this store(s) can be promoted to a memset.
580 bool LoopIdiomRecognize::processLoopStores(SmallVectorImpl<StoreInst *> &SL,
581  const SCEV *BECount, ForMemset For) {
582  // Try to find consecutive stores that can be transformed into memsets.
583  SetVector<StoreInst *> Heads, Tails;
585 
586  // Do a quadratic search on all of the given stores and find
587  // all of the pairs of stores that follow each other.
588  SmallVector<unsigned, 16> IndexQueue;
589  for (unsigned i = 0, e = SL.size(); i < e; ++i) {
590  assert(SL[i]->isSimple() && "Expected only non-volatile stores.");
591 
592  Value *FirstStoredVal = SL[i]->getValueOperand();
593  Value *FirstStorePtr = SL[i]->getPointerOperand();
594  const SCEVAddRecExpr *FirstStoreEv =
595  cast<SCEVAddRecExpr>(SE->getSCEV(FirstStorePtr));
596  APInt FirstStride = getStoreStride(FirstStoreEv);
597  unsigned FirstStoreSize = DL->getTypeStoreSize(SL[i]->getValueOperand()->getType());
598 
599  // See if we can optimize just this store in isolation.
600  if (FirstStride == FirstStoreSize || -FirstStride == FirstStoreSize) {
601  Heads.insert(SL[i]);
602  continue;
603  }
604 
605  Value *FirstSplatValue = nullptr;
606  Constant *FirstPatternValue = nullptr;
607 
608  if (For == ForMemset::Yes)
609  FirstSplatValue = isBytewiseValue(FirstStoredVal);
610  else
611  FirstPatternValue = getMemSetPatternValue(FirstStoredVal, DL);
612 
613  assert((FirstSplatValue || FirstPatternValue) &&
614  "Expected either splat value or pattern value.");
615 
616  IndexQueue.clear();
617  // If a store has multiple consecutive store candidates, search Stores
618  // array according to the sequence: from i+1 to e, then from i-1 to 0.
619  // This is because usually pairing with immediate succeeding or preceding
620  // candidate create the best chance to find memset opportunity.
621  unsigned j = 0;
622  for (j = i + 1; j < e; ++j)
623  IndexQueue.push_back(j);
624  for (j = i; j > 0; --j)
625  IndexQueue.push_back(j - 1);
626 
627  for (auto &k : IndexQueue) {
628  assert(SL[k]->isSimple() && "Expected only non-volatile stores.");
629  Value *SecondStorePtr = SL[k]->getPointerOperand();
630  const SCEVAddRecExpr *SecondStoreEv =
631  cast<SCEVAddRecExpr>(SE->getSCEV(SecondStorePtr));
632  APInt SecondStride = getStoreStride(SecondStoreEv);
633 
634  if (FirstStride != SecondStride)
635  continue;
636 
637  Value *SecondStoredVal = SL[k]->getValueOperand();
638  Value *SecondSplatValue = nullptr;
639  Constant *SecondPatternValue = nullptr;
640 
641  if (For == ForMemset::Yes)
642  SecondSplatValue = isBytewiseValue(SecondStoredVal);
643  else
644  SecondPatternValue = getMemSetPatternValue(SecondStoredVal, DL);
645 
646  assert((SecondSplatValue || SecondPatternValue) &&
647  "Expected either splat value or pattern value.");
648 
649  if (isConsecutiveAccess(SL[i], SL[k], *DL, *SE, false)) {
650  if (For == ForMemset::Yes) {
651  if (isa<UndefValue>(FirstSplatValue))
652  FirstSplatValue = SecondSplatValue;
653  if (FirstSplatValue != SecondSplatValue)
654  continue;
655  } else {
656  if (isa<UndefValue>(FirstPatternValue))
657  FirstPatternValue = SecondPatternValue;
658  if (FirstPatternValue != SecondPatternValue)
659  continue;
660  }
661  Tails.insert(SL[k]);
662  Heads.insert(SL[i]);
663  ConsecutiveChain[SL[i]] = SL[k];
664  break;
665  }
666  }
667  }
668 
669  // We may run into multiple chains that merge into a single chain. We mark the
670  // stores that we transformed so that we don't visit the same store twice.
671  SmallPtrSet<Value *, 16> TransformedStores;
672  bool Changed = false;
673 
674  // For stores that start but don't end a link in the chain:
675  for (SetVector<StoreInst *>::iterator it = Heads.begin(), e = Heads.end();
676  it != e; ++it) {
677  if (Tails.count(*it))
678  continue;
679 
680  // We found a store instr that starts a chain. Now follow the chain and try
681  // to transform it.
682  SmallPtrSet<Instruction *, 8> AdjacentStores;
683  StoreInst *I = *it;
684 
685  StoreInst *HeadStore = I;
686  unsigned StoreSize = 0;
687 
688  // Collect the chain into a list.
689  while (Tails.count(I) || Heads.count(I)) {
690  if (TransformedStores.count(I))
691  break;
692  AdjacentStores.insert(I);
693 
694  StoreSize += DL->getTypeStoreSize(I->getValueOperand()->getType());
695  // Move to the next value in the chain.
696  I = ConsecutiveChain[I];
697  }
698 
699  Value *StoredVal = HeadStore->getValueOperand();
700  Value *StorePtr = HeadStore->getPointerOperand();
701  const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
702  APInt Stride = getStoreStride(StoreEv);
703 
704  // Check to see if the stride matches the size of the stores. If so, then
705  // we know that every byte is touched in the loop.
706  if (StoreSize != Stride && StoreSize != -Stride)
707  continue;
708 
709  bool NegStride = StoreSize == -Stride;
710 
711  if (processLoopStridedStore(StorePtr, StoreSize, HeadStore->getAlignment(),
712  StoredVal, HeadStore, AdjacentStores, StoreEv,
713  BECount, NegStride)) {
714  TransformedStores.insert(AdjacentStores.begin(), AdjacentStores.end());
715  Changed = true;
716  }
717  }
718 
719  return Changed;
720 }
721 
722 /// processLoopMemSet - See if this memset can be promoted to a large memset.
723 bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
724  const SCEV *BECount) {
725  // We can only handle non-volatile memsets with a constant size.
726  if (MSI->isVolatile() || !isa<ConstantInt>(MSI->getLength()))
727  return false;
728 
729  // If we're not allowed to hack on memset, we fail.
730  if (!HasMemset)
731  return false;
732 
733  Value *Pointer = MSI->getDest();
734 
735  // See if the pointer expression is an AddRec like {base,+,1} on the current
736  // loop, which indicates a strided store. If we have something else, it's a
737  // random store we can't handle.
738  const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer));
739  if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine())
740  return false;
741 
742  // Reject memsets that are so large that they overflow an unsigned.
743  uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
744  if ((SizeInBytes >> 32) != 0)
745  return false;
746 
747  // Check to see if the stride matches the size of the memset. If so, then we
748  // know that every byte is touched in the loop.
749  const SCEVConstant *ConstStride = dyn_cast<SCEVConstant>(Ev->getOperand(1));
750  if (!ConstStride)
751  return false;
752 
753  APInt Stride = ConstStride->getAPInt();
754  if (SizeInBytes != Stride && SizeInBytes != -Stride)
755  return false;
756 
757  // Verify that the memset value is loop invariant. If not, we can't promote
758  // the memset.
759  Value *SplatValue = MSI->getValue();
760  if (!SplatValue || !CurLoop->isLoopInvariant(SplatValue))
761  return false;
762 
764  MSIs.insert(MSI);
765  bool NegStride = SizeInBytes == -Stride;
766  return processLoopStridedStore(Pointer, (unsigned)SizeInBytes,
767  MSI->getDestAlignment(), SplatValue, MSI, MSIs,
768  Ev, BECount, NegStride, /*IsLoopMemset=*/true);
769 }
770 
771 /// mayLoopAccessLocation - Return true if the specified loop might access the
772 /// specified pointer location, which is a loop-strided access. The 'Access'
773 /// argument specifies what the verboten forms of access are (read or write).
774 static bool
776  const SCEV *BECount, unsigned StoreSize,
777  AliasAnalysis &AA,
778  SmallPtrSetImpl<Instruction *> &IgnoredStores) {
779  // Get the location that may be stored across the loop. Since the access is
780  // strided positively through memory, we say that the modified location starts
781  // at the pointer and has infinite size.
782  LocationSize AccessSize = LocationSize::unknown();
783 
784  // If the loop iterates a fixed number of times, we can refine the access size
785  // to be exactly the size of the memset, which is (BECount+1)*StoreSize
786  if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
787  AccessSize = LocationSize::precise((BECst->getValue()->getZExtValue() + 1) *
788  StoreSize);
789 
790  // TODO: For this to be really effective, we have to dive into the pointer
791  // operand in the store. Store to &A[i] of 100 will always return may alias
792  // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
793  // which will then no-alias a store to &A[100].
794  MemoryLocation StoreLoc(Ptr, AccessSize);
795 
796  for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E;
797  ++BI)
798  for (Instruction &I : **BI)
799  if (IgnoredStores.count(&I) == 0 &&
801  intersectModRef(AA.getModRefInfo(&I, StoreLoc), Access)))
802  return true;
803 
804  return false;
805 }
806 
807 // If we have a negative stride, Start refers to the end of the memory location
808 // we're trying to memset. Therefore, we need to recompute the base pointer,
809 // which is just Start - BECount*Size.
810 static const SCEV *getStartForNegStride(const SCEV *Start, const SCEV *BECount,
811  Type *IntPtr, unsigned StoreSize,
812  ScalarEvolution *SE) {
813  const SCEV *Index = SE->getTruncateOrZeroExtend(BECount, IntPtr);
814  if (StoreSize != 1)
815  Index = SE->getMulExpr(Index, SE->getConstant(IntPtr, StoreSize),
816  SCEV::FlagNUW);
817  return SE->getMinusSCEV(Start, Index);
818 }
819 
820 /// Compute the number of bytes as a SCEV from the backedge taken count.
821 ///
822 /// This also maps the SCEV into the provided type and tries to handle the
823 /// computation in a way that will fold cleanly.
824 static const SCEV *getNumBytes(const SCEV *BECount, Type *IntPtr,
825  unsigned StoreSize, Loop *CurLoop,
826  const DataLayout *DL, ScalarEvolution *SE) {
827  const SCEV *NumBytesS;
828  // The # stored bytes is (BECount+1)*Size. Expand the trip count out to
829  // pointer size if it isn't already.
830  //
831  // If we're going to need to zero extend the BE count, check if we can add
832  // one to it prior to zero extending without overflow. Provided this is safe,
833  // it allows better simplification of the +1.
834  if (DL->getTypeSizeInBits(BECount->getType()) <
835  DL->getTypeSizeInBits(IntPtr) &&
837  CurLoop, ICmpInst::ICMP_NE, BECount,
838  SE->getNegativeSCEV(SE->getOne(BECount->getType())))) {
839  NumBytesS = SE->getZeroExtendExpr(
840  SE->getAddExpr(BECount, SE->getOne(BECount->getType()), SCEV::FlagNUW),
841  IntPtr);
842  } else {
843  NumBytesS = SE->getAddExpr(SE->getTruncateOrZeroExtend(BECount, IntPtr),
844  SE->getOne(IntPtr), SCEV::FlagNUW);
845  }
846 
847  // And scale it based on the store size.
848  if (StoreSize != 1) {
849  NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize),
850  SCEV::FlagNUW);
851  }
852  return NumBytesS;
853 }
854 
855 /// processLoopStridedStore - We see a strided store of some value. If we can
856 /// transform this into a memset or memset_pattern in the loop preheader, do so.
857 bool LoopIdiomRecognize::processLoopStridedStore(
858  Value *DestPtr, unsigned StoreSize, unsigned StoreAlignment,
859  Value *StoredVal, Instruction *TheStore,
861  const SCEV *BECount, bool NegStride, bool IsLoopMemset) {
862  Value *SplatValue = isBytewiseValue(StoredVal);
863  Constant *PatternValue = nullptr;
864 
865  if (!SplatValue)
866  PatternValue = getMemSetPatternValue(StoredVal, DL);
867 
868  assert((SplatValue || PatternValue) &&
869  "Expected either splat value or pattern value.");
870 
871  // The trip count of the loop and the base pointer of the addrec SCEV is
872  // guaranteed to be loop invariant, which means that it should dominate the
873  // header. This allows us to insert code for it in the preheader.
874  unsigned DestAS = DestPtr->getType()->getPointerAddressSpace();
875  BasicBlock *Preheader = CurLoop->getLoopPreheader();
876  IRBuilder<> Builder(Preheader->getTerminator());
877  SCEVExpander Expander(*SE, *DL, "loop-idiom");
878 
879  Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS);
880  Type *IntPtr = Builder.getIntPtrTy(*DL, DestAS);
881 
882  const SCEV *Start = Ev->getStart();
883  // Handle negative strided loops.
884  if (NegStride)
885  Start = getStartForNegStride(Start, BECount, IntPtr, StoreSize, SE);
886 
887  // TODO: ideally we should still be able to generate memset if SCEV expander
888  // is taught to generate the dependencies at the latest point.
889  if (!isSafeToExpand(Start, *SE))
890  return false;
891 
892  // Okay, we have a strided store "p[i]" of a splattable value. We can turn
893  // this into a memset in the loop preheader now if we want. However, this
894  // would be unsafe to do if there is anything else in the loop that may read
895  // or write to the aliased location. Check for any overlap by generating the
896  // base pointer and checking the region.
897  Value *BasePtr =
898  Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->getTerminator());
899  if (mayLoopAccessLocation(BasePtr, ModRefInfo::ModRef, CurLoop, BECount,
900  StoreSize, *AA, Stores)) {
901  Expander.clear();
902  // If we generated new code for the base pointer, clean up.
904  return false;
905  }
906 
907  if (avoidLIRForMultiBlockLoop(/*IsMemset=*/true, IsLoopMemset))
908  return false;
909 
910  // Okay, everything looks good, insert the memset.
911 
912  const SCEV *NumBytesS =
913  getNumBytes(BECount, IntPtr, StoreSize, CurLoop, DL, SE);
914 
915  // TODO: ideally we should still be able to generate memset if SCEV expander
916  // is taught to generate the dependencies at the latest point.
917  if (!isSafeToExpand(NumBytesS, *SE))
918  return false;
919 
920  Value *NumBytes =
921  Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator());
922 
923  CallInst *NewCall;
924  if (SplatValue) {
925  NewCall =
926  Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, StoreAlignment);
927  } else {
928  // Everything is emitted in default address space
929  Type *Int8PtrTy = DestInt8PtrTy;
930 
931  Module *M = TheStore->getModule();
932  StringRef FuncName = "memset_pattern16";
933  Value *MSP =
934  M->getOrInsertFunction(FuncName, Builder.getVoidTy(),
935  Int8PtrTy, Int8PtrTy, IntPtr);
936  inferLibFuncAttributes(M, FuncName, *TLI);
937 
938  // Otherwise we should form a memset_pattern16. PatternValue is known to be
939  // an constant array of 16-bytes. Plop the value into a mergable global.
940  GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true,
942  PatternValue, ".memset_pattern");
943  GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); // Ok to merge these.
944  GV->setAlignment(16);
945  Value *PatternPtr = ConstantExpr::getBitCast(GV, Int8PtrTy);
946  NewCall = Builder.CreateCall(MSP, {BasePtr, PatternPtr, NumBytes});
947  }
948 
949  LLVM_DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n"
950  << " from store to: " << *Ev << " at: " << *TheStore
951  << "\n");
952  NewCall->setDebugLoc(TheStore->getDebugLoc());
953 
954  // Okay, the memset has been formed. Zap the original store and anything that
955  // feeds into it.
956  for (auto *I : Stores)
958  ++NumMemSet;
959  return true;
960 }
961 
962 /// If the stored value is a strided load in the same loop with the same stride
963 /// this may be transformable into a memcpy. This kicks in for stuff like
964 /// for (i) A[i] = B[i];
965 bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
966  const SCEV *BECount) {
967  assert(SI->isUnordered() && "Expected only non-volatile non-ordered stores.");
968 
969  Value *StorePtr = SI->getPointerOperand();
970  const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
971  APInt Stride = getStoreStride(StoreEv);
972  unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
973  bool NegStride = StoreSize == -Stride;
974 
975  // The store must be feeding a non-volatile load.
976  LoadInst *LI = cast<LoadInst>(SI->getValueOperand());
977  assert(LI->isUnordered() && "Expected only non-volatile non-ordered loads.");
978 
979  // See if the pointer expression is an AddRec like {base,+,1} on the current
980  // loop, which indicates a strided load. If we have something else, it's a
981  // random load we can't handle.
982  const SCEVAddRecExpr *LoadEv =
983  cast<SCEVAddRecExpr>(SE->getSCEV(LI->getPointerOperand()));
984 
985  // The trip count of the loop and the base pointer of the addrec SCEV is
986  // guaranteed to be loop invariant, which means that it should dominate the
987  // header. This allows us to insert code for it in the preheader.
988  BasicBlock *Preheader = CurLoop->getLoopPreheader();
989  IRBuilder<> Builder(Preheader->getTerminator());
990  SCEVExpander Expander(*SE, *DL, "loop-idiom");
991 
992  const SCEV *StrStart = StoreEv->getStart();
993  unsigned StrAS = SI->getPointerAddressSpace();
994  Type *IntPtrTy = Builder.getIntPtrTy(*DL, StrAS);
995 
996  // Handle negative strided loops.
997  if (NegStride)
998  StrStart = getStartForNegStride(StrStart, BECount, IntPtrTy, StoreSize, SE);
999 
1000  // Okay, we have a strided store "p[i]" of a loaded value. We can turn
1001  // this into a memcpy in the loop preheader now if we want. However, this
1002  // would be unsafe to do if there is anything else in the loop that may read
1003  // or write the memory region we're storing to. This includes the load that
1004  // feeds the stores. Check for an alias by generating the base address and
1005  // checking everything.
1006  Value *StoreBasePtr = Expander.expandCodeFor(
1007  StrStart, Builder.getInt8PtrTy(StrAS), Preheader->getTerminator());
1008 
1010  Stores.insert(SI);
1011  if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount,
1012  StoreSize, *AA, Stores)) {
1013  Expander.clear();
1014  // If we generated new code for the base pointer, clean up.
1015  RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
1016  return false;
1017  }
1018 
1019  const SCEV *LdStart = LoadEv->getStart();
1020  unsigned LdAS = LI->getPointerAddressSpace();
1021 
1022  // Handle negative strided loops.
1023  if (NegStride)
1024  LdStart = getStartForNegStride(LdStart, BECount, IntPtrTy, StoreSize, SE);
1025 
1026  // For a memcpy, we have to make sure that the input array is not being
1027  // mutated by the loop.
1028  Value *LoadBasePtr = Expander.expandCodeFor(
1029  LdStart, Builder.getInt8PtrTy(LdAS), Preheader->getTerminator());
1030 
1031  if (mayLoopAccessLocation(LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount,
1032  StoreSize, *AA, Stores)) {
1033  Expander.clear();
1034  // If we generated new code for the base pointer, clean up.
1036  RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
1037  return false;
1038  }
1039 
1040  if (avoidLIRForMultiBlockLoop())
1041  return false;
1042 
1043  // Okay, everything is safe, we can transform this!
1044 
1045  const SCEV *NumBytesS =
1046  getNumBytes(BECount, IntPtrTy, StoreSize, CurLoop, DL, SE);
1047 
1048  Value *NumBytes =
1049  Expander.expandCodeFor(NumBytesS, IntPtrTy, Preheader->getTerminator());
1050 
1051  CallInst *NewCall = nullptr;
1052  // Check whether to generate an unordered atomic memcpy:
1053  // If the load or store are atomic, then they must necessarily be unordered
1054  // by previous checks.
1055  if (!SI->isAtomic() && !LI->isAtomic())
1056  NewCall = Builder.CreateMemCpy(StoreBasePtr, SI->getAlignment(),
1057  LoadBasePtr, LI->getAlignment(), NumBytes);
1058  else {
1059  // We cannot allow unaligned ops for unordered load/store, so reject
1060  // anything where the alignment isn't at least the element size.
1061  unsigned Align = std::min(SI->getAlignment(), LI->getAlignment());
1062  if (Align < StoreSize)
1063  return false;
1064 
1065  // If the element.atomic memcpy is not lowered into explicit
1066  // loads/stores later, then it will be lowered into an element-size
1067  // specific lib call. If the lib call doesn't exist for our store size, then
1068  // we shouldn't generate the memcpy.
1069  if (StoreSize > TTI->getAtomicMemIntrinsicMaxElementSize())
1070  return false;
1071 
1072  // Create the call.
1073  // Note that unordered atomic loads/stores are *required* by the spec to
1074  // have an alignment but non-atomic loads/stores may not.
1075  NewCall = Builder.CreateElementUnorderedAtomicMemCpy(
1076  StoreBasePtr, SI->getAlignment(), LoadBasePtr, LI->getAlignment(),
1077  NumBytes, StoreSize);
1078  }
1079  NewCall->setDebugLoc(SI->getDebugLoc());
1080 
1081  LLVM_DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n"
1082  << " from load ptr=" << *LoadEv << " at: " << *LI << "\n"
1083  << " from store ptr=" << *StoreEv << " at: " << *SI
1084  << "\n");
1085 
1086  // Okay, the memcpy has been formed. Zap the original store and anything that
1087  // feeds into it.
1089  ++NumMemCpy;
1090  return true;
1091 }
1092 
1093 // When compiling for codesize we avoid idiom recognition for a multi-block loop
1094 // unless it is a loop_memset idiom or a memset/memcpy idiom in a nested loop.
1095 //
1096 bool LoopIdiomRecognize::avoidLIRForMultiBlockLoop(bool IsMemset,
1097  bool IsLoopMemset) {
1098  if (ApplyCodeSizeHeuristics && CurLoop->getNumBlocks() > 1) {
1099  if (!CurLoop->getParentLoop() && (!IsMemset || !IsLoopMemset)) {
1100  LLVM_DEBUG(dbgs() << " " << CurLoop->getHeader()->getParent()->getName()
1101  << " : LIR " << (IsMemset ? "Memset" : "Memcpy")
1102  << " avoided: multi-block top-level loop\n");
1103  return true;
1104  }
1105  }
1106 
1107  return false;
1108 }
1109 
1110 bool LoopIdiomRecognize::runOnNoncountableLoop() {
1111  return recognizePopcount() || recognizeAndInsertFFS();
1112 }
1113 
1114 /// Check if the given conditional branch is based on the comparison between
1115 /// a variable and zero, and if the variable is non-zero or zero (JmpOnZero is
1116 /// true), the control yields to the loop entry. If the branch matches the
1117 /// behavior, the variable involved in the comparison is returned. This function
1118 /// will be called to see if the precondition and postcondition of the loop are
1119 /// in desirable form.
1120 static Value *matchCondition(BranchInst *BI, BasicBlock *LoopEntry,
1121  bool JmpOnZero = false) {
1122  if (!BI || !BI->isConditional())
1123  return nullptr;
1124 
1125  ICmpInst *Cond = dyn_cast<ICmpInst>(BI->getCondition());
1126  if (!Cond)
1127  return nullptr;
1128 
1129  ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
1130  if (!CmpZero || !CmpZero->isZero())
1131  return nullptr;
1132 
1133  BasicBlock *TrueSucc = BI->getSuccessor(0);
1134  BasicBlock *FalseSucc = BI->getSuccessor(1);
1135  if (JmpOnZero)
1136  std::swap(TrueSucc, FalseSucc);
1137 
1138  ICmpInst::Predicate Pred = Cond->getPredicate();
1139  if ((Pred == ICmpInst::ICMP_NE && TrueSucc == LoopEntry) ||
1140  (Pred == ICmpInst::ICMP_EQ && FalseSucc == LoopEntry))
1141  return Cond->getOperand(0);
1142 
1143  return nullptr;
1144 }
1145 
1146 // Check if the recurrence variable `VarX` is in the right form to create
1147 // the idiom. Returns the value coerced to a PHINode if so.
1149  BasicBlock *LoopEntry) {
1150  auto *PhiX = dyn_cast<PHINode>(VarX);
1151  if (PhiX && PhiX->getParent() == LoopEntry &&
1152  (PhiX->getOperand(0) == DefX || PhiX->getOperand(1) == DefX))
1153  return PhiX;
1154  return nullptr;
1155 }
1156 
1157 /// Return true iff the idiom is detected in the loop.
1158 ///
1159 /// Additionally:
1160 /// 1) \p CntInst is set to the instruction counting the population bit.
1161 /// 2) \p CntPhi is set to the corresponding phi node.
1162 /// 3) \p Var is set to the value whose population bits are being counted.
1163 ///
1164 /// The core idiom we are trying to detect is:
1165 /// \code
1166 /// if (x0 != 0)
1167 /// goto loop-exit // the precondition of the loop
1168 /// cnt0 = init-val;
1169 /// do {
1170 /// x1 = phi (x0, x2);
1171 /// cnt1 = phi(cnt0, cnt2);
1172 ///
1173 /// cnt2 = cnt1 + 1;
1174 /// ...
1175 /// x2 = x1 & (x1 - 1);
1176 /// ...
1177 /// } while(x != 0);
1178 ///
1179 /// loop-exit:
1180 /// \endcode
1181 static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB,
1182  Instruction *&CntInst, PHINode *&CntPhi,
1183  Value *&Var) {
1184  // step 1: Check to see if the look-back branch match this pattern:
1185  // "if (a!=0) goto loop-entry".
1186  BasicBlock *LoopEntry;
1187  Instruction *DefX2, *CountInst;
1188  Value *VarX1, *VarX0;
1189  PHINode *PhiX, *CountPhi;
1190 
1191  DefX2 = CountInst = nullptr;
1192  VarX1 = VarX0 = nullptr;
1193  PhiX = CountPhi = nullptr;
1194  LoopEntry = *(CurLoop->block_begin());
1195 
1196  // step 1: Check if the loop-back branch is in desirable form.
1197  {
1198  if (Value *T = matchCondition(
1199  dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry))
1200  DefX2 = dyn_cast<Instruction>(T);
1201  else
1202  return false;
1203  }
1204 
1205  // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)"
1206  {
1207  if (!DefX2 || DefX2->getOpcode() != Instruction::And)
1208  return false;
1209 
1210  BinaryOperator *SubOneOp;
1211 
1212  if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0))))
1213  VarX1 = DefX2->getOperand(1);
1214  else {
1215  VarX1 = DefX2->getOperand(0);
1216  SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1));
1217  }
1218  if (!SubOneOp || SubOneOp->getOperand(0) != VarX1)
1219  return false;
1220 
1221  ConstantInt *Dec = dyn_cast<ConstantInt>(SubOneOp->getOperand(1));
1222  if (!Dec ||
1223  !((SubOneOp->getOpcode() == Instruction::Sub && Dec->isOne()) ||
1224  (SubOneOp->getOpcode() == Instruction::Add &&
1225  Dec->isMinusOne()))) {
1226  return false;
1227  }
1228  }
1229 
1230  // step 3: Check the recurrence of variable X
1231  PhiX = getRecurrenceVar(VarX1, DefX2, LoopEntry);
1232  if (!PhiX)
1233  return false;
1234 
1235  // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1
1236  {
1237  CountInst = nullptr;
1238  for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(),
1239  IterE = LoopEntry->end();
1240  Iter != IterE; Iter++) {
1241  Instruction *Inst = &*Iter;
1242  if (Inst->getOpcode() != Instruction::Add)
1243  continue;
1244 
1245  ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
1246  if (!Inc || !Inc->isOne())
1247  continue;
1248 
1249  PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry);
1250  if (!Phi)
1251  continue;
1252 
1253  // Check if the result of the instruction is live of the loop.
1254  bool LiveOutLoop = false;
1255  for (User *U : Inst->users()) {
1256  if ((cast<Instruction>(U))->getParent() != LoopEntry) {
1257  LiveOutLoop = true;
1258  break;
1259  }
1260  }
1261 
1262  if (LiveOutLoop) {
1263  CountInst = Inst;
1264  CountPhi = Phi;
1265  break;
1266  }
1267  }
1268 
1269  if (!CountInst)
1270  return false;
1271  }
1272 
1273  // step 5: check if the precondition is in this form:
1274  // "if (x != 0) goto loop-head ; else goto somewhere-we-don't-care;"
1275  {
1276  auto *PreCondBr = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1277  Value *T = matchCondition(PreCondBr, CurLoop->getLoopPreheader());
1278  if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1))
1279  return false;
1280 
1281  CntInst = CountInst;
1282  CntPhi = CountPhi;
1283  Var = T;
1284  }
1285 
1286  return true;
1287 }
1288 
1289 /// Return true if the idiom is detected in the loop.
1290 ///
1291 /// Additionally:
1292 /// 1) \p CntInst is set to the instruction Counting Leading Zeros (CTLZ)
1293 /// or nullptr if there is no such.
1294 /// 2) \p CntPhi is set to the corresponding phi node
1295 /// or nullptr if there is no such.
1296 /// 3) \p Var is set to the value whose CTLZ could be used.
1297 /// 4) \p DefX is set to the instruction calculating Loop exit condition.
1298 ///
1299 /// The core idiom we are trying to detect is:
1300 /// \code
1301 /// if (x0 == 0)
1302 /// goto loop-exit // the precondition of the loop
1303 /// cnt0 = init-val;
1304 /// do {
1305 /// x = phi (x0, x.next); //PhiX
1306 /// cnt = phi(cnt0, cnt.next);
1307 ///
1308 /// cnt.next = cnt + 1;
1309 /// ...
1310 /// x.next = x >> 1; // DefX
1311 /// ...
1312 /// } while(x.next != 0);
1313 ///
1314 /// loop-exit:
1315 /// \endcode
1316 static bool detectShiftUntilZeroIdiom(Loop *CurLoop, const DataLayout &DL,
1317  Intrinsic::ID &IntrinID, Value *&InitX,
1318  Instruction *&CntInst, PHINode *&CntPhi,
1319  Instruction *&DefX) {
1320  BasicBlock *LoopEntry;
1321  Value *VarX = nullptr;
1322 
1323  DefX = nullptr;
1324  CntInst = nullptr;
1325  CntPhi = nullptr;
1326  LoopEntry = *(CurLoop->block_begin());
1327 
1328  // step 1: Check if the loop-back branch is in desirable form.
1329  if (Value *T = matchCondition(
1330  dyn_cast<BranchInst>(LoopEntry->getTerminator()), LoopEntry))
1331  DefX = dyn_cast<Instruction>(T);
1332  else
1333  return false;
1334 
1335  // step 2: detect instructions corresponding to "x.next = x >> 1 or x << 1"
1336  if (!DefX || !DefX->isShift())
1337  return false;
1338  IntrinID = DefX->getOpcode() == Instruction::Shl ? Intrinsic::cttz :
1339  Intrinsic::ctlz;
1340  ConstantInt *Shft = dyn_cast<ConstantInt>(DefX->getOperand(1));
1341  if (!Shft || !Shft->isOne())
1342  return false;
1343  VarX = DefX->getOperand(0);
1344 
1345  // step 3: Check the recurrence of variable X
1346  PHINode *PhiX = getRecurrenceVar(VarX, DefX, LoopEntry);
1347  if (!PhiX)
1348  return false;
1349 
1350  InitX = PhiX->getIncomingValueForBlock(CurLoop->getLoopPreheader());
1351 
1352  // Make sure the initial value can't be negative otherwise the ashr in the
1353  // loop might never reach zero which would make the loop infinite.
1354  if (DefX->getOpcode() == Instruction::AShr && !isKnownNonNegative(InitX, DL))
1355  return false;
1356 
1357  // step 4: Find the instruction which count the CTLZ: cnt.next = cnt + 1
1358  // TODO: We can skip the step. If loop trip count is known (CTLZ),
1359  // then all uses of "cnt.next" could be optimized to the trip count
1360  // plus "cnt0". Currently it is not optimized.
1361  // This step could be used to detect POPCNT instruction:
1362  // cnt.next = cnt + (x.next & 1)
1363  for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI()->getIterator(),
1364  IterE = LoopEntry->end();
1365  Iter != IterE; Iter++) {
1366  Instruction *Inst = &*Iter;
1367  if (Inst->getOpcode() != Instruction::Add)
1368  continue;
1369 
1370  ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
1371  if (!Inc || !Inc->isOne())
1372  continue;
1373 
1374  PHINode *Phi = getRecurrenceVar(Inst->getOperand(0), Inst, LoopEntry);
1375  if (!Phi)
1376  continue;
1377 
1378  CntInst = Inst;
1379  CntPhi = Phi;
1380  break;
1381  }
1382  if (!CntInst)
1383  return false;
1384 
1385  return true;
1386 }
1387 
1388 /// Recognize CTLZ or CTTZ idiom in a non-countable loop and convert the loop
1389 /// to countable (with CTLZ / CTTZ trip count). If CTLZ / CTTZ inserted as a new
1390 /// trip count returns true; otherwise, returns false.
1391 bool LoopIdiomRecognize::recognizeAndInsertFFS() {
1392  // Give up if the loop has multiple blocks or multiple backedges.
1393  if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
1394  return false;
1395 
1396  Intrinsic::ID IntrinID;
1397  Value *InitX;
1398  Instruction *DefX = nullptr;
1399  PHINode *CntPhi = nullptr;
1400  Instruction *CntInst = nullptr;
1401  // Help decide if transformation is profitable. For ShiftUntilZero idiom,
1402  // this is always 6.
1403  size_t IdiomCanonicalSize = 6;
1404 
1405  if (!detectShiftUntilZeroIdiom(CurLoop, *DL, IntrinID, InitX,
1406  CntInst, CntPhi, DefX))
1407  return false;
1408 
1409  bool IsCntPhiUsedOutsideLoop = false;
1410  for (User *U : CntPhi->users())
1411  if (!CurLoop->contains(cast<Instruction>(U))) {
1412  IsCntPhiUsedOutsideLoop = true;
1413  break;
1414  }
1415  bool IsCntInstUsedOutsideLoop = false;
1416  for (User *U : CntInst->users())
1417  if (!CurLoop->contains(cast<Instruction>(U))) {
1418  IsCntInstUsedOutsideLoop = true;
1419  break;
1420  }
1421  // If both CntInst and CntPhi are used outside the loop the profitability
1422  // is questionable.
1423  if (IsCntInstUsedOutsideLoop && IsCntPhiUsedOutsideLoop)
1424  return false;
1425 
1426  // For some CPUs result of CTLZ(X) intrinsic is undefined
1427  // when X is 0. If we can not guarantee X != 0, we need to check this
1428  // when expand.
1429  bool ZeroCheck = false;
1430  // It is safe to assume Preheader exist as it was checked in
1431  // parent function RunOnLoop.
1432  BasicBlock *PH = CurLoop->getLoopPreheader();
1433 
1434  // If we are using the count instruction outside the loop, make sure we
1435  // have a zero check as a precondition. Without the check the loop would run
1436  // one iteration for before any check of the input value. This means 0 and 1
1437  // would have identical behavior in the original loop and thus
1438  if (!IsCntPhiUsedOutsideLoop) {
1439  auto *PreCondBB = PH->getSinglePredecessor();
1440  if (!PreCondBB)
1441  return false;
1442  auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1443  if (!PreCondBI)
1444  return false;
1445  if (matchCondition(PreCondBI, PH) != InitX)
1446  return false;
1447  ZeroCheck = true;
1448  }
1449 
1450  // Check if CTLZ / CTTZ intrinsic is profitable. Assume it is always
1451  // profitable if we delete the loop.
1452 
1453  // the loop has only 6 instructions:
1454  // %n.addr.0 = phi [ %n, %entry ], [ %shr, %while.cond ]
1455  // %i.0 = phi [ %i0, %entry ], [ %inc, %while.cond ]
1456  // %shr = ashr %n.addr.0, 1
1457  // %tobool = icmp eq %shr, 0
1458  // %inc = add nsw %i.0, 1
1459  // br i1 %tobool
1460 
1461  const Value *Args[] =
1462  {InitX, ZeroCheck ? ConstantInt::getTrue(InitX->getContext())
1463  : ConstantInt::getFalse(InitX->getContext())};
1464  if (CurLoop->getHeader()->size() != IdiomCanonicalSize &&
1465  TTI->getIntrinsicCost(IntrinID, InitX->getType(), Args) >
1467  return false;
1468 
1469  transformLoopToCountable(IntrinID, PH, CntInst, CntPhi, InitX, DefX,
1470  DefX->getDebugLoc(), ZeroCheck,
1471  IsCntPhiUsedOutsideLoop);
1472  return true;
1473 }
1474 
1475 /// Recognizes a population count idiom in a non-countable loop.
1476 ///
1477 /// If detected, transforms the relevant code to issue the popcount intrinsic
1478 /// function call, and returns true; otherwise, returns false.
1479 bool LoopIdiomRecognize::recognizePopcount() {
1481  return false;
1482 
1483  // Counting population are usually conducted by few arithmetic instructions.
1484  // Such instructions can be easily "absorbed" by vacant slots in a
1485  // non-compact loop. Therefore, recognizing popcount idiom only makes sense
1486  // in a compact loop.
1487 
1488  // Give up if the loop has multiple blocks or multiple backedges.
1489  if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
1490  return false;
1491 
1492  BasicBlock *LoopBody = *(CurLoop->block_begin());
1493  if (LoopBody->size() >= 20) {
1494  // The loop is too big, bail out.
1495  return false;
1496  }
1497 
1498  // It should have a preheader containing nothing but an unconditional branch.
1499  BasicBlock *PH = CurLoop->getLoopPreheader();
1500  if (!PH || &PH->front() != PH->getTerminator())
1501  return false;
1502  auto *EntryBI = dyn_cast<BranchInst>(PH->getTerminator());
1503  if (!EntryBI || EntryBI->isConditional())
1504  return false;
1505 
1506  // It should have a precondition block where the generated popcount intrinsic
1507  // function can be inserted.
1508  auto *PreCondBB = PH->getSinglePredecessor();
1509  if (!PreCondBB)
1510  return false;
1511  auto *PreCondBI = dyn_cast<BranchInst>(PreCondBB->getTerminator());
1512  if (!PreCondBI || PreCondBI->isUnconditional())
1513  return false;
1514 
1515  Instruction *CntInst;
1516  PHINode *CntPhi;
1517  Value *Val;
1518  if (!detectPopcountIdiom(CurLoop, PreCondBB, CntInst, CntPhi, Val))
1519  return false;
1520 
1521  transformLoopToPopcount(PreCondBB, CntInst, CntPhi, Val);
1522  return true;
1523 }
1524 
1526  const DebugLoc &DL) {
1527  Value *Ops[] = {Val};
1528  Type *Tys[] = {Val->getType()};
1529 
1530  Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent();
1531  Value *Func = Intrinsic::getDeclaration(M, Intrinsic::ctpop, Tys);
1532  CallInst *CI = IRBuilder.CreateCall(Func, Ops);
1533  CI->setDebugLoc(DL);
1534 
1535  return CI;
1536 }
1537 
1539  const DebugLoc &DL, bool ZeroCheck,
1540  Intrinsic::ID IID) {
1541  Value *Ops[] = {Val, ZeroCheck ? IRBuilder.getTrue() : IRBuilder.getFalse()};
1542  Type *Tys[] = {Val->getType()};
1543 
1544  Module *M = IRBuilder.GetInsertBlock()->getParent()->getParent();
1545  Value *Func = Intrinsic::getDeclaration(M, IID, Tys);
1546  CallInst *CI = IRBuilder.CreateCall(Func, Ops);
1547  CI->setDebugLoc(DL);
1548 
1549  return CI;
1550 }
1551 
1552 /// Transform the following loop (Using CTLZ, CTTZ is similar):
1553 /// loop:
1554 /// CntPhi = PHI [Cnt0, CntInst]
1555 /// PhiX = PHI [InitX, DefX]
1556 /// CntInst = CntPhi + 1
1557 /// DefX = PhiX >> 1
1558 /// LOOP_BODY
1559 /// Br: loop if (DefX != 0)
1560 /// Use(CntPhi) or Use(CntInst)
1561 ///
1562 /// Into:
1563 /// If CntPhi used outside the loop:
1564 /// CountPrev = BitWidth(InitX) - CTLZ(InitX >> 1)
1565 /// Count = CountPrev + 1
1566 /// else
1567 /// Count = BitWidth(InitX) - CTLZ(InitX)
1568 /// loop:
1569 /// CntPhi = PHI [Cnt0, CntInst]
1570 /// PhiX = PHI [InitX, DefX]
1571 /// PhiCount = PHI [Count, Dec]
1572 /// CntInst = CntPhi + 1
1573 /// DefX = PhiX >> 1
1574 /// Dec = PhiCount - 1
1575 /// LOOP_BODY
1576 /// Br: loop if (Dec != 0)
1577 /// Use(CountPrev + Cnt0) // Use(CntPhi)
1578 /// or
1579 /// Use(Count + Cnt0) // Use(CntInst)
1580 ///
1581 /// If LOOP_BODY is empty the loop will be deleted.
1582 /// If CntInst and DefX are not used in LOOP_BODY they will be removed.
1583 void LoopIdiomRecognize::transformLoopToCountable(
1584  Intrinsic::ID IntrinID, BasicBlock *Preheader, Instruction *CntInst,
1585  PHINode *CntPhi, Value *InitX, Instruction *DefX, const DebugLoc &DL,
1586  bool ZeroCheck, bool IsCntPhiUsedOutsideLoop) {
1587  BranchInst *PreheaderBr = cast<BranchInst>(Preheader->getTerminator());
1588 
1589  // Step 1: Insert the CTLZ/CTTZ instruction at the end of the preheader block
1590  IRBuilder<> Builder(PreheaderBr);
1591  Builder.SetCurrentDebugLocation(DL);
1592  Value *FFS, *Count, *CountPrev, *NewCount, *InitXNext;
1593 
1594  // Count = BitWidth - CTLZ(InitX);
1595  // If there are uses of CntPhi create:
1596  // CountPrev = BitWidth - CTLZ(InitX >> 1);
1597  if (IsCntPhiUsedOutsideLoop) {
1598  if (DefX->getOpcode() == Instruction::AShr)
1599  InitXNext =
1600  Builder.CreateAShr(InitX, ConstantInt::get(InitX->getType(), 1));
1601  else if (DefX->getOpcode() == Instruction::LShr)
1602  InitXNext =
1603  Builder.CreateLShr(InitX, ConstantInt::get(InitX->getType(), 1));
1604  else if (DefX->getOpcode() == Instruction::Shl) // cttz
1605  InitXNext =
1606  Builder.CreateShl(InitX, ConstantInt::get(InitX->getType(), 1));
1607  else
1608  llvm_unreachable("Unexpected opcode!");
1609  } else
1610  InitXNext = InitX;
1611  FFS = createFFSIntrinsic(Builder, InitXNext, DL, ZeroCheck, IntrinID);
1612  Count = Builder.CreateSub(
1613  ConstantInt::get(FFS->getType(),
1614  FFS->getType()->getIntegerBitWidth()),
1615  FFS);
1616  if (IsCntPhiUsedOutsideLoop) {
1617  CountPrev = Count;
1618  Count = Builder.CreateAdd(
1619  CountPrev,
1620  ConstantInt::get(CountPrev->getType(), 1));
1621  }
1622 
1623  NewCount = Builder.CreateZExtOrTrunc(
1624  IsCntPhiUsedOutsideLoop ? CountPrev : Count,
1625  cast<IntegerType>(CntInst->getType()));
1626 
1627  // If the counter's initial value is not zero, insert Add Inst.
1628  Value *CntInitVal = CntPhi->getIncomingValueForBlock(Preheader);
1629  ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
1630  if (!InitConst || !InitConst->isZero())
1631  NewCount = Builder.CreateAdd(NewCount, CntInitVal);
1632 
1633  // Step 2: Insert new IV and loop condition:
1634  // loop:
1635  // ...
1636  // PhiCount = PHI [Count, Dec]
1637  // ...
1638  // Dec = PhiCount - 1
1639  // ...
1640  // Br: loop if (Dec != 0)
1641  BasicBlock *Body = *(CurLoop->block_begin());
1642  auto *LbBr = cast<BranchInst>(Body->getTerminator());
1643  ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
1644  Type *Ty = Count->getType();
1645 
1646  PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front());
1647 
1648  Builder.SetInsertPoint(LbCond);
1649  Instruction *TcDec = cast<Instruction>(
1650  Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1),
1651  "tcdec", false, true));
1652 
1653  TcPhi->addIncoming(Count, Preheader);
1654  TcPhi->addIncoming(TcDec, Body);
1655 
1656  CmpInst::Predicate Pred =
1657  (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ;
1658  LbCond->setPredicate(Pred);
1659  LbCond->setOperand(0, TcDec);
1660  LbCond->setOperand(1, ConstantInt::get(Ty, 0));
1661 
1662  // Step 3: All the references to the original counter outside
1663  // the loop are replaced with the NewCount
1664  if (IsCntPhiUsedOutsideLoop)
1665  CntPhi->replaceUsesOutsideBlock(NewCount, Body);
1666  else
1667  CntInst->replaceUsesOutsideBlock(NewCount, Body);
1668 
1669  // step 4: Forget the "non-computable" trip-count SCEV associated with the
1670  // loop. The loop would otherwise not be deleted even if it becomes empty.
1671  SE->forgetLoop(CurLoop);
1672 }
1673 
1674 void LoopIdiomRecognize::transformLoopToPopcount(BasicBlock *PreCondBB,
1675  Instruction *CntInst,
1676  PHINode *CntPhi, Value *Var) {
1677  BasicBlock *PreHead = CurLoop->getLoopPreheader();
1678  auto *PreCondBr = cast<BranchInst>(PreCondBB->getTerminator());
1679  const DebugLoc &DL = CntInst->getDebugLoc();
1680 
1681  // Assuming before transformation, the loop is following:
1682  // if (x) // the precondition
1683  // do { cnt++; x &= x - 1; } while(x);
1684 
1685  // Step 1: Insert the ctpop instruction at the end of the precondition block
1686  IRBuilder<> Builder(PreCondBr);
1687  Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
1688  {
1689  PopCnt = createPopcntIntrinsic(Builder, Var, DL);
1690  NewCount = PopCntZext =
1691  Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType()));
1692 
1693  if (NewCount != PopCnt)
1694  (cast<Instruction>(NewCount))->setDebugLoc(DL);
1695 
1696  // TripCnt is exactly the number of iterations the loop has
1697  TripCnt = NewCount;
1698 
1699  // If the population counter's initial value is not zero, insert Add Inst.
1700  Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead);
1701  ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
1702  if (!InitConst || !InitConst->isZero()) {
1703  NewCount = Builder.CreateAdd(NewCount, CntInitVal);
1704  (cast<Instruction>(NewCount))->setDebugLoc(DL);
1705  }
1706  }
1707 
1708  // Step 2: Replace the precondition from "if (x == 0) goto loop-exit" to
1709  // "if (NewCount == 0) loop-exit". Without this change, the intrinsic
1710  // function would be partial dead code, and downstream passes will drag
1711  // it back from the precondition block to the preheader.
1712  {
1713  ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition());
1714 
1715  Value *Opnd0 = PopCntZext;
1716  Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0);
1717  if (PreCond->getOperand(0) != Var)
1718  std::swap(Opnd0, Opnd1);
1719 
1720  ICmpInst *NewPreCond = cast<ICmpInst>(
1721  Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1));
1722  PreCondBr->setCondition(NewPreCond);
1723 
1725  }
1726 
1727  // Step 3: Note that the population count is exactly the trip count of the
1728  // loop in question, which enable us to convert the loop from noncountable
1729  // loop into a countable one. The benefit is twofold:
1730  //
1731  // - If the loop only counts population, the entire loop becomes dead after
1732  // the transformation. It is a lot easier to prove a countable loop dead
1733  // than to prove a noncountable one. (In some C dialects, an infinite loop
1734  // isn't dead even if it computes nothing useful. In general, DCE needs
1735  // to prove a noncountable loop finite before safely delete it.)
1736  //
1737  // - If the loop also performs something else, it remains alive.
1738  // Since it is transformed to countable form, it can be aggressively
1739  // optimized by some optimizations which are in general not applicable
1740  // to a noncountable loop.
1741  //
1742  // After this step, this loop (conceptually) would look like following:
1743  // newcnt = __builtin_ctpop(x);
1744  // t = newcnt;
1745  // if (x)
1746  // do { cnt++; x &= x-1; t--) } while (t > 0);
1747  BasicBlock *Body = *(CurLoop->block_begin());
1748  {
1749  auto *LbBr = cast<BranchInst>(Body->getTerminator());
1750  ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
1751  Type *Ty = TripCnt->getType();
1752 
1753  PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", &Body->front());
1754 
1755  Builder.SetInsertPoint(LbCond);
1756  Instruction *TcDec = cast<Instruction>(
1757  Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1),
1758  "tcdec", false, true));
1759 
1760  TcPhi->addIncoming(TripCnt, PreHead);
1761  TcPhi->addIncoming(TcDec, Body);
1762 
1763  CmpInst::Predicate Pred =
1764  (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_UGT : CmpInst::ICMP_SLE;
1765  LbCond->setPredicate(Pred);
1766  LbCond->setOperand(0, TcDec);
1767  LbCond->setOperand(1, ConstantInt::get(Ty, 0));
1768  }
1769 
1770  // Step 4: All the references to the original population counter outside
1771  // the loop are replaced with the NewCount -- the value returned from
1772  // __builtin_ctpop().
1773  CntInst->replaceUsesOutsideBlock(NewCount, Body);
1774 
1775  // step 5: Forget the "non-computable" trip-count SCEV associated with the
1776  // loop. The loop would otherwise not be deleted even if it becomes empty.
1777  SE->forgetLoop(CurLoop);
1778 }
The access may reference and may modify the value stored in memory.
virtual void computeLoopSafetyInfo(const Loop *CurLoop)
Computes safety information for a loop checks loop body & header for the possibility of may throw exc...
Definition: MustExecute.cpp:44
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
uint64_t CallInst * C
Value * getValueOperand()
Definition: Instructions.h:409
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks &#39;this&#39; from the containing basic block and deletes it.
Definition: Instruction.cpp:67
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:584
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1948
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static CallInst * createPopcntIntrinsic(IRBuilder<> &IRBuilder, Value *Val, const DebugLoc &DL)
PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition: IRBuilder.h:1668
const SCEV * getConstant(ConstantInt *V)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
BinaryOps getOpcode() const
Definition: InstrTypes.h:315
Value * isBytewiseValue(Value *V)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
bool isAtomic() const
Return true if this instruction has an AtomicOrdering of unordered or higher.
Constant * getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
Definition: Module.cpp:143
ArrayRef< BasicBlock *>::const_iterator block_iterator
Definition: LoopInfo.h:152
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
static constexpr LocationSize unknown()
bool isUnordered() const
Definition: Instructions.h:403
void push_back(const T &Elt)
Definition: SmallVector.h:217
The main scalar evolution driver.
This class represents a function call, abstracting a target machine&#39;s calling convention.
static PHINode * getRecurrenceVar(Value *VarX, Instruction *DefX, BasicBlock *LoopEntry)
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:173
Value * getValue() const
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:56
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:704
static LocationSize precise(uint64_t Value)
This class wraps the llvm.memset intrinsic.
BasicBlock * getSuccessor(unsigned i) const
STATISTIC(NumFunctions, "Total number of functions")
A debug info location.
Definition: DebugLoc.h:33
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:502
An instruction for reading from memory.
Definition: Instructions.h:167
static Value * matchCondition(BranchInst *BI, BasicBlock *LoopEntry, bool JmpOnZero=false)
Check if the given conditional branch is based on the comparison between a variable and zero...
Value * getCondition() const
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:137
void setAlignment(unsigned Align)
Definition: Globals.cpp:115
Value * getLength() const
iterator end()
Get an iterator to the end of the SetVector.
Definition: SetVector.h:92
static Constant * get(ArrayType *T, ArrayRef< Constant *> V)
Definition: Constants.cpp:982
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions (including addrspacecast) that ...
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:268
PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const
Return hardware support for population count.
Simple and conservative implementation of LoopSafetyInfo that can give false-positive answers to its ...
Definition: MustExecute.h:97
AnalysisUsage & addRequired()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
Definition: BasicBlock.cpp:133
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: Instructions.h:231
amdgpu Simplify well known AMD library false Value Value const Twine & Name
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:370
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition: LoopInfo.h:689
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:742
This file contains the simple types necessary to represent the attributes associated with functions a...
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1013
unsigned getDestAlignment() const
This file implements a class to represent arbitrary precision integral constant values and operations...
const APInt & getAPInt() const
BlockT * getHeader() const
Definition: LoopInfo.h:99
static bool isSimple(Instruction *I)
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition: Constants.h:200
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:418
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:244
Value handle that is nullable, but tries to track the Value.
Definition: ValueHandle.h:181
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
This node represents a polynomial recurrence on the trip count of the specified loop.
static APInt getStoreStride(const SCEVAddRecExpr *StoreEv)
#define T
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:120
Class to represent array types.
Definition: DerivedTypes.h:368
static cl::opt< bool > UseLIRCodeSizeHeurs("use-lir-code-size-heurs", cl::desc("Use loop idiom recognition code size heuristics when compiling" "with -Os/-Oz"), cl::init(true), cl::Hidden)
bool has(LibFunc F) const
Tests whether a library function is available.
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition: SetVector.h:82
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:220
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:125
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1030
void initializeLoopIdiomRecognizeLegacyPassPass(PassRegistry &)
An instruction for storing to memory.
Definition: Instructions.h:320
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Definition: Constants.h:208
bool isKnownNonNegative(const Value *V, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Returns true if the give value is known to be non-negative.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:428
static void deleteDeadInstruction(Instruction *I)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1019
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Definition: IRBuilder.h:126
Value * getOperand(unsigned i) const
Definition: User.h:169
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
Definition: SetVector.h:210
bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS. ...
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:1772
bool isConsecutiveAccess(Value *A, Value *B, const DataLayout &DL, ScalarEvolution &SE, bool CheckType=true)
Returns true if the memory operations A and B are consecutive.
bool inferLibFuncAttributes(Function &F, const TargetLibraryInfo &TLI)
Analyze the name and prototype of the given function and set any applicable attributes.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:422
const SCEV * getOperand(unsigned i) const
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:189
Wrapper pass for TargetTransformInfo.
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Definition: Instruction.h:307
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:233
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
ConstantInt * getTrue()
Get the constant value for i1 true.
Definition: IRBuilder.h:286
Conditional or Unconditional Branch instruction.
static CallInst * createFFSIntrinsic(IRBuilder<> &IRBuilder, Value *Val, const DebugLoc &DL, bool ZeroCheck, Intrinsic::ID IID)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:41
Value * getIncomingValueForBlock(const BasicBlock *BB) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
const Instruction & front() const
Definition: BasicBlock.h:280
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:370
const SCEV * getAddExpr(SmallVectorImpl< const SCEV *> &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
size_t size() const
Definition: BasicBlock.h:278
bool isUnordered() const
Definition: Instructions.h:278
Represent the analysis usage information of a pass.
bool optForSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Definition: Function.h:597
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:645
static bool detectShiftUntilZeroIdiom(Loop *CurLoop, const DataLayout &DL, Intrinsic::ID &IntrinID, Value *&InitX, Instruction *&CntInst, PHINode *&CntPhi, Instruction *&DefX)
Return true if the idiom is detected in the loop.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values...
Value * getPointerOperand()
Definition: Instructions.h:284
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
self_iterator getIterator()
Definition: ilist_node.h:81
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS. Minus is represented in SCEV as A+B*-1.
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:429
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1414
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
size_t size() const
Definition: SmallVector.h:52
static wasm::ValType getType(const TargetRegisterClass *RC)
static PointerType * getInt8PtrTy(LLVMContext &C, unsigned AS=0)
Definition: Type.cpp:219
bool isVolatile() const
const SCEV * getMulExpr(SmallVectorImpl< const SCEV *> &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Representation for a specific memory location.
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
typename vector_type::const_iterator iterator
Definition: SetVector.h:48
Iterator for intrusive lists based on ilist_node.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
Type * getType() const
Return the LLVM type of this SCEV expression.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
iterator end()
Definition: BasicBlock.h:270
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:845
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:248
Module.h This file contains the declarations for the Module class.
Provides information about what library functions are available for the current target.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:621
bool isConditional() const
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static Constant * getMemSetPatternValue(Value *V, const DataLayout *DL)
getMemSetPatternValue - If a strided store of the specified value is safe to turn into a memset_patte...
static const SCEV * getNumBytes(const SCEV *BECount, Type *IntPtr, unsigned StoreSize, Loop *CurLoop, const DataLayout *DL, ScalarEvolution *SE)
Compute the number of bytes as a SCEV from the backedge taken count.
static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB, Instruction *&CntInst, PHINode *&CntPhi, Value *&Var)
Return true iff the idiom is detected in the loop.
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:577
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
The access may modify the value stored in memory.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
signed less or equal
Definition: InstrTypes.h:675
loop Recognize loop idioms
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:55
Class for arbitrary precision integers.
Definition: APInt.h:69
loop idiom
iterator_range< user_iterator > users()
Definition: Value.h:399
This class uses information about analyze scalars to rewrite expressions in canonical form...
Pass * createLoopIdiomPass()
ConstantInt * getFalse()
Get the constant value for i1 false.
Definition: IRBuilder.h:291
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:567
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:720
bool isVolatile() const
Return true if this is a store to a volatile memory location.
Definition: Instructions.h:353
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:310
INITIALIZE_PASS_BEGIN(LoopIdiomRecognizeLegacyPass, "loop-idiom", "Recognize loop idioms", false, false) INITIALIZE_PASS_END(LoopIdiomRecognizeLegacyPass
void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:215
unsigned getAtomicMemIntrinsicMaxElementSize() const
unsigned getAlignment() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:240
This class represents an analyzed expression in the program.
unsigned getIntegerBitWidth() const
Definition: DerivedTypes.h:96
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:464
This file provides utility analysis objects describing memory locations.
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:213
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
LLVM_NODISCARD ModRefInfo intersectModRef(const ModRefInfo MRI1, const ModRefInfo MRI2)
#define I(x, y, z)
Definition: MD5.cpp:58
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:192
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass&#39;s AnalysisUsage.
Definition: LoopUtils.cpp:131
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:580
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:322
block_iterator block_end() const
Definition: LoopInfo.h:154
uint32_t Size
Definition: Profile.cpp:46
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1973
unsigned getAlignment() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:365
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:290
static const SCEV * getStartForNegStride(const SCEV *Start, const SCEV *BECount, Type *IntPtr, unsigned StoreSize, ScalarEvolution *SE)
const SCEV * getBackedgeTakenCount(const Loop *L)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
The cost of a typical &#39;add&#39; instruction.
int getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, ArrayRef< Type *> ParamTys) const
Estimate the cost of an intrinsic when lowered.
const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
LLVM Value Representation.
Definition: Value.h:72
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
A vector that has set insertion semantics.
Definition: SetVector.h:40
static bool mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L, const SCEV *BECount, unsigned StoreSize, AliasAnalysis &AA, SmallPtrSetImpl< Instruction *> &IgnoredStores)
mayLoopAccessLocation - Return true if the specified loop might access the specified pointer location...
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
unsigned greater than
Definition: InstrTypes.h:668
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
A container for analyses that lazily runs them and caches their results.
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB)
replaceUsesOutsideBlock - Go through the uses list for this definition and make each use point to "V"...
Definition: Value.cpp:438
const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
This pass exposes codegen information to IR-level passes.
LLVM_NODISCARD bool isModOrRefSet(const ModRefInfo MRI)
bool isSimple() const
Definition: Instructions.h:401
This header defines various interfaces for pass management in LLVM.
bool isBigEndian() const
Definition: DataLayout.h:221
ModRefInfo getModRefInfo(const CallBase *Call, const MemoryLocation &Loc)
getModRefInfo (for call sites) - Return information about whether a particular call site modifies or ...
virtual bool anyBlockMayThrow() const
Returns true iff any block of the loop for which this info is contains an instruction that may throw ...
Definition: MustExecute.cpp:40
#define LLVM_DEBUG(X)
Definition: Debug.h:122
bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE)
Return true if the given expression is safe to expand in the sense that all materialized values are s...
block_iterator block_begin() const
Definition: LoopInfo.h:153
bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count...
Value * getPointerOperand()
Definition: Instructions.h:412
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
This class represents a constant integer value.