2011-08-10 11:46:27 +08:00
|
|
|
//===-- SimplifyIndVar.cpp - Induction variable simplification ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements induction variable simplification. It does
|
|
|
|
// not define any actual pass or policy, but provides a single function to
|
|
|
|
// simplify a loop's induction variables based on ScalarEvolution.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Transforms/Utils/SimplifyIndVar.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2011-08-10 11:46:27 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
2017-10-12 10:54:11 +08:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpander.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Instructions.h"
|
2013-12-24 07:31:49 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2017-07-05 21:25:58 +08:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2011-08-10 11:46:27 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "indvars"
|
|
|
|
|
2011-08-10 11:46:27 +08:00
|
|
|
STATISTIC(NumElimIdentity, "Number of IV identities eliminated");
|
|
|
|
STATISTIC(NumElimOperand, "Number of IV operands folded into a use");
|
2017-09-27 11:11:46 +08:00
|
|
|
STATISTIC(NumFoldedUser, "Number of IV users folded into a constant");
|
2011-08-10 11:46:27 +08:00
|
|
|
STATISTIC(NumElimRem , "Number of IV remainder operations eliminated");
|
2017-03-31 05:56:56 +08:00
|
|
|
STATISTIC(
|
|
|
|
NumSimplifiedSDiv,
|
|
|
|
"Number of IV signed division operations converted to unsigned division");
|
2017-09-26 01:39:40 +08:00
|
|
|
STATISTIC(
|
|
|
|
NumSimplifiedSRem,
|
|
|
|
"Number of IV signed remainder operations converted to unsigned remainder");
|
2011-08-10 11:46:27 +08:00
|
|
|
STATISTIC(NumElimCmp , "Number of IV comparisons eliminated");
|
|
|
|
|
|
|
|
namespace {
|
2014-11-13 02:07:42 +08:00
|
|
|
/// This is a utility for simplifying induction variables
|
2011-08-10 11:46:27 +08:00
|
|
|
/// based on ScalarEvolution. It is the primary instrument of the
|
|
|
|
/// IndvarSimplify pass, but it may also be directly invoked to cleanup after
|
|
|
|
/// other loop passes that preserve SCEV.
|
|
|
|
class SimplifyIndvar {
|
|
|
|
Loop *L;
|
|
|
|
LoopInfo *LI;
|
|
|
|
ScalarEvolution *SE;
|
2015-10-07 05:44:49 +08:00
|
|
|
DominatorTree *DT;
|
2017-10-12 10:54:11 +08:00
|
|
|
SCEVExpander &Rewriter;
|
2017-05-02 01:07:49 +08:00
|
|
|
SmallVectorImpl<WeakTrackingVH> &DeadInsts;
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
bool Changed;
|
|
|
|
|
|
|
|
public:
|
2015-10-07 05:44:49 +08:00
|
|
|
SimplifyIndvar(Loop *Loop, ScalarEvolution *SE, DominatorTree *DT,
|
2017-10-12 10:54:11 +08:00
|
|
|
LoopInfo *LI, SCEVExpander &Rewriter,
|
|
|
|
SmallVectorImpl<WeakTrackingVH> &Dead)
|
|
|
|
: L(Loop), LI(LI), SE(SE), DT(DT), Rewriter(Rewriter), DeadInsts(Dead),
|
|
|
|
Changed(false) {
|
2011-08-10 12:22:26 +08:00
|
|
|
assert(LI && "IV simplification requires LoopInfo");
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasChanged() const { return Changed; }
|
|
|
|
|
|
|
|
/// Iteratively perform simplification on a worklist of users of the
|
|
|
|
/// specified induction variable. This is the top-level driver that applies
|
2015-08-09 02:27:36 +08:00
|
|
|
/// all simplifications to users of an IV.
|
2014-04-25 13:29:35 +08:00
|
|
|
void simplifyUsers(PHINode *CurrIV, IVVisitor *V = nullptr);
|
2011-08-10 11:46:27 +08:00
|
|
|
|
2011-08-10 12:01:31 +08:00
|
|
|
Value *foldIVUser(Instruction *UseInst, Instruction *IVOperand);
|
2011-08-10 11:46:27 +08:00
|
|
|
|
2015-10-07 05:44:39 +08:00
|
|
|
bool eliminateIdentitySCEV(Instruction *UseInst, Instruction *IVOperand);
|
2017-10-12 10:54:11 +08:00
|
|
|
bool replaceIVUserWithLoopInvariant(Instruction *UseInst);
|
2015-10-07 05:44:39 +08:00
|
|
|
|
2016-05-29 08:36:25 +08:00
|
|
|
bool eliminateOverflowIntrinsic(CallInst *CI);
|
2011-08-10 11:46:27 +08:00
|
|
|
bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand);
|
|
|
|
void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand);
|
2017-09-26 01:39:40 +08:00
|
|
|
void simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
|
|
|
|
bool IsSigned);
|
|
|
|
void replaceRemWithNumerator(BinaryOperator *Rem);
|
|
|
|
void replaceRemWithNumeratorOrZero(BinaryOperator *Rem);
|
|
|
|
void replaceSRemWithURem(BinaryOperator *Rem);
|
2017-03-31 05:56:56 +08:00
|
|
|
bool eliminateSDiv(BinaryOperator *SDiv);
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand);
|
2017-07-05 21:25:58 +08:00
|
|
|
bool strengthenRightShift(BinaryOperator *BO, Value *IVOperand);
|
2011-08-10 11:46:27 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2011-08-10 11:46:27 +08:00
|
|
|
|
2014-11-13 02:07:42 +08:00
|
|
|
/// Fold an IV operand into its use. This removes increments of an
|
2011-08-10 11:46:27 +08:00
|
|
|
/// aligned IV when used by a instruction that ignores the low bits.
|
2011-08-10 12:01:31 +08:00
|
|
|
///
|
2011-09-20 01:54:39 +08:00
|
|
|
/// IVOperand is guaranteed SCEVable, but UseInst may not be.
|
|
|
|
///
|
2011-08-10 12:01:31 +08:00
|
|
|
/// Return the operand of IVOperand for this induction variable if IVOperand can
|
2011-08-11 02:07:05 +08:00
|
|
|
/// be folded (in case more folding opportunities have been exposed).
|
2011-08-10 12:01:31 +08:00
|
|
|
/// Otherwise return null.
|
|
|
|
Value *SimplifyIndvar::foldIVUser(Instruction *UseInst, Instruction *IVOperand) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Value *IVSrc = nullptr;
|
2011-08-10 11:46:27 +08:00
|
|
|
unsigned OperIdx = 0;
|
2014-04-25 13:29:35 +08:00
|
|
|
const SCEV *FoldedExpr = nullptr;
|
2011-08-10 11:46:27 +08:00
|
|
|
switch (UseInst->getOpcode()) {
|
|
|
|
default:
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-08-10 11:46:27 +08:00
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::LShr:
|
|
|
|
// We're only interested in the case where we know something about
|
|
|
|
// the numerator and have a constant denominator.
|
|
|
|
if (IVOperand != UseInst->getOperand(OperIdx) ||
|
|
|
|
!isa<ConstantInt>(UseInst->getOperand(1)))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
// Attempt to fold a binary operator with constant operand.
|
|
|
|
// e.g. ((I + 1) >> 2) => I >> 2
|
2011-11-18 07:36:35 +08:00
|
|
|
if (!isa<BinaryOperator>(IVOperand)
|
|
|
|
|| !isa<ConstantInt>(IVOperand->getOperand(1)))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
IVSrc = IVOperand->getOperand(0);
|
|
|
|
// IVSrc must be the (SCEVable) IV, since the other operand is const.
|
|
|
|
assert(SE->isSCEVable(IVSrc->getType()) && "Expect SCEVable IV operand");
|
|
|
|
|
|
|
|
ConstantInt *D = cast<ConstantInt>(UseInst->getOperand(1));
|
|
|
|
if (UseInst->getOpcode() == Instruction::LShr) {
|
|
|
|
// Get a constant for the divisor. See createSCEV.
|
|
|
|
uint32_t BitWidth = cast<IntegerType>(UseInst->getType())->getBitWidth();
|
|
|
|
if (D->getValue().uge(BitWidth))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
D = ConstantInt::get(UseInst->getContext(),
|
2013-07-12 00:05:50 +08:00
|
|
|
APInt::getOneBitSet(BitWidth, D->getZExtValue()));
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
FoldedExpr = SE->getUDivExpr(SE->getSCEV(IVSrc), SE->getSCEV(D));
|
|
|
|
}
|
|
|
|
// We have something that might fold it's operand. Compare SCEVs.
|
|
|
|
if (!SE->isSCEVable(UseInst->getType()))
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
// Bypass the operand if SCEV can prove it has no effect.
|
|
|
|
if (SE->getSCEV(UseInst) != FoldedExpr)
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "INDVARS: Eliminated IV operand: " << *IVOperand
|
|
|
|
<< " -> " << *UseInst << '\n');
|
|
|
|
|
|
|
|
UseInst->setOperand(OperIdx, IVSrc);
|
|
|
|
assert(SE->getSCEV(UseInst) == FoldedExpr && "bad SCEV with folded oper");
|
|
|
|
|
|
|
|
++NumElimOperand;
|
|
|
|
Changed = true;
|
|
|
|
if (IVOperand->use_empty())
|
2015-05-30 03:43:39 +08:00
|
|
|
DeadInsts.emplace_back(IVOperand);
|
2011-08-10 12:01:31 +08:00
|
|
|
return IVSrc;
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
|
2014-11-13 02:07:42 +08:00
|
|
|
/// SimplifyIVUsers helper for eliminating useless
|
2011-08-10 11:46:27 +08:00
|
|
|
/// comparisons against an induction variable.
|
|
|
|
void SimplifyIndvar::eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) {
|
|
|
|
unsigned IVOperIdx = 0;
|
|
|
|
ICmpInst::Predicate Pred = ICmp->getPredicate();
|
2017-07-09 01:17:30 +08:00
|
|
|
ICmpInst::Predicate OriginalPred = Pred;
|
2011-08-10 11:46:27 +08:00
|
|
|
if (IVOperand != ICmp->getOperand(0)) {
|
|
|
|
// Swapped
|
|
|
|
assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand");
|
|
|
|
IVOperIdx = 1;
|
|
|
|
Pred = ICmpInst::getSwappedPredicate(Pred);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the SCEVs for the ICmp operands.
|
|
|
|
const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx));
|
|
|
|
const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx));
|
|
|
|
|
|
|
|
// Simplify unnecessary loops away.
|
|
|
|
const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent());
|
|
|
|
S = SE->getSCEVAtScope(S, ICmpLoop);
|
|
|
|
X = SE->getSCEVAtScope(X, ICmpLoop);
|
|
|
|
|
2015-07-28 05:42:49 +08:00
|
|
|
ICmpInst::Predicate InvariantPredicate;
|
|
|
|
const SCEV *InvariantLHS, *InvariantRHS;
|
|
|
|
|
2011-08-10 11:46:27 +08:00
|
|
|
// If the condition is always true or always false, replace it with
|
|
|
|
// a constant value.
|
2015-07-28 05:42:49 +08:00
|
|
|
if (SE->isKnownPredicate(Pred, S, X)) {
|
2011-08-10 11:46:27 +08:00
|
|
|
ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext()));
|
2015-07-28 05:42:49 +08:00
|
|
|
DeadInsts.emplace_back(ICmp);
|
2015-08-07 04:43:28 +08:00
|
|
|
DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
|
2015-07-28 05:42:49 +08:00
|
|
|
} else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) {
|
2011-08-10 11:46:27 +08:00
|
|
|
ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext()));
|
2015-07-28 05:42:49 +08:00
|
|
|
DeadInsts.emplace_back(ICmp);
|
2015-08-07 04:43:28 +08:00
|
|
|
DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n');
|
2015-07-28 05:42:49 +08:00
|
|
|
} else if (isa<PHINode>(IVOperand) &&
|
2016-03-19 04:37:07 +08:00
|
|
|
SE->isLoopInvariantPredicate(Pred, S, X, L, InvariantPredicate,
|
|
|
|
InvariantLHS, InvariantRHS)) {
|
2015-07-28 05:42:49 +08:00
|
|
|
|
2015-08-09 02:27:36 +08:00
|
|
|
// Rewrite the comparison to a loop invariant comparison if it can be done
|
2015-07-28 05:42:49 +08:00
|
|
|
// cheaply, where cheaply means "we don't need to emit any new
|
|
|
|
// instructions".
|
|
|
|
|
|
|
|
Value *NewLHS = nullptr, *NewRHS = nullptr;
|
|
|
|
|
|
|
|
if (S == InvariantLHS || X == InvariantLHS)
|
|
|
|
NewLHS =
|
|
|
|
ICmp->getOperand(S == InvariantLHS ? IVOperIdx : (1 - IVOperIdx));
|
|
|
|
|
|
|
|
if (S == InvariantRHS || X == InvariantRHS)
|
|
|
|
NewRHS =
|
|
|
|
ICmp->getOperand(S == InvariantRHS ? IVOperIdx : (1 - IVOperIdx));
|
|
|
|
|
2016-03-19 04:37:11 +08:00
|
|
|
auto *PN = cast<PHINode>(IVOperand);
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues();
|
|
|
|
i != e && (!NewLHS || !NewRHS);
|
|
|
|
++i) {
|
|
|
|
|
|
|
|
// If this is a value incoming from the backedge, then it cannot be a loop
|
|
|
|
// invariant value (since we know that IVOperand is an induction variable).
|
|
|
|
if (L->contains(PN->getIncomingBlock(i)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// NB! This following assert does not fundamentally have to be true, but
|
|
|
|
// it is true today given how SCEV analyzes induction variables.
|
|
|
|
// Specifically, today SCEV will *not* recognize %iv as an induction
|
|
|
|
// variable in the following case:
|
|
|
|
//
|
|
|
|
// define void @f(i32 %k) {
|
|
|
|
// entry:
|
|
|
|
// br i1 undef, label %r, label %l
|
|
|
|
//
|
|
|
|
// l:
|
|
|
|
// %k.inc.l = add i32 %k, 1
|
|
|
|
// br label %loop
|
|
|
|
//
|
|
|
|
// r:
|
|
|
|
// %k.inc.r = add i32 %k, 1
|
|
|
|
// br label %loop
|
|
|
|
//
|
|
|
|
// loop:
|
|
|
|
// %iv = phi i32 [ %k.inc.l, %l ], [ %k.inc.r, %r ], [ %iv.inc, %loop ]
|
|
|
|
// %iv.inc = add i32 %iv, 1
|
|
|
|
// br label %loop
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// but if it starts to, at some point, then the assertion below will have
|
|
|
|
// to be changed to a runtime check.
|
|
|
|
|
|
|
|
Value *Incoming = PN->getIncomingValue(i);
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (auto *I = dyn_cast<Instruction>(Incoming))
|
|
|
|
assert(DT->dominates(I, ICmp) && "Should be a unique loop dominating value!");
|
|
|
|
#endif
|
2015-07-28 05:42:49 +08:00
|
|
|
|
|
|
|
const SCEV *IncomingS = SE->getSCEV(Incoming);
|
|
|
|
|
|
|
|
if (!NewLHS && IncomingS == InvariantLHS)
|
|
|
|
NewLHS = Incoming;
|
|
|
|
if (!NewRHS && IncomingS == InvariantRHS)
|
|
|
|
NewRHS = Incoming;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NewLHS || !NewRHS)
|
|
|
|
// We could not find an existing value to replace either LHS or RHS.
|
|
|
|
// Generating new instructions has subtler tradeoffs, so avoid doing that
|
|
|
|
// for now.
|
|
|
|
return;
|
|
|
|
|
2015-08-07 04:43:28 +08:00
|
|
|
DEBUG(dbgs() << "INDVARS: Simplified comparison: " << *ICmp << '\n');
|
2015-07-28 05:42:49 +08:00
|
|
|
ICmp->setPredicate(InvariantPredicate);
|
|
|
|
ICmp->setOperand(0, NewLHS);
|
|
|
|
ICmp->setOperand(1, NewRHS);
|
2017-07-09 01:17:30 +08:00
|
|
|
} else if (ICmpInst::isSigned(OriginalPred) &&
|
|
|
|
SE->isKnownNonNegative(S) && SE->isKnownNonNegative(X)) {
|
|
|
|
// If we were unable to make anything above, all we can is to canonicalize
|
|
|
|
// the comparison hoping that it will open the doors for other
|
|
|
|
// optimizations. If we find out that we compare two non-negative values,
|
|
|
|
// we turn the instruction's predicate to its unsigned version. Note that
|
|
|
|
// we cannot rely on Pred here unless we check if we have swapped it.
|
|
|
|
assert(ICmp->getPredicate() == OriginalPred && "Predicate changed?");
|
|
|
|
DEBUG(dbgs() << "INDVARS: Turn to unsigned comparison: " << *ICmp << '\n');
|
|
|
|
ICmp->setPredicate(ICmpInst::getUnsignedPredicate(OriginalPred));
|
2015-07-28 05:42:49 +08:00
|
|
|
} else
|
2011-08-10 11:46:27 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
++NumElimCmp;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2017-03-31 05:56:56 +08:00
|
|
|
bool SimplifyIndvar::eliminateSDiv(BinaryOperator *SDiv) {
|
|
|
|
// Get the SCEVs for the ICmp operands.
|
|
|
|
auto *N = SE->getSCEV(SDiv->getOperand(0));
|
|
|
|
auto *D = SE->getSCEV(SDiv->getOperand(1));
|
|
|
|
|
|
|
|
// Simplify unnecessary loops away.
|
|
|
|
const Loop *L = LI->getLoopFor(SDiv->getParent());
|
|
|
|
N = SE->getSCEVAtScope(N, L);
|
|
|
|
D = SE->getSCEVAtScope(D, L);
|
|
|
|
|
|
|
|
// Replace sdiv by udiv if both of the operands are non-negative
|
|
|
|
if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) {
|
|
|
|
auto *UDiv = BinaryOperator::Create(
|
|
|
|
BinaryOperator::UDiv, SDiv->getOperand(0), SDiv->getOperand(1),
|
|
|
|
SDiv->getName() + ".udiv", SDiv);
|
|
|
|
UDiv->setIsExact(SDiv->isExact());
|
|
|
|
SDiv->replaceAllUsesWith(UDiv);
|
|
|
|
DEBUG(dbgs() << "INDVARS: Simplified sdiv: " << *SDiv << '\n');
|
|
|
|
++NumSimplifiedSDiv;
|
|
|
|
Changed = true;
|
|
|
|
DeadInsts.push_back(SDiv);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:39:40 +08:00
|
|
|
// i %s n -> i %u n if i >= 0 and n >= 0
|
|
|
|
void SimplifyIndvar::replaceSRemWithURem(BinaryOperator *Rem) {
|
|
|
|
auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
|
|
|
|
auto *URem = BinaryOperator::Create(BinaryOperator::URem, N, D,
|
|
|
|
Rem->getName() + ".urem", Rem);
|
|
|
|
Rem->replaceAllUsesWith(URem);
|
|
|
|
DEBUG(dbgs() << "INDVARS: Simplified srem: " << *Rem << '\n');
|
|
|
|
++NumSimplifiedSRem;
|
2017-09-26 02:10:36 +08:00
|
|
|
Changed = true;
|
2017-09-26 01:39:40 +08:00
|
|
|
DeadInsts.emplace_back(Rem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// i % n --> i if i is in [0,n).
|
|
|
|
void SimplifyIndvar::replaceRemWithNumerator(BinaryOperator *Rem) {
|
|
|
|
Rem->replaceAllUsesWith(Rem->getOperand(0));
|
|
|
|
DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
|
|
|
|
++NumElimRem;
|
|
|
|
Changed = true;
|
|
|
|
DeadInsts.emplace_back(Rem);
|
|
|
|
}
|
|
|
|
|
|
|
|
// (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n).
|
|
|
|
void SimplifyIndvar::replaceRemWithNumeratorOrZero(BinaryOperator *Rem) {
|
|
|
|
auto *T = Rem->getType();
|
|
|
|
auto *N = Rem->getOperand(0), *D = Rem->getOperand(1);
|
|
|
|
ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, N, D);
|
|
|
|
SelectInst *Sel =
|
|
|
|
SelectInst::Create(ICmp, ConstantInt::get(T, 0), N, "iv.rem", Rem);
|
|
|
|
Rem->replaceAllUsesWith(Sel);
|
|
|
|
DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n');
|
|
|
|
++NumElimRem;
|
|
|
|
Changed = true;
|
|
|
|
DeadInsts.emplace_back(Rem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SimplifyIVUsers helper for eliminating useless remainder operations
|
|
|
|
/// operating on an induction variable or replacing srem by urem.
|
|
|
|
void SimplifyIndvar::simplifyIVRemainder(BinaryOperator *Rem, Value *IVOperand,
|
|
|
|
bool IsSigned) {
|
|
|
|
auto *NValue = Rem->getOperand(0);
|
|
|
|
auto *DValue = Rem->getOperand(1);
|
2011-08-10 11:46:27 +08:00
|
|
|
// We're only interested in the case where we know something about
|
2017-09-26 01:39:40 +08:00
|
|
|
// the numerator, unless it is a srem, because we want to replace srem by urem
|
|
|
|
// in general.
|
|
|
|
bool UsedAsNumerator = IVOperand == NValue;
|
|
|
|
if (!UsedAsNumerator && !IsSigned)
|
2011-08-10 11:46:27 +08:00
|
|
|
return;
|
|
|
|
|
2017-09-26 01:39:40 +08:00
|
|
|
const SCEV *N = SE->getSCEV(NValue);
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
// Simplify unnecessary loops away.
|
|
|
|
const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent());
|
2017-09-26 01:39:40 +08:00
|
|
|
N = SE->getSCEVAtScope(N, ICmpLoop);
|
2011-08-10 11:46:27 +08:00
|
|
|
|
2017-09-26 01:39:40 +08:00
|
|
|
bool IsNumeratorNonNegative = !IsSigned || SE->isKnownNonNegative(N);
|
|
|
|
|
|
|
|
// Do not proceed if the Numerator may be negative
|
|
|
|
if (!IsNumeratorNonNegative)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const SCEV *D = SE->getSCEV(DValue);
|
|
|
|
D = SE->getSCEVAtScope(D, ICmpLoop);
|
2011-08-10 11:46:27 +08:00
|
|
|
|
2017-09-26 01:39:40 +08:00
|
|
|
if (UsedAsNumerator) {
|
|
|
|
auto LT = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
|
|
|
|
if (SE->isKnownPredicate(LT, N, D)) {
|
|
|
|
replaceRemWithNumerator(Rem);
|
2011-08-10 11:46:27 +08:00
|
|
|
return;
|
2017-09-26 01:39:40 +08:00
|
|
|
}
|
2011-08-10 11:46:27 +08:00
|
|
|
|
2017-09-26 01:39:40 +08:00
|
|
|
auto *T = Rem->getType();
|
|
|
|
const auto *NLessOne = SE->getMinusSCEV(N, SE->getOne(T));
|
|
|
|
if (SE->isKnownPredicate(LT, NLessOne, D)) {
|
|
|
|
replaceRemWithNumeratorOrZero(Rem);
|
|
|
|
return;
|
|
|
|
}
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
|
2017-09-26 01:39:40 +08:00
|
|
|
// Try to replace SRem with URem, if both N and D are known non-negative.
|
|
|
|
// Since we had already check N, we only need to check D now
|
|
|
|
if (!IsSigned || !SE->isKnownNonNegative(D))
|
|
|
|
return;
|
|
|
|
|
|
|
|
replaceSRemWithURem(Rem);
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
|
2016-05-29 08:36:25 +08:00
|
|
|
bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) {
|
|
|
|
auto *F = CI->getCalledFunction();
|
|
|
|
if (!F)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
typedef const SCEV *(ScalarEvolution::*OperationFunctionTy)(
|
2017-06-15 19:48:21 +08:00
|
|
|
const SCEV *, const SCEV *, SCEV::NoWrapFlags, unsigned);
|
2016-05-29 08:36:25 +08:00
|
|
|
typedef const SCEV *(ScalarEvolution::*ExtensionFunctionTy)(
|
2017-06-30 13:04:09 +08:00
|
|
|
const SCEV *, Type *, unsigned);
|
2016-05-29 08:36:25 +08:00
|
|
|
|
|
|
|
OperationFunctionTy Operation;
|
|
|
|
ExtensionFunctionTy Extension;
|
|
|
|
|
|
|
|
Instruction::BinaryOps RawOp;
|
|
|
|
|
|
|
|
// We always have exactly one of nsw or nuw. If NoSignedOverflow is false, we
|
|
|
|
// have nuw.
|
|
|
|
bool NoSignedOverflow;
|
|
|
|
|
|
|
|
switch (F->getIntrinsicID()) {
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case Intrinsic::sadd_with_overflow:
|
|
|
|
Operation = &ScalarEvolution::getAddExpr;
|
|
|
|
Extension = &ScalarEvolution::getSignExtendExpr;
|
|
|
|
RawOp = Instruction::Add;
|
|
|
|
NoSignedOverflow = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Intrinsic::uadd_with_overflow:
|
|
|
|
Operation = &ScalarEvolution::getAddExpr;
|
|
|
|
Extension = &ScalarEvolution::getZeroExtendExpr;
|
|
|
|
RawOp = Instruction::Add;
|
|
|
|
NoSignedOverflow = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Intrinsic::ssub_with_overflow:
|
|
|
|
Operation = &ScalarEvolution::getMinusSCEV;
|
|
|
|
Extension = &ScalarEvolution::getSignExtendExpr;
|
|
|
|
RawOp = Instruction::Sub;
|
|
|
|
NoSignedOverflow = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Intrinsic::usub_with_overflow:
|
|
|
|
Operation = &ScalarEvolution::getMinusSCEV;
|
|
|
|
Extension = &ScalarEvolution::getZeroExtendExpr;
|
|
|
|
RawOp = Instruction::Sub;
|
|
|
|
NoSignedOverflow = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEV *LHS = SE->getSCEV(CI->getArgOperand(0));
|
|
|
|
const SCEV *RHS = SE->getSCEV(CI->getArgOperand(1));
|
|
|
|
|
|
|
|
auto *NarrowTy = cast<IntegerType>(LHS->getType());
|
|
|
|
auto *WideTy =
|
|
|
|
IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
|
|
|
|
|
|
|
|
const SCEV *A =
|
2017-06-30 13:04:09 +08:00
|
|
|
(SE->*Extension)((SE->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0),
|
|
|
|
WideTy, 0);
|
2016-05-29 08:36:25 +08:00
|
|
|
const SCEV *B =
|
2017-06-30 13:04:09 +08:00
|
|
|
(SE->*Operation)((SE->*Extension)(LHS, WideTy, 0),
|
|
|
|
(SE->*Extension)(RHS, WideTy, 0), SCEV::FlagAnyWrap, 0);
|
2016-05-29 08:36:25 +08:00
|
|
|
|
|
|
|
if (A != B)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Proved no overflow, nuke the overflow check and, if possible, the overflow
|
|
|
|
// intrinsic as well.
|
|
|
|
|
|
|
|
BinaryOperator *NewResult = BinaryOperator::Create(
|
|
|
|
RawOp, CI->getArgOperand(0), CI->getArgOperand(1), "", CI);
|
|
|
|
|
|
|
|
if (NoSignedOverflow)
|
|
|
|
NewResult->setHasNoSignedWrap(true);
|
|
|
|
else
|
|
|
|
NewResult->setHasNoUnsignedWrap(true);
|
|
|
|
|
|
|
|
SmallVector<ExtractValueInst *, 4> ToDelete;
|
|
|
|
|
|
|
|
for (auto *U : CI->users()) {
|
|
|
|
if (auto *EVI = dyn_cast<ExtractValueInst>(U)) {
|
|
|
|
if (EVI->getIndices()[0] == 1)
|
|
|
|
EVI->replaceAllUsesWith(ConstantInt::getFalse(CI->getContext()));
|
|
|
|
else {
|
|
|
|
assert(EVI->getIndices()[0] == 0 && "Only two possibilities!");
|
|
|
|
EVI->replaceAllUsesWith(NewResult);
|
|
|
|
}
|
|
|
|
ToDelete.push_back(EVI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *EVI : ToDelete)
|
|
|
|
EVI->eraseFromParent();
|
|
|
|
|
|
|
|
if (CI->use_empty())
|
|
|
|
CI->eraseFromParent();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-07 05:44:39 +08:00
|
|
|
/// Eliminate an operation that consumes a simple IV and has no observable
|
|
|
|
/// side-effect given the range of IV values. IVOperand is guaranteed SCEVable,
|
|
|
|
/// but UseInst may not be.
|
2011-08-10 11:46:27 +08:00
|
|
|
bool SimplifyIndvar::eliminateIVUser(Instruction *UseInst,
|
|
|
|
Instruction *IVOperand) {
|
|
|
|
if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
|
|
|
|
eliminateIVComparison(ICmp, IVOperand);
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-31 05:56:56 +08:00
|
|
|
if (BinaryOperator *Bin = dyn_cast<BinaryOperator>(UseInst)) {
|
|
|
|
bool IsSRem = Bin->getOpcode() == Instruction::SRem;
|
|
|
|
if (IsSRem || Bin->getOpcode() == Instruction::URem) {
|
2017-09-26 01:39:40 +08:00
|
|
|
simplifyIVRemainder(Bin, IVOperand, IsSRem);
|
2011-08-10 11:46:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-31 05:56:56 +08:00
|
|
|
|
|
|
|
if (Bin->getOpcode() == Instruction::SDiv)
|
|
|
|
return eliminateSDiv(Bin);
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
|
2016-05-29 08:36:25 +08:00
|
|
|
if (auto *CI = dyn_cast<CallInst>(UseInst))
|
|
|
|
if (eliminateOverflowIntrinsic(CI))
|
|
|
|
return true;
|
|
|
|
|
2015-10-07 05:44:39 +08:00
|
|
|
if (eliminateIdentitySCEV(UseInst, IVOperand))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-12 10:54:11 +08:00
|
|
|
static Instruction *GetLoopInvariantInsertPosition(Loop *L, Instruction *Hint) {
|
|
|
|
if (auto *BB = L->getLoopPreheader())
|
|
|
|
return BB->getTerminator();
|
|
|
|
|
|
|
|
return Hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replace the UseInst with a constant if possible.
|
|
|
|
bool SimplifyIndvar::replaceIVUserWithLoopInvariant(Instruction *I) {
|
2017-09-27 11:11:46 +08:00
|
|
|
if (!SE->isSCEVable(I->getType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the symbolic expression for this instruction.
|
|
|
|
const SCEV *S = SE->getSCEV(I);
|
|
|
|
|
2017-10-12 10:54:11 +08:00
|
|
|
if (!SE->isLoopInvariant(S, L))
|
2017-09-30 00:32:12 +08:00
|
|
|
return false;
|
2017-09-27 11:11:46 +08:00
|
|
|
|
2017-10-12 10:54:11 +08:00
|
|
|
// Do not generate something ridiculous even if S is loop invariant.
|
|
|
|
if (Rewriter.isHighCostExpansion(S, L, I))
|
2017-09-30 00:32:12 +08:00
|
|
|
return false;
|
|
|
|
|
2017-10-12 10:54:11 +08:00
|
|
|
auto *IP = GetLoopInvariantInsertPosition(L, I);
|
|
|
|
auto *Invariant = Rewriter.expandCodeFor(S, I->getType(), IP);
|
|
|
|
|
|
|
|
I->replaceAllUsesWith(Invariant);
|
|
|
|
DEBUG(dbgs() << "INDVARS: Replace IV user: " << *I
|
|
|
|
<< " with loop invariant: " << *S << '\n');
|
2017-09-30 00:32:12 +08:00
|
|
|
++NumFoldedUser;
|
|
|
|
Changed = true;
|
|
|
|
DeadInsts.emplace_back(I);
|
|
|
|
return true;
|
2017-09-27 11:11:46 +08:00
|
|
|
}
|
|
|
|
|
2015-10-07 05:44:39 +08:00
|
|
|
/// Eliminate any operation that SCEV can prove is an identity function.
|
|
|
|
bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
|
|
|
|
Instruction *IVOperand) {
|
2011-08-10 11:46:27 +08:00
|
|
|
if (!SE->isSCEVable(UseInst->getType()) ||
|
|
|
|
(UseInst->getType() != IVOperand->getType()) ||
|
|
|
|
(SE->getSCEV(UseInst) != SE->getSCEV(IVOperand)))
|
|
|
|
return false;
|
|
|
|
|
2015-10-07 05:44:49 +08:00
|
|
|
// getSCEV(X) == getSCEV(Y) does not guarantee that X and Y are related in the
|
|
|
|
// dominator tree, even if X is an operand to Y. For instance, in
|
|
|
|
//
|
|
|
|
// %iv = phi i32 {0,+,1}
|
|
|
|
// br %cond, label %left, label %merge
|
|
|
|
//
|
|
|
|
// left:
|
|
|
|
// %X = add i32 %iv, 0
|
|
|
|
// br label %merge
|
|
|
|
//
|
|
|
|
// merge:
|
|
|
|
// %M = phi (%X, %iv)
|
|
|
|
//
|
|
|
|
// getSCEV(%M) == getSCEV(%X) == {0,+,1}, but %X does not dominate %M, and
|
|
|
|
// %M.replaceAllUsesWith(%X) would be incorrect.
|
|
|
|
|
|
|
|
if (isa<PHINode>(UseInst))
|
|
|
|
// If UseInst is not a PHI node then we know that IVOperand dominates
|
|
|
|
// UseInst directly from the legality of SSA.
|
|
|
|
if (!DT || !DT->dominates(IVOperand, UseInst))
|
|
|
|
return false;
|
|
|
|
|
2015-10-08 01:38:31 +08:00
|
|
|
if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
|
|
|
|
return false;
|
|
|
|
|
2011-08-10 11:46:27 +08:00
|
|
|
DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
|
|
|
|
|
|
|
|
UseInst->replaceAllUsesWith(IVOperand);
|
|
|
|
++NumElimIdentity;
|
|
|
|
Changed = true;
|
2015-05-30 03:43:39 +08:00
|
|
|
DeadInsts.emplace_back(UseInst);
|
2011-08-10 11:46:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
/// Annotate BO with nsw / nuw if it provably does not signed-overflow /
|
|
|
|
/// unsigned-overflow. Returns true if anything changed, false otherwise.
|
|
|
|
bool SimplifyIndvar::strengthenOverflowingOperation(BinaryOperator *BO,
|
|
|
|
Value *IVOperand) {
|
|
|
|
|
2015-03-05 06:24:23 +08:00
|
|
|
// Fastpath: we don't have any work to do if `BO` is `nuw` and `nsw`.
|
|
|
|
if (BO->hasNoUnsignedWrap() && BO->hasNoSignedWrap())
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
return false;
|
|
|
|
|
2015-03-05 06:24:23 +08:00
|
|
|
const SCEV *(ScalarEvolution::*GetExprForBO)(const SCEV *, const SCEV *,
|
2017-06-15 19:48:21 +08:00
|
|
|
SCEV::NoWrapFlags, unsigned);
|
2015-03-05 06:24:23 +08:00
|
|
|
switch (BO->getOpcode()) {
|
|
|
|
default:
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
return false;
|
|
|
|
|
2015-03-05 06:24:23 +08:00
|
|
|
case Instruction::Add:
|
|
|
|
GetExprForBO = &ScalarEvolution::getAddExpr;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Sub:
|
|
|
|
GetExprForBO = &ScalarEvolution::getMinusSCEV;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Instruction::Mul:
|
|
|
|
GetExprForBO = &ScalarEvolution::getMulExpr;
|
|
|
|
break;
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-05 06:24:23 +08:00
|
|
|
unsigned BitWidth = cast<IntegerType>(BO->getType())->getBitWidth();
|
|
|
|
Type *WideTy = IntegerType::get(BO->getContext(), BitWidth * 2);
|
|
|
|
const SCEV *LHS = SE->getSCEV(BO->getOperand(0));
|
|
|
|
const SCEV *RHS = SE->getSCEV(BO->getOperand(1));
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
|
2015-03-05 06:24:23 +08:00
|
|
|
bool Changed = false;
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
|
2015-03-05 06:24:23 +08:00
|
|
|
if (!BO->hasNoUnsignedWrap()) {
|
|
|
|
const SCEV *ExtendAfterOp = SE->getZeroExtendExpr(SE->getSCEV(BO), WideTy);
|
|
|
|
const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
|
|
|
|
SE->getZeroExtendExpr(LHS, WideTy), SE->getZeroExtendExpr(RHS, WideTy),
|
2017-06-15 19:48:21 +08:00
|
|
|
SCEV::FlagAnyWrap, 0u);
|
2015-03-05 06:24:23 +08:00
|
|
|
if (ExtendAfterOp == OpAfterExtend) {
|
|
|
|
BO->setHasNoUnsignedWrap();
|
|
|
|
SE->forgetValue(BO);
|
|
|
|
Changed = true;
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-05 06:24:23 +08:00
|
|
|
if (!BO->hasNoSignedWrap()) {
|
|
|
|
const SCEV *ExtendAfterOp = SE->getSignExtendExpr(SE->getSCEV(BO), WideTy);
|
|
|
|
const SCEV *OpAfterExtend = (SE->*GetExprForBO)(
|
|
|
|
SE->getSignExtendExpr(LHS, WideTy), SE->getSignExtendExpr(RHS, WideTy),
|
2017-06-15 19:48:21 +08:00
|
|
|
SCEV::FlagAnyWrap, 0u);
|
2015-03-05 06:24:23 +08:00
|
|
|
if (ExtendAfterOp == OpAfterExtend) {
|
|
|
|
BO->setHasNoSignedWrap();
|
|
|
|
SE->forgetValue(BO);
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-07-05 21:25:58 +08:00
|
|
|
/// Annotate the Shr in (X << IVOperand) >> C as exact using the
|
|
|
|
/// information from the IV's range. Returns true if anything changed, false
|
|
|
|
/// otherwise.
|
|
|
|
bool SimplifyIndvar::strengthenRightShift(BinaryOperator *BO,
|
|
|
|
Value *IVOperand) {
|
|
|
|
using namespace llvm::PatternMatch;
|
|
|
|
|
|
|
|
if (BO->getOpcode() == Instruction::Shl) {
|
|
|
|
bool Changed = false;
|
|
|
|
ConstantRange IVRange = SE->getUnsignedRange(SE->getSCEV(IVOperand));
|
|
|
|
for (auto *U : BO->users()) {
|
|
|
|
const APInt *C;
|
|
|
|
if (match(U,
|
|
|
|
m_AShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C))) ||
|
|
|
|
match(U,
|
|
|
|
m_LShr(m_Shl(m_Value(), m_Specific(IVOperand)), m_APInt(C)))) {
|
|
|
|
BinaryOperator *Shr = cast<BinaryOperator>(U);
|
|
|
|
if (!Shr->isExact() && IVRange.getUnsignedMin().uge(*C)) {
|
|
|
|
Shr->setIsExact(true);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-13 02:07:42 +08:00
|
|
|
/// Add all uses of Def to the current IV's worklist.
|
2011-08-10 11:46:27 +08:00
|
|
|
static void pushIVUsers(
|
2017-10-12 10:54:11 +08:00
|
|
|
Instruction *Def, Loop *L,
|
2011-08-10 11:46:27 +08:00
|
|
|
SmallPtrSet<Instruction*,16> &Simplified,
|
|
|
|
SmallVectorImpl< std::pair<Instruction*,Instruction*> > &SimpleIVUsers) {
|
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
for (User *U : Def->users()) {
|
|
|
|
Instruction *UI = cast<Instruction>(U);
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
// Avoid infinite or exponential worklist processing.
|
|
|
|
// Also ensure unique worklist users.
|
|
|
|
// If Def is a LoopPhi, it may not be in the Simplified set, so check for
|
|
|
|
// self edges first.
|
2017-10-12 10:54:11 +08:00
|
|
|
if (UI == Def)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Only change the current Loop, do not change the other parts (e.g. other
|
|
|
|
// Loops).
|
|
|
|
if (!L->contains(UI))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Do not push the same instruction more than once.
|
|
|
|
if (!Simplified.insert(UI).second)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SimpleIVUsers.push_back(std::make_pair(UI, Def));
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 02:07:42 +08:00
|
|
|
/// Return true if this instruction generates a simple SCEV
|
2011-08-10 11:46:27 +08:00
|
|
|
/// expression in terms of that IV.
|
|
|
|
///
|
2011-08-11 02:07:05 +08:00
|
|
|
/// This is similar to IVUsers' isInteresting() but processes each instruction
|
2011-08-10 11:46:27 +08:00
|
|
|
/// non-recursively when the operand is already known to be a simpleIVUser.
|
|
|
|
///
|
|
|
|
static bool isSimpleIVUser(Instruction *I, const Loop *L, ScalarEvolution *SE) {
|
|
|
|
if (!SE->isSCEVable(I->getType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Get the symbolic expression for this instruction.
|
|
|
|
const SCEV *S = SE->getSCEV(I);
|
|
|
|
|
|
|
|
// Only consider affine recurrences.
|
|
|
|
const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S);
|
|
|
|
if (AR && AR->getLoop() == L)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-13 02:07:42 +08:00
|
|
|
/// Iteratively perform simplification on a worklist of users
|
2011-08-10 11:46:27 +08:00
|
|
|
/// of the specified induction variable. Each successive simplification may push
|
|
|
|
/// more users which may themselves be candidates for simplification.
|
|
|
|
///
|
|
|
|
/// This algorithm does not require IVUsers analysis. Instead, it simplifies
|
|
|
|
/// instructions in-place during analysis. Rather than rewriting induction
|
|
|
|
/// variables bottom-up from their users, it transforms a chain of IVUsers
|
2015-08-09 02:27:36 +08:00
|
|
|
/// top-down, updating the IR only when it encounters a clear optimization
|
|
|
|
/// opportunity.
|
2011-08-10 11:46:27 +08:00
|
|
|
///
|
|
|
|
/// Once DisableIVRewrite is default, LSR will be the only client of IVUsers.
|
|
|
|
///
|
|
|
|
void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
|
2011-09-20 01:54:39 +08:00
|
|
|
if (!SE->isSCEVable(CurrIV->getType()))
|
|
|
|
return;
|
|
|
|
|
2011-08-10 11:46:27 +08:00
|
|
|
// Instructions processed by SimplifyIndvar for CurrIV.
|
|
|
|
SmallPtrSet<Instruction*,16> Simplified;
|
|
|
|
|
|
|
|
// Use-def pairs if IV users waiting to be processed for CurrIV.
|
|
|
|
SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers;
|
|
|
|
|
|
|
|
// Push users of the current LoopPhi. In rare cases, pushIVUsers may be
|
|
|
|
// called multiple times for the same LoopPhi. This is the proper thing to
|
|
|
|
// do for loop header phis that use each other.
|
2017-10-12 10:54:11 +08:00
|
|
|
pushIVUsers(CurrIV, L, Simplified, SimpleIVUsers);
|
2011-08-10 11:46:27 +08:00
|
|
|
|
|
|
|
while (!SimpleIVUsers.empty()) {
|
|
|
|
std::pair<Instruction*, Instruction*> UseOper =
|
|
|
|
SimpleIVUsers.pop_back_val();
|
2013-12-24 07:31:49 +08:00
|
|
|
Instruction *UseInst = UseOper.first;
|
|
|
|
|
2011-08-10 11:46:27 +08:00
|
|
|
// Bypass back edges to avoid extra work.
|
2013-12-24 07:31:49 +08:00
|
|
|
if (UseInst == CurrIV) continue;
|
|
|
|
|
2017-10-12 10:54:11 +08:00
|
|
|
// Try to replace UseInst with a loop invariant before any other
|
|
|
|
// simplifications.
|
|
|
|
if (replaceIVUserWithLoopInvariant(UseInst))
|
2017-09-27 11:11:46 +08:00
|
|
|
continue;
|
|
|
|
|
2011-08-10 12:01:31 +08:00
|
|
|
Instruction *IVOperand = UseOper.second;
|
|
|
|
for (unsigned N = 0; IVOperand; ++N) {
|
|
|
|
assert(N <= Simplified.size() && "runaway iteration");
|
|
|
|
|
|
|
|
Value *NewOper = foldIVUser(UseOper.first, IVOperand);
|
|
|
|
if (!NewOper)
|
|
|
|
break; // done folding
|
|
|
|
IVOperand = dyn_cast<Instruction>(NewOper);
|
|
|
|
}
|
|
|
|
if (!IVOperand)
|
|
|
|
continue;
|
2011-08-10 11:46:27 +08:00
|
|
|
|
2011-08-10 12:01:31 +08:00
|
|
|
if (eliminateIVUser(UseOper.first, IVOperand)) {
|
2017-10-12 10:54:11 +08:00
|
|
|
pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
|
2011-08-10 11:46:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
|
|
|
|
if (BinaryOperator *BO = dyn_cast<BinaryOperator>(UseOper.first)) {
|
2017-07-05 21:25:58 +08:00
|
|
|
if ((isa<OverflowingBinaryOperator>(BO) &&
|
|
|
|
strengthenOverflowingOperation(BO, IVOperand)) ||
|
|
|
|
(isa<ShlOperator>(BO) && strengthenRightShift(BO, IVOperand))) {
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
// re-queue uses of the now modified binary operator and fall
|
|
|
|
// through to the checks that remain.
|
2017-10-12 10:54:11 +08:00
|
|
|
pushIVUsers(IVOperand, L, Simplified, SimpleIVUsers);
|
This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.
define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
entry:
%length = load i32* %length_ptr, !range !0
%len.sub.1 = sub i32 %length, 1
%upper = icmp slt i32 %init, %len.sub.1
br i1 %upper, label %loop, label %exit
loop:
%civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
%civ.inc = add i32 %civ, 1
%cmp = icmp slt i32 %civ.inc, %length
br i1 %cmp, label %latch, label %break
latch:
store i32 0, i32* %array
%check = icmp slt i32 %civ.inc, %len.sub.1
br i1 %check, label %loop, label %break
break:
ret i32 %civ.inc
exit:
ret i32 42
}
Differential Revision: http://reviews.llvm.org/D6748
llvm-svn: 225282
2015-01-07 03:02:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-10 11:46:27 +08:00
|
|
|
CastInst *Cast = dyn_cast<CastInst>(UseOper.first);
|
|
|
|
if (V && Cast) {
|
|
|
|
V->visitCast(Cast);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (isSimpleIVUser(UseOper.first, L, SE)) {
|
2017-10-12 10:54:11 +08:00
|
|
|
pushIVUsers(UseOper.first, L, Simplified, SimpleIVUsers);
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2011-12-20 10:50:00 +08:00
|
|
|
void IVVisitor::anchor() { }
|
|
|
|
|
2014-11-13 02:07:42 +08:00
|
|
|
/// Simplify instructions that use this induction variable
|
2011-08-10 11:46:27 +08:00
|
|
|
/// by using ScalarEvolution to analyze the IV's recurrence.
|
2015-10-07 05:44:49 +08:00
|
|
|
bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT,
|
2017-05-02 01:07:49 +08:00
|
|
|
LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead,
|
2017-10-12 10:54:11 +08:00
|
|
|
SCEVExpander &Rewriter, IVVisitor *V) {
|
|
|
|
SimplifyIndvar SIV(LI->getLoopFor(CurrIV->getParent()), SE, DT, LI, Rewriter,
|
|
|
|
Dead);
|
2011-08-10 11:46:27 +08:00
|
|
|
SIV.simplifyUsers(CurrIV, V);
|
|
|
|
return SIV.hasChanged();
|
|
|
|
}
|
|
|
|
|
2014-11-13 02:07:42 +08:00
|
|
|
/// Simplify users of induction variables within this
|
2011-08-10 11:46:27 +08:00
|
|
|
/// loop. This does not actually change or add IVs.
|
2015-10-07 05:44:49 +08:00
|
|
|
bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
|
2017-05-02 01:07:49 +08:00
|
|
|
LoopInfo *LI, SmallVectorImpl<WeakTrackingVH> &Dead) {
|
2017-10-12 10:54:11 +08:00
|
|
|
SCEVExpander Rewriter(*SE, SE->getDataLayout(), "indvars");
|
|
|
|
#ifndef NDEBUG
|
|
|
|
Rewriter.setDebugType(DEBUG_TYPE);
|
|
|
|
#endif
|
2011-08-10 11:46:27 +08:00
|
|
|
bool Changed = false;
|
|
|
|
for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
|
2017-10-12 10:54:11 +08:00
|
|
|
Changed |= simplifyUsersOfIV(cast<PHINode>(I), SE, DT, LI, Dead, Rewriter);
|
2011-08-10 11:46:27 +08:00
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llvm
|