2005-07-30 02:12:19 +02:00
|
|
|
//===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:59:42 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-07-30 02:12:19 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the classes used to generate code from scalar expressions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 01:45:19 +01:00
|
|
|
#ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
|
|
|
|
#define LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2016-12-09 15:42:11 +01:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2016-08-09 22:40:03 +02:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2005-07-30 02:12:19 +02:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
|
2010-04-08 00:27:08 +02:00
|
|
|
#include "llvm/Analysis/ScalarEvolutionNormalization.h"
|
2014-03-04 12:59:06 +01:00
|
|
|
#include "llvm/Analysis/TargetFolder.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
2014-03-04 12:17:44 +01:00
|
|
|
#include "llvm/IR/ValueHandle.h"
|
2005-07-30 02:12:19 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
Switch the SCEV expander and LoopStrengthReduce to use
TargetTransformInfo rather than TargetLowering, removing one of the
primary instances of the layering violation of Transforms depending
directly on Target.
This is a really big deal because LSR used to be a "special" pass that
could only be tested fully using llc and by looking at the full output
of it. It also couldn't run with any other loop passes because it had to
be created by the backend. No longer is this true. LSR is now just
a normal pass and we should probably lift the creation of LSR out of
lib/CodeGen/Passes.cpp and into the PassManagerBuilder. =] I've not done
this, or updated all of the tests to use opt and a triple, because
I suspect someone more familiar with LSR would do a better job. This
change should be essentially without functional impact for normal
compilations, and only change behvaior of targetless compilations.
The conversion required changing all of the LSR code to refer to the TTI
interfaces, which fortunately are very similar to TargetLowering's
interfaces. However, it also allowed us to *always* expect to have some
implementation around. I've pushed that simplification through the pass,
and leveraged it to simplify code somewhat. It required some test
updates for one of two things: either we used to skip some checks
altogether but now we get the default "no" answer for them, or we used
to have no information about the target and now we do have some.
I've also started the process of removing AddrMode, as the TTI interface
doesn't use it any longer. In some cases this simplifies code, and in
others it adds some complexity, but I think it's not a bad tradeoff even
there. Subsequent patches will try to clean this up even further and use
other (more appropriate) abstractions.
Yet again, almost all of the formatting changes brought to you by
clang-format. =]
llvm-svn: 171735
2013-01-07 15:41:08 +01:00
|
|
|
class TargetTransformInfo;
|
2012-01-07 02:12:09 +01:00
|
|
|
|
2012-07-14 01:33:10 +02:00
|
|
|
/// Return true if the given expression is safe to expand in the sense that
|
|
|
|
/// all materialized values are safe to speculate.
|
2013-10-25 23:35:56 +02:00
|
|
|
bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE);
|
2012-07-14 01:33:10 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// This class uses information about analyze scalars to
|
2005-07-30 02:12:19 +02:00
|
|
|
/// rewrite expressions in canonical form.
|
|
|
|
///
|
|
|
|
/// Clients should create an instance of this class when rewriting is needed,
|
2009-08-20 18:42:55 +02:00
|
|
|
/// and destroy it when finished to allow the release of the associated
|
2005-07-30 02:12:19 +02:00
|
|
|
/// memory.
|
2010-01-21 03:09:26 +01:00
|
|
|
class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
|
2005-07-30 02:12:19 +02:00
|
|
|
ScalarEvolution &SE;
|
2015-03-10 03:37:25 +01:00
|
|
|
const DataLayout &DL;
|
2011-06-28 07:07:32 +02:00
|
|
|
|
|
|
|
// New instructions receive a name to identifies them with the current pass.
|
2011-06-28 07:44:06 +02:00
|
|
|
const char* IVName;
|
2011-06-28 07:07:32 +02:00
|
|
|
|
2013-01-14 22:00:37 +01:00
|
|
|
// InsertedExpressions caches Values for reuse, so must track RAUW.
|
2016-12-09 15:42:11 +01:00
|
|
|
DenseMap<std::pair<const SCEV *, Instruction *>, TrackingVH<Value>>
|
|
|
|
InsertedExpressions;
|
|
|
|
|
2013-01-14 22:00:37 +01:00
|
|
|
// InsertedValues only flags inserted instructions so needs no RAUW.
|
2016-12-09 15:42:11 +01:00
|
|
|
DenseSet<AssertingVH<Value>> InsertedValues;
|
|
|
|
DenseSet<AssertingVH<Value>> InsertedPostIncValues;
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// A memoization of the "relevant" loop for a given SCEV.
|
2010-11-18 01:34:22 +01:00
|
|
|
DenseMap<const SCEV *, const Loop *> RelevantLoops;
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Addrecs referring to any of the given loops are expanded
|
2010-04-08 00:27:08 +02:00
|
|
|
/// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode
|
|
|
|
/// returns the add instruction that adds one to the phi for {0,+,1}<L>,
|
|
|
|
/// as opposed to a new phi starting at 1. This is only supported in
|
|
|
|
/// non-canonical mode.
|
|
|
|
PostIncLoopSet PostIncLoops;
|
2010-01-21 03:09:26 +01:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief When this is non-null, addrecs expanded in the loop it indicates
|
|
|
|
/// should be inserted with increments at IVIncInsertPos.
|
2010-01-21 03:09:26 +01:00
|
|
|
const Loop *IVIncInsertLoop;
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief When expanding addrecs in the IVIncInsertLoop loop, insert the IV
|
|
|
|
/// increment at this position.
|
2010-01-21 03:09:26 +01:00
|
|
|
Instruction *IVIncInsertPos;
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Phis that complete an IV chain. Reuse
|
2016-12-09 15:42:11 +01:00
|
|
|
DenseSet<AssertingVH<PHINode>> ChainedPhis;
|
2012-01-10 02:45:08 +01:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief When true, expressions are expanded in "canonical" form. In
|
|
|
|
/// particular, addrecs are expanded as arithmetic based on a canonical
|
|
|
|
/// induction variable. When false, expression are expanded in a more
|
|
|
|
/// literal form.
|
2010-01-21 03:09:26 +01:00
|
|
|
bool CanonicalMode;
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief When invoked from LSR, the expander is in "strength reduction"
|
|
|
|
/// mode. The only difference is that phi's are only reused if they are
|
|
|
|
/// already in "expanded" form.
|
2011-10-08 01:46:21 +02:00
|
|
|
bool LSRMode;
|
|
|
|
|
2016-03-13 22:05:13 +01:00
|
|
|
typedef IRBuilder<TargetFolder> BuilderType;
|
2009-06-27 23:18:18 +02:00
|
|
|
BuilderType Builder;
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2016-06-01 22:03:09 +02:00
|
|
|
// RAII object that stores the current insertion point and restores it when
|
|
|
|
// the object is destroyed. This includes the debug location. Duplicated
|
|
|
|
// from InsertPointGuard to add SetInsertPoint() which is used to updated
|
|
|
|
// InsertPointGuards stack when insert points are moved during SCEV
|
|
|
|
// expansion.
|
|
|
|
class SCEVInsertPointGuard {
|
|
|
|
IRBuilderBase &Builder;
|
|
|
|
AssertingVH<BasicBlock> Block;
|
|
|
|
BasicBlock::iterator Point;
|
|
|
|
DebugLoc DbgLoc;
|
|
|
|
SCEVExpander *SE;
|
|
|
|
|
|
|
|
SCEVInsertPointGuard(const SCEVInsertPointGuard &) = delete;
|
|
|
|
SCEVInsertPointGuard &operator=(const SCEVInsertPointGuard &) = delete;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SCEVInsertPointGuard(IRBuilderBase &B, SCEVExpander *SE)
|
|
|
|
: Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
|
|
|
|
DbgLoc(B.getCurrentDebugLocation()), SE(SE) {
|
|
|
|
SE->InsertPointGuards.push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SCEVInsertPointGuard() {
|
|
|
|
// These guards should always created/destroyed in FIFO order since they
|
|
|
|
// are used to guard lexically scoped blocks of code in
|
|
|
|
// ScalarEvolutionExpander.
|
|
|
|
assert(SE->InsertPointGuards.back() == this);
|
|
|
|
SE->InsertPointGuards.pop_back();
|
|
|
|
Builder.restoreIP(IRBuilderBase::InsertPoint(Block, Point));
|
|
|
|
Builder.SetCurrentDebugLocation(DbgLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
BasicBlock::iterator GetInsertPoint() const { return Point; }
|
|
|
|
void SetInsertPoint(BasicBlock::iterator I) { Point = I; }
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Stack of pointers to saved insert points, used to keep insert points
|
|
|
|
/// consistent when instructions are moved.
|
|
|
|
SmallVector<SCEVInsertPointGuard *, 8> InsertPointGuards;
|
|
|
|
|
2011-10-11 04:28:51 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
const char *DebugType;
|
|
|
|
#endif
|
|
|
|
|
2005-07-30 02:12:19 +02:00
|
|
|
friend struct SCEVVisitor<SCEVExpander, Value*>;
|
2010-02-14 03:47:26 +01:00
|
|
|
|
2005-07-30 02:12:19 +02:00
|
|
|
public:
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Construct a SCEVExpander in "canonical" mode.
|
2015-03-10 03:37:25 +01:00
|
|
|
explicit SCEVExpander(ScalarEvolution &se, const DataLayout &DL,
|
|
|
|
const char *name)
|
|
|
|
: SE(se), DL(DL), IVName(name), IVIncInsertLoop(nullptr),
|
|
|
|
IVIncInsertPos(nullptr), CanonicalMode(true), LSRMode(false),
|
|
|
|
Builder(se.getContext(), TargetFolder(DL)) {
|
2011-10-11 04:28:51 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
DebugType = "";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:03:09 +02:00
|
|
|
~SCEVExpander() {
|
|
|
|
// Make sure the insert point guard stack is consistent.
|
|
|
|
assert(InsertPointGuards.empty());
|
|
|
|
}
|
|
|
|
|
2011-10-11 04:28:51 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
void setDebugType(const char* s) { DebugType = s; }
|
|
|
|
#endif
|
2006-02-04 06:49:01 +01:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Erase the contents of the InsertedExpressions map so that users
|
2005-07-30 20:33:25 +02:00
|
|
|
/// trying to expand the same expression into multiple BasicBlocks or
|
2005-07-30 02:12:19 +02:00
|
|
|
/// different places within the same BasicBlock can do so.
|
2010-07-27 03:19:06 +02:00
|
|
|
void clear() {
|
|
|
|
InsertedExpressions.clear();
|
|
|
|
InsertedValues.clear();
|
|
|
|
InsertedPostIncValues.clear();
|
2012-01-10 02:45:08 +01:00
|
|
|
ChainedPhis.clear();
|
2010-07-27 03:19:06 +02:00
|
|
|
}
|
2005-07-30 20:33:25 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Return true for expressions that may incur non-trivial cost to
|
|
|
|
/// evaluate at runtime.
|
2015-09-16 01:45:31 +02:00
|
|
|
///
|
|
|
|
/// At is an optional parameter which specifies point in code where user is
|
|
|
|
/// going to expand this expression. Sometimes this knowledge can lead to a
|
|
|
|
/// more accurate cost estimation.
|
2015-08-10 20:23:58 +02:00
|
|
|
bool isHighCostExpansion(const SCEV *Expr, Loop *L,
|
|
|
|
const Instruction *At = nullptr) {
|
2015-04-14 05:20:28 +02:00
|
|
|
SmallPtrSet<const SCEV *, 8> Processed;
|
2015-08-10 20:23:58 +02:00
|
|
|
return isHighCostExpansionHelper(Expr, L, At, Processed);
|
2015-04-14 05:20:28 +02:00
|
|
|
}
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief This method returns the canonical induction variable of the
|
|
|
|
/// specified type for the specified loop (inserting one if there is none).
|
|
|
|
/// A canonical induction variable starts at zero and steps by one on each
|
|
|
|
/// iteration.
|
2011-10-08 01:46:21 +02:00
|
|
|
PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty);
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Return the induction variable increment's IV operand.
|
2012-01-20 08:41:13 +01:00
|
|
|
Instruction *getIVIncOperand(Instruction *IncV, Instruction *InsertPos,
|
|
|
|
bool allowScale);
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Utility for hoisting an IV increment.
|
2012-01-20 08:41:13 +01:00
|
|
|
bool hoistIVInc(Instruction *IncV, Instruction *InsertPos);
|
2011-10-11 04:28:51 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief replace congruent phis with their most canonical
|
2011-10-11 04:28:51 +02:00
|
|
|
/// representative. Return the number of phis eliminated.
|
|
|
|
unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
|
2012-01-07 02:12:09 +01:00
|
|
|
SmallVectorImpl<WeakVH> &DeadInsts,
|
2014-04-15 06:59:12 +02:00
|
|
|
const TargetTransformInfo *TTI = nullptr);
|
2011-10-11 04:28:51 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Insert code to directly compute the specified SCEV expression
|
|
|
|
/// into the program. The inserted code is inserted into the specified
|
|
|
|
/// block.
|
2011-07-18 06:54:35 +02:00
|
|
|
Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I);
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2016-08-11 23:05:17 +02:00
|
|
|
/// \brief Insert code to directly compute the specified SCEV expression
|
|
|
|
/// into the program. The inserted code is inserted into the SCEVExpander's
|
|
|
|
/// current insertion point. If a type is specified, the result will be
|
|
|
|
/// expanded to have that type, with a cast if necessary.
|
|
|
|
Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr);
|
|
|
|
|
|
|
|
|
[SCEV][LV] Add SCEV Predicates and use them to re-implement stride versioning
Summary:
SCEV Predicates represent conditions that typically cannot be derived from
static analysis, but can be used to reduce SCEV expressions to forms which are
usable for different optimizers.
ScalarEvolution now has the rewriteUsingPredicate method which can simplify a
SCEV expression using a SCEVPredicateSet. The normal workflow of a pass using
SCEVPredicates would be to hold a SCEVPredicateSet and every time assumptions
need to be made a new SCEV Predicate would be created and added to the set.
Each time after calling getSCEV, the user will call the rewriteUsingPredicate
method.
We add two types of predicates
SCEVPredicateSet - implements a set of predicates
SCEVEqualPredicate - tests for equality between two SCEV expressions
We use the SCEVEqualPredicate to re-implement stride versioning. Every time we
version a stride, we will add a SCEVEqualPredicate to the context.
Instead of adding specific stride checks, LoopVectorize now adds a more
generic SCEV check.
We only need to add support for this in the LoopVectorizer since this is the
only pass that will do stride versioning.
Reviewers: mzolotukhin, anemet, hfinkel, sanjoy
Subscribers: sanjoy, hfinkel, rengolin, jmolloy, llvm-commits
Differential Revision: http://reviews.llvm.org/D13595
llvm-svn: 251800
2015-11-02 15:41:02 +01:00
|
|
|
/// \brief Generates a code sequence that evaluates this predicate.
|
|
|
|
/// The inserted instructions will be at position \p Loc.
|
|
|
|
/// The result will be of type i1 and will have a value of 0 when the
|
|
|
|
/// predicate is false and 1 otherwise.
|
|
|
|
Value *expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc);
|
|
|
|
|
|
|
|
/// \brief A specialized variant of expandCodeForPredicate, handling the
|
|
|
|
/// case when we are expanding code for a SCEVEqualPredicate.
|
|
|
|
Value *expandEqualPredicate(const SCEVEqualPredicate *Pred,
|
|
|
|
Instruction *Loc);
|
|
|
|
|
[SCEV][LAA] Re-commit r260085 and r260086, this time with a fix for the memory
sanitizer issue. The PredicatedScalarEvolution's copy constructor
wasn't copying the Generation value, and was leaving it un-initialized.
Original commit message:
[SCEV][LAA] Add no wrap SCEV predicates and use use them to improve strided pointer detection
Summary:
This change adds no wrap SCEV predicates with:
- support for runtime checking
- support for expression rewriting:
(sext ({x,+,y}) -> {sext(x),+,sext(y)}
(zext ({x,+,y}) -> {zext(x),+,sext(y)}
Note that we are sign extending the increment of the SCEV, even for
the zext case. This is needed to cover the fairly common case where y would
be a (small) negative integer. In order to do this, this change adds two new
flags: nusw and nssw that are applicable to AddRecExprs and permit the
transformations above.
We also change isStridedPtr in LAA to be able to make use of
these predicates. With this feature we should now always be able to
work around overflow issues in the dependence analysis.
Reviewers: mzolotukhin, sanjoy, anemet
Subscribers: mzolotukhin, sanjoy, llvm-commits, rengolin, jmolloy, hfinkel
Differential Revision: http://reviews.llvm.org/D15412
llvm-svn: 260112
2016-02-08 18:02:45 +01:00
|
|
|
/// \brief Generates code that evaluates if the \p AR expression will
|
|
|
|
/// overflow.
|
|
|
|
Value *generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc,
|
|
|
|
bool Signed);
|
|
|
|
|
|
|
|
/// \brief A specialized variant of expandCodeForPredicate, handling the
|
|
|
|
/// case when we are expanding code for a SCEVWrapPredicate.
|
|
|
|
Value *expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc);
|
|
|
|
|
[SCEV][LV] Add SCEV Predicates and use them to re-implement stride versioning
Summary:
SCEV Predicates represent conditions that typically cannot be derived from
static analysis, but can be used to reduce SCEV expressions to forms which are
usable for different optimizers.
ScalarEvolution now has the rewriteUsingPredicate method which can simplify a
SCEV expression using a SCEVPredicateSet. The normal workflow of a pass using
SCEVPredicates would be to hold a SCEVPredicateSet and every time assumptions
need to be made a new SCEV Predicate would be created and added to the set.
Each time after calling getSCEV, the user will call the rewriteUsingPredicate
method.
We add two types of predicates
SCEVPredicateSet - implements a set of predicates
SCEVEqualPredicate - tests for equality between two SCEV expressions
We use the SCEVEqualPredicate to re-implement stride versioning. Every time we
version a stride, we will add a SCEVEqualPredicate to the context.
Instead of adding specific stride checks, LoopVectorize now adds a more
generic SCEV check.
We only need to add support for this in the LoopVectorizer since this is the
only pass that will do stride versioning.
Reviewers: mzolotukhin, anemet, hfinkel, sanjoy
Subscribers: sanjoy, hfinkel, rengolin, jmolloy, llvm-commits
Differential Revision: http://reviews.llvm.org/D13595
llvm-svn: 251800
2015-11-02 15:41:02 +01:00
|
|
|
/// \brief A specialized variant of expandCodeForPredicate, handling the
|
|
|
|
/// case when we are expanding code for a SCEVUnionPredicate.
|
|
|
|
Value *expandUnionPredicate(const SCEVUnionPredicate *Pred,
|
|
|
|
Instruction *Loc);
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Set the current IV increment loop and position.
|
2010-01-21 03:09:26 +01:00
|
|
|
void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
|
|
|
|
assert(!CanonicalMode &&
|
|
|
|
"IV increment positions are not supported in CanonicalMode");
|
|
|
|
IVIncInsertLoop = L;
|
|
|
|
IVIncInsertPos = Pos;
|
|
|
|
}
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Enable post-inc expansion for addrecs referring to the given
|
|
|
|
/// loops. Post-inc expansion is only supported in non-canonical mode.
|
2010-04-08 00:27:08 +02:00
|
|
|
void setPostInc(const PostIncLoopSet &L) {
|
2010-01-21 03:09:26 +01:00
|
|
|
assert(!CanonicalMode &&
|
|
|
|
"Post-inc expansion is not supported in CanonicalMode");
|
2010-04-08 00:27:08 +02:00
|
|
|
PostIncLoops = L;
|
|
|
|
}
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Disable all post-inc expansion.
|
2010-04-08 00:27:08 +02:00
|
|
|
void clearPostInc() {
|
|
|
|
PostIncLoops.clear();
|
2010-06-05 02:33:07 +02:00
|
|
|
|
|
|
|
// When we change the post-inc loop set, cached expansions may no
|
|
|
|
// longer be valid.
|
|
|
|
InsertedPostIncValues.clear();
|
2010-01-21 03:09:26 +01:00
|
|
|
}
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Disable the behavior of expanding expressions in canonical form
|
|
|
|
/// rather than in a more literal form. Non-canonical mode is useful for
|
|
|
|
/// late optimization passes.
|
2010-01-21 03:09:26 +01:00
|
|
|
void disableCanonicalMode() { CanonicalMode = false; }
|
|
|
|
|
2011-10-08 01:46:21 +02:00
|
|
|
void enableLSRMode() { LSRMode = true; }
|
|
|
|
|
2016-08-11 23:05:17 +02:00
|
|
|
/// \brief Set the current insertion point. This is useful if multiple calls
|
|
|
|
/// to expandCodeFor() are going to be made with the same insert point and
|
|
|
|
/// the insert point may be moved during one of the expansions (e.g. if the
|
|
|
|
/// insert point is not a block terminator).
|
|
|
|
void setInsertPoint(Instruction *IP) {
|
|
|
|
assert(IP);
|
|
|
|
Builder.SetInsertPoint(IP);
|
|
|
|
}
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Clear the current insertion point. This is useful if the
|
|
|
|
/// instruction that had been serving as the insertion point may have been
|
|
|
|
/// deleted.
|
2010-03-20 04:53:53 +01:00
|
|
|
void clearInsertPoint() {
|
|
|
|
Builder.ClearInsertionPoint();
|
|
|
|
}
|
2012-01-10 02:45:08 +01:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Return true if the specified instruction was inserted by the code
|
|
|
|
/// rewriter. If so, the client should not modify the instruction.
|
2012-01-20 08:41:13 +01:00
|
|
|
bool isInsertedInstruction(Instruction *I) const {
|
|
|
|
return InsertedValues.count(I) || InsertedPostIncValues.count(I);
|
|
|
|
}
|
|
|
|
|
2012-01-10 02:45:08 +01:00
|
|
|
void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); }
|
|
|
|
|
2016-08-09 22:40:03 +02:00
|
|
|
/// Try to find existing LLVM IR value for S available at the point At.
|
|
|
|
Value *getExactExistingExpansion(const SCEV *S, const Instruction *At,
|
|
|
|
Loop *L);
|
|
|
|
|
|
|
|
/// Try to find the ValueOffsetPair for S. The function is mainly
|
|
|
|
/// used to check whether S can be expanded cheaply.
|
|
|
|
/// If this returns a non-None value, we know we can codegen the
|
|
|
|
/// `ValueOffsetPair` into a suitable expansion identical with S
|
|
|
|
/// so that S can be expanded cheaply.
|
2015-09-16 01:45:31 +02:00
|
|
|
///
|
|
|
|
/// L is a hint which tells in which loop to look for the suitable value.
|
|
|
|
/// On success return value which is equivalent to the expanded S at point
|
|
|
|
/// At. Return nullptr if value was not found.
|
|
|
|
///
|
|
|
|
/// Note that this function does not perform an exhaustive search. I.e if it
|
|
|
|
/// didn't find any value it does not mean that there is no such value.
|
2016-08-09 22:40:03 +02:00
|
|
|
///
|
|
|
|
Optional<ScalarEvolution::ValueOffsetPair>
|
|
|
|
getRelatedExistingExpansion(const SCEV *S, const Instruction *At, Loop *L);
|
2015-08-10 20:23:58 +02:00
|
|
|
|
2009-06-27 23:18:18 +02:00
|
|
|
private:
|
2009-07-22 02:24:57 +02:00
|
|
|
LLVMContext &getContext() const { return SE.getContext(); }
|
2009-08-20 18:42:55 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Recursive helper function for isHighCostExpansion.
|
2015-04-14 05:20:28 +02:00
|
|
|
bool isHighCostExpansionHelper(const SCEV *S, Loop *L,
|
2015-08-10 20:23:58 +02:00
|
|
|
const Instruction *At,
|
2015-04-14 05:20:28 +02:00
|
|
|
SmallPtrSetImpl<const SCEV *> &Processed);
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Insert the specified binary operator, doing a small amount
|
2009-06-27 23:18:18 +02:00
|
|
|
/// of work to avoid inserting an obviously redundant operation.
|
|
|
|
Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
|
2009-04-21 03:07:12 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Arrange for there to be a cast of V to Ty at IP, reusing an
|
|
|
|
/// existing cast if a suitable one exists, moving an existing cast if a
|
|
|
|
/// suitable one exists but isn't in the right place, or or creating a new
|
|
|
|
/// one.
|
2011-07-18 06:54:35 +02:00
|
|
|
Value *ReuseOrCreateCast(Value *V, Type *Ty,
|
2010-06-19 15:25:23 +02:00
|
|
|
Instruction::CastOps Op,
|
|
|
|
BasicBlock::iterator IP);
|
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Insert a cast of V to the specified type, which must be possible
|
|
|
|
/// with a noop cast, doing what we can to share the casts.
|
2011-07-18 06:54:35 +02:00
|
|
|
Value *InsertNoopCastOfTo(Value *V, Type *Ty);
|
2009-04-21 03:07:12 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Expand a SCEVAddExpr with a pointer type into a GEP
|
2009-05-19 04:15:55 +02:00
|
|
|
/// instead of using ptrtoint+arithmetic+inttoptr.
|
2009-07-07 19:06:11 +02:00
|
|
|
Value *expandAddToGEP(const SCEV *const *op_begin,
|
|
|
|
const SCEV *const *op_end,
|
2011-07-18 06:54:35 +02:00
|
|
|
PointerType *PTy, Type *Ty, Value *V);
|
2009-05-19 04:15:55 +02:00
|
|
|
|
2016-02-16 07:46:58 +01:00
|
|
|
/// \brief Find a previous Value in ExprValueMap for expand.
|
Recommit "Use ValueOffsetPair to enhance value reuse during SCEV expansion".
The fix for PR28705 will be committed consecutively.
In D12090, the ExprValueMap was added to reuse existing value during SCEV expansion.
However, const folding and sext/zext distribution can make the reuse still difficult.
A simplified case is: suppose we know S1 expands to V1 in ExprValueMap, and
S1 = S2 + C_a
S3 = S2 + C_b
where C_a and C_b are different SCEVConstants. Then we'd like to expand S3 as
V1 - C_a + C_b instead of expanding S2 literally. It is helpful when S2 is a
complex SCEV expr and S2 has no entry in ExprValueMap, which is usually caused
by the fact that S3 is generated from S1 after const folding.
In order to do that, we represent ExprValueMap as a mapping from SCEV to
ValueOffsetPair. We will save both S1->{V1, 0} and S2->{V1, C_a} into the
ExprValueMap when we create SCEV for V1. When S3 is expanded, it will first
expand S2 to V1 - C_a because of S2->{V1, C_a} in the map, then expand S3 to
V1 - C_a + C_b.
Differential Revision: https://reviews.llvm.org/D21313
llvm-svn: 278160
2016-08-09 22:37:50 +02:00
|
|
|
ScalarEvolution::ValueOffsetPair
|
|
|
|
FindValueInExprValueMap(const SCEV *S, const Instruction *InsertPt);
|
2016-02-16 07:46:58 +01:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *expand(const SCEV *S);
|
2009-04-16 05:18:22 +02:00
|
|
|
|
2015-04-14 05:20:40 +02:00
|
|
|
/// \brief Determine the most "relevant" loop for the given SCEV.
|
2010-11-18 01:34:22 +01:00
|
|
|
const Loop *getRelevantLoop(const SCEV *);
|
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitConstant(const SCEVConstant *S) {
|
2005-07-30 02:12:19 +02:00
|
|
|
return S->getValue();
|
|
|
|
}
|
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitTruncateExpr(const SCEVTruncateExpr *S);
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
|
2007-06-15 16:38:12 +02:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitAddExpr(const SCEVAddExpr *S);
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitMulExpr(const SCEVMulExpr *S);
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitUDivExpr(const SCEVUDivExpr *S);
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitAddRecExpr(const SCEVAddRecExpr *S);
|
2005-07-30 02:12:19 +02:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitSMaxExpr(const SCEVSMaxExpr *S);
|
2007-11-25 23:41:31 +01:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitUMaxExpr(const SCEVUMaxExpr *S);
|
2008-02-20 07:48:22 +01:00
|
|
|
|
2009-04-18 19:56:28 +02:00
|
|
|
Value *visitUnknown(const SCEVUnknown *S) {
|
2005-07-30 02:12:19 +02:00
|
|
|
return S->getValue();
|
|
|
|
}
|
2010-01-21 03:09:26 +01:00
|
|
|
|
2010-02-14 04:12:47 +01:00
|
|
|
void rememberInstruction(Value *I);
|
2010-01-21 03:09:26 +01:00
|
|
|
|
2011-10-08 01:46:21 +02:00
|
|
|
bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
|
|
|
|
|
2011-10-15 08:19:55 +02:00
|
|
|
bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
|
2011-10-08 01:46:21 +02:00
|
|
|
|
2010-01-21 03:09:26 +01:00
|
|
|
Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
|
|
|
|
PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
|
|
|
|
const Loop *L,
|
2011-07-18 06:54:35 +02:00
|
|
|
Type *ExpandTy,
|
2014-02-16 16:49:50 +01:00
|
|
|
Type *IntTy,
|
|
|
|
Type *&TruncTy,
|
|
|
|
bool &InvertStep);
|
2011-11-30 07:07:54 +01:00
|
|
|
Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
|
|
|
|
Type *ExpandTy, Type *IntTy, bool useSubtract);
|
2016-06-01 22:03:09 +02:00
|
|
|
|
|
|
|
void hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist,
|
|
|
|
Instruction *Pos, PHINode *LoopPhi);
|
|
|
|
|
|
|
|
void fixupInsertPoints(Instruction *I);
|
2005-07-30 02:12:19 +02:00
|
|
|
};
|
2015-06-23 11:49:53 +02:00
|
|
|
}
|
2005-07-30 02:12:19 +02:00
|
|
|
|
|
|
|
#endif
|