From 2892fcc204f30ff16d9adb382a67d7125598813d Mon Sep 17 00:00:00 2001 From: Max Kazantsev Date: Mon, 1 Mar 2021 12:07:46 +0700 Subject: [PATCH] [NFC] Factor out IV detector function for further reuse --- llvm/lib/CodeGen/CodeGenPrepare.cpp | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index ed83fad..b62c332 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -1276,23 +1276,30 @@ static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI, return SinkCast(CI); } +static bool isIVIncrement(const BinaryOperator *BO, const LoopInfo *LI) { + auto *PN = dyn_cast(BO->getOperand(0)); + if (!PN) + return false; + const Loop *L = LI->getLoopFor(BO->getParent()); + if (!L || L->getHeader() != PN->getParent() || !L->getLoopLatch()) + return false; + const BasicBlock *Latch = L->getLoopLatch(); + if (PN->getIncomingValueForBlock(Latch) != BO) + return false; + if (!L->isLoopInvariant(BO->getOperand(1))) + // Avoid complexities w/loop varying steps. + return false; + return true; +} + bool CodeGenPrepare::replaceMathCmpWithIntrinsic(BinaryOperator *BO, Value *Arg0, Value *Arg1, CmpInst *Cmp, Intrinsic::ID IID) { - auto isIVIncrement = [this, &Cmp](BinaryOperator *BO) { - auto *PN = dyn_cast(BO->getOperand(0)); - if (!PN) + auto IsReplacableIVIncrement = [this, &Cmp](BinaryOperator *BO) { + if (!isIVIncrement(BO, LI)) return false; const Loop *L = LI->getLoopFor(BO->getParent()); - if (!L || L->getHeader() != PN->getParent() || !L->getLoopLatch()) - return false; - const BasicBlock *Latch = L->getLoopLatch(); - if (PN->getIncomingValueForBlock(Latch) != BO) - return false; - if (!L->isLoopInvariant(BO->getOperand(1))) - // Avoid complexities w/loop varying steps. - return false; // IV increment may have other users than the IV. We do not want to make // dominance queries to analyze the legality of moving it towards the cmp, // so just check that there is no other users. @@ -1305,9 +1312,9 @@ bool CodeGenPrepare::replaceMathCmpWithIntrinsic(BinaryOperator *BO, // cheap check because no CFG changes & dom tree recomputation happens // during the transform. Function *F = BO->getParent()->getParent(); - return getDT(*F).dominates(Cmp->getParent(), Latch); + return getDT(*F).dominates(Cmp->getParent(), L->getLoopLatch()); }; - if (BO->getParent() != Cmp->getParent() && !isIVIncrement(BO)) { + if (BO->getParent() != Cmp->getParent() && !IsReplacableIVIncrement(BO)) { // We used to use a dominator tree here to allow multi-block optimization. // But that was problematic because: // 1. It could cause a perf regression by hoisting the math op into the -- 2.7.4