diff options
author | Philip Reames <preames@rivosinc.com> | 2023-10-31 08:45:06 -0700 |
---|---|---|
committer | Philip Reames <listmail@philipreames.com> | 2023-11-03 09:20:59 -0700 |
commit | a6c8e27b3a052913a15a13ee0d4ac466c5ab3f92 (patch) | |
tree | 507ee561309af5060e64574ef1b257768c2b163f /llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | |
parent | 1e39575a981088e8596461a3511cce3ec4c3b274 (diff) | |
download | llvm-a6c8e27b3a052913a15a13ee0d4ac466c5ab3f92.zip llvm-a6c8e27b3a052913a15a13ee0d4ac466c5ab3f92.tar.gz llvm-a6c8e27b3a052913a15a13ee0d4ac466c5ab3f92.tar.bz2 |
[IndVars] Generate zext nneg when locally obvious
zext nneg was recently added to the IR in #67982. This patch teaches
SimplifyIndVars to prefer zext nneg over *both* sext and plain zext,
when a local SCEV query indicates the source is non-negative.
The choice to prefer zext nneg over sext looks slightly aggressive
here, but probably isn't so much in practice. For cases where we'd
"remember" the range fact, instcombine would convert the sext into
a zext nneg anyways. The only cases where this produces a different
result overall are when SCEV knows a non-local fact, and it doesn't
get materialized into the IR. Those are exactly the cases where
using zext nneg are most useful. We do run the risk of e.g. a
missing combine - since we haven't updated most of them yet - but
that seems like a manageable risk.
Note that there are much deeper algorithmic changes we could make
to this code to exploit zext nneg, but this seemed like a reasonable
and low risk starting point.
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyIndVar.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index ae36441..de2556f 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -1201,6 +1201,15 @@ Value *WidenIV::createExtendInst(Value *NarrowOper, Type *WideType, L = L->getParentLoop()) Builder.SetInsertPoint(L->getLoopPreheader()->getTerminator()); + // If we know the operand is never negative, prefer zext nneg. + // For constant expressions, fall back to plain sext or zext. + if (SE->isKnownNonNegative(SE->getSCEV(NarrowOper))) { + auto *Res = Builder.CreateZExt(NarrowOper, WideType); + if (auto *I = dyn_cast<Instruction>(Res)) + I->setNonNeg(true); + return Res; + } + return IsSigned ? Builder.CreateSExt(NarrowOper, WideType) : Builder.CreateZExt(NarrowOper, WideType); } @@ -1686,6 +1695,16 @@ bool WidenIV::widenWithVariantUse(WidenIV::NarrowIVDefUse DU) { auto ExtendedOp = [&](Value * V)->Value * { if (V == NarrowUse) return WideBO; + + // If we know the operand is never negative, prefer zext nneg. + // For constant expressions, fall back to plain sext or zext. + if (SE->isKnownNonNegative(SE->getSCEV(V))) { + auto *Res = Builder.CreateZExt(V, WideBO->getType()); + if (auto *I = dyn_cast<Instruction>(Res)) + I->setNonNeg(true); + return Res; + } + if (ExtKind == ExtendKind::Zero) return Builder.CreateZExt(V, WideBO->getType()); else |