LLVM 24.0.0git
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: memcmp, etc.
24//
25// This could recognize common matrix multiplies and dot product idioms and
26// replace them with calls to BLAS (if linked in??).
27//
28//===----------------------------------------------------------------------===//
29
31#include "llvm/ADT/APInt.h"
32#include "llvm/ADT/ArrayRef.h"
33#include "llvm/ADT/DenseMap.h"
34#include "llvm/ADT/MapVector.h"
35#include "llvm/ADT/STLExtras.h"
36#include "llvm/ADT/SetVector.h"
39#include "llvm/ADT/Statistic.h"
40#include "llvm/ADT/StringRef.h"
58#include "llvm/IR/BasicBlock.h"
59#include "llvm/IR/Constant.h"
60#include "llvm/IR/Constants.h"
61#include "llvm/IR/DataLayout.h"
62#include "llvm/IR/DebugLoc.h"
64#include "llvm/IR/Dominators.h"
65#include "llvm/IR/GlobalValue.h"
67#include "llvm/IR/IRBuilder.h"
68#include "llvm/IR/InstrTypes.h"
69#include "llvm/IR/Instruction.h"
72#include "llvm/IR/Intrinsics.h"
73#include "llvm/IR/LLVMContext.h"
74#include "llvm/IR/Module.h"
75#include "llvm/IR/PassManager.h"
78#include "llvm/IR/Type.h"
79#include "llvm/IR/User.h"
80#include "llvm/IR/Value.h"
81#include "llvm/IR/ValueHandle.h"
84#include "llvm/Support/Debug.h"
91#include <algorithm>
92#include <cassert>
93#include <cstdint>
94#include <utility>
95
96using namespace llvm;
97using namespace SCEVPatternMatch;
98
99#define DEBUG_TYPE "loop-idiom"
100
101STATISTIC(NumMemSet, "Number of memset's formed from loop stores");
102STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores");
103STATISTIC(NumMemMove, "Number of memmove's formed from loop load+stores");
104STATISTIC(NumStrLen, "Number of strlen's and wcslen's formed from loop loads");
106 NumShiftUntilBitTest,
107 "Number of uncountable loops recognized as 'shift until bitttest' idiom");
108STATISTIC(NumShiftUntilZero,
109 "Number of uncountable loops recognized as 'shift until zero' idiom");
110
111namespace llvm {
114 DisableLIRPAll("disable-" DEBUG_TYPE "-all",
115 cl::desc("Options to disable Loop Idiom Recognize Pass."),
118
121 DisableLIRPMemset("disable-" DEBUG_TYPE "-memset",
122 cl::desc("Proceed with loop idiom recognize pass, but do "
123 "not convert loop(s) to memset."),
126
129 DisableLIRPMemcpy("disable-" DEBUG_TYPE "-memcpy",
130 cl::desc("Proceed with loop idiom recognize pass, but do "
131 "not convert loop(s) to memcpy."),
134
137 DisableLIRPStrlen("disable-loop-idiom-strlen",
138 cl::desc("Proceed with loop idiom recognize pass, but do "
139 "not convert loop(s) to strlen."),
142
145 EnableLIRPWcslen("disable-loop-idiom-wcslen",
146 cl::desc("Proceed with loop idiom recognize pass, "
147 "enable conversion of loop(s) to wcslen."),
150
153 DisableLIRPHashRecognize("disable-" DEBUG_TYPE "-hashrecognize",
154 cl::desc("Proceed with loop idiom recognize pass, "
155 "but do not optimize CRC loops."),
157 cl::init(false), cl::ReallyHidden);
158
160 "use-lir-code-size-heurs",
161 cl::desc("Use loop idiom recognition code size heuristics when compiling "
162 "with -Os/-Oz"),
163 cl::init(true), cl::Hidden);
164
166 "loop-idiom-force-memset-pattern-intrinsic",
167 cl::desc("Use memset.pattern intrinsic whenever possible"), cl::init(false),
168 cl::Hidden);
169
171 "loop-idiom-force-crc-clmul",
172 cl::desc("Use the clmul-based CRC loop optimization whenever possible"),
173 cl::init(false), cl::Hidden);
174
176
177} // namespace llvm
178
179namespace {
180
181class LoopIdiomRecognize {
182 Loop *CurLoop = nullptr;
184 DominatorTree *DT;
185 LoopInfo *LI;
186 ScalarEvolution *SE;
189 const DataLayout *DL;
191 bool ApplyCodeSizeHeuristics;
192 std::unique_ptr<MemorySSAUpdater> MSSAU;
193
194public:
195 explicit LoopIdiomRecognize(AliasAnalysis *AA, DominatorTree *DT,
196 LoopInfo *LI, ScalarEvolution *SE,
198 const TargetTransformInfo *TTI, MemorySSA *MSSA,
199 const DataLayout *DL,
201 : AA(AA), DT(DT), LI(LI), SE(SE), TLI(TLI), TTI(TTI), DL(DL), ORE(ORE) {
202 if (MSSA)
203 MSSAU = std::make_unique<MemorySSAUpdater>(MSSA);
204 }
205
206 bool runOnLoop(Loop *L);
207
208private:
209 using StoreList = SmallVector<StoreInst *, 8>;
210 using StoreListMap = MapVector<Value *, StoreList>;
211
212 StoreListMap StoreRefsForMemset;
213 StoreListMap StoreRefsForMemsetPattern;
214 StoreList StoreRefsForMemcpy;
215 bool HasMemset;
216 bool HasMemsetPattern;
217 bool HasMemcpy;
218
219 /// Return code for isLegalStore()
220 enum LegalStoreKind {
221 None = 0,
222 Memset,
223 MemsetPattern,
224 Memcpy,
225 UnorderedAtomicMemcpy,
226 DontUse // Dummy retval never to be used. Allows catching errors in retval
227 // handling.
228 };
229
230 /// \name Countable Loop Idiom Handling
231 /// @{
232
233 bool runOnCountableLoop();
234 bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
235 SmallVectorImpl<BasicBlock *> &ExitBlocks);
236
237 void collectStores(BasicBlock *BB);
238 LegalStoreKind isLegalStore(StoreInst *SI);
239 enum class ForMemset { No, Yes };
240 bool processLoopStores(SmallVectorImpl<StoreInst *> &SL, const SCEV *BECount,
241 ForMemset For);
242
243 template <typename MemInst>
244 bool processLoopMemIntrinsic(
245 BasicBlock *BB,
246 bool (LoopIdiomRecognize::*Processor)(MemInst *, const SCEV *),
247 const SCEV *BECount);
248 bool processLoopMemCpy(MemCpyInst *MCI, const SCEV *BECount);
249 bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount);
250
251 bool processLoopStridedStore(Value *DestPtr, const SCEV *StoreSizeSCEV,
252 MaybeAlign StoreAlignment, Value *StoredVal,
253 Instruction *TheStore,
254 SmallPtrSetImpl<Instruction *> &Stores,
255 const SCEVAddRecExpr *Ev, const SCEV *BECount,
256 bool IsNegStride, bool IsLoopMemset = false);
257 bool processLoopStoreOfLoopLoad(StoreInst *SI, const SCEV *BECount);
258 bool processLoopStoreOfLoopLoad(Value *DestPtr, Value *SourcePtr,
259 const SCEV *StoreSize, MaybeAlign StoreAlign,
260 MaybeAlign LoadAlign, Instruction *TheStore,
261 Instruction *TheLoad,
262 const SCEVAddRecExpr *StoreEv,
263 const SCEVAddRecExpr *LoadEv,
264 const SCEV *BECount);
265 bool avoidLIRForMultiBlockLoop(bool IsMemset = false,
266 bool IsLoopMemset = false);
267 bool optimizeCRCLoop(const PolynomialInfo &Info);
268 void optimizeCRCLoopUsingClmul(const PolynomialInfo &Info,
269 IntegerType *ClmulTy);
270 void optimizeCRCLoopUsingTableLookup(const PolynomialInfo &Info);
271
272 /// @}
273 /// \name Noncountable Loop Idiom Handling
274 /// @{
275
276 bool runOnNoncountableLoop();
277
278 bool recognizePopcount();
279 void transformLoopToPopcount(BasicBlock *PreCondBB, Instruction *CntInst,
280 PHINode *CntPhi, Value *Var);
281 bool isProfitableToInsertFFS(Intrinsic::ID IntrinID, Value *InitX,
282 bool ZeroCheck, size_t CanonicalSize);
283 bool insertFFSIfProfitable(Intrinsic::ID IntrinID, Value *InitX,
284 Instruction *DefX, PHINode *CntPhi,
285 Instruction *CntInst);
286 bool recognizeAndInsertFFS(); /// Find First Set: ctlz or cttz
287 bool recognizeShiftUntilLessThan();
288 void transformLoopToCountable(Intrinsic::ID IntrinID, BasicBlock *PreCondBB,
289 Instruction *CntInst, PHINode *CntPhi,
290 Value *Var, Instruction *DefX,
291 const DebugLoc &DL, bool ZeroCheck,
292 bool IsCntPhiUsedOutsideLoop,
293 bool InsertSub = false);
294
295 bool recognizeShiftUntilBitTest();
296 bool recognizeShiftUntilZero();
297 bool recognizeAndInsertStrLen();
298
299 /// @}
300};
301} // end anonymous namespace
302
305 LPMUpdater &) {
307 return PreservedAnalyses::all();
308
309 const auto *DL = &L.getHeader()->getDataLayout();
310
311 // For the new PM, we also can't use OptimizationRemarkEmitter as an analysis
312 // pass. Function analyses need to be preserved across loop transformations
313 // but ORE cannot be preserved (see comment before the pass definition).
314 OptimizationRemarkEmitter ORE(L.getHeader()->getParent());
315
316 LoopIdiomRecognize LIR(&AR.AA, &AR.DT, &AR.LI, &AR.SE, &AR.TLI, &AR.TTI,
317 AR.MSSA, DL, ORE);
318 if (!LIR.runOnLoop(&L))
319 return PreservedAnalyses::all();
320
322 if (AR.MSSA)
323 PA.preserve<MemorySSAAnalysis>();
324 return PA;
325}
326
328 I->replaceAllUsesWith(PoisonValue::get(I->getType()));
329 I->eraseFromParent();
330}
331
332//===----------------------------------------------------------------------===//
333//
334// Implementation of LoopIdiomRecognize
335//
336//===----------------------------------------------------------------------===//
337
338bool LoopIdiomRecognize::runOnLoop(Loop *L) {
339 CurLoop = L;
340 // If the loop could not be converted to canonical form, it must have an
341 // indirectbr in it, just give up.
342 if (!L->getLoopPreheader())
343 return false;
344
345 // Disable loop idiom recognition if the function's name is a common idiom.
346 StringRef Name = L->getHeader()->getParent()->getName();
347 if (Name == "memset" || Name == "memcpy" || Name == "strlen" ||
348 Name == "wcslen")
349 return false;
350
351 // Determine if code size heuristics need to be applied.
352 ApplyCodeSizeHeuristics =
353 L->getHeader()->getParent()->hasOptSize() && UseLIRCodeSizeHeurs;
354
355 HasMemset = TLI->has(LibFunc_memset);
356 // TODO: Unconditionally enable use of the memset pattern intrinsic (or at
357 // least, opt-in via target hook) once we are confident it will never result
358 // in worse codegen than without. For now, use it only when the target
359 // supports memset_pattern16 libcall (or unless this is overridden by
360 // command line option).
361 HasMemsetPattern = TLI->has(LibFunc_memset_pattern16);
362 HasMemcpy = TLI->has(LibFunc_memcpy);
363
364 if (HasMemset || HasMemsetPattern || ForceMemsetPatternIntrinsic ||
365 HasMemcpy || !DisableLIRP::HashRecognize)
367 return runOnCountableLoop();
368
369 return runOnNoncountableLoop();
370}
371
372bool LoopIdiomRecognize::runOnCountableLoop() {
373 const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop);
375 "runOnCountableLoop() called on a loop without a predictable"
376 "backedge-taken count");
377
378 // If this loop executes exactly one time, then it should be peeled, not
379 // optimized by this pass.
380 if (BECount->isZero())
381 return false;
382
384 CurLoop->getUniqueExitBlocks(ExitBlocks);
385
386 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Scanning: F["
387 << CurLoop->getHeader()->getParent()->getName()
388 << "] Countable Loop %" << CurLoop->getHeader()->getName()
389 << "\n");
390
391 // The following transforms hoist stores/memsets into the loop pre-header.
392 // Give up if the loop has instructions that may throw.
393 SimpleLoopSafetyInfo SafetyInfo;
394 SafetyInfo.computeLoopSafetyInfo(CurLoop);
395 if (SafetyInfo.anyBlockMayThrow())
396 return false;
397
398 bool MadeChange = false;
399
400 // Scan all the blocks in the loop that are not in subloops.
401 for (auto *BB : CurLoop->getBlocks()) {
402 // Ignore blocks in subloops.
403 if (LI->getLoopFor(BB) != CurLoop)
404 continue;
405
406 MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks);
407 }
408
409 // Attempt to optimize a CRC loop if one is detected by HashRecognize.
411 if (auto Res = HashRecognize(*CurLoop, *SE).getResult())
412 MadeChange |= optimizeCRCLoop(*Res);
413
414 return MadeChange;
415}
416
417static APInt getStoreStride(const SCEVAddRecExpr *StoreEv) {
418 const SCEVConstant *ConstStride = cast<SCEVConstant>(StoreEv->getOperand(1));
419 return ConstStride->getAPInt();
420}
421
422/// getMemSetPatternValue - If a strided store of the specified value is safe to
423/// turn into a memset.patternn intrinsic, return the Constant that should
424/// be passed in. Otherwise, return null.
425///
426/// TODO this function could allow more constants than it does today (e.g.
427/// those over 16 bytes) now it has transitioned to being used for the
428/// memset.pattern intrinsic rather than directly the memset_pattern16
429/// libcall.
431 // FIXME: This could check for UndefValue because it can be merged into any
432 // other valid pattern.
433
434 // If the value isn't a constant, we can't promote it to being in a constant
435 // array. We could theoretically do a store to an alloca or something, but
436 // that doesn't seem worthwhile.
438 if (!C || isa<ConstantExpr>(C))
439 return nullptr;
440
441 // Only handle simple values that are a power of two bytes in size.
442 uint64_t Size = DL->getTypeSizeInBits(V->getType());
443 if (Size == 0 || (Size & 7) || (Size & (Size - 1)))
444 return nullptr;
445
446 // Don't care enough about darwin/ppc to implement this.
447 if (DL->isBigEndian())
448 return nullptr;
449
450 // Convert to size in bytes.
451 Size /= 8;
452
453 // TODO: If CI is larger than 16-bytes, we can try slicing it in half to see
454 // if the top and bottom are the same (e.g. for vectors and large integers).
455 if (Size > 16)
456 return nullptr;
457
458 // For now, don't handle types that aren't int, floats, or pointers.
459 Type *CTy = C->getType();
460 if (!CTy->isIntOrPtrTy() && !CTy->isFloatingPointTy())
461 return nullptr;
462
463 return C;
464}
465
466LoopIdiomRecognize::LegalStoreKind
467LoopIdiomRecognize::isLegalStore(StoreInst *SI) {
468 // Don't touch volatile stores.
469 if (SI->isVolatile())
470 return LegalStoreKind::None;
471 // We only want simple or unordered-atomic stores.
472 if (!SI->isUnordered())
473 return LegalStoreKind::None;
474
475 // Avoid merging nontemporal stores.
476 if (SI->getMetadata(LLVMContext::MD_nontemporal))
477 return LegalStoreKind::None;
478
479 Value *StoredVal = SI->getValueOperand();
480 Value *StorePtr = SI->getPointerOperand();
481
482 if (DL->hasUnstableRepresentation(StoredVal->getType()))
483 return LegalStoreKind::None;
484
485 // Transformations could invalidate the external-state pointers
486 // memcpy - LangRef specifies that a valid memcpy must preserve external
487 // state, so no transformations are blocked by it.
488 // memset - We assume that a memset of 0 has an equivalent external state
489 // effect as a null pointer store. This is currently not explicitly
490 // specified, but is true of the one exemplar we have (CHERI
491 // capabilities). All other memset formations are not safe.
492 bool MustPreserveExternalState = DL->hasExternalState(StoredVal->getType()) &&
493 !isa<ConstantPointerNull>(StoredVal);
494
495 // Reject stores that are so large that they overflow an unsigned.
496 // When storing out scalable vectors we bail out for now, since the code
497 // below currently only works for constant strides.
498 TypeSize SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
499 if (SizeInBits.isScalable() || (SizeInBits.getFixedValue() & 7) ||
500 (SizeInBits.getFixedValue() >> 32) != 0)
501 return LegalStoreKind::None;
502
503 // See if the pointer expression is an AddRec like {base,+,1} on the current
504 // loop, which indicates a strided store. If we have something else, it's a
505 // random store we can't handle.
506 const SCEV *StoreEv = SE->getSCEV(StorePtr);
507 const SCEVConstant *Stride;
508 if (!match(StoreEv, m_scev_AffineAddRec(m_SCEV(), m_SCEVConstant(Stride),
509 m_SpecificLoop(CurLoop))))
510 return LegalStoreKind::None;
511
512 // See if the store can be turned into a memset.
513
514 // If the stored value is a byte-wise value (like i32 -1), then it may be
515 // turned into a memset of i8 -1, assuming that all the consecutive bytes
516 // are stored. A store of i32 0x01020304 can never be turned into a memset,
517 // but it can be turned into memset_pattern if the target supports it.
518 Value *SplatValue = isBytewiseValue(StoredVal, *DL);
519
520 // Note: memset and memset_pattern on unordered-atomic is yet not supported
521 bool UnorderedAtomic = SI->isUnordered() && !SI->isSimple();
522
523 // If we're allowed to form a memset, and the stored value would be
524 // acceptable for memset, use it.
525 if (!MustPreserveExternalState && !UnorderedAtomic && HasMemset &&
526 SplatValue && !DisableLIRP::Memset &&
527 // Verify that the stored value is loop invariant. If not, we can't
528 // promote the memset.
529 CurLoop->isLoopInvariant(SplatValue)) {
530 // It looks like we can use SplatValue.
531 return LegalStoreKind::Memset;
532 }
533 if (!MustPreserveExternalState && !UnorderedAtomic &&
534 (HasMemsetPattern || ForceMemsetPatternIntrinsic) &&
536 // Don't create memset_pattern16s with address spaces.
537 StorePtr->getType()->getPointerAddressSpace() == 0 &&
538 getMemSetPatternValue(StoredVal, DL)) {
539 // It looks like we can use PatternValue!
540 return LegalStoreKind::MemsetPattern;
541 }
542
543 // Otherwise, see if the store can be turned into a memcpy.
544 if (HasMemcpy && !DisableLIRP::Memcpy) {
545 // Check to see if the stride matches the size of the store. If so, then we
546 // know that every byte is touched in the loop.
547 unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
548 APInt StrideAP = Stride->getAPInt();
549 if (StoreSize != StrideAP && StoreSize != -StrideAP)
550 return LegalStoreKind::None;
551
552 // The store must be feeding a non-volatile load.
553 LoadInst *LI = dyn_cast<LoadInst>(SI->getValueOperand());
554
555 // Only allow non-volatile loads
556 if (!LI || LI->isVolatile())
557 return LegalStoreKind::None;
558 // Only allow simple or unordered-atomic loads
559 if (!LI->isUnordered())
560 return LegalStoreKind::None;
561
562 // See if the pointer expression is an AddRec like {base,+,1} on the current
563 // loop, which indicates a strided load. If we have something else, it's a
564 // random load we can't handle.
565 const SCEV *LoadEv = SE->getSCEV(LI->getPointerOperand());
566
567 // The store and load must share the same stride.
568 if (!match(LoadEv, m_scev_AffineAddRec(m_SCEV(), m_scev_Specific(Stride),
569 m_SpecificLoop(CurLoop))))
570 return LegalStoreKind::None;
571
572 // Success. This store can be converted into a memcpy.
573 UnorderedAtomic = UnorderedAtomic || LI->isAtomic();
574 return UnorderedAtomic ? LegalStoreKind::UnorderedAtomicMemcpy
575 : LegalStoreKind::Memcpy;
576 }
577 // This store can't be transformed into a memset/memcpy.
578 return LegalStoreKind::None;
579}
580
581void LoopIdiomRecognize::collectStores(BasicBlock *BB) {
582 StoreRefsForMemset.clear();
583 StoreRefsForMemsetPattern.clear();
584 StoreRefsForMemcpy.clear();
585 for (Instruction &I : *BB) {
587 if (!SI)
588 continue;
589
590 // Make sure this is a strided store with a constant stride.
591 switch (isLegalStore(SI)) {
592 case LegalStoreKind::None:
593 // Nothing to do
594 break;
595 case LegalStoreKind::Memset: {
596 // Find the base pointer.
597 Value *Ptr = getUnderlyingObject(SI->getPointerOperand());
598 StoreRefsForMemset[Ptr].push_back(SI);
599 } break;
600 case LegalStoreKind::MemsetPattern: {
601 // Find the base pointer.
602 Value *Ptr = getUnderlyingObject(SI->getPointerOperand());
603 StoreRefsForMemsetPattern[Ptr].push_back(SI);
604 } break;
605 case LegalStoreKind::Memcpy:
606 case LegalStoreKind::UnorderedAtomicMemcpy:
607 StoreRefsForMemcpy.push_back(SI);
608 break;
609 default:
610 assert(false && "unhandled return value");
611 break;
612 }
613 }
614}
615
616/// runOnLoopBlock - Process the specified block, which lives in a counted loop
617/// with the specified backedge count. This block is known to be in the current
618/// loop and not in any subloops.
619bool LoopIdiomRecognize::runOnLoopBlock(
620 BasicBlock *BB, const SCEV *BECount,
621 SmallVectorImpl<BasicBlock *> &ExitBlocks) {
622 // We can only promote stores in this block if they are unconditionally
623 // executed in the loop. For a block to be unconditionally executed, it has
624 // to dominate all the exit blocks of the loop. Verify this now.
625 for (BasicBlock *ExitBlock : ExitBlocks)
626 if (!DT->dominates(BB, ExitBlock))
627 return false;
628
629 bool MadeChange = false;
630 // Look for store instructions, which may be optimized to memset/memcpy.
631 collectStores(BB);
632
633 // Look for a single store or sets of stores with a common base, which can be
634 // optimized into a memset (memset_pattern). The latter most commonly happens
635 // with structs and handunrolled loops.
636 for (auto &SL : StoreRefsForMemset)
637 MadeChange |= processLoopStores(SL.second, BECount, ForMemset::Yes);
638
639 for (auto &SL : StoreRefsForMemsetPattern)
640 MadeChange |= processLoopStores(SL.second, BECount, ForMemset::No);
641
642 // Optimize the store into a memcpy, if it feeds an similarly strided load.
643 for (auto &SI : StoreRefsForMemcpy)
644 MadeChange |= processLoopStoreOfLoopLoad(SI, BECount);
645
646 MadeChange |= processLoopMemIntrinsic<MemCpyInst>(
647 BB, &LoopIdiomRecognize::processLoopMemCpy, BECount);
648 MadeChange |= processLoopMemIntrinsic<MemSetInst>(
649 BB, &LoopIdiomRecognize::processLoopMemSet, BECount);
650
651 return MadeChange;
652}
653
654/// See if this store(s) can be promoted to a memset.
655bool LoopIdiomRecognize::processLoopStores(SmallVectorImpl<StoreInst *> &SL,
656 const SCEV *BECount, ForMemset For) {
657 // Try to find consecutive stores that can be transformed into memsets.
658 SetVector<StoreInst *> Heads, Tails;
660
661 // Do a quadratic search on all of the given stores and find
662 // all of the pairs of stores that follow each other.
663 SmallVector<unsigned, 16> IndexQueue;
664 for (unsigned i = 0, e = SL.size(); i < e; ++i) {
665 assert(SL[i]->isSimple() && "Expected only non-volatile stores.");
666
667 Value *FirstStoredVal = SL[i]->getValueOperand();
668 Value *FirstStorePtr = SL[i]->getPointerOperand();
669 const SCEVAddRecExpr *FirstStoreEv =
670 cast<SCEVAddRecExpr>(SE->getSCEV(FirstStorePtr));
671 APInt FirstStride = getStoreStride(FirstStoreEv);
672 unsigned FirstStoreSize = DL->getTypeStoreSize(SL[i]->getValueOperand()->getType());
673
674 // See if we can optimize just this store in isolation.
675 if (FirstStride == FirstStoreSize || -FirstStride == FirstStoreSize) {
676 Heads.insert(SL[i]);
677 continue;
678 }
679
680 Value *FirstSplatValue = nullptr;
681 Constant *FirstPatternValue = nullptr;
682
683 if (For == ForMemset::Yes)
684 FirstSplatValue = isBytewiseValue(FirstStoredVal, *DL);
685 else
686 FirstPatternValue = getMemSetPatternValue(FirstStoredVal, DL);
687
688 assert((FirstSplatValue || FirstPatternValue) &&
689 "Expected either splat value or pattern value.");
690
691 IndexQueue.clear();
692 // If a store has multiple consecutive store candidates, search Stores
693 // array according to the sequence: from i+1 to e, then from i-1 to 0.
694 // This is because usually pairing with immediate succeeding or preceding
695 // candidate create the best chance to find memset opportunity.
696 unsigned j = 0;
697 for (j = i + 1; j < e; ++j)
698 IndexQueue.push_back(j);
699 for (j = i; j > 0; --j)
700 IndexQueue.push_back(j - 1);
701
702 for (auto &k : IndexQueue) {
703 assert(SL[k]->isSimple() && "Expected only non-volatile stores.");
704 Value *SecondStorePtr = SL[k]->getPointerOperand();
705 const SCEVAddRecExpr *SecondStoreEv =
706 cast<SCEVAddRecExpr>(SE->getSCEV(SecondStorePtr));
707 APInt SecondStride = getStoreStride(SecondStoreEv);
708
709 if (FirstStride != SecondStride)
710 continue;
711
712 Value *SecondStoredVal = SL[k]->getValueOperand();
713 Value *SecondSplatValue = nullptr;
714 Constant *SecondPatternValue = nullptr;
715
716 if (For == ForMemset::Yes)
717 SecondSplatValue = isBytewiseValue(SecondStoredVal, *DL);
718 else
719 SecondPatternValue = getMemSetPatternValue(SecondStoredVal, DL);
720
721 assert((SecondSplatValue || SecondPatternValue) &&
722 "Expected either splat value or pattern value.");
723
724 if (isConsecutiveAccess(SL[i], SL[k], *DL, *SE, false)) {
725 if (For == ForMemset::Yes) {
726 if (isa<UndefValue>(FirstSplatValue))
727 FirstSplatValue = SecondSplatValue;
728 if (FirstSplatValue != SecondSplatValue)
729 continue;
730 } else {
731 if (isa<UndefValue>(FirstPatternValue))
732 FirstPatternValue = SecondPatternValue;
733 if (FirstPatternValue != SecondPatternValue)
734 continue;
735 }
736 Tails.insert(SL[k]);
737 Heads.insert(SL[i]);
738 ConsecutiveChain[SL[i]] = SL[k];
739 break;
740 }
741 }
742 }
743
744 // We may run into multiple chains that merge into a single chain. We mark the
745 // stores that we transformed so that we don't visit the same store twice.
746 SmallPtrSet<Value *, 16> TransformedStores;
747 bool Changed = false;
748
749 // For stores that start but don't end a link in the chain:
750 for (StoreInst *I : Heads) {
751 if (Tails.count(I))
752 continue;
753
754 // We found a store instr that starts a chain. Now follow the chain and try
755 // to transform it.
756 SmallPtrSet<Instruction *, 8> AdjacentStores;
757 StoreInst *HeadStore = I;
758 unsigned StoreSize = 0;
759
760 // Collect the chain into a list.
761 while (Tails.count(I) || Heads.count(I)) {
762 if (TransformedStores.count(I))
763 break;
764 AdjacentStores.insert(I);
765
766 StoreSize += DL->getTypeStoreSize(I->getValueOperand()->getType());
767 // Move to the next value in the chain.
768 I = ConsecutiveChain[I];
769 }
770
771 Value *StoredVal = HeadStore->getValueOperand();
772 Value *StorePtr = HeadStore->getPointerOperand();
773 const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
774 APInt Stride = getStoreStride(StoreEv);
775
776 // Check to see if the stride matches the size of the stores. If so, then
777 // we know that every byte is touched in the loop.
778 if (StoreSize != Stride && StoreSize != -Stride)
779 continue;
780
781 bool IsNegStride = StoreSize == -Stride;
782
783 Type *IntIdxTy = DL->getIndexType(StorePtr->getType());
784 const SCEV *StoreSizeSCEV = SE->getConstant(IntIdxTy, StoreSize);
785 if (processLoopStridedStore(StorePtr, StoreSizeSCEV,
786 MaybeAlign(HeadStore->getAlign()), StoredVal,
787 HeadStore, AdjacentStores, StoreEv, BECount,
788 IsNegStride)) {
789 TransformedStores.insert_range(AdjacentStores);
790 Changed = true;
791 }
792 }
793
794 return Changed;
795}
796
797/// processLoopMemIntrinsic - Template function for calling different processor
798/// functions based on mem intrinsic type.
799template <typename MemInst>
800bool LoopIdiomRecognize::processLoopMemIntrinsic(
801 BasicBlock *BB,
802 bool (LoopIdiomRecognize::*Processor)(MemInst *, const SCEV *),
803 const SCEV *BECount) {
804 bool MadeChange = false;
805 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) {
806 Instruction *Inst = &*I++;
807 // Look for memory instructions, which may be optimized to a larger one.
808 if (MemInst *MI = dyn_cast<MemInst>(Inst)) {
809 WeakTrackingVH InstPtr(&*I);
810 if (!(this->*Processor)(MI, BECount))
811 continue;
812 MadeChange = true;
813
814 // If processing the instruction invalidated our iterator, start over from
815 // the top of the block.
816 if (!InstPtr)
817 I = BB->begin();
818 }
819 }
820 return MadeChange;
821}
822
823/// processLoopMemCpy - See if this memcpy can be promoted to a large memcpy
824bool LoopIdiomRecognize::processLoopMemCpy(MemCpyInst *MCI,
825 const SCEV *BECount) {
826 // We can only handle non-volatile memcpys with a constant size.
827 if (MCI->isVolatile() || !isa<ConstantInt>(MCI->getLength()))
828 return false;
829
830 // If we're not allowed to hack on memcpy, we fail.
831 if ((!HasMemcpy && !MCI->isForceInlined()) || DisableLIRP::Memcpy)
832 return false;
833
834 Value *Dest = MCI->getDest();
835 Value *Source = MCI->getSource();
836 if (!Dest || !Source)
837 return false;
838
839 // See if the load and store pointer expressions are AddRec like {base,+,1} on
840 // the current loop, which indicates a strided load and store. If we have
841 // something else, it's a random load or store we can't handle.
842 const SCEV *StoreEv = SE->getSCEV(Dest);
843 const SCEV *LoadEv = SE->getSCEV(Source);
844 const APInt *StoreStrideValue, *LoadStrideValue;
845 if (!match(StoreEv,
846 m_scev_AffineAddRec(m_SCEV(), m_scev_APInt(StoreStrideValue),
847 m_SpecificLoop(CurLoop))) ||
848 !match(LoadEv,
849 m_scev_AffineAddRec(m_SCEV(), m_scev_APInt(LoadStrideValue),
850 m_SpecificLoop(CurLoop))))
851 return false;
852
853 // Reject memcpys that are so large that they overflow an unsigned.
854 uint64_t SizeInBytes = cast<ConstantInt>(MCI->getLength())->getZExtValue();
855 if ((SizeInBytes >> 32) != 0)
856 return false;
857
858 // Huge stride value - give up
859 if (StoreStrideValue->getBitWidth() > 64 ||
860 LoadStrideValue->getBitWidth() > 64)
861 return false;
862
863 if (SizeInBytes != *StoreStrideValue && SizeInBytes != -*StoreStrideValue) {
864 ORE.emit([&]() {
865 return OptimizationRemarkMissed(DEBUG_TYPE, "SizeStrideUnequal", MCI)
866 << ore::NV("Inst", "memcpy") << " in "
867 << ore::NV("Function", MCI->getFunction())
868 << " function will not be hoisted: "
869 << ore::NV("Reason", "memcpy size is not equal to stride");
870 });
871 return false;
872 }
873
874 int64_t StoreStrideInt = StoreStrideValue->getSExtValue();
875 int64_t LoadStrideInt = LoadStrideValue->getSExtValue();
876 // Check if the load stride matches the store stride.
877 if (StoreStrideInt != LoadStrideInt)
878 return false;
879
880 return processLoopStoreOfLoopLoad(
881 Dest, Source, SE->getConstant(Dest->getType(), SizeInBytes),
882 MCI->getDestAlign(), MCI->getSourceAlign(), MCI, MCI,
883 cast<SCEVAddRecExpr>(StoreEv), cast<SCEVAddRecExpr>(LoadEv), BECount);
884}
885
886/// processLoopMemSet - See if this memset can be promoted to a large memset.
887bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI,
888 const SCEV *BECount) {
889 // We can only handle non-volatile memsets.
890 if (MSI->isVolatile())
891 return false;
892
893 // If we're not allowed to hack on memset, we fail.
894 if (!HasMemset || DisableLIRP::Memset)
895 return false;
896
897 Value *Pointer = MSI->getDest();
898
899 // See if the pointer expression is an AddRec like {base,+,1} on the current
900 // loop, which indicates a strided store. If we have something else, it's a
901 // random store we can't handle.
902 const SCEV *Ev = SE->getSCEV(Pointer);
903 const SCEV *PointerStrideSCEV;
904 if (!match(Ev, m_scev_AffineAddRec(m_SCEV(), m_SCEV(PointerStrideSCEV),
905 m_SpecificLoop(CurLoop)))) {
906 LLVM_DEBUG(dbgs() << " Pointer is not affine, abort\n");
907 return false;
908 }
909
910 SCEVUse MemsetSizeSCEV = SE->getSCEV(MSI->getLength());
911
912 bool IsNegStride = false;
913 const bool IsConstantSize = isa<ConstantInt>(MSI->getLength());
914
915 if (IsConstantSize) {
916 // Memset size is constant.
917 // Check if the pointer stride matches the memset size. If so, then
918 // we know that every byte is touched in the loop.
919 LLVM_DEBUG(dbgs() << " memset size is constant\n");
920 uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
921 const APInt *Stride;
922 if (!match(PointerStrideSCEV, m_scev_APInt(Stride)))
923 return false;
924
925 if (SizeInBytes != *Stride && SizeInBytes != -*Stride)
926 return false;
927
928 IsNegStride = SizeInBytes == -*Stride;
929 } else {
930 // Memset size is non-constant.
931 // Check if the pointer stride matches the memset size.
932 // To be conservative, the pass would not promote pointers that aren't in
933 // address space zero. Also, the pass only handles memset length and stride
934 // that are invariant for the top level loop.
935 LLVM_DEBUG(dbgs() << " memset size is non-constant\n");
936 if (Pointer->getType()->getPointerAddressSpace() != 0) {
937 LLVM_DEBUG(dbgs() << " pointer is not in address space zero, "
938 << "abort\n");
939 return false;
940 }
941 if (!SE->isLoopInvariant(MemsetSizeSCEV, CurLoop)) {
942 LLVM_DEBUG(dbgs() << " memset size is not a loop-invariant, "
943 << "abort\n");
944 return false;
945 }
946
947 // Compare positive direction PointerStrideSCEV with MemsetSizeSCEV
948 IsNegStride = PointerStrideSCEV->isNonConstantNegative();
949 SCEVUse PositiveStrideSCEV =
950 IsNegStride ? SCEVUse(SE->getNegativeSCEV(PointerStrideSCEV))
951 : SCEVUse(PointerStrideSCEV);
952 LLVM_DEBUG(dbgs() << " MemsetSizeSCEV: " << *MemsetSizeSCEV << "\n"
953 << " PositiveStrideSCEV: " << *PositiveStrideSCEV
954 << "\n");
955
956 if (PositiveStrideSCEV != MemsetSizeSCEV) {
957 // If an expression is covered by the loop guard, compare again and
958 // proceed with optimization if equal.
959 const SCEV *FoldedPositiveStride =
960 SE->applyLoopGuards(PositiveStrideSCEV, CurLoop);
961 const SCEV *FoldedMemsetSize =
962 SE->applyLoopGuards(MemsetSizeSCEV, CurLoop);
963
964 LLVM_DEBUG(dbgs() << " Try to fold SCEV based on loop guard\n"
965 << " FoldedMemsetSize: " << *FoldedMemsetSize << "\n"
966 << " FoldedPositiveStride: " << *FoldedPositiveStride
967 << "\n");
968
969 if (FoldedPositiveStride != FoldedMemsetSize) {
970 LLVM_DEBUG(dbgs() << " SCEV don't match, abort\n");
971 return false;
972 }
973 }
974 }
975
976 // Verify that the memset value is loop invariant. If not, we can't promote
977 // the memset.
978 Value *SplatValue = MSI->getValue();
979 if (!SplatValue || !CurLoop->isLoopInvariant(SplatValue))
980 return false;
981
983 MSIs.insert(MSI);
984 return processLoopStridedStore(Pointer, SE->getSCEV(MSI->getLength()),
985 MSI->getDestAlign(), SplatValue, MSI, MSIs,
986 cast<SCEVAddRecExpr>(Ev), BECount, IsNegStride,
987 /*IsLoopMemset=*/true);
988}
989
990/// Return true if \p I is a (simple, loop-invariant-valued) store of the same
991/// bytewise value \p SplatByte.
992static bool isSameByteValueStore(Instruction &I, Value *SplatByte, Loop *L,
993 const DataLayout &DL) {
994 assert(SplatByte && "expected a bytewise splat value to match against");
995 auto *SI = dyn_cast<StoreInst>(&I);
996 if (!SI || !SI->isSimple() || !L->isLoopInvariant(SI->getValueOperand()))
997 return false;
998 return isBytewiseValue(SI->getValueOperand(), DL) == SplatByte;
999}
1000
1001/// mayLoopAccessLocation - Return true if the specified loop might access the
1002/// specified pointer location, which is a loop-strided access. The 'Access'
1003/// argument specifies what the verboten forms of access are (read or write).
1004///
1005/// When the access size cannot be bounded, fall back to allow stores writing
1006/// the same byte value \p SplatByte.
1008 const SCEV *BECount,
1009 const SCEV *StoreSizeSCEV, AliasAnalysis &AA,
1010 SmallPtrSetImpl<Instruction *> &IgnoredInsts,
1011 Value *SplatByte = nullptr,
1012 const DataLayout *DL = nullptr) {
1013 // Get the location that may be stored across the loop. Since the access is
1014 // strided positively through memory, we say that the modified location starts
1015 // at the pointer and has infinite size.
1017
1018 // If the loop iterates a fixed number of times, we can refine the access size
1019 // to be exactly the size of the memset, which is (BECount+1)*StoreSize
1020 const APInt *BECst, *ConstSize;
1021 if (match(BECount, m_scev_APInt(BECst)) &&
1022 match(StoreSizeSCEV, m_scev_APInt(ConstSize))) {
1023 std::optional<uint64_t> BEInt = BECst->tryZExtValue();
1024 std::optional<uint64_t> SizeInt = ConstSize->tryZExtValue();
1025 // FIXME: Should this check for overflow?
1026 if (BEInt && SizeInt)
1027 AccessSize = LocationSize::precise((*BEInt + 1) * *SizeInt);
1028 }
1029
1030 // TODO: For this to be really effective, we have to dive into the pointer
1031 // operand in the store. Store to &A[i] of 100 will always return may alias
1032 // with store of &A[100], we need to StoreLoc to be "A" with size of 100,
1033 // which will then no-alias a store to &A[100].
1034 MemoryLocation StoreLoc(Ptr, AccessSize);
1035
1036 // Only consult the same-byte-value fallback when the access size stayed
1037 // infinite (non-constant trip count); with a precise size AA is accurate.
1038 bool TrySameByteValue = !AccessSize.isPrecise() && SplatByte && DL;
1039
1040 for (BasicBlock *B : L->blocks())
1041 for (Instruction &I : *B)
1042 if (!IgnoredInsts.contains(&I) &&
1043 isModOrRefSet(AA.getModRefInfo(&I, StoreLoc) & Access)) {
1044 if (TrySameByteValue && isSameByteValueStore(I, SplatByte, L, *DL))
1045 continue;
1046 return true;
1047 }
1048 return false;
1049}
1050
1051// If we have a negative stride, Start refers to the end of the memory location
1052// we're trying to memset. Therefore, we need to recompute the base pointer,
1053// which is just Start - BECount*Size.
1054static const SCEV *getStartForNegStride(const SCEV *Start, const SCEV *BECount,
1055 Type *IntPtr, const SCEV *StoreSizeSCEV,
1056 ScalarEvolution *SE) {
1057 const SCEV *Index = SE->getTruncateOrZeroExtend(BECount, IntPtr);
1058 if (!StoreSizeSCEV->isOne()) {
1059 // index = back edge count * store size
1060 Index = SE->getMulExpr(Index,
1061 SE->getTruncateOrZeroExtend(StoreSizeSCEV, IntPtr),
1063 }
1064 // base pointer = start - index * store size
1065 return SE->getMinusSCEV(Start, Index);
1066}
1067
1068/// Compute the number of bytes as a SCEV from the backedge taken count.
1069///
1070/// This also maps the SCEV into the provided type and tries to handle the
1071/// computation in a way that will fold cleanly.
1072static const SCEV *getNumBytes(const SCEV *BECount, Type *IntPtr,
1073 const SCEV *StoreSizeSCEV, Loop *CurLoop,
1074 const DataLayout *DL, ScalarEvolution *SE) {
1075 const SCEV *TripCountSCEV =
1076 SE->getTripCountFromExitCount(BECount, IntPtr, CurLoop);
1077 return SE->getMulExpr(TripCountSCEV,
1078 SE->getTruncateOrZeroExtend(StoreSizeSCEV, IntPtr),
1080}
1081
1082/// processLoopStridedStore - We see a strided store of some value. If we can
1083/// transform this into a memset or memset_pattern in the loop preheader, do so.
1084bool LoopIdiomRecognize::processLoopStridedStore(
1085 Value *DestPtr, const SCEV *StoreSizeSCEV, MaybeAlign StoreAlignment,
1086 Value *StoredVal, Instruction *TheStore,
1088 const SCEV *BECount, bool IsNegStride, bool IsLoopMemset) {
1089 Module *M = TheStore->getModule();
1090
1091 // The trip count of the loop and the base pointer of the addrec SCEV is
1092 // guaranteed to be loop invariant, which means that it should dominate the
1093 // header. This allows us to insert code for it in the preheader.
1094 unsigned DestAS = DestPtr->getType()->getPointerAddressSpace();
1095 BasicBlock *Preheader = CurLoop->getLoopPreheader();
1096 IRBuilder<> Builder(Preheader->getTerminator());
1097 SCEVExpander Expander(*SE, "loop-idiom");
1098 SCEVExpanderCleaner ExpCleaner(Expander);
1099
1100 Type *DestInt8PtrTy = Builder.getPtrTy(DestAS);
1101 Type *IntIdxTy = DL->getIndexType(DestPtr->getType());
1102
1103 bool Changed = false;
1104 const SCEV *Start = Ev->getStart();
1105 // Handle negative strided loops.
1106 if (IsNegStride)
1107 Start = getStartForNegStride(Start, BECount, IntIdxTy, StoreSizeSCEV, SE);
1108
1109 // TODO: ideally we should still be able to generate memset if SCEV expander
1110 // is taught to generate the dependencies at the latest point.
1111 if (!Expander.isSafeToExpand(Start))
1112 return Changed;
1113
1114 // Okay, we have a strided store "p[i]" of a splattable value. We can turn
1115 // this into a memset in the loop preheader now if we want. However, this
1116 // would be unsafe to do if there is anything else in the loop that may read
1117 // or write to the aliased location. Check for any overlap by generating the
1118 // base pointer and checking the region.
1119 Value *BasePtr =
1120 Expander.expandCodeFor(Start, DestInt8PtrTy, Preheader->getTerminator());
1121
1122 // From here on out, conservatively report to the pass manager that we've
1123 // changed the IR, even if we later clean up these added instructions. There
1124 // may be structural differences e.g. in the order of use lists not accounted
1125 // for in just a textual dump of the IR. This is written as a variable, even
1126 // though statically all the places this dominates could be replaced with
1127 // 'true', with the hope that anyone trying to be clever / "more precise" with
1128 // the return value will read this comment, and leave them alone.
1129 Changed = true;
1130
1131 Value *SplatValue = isBytewiseValue(StoredVal, *DL);
1132 if (mayLoopAccessLocation(BasePtr, ModRefInfo::ModRef, CurLoop, BECount,
1133 StoreSizeSCEV, *AA, Stores, SplatValue, DL))
1134 return Changed;
1135
1136 if (avoidLIRForMultiBlockLoop(/*IsMemset=*/true, IsLoopMemset))
1137 return Changed;
1138
1139 // Okay, everything looks good, insert the memset.
1140 Constant *PatternValue = nullptr;
1141 if (!SplatValue)
1142 PatternValue = getMemSetPatternValue(StoredVal, DL);
1143
1144 // MemsetArg is the number of bytes for the memset libcall, and the number
1145 // of pattern repetitions if the memset.pattern intrinsic is being used.
1146 Value *MemsetArg;
1147 std::optional<int64_t> BytesWritten;
1148
1149 if (PatternValue && (HasMemsetPattern || ForceMemsetPatternIntrinsic)) {
1150 const SCEV *TripCountS =
1151 SE->getTripCountFromExitCount(BECount, IntIdxTy, CurLoop);
1152 if (!Expander.isSafeToExpand(TripCountS))
1153 return Changed;
1154 const SCEVConstant *ConstStoreSize = dyn_cast<SCEVConstant>(StoreSizeSCEV);
1155 if (!ConstStoreSize)
1156 return Changed;
1157 Value *TripCount = Expander.expandCodeFor(TripCountS, IntIdxTy,
1158 Preheader->getTerminator());
1159 uint64_t PatternRepsPerTrip =
1160 (ConstStoreSize->getValue()->getZExtValue() * 8) /
1161 DL->getTypeSizeInBits(PatternValue->getType());
1162 // If ConstStoreSize is not equal to the width of PatternValue, then
1163 // MemsetArg is TripCount * (ConstStoreSize/PatternValueWidth). Else
1164 // MemSetArg is just TripCount.
1165 MemsetArg =
1166 PatternRepsPerTrip == 1
1167 ? TripCount
1168 : Builder.CreateMul(TripCount,
1169 Builder.getIntN(IntIdxTy->getIntegerBitWidth(),
1170 PatternRepsPerTrip));
1171 if (auto *CI = dyn_cast<ConstantInt>(TripCount))
1172 BytesWritten =
1173 CI->getZExtValue() * ConstStoreSize->getValue()->getZExtValue();
1174
1175 } else {
1176 const SCEV *NumBytesS =
1177 getNumBytes(BECount, IntIdxTy, StoreSizeSCEV, CurLoop, DL, SE);
1178
1179 // TODO: ideally we should still be able to generate memset if SCEV expander
1180 // is taught to generate the dependencies at the latest point.
1181 if (!Expander.isSafeToExpand(NumBytesS))
1182 return Changed;
1183 MemsetArg =
1184 Expander.expandCodeFor(NumBytesS, IntIdxTy, Preheader->getTerminator());
1185 if (auto *CI = dyn_cast<ConstantInt>(MemsetArg))
1186 BytesWritten = CI->getZExtValue();
1187 }
1188 assert(MemsetArg && "MemsetArg should have been set");
1189
1190 AAMDNodes AATags = TheStore->getAAMetadata();
1191 for (Instruction *Store : Stores)
1192 AATags = AATags.merge(Store->getAAMetadata());
1193 if (BytesWritten)
1194 AATags = AATags.extendTo(BytesWritten.value());
1195 else
1196 AATags = AATags.extendTo(-1);
1197
1198 CallInst *NewCall;
1199 if (SplatValue) {
1200 NewCall = Builder.CreateMemSet(BasePtr, SplatValue, MemsetArg,
1201 MaybeAlign(StoreAlignment),
1202 /*isVolatile=*/false, AATags);
1203 } else if (ForceMemsetPatternIntrinsic ||
1204 isLibFuncEmittable(M, TLI, LibFunc_memset_pattern16)) {
1205 assert(isa<SCEVConstant>(StoreSizeSCEV) && "Expected constant store size");
1206
1207 NewCall = Builder.CreateIntrinsicWithoutFolding(
1208 Intrinsic::experimental_memset_pattern,
1209 {DestInt8PtrTy, PatternValue->getType(), IntIdxTy},
1210 {BasePtr, PatternValue, MemsetArg,
1211 ConstantInt::getFalse(M->getContext())});
1212 if (StoreAlignment)
1213 cast<MemSetPatternInst>(NewCall)->setDestAlignment(*StoreAlignment);
1214 NewCall->setAAMetadata(AATags);
1215 } else {
1216 // Neither a memset, nor memset_pattern16
1217 return Changed;
1218 }
1219
1220 NewCall->setDebugLoc(TheStore->getDebugLoc());
1221
1222 if (MSSAU) {
1223 MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB(
1224 NewCall, nullptr, NewCall->getParent(), MemorySSA::BeforeTerminator);
1225 MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true);
1226 }
1227
1228 LLVM_DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n"
1229 << " from store to: " << *Ev << " at: " << *TheStore
1230 << "\n");
1231
1232 ORE.emit([&]() {
1233 OptimizationRemark R(DEBUG_TYPE, "ProcessLoopStridedStore",
1234 NewCall->getDebugLoc(), Preheader);
1235 R << "Transformed loop-strided store in "
1236 << ore::NV("Function", TheStore->getFunction())
1237 << " function into a call to "
1238 << ore::NV("NewFunction", NewCall->getCalledFunction())
1239 << "() intrinsic";
1240 if (!Stores.empty())
1241 R << ore::setExtraArgs();
1242 for (auto *I : Stores) {
1243 R << ore::NV("FromBlock", I->getParent()->getName())
1244 << ore::NV("ToBlock", Preheader->getName());
1245 }
1246 return R;
1247 });
1248
1249 // Okay, the memset has been formed. Zap the original store and anything that
1250 // feeds into it.
1251 for (auto *I : Stores) {
1252 if (MSSAU)
1253 MSSAU->removeMemoryAccess(I, true);
1255 }
1256 if (MSSAU && VerifyMemorySSA)
1257 MSSAU->getMemorySSA()->verifyMemorySSA();
1258 ++NumMemSet;
1259 ExpCleaner.markResultUsed();
1260 return true;
1261}
1262
1263/// If the stored value is a strided load in the same loop with the same stride
1264/// this may be transformable into a memcpy. This kicks in for stuff like
1265/// for (i) A[i] = B[i];
1266bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(StoreInst *SI,
1267 const SCEV *BECount) {
1268 assert(SI->isUnordered() && "Expected only non-volatile non-ordered stores.");
1269
1270 Value *StorePtr = SI->getPointerOperand();
1271 const SCEVAddRecExpr *StoreEv = cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
1272 unsigned StoreSize = DL->getTypeStoreSize(SI->getValueOperand()->getType());
1273
1274 // The store must be feeding a non-volatile load.
1275 LoadInst *LI = cast<LoadInst>(SI->getValueOperand());
1276 assert(LI->isUnordered() && "Expected only non-volatile non-ordered loads.");
1277
1278 // See if the pointer expression is an AddRec like {base,+,1} on the current
1279 // loop, which indicates a strided load. If we have something else, it's a
1280 // random load we can't handle.
1281 Value *LoadPtr = LI->getPointerOperand();
1282 const SCEVAddRecExpr *LoadEv = cast<SCEVAddRecExpr>(SE->getSCEV(LoadPtr));
1283
1284 const SCEV *StoreSizeSCEV = SE->getConstant(StorePtr->getType(), StoreSize);
1285 return processLoopStoreOfLoopLoad(StorePtr, LoadPtr, StoreSizeSCEV,
1286 SI->getAlign(), LI->getAlign(), SI, LI,
1287 StoreEv, LoadEv, BECount);
1288}
1289
1290namespace {
1291class MemmoveVerifier {
1292public:
1293 explicit MemmoveVerifier(const Value &LoadBasePtr, const Value &StoreBasePtr,
1294 const DataLayout &DL)
1296 LoadBasePtr.stripPointerCasts(), LoadOff, DL)),
1298 StoreBasePtr.stripPointerCasts(), StoreOff, DL)),
1299 IsSameObject(BP1 == BP2) {}
1300
1301 bool loadAndStoreMayFormMemmove(unsigned StoreSize, bool IsNegStride,
1302 const Instruction &TheLoad,
1303 bool IsMemCpy) const {
1304 if (IsMemCpy) {
1305 // Ensure that LoadBasePtr is after StoreBasePtr or before StoreBasePtr
1306 // for negative stride.
1307 if ((!IsNegStride && LoadOff <= StoreOff) ||
1308 (IsNegStride && LoadOff >= StoreOff))
1309 return false;
1310 } else {
1311 // Ensure that LoadBasePtr is after StoreBasePtr or before StoreBasePtr
1312 // for negative stride. LoadBasePtr shouldn't overlap with StoreBasePtr.
1313 int64_t LoadSize =
1314 DL.getTypeSizeInBits(TheLoad.getType()).getFixedValue() / 8;
1315 if (BP1 != BP2 || LoadSize != int64_t(StoreSize))
1316 return false;
1317 if ((!IsNegStride && LoadOff < StoreOff + int64_t(StoreSize)) ||
1318 (IsNegStride && LoadOff + LoadSize > StoreOff))
1319 return false;
1320 }
1321 return true;
1322 }
1323
1324private:
1325 const DataLayout &DL;
1326 int64_t LoadOff = 0;
1327 int64_t StoreOff = 0;
1328 const Value *BP1;
1329 const Value *BP2;
1330
1331public:
1332 const bool IsSameObject;
1333};
1334} // namespace
1335
1336bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(
1337 Value *DestPtr, Value *SourcePtr, const SCEV *StoreSizeSCEV,
1338 MaybeAlign StoreAlign, MaybeAlign LoadAlign, Instruction *TheStore,
1339 Instruction *TheLoad, const SCEVAddRecExpr *StoreEv,
1340 const SCEVAddRecExpr *LoadEv, const SCEV *BECount) {
1341
1342 // FIXME: until llvm.memcpy.inline supports dynamic sizes, we need to
1343 // conservatively bail here, since otherwise we may have to transform
1344 // llvm.memcpy.inline into llvm.memcpy which is illegal.
1345 if (auto *MCI = dyn_cast<MemCpyInst>(TheStore); MCI && MCI->isForceInlined())
1346 return false;
1347
1348 // The trip count of the loop and the base pointer of the addrec SCEV is
1349 // guaranteed to be loop invariant, which means that it should dominate the
1350 // header. This allows us to insert code for it in the preheader.
1351 BasicBlock *Preheader = CurLoop->getLoopPreheader();
1352 IRBuilder<> Builder(Preheader->getTerminator());
1353 SCEVExpander Expander(*SE, "loop-idiom");
1354
1355 SCEVExpanderCleaner ExpCleaner(Expander);
1356
1357 bool Changed = false;
1358 const SCEV *StrStart = StoreEv->getStart();
1359 unsigned StrAS = DestPtr->getType()->getPointerAddressSpace();
1360 Type *IntIdxTy = Builder.getIntNTy(DL->getIndexSizeInBits(StrAS));
1361
1362 APInt Stride = getStoreStride(StoreEv);
1363 const SCEVConstant *ConstStoreSize = dyn_cast<SCEVConstant>(StoreSizeSCEV);
1364
1365 // TODO: Deal with non-constant size; Currently expect constant store size
1366 assert(ConstStoreSize && "store size is expected to be a constant");
1367
1368 int64_t StoreSize = ConstStoreSize->getValue()->getZExtValue();
1369 bool IsNegStride = StoreSize == -Stride;
1370
1371 // Handle negative strided loops.
1372 if (IsNegStride)
1373 StrStart =
1374 getStartForNegStride(StrStart, BECount, IntIdxTy, StoreSizeSCEV, SE);
1375
1376 // Okay, we have a strided store "p[i]" of a loaded value. We can turn
1377 // this into a memcpy in the loop preheader now if we want. However, this
1378 // would be unsafe to do if there is anything else in the loop that may read
1379 // or write the memory region we're storing to. This includes the load that
1380 // feeds the stores. Check for an alias by generating the base address and
1381 // checking everything.
1382 Value *StoreBasePtr = Expander.expandCodeFor(
1383 StrStart, Builder.getPtrTy(StrAS), Preheader->getTerminator());
1384
1385 // From here on out, conservatively report to the pass manager that we've
1386 // changed the IR, even if we later clean up these added instructions. There
1387 // may be structural differences e.g. in the order of use lists not accounted
1388 // for in just a textual dump of the IR. This is written as a variable, even
1389 // though statically all the places this dominates could be replaced with
1390 // 'true', with the hope that anyone trying to be clever / "more precise" with
1391 // the return value will read this comment, and leave them alone.
1392 Changed = true;
1393
1394 SmallPtrSet<Instruction *, 2> IgnoredInsts;
1395 IgnoredInsts.insert(TheStore);
1396
1397 bool IsMemCpy = isa<MemCpyInst>(TheStore);
1398 const StringRef InstRemark = IsMemCpy ? "memcpy" : "load and store";
1399
1400 bool LoopAccessStore =
1401 mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop, BECount,
1402 StoreSizeSCEV, *AA, IgnoredInsts);
1403 if (LoopAccessStore) {
1404 // For memmove case it's not enough to guarantee that loop doesn't access
1405 // TheStore and TheLoad. Additionally we need to make sure that TheStore is
1406 // the only user of TheLoad.
1407 if (!TheLoad->hasOneUse())
1408 return Changed;
1409 IgnoredInsts.insert(TheLoad);
1410 if (mayLoopAccessLocation(StoreBasePtr, ModRefInfo::ModRef, CurLoop,
1411 BECount, StoreSizeSCEV, *AA, IgnoredInsts)) {
1412 ORE.emit([&]() {
1413 return OptimizationRemarkMissed(DEBUG_TYPE, "LoopMayAccessStore",
1414 TheStore)
1415 << ore::NV("Inst", InstRemark) << " in "
1416 << ore::NV("Function", TheStore->getFunction())
1417 << " function will not be hoisted: "
1418 << ore::NV("Reason", "The loop may access store location");
1419 });
1420 return Changed;
1421 }
1422 IgnoredInsts.erase(TheLoad);
1423 }
1424
1425 const SCEV *LdStart = LoadEv->getStart();
1426 unsigned LdAS = SourcePtr->getType()->getPointerAddressSpace();
1427
1428 // Handle negative strided loops.
1429 if (IsNegStride)
1430 LdStart =
1431 getStartForNegStride(LdStart, BECount, IntIdxTy, StoreSizeSCEV, SE);
1432
1433 // For a memcpy, we have to make sure that the input array is not being
1434 // mutated by the loop.
1435 Value *LoadBasePtr = Expander.expandCodeFor(LdStart, Builder.getPtrTy(LdAS),
1436 Preheader->getTerminator());
1437
1438 // If the store is a memcpy instruction, we must check if it will write to
1439 // the load memory locations. So remove it from the ignored stores.
1440 MemmoveVerifier Verifier(*LoadBasePtr, *StoreBasePtr, *DL);
1441 if (IsMemCpy && !Verifier.IsSameObject)
1442 IgnoredInsts.erase(TheStore);
1443 if (mayLoopAccessLocation(LoadBasePtr, ModRefInfo::Mod, CurLoop, BECount,
1444 StoreSizeSCEV, *AA, IgnoredInsts)) {
1445 ORE.emit([&]() {
1446 return OptimizationRemarkMissed(DEBUG_TYPE, "LoopMayAccessLoad", TheLoad)
1447 << ore::NV("Inst", InstRemark) << " in "
1448 << ore::NV("Function", TheStore->getFunction())
1449 << " function will not be hoisted: "
1450 << ore::NV("Reason", "The loop may access load location");
1451 });
1452 return Changed;
1453 }
1454
1455 bool IsAtomic = TheStore->isAtomic() || TheLoad->isAtomic();
1456 bool UseMemMove = IsMemCpy ? Verifier.IsSameObject : LoopAccessStore;
1457
1458 if (IsAtomic) {
1459 // For now don't support unordered atomic memmove.
1460 if (UseMemMove)
1461 return Changed;
1462
1463 // We cannot allow unaligned ops for unordered load/store, so reject
1464 // anything where the alignment isn't at least the element size.
1465 assert((StoreAlign && LoadAlign) &&
1466 "Expect unordered load/store to have align.");
1467 if (*StoreAlign < StoreSize || *LoadAlign < StoreSize)
1468 return Changed;
1469
1470 // If the element.atomic memcpy is not lowered into explicit
1471 // loads/stores later, then it will be lowered into an element-size
1472 // specific lib call. If the lib call doesn't exist for our store size, then
1473 // we shouldn't generate the memcpy.
1474 if (StoreSize > TTI->getAtomicMemIntrinsicMaxElementSize())
1475 return Changed;
1476 }
1477
1478 if (UseMemMove)
1479 if (!Verifier.loadAndStoreMayFormMemmove(StoreSize, IsNegStride, *TheLoad,
1480 IsMemCpy))
1481 return Changed;
1482
1483 if (avoidLIRForMultiBlockLoop())
1484 return Changed;
1485
1486 // Okay, everything is safe, we can transform this!
1487
1488 const SCEV *NumBytesS =
1489 getNumBytes(BECount, IntIdxTy, StoreSizeSCEV, CurLoop, DL, SE);
1490
1491 Value *NumBytes =
1492 Expander.expandCodeFor(NumBytesS, IntIdxTy, Preheader->getTerminator());
1493
1494 AAMDNodes AATags = TheLoad->getAAMetadata();
1495 AAMDNodes StoreAATags = TheStore->getAAMetadata();
1496 AATags = AATags.merge(StoreAATags);
1497 if (auto CI = dyn_cast<ConstantInt>(NumBytes))
1498 AATags = AATags.extendTo(CI->getZExtValue());
1499 else
1500 AATags = AATags.extendTo(-1);
1501
1502 CallInst *NewCall = nullptr;
1503 // Check whether to generate an unordered atomic memcpy:
1504 // If the load or store are atomic, then they must necessarily be unordered
1505 // by previous checks.
1506 if (!IsAtomic) {
1507 if (UseMemMove)
1508 NewCall = Builder.CreateMemMove(StoreBasePtr, StoreAlign, LoadBasePtr,
1509 LoadAlign, NumBytes,
1510 /*isVolatile=*/false, AATags);
1511 else
1512 NewCall =
1513 Builder.CreateMemCpy(StoreBasePtr, StoreAlign, LoadBasePtr, LoadAlign,
1514 NumBytes, /*isVolatile=*/false, AATags);
1515 } else {
1516 // Create the call.
1517 // Note that unordered atomic loads/stores are *required* by the spec to
1518 // have an alignment but non-atomic loads/stores may not.
1519 NewCall = Builder.CreateElementUnorderedAtomicMemCpy(
1520 StoreBasePtr, *StoreAlign, LoadBasePtr, *LoadAlign, NumBytes, StoreSize,
1521 AATags);
1522 }
1523 NewCall->setDebugLoc(TheStore->getDebugLoc());
1524
1525 if (MSSAU) {
1526 MemoryAccess *NewMemAcc = MSSAU->createMemoryAccessInBB(
1527 NewCall, nullptr, NewCall->getParent(), MemorySSA::BeforeTerminator);
1528 MSSAU->insertDef(cast<MemoryDef>(NewMemAcc), true);
1529 }
1530
1531 LLVM_DEBUG(dbgs() << " Formed new call: " << *NewCall << "\n"
1532 << " from load ptr=" << *LoadEv << " at: " << *TheLoad
1533 << "\n"
1534 << " from store ptr=" << *StoreEv << " at: " << *TheStore
1535 << "\n");
1536
1537 ORE.emit([&]() {
1538 return OptimizationRemark(DEBUG_TYPE, "ProcessLoopStoreOfLoopLoad",
1539 NewCall->getDebugLoc(), Preheader)
1540 << "Formed a call to "
1541 << ore::NV("NewFunction", NewCall->getCalledFunction())
1542 << "() intrinsic from " << ore::NV("Inst", InstRemark)
1543 << " instruction in " << ore::NV("Function", TheStore->getFunction())
1544 << " function"
1546 << ore::NV("FromBlock", TheStore->getParent()->getName())
1547 << ore::NV("ToBlock", Preheader->getName());
1548 });
1549
1550 // Okay, a new call to memcpy/memmove has been formed. Zap the original store
1551 // and anything that feeds into it.
1552 if (MSSAU)
1553 MSSAU->removeMemoryAccess(TheStore, true);
1554 deleteDeadInstruction(TheStore);
1555 if (MSSAU && VerifyMemorySSA)
1556 MSSAU->getMemorySSA()->verifyMemorySSA();
1557 if (UseMemMove)
1558 ++NumMemMove;
1559 else
1560 ++NumMemCpy;
1561 ExpCleaner.markResultUsed();
1562 return true;
1563}
1564
1565// When compiling for codesize we avoid idiom recognition for a multi-block loop
1566// unless it is a loop_memset idiom or a memset/memcpy idiom in a nested loop.
1567//
1568bool LoopIdiomRecognize::avoidLIRForMultiBlockLoop(bool IsMemset,
1569 bool IsLoopMemset) {
1570 if (ApplyCodeSizeHeuristics && CurLoop->getNumBlocks() > 1) {
1571 if (CurLoop->isOutermost() && (!IsMemset || !IsLoopMemset)) {
1572 LLVM_DEBUG(dbgs() << " " << CurLoop->getHeader()->getParent()->getName()
1573 << " : LIR " << (IsMemset ? "Memset" : "Memcpy")
1574 << " avoided: multi-block top-level loop\n");
1575 return true;
1576 }
1577 }
1578
1579 return false;
1580}
1581
1582bool LoopIdiomRecognize::optimizeCRCLoop(const PolynomialInfo &Info) {
1583 // FIXME: Hexagon has a special HexagonLoopIdiom that optimizes CRC using
1584 // carry-less multiplication instructions, which is more efficient than our
1585 // Sarwate table-lookup optimization. Hence, until we're able to emit
1586 // target-specific instructions for Hexagon, subsuming HexagonLoopIdiom,
1587 // disable the optimization for Hexagon.
1588 Module &M = *CurLoop->getHeader()->getModule();
1589 Triple TT(M.getTargetTriple());
1590 if (TT.getArch() == Triple::hexagon)
1591 return false;
1592
1593 // In the clmul optimization, the first clmul uses 2*TC bits, and the second
1594 // clmul uses CRCBW+TC bits. For simplicity, have both clmuls operate on the
1595 // same bit width.
1596 unsigned CRCBW = Info.LHS->getType()->getIntegerBitWidth();
1597 unsigned ClmulBW = std::max(2 * Info.TripCount, CRCBW + Info.TripCount);
1598 auto *ClmulTy = IntegerType::get(Info.LHS->getContext(), ClmulBW);
1599
1600 // The force-crc-clmul flag should cause the clmul optimization to run
1601 // unconditionally.
1602 if (ForceCRCClmul) {
1603 optimizeCRCLoopUsingClmul(Info, ClmulTy);
1604 return true;
1605 }
1606
1607 // FIXME: Once intrinsic cost modeling is more reliable for clmul, that should
1608 // be used to determine which optimization to use. Until then, only apply the
1609 // clmul optimization when optimizing for size, since a lookup table is not
1610 // viable in that case.
1611 if (!ApplyCodeSizeHeuristics && Info.TripCount % 8 == 0) {
1612 optimizeCRCLoopUsingTableLookup(Info);
1613 return true;
1614 }
1615
1616 // The clmul optimization should only be applied if clmul with the required
1617 // bit width is a fast operation on the target.
1618 // TODO: If clmul exists on the target but not for the required width, it
1619 // might be possible to split into multiple iterations of reduction.
1620 if (TTI->haveFastClmul(ClmulTy)) {
1621 optimizeCRCLoopUsingClmul(Info, ClmulTy);
1622 return true;
1623 }
1624
1625 return false;
1626}
1627
1628// The algorithm used in this optimization is a Polynomial (GF(2)) Barrett
1629// Reduction based on Intel's "Fast CRC Computation for Generic Polynomials
1630// Using PCLMULQDQ Instruction" white paper (December 2009).
1631void LoopIdiomRecognize::optimizeCRCLoopUsingClmul(const PolynomialInfo &Info,
1632 IntegerType *ClmulTy) {
1633 Type *CRCTy = Info.LHS->getType();
1634 LLVMContext &Ctx = CRCTy->getContext();
1635 unsigned CRCBW = CRCTy->getIntegerBitWidth();
1636 // The loop's TripCount determines how many bits of the data are processed,
1637 // regardless of whether the actual data bit width matches (if auxiliary data
1638 // is even used at all).
1639 unsigned TC = Info.TripCount;
1640 unsigned ClmulBW = ClmulTy->getBitWidth();
1641
1642 // First, generate the constants required for GF(2) Barrett reduction.
1643 auto [Mu, FullGenPoly] = HashRecognize::genBarrettConstants(Info);
1644 Value *MuConst = ConstantInt::get(Ctx, Mu.zext(ClmulBW));
1645 Value *GenPolyConst = ConstantInt::get(Ctx, FullGenPoly.zext(ClmulBW));
1646
1647 IRBuilder<> Builder(CurLoop->getLoopPreheader()->getTerminator());
1648
1649 auto LoTCBits = [TC, &Builder, &Ctx](Value *Op, const Twine &Name) {
1650 unsigned OpBW = Op->getType()->getIntegerBitWidth();
1651 assert(OpBW >= TC && "Bit width should be at least TripCount");
1652 auto *Mask = ConstantInt::get(Ctx, APInt::getLowBitsSet(OpBW, TC));
1653 return Builder.CreateAnd(Op, Mask, Name);
1654 };
1655
1656 Value *LHS = Builder.CreateZExt(Info.LHS, ClmulTy, "crc.cast");
1657
1658 // Based on the Intel white paper, in our case, we have
1659 // R(x) = (LHS*x^TC) xor (LHSAux ? getTCBits(LHSAux)*x^CRCBW : 0)
1660 // since the CRC loop multiplies LHS by x each iteration, and the x^CRCBW term
1661 // of getTCBits(LHSAux) is XORed in for the significant bit check.
1662 // Rather than compute the full R(x), we can split it in two: a quotient for
1663 // step 1 (floor(R(x)/x^CRCBW)) and a remainder for step 3 (R(x) mod x^CRCBW).
1664 //
1665 // ClmulMuInput is an evolving variable that will eventually become the part
1666 // used in step 1, which can be simplified to
1667 // (LHS*x^(TC-CRCBW)) xor (LHSAux ? getTCBits(LHSAux) : 0).
1668 // Thanks to restrictions imposed by HashRecognize for big-endian CRC loops,
1669 // getTCBits(LHSAux) = LHSAux*x^(TC-CRCBW), so this can be further simplified
1670 // to (LHS xor (LHSAux ? LHSAux : 0))*x^(TC-CRCBW).
1671 Value *ClmulMuInput = LHS;
1672
1673 // If auxiliary data is present, XOR it in with the CRC.
1674 if (Value *Data = Info.LHSAux) {
1675 // This is usually a zext, but DataBW may exceed ClmulBW if both CRCBW and
1676 // TC are small enough.
1677 Data = Builder.CreateZExtOrTrunc(Data, ClmulTy, "data.cast");
1678
1679 ClmulMuInput = Builder.CreateXor(ClmulMuInput, Data, "xor.crc.data");
1680 }
1681
1682 // Align the current CRC with TripCount (multiply or divide by x^(TC-CRCBW)).
1683 if (Info.IsBigEndian && TC != CRCBW) {
1684 ClmulMuInput =
1685 TC > CRCBW
1686 ? Builder.CreateShl(ClmulMuInput, TC - CRCBW, "crc.align.tc")
1687 : Builder.CreateLShr(ClmulMuInput, CRCBW - TC, "crc.align.tc");
1688 }
1689
1690 // Zero out any bits above (TC-1) for calculation since the original loop
1691 // doesn't use them in the significant bit checks.
1692 ClmulMuInput = LoTCBits(ClmulMuInput, "crc.tcbits");
1693
1694 // Step 1: T1(x) = floor(R(x)/x^CRCBW) * mu
1695 // Input is TC bits and mu is TC+1 bits, so result will be 2*TC bits.
1696 Value *ClmulMu = Builder.CreateBinaryIntrinsic(
1697 Intrinsic::clmul, ClmulMuInput, MuConst, /*FMFSource=*/{}, "clmul.mu");
1698
1699 // Calculate floor(T1(x)/x^TC) for step 2.
1700 Value *ClmulGPInput = Info.IsBigEndian
1701 ? Builder.CreateLShr(ClmulMu, TC, "quot.lshr")
1702 : LoTCBits(ClmulMu, "quot.mask");
1703
1704 // Step 2: T2(x) = floor(T1(x)/x^TC) * P(x)
1705 // Input is TC bits and P(x) is CRCBW+1 bits, so result will be CRCBW+TC bits.
1706 Value *ClmulGP = Builder.CreateBinaryIntrinsic(Intrinsic::clmul, ClmulGPInput,
1707 GenPolyConst,
1708 /*FMFSource=*/{}, "clmul.gp");
1709
1710 // Calculate the least significant part of R(x) for step 3 as specified above.
1711 // R(x) mod x^CRCBW = LHS*x^TC mod x^CRCBW, though the (mod x^CRCBW) is
1712 // handled later on when truncating back to CRCBW for ComputedValue.
1713 Value *CRCAlignClmul =
1714 Info.IsBigEndian ? Builder.CreateShl(LHS, TC, "crc.shl") : LHS;
1715
1716 // Step 3: C(x) = (R(x) xor T2(x)) mod x^CRCBW
1717 Value *CRCNext = Builder.CreateXor(CRCAlignClmul, ClmulGP, "xor.crc.mult");
1718 if (!Info.IsBigEndian)
1719 CRCNext = Builder.CreateLShr(CRCNext, TC, "crc.lshr");
1720
1721 // Bring the result back down the the CRC bit width.
1722 CRCNext = Builder.CreateTrunc(CRCNext, CRCTy, "crc.next");
1723
1724 // Replace the result of the loop with the new computed CRC value.
1725 Info.ComputedValue->replaceUsesOutsideBlock(CRCNext, CurLoop->getLoopLatch());
1726
1727 // Finally, clean up the loop as much as possible so it can be trivially
1728 // deleted.
1729 {
1730 for (PHINode &PN : make_early_inc_range(CurLoop->getHeader()->phis())) {
1731 PN.replaceAllUsesWith(PoisonValue::get(PN.getType()));
1733 }
1734 // Replace the exit condition with constant true/false to always cause a
1735 // branch to the exit block.
1737 auto *BrInst = cast<CondBrInst>(CurLoop->getLoopLatch()->getTerminator());
1738 BrInst->setCondition(ConstantInt::getBool(
1739 Ctx, BrInst->getSuccessor(0) == CurLoop->getExitBlock()));
1740 SE->forgetLoop(CurLoop);
1741 }
1742}
1743
1744void LoopIdiomRecognize::optimizeCRCLoopUsingTableLookup(
1745 const PolynomialInfo &Info) {
1746 assert(Info.TripCount % 8 == 0 && "A byte-multiple trip count is required");
1747
1748 // First, create a new GlobalVariable corresponding to the
1749 // Sarwate-lookup-table.
1750 Type *CRCTy = Info.LHS->getType();
1751 unsigned CRCBW = CRCTy->getIntegerBitWidth();
1752 std::array<Constant *, 256> CRCConstants;
1754 CRCConstants.begin(),
1755 [CRCTy](const APInt &E) { return ConstantInt::get(CRCTy, E); });
1756 Constant *ConstArray =
1757 ConstantArray::get(ArrayType::get(CRCTy, 256), CRCConstants);
1759 *CurLoop->getHeader()->getModule(), ConstArray->getType(), true,
1760 GlobalValue::PrivateLinkage, ConstArray, ".crctable");
1761
1764
1765 // Next, mark all PHIs for removal except IV.
1766 {
1767 for (PHINode &PN : CurLoop->getHeader()->phis()) {
1768 if (&PN == IV)
1769 continue;
1770 PN.replaceAllUsesWith(PoisonValue::get(PN.getType()));
1771 Cleanup.push_back(&PN);
1772 }
1773 }
1774
1775 // Next, fix up the trip count.
1776 {
1777 unsigned NewBTC = (Info.TripCount / 8) - 1;
1778 BasicBlock *LoopBlk = CurLoop->getLoopLatch();
1779 CondBrInst *BrInst = cast<CondBrInst>(LoopBlk->getTerminator());
1780 CmpPredicate ExitPred = BrInst->getSuccessor(0) == LoopBlk
1783 Instruction *ExitCond = CurLoop->getLatchCmpInst();
1784 Value *ExitLimit = ConstantInt::get(IV->getType(), NewBTC);
1785 IRBuilder<> Builder(ExitCond);
1786 Value *NewExitCond =
1787 Builder.CreateICmp(ExitPred, IV, ExitLimit, "exit.cond");
1788 ExitCond->replaceAllUsesWith(NewExitCond);
1789 deleteDeadInstruction(ExitCond);
1790 }
1791
1792 // Finally, fill the loop with the Sarwate-table-lookup logic, and replace all
1793 // uses of ComputedValue.
1794 //
1795 // Little-endian:
1796 // crc = (crc >> 8) ^ tbl[(iv'th byte of data) ^ (bottom byte of crc)]
1797 // Big-Endian:
1798 // crc = (crc << 8) ^ tbl[(iv'th byte of data) ^ (top byte of crc)]
1799 {
1800 auto LoByte = [](IRBuilderBase &Builder, Value *Op, const Twine &Name) {
1801 return Builder.CreateZExtOrTrunc(
1802 Op, IntegerType::getInt8Ty(Op->getContext()), Name);
1803 };
1804 auto HiIdx = [LoByte, CRCBW](IRBuilderBase &Builder, Value *Op,
1805 const Twine &Name) {
1806 Type *OpTy = Op->getType();
1807
1808 // When the bitwidth of the CRC mismatches the Op's bitwidth, we need to
1809 // use the CRC's bitwidth as the reference for shifting right.
1810 return LoByte(Builder,
1811 CRCBW > 8 ? Builder.CreateLShr(
1812 Op, ConstantInt::get(OpTy, CRCBW - 8), Name)
1813 : Op,
1814 Name + ".lo.byte");
1815 };
1816
1817 IRBuilder<> Builder(CurLoop->getHeader(),
1818 CurLoop->getHeader()->getFirstNonPHIIt());
1819
1820 // Create the CRC PHI, and initialize its incoming value to the initial
1821 // value of CRC.
1822 PHINode *CRCPhi = Builder.CreatePHI(CRCTy, 2, "crc");
1823 CRCPhi->addIncoming(Info.LHS, CurLoop->getLoopPreheader());
1824
1825 // CRC is now an evolving variable, initialized to the PHI.
1826 Value *CRC = CRCPhi;
1827
1828 // TableIndexer = ((top|bottom) byte of CRC). It is XOR'ed with (iv'th byte
1829 // of LHSAux), if LHSAux is non-nullptr.
1830 Value *Indexer = CRC;
1831 if (Value *Data = Info.LHSAux) {
1832 Type *DataTy = Data->getType();
1833
1834 // To index into the (iv'th byte of LHSAux), we multiply iv by 8, and we
1835 // shift right by that amount, and take the lo-byte (in the little-endian
1836 // case), or shift left by that amount, and take the hi-idx (in the
1837 // big-endian case).
1838 Value *IVBits = Builder.CreateZExtOrTrunc(
1839 Builder.CreateShl(IV, 3, "iv.bits"), DataTy, "iv.indexer");
1840 Value *DataIndexer =
1841 Info.IsBigEndian ? Builder.CreateShl(Data, IVBits, "data.indexer")
1842 : Builder.CreateLShr(Data, IVBits, "data.indexer");
1843 Indexer = Builder.CreateXor(
1844 DataIndexer,
1845 Builder.CreateZExtOrTrunc(Indexer, DataTy, "crc.indexer.cast"),
1846 "crc.data.indexer");
1847 }
1848
1849 Indexer = Info.IsBigEndian ? HiIdx(Builder, Indexer, "indexer.hi")
1850 : LoByte(Builder, Indexer, "indexer.lo");
1851
1852 // Always index into a GEP using the index type.
1853 Indexer = Builder.CreateZExt(
1854 Indexer, SE->getDataLayout().getIndexType(GV->getType()),
1855 "indexer.ext");
1856
1857 // CRCTableLd = CRCTable[(iv'th byte of data) ^ (top|bottom) byte of CRC].
1858 Value *CRCTableGEP =
1859 Builder.CreateInBoundsGEP(CRCTy, GV, Indexer, "tbl.ptradd");
1860 Value *CRCTableLd = Builder.CreateLoad(CRCTy, CRCTableGEP, "tbl.ld");
1861
1862 // CRCNext = (CRC (<<|>>) 8) ^ CRCTableLd, or simply CRCTableLd in case of
1863 // CRC-8.
1864 Value *CRCNext = CRCTableLd;
1865 if (CRCBW > 8) {
1866 Value *CRCShift = Info.IsBigEndian
1867 ? Builder.CreateShl(CRC, 8, "crc.be.shift")
1868 : Builder.CreateLShr(CRC, 8, "crc.le.shift");
1869 CRCNext = Builder.CreateXor(CRCShift, CRCTableLd, "crc.next");
1870 }
1871
1872 // Connect the back-edge for the loop, and RAUW the ComputedValue.
1873 CRCPhi->addIncoming(CRCNext, CurLoop->getLoopLatch());
1874 Info.ComputedValue->replaceUsesOutsideBlock(CRCNext,
1875 CurLoop->getLoopLatch());
1876 }
1877
1878 // Cleanup.
1879 {
1880 for (PHINode *PN : Cleanup)
1882 SE->forgetLoop(CurLoop);
1883 }
1884}
1885
1886bool LoopIdiomRecognize::runOnNoncountableLoop() {
1887 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Scanning: F["
1888 << CurLoop->getHeader()->getParent()->getName()
1889 << "] Noncountable Loop %"
1890 << CurLoop->getHeader()->getName() << "\n");
1891
1892 return recognizePopcount() || recognizeAndInsertFFS() ||
1893 recognizeShiftUntilBitTest() || recognizeShiftUntilZero() ||
1894 recognizeShiftUntilLessThan() || recognizeAndInsertStrLen();
1895}
1896
1897/// Check if the given conditional branch is based on the comparison between
1898/// a variable and zero, and if the variable is non-zero or zero (JmpOnZero is
1899/// true), the control yields to the loop entry. If the branch matches the
1900/// behavior, the variable involved in the comparison is returned. This function
1901/// will be called to see if the precondition and postcondition of the loop are
1902/// in desirable form.
1904 bool JmpOnZero = false) {
1906 if (!Cond)
1907 return nullptr;
1908
1909 auto *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
1910 if (!CmpZero || !CmpZero->isZero())
1911 return nullptr;
1912
1913 BasicBlock *TrueSucc = BI->getSuccessor(0);
1914 BasicBlock *FalseSucc = BI->getSuccessor(1);
1915 if (JmpOnZero)
1916 std::swap(TrueSucc, FalseSucc);
1917
1918 ICmpInst::Predicate Pred = Cond->getPredicate();
1919 if ((Pred == ICmpInst::ICMP_NE && TrueSucc == LoopEntry) ||
1920 (Pred == ICmpInst::ICMP_EQ && FalseSucc == LoopEntry))
1921 return Cond->getOperand(0);
1922
1923 return nullptr;
1924}
1925
1926namespace {
1927
1928class StrlenVerifier {
1929public:
1930 explicit StrlenVerifier(const Loop *CurLoop, ScalarEvolution *SE,
1931 const TargetLibraryInfo *TLI)
1932 : CurLoop(CurLoop), SE(SE), TLI(TLI) {}
1933
1934 bool isValidStrlenIdiom() {
1935 // Give up if the loop has multiple blocks, multiple backedges, or
1936 // multiple exit blocks
1937 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1 ||
1938 !CurLoop->getUniqueExitBlock())
1939 return false;
1940
1941 // It should have a preheader and a branch instruction.
1942 BasicBlock *Preheader = CurLoop->getLoopPreheader();
1943 if (!Preheader ||
1945 return false;
1946
1947 // The loop exit must be conditioned on an icmp with 0 the null terminator.
1948 // The icmp operand has to be a load on some SSA reg that increments
1949 // by 1 in the loop.
1950 BasicBlock *LoopBody = *CurLoop->block_begin();
1951
1952 // Skip if the body is too big as it most likely is not a strlen idiom.
1953 if (!LoopBody || LoopBody->size() >= 15)
1954 return false;
1955
1956 CondBrInst *LoopTerm = dyn_cast<CondBrInst>(LoopBody->getTerminator());
1957 if (!LoopTerm)
1958 return false;
1959 Value *LoopCond = matchCondition(LoopTerm, LoopBody);
1960 if (!LoopCond)
1961 return false;
1962
1963 LoadInst *LoopLoad = dyn_cast<LoadInst>(LoopCond);
1964 if (!LoopLoad || LoopLoad->getPointerAddressSpace() != 0)
1965 return false;
1966
1967 OperandType = LoopLoad->getType();
1968 if (!OperandType || !OperandType->isIntegerTy())
1969 return false;
1970
1971 // See if the pointer expression is an AddRec with constant step a of form
1972 // ({n,+,a}) where a is the width of the char type.
1973 Value *IncPtr = LoopLoad->getPointerOperand();
1974 const SCEV *LoadEv = SE->getSCEV(IncPtr);
1975 const APInt *Step;
1976 if (!match(LoadEv,
1977 m_scev_AffineAddRec(m_SCEV(LoadBaseEv), m_scev_APInt(Step))))
1978 return false;
1979
1980 LLVM_DEBUG(dbgs() << "pointer load scev: " << *LoadEv << "\n");
1981
1982 unsigned StepSize = Step->getZExtValue();
1983
1984 // Verify that StepSize is consistent with platform char width.
1985 OpWidth = OperandType->getIntegerBitWidth();
1986 unsigned WcharSize = TLI->getWCharSize(*LoopLoad->getModule());
1987 if (OpWidth != StepSize * 8)
1988 return false;
1989 if (OpWidth != 8 && OpWidth != 16 && OpWidth != 32)
1990 return false;
1991 if (OpWidth >= 16)
1992 if (OpWidth != WcharSize * 8)
1993 return false;
1994
1995 // Scan every instruction in the loop to ensure there are no side effects.
1996 for (Instruction &I : *LoopBody)
1997 if (I.mayHaveSideEffects())
1998 return false;
1999
2000 BasicBlock *LoopExitBB = CurLoop->getExitBlock();
2001 if (!LoopExitBB)
2002 return false;
2003
2004 for (PHINode &PN : LoopExitBB->phis()) {
2005 if (!SE->isSCEVable(PN.getType()))
2006 return false;
2007
2008 const SCEV *Ev = SE->getSCEV(&PN);
2009 if (!Ev)
2010 return false;
2011
2012 LLVM_DEBUG(dbgs() << "loop exit phi scev: " << *Ev << "\n");
2013
2014 // Since we verified that the loop trip count will be a valid strlen
2015 // idiom, we can expand all lcssa phi with {n,+,1} as (n + strlen) and use
2016 // SCEVExpander materialize the loop output.
2017 const SCEVAddRecExpr *AddRecEv = dyn_cast<SCEVAddRecExpr>(Ev);
2018 if (!AddRecEv || !AddRecEv->isAffine())
2019 return false;
2020
2021 // We only want RecAddExpr with recurrence step that is constant. This
2022 // is good enough for all the idioms we want to recognize. Later we expand
2023 // and materialize the recurrence as {base,+,a} -> (base + a * strlen)
2024 if (!isa<SCEVConstant>(AddRecEv->getStepRecurrence(*SE)))
2025 return false;
2026 }
2027
2028 return true;
2029 }
2030
2031public:
2032 const Loop *CurLoop;
2033 ScalarEvolution *SE;
2034 const TargetLibraryInfo *TLI;
2035
2036 unsigned OpWidth;
2037 ConstantInt *StepSizeCI;
2038 const SCEV *LoadBaseEv;
2040};
2041
2042} // namespace
2043
2044/// The Strlen Idiom we are trying to detect has the following structure
2045///
2046/// preheader:
2047/// ...
2048/// br label %body, ...
2049///
2050/// body:
2051/// ... ; %0 is incremented by a gep
2052/// %1 = load i8, ptr %0, align 1
2053/// %2 = icmp eq i8 %1, 0
2054/// br i1 %2, label %exit, label %body
2055///
2056/// exit:
2057/// %lcssa = phi [%0, %body], ...
2058///
2059/// We expect the strlen idiom to have a load of a character type that
2060/// is compared against '\0', and such load pointer operand must have scev
2061/// expression of the form {%str,+,c} where c is a ConstantInt of the
2062/// appropiate character width for the idiom, and %str is the base of the string
2063/// And, that all lcssa phis have the form {...,+,n} where n is a constant,
2064///
2065/// When transforming the output of the strlen idiom, the lccsa phi are
2066/// expanded using SCEVExpander as {base scev,+,a} -> (base scev + a * strlen)
2067/// and all subsequent uses are replaced. For example,
2068///
2069/// \code{.c}
2070/// const char* base = str;
2071/// while (*str != '\0')
2072/// ++str;
2073/// size_t result = str - base;
2074/// \endcode
2075///
2076/// will be transformed as follows: The idiom will be replaced by a strlen
2077/// computation to compute the address of the null terminator of the string.
2078///
2079/// \code{.c}
2080/// const char* base = str;
2081/// const char* end = base + strlen(str);
2082/// size_t result = end - base;
2083/// \endcode
2084///
2085/// In the case we index by an induction variable, as long as the induction
2086/// variable has a constant int increment, we can replace all such indvars
2087/// with the closed form computation of strlen
2088///
2089/// \code{.c}
2090/// size_t i = 0;
2091/// while (str[i] != '\0')
2092/// ++i;
2093/// size_t result = i;
2094/// \endcode
2095///
2096/// Will be replaced by
2097///
2098/// \code{.c}
2099/// size_t i = 0 + strlen(str);
2100/// size_t result = i;
2101/// \endcode
2102///
2103bool LoopIdiomRecognize::recognizeAndInsertStrLen() {
2104 if (DisableLIRP::All)
2105 return false;
2106
2107 StrlenVerifier Verifier(CurLoop, SE, TLI);
2108
2109 if (!Verifier.isValidStrlenIdiom())
2110 return false;
2111
2112 BasicBlock *Preheader = CurLoop->getLoopPreheader();
2113 BasicBlock *LoopBody = *CurLoop->block_begin();
2114 BasicBlock *LoopExitBB = CurLoop->getExitBlock();
2115 CondBrInst *LoopTerm = cast<CondBrInst>(LoopBody->getTerminator());
2116 assert(Preheader && LoopBody && LoopExitBB &&
2117 "Should be verified to be valid by StrlenVerifier");
2118
2119 if (Verifier.OpWidth == 8) {
2121 return false;
2122 if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_strlen))
2123 return false;
2124 } else {
2126 return false;
2127 if (!isLibFuncEmittable(Preheader->getModule(), TLI, LibFunc_wcslen))
2128 return false;
2129 }
2130
2131 IRBuilder<> Builder(Preheader->getTerminator());
2132 Builder.SetCurrentDebugLocation(CurLoop->getStartLoc());
2133 SCEVExpander Expander(*SE, "strlen_idiom");
2134 Value *MaterialzedBase = Expander.expandCodeFor(
2135 Verifier.LoadBaseEv, Verifier.LoadBaseEv->getType(),
2136 Builder.GetInsertPoint());
2137
2138 Value *StrLenFunc = nullptr;
2139 if (Verifier.OpWidth == 8) {
2140 StrLenFunc = emitStrLen(MaterialzedBase, Builder, *DL, TLI);
2141 } else {
2142 StrLenFunc = emitWcsLen(MaterialzedBase, Builder, *DL, TLI);
2143 }
2144 assert(StrLenFunc && "Failed to emit strlen function.");
2145
2146 const SCEV *StrlenEv = SE->getSCEV(StrLenFunc);
2148 for (PHINode &PN : LoopExitBB->phis()) {
2149 // We can now materialize the loop output as all phi have scev {base,+,a}.
2150 // We expand the phi as:
2151 // %strlen = call i64 @strlen(%str)
2152 // %phi.new = base expression + step * %strlen
2153 const SCEV *Ev = SE->getSCEV(&PN);
2154 const SCEVAddRecExpr *AddRecEv = dyn_cast<SCEVAddRecExpr>(Ev);
2155 const SCEVConstant *Step =
2157 const SCEV *Base = AddRecEv->getStart();
2158
2159 // It is safe to truncate to base since if base is narrower than size_t
2160 // the equivalent user code will have to truncate anyways.
2161 const SCEV *NewEv = SE->getAddExpr(
2163 StrlenEv, Base->getType())));
2164
2165 Value *MaterializedPHI = Expander.expandCodeFor(NewEv, NewEv->getType(),
2166 Builder.GetInsertPoint());
2167 Expander.clear();
2168 PN.replaceAllUsesWith(MaterializedPHI);
2169 Cleanup.push_back(&PN);
2170 }
2171
2172 // All LCSSA Loop Phi are dead, the left over dead loop body can be cleaned
2173 // up by later passes
2174 for (PHINode *PN : Cleanup)
2176
2177 // LoopDeletion only delete invariant loops with known trip-count. We can
2178 // update the condition so it will reliablely delete the invariant loop
2179 assert((LoopTerm->getSuccessor(0) == LoopBody ||
2180 LoopTerm->getSuccessor(1) == LoopBody) &&
2181 "loop body must have a successor that is it self");
2182 ConstantInt *NewLoopCond = LoopTerm->getSuccessor(0) == LoopBody
2183 ? Builder.getFalse()
2184 : Builder.getTrue();
2185 LoopTerm->setCondition(NewLoopCond);
2186 SE->forgetLoop(CurLoop);
2187
2188 ++NumStrLen;
2189 LLVM_DEBUG(dbgs() << " Formed strlen idiom: " << *StrLenFunc << "\n");
2190 ORE.emit([&]() {
2191 return OptimizationRemark(DEBUG_TYPE, "recognizeAndInsertStrLen",
2192 CurLoop->getStartLoc(), Preheader)
2193 << "Transformed " << StrLenFunc->getName() << " loop idiom";
2194 });
2195
2196 return true;
2197}
2198
2199/// Check if the given conditional branch is based on an unsigned less-than
2200/// comparison between a variable and a constant, and if the comparison is false
2201/// the control yields to the loop entry. If the branch matches the behaviour,
2202/// the variable involved in the comparison is returned.
2204 APInt &Threshold) {
2206 if (!Cond)
2207 return nullptr;
2208
2209 ConstantInt *CmpConst = dyn_cast<ConstantInt>(Cond->getOperand(1));
2210 if (!CmpConst)
2211 return nullptr;
2212
2213 BasicBlock *FalseSucc = BI->getSuccessor(1);
2214 ICmpInst::Predicate Pred = Cond->getPredicate();
2215
2216 if (Pred == ICmpInst::ICMP_ULT && FalseSucc == LoopEntry) {
2217 Threshold = CmpConst->getValue();
2218 return Cond->getOperand(0);
2219 }
2220
2221 return nullptr;
2222}
2223
2224// Check if the recurrence variable `VarX` is in the right form to create
2225// the idiom. Returns the value coerced to a PHINode if so.
2227 BasicBlock *LoopEntry) {
2228 auto *PhiX = dyn_cast<PHINode>(VarX);
2229 if (PhiX && PhiX->getParent() == LoopEntry &&
2230 (PhiX->getOperand(0) == DefX || PhiX->getOperand(1) == DefX))
2231 return PhiX;
2232 return nullptr;
2233}
2234
2235/// Return true if the idiom is detected in the loop.
2236///
2237/// Additionally:
2238/// 1) \p CntInst is set to the instruction Counting Leading Zeros (CTLZ)
2239/// or nullptr if there is no such.
2240/// 2) \p CntPhi is set to the corresponding phi node
2241/// or nullptr if there is no such.
2242/// 3) \p InitX is set to the value whose CTLZ could be used.
2243/// 4) \p DefX is set to the instruction calculating Loop exit condition.
2244/// 5) \p Threshold is set to the constant involved in the unsigned less-than
2245/// comparison.
2246///
2247/// The core idiom we are trying to detect is:
2248/// \code
2249/// if (x0 < 2)
2250/// goto loop-exit // the precondition of the loop
2251/// cnt0 = init-val
2252/// do {
2253/// x = phi (x0, x.next); //PhiX
2254/// cnt = phi (cnt0, cnt.next)
2255///
2256/// cnt.next = cnt + 1;
2257/// ...
2258/// x.next = x >> 1; // DefX
2259/// } while (x >= 4)
2260/// loop-exit:
2261/// \endcode
2263 Intrinsic::ID &IntrinID,
2264 Value *&InitX, Instruction *&CntInst,
2265 PHINode *&CntPhi, Instruction *&DefX,
2266 APInt &Threshold) {
2267 BasicBlock *LoopEntry;
2268
2269 DefX = nullptr;
2270 CntInst = nullptr;
2271 CntPhi = nullptr;
2272 LoopEntry = *(CurLoop->block_begin());
2273
2274 // step 1: Check if the loop-back branch is in desirable form.
2275 auto *EntryBI = dyn_cast<CondBrInst>(LoopEntry->getTerminator());
2276 if (!EntryBI)
2277 return false;
2278 if (Value *T = matchShiftULTCondition(EntryBI, LoopEntry, Threshold))
2279 DefX = dyn_cast<Instruction>(T);
2280 else
2281 return false;
2282
2283 // step 2: Check the recurrence of variable X
2284 if (!DefX || !isa<PHINode>(DefX))
2285 return false;
2286
2287 PHINode *VarPhi = cast<PHINode>(DefX);
2288 int Idx = VarPhi->getBasicBlockIndex(LoopEntry);
2289 if (Idx == -1)
2290 return false;
2291
2292 DefX = dyn_cast<Instruction>(VarPhi->getIncomingValue(Idx));
2293 if (!DefX || DefX->getNumOperands() == 0 || DefX->getOperand(0) != VarPhi)
2294 return false;
2295
2296 // step 3: detect instructions corresponding to "x.next = x >> 1"
2297 if (DefX->getOpcode() != Instruction::LShr)
2298 return false;
2299
2300 IntrinID = Intrinsic::ctlz;
2302 if (!Shft || !Shft->isOne())
2303 return false;
2304
2305 InitX = VarPhi->getIncomingValueForBlock(CurLoop->getLoopPreheader());
2306
2307 // step 4: Find the instruction which count the CTLZ: cnt.next = cnt + 1
2308 // or cnt.next = cnt + -1.
2309 // TODO: We can skip the step. If loop trip count is known (CTLZ),
2310 // then all uses of "cnt.next" could be optimized to the trip count
2311 // plus "cnt0". Currently it is not optimized.
2312 // This step could be used to detect POPCNT instruction:
2313 // cnt.next = cnt + (x.next & 1)
2314 for (Instruction &Inst :
2315 llvm::make_range(LoopEntry->getFirstNonPHIIt(), LoopEntry->end())) {
2316 if (Inst.getOpcode() != Instruction::Add)
2317 continue;
2318
2320 if (!Inc || (!Inc->isOne() && !Inc->isMinusOne()))
2321 continue;
2322
2323 PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry);
2324 if (!Phi)
2325 continue;
2326
2327 CntInst = &Inst;
2328 CntPhi = Phi;
2329 break;
2330 }
2331 if (!CntInst)
2332 return false;
2333
2334 return true;
2335}
2336
2337/// Return true iff the idiom is detected in the loop.
2338///
2339/// Additionally:
2340/// 1) \p CntInst is set to the instruction counting the population bit.
2341/// 2) \p CntPhi is set to the corresponding phi node.
2342/// 3) \p Var is set to the value whose population bits are being counted.
2343///
2344/// The core idiom we are trying to detect is:
2345/// \code
2346/// if (x0 != 0)
2347/// goto loop-exit // the precondition of the loop
2348/// cnt0 = init-val;
2349/// do {
2350/// x1 = phi (x0, x2);
2351/// cnt1 = phi(cnt0, cnt2);
2352///
2353/// cnt2 = cnt1 + 1;
2354/// ...
2355/// x2 = x1 & (x1 - 1);
2356/// ...
2357/// } while(x != 0);
2358///
2359/// loop-exit:
2360/// \endcode
2361static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB,
2362 Instruction *&CntInst, PHINode *&CntPhi,
2363 Value *&Var) {
2364 // step 1: Check to see if the look-back branch match this pattern:
2365 // "if (a!=0) goto loop-entry".
2366 BasicBlock *LoopEntry;
2367 Instruction *DefX2, *CountInst;
2368 Value *VarX1, *VarX0;
2369 PHINode *PhiX, *CountPhi;
2370
2371 DefX2 = CountInst = nullptr;
2372 VarX1 = VarX0 = nullptr;
2373 PhiX = CountPhi = nullptr;
2374 LoopEntry = *(CurLoop->block_begin());
2375
2376 // step 1: Check if the loop-back branch is in desirable form.
2377 {
2378 auto *LoopTerm = dyn_cast<CondBrInst>(LoopEntry->getTerminator());
2379 if (!LoopTerm)
2380 return false;
2381 DefX2 = dyn_cast_or_null<Instruction>(matchCondition(LoopTerm, LoopEntry));
2382 }
2383
2384 // step 2: detect instructions corresponding to "x2 = x1 & (x1 - 1)"
2385 {
2386 if (!DefX2 || DefX2->getOpcode() != Instruction::And)
2387 return false;
2388
2389 BinaryOperator *SubOneOp;
2390
2391 if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0))))
2392 VarX1 = DefX2->getOperand(1);
2393 else {
2394 VarX1 = DefX2->getOperand(0);
2395 SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1));
2396 }
2397 if (!SubOneOp || SubOneOp->getOperand(0) != VarX1)
2398 return false;
2399
2400 ConstantInt *Dec = dyn_cast<ConstantInt>(SubOneOp->getOperand(1));
2401 if (!Dec ||
2402 !((SubOneOp->getOpcode() == Instruction::Sub && Dec->isOne()) ||
2403 (SubOneOp->getOpcode() == Instruction::Add &&
2404 Dec->isMinusOne()))) {
2405 return false;
2406 }
2407 }
2408
2409 // step 3: Check the recurrence of variable X
2410 PhiX = getRecurrenceVar(VarX1, DefX2, LoopEntry);
2411 if (!PhiX)
2412 return false;
2413
2414 // step 4: Find the instruction which count the population: cnt2 = cnt1 + 1
2415 {
2416 CountInst = nullptr;
2417 for (Instruction &Inst :
2418 llvm::make_range(LoopEntry->getFirstNonPHIIt(), LoopEntry->end())) {
2419 if (Inst.getOpcode() != Instruction::Add)
2420 continue;
2421
2423 if (!Inc || !Inc->isOne())
2424 continue;
2425
2426 PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry);
2427 if (!Phi)
2428 continue;
2429
2430 // Check if the result of the instruction is live of the loop.
2431 bool LiveOutLoop = false;
2432 for (User *U : Inst.users()) {
2433 if ((cast<Instruction>(U))->getParent() != LoopEntry) {
2434 LiveOutLoop = true;
2435 break;
2436 }
2437 }
2438
2439 if (LiveOutLoop) {
2440 CountInst = &Inst;
2441 CountPhi = Phi;
2442 break;
2443 }
2444 }
2445
2446 if (!CountInst)
2447 return false;
2448 }
2449
2450 // step 5: check if the precondition is in this form:
2451 // "if (x != 0) goto loop-head ; else goto somewhere-we-don't-care;"
2452 {
2453 auto *PreCondBr = dyn_cast<CondBrInst>(PreCondBB->getTerminator());
2454 if (!PreCondBr)
2455 return false;
2456 Value *T = matchCondition(PreCondBr, CurLoop->getLoopPreheader());
2457 if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1))
2458 return false;
2459
2460 CntInst = CountInst;
2461 CntPhi = CountPhi;
2462 Var = T;
2463 }
2464
2465 return true;
2466}
2467
2468/// Return true if the idiom is detected in the loop.
2469///
2470/// Additionally:
2471/// 1) \p CntInst is set to the instruction Counting Leading Zeros (CTLZ)
2472/// or nullptr if there is no such.
2473/// 2) \p CntPhi is set to the corresponding phi node
2474/// or nullptr if there is no such.
2475/// 3) \p Var is set to the value whose CTLZ could be used.
2476/// 4) \p DefX is set to the instruction calculating Loop exit condition.
2477///
2478/// The core idiom we are trying to detect is:
2479/// \code
2480/// if (x0 == 0)
2481/// goto loop-exit // the precondition of the loop
2482/// cnt0 = init-val;
2483/// do {
2484/// x = phi (x0, x.next); //PhiX
2485/// cnt = phi(cnt0, cnt.next);
2486///
2487/// cnt.next = cnt + 1;
2488/// ...
2489/// x.next = x >> 1; // DefX
2490/// ...
2491/// } while(x.next != 0);
2492///
2493/// loop-exit:
2494/// \endcode
2495static bool detectShiftUntilZeroIdiom(Loop *CurLoop, const DataLayout &DL,
2496 Intrinsic::ID &IntrinID, Value *&InitX,
2497 Instruction *&CntInst, PHINode *&CntPhi,
2498 Instruction *&DefX) {
2499 BasicBlock *LoopEntry;
2500 Value *VarX = nullptr;
2501
2502 DefX = nullptr;
2503 CntInst = nullptr;
2504 CntPhi = nullptr;
2505 LoopEntry = *(CurLoop->block_begin());
2506
2507 // step 1: Check if the loop-back branch is in desirable form.
2508 auto *LoopTerm = dyn_cast<CondBrInst>(LoopEntry->getTerminator());
2509 if (!LoopTerm)
2510 return false;
2511 DefX = dyn_cast_or_null<Instruction>(matchCondition(LoopTerm, LoopEntry));
2512
2513 // step 2: detect instructions corresponding to "x.next = x >> 1 or x << 1"
2514 if (!DefX || !DefX->isShift())
2515 return false;
2516 IntrinID = DefX->getOpcode() == Instruction::Shl ? Intrinsic::cttz :
2517 Intrinsic::ctlz;
2519 if (!Shft || !Shft->isOne())
2520 return false;
2521 VarX = DefX->getOperand(0);
2522
2523 // step 3: Check the recurrence of variable X
2524 PHINode *PhiX = getRecurrenceVar(VarX, DefX, LoopEntry);
2525 if (!PhiX)
2526 return false;
2527
2528 InitX = PhiX->getIncomingValueForBlock(CurLoop->getLoopPreheader());
2529
2530 // Make sure the initial value can't be negative otherwise the ashr in the
2531 // loop might never reach zero which would make the loop infinite.
2532 if (DefX->getOpcode() == Instruction::AShr && !isKnownNonNegative(InitX, DL))
2533 return false;
2534
2535 // step 4: Find the instruction which count the CTLZ: cnt.next = cnt + 1
2536 // or cnt.next = cnt + -1.
2537 // TODO: We can skip the step. If loop trip count is known (CTLZ),
2538 // then all uses of "cnt.next" could be optimized to the trip count
2539 // plus "cnt0". Currently it is not optimized.
2540 // This step could be used to detect POPCNT instruction:
2541 // cnt.next = cnt + (x.next & 1)
2542 for (Instruction &Inst :
2543 llvm::make_range(LoopEntry->getFirstNonPHIIt(), LoopEntry->end())) {
2544 if (Inst.getOpcode() != Instruction::Add)
2545 continue;
2546
2548 if (!Inc || (!Inc->isOne() && !Inc->isMinusOne()))
2549 continue;
2550
2551 PHINode *Phi = getRecurrenceVar(Inst.getOperand(0), &Inst, LoopEntry);
2552 if (!Phi)
2553 continue;
2554
2555 CntInst = &Inst;
2556 CntPhi = Phi;
2557 break;
2558 }
2559 if (!CntInst)
2560 return false;
2561
2562 return true;
2563}
2564
2565// Check if CTLZ / CTTZ intrinsic is profitable. Assume it is always
2566// profitable if we delete the loop.
2567bool LoopIdiomRecognize::isProfitableToInsertFFS(Intrinsic::ID IntrinID,
2568 Value *InitX, bool ZeroCheck,
2569 size_t CanonicalSize) {
2570 const Value *Args[] = {InitX,
2571 ConstantInt::getBool(InitX->getContext(), ZeroCheck)};
2572
2573 uint32_t HeaderSize = CurLoop->getHeader()->size();
2574
2575 IntrinsicCostAttributes Attrs(IntrinID, InitX->getType(), Args);
2576 InstructionCost Cost = TTI->getIntrinsicInstrCost(
2578 if (HeaderSize != CanonicalSize && Cost > TargetTransformInfo::TCC_Basic)
2579 return false;
2580
2581 return true;
2582}
2583
2584/// Convert CTLZ / CTTZ idiom loop into countable loop.
2585/// If CTLZ / CTTZ inserted as a new trip count returns true; otherwise,
2586/// returns false.
2587bool LoopIdiomRecognize::insertFFSIfProfitable(Intrinsic::ID IntrinID,
2588 Value *InitX, Instruction *DefX,
2589 PHINode *CntPhi,
2590 Instruction *CntInst) {
2591 bool IsCntPhiUsedOutsideLoop = false;
2592 for (User *U : CntPhi->users())
2593 if (!CurLoop->contains(cast<Instruction>(U))) {
2594 IsCntPhiUsedOutsideLoop = true;
2595 break;
2596 }
2597 bool IsCntInstUsedOutsideLoop = false;
2598 for (User *U : CntInst->users())
2599 if (!CurLoop->contains(cast<Instruction>(U))) {
2600 IsCntInstUsedOutsideLoop = true;
2601 break;
2602 }
2603 // If both CntInst and CntPhi are used outside the loop the profitability
2604 // is questionable.
2605 if (IsCntInstUsedOutsideLoop && IsCntPhiUsedOutsideLoop)
2606 return false;
2607
2608 // For some CPUs result of CTLZ(X) intrinsic is undefined
2609 // when X is 0. If we can not guarantee X != 0, we need to check this
2610 // when expand.
2611 bool ZeroCheck = false;
2612 // It is safe to assume Preheader exist as it was checked in
2613 // parent function RunOnLoop.
2614 BasicBlock *PH = CurLoop->getLoopPreheader();
2615
2616 // If we are using the count instruction outside the loop, make sure we
2617 // have a zero check as a precondition. Without the check the loop would run
2618 // one iteration for before any check of the input value. This means 0 and 1
2619 // would have identical behavior in the original loop and thus
2620 if (!IsCntPhiUsedOutsideLoop) {
2621 auto *PreCondBB = PH->getSinglePredecessor();
2622 if (!PreCondBB)
2623 return false;
2624 auto *PreCondBI = dyn_cast<CondBrInst>(PreCondBB->getTerminator());
2625 if (!PreCondBI)
2626 return false;
2627 if (matchCondition(PreCondBI, PH) != InitX)
2628 return false;
2629 ZeroCheck = true;
2630 }
2631
2632 // FFS idiom loop has only 6 instructions:
2633 // %n.addr.0 = phi [ %n, %entry ], [ %shr, %while.cond ]
2634 // %i.0 = phi [ %i0, %entry ], [ %inc, %while.cond ]
2635 // %shr = ashr %n.addr.0, 1
2636 // %tobool = icmp eq %shr, 0
2637 // %inc = add nsw %i.0, 1
2638 // br i1 %tobool
2639 size_t IdiomCanonicalSize = 6;
2640 if (!isProfitableToInsertFFS(IntrinID, InitX, ZeroCheck, IdiomCanonicalSize))
2641 return false;
2642
2643 transformLoopToCountable(IntrinID, PH, CntInst, CntPhi, InitX, DefX,
2644 DefX->getDebugLoc(), ZeroCheck,
2645 IsCntPhiUsedOutsideLoop);
2646 return true;
2647}
2648
2649/// Recognize CTLZ or CTTZ idiom in a non-countable loop and convert the loop
2650/// to countable (with CTLZ / CTTZ trip count). If CTLZ / CTTZ inserted as a new
2651/// trip count returns true; otherwise, returns false.
2652bool LoopIdiomRecognize::recognizeAndInsertFFS() {
2653 // Give up if the loop has multiple blocks or multiple backedges.
2654 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
2655 return false;
2656
2657 Intrinsic::ID IntrinID;
2658 Value *InitX;
2659 Instruction *DefX = nullptr;
2660 PHINode *CntPhi = nullptr;
2661 Instruction *CntInst = nullptr;
2662
2663 if (!detectShiftUntilZeroIdiom(CurLoop, *DL, IntrinID, InitX, CntInst, CntPhi,
2664 DefX))
2665 return false;
2666
2667 return insertFFSIfProfitable(IntrinID, InitX, DefX, CntPhi, CntInst);
2668}
2669
2670bool LoopIdiomRecognize::recognizeShiftUntilLessThan() {
2671 // Give up if the loop has multiple blocks or multiple backedges.
2672 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
2673 return false;
2674
2675 Intrinsic::ID IntrinID;
2676 Value *InitX;
2677 Instruction *DefX = nullptr;
2678 PHINode *CntPhi = nullptr;
2679 Instruction *CntInst = nullptr;
2680
2681 APInt LoopThreshold;
2682 if (!detectShiftUntilLessThanIdiom(CurLoop, *DL, IntrinID, InitX, CntInst,
2683 CntPhi, DefX, LoopThreshold))
2684 return false;
2685
2686 if (LoopThreshold == 2) {
2687 // Treat as regular FFS.
2688 return insertFFSIfProfitable(IntrinID, InitX, DefX, CntPhi, CntInst);
2689 }
2690
2691 // Look for Floor Log2 Idiom.
2692 if (LoopThreshold != 4)
2693 return false;
2694
2695 // Abort if CntPhi is used outside of the loop.
2696 for (User *U : CntPhi->users())
2697 if (!CurLoop->contains(cast<Instruction>(U)))
2698 return false;
2699
2700 // It is safe to assume Preheader exist as it was checked in
2701 // parent function RunOnLoop.
2702 BasicBlock *PH = CurLoop->getLoopPreheader();
2703 auto *PreCondBB = PH->getSinglePredecessor();
2704 if (!PreCondBB)
2705 return false;
2706 auto *PreCondBI = dyn_cast<CondBrInst>(PreCondBB->getTerminator());
2707 if (!PreCondBI)
2708 return false;
2709
2710 APInt PreLoopThreshold;
2711 if (matchShiftULTCondition(PreCondBI, PH, PreLoopThreshold) != InitX ||
2712 PreLoopThreshold != 2)
2713 return false;
2714
2715 bool ZeroCheck = true;
2716
2717 // the loop has only 6 instructions:
2718 // %n.addr.0 = phi [ %n, %entry ], [ %shr, %while.cond ]
2719 // %i.0 = phi [ %i0, %entry ], [ %inc, %while.cond ]
2720 // %shr = ashr %n.addr.0, 1
2721 // %tobool = icmp ult %n.addr.0, C
2722 // %inc = add nsw %i.0, 1
2723 // br i1 %tobool
2724 size_t IdiomCanonicalSize = 6;
2725 if (!isProfitableToInsertFFS(IntrinID, InitX, ZeroCheck, IdiomCanonicalSize))
2726 return false;
2727
2728 // log2(x) = w − 1 − clz(x)
2729 transformLoopToCountable(IntrinID, PH, CntInst, CntPhi, InitX, DefX,
2730 DefX->getDebugLoc(), ZeroCheck,
2731 /*IsCntPhiUsedOutsideLoop=*/false,
2732 /*InsertSub=*/true);
2733 return true;
2734}
2735
2736/// Recognizes a population count idiom in a non-countable loop.
2737///
2738/// If detected, transforms the relevant code to issue the popcount intrinsic
2739/// function call, and returns true; otherwise, returns false.
2740bool LoopIdiomRecognize::recognizePopcount() {
2741 if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware)
2742 return false;
2743
2744 // Counting population are usually conducted by few arithmetic instructions.
2745 // Such instructions can be easily "absorbed" by vacant slots in a
2746 // non-compact loop. Therefore, recognizing popcount idiom only makes sense
2747 // in a compact loop.
2748
2749 // Give up if the loop has multiple blocks or multiple backedges.
2750 if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
2751 return false;
2752
2753 BasicBlock *LoopBody = *(CurLoop->block_begin());
2754 if (LoopBody->size() >= 20) {
2755 // The loop is too big, bail out.
2756 return false;
2757 }
2758
2759 // It should have a preheader containing nothing but an unconditional branch.
2760 BasicBlock *PH = CurLoop->getLoopPreheader();
2761 if (!PH || &PH->front() != PH->getTerminator())
2762 return false;
2763 auto *EntryBI = dyn_cast<UncondBrInst>(PH->getTerminator());
2764 if (!EntryBI)
2765 return false;
2766
2767 // It should have a precondition block where the generated popcount intrinsic
2768 // function can be inserted.
2769 auto *PreCondBB = PH->getSinglePredecessor();
2770 if (!PreCondBB)
2771 return false;
2772 auto *PreCondBI = dyn_cast<CondBrInst>(PreCondBB->getTerminator());
2773 if (!PreCondBI)
2774 return false;
2775
2776 Instruction *CntInst;
2777 PHINode *CntPhi;
2778 Value *Val;
2779 if (!detectPopcountIdiom(CurLoop, PreCondBB, CntInst, CntPhi, Val))
2780 return false;
2781
2782 transformLoopToPopcount(PreCondBB, CntInst, CntPhi, Val);
2783 return true;
2784}
2785
2787 const DebugLoc &DL) {
2788 Value *Ops[] = {Val};
2789 Type *Tys[] = {Val->getType()};
2790
2792 return IRBuilder.CreateIntrinsic(Intrinsic::ctpop, Tys, Ops);
2793}
2794
2796 const DebugLoc &DL, bool ZeroCheck,
2797 Intrinsic::ID IID) {
2798 Value *Ops[] = {Val, IRBuilder.getInt1(ZeroCheck)};
2799 Type *Tys[] = {Val->getType()};
2800
2802 return IRBuilder.CreateIntrinsic(IID, Tys, Ops);
2803}
2804
2805/// Transform the following loop (Using CTLZ, CTTZ is similar):
2806/// loop:
2807/// CntPhi = PHI [Cnt0, CntInst]
2808/// PhiX = PHI [InitX, DefX]
2809/// CntInst = CntPhi + 1
2810/// DefX = PhiX >> 1
2811/// LOOP_BODY
2812/// Br: loop if (DefX != 0)
2813/// Use(CntPhi) or Use(CntInst)
2814///
2815/// Into:
2816/// If CntPhi used outside the loop:
2817/// CountPrev = BitWidth(InitX) - CTLZ(InitX >> 1)
2818/// Count = CountPrev + 1
2819/// else
2820/// Count = BitWidth(InitX) - CTLZ(InitX)
2821/// loop:
2822/// CntPhi = PHI [Cnt0, CntInst]
2823/// PhiX = PHI [InitX, DefX]
2824/// PhiCount = PHI [Count, Dec]
2825/// CntInst = CntPhi + 1
2826/// DefX = PhiX >> 1
2827/// Dec = PhiCount - 1
2828/// LOOP_BODY
2829/// Br: loop if (Dec != 0)
2830/// Use(CountPrev + Cnt0) // Use(CntPhi)
2831/// or
2832/// Use(Count + Cnt0) // Use(CntInst)
2833///
2834/// If LOOP_BODY is empty the loop will be deleted.
2835/// If CntInst and DefX are not used in LOOP_BODY they will be removed.
2836void LoopIdiomRecognize::transformLoopToCountable(
2837 Intrinsic::ID IntrinID, BasicBlock *Preheader, Instruction *CntInst,
2838 PHINode *CntPhi, Value *InitX, Instruction *DefX, const DebugLoc &DL,
2839 bool ZeroCheck, bool IsCntPhiUsedOutsideLoop, bool InsertSub) {
2840 // Step 1: Insert the CTLZ/CTTZ instruction at the end of the preheader block
2841 IRBuilder<> Builder(Preheader->getTerminator());
2842 Builder.SetCurrentDebugLocation(DL);
2843
2844 // If there are no uses of CntPhi crate:
2845 // Count = BitWidth - CTLZ(InitX);
2846 // NewCount = Count;
2847 // If there are uses of CntPhi create:
2848 // NewCount = BitWidth - CTLZ(InitX >> 1);
2849 // Count = NewCount + 1;
2850 Value *InitXNext;
2851 if (IsCntPhiUsedOutsideLoop) {
2852 if (DefX->getOpcode() == Instruction::AShr)
2853 InitXNext = Builder.CreateAShr(InitX, 1);
2854 else if (DefX->getOpcode() == Instruction::LShr)
2855 InitXNext = Builder.CreateLShr(InitX, 1);
2856 else if (DefX->getOpcode() == Instruction::Shl) // cttz
2857 InitXNext = Builder.CreateShl(InitX, 1);
2858 else
2859 llvm_unreachable("Unexpected opcode!");
2860 } else
2861 InitXNext = InitX;
2862 Value *Count =
2863 createFFSIntrinsic(Builder, InitXNext, DL, ZeroCheck, IntrinID);
2864 Type *CountTy = Count->getType();
2865 Count = Builder.CreateSub(
2866 ConstantInt::get(CountTy, CountTy->getIntegerBitWidth()), Count);
2867 if (InsertSub)
2868 Count = Builder.CreateSub(Count, ConstantInt::get(CountTy, 1));
2869 Value *NewCount = Count;
2870 if (IsCntPhiUsedOutsideLoop)
2871 Count = Builder.CreateAdd(Count, ConstantInt::get(CountTy, 1));
2872
2873 NewCount = Builder.CreateZExtOrTrunc(NewCount, CntInst->getType());
2874
2875 Value *CntInitVal = CntPhi->getIncomingValueForBlock(Preheader);
2876 if (cast<ConstantInt>(CntInst->getOperand(1))->isOne()) {
2877 // If the counter was being incremented in the loop, add NewCount to the
2878 // counter's initial value, but only if the initial value is not zero.
2879 ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
2880 if (!InitConst || !InitConst->isZero())
2881 NewCount = Builder.CreateAdd(NewCount, CntInitVal);
2882 } else {
2883 // If the count was being decremented in the loop, subtract NewCount from
2884 // the counter's initial value.
2885 NewCount = Builder.CreateSub(CntInitVal, NewCount);
2886 }
2887
2888 // Step 2: Insert new IV and loop condition:
2889 // loop:
2890 // ...
2891 // PhiCount = PHI [Count, Dec]
2892 // ...
2893 // Dec = PhiCount - 1
2894 // ...
2895 // Br: loop if (Dec != 0)
2896 BasicBlock *Body = *(CurLoop->block_begin());
2897 auto *LbBr = cast<CondBrInst>(Body->getTerminator());
2898 ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
2899
2900 PHINode *TcPhi = PHINode::Create(CountTy, 2, "tcphi");
2901 TcPhi->insertBefore(Body->begin());
2902
2903 Builder.SetInsertPoint(LbCond);
2904 Instruction *TcDec = cast<Instruction>(Builder.CreateSub(
2905 TcPhi, ConstantInt::get(CountTy, 1), "tcdec", false, true));
2906
2907 TcPhi->addIncoming(Count, Preheader);
2908 TcPhi->addIncoming(TcDec, Body);
2909
2910 CmpInst::Predicate Pred =
2911 (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_NE : CmpInst::ICMP_EQ;
2912 LbCond->setPredicate(Pred);
2913 LbCond->setOperand(0, TcDec);
2914 LbCond->setOperand(1, ConstantInt::get(CountTy, 0));
2915
2916 // Step 3: All the references to the original counter outside
2917 // the loop are replaced with the NewCount
2918 if (IsCntPhiUsedOutsideLoop)
2919 CntPhi->replaceUsesOutsideBlock(NewCount, Body);
2920 else
2921 CntInst->replaceUsesOutsideBlock(NewCount, Body);
2922
2923 // step 4: Forget the "non-computable" trip-count SCEV associated with the
2924 // loop. The loop would otherwise not be deleted even if it becomes empty.
2925 SE->forgetLoop(CurLoop);
2926}
2927
2928void LoopIdiomRecognize::transformLoopToPopcount(BasicBlock *PreCondBB,
2929 Instruction *CntInst,
2930 PHINode *CntPhi, Value *Var) {
2931 BasicBlock *PreHead = CurLoop->getLoopPreheader();
2932 auto *PreCondBr = cast<CondBrInst>(PreCondBB->getTerminator());
2933 const DebugLoc &DL = CntInst->getDebugLoc();
2934
2935 // Assuming before transformation, the loop is following:
2936 // if (x) // the precondition
2937 // do { cnt++; x &= x - 1; } while(x);
2938
2939 // Step 1: Insert the ctpop instruction at the end of the precondition block
2940 IRBuilder<> Builder(PreCondBr);
2941 Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
2942 {
2943 PopCnt = createPopcntIntrinsic(Builder, Var, DL);
2944 NewCount = PopCntZext =
2945 Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType()));
2946
2947 if (NewCount != PopCnt)
2948 (cast<Instruction>(NewCount))->setDebugLoc(DL);
2949
2950 // TripCnt is exactly the number of iterations the loop has
2951 TripCnt = NewCount;
2952
2953 // If the population counter's initial value is not zero, insert Add Inst.
2954 Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead);
2955 ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
2956 if (!InitConst || !InitConst->isZero()) {
2957 NewCount = Builder.CreateAdd(NewCount, CntInitVal);
2958 (cast<Instruction>(NewCount))->setDebugLoc(DL);
2959 }
2960 }
2961
2962 // Step 2: Replace the precondition from "if (x == 0) goto loop-exit" to
2963 // "if (NewCount == 0) loop-exit". Without this change, the intrinsic
2964 // function would be partial dead code, and downstream passes will drag
2965 // it back from the precondition block to the preheader.
2966 {
2967 ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition());
2968
2969 Value *Opnd0 = PopCntZext;
2970 Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0);
2971 if (PreCond->getOperand(0) != Var)
2972 std::swap(Opnd0, Opnd1);
2973
2974 ICmpInst *NewPreCond = cast<ICmpInst>(
2975 Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1));
2976 PreCondBr->setCondition(NewPreCond);
2977
2979 }
2980
2981 // Step 3: Note that the population count is exactly the trip count of the
2982 // loop in question, which enable us to convert the loop from noncountable
2983 // loop into a countable one. The benefit is twofold:
2984 //
2985 // - If the loop only counts population, the entire loop becomes dead after
2986 // the transformation. It is a lot easier to prove a countable loop dead
2987 // than to prove a noncountable one. (In some C dialects, an infinite loop
2988 // isn't dead even if it computes nothing useful. In general, DCE needs
2989 // to prove a noncountable loop finite before safely delete it.)
2990 //
2991 // - If the loop also performs something else, it remains alive.
2992 // Since it is transformed to countable form, it can be aggressively
2993 // optimized by some optimizations which are in general not applicable
2994 // to a noncountable loop.
2995 //
2996 // After this step, this loop (conceptually) would look like following:
2997 // newcnt = __builtin_ctpop(x);
2998 // t = newcnt;
2999 // if (x)
3000 // do { cnt++; x &= x-1; t--) } while (t > 0);
3001 BasicBlock *Body = *(CurLoop->block_begin());
3002 {
3003 auto *LbBr = cast<CondBrInst>(Body->getTerminator());
3004 ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
3005 Type *Ty = TripCnt->getType();
3006
3007 PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi");
3008 TcPhi->insertBefore(Body->begin());
3009
3010 Builder.SetInsertPoint(LbCond);
3012 Builder.CreateSub(TcPhi, ConstantInt::get(Ty, 1),
3013 "tcdec", false, true));
3014
3015 TcPhi->addIncoming(TripCnt, PreHead);
3016 TcPhi->addIncoming(TcDec, Body);
3017
3018 CmpInst::Predicate Pred =
3019 (LbBr->getSuccessor(0) == Body) ? CmpInst::ICMP_UGT : CmpInst::ICMP_SLE;
3020 LbCond->setPredicate(Pred);
3021 LbCond->setOperand(0, TcDec);
3022 LbCond->setOperand(1, ConstantInt::get(Ty, 0));
3023 }
3024
3025 // Step 4: All the references to the original population counter outside
3026 // the loop are replaced with the NewCount -- the value returned from
3027 // __builtin_ctpop().
3028 CntInst->replaceUsesOutsideBlock(NewCount, Body);
3029
3030 // step 5: Forget the "non-computable" trip-count SCEV associated with the
3031 // loop. The loop would otherwise not be deleted even if it becomes empty.
3032 SE->forgetLoop(CurLoop);
3033}
3034
3035/// Match loop-invariant value.
3036template <typename SubPattern_t> struct match_LoopInvariant {
3037 SubPattern_t SubPattern;
3038 const Loop *L;
3039
3040 match_LoopInvariant(const SubPattern_t &SP, const Loop *L)
3041 : SubPattern(SP), L(L) {}
3042
3043 template <typename ITy> bool match(ITy *V) const {
3044 return L->isLoopInvariant(V) && SubPattern.match(V);
3045 }
3046};
3047
3048/// Matches if the value is loop-invariant.
3049template <typename Ty>
3050inline match_LoopInvariant<Ty> m_LoopInvariant(const Ty &M, const Loop *L) {
3051 return match_LoopInvariant<Ty>(M, L);
3052}
3053
3054/// Return true if the idiom is detected in the loop.
3055///
3056/// The core idiom we are trying to detect is:
3057/// \code
3058/// entry:
3059/// <...>
3060/// %bitmask = shl i32 1, %bitpos
3061/// br label %loop
3062///
3063/// loop:
3064/// %x.curr = phi i32 [ %x, %entry ], [ %x.next, %loop ]
3065/// %x.curr.bitmasked = and i32 %x.curr, %bitmask
3066/// %x.curr.isbitunset = icmp eq i32 %x.curr.bitmasked, 0
3067/// %x.next = shl i32 %x.curr, 1
3068/// <...>
3069/// br i1 %x.curr.isbitunset, label %loop, label %end
3070///
3071/// end:
3072/// %x.curr.res = phi i32 [ %x.curr, %loop ] <...>
3073/// %x.next.res = phi i32 [ %x.next, %loop ] <...>
3074/// <...>
3075/// \endcode
3076static bool detectShiftUntilBitTestIdiom(Loop *CurLoop, Value *&BaseX,
3077 Value *&BitMask, Value *&BitPos,
3078 Value *&CurrX, Instruction *&NextX) {
3080 " Performing shift-until-bittest idiom detection.\n");
3081
3082 // Give up if the loop has multiple blocks or multiple backedges.
3083 if (CurLoop->getNumBlocks() != 1 || CurLoop->getNumBackEdges() != 1) {
3084 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad block/backedge count.\n");
3085 return false;
3086 }
3087
3088 BasicBlock *LoopHeaderBB = CurLoop->getHeader();
3089 BasicBlock *LoopPreheaderBB = CurLoop->getLoopPreheader();
3090 assert(LoopPreheaderBB && "There is always a loop preheader.");
3091
3092 using namespace PatternMatch;
3093
3094 // Step 1: Check if the loop backedge is in desirable form.
3095
3096 CmpPredicate Pred;
3097 Value *CmpLHS, *CmpRHS;
3098 BasicBlock *TrueBB, *FalseBB;
3099 if (!match(LoopHeaderBB->getTerminator(),
3100 m_Br(m_ICmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)),
3101 m_BasicBlock(TrueBB), m_BasicBlock(FalseBB)))) {
3102 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge structure.\n");
3103 return false;
3104 }
3105
3106 // Step 2: Check if the backedge's condition is in desirable form.
3107
3108 auto MatchVariableBitMask = [&]() {
3109 return ICmpInst::isEquality(Pred) && match(CmpRHS, m_Zero()) &&
3110 match(CmpLHS,
3111 m_c_And(m_Value(CurrX),
3113 m_Value(BitMask),
3114 m_LoopInvariant(m_Shl(m_One(), m_Value(BitPos)),
3115 CurLoop))));
3116 };
3117
3118 auto MatchDecomposableConstantBitMask = [&]() {
3119 auto Res = llvm::decomposeBitTestICmp(
3120 CmpLHS, CmpRHS, Pred, /*LookThroughTrunc=*/true,
3121 /*AllowNonZeroC=*/false, /*DecomposeAnd=*/true);
3122 if (Res && Res->Mask.isPowerOf2()) {
3123 assert(ICmpInst::isEquality(Res->Pred));
3124 Pred = Res->Pred;
3125 CurrX = Res->X;
3126 BitMask = ConstantInt::get(CurrX->getType(), Res->Mask);
3127 BitPos = ConstantInt::get(CurrX->getType(), Res->Mask.logBase2());
3128 return true;
3129 }
3130 return false;
3131 };
3132
3133 if (!MatchVariableBitMask() && !MatchDecomposableConstantBitMask()) {
3134 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge comparison.\n");
3135 return false;
3136 }
3137
3138 // Step 3: Check if the recurrence is in desirable form.
3139 auto *CurrXPN = dyn_cast<PHINode>(CurrX);
3140 if (!CurrXPN || CurrXPN->getParent() != LoopHeaderBB) {
3141 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Not an expected PHI node.\n");
3142 return false;
3143 }
3144
3145 BaseX = CurrXPN->getIncomingValueForBlock(LoopPreheaderBB);
3146 NextX =
3147 dyn_cast<Instruction>(CurrXPN->getIncomingValueForBlock(LoopHeaderBB));
3148
3149 assert(CurLoop->isLoopInvariant(BaseX) &&
3150 "Expected BaseX to be available in the preheader!");
3151
3152 if (!NextX || !match(NextX, m_Shl(m_Specific(CurrX), m_One()))) {
3153 // FIXME: support right-shift?
3154 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad recurrence.\n");
3155 return false;
3156 }
3157
3158 // Step 4: Check if the backedge's destinations are in desirable form.
3159
3161 "Should only get equality predicates here.");
3162
3163 // cmp-br is commutative, so canonicalize to a single variant.
3164 if (Pred != ICmpInst::Predicate::ICMP_EQ) {
3165 Pred = ICmpInst::getInversePredicate(Pred);
3166 std::swap(TrueBB, FalseBB);
3167 }
3168
3169 // We expect to exit loop when comparison yields false,
3170 // so when it yields true we should branch back to loop header.
3171 if (TrueBB != LoopHeaderBB) {
3172 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge flow.\n");
3173 return false;
3174 }
3175
3176 // Okay, idiom checks out.
3177 return true;
3178}
3179
3180/// Look for the following loop:
3181/// \code
3182/// entry:
3183/// <...>
3184/// %bitmask = shl i32 1, %bitpos
3185/// br label %loop
3186///
3187/// loop:
3188/// %x.curr = phi i32 [ %x, %entry ], [ %x.next, %loop ]
3189/// %x.curr.bitmasked = and i32 %x.curr, %bitmask
3190/// %x.curr.isbitunset = icmp eq i32 %x.curr.bitmasked, 0
3191/// %x.next = shl i32 %x.curr, 1
3192/// <...>
3193/// br i1 %x.curr.isbitunset, label %loop, label %end
3194///
3195/// end:
3196/// %x.curr.res = phi i32 [ %x.curr, %loop ] <...>
3197/// %x.next.res = phi i32 [ %x.next, %loop ] <...>
3198/// <...>
3199/// \endcode
3200///
3201/// And transform it into:
3202/// \code
3203/// entry:
3204/// %bitmask = shl i32 1, %bitpos
3205/// %lowbitmask = add i32 %bitmask, -1
3206/// %mask = or i32 %lowbitmask, %bitmask
3207/// %x.masked = and i32 %x, %mask
3208/// %x.masked.numleadingzeros = call i32 @llvm.ctlz.i32(i32 %x.masked,
3209/// i1 true)
3210/// %x.masked.numactivebits = sub i32 32, %x.masked.numleadingzeros
3211/// %x.masked.leadingonepos = add i32 %x.masked.numactivebits, -1
3212/// %backedgetakencount = sub i32 %bitpos, %x.masked.leadingonepos
3213/// %tripcount = add i32 %backedgetakencount, 1
3214/// %x.curr = shl i32 %x, %backedgetakencount
3215/// %x.next = shl i32 %x, %tripcount
3216/// br label %loop
3217///
3218/// loop:
3219/// %loop.iv = phi i32 [ 0, %entry ], [ %loop.iv.next, %loop ]
3220/// %loop.iv.next = add nuw i32 %loop.iv, 1
3221/// %loop.ivcheck = icmp eq i32 %loop.iv.next, %tripcount
3222/// <...>
3223/// br i1 %loop.ivcheck, label %end, label %loop
3224///
3225/// end:
3226/// %x.curr.res = phi i32 [ %x.curr, %loop ] <...>
3227/// %x.next.res = phi i32 [ %x.next, %loop ] <...>
3228/// <...>
3229/// \endcode
3230bool LoopIdiomRecognize::recognizeShiftUntilBitTest() {
3231 bool MadeChange = false;
3232
3233 Value *X, *BitMask, *BitPos, *XCurr;
3234 Instruction *XNext;
3235 if (!detectShiftUntilBitTestIdiom(CurLoop, X, BitMask, BitPos, XCurr,
3236 XNext)) {
3238 " shift-until-bittest idiom detection failed.\n");
3239 return MadeChange;
3240 }
3241 LLVM_DEBUG(dbgs() << DEBUG_TYPE " shift-until-bittest idiom detected!\n");
3242
3243 // Ok, it is the idiom we were looking for, we *could* transform this loop,
3244 // but is it profitable to transform?
3245
3246 BasicBlock *LoopHeaderBB = CurLoop->getHeader();
3247 BasicBlock *LoopPreheaderBB = CurLoop->getLoopPreheader();
3248 assert(LoopPreheaderBB && "There is always a loop preheader.");
3249
3250 BasicBlock *SuccessorBB = CurLoop->getExitBlock();
3251 assert(SuccessorBB && "There is only a single successor.");
3252
3253 IRBuilder<> Builder(LoopPreheaderBB->getTerminator());
3254 Builder.SetCurrentDebugLocation(cast<Instruction>(XCurr)->getDebugLoc());
3255
3256 Intrinsic::ID IntrID = Intrinsic::ctlz;
3257 Type *Ty = X->getType();
3258 unsigned Bitwidth = Ty->getScalarSizeInBits();
3259
3262
3263 // The rewrite is considered to be unprofitable iff and only iff the
3264 // intrinsic/shift we'll use are not cheap. Note that we are okay with *just*
3265 // making the loop countable, even if nothing else changes.
3267 IntrID, Ty, {PoisonValue::get(Ty), /*is_zero_poison=*/Builder.getTrue()});
3268 InstructionCost Cost = TTI->getIntrinsicInstrCost(Attrs, CostKind);
3271 " Intrinsic is too costly, not beneficial\n");
3272 return MadeChange;
3273 }
3274 if (TTI->getArithmeticInstrCost(Instruction::Shl, Ty, CostKind) >
3276 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Shift is too costly, not beneficial\n");
3277 return MadeChange;
3278 }
3279
3280 // Ok, transform appears worthwhile.
3281 MadeChange = true;
3282
3283 if (!isGuaranteedNotToBeUndefOrPoison(BitPos)) {
3284 // BitMask may be computed from BitPos, Freeze BitPos so we can increase
3285 // it's use count.
3286 std::optional<BasicBlock::iterator> InsertPt = std::nullopt;
3287 if (auto *BitPosI = dyn_cast<Instruction>(BitPos))
3288 InsertPt = BitPosI->getInsertionPointAfterDef();
3289 else
3290 InsertPt = DT->getRoot()->getFirstNonPHIOrDbgOrAlloca();
3291 if (!InsertPt)
3292 return false;
3293 FreezeInst *BitPosFrozen =
3294 new FreezeInst(BitPos, BitPos->getName() + ".fr", *InsertPt);
3295 BitPos->replaceUsesWithIf(BitPosFrozen, [BitPosFrozen](Use &U) {
3296 return U.getUser() != BitPosFrozen;
3297 });
3298 BitPos = BitPosFrozen;
3299 }
3300
3301 // Step 1: Compute the loop trip count.
3302
3303 Value *LowBitMask = Builder.CreateAdd(BitMask, Constant::getAllOnesValue(Ty),
3304 BitPos->getName() + ".lowbitmask");
3305 Value *Mask =
3306 Builder.CreateOr(LowBitMask, BitMask, BitPos->getName() + ".mask");
3307 Value *XMasked = Builder.CreateAnd(X, Mask, X->getName() + ".masked");
3308 Value *XMaskedNumLeadingZeros = Builder.CreateIntrinsic(
3309 IntrID, Ty, {XMasked, /*is_zero_poison=*/Builder.getTrue()},
3310 /*FMFSource=*/nullptr, XMasked->getName() + ".numleadingzeros");
3311 Value *XMaskedNumActiveBits = Builder.CreateSub(
3312 ConstantInt::get(Ty, Ty->getScalarSizeInBits()), XMaskedNumLeadingZeros,
3313 XMasked->getName() + ".numactivebits", /*HasNUW=*/true,
3314 /*HasNSW=*/Bitwidth != 2);
3315 Value *XMaskedLeadingOnePos =
3316 Builder.CreateAdd(XMaskedNumActiveBits, Constant::getAllOnesValue(Ty),
3317 XMasked->getName() + ".leadingonepos", /*HasNUW=*/false,
3318 /*HasNSW=*/Bitwidth > 2);
3319
3320 Value *LoopBackedgeTakenCount = Builder.CreateSub(
3321 BitPos, XMaskedLeadingOnePos, CurLoop->getName() + ".backedgetakencount",
3322 /*HasNUW=*/true, /*HasNSW=*/true);
3323 // We know loop's backedge-taken count, but what's loop's trip count?
3324 // Note that while NUW is always safe, while NSW is only for bitwidths != 2.
3325 Value *LoopTripCount =
3326 Builder.CreateAdd(LoopBackedgeTakenCount, ConstantInt::get(Ty, 1),
3327 CurLoop->getName() + ".tripcount", /*HasNUW=*/true,
3328 /*HasNSW=*/Bitwidth != 2);
3329
3330 // Step 2: Compute the recurrence's final value without a loop.
3331
3332 // NewX is always safe to compute, because `LoopBackedgeTakenCount`
3333 // will always be smaller than `bitwidth(X)`, i.e. we never get poison.
3334 Value *NewX = Builder.CreateShl(X, LoopBackedgeTakenCount);
3335 NewX->takeName(XCurr);
3336 if (auto *I = dyn_cast<Instruction>(NewX))
3337 I->copyIRFlags(XNext, /*IncludeWrapFlags=*/true);
3338
3339 Value *NewXNext;
3340 // Rewriting XNext is more complicated, however, because `X << LoopTripCount`
3341 // will be poison iff `LoopTripCount == bitwidth(X)` (which will happen
3342 // iff `BitPos` is `bitwidth(x) - 1` and `X` is `1`). So unless we know
3343 // that isn't the case, we'll need to emit an alternative, safe IR.
3344 if (XNext->hasNoSignedWrap() || XNext->hasNoUnsignedWrap() ||
3348 Ty->getScalarSizeInBits() - 1))))
3349 NewXNext = Builder.CreateShl(X, LoopTripCount);
3350 else {
3351 // Otherwise, just additionally shift by one. It's the smallest solution,
3352 // alternatively, we could check that NewX is INT_MIN (or BitPos is )
3353 // and select 0 instead.
3354 NewXNext = Builder.CreateShl(NewX, ConstantInt::get(Ty, 1));
3355 }
3356
3357 NewXNext->takeName(XNext);
3358 if (auto *I = dyn_cast<Instruction>(NewXNext))
3359 I->copyIRFlags(XNext, /*IncludeWrapFlags=*/true);
3360
3361 // Step 3: Adjust the successor basic block to receive the computed
3362 // recurrence's final value instead of the recurrence itself.
3363
3364 XCurr->replaceUsesOutsideBlock(NewX, LoopHeaderBB);
3365 XNext->replaceUsesOutsideBlock(NewXNext, LoopHeaderBB);
3366
3367 // Step 4: Rewrite the loop into a countable form, with canonical IV.
3368
3369 // The new canonical induction variable.
3370 Builder.SetInsertPoint(LoopHeaderBB, LoopHeaderBB->begin());
3371 auto *IV = Builder.CreatePHI(Ty, 2, CurLoop->getName() + ".iv");
3372
3373 // The induction itself.
3374 // Note that while NUW is always safe, while NSW is only for bitwidths != 2.
3375 Builder.SetInsertPoint(LoopHeaderBB->getTerminator());
3376 auto *IVNext =
3377 Builder.CreateAdd(IV, ConstantInt::get(Ty, 1), IV->getName() + ".next",
3378 /*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2);
3379
3380 // The loop trip count check.
3381 auto *IVCheck = Builder.CreateICmpEQ(IVNext, LoopTripCount,
3382 CurLoop->getName() + ".ivcheck");
3383 SmallVector<uint32_t> BranchWeights;
3384 const bool HasBranchWeights =
3386 extractBranchWeights(*LoopHeaderBB->getTerminator(), BranchWeights);
3387
3388 auto *BI = Builder.CreateCondBr(IVCheck, SuccessorBB, LoopHeaderBB);
3389 if (HasBranchWeights) {
3390 if (SuccessorBB == LoopHeaderBB->getTerminator()->getSuccessor(1))
3391 std::swap(BranchWeights[0], BranchWeights[1]);
3392 // We're not changing the loop profile, so we can reuse the original loop's
3393 // profile.
3394 setBranchWeights(*BI, BranchWeights,
3395 /*IsExpected=*/false);
3396 }
3397
3398 LoopHeaderBB->getTerminator()->eraseFromParent();
3399
3400 // Populate the IV PHI.
3401 IV->addIncoming(ConstantInt::get(Ty, 0), LoopPreheaderBB);
3402 IV->addIncoming(IVNext, LoopHeaderBB);
3403
3404 // Step 5: Forget the "non-computable" trip-count SCEV associated with the
3405 // loop. The loop would otherwise not be deleted even if it becomes empty.
3406
3407 SE->forgetLoop(CurLoop);
3408
3409 // Other passes will take care of actually deleting the loop if possible.
3410
3411 LLVM_DEBUG(dbgs() << DEBUG_TYPE " shift-until-bittest idiom optimized!\n");
3412
3413 ++NumShiftUntilBitTest;
3414 return MadeChange;
3415}
3416
3417/// Return true if the idiom is detected in the loop.
3418///
3419/// The core idiom we are trying to detect is:
3420/// \code
3421/// entry:
3422/// <...>
3423/// %start = <...>
3424/// %extraoffset = <...>
3425/// <...>
3426/// br label %for.cond
3427///
3428/// loop:
3429/// %iv = phi i8 [ %start, %entry ], [ %iv.next, %for.cond ]
3430/// %nbits = add nsw i8 %iv, %extraoffset
3431/// %val.shifted = {{l,a}shr,shl} i8 %val, %nbits
3432/// %val.shifted.iszero = icmp eq i8 %val.shifted, 0
3433/// %iv.next = add i8 %iv, 1
3434/// <...>
3435/// br i1 %val.shifted.iszero, label %end, label %loop
3436///
3437/// end:
3438/// %iv.res = phi i8 [ %iv, %loop ] <...>
3439/// %nbits.res = phi i8 [ %nbits, %loop ] <...>
3440/// %val.shifted.res = phi i8 [ %val.shifted, %loop ] <...>
3441/// %val.shifted.iszero.res = phi i1 [ %val.shifted.iszero, %loop ] <...>
3442/// %iv.next.res = phi i8 [ %iv.next, %loop ] <...>
3443/// <...>
3444/// \endcode
3446 Instruction *&ValShiftedIsZero,
3447 Intrinsic::ID &IntrinID, Instruction *&IV,
3448 Value *&Start, Value *&Val,
3449 const SCEV *&ExtraOffsetExpr,
3450 bool &InvertedCond) {
3452 " Performing shift-until-zero idiom detection.\n");
3453
3454 // Give up if the loop has multiple blocks or multiple backedges.
3455 if (CurLoop->getNumBlocks() != 1 || CurLoop->getNumBackEdges() != 1) {
3456 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad block/backedge count.\n");
3457 return false;
3458 }
3459
3460 Instruction *ValShifted, *NBits, *IVNext;
3461 Value *ExtraOffset;
3462
3463 BasicBlock *LoopHeaderBB = CurLoop->getHeader();
3464 BasicBlock *LoopPreheaderBB = CurLoop->getLoopPreheader();
3465 assert(LoopPreheaderBB && "There is always a loop preheader.");
3466
3467 using namespace PatternMatch;
3468
3469 // Step 1: Check if the loop backedge, condition is in desirable form.
3470
3471 CmpPredicate Pred;
3472 BasicBlock *TrueBB, *FalseBB;
3473 if (!match(LoopHeaderBB->getTerminator(),
3474 m_Br(m_Instruction(ValShiftedIsZero), m_BasicBlock(TrueBB),
3475 m_BasicBlock(FalseBB))) ||
3476 !match(ValShiftedIsZero,
3477 m_ICmp(Pred, m_Instruction(ValShifted), m_Zero())) ||
3478 !ICmpInst::isEquality(Pred)) {
3479 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge structure.\n");
3480 return false;
3481 }
3482
3483 // Step 2: Check if the comparison's operand is in desirable form.
3484 // FIXME: Val could be a one-input PHI node, which we should look past.
3485 if (!match(ValShifted, m_Shift(m_LoopInvariant(m_Value(Val), CurLoop),
3486 m_Instruction(NBits)))) {
3487 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad comparisons value computation.\n");
3488 return false;
3489 }
3490 IntrinID = ValShifted->getOpcode() == Instruction::Shl ? Intrinsic::cttz
3491 : Intrinsic::ctlz;
3492
3493 // Step 3: Check if the shift amount is in desirable form.
3494
3495 if (match(NBits, m_c_Add(m_Instruction(IV),
3496 m_LoopInvariant(m_Value(ExtraOffset), CurLoop))) &&
3497 (NBits->hasNoSignedWrap() || NBits->hasNoUnsignedWrap()))
3498 ExtraOffsetExpr = SE->getNegativeSCEV(SE->getSCEV(ExtraOffset));
3499 else if (match(NBits,
3501 m_LoopInvariant(m_Value(ExtraOffset), CurLoop))) &&
3502 NBits->hasNoSignedWrap())
3503 ExtraOffsetExpr = SE->getSCEV(ExtraOffset);
3504 else {
3505 IV = NBits;
3506 ExtraOffsetExpr = SE->getZero(NBits->getType());
3507 }
3508
3509 // Step 4: Check if the recurrence is in desirable form.
3510 auto *IVPN = dyn_cast<PHINode>(IV);
3511 if (!IVPN || IVPN->getParent() != LoopHeaderBB) {
3512 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Not an expected PHI node.\n");
3513 return false;
3514 }
3515
3516 Start = IVPN->getIncomingValueForBlock(LoopPreheaderBB);
3517 IVNext = dyn_cast<Instruction>(IVPN->getIncomingValueForBlock(LoopHeaderBB));
3518
3519 if (!IVNext || !match(IVNext, m_Add(m_Specific(IVPN), m_One()))) {
3520 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad recurrence.\n");
3521 return false;
3522 }
3523
3524 // Step 4: Check if the backedge's destinations are in desirable form.
3525
3527 "Should only get equality predicates here.");
3528
3529 // cmp-br is commutative, so canonicalize to a single variant.
3530 InvertedCond = Pred != ICmpInst::Predicate::ICMP_EQ;
3531 if (InvertedCond) {
3532 Pred = ICmpInst::getInversePredicate(Pred);
3533 std::swap(TrueBB, FalseBB);
3534 }
3535
3536 // We expect to exit loop when comparison yields true,
3537 // so when it yields false we should branch back to loop header.
3538 if (FalseBB != LoopHeaderBB) {
3539 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Bad backedge flow.\n");
3540 return false;
3541 }
3542
3543 // The new, countable, loop will certainly only run a known number of
3544 // iterations, It won't be infinite. But the old loop might be infinite
3545 // under certain conditions. For logical shifts, the value will become zero
3546 // after at most bitwidth(%Val) loop iterations. However, for arithmetic
3547 // right-shift, iff the sign bit was set, the value will never become zero,
3548 // and the loop may never finish.
3549 if (ValShifted->getOpcode() == Instruction::AShr &&
3550 !isMustProgress(CurLoop) && !SE->isKnownNonNegative(SE->getSCEV(Val))) {
3551 LLVM_DEBUG(dbgs() << DEBUG_TYPE " Can not prove the loop is finite.\n");
3552 return false;
3553 }
3554
3555 // Okay, idiom checks out.
3556 return true;
3557}
3558
3559/// Look for the following loop:
3560/// \code
3561/// entry:
3562/// <...>
3563/// %start = <...>
3564/// %extraoffset = <...>
3565/// <...>
3566/// br label %loop
3567///
3568/// loop:
3569/// %iv = phi i8 [ %start, %entry ], [ %iv.next, %loop ]
3570/// %nbits = add nsw i8 %iv, %extraoffset
3571/// %val.shifted = {{l,a}shr,shl} i8 %val, %nbits
3572/// %val.shifted.iszero = icmp eq i8 %val.shifted, 0
3573/// %iv.next = add i8 %iv, 1
3574/// <...>
3575/// br i1 %val.shifted.iszero, label %end, label %loop
3576///
3577/// end:
3578/// %iv.res = phi i8 [ %iv, %loop ] <...>
3579/// %nbits.res = phi i8 [ %nbits, %loop ] <...>
3580/// %val.shifted.res = phi i8 [ %val.shifted, %loop ] <...>
3581/// %val.shifted.iszero.res = phi i1 [ %val.shifted.iszero, %loop ] <...>
3582/// %iv.next.res = phi i8 [ %iv.next, %loop ] <...>
3583/// <...>
3584/// \endcode
3585///
3586/// And transform it into:
3587/// \code
3588/// entry:
3589/// <...>
3590/// %start = <...>
3591/// %extraoffset = <...>
3592/// <...>
3593/// %val.numleadingzeros = call i8 @llvm.ct{l,t}z.i8(i8 %val, i1 0)
3594/// %val.numactivebits = sub i8 8, %val.numleadingzeros
3595/// %extraoffset.neg = sub i8 0, %extraoffset
3596/// %tmp = add i8 %val.numactivebits, %extraoffset.neg
3597/// %iv.final = call i8 @llvm.smax.i8(i8 %tmp, i8 %start)
3598/// %loop.tripcount = sub i8 %iv.final, %start
3599/// br label %loop
3600///
3601/// loop:
3602/// %loop.iv = phi i8 [ 0, %entry ], [ %loop.iv.next, %loop ]
3603/// %loop.iv.next = add i8 %loop.iv, 1
3604/// %loop.ivcheck = icmp eq i8 %loop.iv.next, %loop.tripcount
3605/// %iv = add i8 %loop.iv, %start
3606/// <...>
3607/// br i1 %loop.ivcheck, label %end, label %loop
3608///
3609/// end:
3610/// %iv.res = phi i8 [ %iv.final, %loop ] <...>
3611/// <...>
3612/// \endcode
3613bool LoopIdiomRecognize::recognizeShiftUntilZero() {
3614 bool MadeChange = false;
3615
3616 Instruction *ValShiftedIsZero;
3617 Intrinsic::ID IntrID;
3618 Instruction *IV;
3619 Value *Start, *Val;
3620 const SCEV *ExtraOffsetExpr;
3621 bool InvertedCond;
3622 if (!detectShiftUntilZeroIdiom(CurLoop, SE, ValShiftedIsZero, IntrID, IV,
3623 Start, Val, ExtraOffsetExpr, InvertedCond)) {
3625 " shift-until-zero idiom detection failed.\n");
3626 return MadeChange;
3627 }
3628 LLVM_DEBUG(dbgs() << DEBUG_TYPE " shift-until-zero idiom detected!\n");
3629
3630 // Ok, it is the idiom we were looking for, we *could* transform this loop,
3631 // but is it profitable to transform?
3632
3633 BasicBlock *LoopHeaderBB = CurLoop->getHeader();
3634 BasicBlock *LoopPreheaderBB = CurLoop->getLoopPreheader();
3635 assert(LoopPreheaderBB && "There is always a loop preheader.");
3636
3637 BasicBlock *SuccessorBB = CurLoop->getExitBlock();
3638 assert(SuccessorBB && "There is only a single successor.");
3639
3640 IRBuilder<> Builder(LoopPreheaderBB->getTerminator());
3641 Builder.SetCurrentDebugLocation(IV->getDebugLoc());
3642
3643 Type *Ty = Val->getType();
3644 unsigned Bitwidth = Ty->getScalarSizeInBits();
3645
3648
3649 // The rewrite is considered to be unprofitable iff and only iff the
3650 // intrinsic we'll use are not cheap. Note that we are okay with *just*
3651 // making the loop countable, even if nothing else changes.
3653 IntrID, Ty, {PoisonValue::get(Ty), /*is_zero_poison=*/Builder.getFalse()});
3654 InstructionCost Cost = TTI->getIntrinsicInstrCost(Attrs, CostKind);
3657 " Intrinsic is too costly, not beneficial\n");
3658 return MadeChange;
3659 }
3660
3661 // Ok, transform appears worthwhile.
3662 MadeChange = true;
3663
3664 bool OffsetIsZero = ExtraOffsetExpr->isZero();
3665
3666 // Step 1: Compute the loop's final IV value / trip count.
3667
3668 Value *ValNumLeadingZeros = Builder.CreateIntrinsic(
3669 IntrID, Ty, {Val, /*is_zero_poison=*/Builder.getFalse()},
3670 /*FMFSource=*/nullptr, Val->getName() + ".numleadingzeros");
3671 Value *ValNumActiveBits = Builder.CreateSub(
3672 ConstantInt::get(Ty, Ty->getScalarSizeInBits()), ValNumLeadingZeros,
3673 Val->getName() + ".numactivebits", /*HasNUW=*/true,
3674 /*HasNSW=*/Bitwidth != 2);
3675
3676 SCEVExpander Expander(*SE, "loop-idiom");
3677 Expander.setInsertPoint(&*Builder.GetInsertPoint());
3678 Value *ExtraOffset = Expander.expandCodeFor(ExtraOffsetExpr);
3679
3680 Value *ValNumActiveBitsOffset = Builder.CreateAdd(
3681 ValNumActiveBits, ExtraOffset, ValNumActiveBits->getName() + ".offset",
3682 /*HasNUW=*/OffsetIsZero, /*HasNSW=*/true);
3683 Value *IVFinal = Builder.CreateIntrinsic(Intrinsic::smax, {Ty},
3684 {ValNumActiveBitsOffset, Start},
3685 /*FMFSource=*/nullptr, "iv.final");
3686
3687 auto *LoopBackedgeTakenCount = cast<Instruction>(Builder.CreateSub(
3688 IVFinal, Start, CurLoop->getName() + ".backedgetakencount",
3689 /*HasNUW=*/OffsetIsZero, /*HasNSW=*/true));
3690 // FIXME: or when the offset was `add nuw`
3691
3692 // We know loop's backedge-taken count, but what's loop's trip count?
3693 Value *LoopTripCount =
3694 Builder.CreateAdd(LoopBackedgeTakenCount, ConstantInt::get(Ty, 1),
3695 CurLoop->getName() + ".tripcount", /*HasNUW=*/true,
3696 /*HasNSW=*/Bitwidth != 2);
3697
3698 // Step 2: Adjust the successor basic block to receive the original
3699 // induction variable's final value instead of the orig. IV itself.
3700
3701 IV->replaceUsesOutsideBlock(IVFinal, LoopHeaderBB);
3702
3703 // Step 3: Rewrite the loop into a countable form, with canonical IV.
3704
3705 // The new canonical induction variable.
3706 Builder.SetInsertPoint(LoopHeaderBB, LoopHeaderBB->begin());
3707 auto *CIV = Builder.CreatePHI(Ty, 2, CurLoop->getName() + ".iv");
3708
3709 // The induction itself.
3710 Builder.SetInsertPoint(LoopHeaderBB, LoopHeaderBB->getFirstNonPHIIt());
3711 auto *CIVNext =
3712 Builder.CreateAdd(CIV, ConstantInt::get(Ty, 1), CIV->getName() + ".next",
3713 /*HasNUW=*/true, /*HasNSW=*/Bitwidth != 2);
3714
3715 // The loop trip count check.
3716 auto *CIVCheck = Builder.CreateICmpEQ(CIVNext, LoopTripCount,
3717 CurLoop->getName() + ".ivcheck");
3718 auto *NewIVCheck = CIVCheck;
3719 if (InvertedCond) {
3720 NewIVCheck = Builder.CreateNot(CIVCheck);
3721 NewIVCheck->takeName(ValShiftedIsZero);
3722 }
3723
3724 // The original IV, but rebased to be an offset to the CIV.
3725 auto *IVDePHId = Builder.CreateAdd(CIV, Start, "", /*HasNUW=*/false,
3726 /*HasNSW=*/true); // FIXME: what about NUW?
3727 IVDePHId->takeName(IV);
3728
3729 // The loop terminator.
3730 Builder.SetInsertPoint(LoopHeaderBB->getTerminator());
3731 SmallVector<uint32_t> BranchWeights;
3732 const bool HasBranchWeights =
3734 extractBranchWeights(*LoopHeaderBB->getTerminator(), BranchWeights);
3735
3736 auto *BI = Builder.CreateCondBr(CIVCheck, SuccessorBB, LoopHeaderBB);
3737 if (HasBranchWeights) {
3738 if (InvertedCond)
3739 std::swap(BranchWeights[0], BranchWeights[1]);
3740 // We're not changing the loop profile, so we can reuse the original loop's
3741 // profile.
3742 setBranchWeights(*BI, BranchWeights, /*IsExpected=*/false);
3743 }
3744 LoopHeaderBB->getTerminator()->eraseFromParent();
3745
3746 // Populate the IV PHI.
3747 CIV->addIncoming(ConstantInt::get(Ty, 0), LoopPreheaderBB);
3748 CIV->addIncoming(CIVNext, LoopHeaderBB);
3749
3750 // Step 4: Forget the "non-computable" trip-count SCEV associated with the
3751 // loop. The loop would otherwise not be deleted even if it becomes empty.
3752
3753 SE->forgetLoop(CurLoop);
3754
3755 // Step 5: Try to cleanup the loop's body somewhat.
3756 IV->replaceAllUsesWith(IVDePHId);
3757 IV->eraseFromParent();
3758
3759 ValShiftedIsZero->replaceAllUsesWith(NewIVCheck);
3760 ValShiftedIsZero->eraseFromParent();
3761
3762 // Other passes will take care of actually deleting the loop if possible.
3763
3764 LLVM_DEBUG(dbgs() << DEBUG_TYPE " shift-until-zero idiom optimized!\n");
3765
3766 ++NumShiftUntilZero;
3767 return MadeChange;
3768}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
#define X(NUM, ENUM, NAME)
Definition ELF.h:856
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static cl::opt< OutputCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(OutputCostKind::RecipThroughput), cl::values(clEnumValN(OutputCostKind::RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(OutputCostKind::Latency, "latency", "Instruction latency"), clEnumValN(OutputCostKind::CodeSize, "code-size", "Code size"), clEnumValN(OutputCostKind::SizeAndLatency, "size-latency", "Code size and latency"), clEnumValN(OutputCostKind::All, "all", "Print all cost kinds")))
DXIL Resource Access
This file defines the DenseMap class.
#define DEBUG_TYPE
ManagedStatic< HTTPClientCleanup > Cleanup
static bool mayLoopAccessLocation(Value *Ptr, ModRefInfo Access, Loop *L, const SCEV *BECount, unsigned StoreSize, AliasAnalysis &AA, SmallPtrSetImpl< Instruction * > &Ignored)
mayLoopAccessLocation - Return true if the specified loop might access the specified pointer location...
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
This file defines an InstructionCost class that is used when calculating the cost of an instruction,...
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static PHINode * getRecurrenceVar(Value *VarX, Instruction *DefX, BasicBlock *LoopEntry)
static Value * createPopcntIntrinsic(IRBuilder<> &IRBuilder, Value *Val, const DebugLoc &DL)
static Value * matchShiftULTCondition(CondBrInst *BI, BasicBlock *LoopEntry, APInt &Threshold)
Check if the given conditional branch is based on an unsigned less-than comparison between a variable...
static bool detectShiftUntilLessThanIdiom(Loop *CurLoop, const DataLayout &DL, Intrinsic::ID &IntrinID, Value *&InitX, Instruction *&CntInst, PHINode *&CntPhi, Instruction *&DefX, APInt &Threshold)
Return true if the idiom is detected in the loop.
static Value * matchCondition(CondBrInst *BI, BasicBlock *LoopEntry, bool JmpOnZero=false)
Check if the given conditional branch is based on the comparison between a variable and zero,...
static bool detectShiftUntilBitTestIdiom(Loop *CurLoop, Value *&BaseX, Value *&BitMask, Value *&BitPos, Value *&CurrX, Instruction *&NextX)
Return true if the idiom is detected in the loop.
static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB, Instruction *&CntInst, PHINode *&CntPhi, Value *&Var)
Return true iff the idiom is detected in the loop.
static Constant * getMemSetPatternValue(Value *V, const DataLayout *DL)
getMemSetPatternValue - If a strided store of the specified value is safe to turn into a memset....
static const SCEV * getNumBytes(const SCEV *BECount, Type *IntPtr, const SCEV *StoreSizeSCEV, Loop *CurLoop, const DataLayout *DL, ScalarEvolution *SE)
Compute the number of bytes as a SCEV from the backedge taken count.
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.
static Value * createFFSIntrinsic(IRBuilder<> &IRBuilder, Value *Val, const DebugLoc &DL, bool ZeroCheck, Intrinsic::ID IID)
static const SCEV * getStartForNegStride(const SCEV *Start, const SCEV *BECount, Type *IntPtr, const SCEV *StoreSizeSCEV, ScalarEvolution *SE)
static APInt getStoreStride(const SCEVAddRecExpr *StoreEv)
match_LoopInvariant< Ty > m_LoopInvariant(const Ty &M, const Loop *L)
Matches if the value is loop-invariant.
static bool isSameByteValueStore(Instruction &I, Value *SplatByte, Loop *L, const DataLayout &DL)
Return true if I is a (simple, loop-invariant-valued) store of the same bytewise value SplatByte.
static void deleteDeadInstruction(Instruction *I)
#define I(x, y, z)
Definition MD5.cpp:57
This file implements a map that provides insertion order iteration.
This file provides utility analysis objects describing memory locations.
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
#define T
Contains a collection of routines for determining if a given instruction is guaranteed to execute if ...
if(PassOpts->AAPipeline)
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
Func MI getDebugLoc()))
This file contains some templates that are useful if you are working with the STL at all.
verify safepoint Safepoint IR Verifier
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
This pass exposes codegen information to IR-level passes.
Value * LHS
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
std::optional< uint64_t > tryZExtValue() const
Get zero extended value if possible.
Definition APInt.h:1577
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1565
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1513
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:307
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1587
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:474
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:461
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:530
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
const Instruction & front() const
Definition BasicBlock.h:484
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
LLVM_ABI const_iterator getFirstNonPHIOrDbgOrAlloca() const
Returns an iterator to the first instruction in this block that is not a PHINode, a debug intrinsic,...
size_t size() const
Definition BasicBlock.h:482
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
BinaryOps getOpcode() const
Definition InstrTypes.h:409
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
This class represents a function call, abstracting a target machine's calling convention.
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
Definition InstrTypes.h:831
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ ICMP_NE
not equal
Definition InstrTypes.h:762
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:828
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Conditional Branch instruction.
void setCondition(Value *V)
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
static LLVM_ABI Constant * get(ArrayType *T, ArrayRef< Constant * > V)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Definition Constants.h:231
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition Constants.h:225
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:219
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
A debug info location.
Definition DebugLoc.h:126
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This class represents a freeze function that returns random concrete value if an operand is either a ...
PointerType * getType() const
Global values are always pointers.
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
static LLVM_ABI CRCTable genSarwateTable(const APInt &GenPoly, bool IsBigEndian)
Generate a lookup table of 256 entries by interleaving the generating polynomial.
static LLVM_ABI std::pair< APInt, APInt > genBarrettConstants(const PolynomialInfo &Info)
Auxilary entry point after analysis to generate constants for a GF(2) Barrett Reduction.
This instruction compares its operands according to the predicate given to the constructor.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
Common base class shared among various IRBuilders.
Definition IRBuilder.h:114
ConstantInt * getInt1(bool V)
Get a constant value representing either true or false.
Definition IRBuilder.h:452
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy.
Definition IRBuilder.h:2139
void SetCurrentDebugLocation(const DebugLoc &L)
Set location information used by debugging information.
Definition IRBuilder.h:221
LLVM_ABI Value * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > OverloadTypes, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="", ArrayRef< OperandBundleDef > OpBundles={}, function_ref< void(CallInst *)> SetFn=[](CallInst *) {})
Variant to create a possibly constant-folded intrinsic.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2893
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
LLVM_ABI bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isShift() const
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
Class to represent integer types.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:348
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
This class provides an interface for updating the loop pass manager based on mutations to the loop ne...
An instruction for reading from memory.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Value * getPointerOperand()
bool isVolatile() const
Return true if this is a load from a volatile memory location.
bool isUnordered() const
Align getAlign() const
Return the alignment of the access that is being performed.
static LocationSize precise(uint64_t Value)
bool isPrecise() const
static constexpr LocationSize afterPointer()
Any location after the base pointer (but still within the underlying object).
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
bool isOutermost() const
Return true if the loop does not have a parent (natural) loop.
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
unsigned getNumBlocks() const
Get the number of blocks in this loop in constant time.
unsigned getNumBackEdges() const
Calculate the number of back edges to the loop header.
BlockT * getHeader() const
BlockT * getExitBlock() const
If getExitBlocks would return exactly one block, return that block.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
ArrayRef< BlockT * > getBlocks() const
Get a list of the basic blocks which make up this loop.
void getUniqueExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all unique successor blocks of this loop.
block_iterator block_begin() const
BlockT * getUniqueExitBlock() const
If getUniqueExitBlocks would return exactly one block, return that block.
LLVM_ABI PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM, LoopStandardAnalysisResults &AR, LPMUpdater &U)
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
Definition LoopInfo.cpp:659
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition LoopInfo.cpp:67
ICmpInst * getLatchCmpInst() const
Get the latch condition instruction.
Definition LoopInfo.cpp:198
StringRef getName() const
Definition LoopInfo.h:407
PHINode * getCanonicalInductionVariable() const
Check to see if the loop has a canonical induction variable: an integer recurrence that starts at 0 a...
Definition LoopInfo.cpp:174
This class wraps the llvm.memcpy intrinsic.
Value * getLength() const
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions (including addrspacecast) that ...
MaybeAlign getDestAlign() const
bool isForceInlined() const
bool isVolatile() const
Value * getValue() const
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
MaybeAlign getSourceAlign() const
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it,...
Representation for a specific memory location.
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:922
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition MemorySSA.h:702
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for missed-optimization remarks.
Diagnostic information for applied optimization remarks.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
This node represents a polynomial recurrence on the trip count of the specified loop.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
SCEVUse getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
This class represents a constant integer value.
ConstantInt * getValue() const
const APInt & getAPInt() const
Helper to remove instructions inserted during SCEV expansion, unless they are marked as used.
This class uses information about analyze scalars to rewrite expressions in canonical form.
SCEVUse getOperand(unsigned i) const
This class represents an analyzed expression in the program.
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
static constexpr auto FlagNUW
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
LLVM_ABI bool isNonConstantNegative() const
Return true if the specified scev is negated, but not a constant.
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
const DataLayout & getDataLayout() const
Return the DataLayout associated with the module this SCEV instance is operating on.
LLVM_ABI bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI const SCEV * getTripCountFromExitCount(const SCEV *ExitCount)
A version of getTripCountFromExitCount below which always picks an evaluation type which can not resu...
LLVM_ABI 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...
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count.
LLVM_ABI const SCEV * getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI const SCEV * getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
LLVM_ABI const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
A vector that has set insertion semantics.
Definition SetVector.h:57
size_type count(const_arg_type key) const
Count the number of elements of a given key in the SetVector.
Definition SetVector.h:262
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
Simple and conservative implementation of LoopSafetyInfo that can give false-positive answers to its ...
void computeLoopSafetyInfo(const Loop *CurLoop) override
Computes safety information for a loop checks loop body & header for the possibility of may throw exc...
bool anyBlockMayThrow() const override
Returns true iff any block of the loop for which this info is contains an instruction that may throw ...
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Align getAlign() const
Value * getValueOperand()
Value * getPointerOperand()
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Provides information about what library functions are available for the current target.
unsigned getWCharSize(const Module &M) const
Returns the size of the wchar_t type in bytes or 0 if the size is unknown.
bool has(LibFunc F) const
Tests whether a library function is available.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
TargetCostKind
The kind of cost model.
@ TCK_SizeAndLatency
The weighted sum of size and latency.
@ TCC_Basic
The cost of a typical 'add' instruction.
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getIntegerBitWidth() const
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition Type.h:130
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition Type.h:186
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:270
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:313
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
void setOperand(unsigned i, Value *Val)
Definition User.h:212
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:258
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI 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:611
LLVM_ABI bool replaceUsesWithIf(Value *New, llvm::function_ref< bool(Use &U)> ShouldReplace)
Go through the uses list for this definition and make each use point to "V" if the callback ShouldRep...
Definition Value.cpp:561
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
Definition Value.cpp:400
Value handle that is nullable, but tries to track the Value.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
const ParentTy * getParent() const
Definition ilist_node.h:34
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:165
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
@ HeaderSize
Definition BTF.h:61
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
OperandType
Operands are tagged with one of the values of this enum.
Definition MCInstrDesc.h:59
match_combine_and< Ty... > m_CombineAnd(const Ty &...Ps)
Combine pattern matchers matching all of Ps patterns.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
bool match(Val *V, const Pattern &P)
match_bind< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
auto m_BasicBlock()
Match an arbitrary basic block value and ignore it.
auto m_Value()
Match an arbitrary value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
cst_pred_ty< icmp_pred_with_threshold > m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold)
Match an integer or vector with every element comparing 'pred' (eg/ne/...) to Threshold.
bind_cst_ty m_scev_APInt(const APInt *&C)
Match an SCEV constant and bind it to an APInt.
specificloop_ty m_SpecificLoop(const Loop *L)
bool match(const SCEV *S, const Pattern &P)
specificscev_ty m_scev_Specific(const SCEV *S)
Match if we have a specific specified SCEV.
SCEVAffineAddRec_match< Op0_t, Op1_t, match_isa< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
constexpr double e
DiagnosticInfoOptimizationBase::Argument NV
DiagnosticInfoOptimizationBase::setExtraArgs setExtraArgs
bool isSimple(Instruction *I)
Definition SLPUtils.cpp:532
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI cl::opt< bool > ProfcheckDisableMetadataFixes
Definition LoopInfo.cpp:60
LLVM_ABI bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition Local.cpp:535
static cl::opt< bool, true > EnableLIRPWcslen("disable-loop-idiom-wcslen", cl::desc("Proceed with loop idiom recognize pass, " "enable conversion of loop(s) to wcslen."), cl::location(DisableLIRP::Wcslen), cl::init(false), cl::ReallyHidden)
InstructionCost Cost
static cl::opt< bool, true > DisableLIRPMemcpy("disable-" DEBUG_TYPE "-memcpy", cl::desc("Proceed with loop idiom recognize pass, but do " "not convert loop(s) to memcpy."), cl::location(DisableLIRP::Memcpy), cl::init(false), cl::ReallyHidden)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
static cl::opt< bool, true > DisableLIRPStrlen("disable-loop-idiom-strlen", cl::desc("Proceed with loop idiom recognize pass, but do " "not convert loop(s) to strlen."), cl::location(DisableLIRP::Strlen), cl::init(false), cl::ReallyHidden)
@ Store
The extracted value is stored (ExtractElement only).
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
static cl::opt< bool > ForceMemsetPatternIntrinsic("loop-idiom-force-memset-pattern-intrinsic", cl::desc("Use memset.pattern intrinsic whenever possible"), cl::init(false), cl::Hidden)
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI bool isLibFuncEmittable(const Module *M, const TargetLibraryInfo *TLI, LibFunc TheLibFunc)
Check whether the library function is available on target and also that it in the current Module is a...
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
AnalysisManager< Loop, LoopStandardAnalysisResults & > LoopAnalysisManager
The loop analysis manager.
static cl::opt< bool > ForceCRCClmul("loop-idiom-force-crc-clmul", cl::desc("Use the clmul-based CRC loop optimization whenever possible"), cl::init(false), cl::Hidden)
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition STLExtras.h:2026
LLVM_ABI bool isMustProgress(const Loop *L)
Return true if this loop can be assumed to make progress.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isModOrRefSet(const ModRefInfo MRI)
Definition ModRef.h:43
LLVM_ABI bool RecursivelyDeleteDeadPHINode(PHINode *PN, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, SmallPtrSetImpl< PHINode * > *KnownNonDeadPHIs=nullptr)
If the specified value is an effectively dead PHI node, due to being a def-use chain of single-use no...
Definition Local.cpp:643
LLVM_ABI Value * emitStrLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the strlen function to the builder, for the specified pointer.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
ModRefInfo
Flags indicating whether a memory access modifies or references memory.
Definition ModRef.h:28
@ ModRef
The access may reference and may modify the value stored in memory.
Definition ModRef.h:36
@ Mod
The access may modify the value stored in memory.
Definition ModRef.h:34
static cl::opt< bool, true > DisableLIRPHashRecognize("disable-" DEBUG_TYPE "-hashrecognize", cl::desc("Proceed with loop idiom recognize pass, " "but do not optimize CRC loops."), cl::location(DisableLIRP::HashRecognize), cl::init(false), cl::ReallyHidden)
TargetTransformInfo TTI
LLVM_ABI bool VerifyMemorySSA
Enables verification of MemorySSA.
Definition MemorySSA.cpp:85
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
LLVM_ABI 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.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
LLVM_ABI Value * emitWcsLen(Value *Ptr, IRBuilderBase &B, const DataLayout &DL, const TargetLibraryInfo *TLI)
Emit a call to the wcslen function to the builder, for the specified pointer.
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
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)
static cl::opt< bool, true > DisableLIRPMemset("disable-" DEBUG_TYPE "-memset", cl::desc("Proceed with loop idiom recognize pass, but do " "not convert loop(s) to memset."), cl::location(DisableLIRP::Memset), cl::init(false), cl::ReallyHidden)
static cl::opt< bool, true > DisableLIRPAll("disable-" DEBUG_TYPE "-all", cl::desc("Options to disable Loop Idiom Recognize Pass."), cl::location(DisableLIRP::All), cl::init(false), cl::ReallyHidden)
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI std::optional< DecomposedBitTest > decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate Pred, bool LookThroughTrunc=true, bool AllowNonZeroC=false, bool DecomposeAnd=false)
Decompose an icmp into the form ((X & Mask) pred C) if possible.
SCEVUseT< const SCEV * > SCEVUse
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition Metadata.h:763
LLVM_ABI AAMDNodes merge(const AAMDNodes &Other) const
Given two sets of AAMDNodes applying to potentially different locations, determine the best AAMDNodes...
AAMDNodes extendTo(ssize_t Len) const
Create a new AAMDNode that describes this AAMDNode after extending it to apply to a series of bytes o...
Definition Metadata.h:836
static LLVM_ABI bool Memcpy
When true, Memcpy is disabled.
static LLVM_ABI bool Wcslen
When true, Wcslen is disabled.
static LLVM_ABI bool Strlen
When true, Strlen is disabled.
static LLVM_ABI bool HashRecognize
When true, HashRecognize is disabled.
static LLVM_ABI bool Memset
When true, Memset is disabled.
static LLVM_ABI bool All
When true, the entire pass is disabled.
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition Alignment.h:106
The structure that is returned when a polynomial algorithm was recognized by the analysis.
Match loop-invariant value.
match_LoopInvariant(const SubPattern_t &SP, const Loop *L)