diff --git a/llvm/include/llvm/Transforms/Scalar/LoopIdiomRecognize.h b/llvm/include/llvm/Transforms/Scalar/LoopIdiomRecognize.h --- a/llvm/include/llvm/Transforms/Scalar/LoopIdiomRecognize.h +++ b/llvm/include/llvm/Transforms/Scalar/LoopIdiomRecognize.h @@ -44,7 +44,11 @@ LoopStandardAnalysisResults &AR, LPMUpdater &U); }; -// NFC LoopNestPass with regards to the current LoopPass-LoopIdiomRecognize +// The LoopNestIdiomRecognize is a LoopNestPass that feeds LoopNest into +// LoopIdiomRecognize. The main difference from LoopIdiomRecognize is it +// allows runtime-determined store size optimization by versioning and creates +// runtime checks on the top-level loop. The reason to only version on the +// top-level loop is to avoid the exponential growth of versioning. class LoopNestIdiomRecognizePass : public PassInfoMixin { public: diff --git a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp --- a/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp +++ b/llvm/lib/Transforms/Scalar/LoopIdiomRecognize.cpp @@ -96,6 +96,7 @@ #include "llvm/Transforms/Utils/BuildLibCalls.h" #include "llvm/Transforms/Utils/Local.h" #include "llvm/Transforms/Utils/LoopUtils.h" +#include "llvm/Transforms/Utils/LoopVersioning.h" #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h" #include #include @@ -108,6 +109,8 @@ #define DEBUG_TYPE "loop-idiom" STATISTIC(NumMemSet, "Number of memset's formed from loop stores"); +STATISTIC(NumMemSetRuntimeLength, + "Number of memset's formed from memset with runtime length"); STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores"); STATISTIC( NumShiftUntilBitTest, @@ -144,11 +147,39 @@ "with -Os/-Oz"), cl::init(true), cl::Hidden); +static cl::opt ForceNoLoopVersion( + DEBUG_TYPE "-no-loop-version", + cl::desc("Force not to create loop versions if the user guarantees that" + "the length of each array dimension is positive value, and" + "the multiplication of lengths of all array dimensions does not" + "exceeds the range of type size_t"), + cl::init(false), cl::Hidden); + namespace { +typedef SmallSet SCEVSet; +typedef DenseMap SCEVConstValPairMap; + +/// A helper class to do the following SCEV expression conversions. +/// 1) "sext %val" to "zext %val" +/// 2) "SOME_CONSTANT_VALUE smax %val" to "%val" +class SCEVExprConverter { +public: + ScalarEvolution &SE; + SCEVConstValPairMap CheckSltMap; + + SCEVExprConverter(ScalarEvolution &SE) + : SE(SE) {} + + const SCEV *convertSCEV(const SCEV *Expr); +}; + class LoopIdiomRecognize { Loop *CurLoop = nullptr; LoopNest *LN; + Loop *TopLoop = nullptr; + Loop *FallBackLoop = nullptr; + BasicBlock *RuntimeCheckBB = nullptr; AliasAnalysis *AA; DominatorTree *DT; LoopInfo *LI; @@ -159,6 +190,8 @@ OptimizationRemarkEmitter &ORE; bool ApplyCodeSizeHeuristics; std::unique_ptr MSSAU; + SCEVConstValPairMap CheckSltMap; + SCEVExprConverter Converter; public: explicit LoopIdiomRecognize(AliasAnalysis *AA, DominatorTree *DT, @@ -168,9 +201,11 @@ const DataLayout *DL, OptimizationRemarkEmitter &ORE) : LN(LN), AA(AA), DT(DT), LI(LI), SE(SE), TLI(TLI), TTI(TTI), DL(DL), - ORE(ORE) { + ORE(ORE), Converter(*SE) { if (MSSA) MSSAU = std::make_unique(MSSA); + if (LN) + TopLoop = &LN->getOutermostLoop(); } bool runOnLoopNest(); @@ -235,6 +270,8 @@ const SCEV *BECount); bool avoidLIRForMultiBlockLoop(bool IsMemset = false, bool IsLoopMemset = false); + bool isTopLoopVersioned() const { return RuntimeCheckBB != nullptr; } + void versionTopLoop(); /// @} /// \name Noncountable Loop Idiom Handling @@ -311,6 +348,133 @@ } // end anonymous namespace +/// Implementation of SCEVExprConverter. +const SCEV *SCEVExprConverter::convertSCEV(const SCEV *Expr) { + switch (Expr->getSCEVType()) { + case scConstant: + case scUnknown: + case scCouldNotCompute: + return Expr; + case scTruncate: { + const SCEVTruncateExpr *Trunc = cast(Expr); + Type *Ty = Trunc->getType(); + const SCEV *NewTrunc = convertSCEV(Trunc->getOperand()); + return SE.getTruncateExpr(NewTrunc, Ty); + } + case scZeroExtend: { + const SCEVZeroExtendExpr *Zext = cast(Expr); + Type *Ty = Zext->getType(); + const SCEV *NewZext = convertSCEV(Zext->getOperand()); + return SE.getZeroExtendExpr(NewZext, Ty); + } + case scSignExtend: { + // Record the original SCEV sext expression, and + // convert it to zext. + const SCEVSignExtendExpr *Sext = cast(Expr); + if (CheckSltMap[Sext] < 0) + CheckSltMap[Sext] = 0; + Type *Ty = Sext->getType(); + const SCEV *NewZext = convertSCEV(Sext->getOperand()); + return SE.getZeroExtendExpr(NewZext, Ty); + } + case scAddExpr: { + const SCEVAddExpr *Add = cast(Expr); + const SCEV *NewAdd = convertSCEV(Add->getOperand(0)); + for (int I = 1, E = Add->getNumOperands(); I != E; ++I) { + NewAdd = SE.getAddExpr(NewAdd, convertSCEV(Add->getOperand(I))); + } + return NewAdd; + } + case scMulExpr: { + const SCEVMulExpr *Mul = cast(Expr); + const SCEV *NewMul = convertSCEV(Mul->getOperand(0)); + for (int I = 1, E = Mul->getNumOperands(); I != E; ++I) { + NewMul = SE.getMulExpr(NewMul, convertSCEV(Mul->getOperand(I))); + } + return NewMul; + } + case scUDivExpr: { + const SCEVUDivExpr *UDiv = cast(Expr); + const SCEV *NewLHS = convertSCEV(UDiv->getLHS()); + const SCEV *NewRHS = convertSCEV(UDiv->getRHS()); + return SE.getUDivExpr(NewLHS, NewRHS); + } + case scAddRecExpr: + assert(false && "Do not expect AddRec here!"); + case scUMaxExpr: { + const SCEVUMaxExpr *UMax = cast(Expr); + const SCEV *NewUMax = convertSCEV(UMax->getOperand(0)); + for (int I = 1, E = UMax->getNumOperands(); I != E; ++I) { + NewUMax = SE.getUMaxExpr(NewUMax, convertSCEV(UMax->getOperand(I))); + } + return NewUMax; + } + case scSMaxExpr: { + const SCEVSMaxExpr *SMax = cast(Expr); + const int NumOfOps = SMax->getNumOperands(); + bool Fold = false; + // If an operand is constant zero, it will be the first operand. + const SCEV *SMaxOp0 = SMax->getOperand(0); + const SCEVConstant *LHSC = dyn_cast(SMaxOp0); + + if (LHSC) { + // fold the constant with the other operands. there will be + // runtime-check to check our assumption for folding the smax is + // feasible. + Fold = true; + for (int I = 1, E = NumOfOps; I != E; ++I) { + auto Ev = SMax->getOperand(I); + auto Cst = LHSC->getAPInt().roundToDouble(); + if (CheckSltMap[Ev] < Cst) + CheckSltMap[Ev] = Cst; + } + } + + const int StartIdx = Fold ? 1 : 0; + const SCEV *NewSMax = convertSCEV(SMax->getOperand(StartIdx)); + for (int I = StartIdx + 1, E = NumOfOps; I != E; ++I) { + NewSMax = SE.getSMaxExpr(NewSMax, convertSCEV(SMax->getOperand(I))); + } + return NewSMax; + } + case scUMinExpr: { + const SCEVUMinExpr *UMin = cast(Expr); + const SCEV *NewUMin = convertSCEV(UMin->getOperand(0)); + for (int I = 1, E = UMin->getNumOperands(); I != E; ++I) { + NewUMin = SE.getUMinExpr(NewUMin, convertSCEV(UMin->getOperand(I))); + } + return NewUMin; + } + case scSMinExpr: { + const SCEVSMinExpr *SMin = cast(Expr); + const SCEV *NewSMin = convertSCEV(SMin->getOperand(0)); + for (int I = 1, E = SMin->getNumOperands(); I != E; ++I) { + NewSMin = SE.getSMinExpr(NewSMin, convertSCEV(SMin->getOperand(I))); + } + return NewSMin; + } + default: + llvm_unreachable("Unexpected SCEV expression!"); + } +} + +/// Helper function to generate predicate "X < ConstVal". +// This function is used to add runtime check conditions for value that we +// assume in order to canonicalize the SCEV for comparison. +// For example: turn sext to zext, eliminate smax +static Value *generateSltCstPredicate(const SCEV *Ev, int ConstVal, + BranchInst *BI, + const DataLayout *DL, + ScalarEvolution *SE, + IRBuilder<> &Builder) { + SCEVExpander Expander(*SE, *DL, "loop-nest-idiom-runtime-bound-check"); + Type *Ty = Ev->getType(); + Value *Val = Expander.expandCodeFor(Ev, Ty, BI); + Constant *Cst = ConstantInt::get(Ty, ConstVal); + + return Builder.CreateICmpSLT(Val, Cst); +} + char LoopIdiomRecognizeLegacyPass::ID = 0; PreservedAnalyses LoopIdiomRecognizePass::run(Loop &L, LoopAnalysisManager &AM, @@ -400,6 +564,35 @@ Changed |= runOnLoop(L); } + // After processing all the loops, we now add the stored conditions into + // the RuntimeCheckBB. Conditions are stored when: + // - detect runtime store size in StridedStore + if (Changed && isTopLoopVersioned()) { + // Get the branch instruction in the runtime check basic block. + BranchInst *BI = dyn_cast(RuntimeCheckBB->getTerminator()); + assert(BI && "Expects a BranchInst"); + LLVM_DEBUG(dbgs() << "runOnLoopNest: Add runtime check to RuntimeCheckBB\n"); + // Create conditional branch instructions for SCEV that is folded. + // If any of the condition above is true, the fallback loop is taken. + // Otherwise, the optimized loop is taken. + LLVMContext &Context = TopLoop->getHeader()->getContext(); + Value *Cond = ConstantInt::getFalse(Context); + + IRBuilder<> Builder(BI); + // add runtime check for SCEV we fold in SCEVExprConverter + for (auto Pair : CheckSltMap) { + const SCEV *Ev = Pair.first; + const int Cst = Pair.second; + LLVM_DEBUG(dbgs() << " check: " << *Ev << " < " << Cst << "\n"); + Value *CheckInBoundCond = generateSltCstPredicate(Ev, Cst, BI, DL, SE, Builder); + Cond = Builder.CreateOr(Cond, CheckInBoundCond); + } + + BranchInst::Create(FallBackLoop->getLoopPreheader(), + LN->getOutermostLoop().getLoopPreheader(), Cond, BI); + deleteDeadInstruction(BI); + } + return Changed; } @@ -462,8 +655,11 @@ // Scan all the blocks in the loop that are not in subloops. for (auto *BB : CurLoop->getBlocks()) { // Ignore blocks in subloops. - if (LI->getLoopFor(BB) != CurLoop) + LLVM_DEBUG(dbgs() << "Loop Block: " << BB->getName() << "\n"); + if (LI->getLoopFor(BB) != CurLoop) { + LLVM_DEBUG(dbgs() << "block is not on current loop, abort\n"); continue; + } MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks); } @@ -673,8 +869,10 @@ // executed in the loop. For a block to be unconditionally executed, it has // to dominate all the exit blocks of the loop. Verify this now. for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) - if (!DT->dominates(BB, ExitBlocks[i])) + if (!DT->dominates(BB, ExitBlocks[i])) { + LLVM_DEBUG(dbgs() << "does not dominate all exit blocks to promote the store, abort.\n"); return false; + } bool MadeChange = false; // Look for store instructions, which may be optimized to memset/memcpy. @@ -858,6 +1056,7 @@ Instruction *Inst = &*I++; // Look for memory instructions, which may be optimized to a larger one. if (MemInst *MI = dyn_cast(Inst)) { + LLVM_DEBUG(dbgs() << "found MemInst: " << *MI << "\n"); WeakTrackingVH InstPtr(&*I); if (!(this->*Processor)(MI, BECount)) continue; @@ -944,7 +1143,7 @@ bool LoopIdiomRecognize::processLoopMemSet(MemSetInst *MSI, const SCEV *BECount) { // We can only handle non-volatile memsets with a constant size. - if (MSI->isVolatile() || !isa(MSI->getLength())) + if (MSI->isVolatile()) return false; // If we're not allowed to hack on memset, we fail. @@ -956,28 +1155,89 @@ // See if the pointer expression is an AddRec like {base,+,1} on the current // loop, which indicates a strided store. If we have something else, it's a // random store we can't handle. + const SCEV *PointerSCEV = SE->getSCEV(Pointer); + if (PointerSCEV) + LLVM_DEBUG(dbgs() << "PointerSCEV: " << *PointerSCEV << "\n"); const SCEVAddRecExpr *Ev = dyn_cast(SE->getSCEV(Pointer)); - if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine()) + if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine()) { + LLVM_DEBUG(dbgs() << "PointerSCEV cannot be converted to SCEVAddRecExpr, abort\n"); return false; - + } + const SCEV *StrideSCEV = Ev->getOperand(1); const SCEV *MemsetSizeSCEV = SE->getSCEV(MSI->getLength()); - if (!MemsetSizeSCEV) + if (!StrideSCEV || !MemsetSizeSCEV) return false; - // Reject memsets that are so large that they overflow an unsigned. - uint64_t SizeInBytes = cast(MSI->getLength())->getZExtValue(); - if ((SizeInBytes >> 32) != 0) - return false; + bool NegStride; + const bool IsConstantSize = isa(MSI->getLength()); + if (IsConstantSize) { + // Memset size is constant + // Reject memsets that are so large that they overflow an unsigned. + LLVM_DEBUG(dbgs() << " memset size is constant\n"); + uint64_t SizeInBytes = cast(MSI->getLength())->getZExtValue(); + if ((SizeInBytes >> 32) != 0) + return false; - // Check to see if the stride matches the size of the memset. If so, then we - // know that every byte is touched in the loop. - const SCEVConstant *ConstStride = dyn_cast(Ev->getOperand(1)); - if (!ConstStride) - return false; + // Check to see if the stride matches the size of the memset. If so, then + // we know that every byte is touched in the loop. + const SCEVConstant *ConstStride = dyn_cast(Ev->getOperand(1)); + if (!ConstStride) + return false; - APInt Stride = ConstStride->getAPInt(); - if (SizeInBytes != Stride && SizeInBytes != -Stride) - return false; + APInt Stride = ConstStride->getAPInt(); + if (SizeInBytes != Stride && SizeInBytes != -Stride) + return false; + + NegStride = SizeInBytes == -Stride; + } else { + // Memset size is non-constant + // Check if the stride matches the memset size, by comparing the SCEV + // expressions of the stride and memset size. The two expressions match + // if they are equal. If they match, then we know that every byte is + // touched in the loop. We only handle memset length and stride that + // are invariant for the top level loop. + LLVM_DEBUG(dbgs() << " memset size is non-constant\n"); + if (LN == nullptr) { + LLVM_DEBUG(dbgs() << " need to call LNIR for non-constant memset" + << "optimization\n"); + return false; + } + if (!SE->isLoopInvariant(MemsetSizeSCEV, TopLoop) || + !SE->isLoopInvariant(StrideSCEV, TopLoop)) { + LLVM_DEBUG(dbgs() << " memset size or stride is not a loop-invariant, " + << "abort\n"); + return false; + } + + // compare positive direction strideSCEV with MemsizeSizeSCEV + NegStride = StrideSCEV->isNonConstantNegative(); + const SCEV *PositiveStrideSCEV = + NegStride ? SE->getNegativeSCEV(StrideSCEV) : StrideSCEV; + LLVM_DEBUG(dbgs() << " MemsetSizeSCEV: " << *MemsetSizeSCEV << "\n" + << " PositiveStrideSCEV: " << *PositiveStrideSCEV + << "\n"); + + if (PositiveStrideSCEV != MemsetSizeSCEV) { + // We will convert the SCEV expressions, and compare again. + // required conversion to SCEV will be saved inside Converter. + // if this function returns true, which means the optimization does happen, + // the pair will be added when we return to processLoopMemIntrinsic. + Converter.CheckSltMap.clear(); + const SCEV *PositiveStrideSCEVConv = + Converter.convertSCEV(PositiveStrideSCEV); + const SCEV *MemsetSizeSCEVConv = + Converter.convertSCEV(MemsetSizeSCEV); + LLVM_DEBUG(dbgs() << " Try to convert SCEV expression and compare again\n" + << " MemsetSCEVConv: " << *MemsetSizeSCEVConv << "\n" + << " PositiveStrideSCEVConv: " + << *PositiveStrideSCEVConv << "\n"); + + if (PositiveStrideSCEVConv != MemsetSizeSCEVConv) { + LLVM_DEBUG(dbgs() << " Converted SCEV still inequal, abort\n"); + return false; + } + } + } // Verify that the memset value is loop invariant. If not, we can't promote // the memset. @@ -987,10 +1247,23 @@ SmallPtrSet MSIs; MSIs.insert(MSI); - bool NegStride = SizeInBytes == -Stride; - return processLoopStridedStore( + bool Changed = processLoopStridedStore( Pointer, MemsetSizeSCEV, MaybeAlign(MSI->getDestAlignment()), SplatValue, MSI, MSIs, Ev, BECount, NegStride, /*IsLoopMemset=*/true); + + // if we have successfully changed with processLoopStridedStore + // add the require runtime check information into list. + if (Changed) { + for (auto Pair : Converter.CheckSltMap) { + auto Ev = Pair.first; + auto Cst = Pair.second; + if (CheckSltMap[Ev] < Cst) + CheckSltMap[Ev] = Cst; + } + Converter.CheckSltMap.clear(); + } + + return Changed; } /// mayLoopAccessLocation - Return true if the specified loop might access the @@ -1180,8 +1453,12 @@ bool Changed = false; const SCEV *Start = Ev->getStart(); // Handle negative strided loops. - if (NegStride) + if (NegStride) { + LLVM_DEBUG(dbgs() << "Handle negative stride," + << " original Start: " << Start << "\n"); Start = getStartForNegStride(Start, BECount, IntIdxTy, StoreSizeSCEV, SE); + LLVM_DEBUG(dbgs() << " converted Start: " << Start << "\n"); + } // TODO: ideally we should still be able to generate memset if SCEV expander // is taught to generate the dependencies at the latest point. @@ -1216,15 +1493,37 @@ // NumBytes = TripCount * StoreSize const SCEV *TripCountS = getTripCount(BECount, IntIdxTy, CurLoop, DL, SE); + + // This check is possible only for LoopNestIdiomRecognize, since we are + // trying to version on the top-level loop. + // Give up if the store size is not constant and the trip count SCEV + // expression is variant to the top level loop. In this sense versioning is + // needed and compile option enforces not to. + if (LN != nullptr && !SE->isLoopInvariant(TripCountS, TopLoop)) { + const bool IsConstantSize = isa(StoreSizeSCEV); + if (IsLoopMemset && !IsConstantSize && ForceNoLoopVersion) { + LLVM_DEBUG(dbgs() << "requires versioning but abort becuase " + << "ForceNoLoopVersion is set to true\n"); + return Changed; + } + } + const SCEVConstant *ConstSize = dyn_cast(StoreSizeSCEV); const SCEV *NumBytesS; - if (!ConstSize && ConstSize->getAPInt() == 1) + + if (ConstSize && ConstSize->getAPInt() == 1) { NumBytesS = TripCountS; - else + LLVM_DEBUG(dbgs() << "StoreSize = 1, NumbytesS: " << *NumBytesS << "\n"); + } else { NumBytesS = SE->getMulExpr(TripCountS, SE->getTruncateOrZeroExtend(StoreSizeSCEV, IntIdxTy), SCEV::FlagNUW); + LLVM_DEBUG(dbgs() << " Calculate NumBytesS = TripCountS * StoreSizeSCEV\n" + << " TripCountS: " << *TripCountS << "\n" + << " StoreSizeSCEV: " << *StoreSizeSCEV << "\n" + << " NumBytesS: " << *NumBytesS << "\n"); + } // TODO: ideally we should still be able to generate memset if SCEV expander // is taught to generate the dependencies at the latest point. @@ -1234,6 +1533,28 @@ Value *NumBytes = Expander.expandCodeFor(NumBytesS, IntIdxTy, Preheader->getTerminator()); + // If the memset size is not a constant, we will need to version the top + // level loop in the current loop nest with runtime checks. We are going + // to version on only the top level loop once to avoid exponential growth + // of versioning. + // Here we check whether the top level clone has beed created yet, and create + // it if it hasn't. The initial runtime check is set to false and the + // conditions would be updated after we process all the loops. + const bool IsConstantSize = isa(StoreSizeSCEV); + if (LN != nullptr && IsLoopMemset && !IsConstantSize && !ForceNoLoopVersion) { + if (!isTopLoopVersioned() && Converter.CheckSltMap.size()) { + LLVM_DEBUG(dbgs() << " Create versioning for top loop because SCEV folding is needed\n"); + versionTopLoop(); + + // If current loop is the top loop, versioning would change the loop's + // preheader to RuntimeCheckBB, so we need to reset the insert point. + if (CurLoop == TopLoop) { + Preheader = CurLoop->getLoopPreheader(); + Builder.SetInsertPoint(Preheader->getTerminator()); + } + } + } + CallInst *NewCall; if (SplatValue) { NewCall = Builder.CreateMemSet(BasePtr, SplatValue, NumBytes, @@ -1291,6 +1612,8 @@ MSSAU->getMemorySSA()->verifyMemorySSA(); ++NumMemSet; ExpCleaner.markResultUsed(); + if (IsLoopMemset && !IsConstantSize) + ++NumMemSetRuntimeLength; return true; } @@ -1503,6 +1826,18 @@ return false; } +/// versionTopLoop - Create a fallback version the TopLoop +void LoopIdiomRecognize::versionTopLoop() { + const LoopAccessInfo LAI(TopLoop, SE, TLI, AA, DT, LI); + LoopVersioning LV(LAI, LAI.getRuntimePointerChecking()->getChecks(), TopLoop, + LI, DT, SE); + + LV.versionLoopWithPlainRuntimeCheck(); + + RuntimeCheckBB = LV.getRuntimeCheckBB(); + FallBackLoop = LV.getNonVersionedLoop(); +} + bool LoopIdiomRecognize::runOnNoncountableLoop() { LLVM_DEBUG(dbgs() << DEBUG_TYPE " Scanning: F[" << CurLoop->getHeader()->getParent()->getName() diff --git a/llvm/test/Transforms/LoopIdiom/memset-runtime.ll b/llvm/test/Transforms/LoopIdiom/memset-runtime.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/LoopIdiom/memset-runtime.ll @@ -0,0 +1,289 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -passes="function(loop(loop-nest-idiom,loop-deletion),simplifycfg)" < %s -S | FileCheck %s +; The C code to generate this testcase: +; void test(int ar[][m], long n, long m) +; { +; long i; +; for (i=0; i> 32) != 0 || (4 * m >> 32) != 0) +; /* optimization result identical to LoopIdiomRecognize */ +; else +; /* hoists memset to loop-preheader */ +; } +define void @test_simple(i32* nocapture %ar, i64 %n, i64 %m) { +; CHECK-LABEL: @test_simple( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[AR1:%.*]] = bitcast i32* [[AR:%.*]] to i8* +; CHECK-NEXT: [[TMP0:%.*]] = shl nuw i64 [[M:%.*]], 2 +; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[M]], [[N:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = shl i64 [[TMP1]], 2 +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 4 [[AR1]], i8 0, i64 [[TMP2]], i1 false) +; CHECK-NEXT: ret void +; +entry: + %0 = shl nuw i64 %m, 2 + br label %for.cond1.preheader + +for.cond1.preheader: ; preds = %for.inc4, %entry + %i.017 = phi i64 [ 0, %entry ], [ %inc5, %for.inc4 ] + %1 = mul i64 %m, %i.017 + %scevgep = getelementptr i32, i32* %ar, i64 %1 + %scevgep1 = bitcast i32* %scevgep to i8* + %mul = mul nsw i64 %i.017, %m + call void @llvm.memset.p0i8.i64(i8* align 4 %scevgep1, i8 0, i64 %0, i1 false) + br label %for.inc4 + +for.inc4: ; preds = %for.cond1.preheader + %inc5 = add nuw nsw i64 %i.017, 1 + %exitcond18.not = icmp eq i64 %inc5, %n + br i1 %exitcond18.not, label %for.end6, label %for.cond1.preheader + +for.end6: ; preds = %for.inc4 + ret void +} + +; The C code to generate this testcase: +; void test(int n, int m, int o, int *ar) +; { +; long i = 0, j; +; do { +; j = 0; +; do { +; int *arr = ar + i * m * o + j * o; +; memset(arr, 0, o * sizeof(int)); +; ++j; +; } while (j < m); +; ++i; +; } while (i < n); +; } +define void @test_nested_do_while(i32 %n, i32 %m, i32 %o, i32* nocapture %ar){ +; CHECK-LABEL: @test_nested_do_while( +; CHECK-NEXT: do.body.lver.check: +; CHECK-NEXT: [[AR2:%.*]] = bitcast i32* [[AR:%.*]] to i8* +; CHECK-NEXT: [[CONV:%.*]] = sext i32 [[M:%.*]] to i64 +; CHECK-NEXT: [[CONV2:%.*]] = sext i32 [[O:%.*]] to i64 +; CHECK-NEXT: [[MUL:%.*]] = mul nsw i64 [[CONV2]], [[CONV]] +; CHECK-NEXT: [[MUL8:%.*]] = shl nsw i64 [[CONV2]], 2 +; CHECK-NEXT: [[SMAX:%.*]] = call i64 @llvm.smax.i64(i64 [[CONV]], i64 1) +; CHECK-NEXT: [[TMP0:%.*]] = call i32 @llvm.smax.i32(i32 [[N:%.*]], i32 1) +; CHECK-NEXT: [[SMAX27:%.*]] = zext i32 [[TMP0]] to i64 +; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[CONV2]], [[CONV]] +; CHECK-NEXT: [[TMP2:%.*]] = mul i64 [[SMAX]], [[CONV2]] +; CHECK-NEXT: [[TMP3:%.*]] = shl i64 [[TMP2]], 2 +; CHECK-NEXT: [[TMP4:%.*]] = mul i64 [[TMP2]], [[SMAX27]] +; CHECK-NEXT: [[TMP5:%.*]] = shl i64 [[TMP4]], 2 +; CHECK-NEXT: [[TMP6:%.*]] = icmp slt i64 [[CONV]], 1 +; CHECK-NEXT: [[TMP7:%.*]] = or i1 false, [[TMP6]] +; CHECK-NEXT: [[TMP8:%.*]] = icmp slt i64 [[CONV2]], 0 +; CHECK-NEXT: [[TMP9:%.*]] = or i1 [[TMP7]], [[TMP8]] +; CHECK-NEXT: br i1 [[TMP9]], label [[DO_BODY_LVER_ORIG:%.*]], label [[DO_BODY_PH:%.*]] +; CHECK: do.body.lver.orig: +; CHECK-NEXT: [[I_0_LVER_ORIG:%.*]] = phi i64 [ [[INC11_LVER_ORIG:%.*]], [[DO_END_LVER_ORIG:%.*]] ], [ 0, [[DO_BODY_LVER_CHECK:%.*]] ] +; CHECK-NEXT: [[TMP10:%.*]] = mul i64 [[TMP1]], [[I_0_LVER_ORIG]] +; CHECK-NEXT: [[SCEVGEP_LVER_ORIG:%.*]] = getelementptr i32, i32* [[AR]], i64 [[TMP10]] +; CHECK-NEXT: [[SCEVGEP1_LVER_ORIG:%.*]] = bitcast i32* [[SCEVGEP_LVER_ORIG]] to i8* +; CHECK-NEXT: [[MUL3_LVER_ORIG:%.*]] = mul i64 [[MUL]], [[I_0_LVER_ORIG]] +; CHECK-NEXT: [[ADD_PTR_LVER_ORIG:%.*]] = getelementptr inbounds i32, i32* [[AR]], i64 [[MUL3_LVER_ORIG]] +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 4 [[SCEVGEP1_LVER_ORIG]], i8 0, i64 [[TMP3]], i1 false) +; CHECK-NEXT: br label [[DO_BODY1_LVER_ORIG:%.*]] +; CHECK: do.body1.lver.orig: +; CHECK-NEXT: [[J_0_LVER_ORIG:%.*]] = phi i64 [ 0, [[DO_BODY_LVER_ORIG]] ], [ [[INC_LVER_ORIG:%.*]], [[DO_BODY1_LVER_ORIG]] ] +; CHECK-NEXT: [[MUL5_LVER_ORIG:%.*]] = mul nsw i64 [[J_0_LVER_ORIG]], [[CONV2]] +; CHECK-NEXT: [[ADD_PTR6_LVER_ORIG:%.*]] = getelementptr inbounds i32, i32* [[ADD_PTR_LVER_ORIG]], i64 [[MUL5_LVER_ORIG]] +; CHECK-NEXT: [[TMP11:%.*]] = bitcast i32* [[ADD_PTR6_LVER_ORIG]] to i8* +; CHECK-NEXT: [[INC_LVER_ORIG]] = add nuw nsw i64 [[J_0_LVER_ORIG]], 1 +; CHECK-NEXT: [[EXITCOND_NOT_LVER_ORIG:%.*]] = icmp eq i64 [[INC_LVER_ORIG]], [[SMAX]] +; CHECK-NEXT: br i1 [[EXITCOND_NOT_LVER_ORIG]], label [[DO_END_LVER_ORIG]], label [[DO_BODY1_LVER_ORIG]] +; CHECK: do.end.lver.orig: +; CHECK-NEXT: [[INC11_LVER_ORIG]] = add nuw nsw i64 [[I_0_LVER_ORIG]], 1 +; CHECK-NEXT: [[EXITCOND28_NOT_LVER_ORIG:%.*]] = icmp eq i64 [[INC11_LVER_ORIG]], [[SMAX27]] +; CHECK-NEXT: br i1 [[EXITCOND28_NOT_LVER_ORIG]], label [[DO_END16:%.*]], label [[DO_BODY_LVER_ORIG]] +; CHECK: do.body.ph: +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 4 [[AR2]], i8 0, i64 [[TMP5]], i1 false) +; CHECK-NEXT: br label [[DO_END16]] +; CHECK: do.end16: +; CHECK-NEXT: ret void +; +entry: + %conv = sext i32 %m to i64 + %conv2 = sext i32 %o to i64 + %mul = mul nsw i64 %conv2, %conv + %mul8 = shl nsw i64 %conv2, 2 + %smax = call i64 @llvm.smax.i64(i64 %conv, i64 1) + %0 = call i32 @llvm.smax.i32(i32 %n, i32 1) + %smax27 = zext i32 %0 to i64 + br label %do.body + +do.body: ; preds = %do.end, %entry + %i.0 = phi i64 [ 0, %entry ], [ %inc11, %do.end ] + %mul3 = mul i64 %mul, %i.0 + %add.ptr = getelementptr inbounds i32, i32* %ar, i64 %mul3 + br label %do.body1 + +do.body1: ; preds = %do.body1, %do.body + %j.0 = phi i64 [ 0, %do.body ], [ %inc, %do.body1 ] + %mul5 = mul nsw i64 %j.0, %conv2 + %add.ptr6 = getelementptr inbounds i32, i32* %add.ptr, i64 %mul5 + %1 = bitcast i32* %add.ptr6 to i8* + call void @llvm.memset.p0i8.i64(i8* align 4 %1, i8 0, i64 %mul8, i1 false) + %inc = add nuw nsw i64 %j.0, 1 + %exitcond.not = icmp eq i64 %inc, %smax + br i1 %exitcond.not, label %do.end, label %do.body1 + +do.end: ; preds = %do.body1 + %inc11 = add nuw nsw i64 %i.0, 1 + %exitcond28.not = icmp eq i64 %inc11, %smax27 + br i1 %exitcond28.not, label %do.end16, label %do.body + +do.end16: ; preds = %do.end + ret void +} +; The C code to generate this testcase: +; void test(int n, int m, int o, int *ar) +; { +; for (int i=0; i