2004-04-02 20:23:17 +00:00
|
|
|
//===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-04-02 20:23:17 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-04-02 20:23:17 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The ScalarEvolution class is an LLVM pass which can be used to analyze and
|
|
|
|
// catagorize scalar expressions in loops. It specializes in recognizing
|
|
|
|
// general induction variables, representing them with the abstract and opaque
|
|
|
|
// SCEV class. Given this analysis, trip counts of loops and other important
|
|
|
|
// properties can be obtained.
|
|
|
|
//
|
|
|
|
// This analysis is primarily useful for induction variable substitution and
|
|
|
|
// strength reduction.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-04-02 20:23:17 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
|
|
|
|
#define LLVM_ANALYSIS_SCALAREVOLUTION_H
|
|
|
|
|
|
|
|
#include "llvm/Pass.h"
|
2007-11-14 02:33:58 +00:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2007-03-05 00:00:42 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2008-05-29 19:52:31 +00:00
|
|
|
#include <iosfwd>
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2007-10-22 18:31:58 +00:00
|
|
|
class APInt;
|
|
|
|
class ConstantInt;
|
2004-04-02 20:23:17 +00:00
|
|
|
class Instruction;
|
|
|
|
class Type;
|
|
|
|
class ConstantRange;
|
|
|
|
class SCEVHandle;
|
2007-10-22 18:31:58 +00:00
|
|
|
class ScalarEvolution;
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
/// SCEV - This class represent an analyzed expression in the program. These
|
|
|
|
/// are reference counted opaque objects that the client is not allowed to
|
|
|
|
/// do much with directly.
|
|
|
|
///
|
|
|
|
class SCEV {
|
|
|
|
const unsigned SCEVType; // The SCEV baseclass this node corresponds to
|
2005-02-13 04:34:51 +00:00
|
|
|
mutable unsigned RefCount;
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
friend class SCEVHandle;
|
2005-02-13 04:34:51 +00:00
|
|
|
void addRef() const { ++RefCount; }
|
|
|
|
void dropRef() const {
|
2004-04-02 20:46:26 +00:00
|
|
|
if (--RefCount == 0)
|
2004-04-02 20:23:17 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCEV(const SCEV &); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const SCEV &); // DO NOT IMPLEMENT
|
|
|
|
protected:
|
|
|
|
virtual ~SCEV();
|
|
|
|
public:
|
2007-05-03 18:45:06 +00:00
|
|
|
explicit SCEV(unsigned SCEVTy) : SCEVType(SCEVTy), RefCount(0) {}
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
unsigned getSCEVType() const { return SCEVType; }
|
|
|
|
|
|
|
|
/// isLoopInvariant - Return true if the value of this SCEV is unchanging in
|
|
|
|
/// the specified loop.
|
|
|
|
virtual bool isLoopInvariant(const Loop *L) const = 0;
|
|
|
|
|
|
|
|
/// hasComputableLoopEvolution - Return true if this SCEV changes value in a
|
|
|
|
/// known way in the specified loop. This property being true implies that
|
|
|
|
/// the value is variant in the loop AND that we can emit an expression to
|
|
|
|
/// compute the value of the expression at any particular loop iteration.
|
|
|
|
virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
|
|
|
|
|
|
|
|
/// getType - Return the LLVM type of this SCEV expression.
|
|
|
|
///
|
|
|
|
virtual const Type *getType() const = 0;
|
|
|
|
|
2007-02-28 19:57:34 +00:00
|
|
|
/// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
|
|
|
|
///
|
|
|
|
uint32_t getBitWidth() const;
|
|
|
|
|
2008-06-18 16:23:07 +00:00
|
|
|
/// isZero - Return true if the expression is a constant zero.
|
|
|
|
///
|
|
|
|
bool isZero() const;
|
|
|
|
|
2005-02-13 04:34:51 +00:00
|
|
|
/// replaceSymbolicValuesWithConcrete - If this SCEV internally references
|
|
|
|
/// the symbolic value "Sym", construct and return a new SCEV that produces
|
|
|
|
/// the same value, but which uses the concrete value Conc instead of the
|
|
|
|
/// symbolic value. If this SCEV does not use the symbolic value, it
|
|
|
|
/// returns itself.
|
2005-04-21 20:19:05 +00:00
|
|
|
virtual SCEVHandle
|
2005-02-13 04:34:51 +00:00
|
|
|
replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
|
2007-10-22 18:31:58 +00:00
|
|
|
const SCEVHandle &Conc,
|
|
|
|
ScalarEvolution &SE) const = 0;
|
2005-02-13 04:34:51 +00:00
|
|
|
|
2004-04-02 20:23:17 +00:00
|
|
|
/// print - Print out the internal representation of this scalar to the
|
|
|
|
/// specified stream. This should really only be used for debugging
|
|
|
|
/// purposes.
|
|
|
|
virtual void print(std::ostream &OS) const = 0;
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *OS) const { if (OS) print(*OS); }
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
/// dump - This method is used for debugging.
|
|
|
|
///
|
|
|
|
void dump() const;
|
|
|
|
};
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2004-04-02 20:23:17 +00:00
|
|
|
inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) {
|
|
|
|
S.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// SCEVCouldNotCompute - An object of this class is returned by queries that
|
|
|
|
/// could not be answered. For example, if you ask for the number of
|
|
|
|
/// iterations of a linked-list traversal loop, you will get one of these.
|
|
|
|
/// None of the standard SCEV operations are valid on this class, it is just a
|
|
|
|
/// marker.
|
|
|
|
struct SCEVCouldNotCompute : public SCEV {
|
|
|
|
SCEVCouldNotCompute();
|
|
|
|
|
|
|
|
// None of these methods are valid for this object.
|
|
|
|
virtual bool isLoopInvariant(const Loop *L) const;
|
|
|
|
virtual const Type *getType() const;
|
|
|
|
virtual bool hasComputableLoopEvolution(const Loop *L) const;
|
|
|
|
virtual void print(std::ostream &OS) const;
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *OS) const { if (OS) print(*OS); }
|
2005-04-21 20:19:05 +00:00
|
|
|
virtual SCEVHandle
|
2005-02-13 04:34:51 +00:00
|
|
|
replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
|
2007-10-22 18:31:58 +00:00
|
|
|
const SCEVHandle &Conc,
|
|
|
|
ScalarEvolution &SE) const;
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
|
|
|
|
static bool classof(const SCEV *S);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// SCEVHandle - This class is used to maintain the SCEV object's refcounts,
|
|
|
|
/// freeing the objects when the last reference is dropped.
|
|
|
|
class SCEVHandle {
|
|
|
|
SCEV *S;
|
|
|
|
SCEVHandle(); // DO NOT IMPLEMENT
|
|
|
|
public:
|
2005-02-13 04:34:51 +00:00
|
|
|
SCEVHandle(const SCEV *s) : S(const_cast<SCEV*>(s)) {
|
2004-04-02 20:23:17 +00:00
|
|
|
assert(S && "Cannot create a handle to a null SCEV!");
|
|
|
|
S->addRef();
|
|
|
|
}
|
|
|
|
SCEVHandle(const SCEVHandle &RHS) : S(RHS.S) {
|
2005-04-21 20:19:05 +00:00
|
|
|
S->addRef();
|
2004-04-02 20:23:17 +00:00
|
|
|
}
|
|
|
|
~SCEVHandle() { S->dropRef(); }
|
|
|
|
|
|
|
|
operator SCEV*() const { return S; }
|
|
|
|
|
|
|
|
SCEV &operator*() const { return *S; }
|
|
|
|
SCEV *operator->() const { return S; }
|
|
|
|
|
|
|
|
bool operator==(SCEV *RHS) const { return S == RHS; }
|
|
|
|
bool operator!=(SCEV *RHS) const { return S != RHS; }
|
|
|
|
|
|
|
|
const SCEVHandle &operator=(SCEV *RHS) {
|
|
|
|
if (S != RHS) {
|
|
|
|
S->dropRef();
|
|
|
|
S = RHS;
|
|
|
|
S->addRef();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCEVHandle &operator=(const SCEVHandle &RHS) {
|
|
|
|
if (S != RHS.S) {
|
|
|
|
S->dropRef();
|
|
|
|
S = RHS.S;
|
|
|
|
S->addRef();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename From> struct simplify_type;
|
|
|
|
template<> struct simplify_type<const SCEVHandle> {
|
|
|
|
typedef SCEV* SimpleType;
|
|
|
|
static SimpleType getSimplifiedValue(const SCEVHandle &Node) {
|
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template<> struct simplify_type<SCEVHandle>
|
|
|
|
: public simplify_type<const SCEVHandle> {};
|
|
|
|
|
|
|
|
/// ScalarEvolution - This class is the main scalar evolution driver. Because
|
|
|
|
/// client code (intentionally) can't do much with the SCEV objects directly,
|
|
|
|
/// they must ask this class for services.
|
|
|
|
///
|
|
|
|
class ScalarEvolution : public FunctionPass {
|
|
|
|
void *Impl; // ScalarEvolution uses the pimpl pattern
|
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2008-09-04 17:05:41 +00:00
|
|
|
ScalarEvolution() : FunctionPass(&ID), Impl(0) {}
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2004-04-02 20:23:17 +00:00
|
|
|
/// getSCEV - Return a SCEV expression handle for the full generality of the
|
|
|
|
/// specified expression.
|
|
|
|
SCEVHandle getSCEV(Value *V) const;
|
|
|
|
|
2007-10-22 18:31:58 +00:00
|
|
|
SCEVHandle getConstant(ConstantInt *V);
|
|
|
|
SCEVHandle getConstant(const APInt& Val);
|
|
|
|
SCEVHandle getTruncateExpr(const SCEVHandle &Op, const Type *Ty);
|
|
|
|
SCEVHandle getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty);
|
|
|
|
SCEVHandle getSignExtendExpr(const SCEVHandle &Op, const Type *Ty);
|
|
|
|
SCEVHandle getAddExpr(std::vector<SCEVHandle> &Ops);
|
|
|
|
SCEVHandle getAddExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
|
|
|
|
std::vector<SCEVHandle> Ops;
|
|
|
|
Ops.push_back(LHS);
|
|
|
|
Ops.push_back(RHS);
|
|
|
|
return getAddExpr(Ops);
|
|
|
|
}
|
|
|
|
SCEVHandle getAddExpr(const SCEVHandle &Op0, const SCEVHandle &Op1,
|
|
|
|
const SCEVHandle &Op2) {
|
|
|
|
std::vector<SCEVHandle> Ops;
|
|
|
|
Ops.push_back(Op0);
|
|
|
|
Ops.push_back(Op1);
|
|
|
|
Ops.push_back(Op2);
|
|
|
|
return getAddExpr(Ops);
|
|
|
|
}
|
|
|
|
SCEVHandle getMulExpr(std::vector<SCEVHandle> &Ops);
|
|
|
|
SCEVHandle getMulExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
|
|
|
|
std::vector<SCEVHandle> Ops;
|
|
|
|
Ops.push_back(LHS);
|
|
|
|
Ops.push_back(RHS);
|
|
|
|
return getMulExpr(Ops);
|
|
|
|
}
|
2008-02-11 11:03:14 +00:00
|
|
|
SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
|
2007-10-22 18:31:58 +00:00
|
|
|
SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
|
|
|
|
const Loop *L);
|
|
|
|
SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,
|
|
|
|
const Loop *L);
|
|
|
|
SCEVHandle getAddRecExpr(const std::vector<SCEVHandle> &Operands,
|
|
|
|
const Loop *L) {
|
|
|
|
std::vector<SCEVHandle> NewOp(Operands);
|
|
|
|
return getAddRecExpr(NewOp, L);
|
|
|
|
}
|
2007-11-25 22:41:31 +00:00
|
|
|
SCEVHandle getSMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
|
|
|
|
SCEVHandle getSMaxExpr(std::vector<SCEVHandle> Operands);
|
2008-02-20 06:48:22 +00:00
|
|
|
SCEVHandle getUMaxExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
|
|
|
|
SCEVHandle getUMaxExpr(std::vector<SCEVHandle> Operands);
|
2007-10-22 18:31:58 +00:00
|
|
|
SCEVHandle getUnknown(Value *V);
|
|
|
|
|
|
|
|
/// getNegativeSCEV - Return the SCEV object corresponding to -V.
|
|
|
|
///
|
|
|
|
SCEVHandle getNegativeSCEV(const SCEVHandle &V);
|
|
|
|
|
2008-02-20 06:48:22 +00:00
|
|
|
/// getNotSCEV - Return the SCEV object corresponding to ~V.
|
|
|
|
///
|
|
|
|
SCEVHandle getNotSCEV(const SCEVHandle &V);
|
|
|
|
|
2007-10-22 18:31:58 +00:00
|
|
|
/// getMinusSCEV - Return LHS-RHS.
|
|
|
|
///
|
|
|
|
SCEVHandle getMinusSCEV(const SCEVHandle &LHS,
|
|
|
|
const SCEVHandle &RHS);
|
|
|
|
|
2008-06-13 04:38:55 +00:00
|
|
|
/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
|
|
|
|
/// of the input value to the specified type. If the type must be
|
|
|
|
/// extended, it is zero extended.
|
|
|
|
SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
|
|
|
|
|
2007-10-22 18:31:58 +00:00
|
|
|
/// getIntegerSCEV - Given an integer or FP type, create a constant for the
|
|
|
|
/// specified signed integer value and return a SCEV for the constant.
|
|
|
|
SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
|
|
|
|
|
2005-08-09 23:36:18 +00:00
|
|
|
/// hasSCEV - Return true if the SCEV for this value has already been
|
|
|
|
/// computed.
|
|
|
|
bool hasSCEV(Value *V) const;
|
|
|
|
|
|
|
|
/// setSCEV - Insert the specified SCEV into the map of current SCEVs for
|
|
|
|
/// the specified value.
|
|
|
|
void setSCEV(Value *V, const SCEVHandle &H);
|
|
|
|
|
2004-04-02 20:23:17 +00:00
|
|
|
/// getSCEVAtScope - Return a SCEV expression handle for the specified value
|
|
|
|
/// at the specified scope in the program. The L value specifies a loop
|
|
|
|
/// nest to evaluate the expression at, where null is the top-level or a
|
|
|
|
/// specified loop is immediately inside of the loop.
|
|
|
|
///
|
|
|
|
/// This method can be used to compute the exit value for a variable defined
|
|
|
|
/// in a loop by querying what the value will hold in the parent loop.
|
|
|
|
///
|
|
|
|
/// If this value is not computable at this scope, a SCEVCouldNotCompute
|
|
|
|
/// object is returned.
|
|
|
|
SCEVHandle getSCEVAtScope(Value *V, const Loop *L) const;
|
|
|
|
|
|
|
|
/// getIterationCount - If the specified loop has a predictable iteration
|
|
|
|
/// count, return it, otherwise return a SCEVCouldNotCompute object.
|
|
|
|
SCEVHandle getIterationCount(const Loop *L) const;
|
|
|
|
|
|
|
|
/// hasLoopInvariantIterationCount - Return true if the specified loop has
|
|
|
|
/// an analyzable loop-invariant iteration count.
|
|
|
|
bool hasLoopInvariantIterationCount(const Loop *L) const;
|
|
|
|
|
2007-06-19 14:28:31 +00:00
|
|
|
/// deleteValueFromRecords - This method should be called by the
|
|
|
|
/// client before it removes a Value from the program, to make sure
|
2004-04-02 20:23:17 +00:00
|
|
|
/// that no dangling references are left around.
|
2007-06-19 14:28:31 +00:00
|
|
|
void deleteValueFromRecords(Value *V) const;
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
virtual void releaseMemory();
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
2004-12-07 04:03:45 +00:00
|
|
|
virtual void print(std::ostream &OS, const Module* = 0) const;
|
2006-12-17 05:15:13 +00:00
|
|
|
void print(std::ostream *OS, const Module* M = 0) const {
|
|
|
|
if (OS) print(*OS, M);
|
|
|
|
}
|
2004-04-02 20:23:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|