2004-04-15 15:06:59 +00:00
|
|
|
//===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===//
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-04-15 15:06:59 +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-15 15:06:59 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the classes used to represent and build scalar expressions.
|
2005-04-21 20:19:05 +00:00
|
|
|
//
|
2004-04-15 15:06:59 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-10 00:45:19 +00:00
|
|
|
#ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
|
|
|
|
#define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2012-07-18 05:14:03 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-12-03 17:02:12 +00:00
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2004-04-15 15:06:59 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
class ConstantInt;
|
|
|
|
class ConstantRange;
|
2009-02-17 00:13:06 +00:00
|
|
|
class DominatorTree;
|
2004-04-15 15:06:59 +00:00
|
|
|
|
|
|
|
enum SCEVTypes {
|
|
|
|
// These should be ordered in terms of increasing complexity to make the
|
|
|
|
// folders simpler.
|
2007-06-15 14:38:12 +00:00
|
|
|
scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
|
2009-08-18 16:46:41 +00:00
|
|
|
scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr,
|
2010-01-28 02:15:55 +00:00
|
|
|
scUnknown, scCouldNotCompute
|
2004-04-15 15:06:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVConstant - This class represents a constant integer value.
|
|
|
|
///
|
|
|
|
class SCEVConstant : public SCEV {
|
2007-10-22 18:31:58 +00:00
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
ConstantInt *V;
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVConstant(const FoldingSetNodeIDRef ID, ConstantInt *v) :
|
|
|
|
SCEV(ID, scConstant), V(v) {}
|
2004-04-15 15:06:59 +00:00
|
|
|
public:
|
|
|
|
ConstantInt *getValue() const { return V; }
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *getType() const { return V->getType(); }
|
2004-04-15 15:06:59 +00:00
|
|
|
|
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scConstant;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-04-21 01:25:57 +00:00
|
|
|
/// SCEVCastExpr - This is the base class for unary cast operator classes.
|
2004-04-15 15:06:59 +00:00
|
|
|
///
|
2009-04-21 01:25:57 +00:00
|
|
|
class SCEVCastExpr : public SCEV {
|
|
|
|
protected:
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *Op;
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *Ty;
|
2009-04-21 01:25:57 +00:00
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVCastExpr(const FoldingSetNodeIDRef ID,
|
2011-07-18 04:54:35 +00:00
|
|
|
unsigned SCEVTy, const SCEV *op, Type *ty);
|
2009-04-21 01:25:57 +00:00
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
public:
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getOperand() const { return Op; }
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *getType() const { return Ty; }
|
2009-07-25 01:13:03 +00:00
|
|
|
|
2009-04-21 01:25:57 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scTruncate ||
|
|
|
|
S->getSCEVType() == scZeroExtend ||
|
|
|
|
S->getSCEVType() == scSignExtend;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVTruncateExpr - This class represents a truncation of an integer value
|
|
|
|
/// to a smaller integer value.
|
|
|
|
///
|
|
|
|
class SCEVTruncateExpr : public SCEVCastExpr {
|
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
|
2011-07-18 04:54:35 +00:00
|
|
|
const SCEV *op, Type *ty);
|
2009-04-21 01:25:57 +00:00
|
|
|
|
|
|
|
public:
|
2004-04-15 15:06:59 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scTruncate;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVZeroExtendExpr - This class represents a zero extension of a small
|
|
|
|
/// integer value to a larger integer value.
|
|
|
|
///
|
2009-04-21 01:25:57 +00:00
|
|
|
class SCEVZeroExtendExpr : public SCEVCastExpr {
|
2007-10-22 18:31:58 +00:00
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
|
2011-07-18 04:54:35 +00:00
|
|
|
const SCEV *op, Type *ty);
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2009-04-21 01:25:57 +00:00
|
|
|
public:
|
2004-04-15 15:06:59 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scZeroExtend;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-06-15 14:38:12 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVSignExtendExpr - This class represents a sign extension of a small
|
|
|
|
/// integer value to a larger integer value.
|
|
|
|
///
|
2009-04-21 01:25:57 +00:00
|
|
|
class SCEVSignExtendExpr : public SCEVCastExpr {
|
2007-10-22 18:31:58 +00:00
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
|
2011-07-18 04:54:35 +00:00
|
|
|
const SCEV *op, Type *ty);
|
2007-06-15 14:38:12 +00:00
|
|
|
|
2009-04-21 01:25:57 +00:00
|
|
|
public:
|
2007-06-15 14:38:12 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scSignExtend;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2009-05-26 17:42:32 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVNAryExpr - This node is a base class providing common
|
|
|
|
/// functionality for n'ary operators.
|
|
|
|
///
|
2009-05-07 14:00:19 +00:00
|
|
|
class SCEVNAryExpr : public SCEV {
|
|
|
|
protected:
|
2010-03-18 01:17:13 +00:00
|
|
|
// Since SCEVs are immutable, ScalarEvolution allocates operand
|
|
|
|
// arrays with its SCEVAllocator, so this class just needs a simple
|
|
|
|
// pointer rather than a more elaborate vector-like data structure.
|
|
|
|
// This also avoids the need for a non-trivial destructor.
|
|
|
|
const SCEV *const *Operands;
|
|
|
|
size_t NumOperands;
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVNAryExpr(const FoldingSetNodeIDRef ID,
|
2010-03-18 01:17:13 +00:00
|
|
|
enum SCEVTypes T, const SCEV *const *O, size_t N)
|
2010-06-18 19:54:20 +00:00
|
|
|
: SCEV(ID, T), Operands(O), NumOperands(N) {}
|
2004-04-15 15:06:59 +00:00
|
|
|
|
|
|
|
public:
|
2010-03-18 01:17:13 +00:00
|
|
|
size_t getNumOperands() const { return NumOperands; }
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getOperand(unsigned i) const {
|
2010-03-18 01:17:13 +00:00
|
|
|
assert(i < NumOperands && "Operand index out of range!");
|
2004-04-15 15:06:59 +00:00
|
|
|
return Operands[i];
|
|
|
|
}
|
|
|
|
|
2010-03-18 01:17:13 +00:00
|
|
|
typedef const SCEV *const *op_iterator;
|
|
|
|
op_iterator op_begin() const { return Operands; }
|
|
|
|
op_iterator op_end() const { return Operands + NumOperands; }
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *getType() const { return getOperand(0)->getType(); }
|
2009-05-07 14:00:19 +00:00
|
|
|
|
2011-03-14 16:50:06 +00:00
|
|
|
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask = NoWrapMask) const {
|
|
|
|
return (NoWrapFlags)(SubclassData & Mask);
|
2009-10-09 00:10:36 +00:00
|
|
|
}
|
|
|
|
|
2009-05-07 14:00:19 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scAddExpr ||
|
|
|
|
S->getSCEVType() == scMulExpr ||
|
|
|
|
S->getSCEVType() == scSMaxExpr ||
|
|
|
|
S->getSCEVType() == scUMaxExpr ||
|
|
|
|
S->getSCEVType() == scAddRecExpr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVCommutativeExpr - This node is the base class for n'ary commutative
|
|
|
|
/// operators.
|
|
|
|
///
|
|
|
|
class SCEVCommutativeExpr : public SCEVNAryExpr {
|
|
|
|
protected:
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVCommutativeExpr(const FoldingSetNodeIDRef ID,
|
2010-03-18 16:16:38 +00:00
|
|
|
enum SCEVTypes T, const SCEV *const *O, size_t N)
|
2010-06-18 19:54:20 +00:00
|
|
|
: SCEVNAryExpr(ID, T, O, N) {}
|
2009-05-07 14:00:19 +00:00
|
|
|
|
|
|
|
public:
|
2004-04-15 15:06:59 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scAddExpr ||
|
2007-11-25 22:41:31 +00:00
|
|
|
S->getSCEVType() == scMulExpr ||
|
2008-02-20 06:48:22 +00:00
|
|
|
S->getSCEVType() == scSMaxExpr ||
|
|
|
|
S->getSCEVType() == scUMaxExpr;
|
2004-04-15 15:06:59 +00:00
|
|
|
}
|
2011-03-14 16:50:06 +00:00
|
|
|
|
|
|
|
/// Set flags for a non-recurrence without clearing previously set flags.
|
|
|
|
void setNoWrapFlags(NoWrapFlags Flags) {
|
|
|
|
SubclassData |= Flags;
|
|
|
|
}
|
2004-04-15 15:06:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVAddExpr - This node represents an addition of some number of SCEVs.
|
|
|
|
///
|
|
|
|
class SCEVAddExpr : public SCEVCommutativeExpr {
|
2007-10-22 18:31:58 +00:00
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVAddExpr(const FoldingSetNodeIDRef ID,
|
2010-03-18 01:17:13 +00:00
|
|
|
const SCEV *const *O, size_t N)
|
2010-06-18 19:54:20 +00:00
|
|
|
: SCEVCommutativeExpr(ID, scAddExpr, O, N) {
|
2004-04-15 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *getType() const {
|
2010-01-19 22:53:50 +00:00
|
|
|
// Use the type of the last operand, which is likely to be a pointer
|
|
|
|
// type, if there is one. This doesn't usually matter, but it can help
|
|
|
|
// reduce casts when the expressions are expanded.
|
|
|
|
return getOperand(getNumOperands() - 1)->getType();
|
|
|
|
}
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scAddExpr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVMulExpr - This node represents multiplication of some number of SCEVs.
|
|
|
|
///
|
|
|
|
class SCEVMulExpr : public SCEVCommutativeExpr {
|
2007-10-22 18:31:58 +00:00
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVMulExpr(const FoldingSetNodeIDRef ID,
|
2010-03-18 01:17:13 +00:00
|
|
|
const SCEV *const *O, size_t N)
|
2010-06-18 19:54:20 +00:00
|
|
|
: SCEVCommutativeExpr(ID, scMulExpr, O, N) {
|
2004-04-15 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scMulExpr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
2008-02-11 11:03:14 +00:00
|
|
|
/// SCEVUDivExpr - This class represents a binary unsigned division operation.
|
2004-04-15 15:06:59 +00:00
|
|
|
///
|
2008-02-11 11:03:14 +00:00
|
|
|
class SCEVUDivExpr : public SCEV {
|
2007-10-22 18:31:58 +00:00
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *LHS;
|
|
|
|
const SCEV *RHS;
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVUDivExpr(const FoldingSetNodeIDRef ID, const SCEV *lhs, const SCEV *rhs)
|
|
|
|
: SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {}
|
2004-04-15 15:06:59 +00:00
|
|
|
|
|
|
|
public:
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getLHS() const { return LHS; }
|
|
|
|
const SCEV *getRHS() const { return RHS; }
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *getType() const {
|
2010-11-17 22:27:42 +00:00
|
|
|
// In most cases the types of LHS and RHS will be the same, but in some
|
|
|
|
// crazy cases one or the other may be a pointer. ScalarEvolution doesn't
|
|
|
|
// depend on the type for correctness, but handling types carefully can
|
|
|
|
// avoid extra casts in the SCEVExpander. The LHS is more likely to be
|
|
|
|
// a pointer type than the RHS, so use the RHS' type here.
|
|
|
|
return getRHS()->getType();
|
2005-02-13 04:34:51 +00:00
|
|
|
}
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
2008-02-11 11:03:14 +00:00
|
|
|
return S->getSCEVType() == scUDivExpr;
|
2004-04-15 15:06:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
|
2009-04-29 00:59:03 +00:00
|
|
|
/// count of the specified loop. This is the primary focus of the
|
|
|
|
/// ScalarEvolution framework; all the other SCEV subclasses are mostly just
|
|
|
|
/// supporting infrastructure to allow SCEVAddRecExpr expressions to be
|
|
|
|
/// created and analyzed.
|
2004-04-15 15:06:59 +00:00
|
|
|
///
|
|
|
|
/// All operands of an AddRec are required to be loop invariant.
|
|
|
|
///
|
2009-05-07 14:00:19 +00:00
|
|
|
class SCEVAddRecExpr : public SCEVNAryExpr {
|
2007-10-22 18:31:58 +00:00
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
const Loop *L;
|
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVAddRecExpr(const FoldingSetNodeIDRef ID,
|
2010-03-18 01:17:13 +00:00
|
|
|
const SCEV *const *O, size_t N, const Loop *l)
|
2010-11-17 20:48:38 +00:00
|
|
|
: SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) {}
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2009-05-07 14:00:19 +00:00
|
|
|
public:
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getStart() const { return Operands[0]; }
|
2004-04-15 15:06:59 +00:00
|
|
|
const Loop *getLoop() const { return L; }
|
|
|
|
|
|
|
|
/// getStepRecurrence - This method constructs and returns the recurrence
|
|
|
|
/// indicating how much this expression steps by. If this is a polynomial
|
|
|
|
/// of degree N, it returns a chrec of degree N-1.
|
2011-03-14 16:50:06 +00:00
|
|
|
/// We cannot determine whether the step recurrence has self-wraparound.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getStepRecurrence(ScalarEvolution &SE) const {
|
2008-06-22 19:23:09 +00:00
|
|
|
if (isAffine()) return getOperand(1);
|
2009-07-07 17:06:11 +00:00
|
|
|
return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1,
|
|
|
|
op_end()),
|
2011-03-14 16:50:06 +00:00
|
|
|
getLoop(), FlagAnyWrap);
|
2004-04-15 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// isAffine - Return true if this is an affine AddRec (i.e., it represents
|
|
|
|
/// an expressions A+B*x where A and B are loop invariant values.
|
|
|
|
bool isAffine() const {
|
|
|
|
// We know that the start value is invariant. This expression is thus
|
2012-09-27 10:14:43 +00:00
|
|
|
// affine iff the step is also invariant.
|
2004-04-15 15:06:59 +00:00
|
|
|
return getNumOperands() == 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
|
|
|
|
/// represents an expressions A+B*x+C*x^2 where A, B and C are loop
|
|
|
|
/// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
|
|
|
|
bool isQuadratic() const {
|
|
|
|
return getNumOperands() == 3;
|
|
|
|
}
|
|
|
|
|
2011-03-14 16:50:06 +00:00
|
|
|
/// Set flags for a recurrence without clearing any previously set flags.
|
|
|
|
/// For AddRec, either NUW or NSW implies NW. Keep track of this fact here
|
|
|
|
/// to make it easier to propagate flags.
|
|
|
|
void setNoWrapFlags(NoWrapFlags Flags) {
|
|
|
|
if (Flags & (FlagNUW | FlagNSW))
|
|
|
|
Flags = ScalarEvolution::setFlags(Flags, FlagNW);
|
|
|
|
SubclassData |= Flags;
|
|
|
|
}
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
/// evaluateAtIteration - Return the value of this chain of recurrences at
|
|
|
|
/// the specified iteration number.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const;
|
2004-04-15 15:06:59 +00:00
|
|
|
|
|
|
|
/// getNumIterationsInRange - Return the number of iterations of this loop
|
|
|
|
/// that produce values in the specified constant range. Another way of
|
|
|
|
/// looking at this is that it returns the first iteration number where the
|
|
|
|
/// value is not in the condition, thus computing the exit count. If the
|
|
|
|
/// iteration count can't be computed, an instance of SCEVCouldNotCompute is
|
2007-07-16 02:08:00 +00:00
|
|
|
/// returned.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getNumIterationsInRange(ConstantRange Range,
|
2007-10-22 18:31:58 +00:00
|
|
|
ScalarEvolution &SE) const;
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2009-07-13 20:50:19 +00:00
|
|
|
/// getPostIncExpr - Return an expression representing the value of
|
|
|
|
/// this expression one iteration of the loop ahead.
|
2009-07-25 01:13:03 +00:00
|
|
|
const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const {
|
|
|
|
return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE)));
|
2009-07-13 20:50:19 +00:00
|
|
|
}
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scAddRecExpr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-11-25 22:41:31 +00:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVSMaxExpr - This class represents a signed maximum selection.
|
|
|
|
///
|
|
|
|
class SCEVSMaxExpr : public SCEVCommutativeExpr {
|
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVSMaxExpr(const FoldingSetNodeIDRef ID,
|
2010-03-18 01:17:13 +00:00
|
|
|
const SCEV *const *O, size_t N)
|
2010-06-18 19:54:20 +00:00
|
|
|
: SCEVCommutativeExpr(ID, scSMaxExpr, O, N) {
|
2009-10-09 00:10:36 +00:00
|
|
|
// Max never overflows.
|
2011-03-14 16:50:06 +00:00
|
|
|
setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
|
2007-11-25 22:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scSMaxExpr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-02-20 06:48:22 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVUMaxExpr - This class represents an unsigned maximum selection.
|
|
|
|
///
|
|
|
|
class SCEVUMaxExpr : public SCEVCommutativeExpr {
|
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2010-06-18 19:54:20 +00:00
|
|
|
SCEVUMaxExpr(const FoldingSetNodeIDRef ID,
|
2010-03-18 01:17:13 +00:00
|
|
|
const SCEV *const *O, size_t N)
|
2010-06-18 19:54:20 +00:00
|
|
|
: SCEVCommutativeExpr(ID, scUMaxExpr, O, N) {
|
2009-10-09 00:10:36 +00:00
|
|
|
// Max never overflows.
|
2011-03-14 16:50:06 +00:00
|
|
|
setNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW));
|
2008-02-20 06:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scUMaxExpr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
/// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
|
2009-09-15 15:08:33 +00:00
|
|
|
/// value, and only represent it as its LLVM Value. This is the "bottom"
|
2004-04-15 15:06:59 +00:00
|
|
|
/// value for the analysis.
|
|
|
|
///
|
2010-08-02 23:49:30 +00:00
|
|
|
class SCEVUnknown : public SCEV, private CallbackVH {
|
2007-10-22 18:31:58 +00:00
|
|
|
friend class ScalarEvolution;
|
|
|
|
|
2010-08-02 23:49:30 +00:00
|
|
|
// Implement CallbackVH.
|
|
|
|
virtual void deleted();
|
|
|
|
virtual void allUsesReplacedWith(Value *New);
|
|
|
|
|
|
|
|
/// SE - The parent ScalarEvolution value. This is used to update
|
|
|
|
/// the parent's maps when the value associated with a SCEVUnknown
|
|
|
|
/// is deleted or RAUW'd.
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
|
|
|
|
/// Next - The next pointer in the linked list of all
|
|
|
|
/// SCEVUnknown instances owned by a ScalarEvolution.
|
|
|
|
SCEVUnknown *Next;
|
|
|
|
|
|
|
|
SCEVUnknown(const FoldingSetNodeIDRef ID, Value *V,
|
|
|
|
ScalarEvolution *se, SCEVUnknown *next) :
|
|
|
|
SCEV(ID, scUnknown), CallbackVH(V), SE(se), Next(next) {}
|
2009-06-27 21:21:31 +00:00
|
|
|
|
2009-07-13 20:50:19 +00:00
|
|
|
public:
|
2010-08-02 23:49:30 +00:00
|
|
|
Value *getValue() const { return getValPtr(); }
|
2004-04-15 15:06:59 +00:00
|
|
|
|
2010-01-28 02:15:55 +00:00
|
|
|
/// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special
|
|
|
|
/// constant representing a type size, alignment, or field offset in
|
|
|
|
/// a target-independent manner, and hasn't happened to have been
|
|
|
|
/// folded with other operations into something unrecognizable. This
|
|
|
|
/// is mainly only useful for pretty-printing and other situations
|
|
|
|
/// where it isn't absolutely required for these to succeed.
|
2011-07-18 04:54:35 +00:00
|
|
|
bool isSizeOf(Type *&AllocTy) const;
|
|
|
|
bool isAlignOf(Type *&AllocTy) const;
|
|
|
|
bool isOffsetOf(Type *&STy, Constant *&FieldNo) const;
|
2010-01-28 02:15:55 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *getType() const { return getValPtr()->getType(); }
|
2004-04-15 15:06:59 +00:00
|
|
|
|
|
|
|
/// Methods for support type inquiry through isa, cast, and dyn_cast:
|
|
|
|
static inline bool classof(const SCEV *S) {
|
|
|
|
return S->getSCEVType() == scUnknown;
|
|
|
|
}
|
|
|
|
};
|
2004-04-23 21:28:25 +00:00
|
|
|
|
|
|
|
/// SCEVVisitor - This class defines a simple visitor class that may be used
|
|
|
|
/// for various SCEV analysis purposes.
|
|
|
|
template<typename SC, typename RetVal=void>
|
|
|
|
struct SCEVVisitor {
|
2009-04-18 17:56:28 +00:00
|
|
|
RetVal visit(const SCEV *S) {
|
2004-04-23 21:28:25 +00:00
|
|
|
switch (S->getSCEVType()) {
|
|
|
|
case scConstant:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitConstant((const SCEVConstant*)S);
|
2004-04-23 21:28:25 +00:00
|
|
|
case scTruncate:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S);
|
2004-04-23 21:28:25 +00:00
|
|
|
case scZeroExtend:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S);
|
2007-06-15 14:38:12 +00:00
|
|
|
case scSignExtend:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S);
|
2004-04-23 21:28:25 +00:00
|
|
|
case scAddExpr:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S);
|
2004-04-23 21:28:25 +00:00
|
|
|
case scMulExpr:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S);
|
2008-02-11 11:03:14 +00:00
|
|
|
case scUDivExpr:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S);
|
2004-04-23 21:28:25 +00:00
|
|
|
case scAddRecExpr:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S);
|
2007-11-25 22:41:31 +00:00
|
|
|
case scSMaxExpr:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S);
|
2008-02-20 06:48:22 +00:00
|
|
|
case scUMaxExpr:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S);
|
2004-04-23 21:28:25 +00:00
|
|
|
case scUnknown:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitUnknown((const SCEVUnknown*)S);
|
2004-04-23 21:28:25 +00:00
|
|
|
case scCouldNotCompute:
|
2009-04-18 17:56:28 +00:00
|
|
|
return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S);
|
2004-04-23 21:28:25 +00:00
|
|
|
default:
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Unknown SCEV type!");
|
2004-04-23 21:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-18 17:56:28 +00:00
|
|
|
RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) {
|
2009-07-14 16:55:14 +00:00
|
|
|
llvm_unreachable("Invalid use of SCEVCouldNotCompute!");
|
2004-04-23 21:28:25 +00:00
|
|
|
}
|
|
|
|
};
|
2012-07-13 23:33:03 +00:00
|
|
|
|
|
|
|
/// Visit all nodes in the expression tree using worklist traversal.
|
|
|
|
///
|
|
|
|
/// Visitor implements:
|
|
|
|
/// // return true to follow this node.
|
|
|
|
/// bool follow(const SCEV *S);
|
|
|
|
/// // return true to terminate the search.
|
|
|
|
/// bool isDone();
|
|
|
|
template<typename SV>
|
|
|
|
class SCEVTraversal {
|
|
|
|
SV &Visitor;
|
|
|
|
SmallVector<const SCEV *, 8> Worklist;
|
2012-07-18 05:14:03 +00:00
|
|
|
SmallPtrSet<const SCEV *, 8> Visited;
|
2012-07-13 23:33:03 +00:00
|
|
|
|
|
|
|
void push(const SCEV *S) {
|
2012-07-18 05:14:03 +00:00
|
|
|
if (Visited.insert(S) && Visitor.follow(S))
|
2012-07-13 23:33:03 +00:00
|
|
|
Worklist.push_back(S);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
SCEVTraversal(SV& V): Visitor(V) {}
|
|
|
|
|
|
|
|
void visitAll(const SCEV *Root) {
|
|
|
|
push(Root);
|
|
|
|
while (!Worklist.empty() && !Visitor.isDone()) {
|
|
|
|
const SCEV *S = Worklist.pop_back_val();
|
|
|
|
|
|
|
|
switch (S->getSCEVType()) {
|
|
|
|
case scConstant:
|
|
|
|
case scUnknown:
|
|
|
|
break;
|
|
|
|
case scTruncate:
|
|
|
|
case scZeroExtend:
|
|
|
|
case scSignExtend:
|
|
|
|
push(cast<SCEVCastExpr>(S)->getOperand());
|
|
|
|
break;
|
|
|
|
case scAddExpr:
|
|
|
|
case scMulExpr:
|
|
|
|
case scSMaxExpr:
|
|
|
|
case scUMaxExpr:
|
|
|
|
case scAddRecExpr: {
|
|
|
|
const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
|
|
|
|
for (SCEVNAryExpr::op_iterator I = NAry->op_begin(),
|
|
|
|
E = NAry->op_end(); I != E; ++I) {
|
|
|
|
push(*I);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case scUDivExpr: {
|
|
|
|
const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
|
|
|
|
push(UDiv->getLHS());
|
|
|
|
push(UDiv->getRHS());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case scCouldNotCompute:
|
|
|
|
llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown SCEV kind!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Use SCEVTraversal to visit all nodes in the givien expression tree.
|
|
|
|
template<typename SV>
|
|
|
|
void visitAll(const SCEV *Root, SV& Visitor) {
|
|
|
|
SCEVTraversal<SV> T(Visitor);
|
|
|
|
T.visitAll(Root);
|
|
|
|
}
|
2013-02-15 20:55:33 +00:00
|
|
|
|
2013-02-15 21:26:36 +00:00
|
|
|
/// The SCEVRewriter takes a scalar evolution expression and copies all its
|
2013-02-15 20:55:33 +00:00
|
|
|
/// components. The result after a rewrite is an identical SCEV.
|
2013-02-15 21:26:36 +00:00
|
|
|
struct SCEVRewriter
|
|
|
|
: public SCEVVisitor<SCEVRewriter, const SCEV*> {
|
2013-02-15 20:55:33 +00:00
|
|
|
public:
|
2013-02-15 21:26:36 +00:00
|
|
|
SCEVRewriter(ScalarEvolution &S) : SE(S) {}
|
2013-02-15 20:55:33 +00:00
|
|
|
|
2013-02-15 23:51:59 +00:00
|
|
|
virtual ~SCEVRewriter() {}
|
|
|
|
|
2013-02-15 20:55:33 +00:00
|
|
|
virtual const SCEV *visitConstant(const SCEVConstant *Constant) {
|
|
|
|
return Constant;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitTruncateExpr(const SCEVTruncateExpr *Expr) {
|
|
|
|
const SCEV *Operand = visit(Expr->getOperand());
|
|
|
|
return SE.getTruncateExpr(Operand, Expr->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
|
|
|
|
const SCEV *Operand = visit(Expr->getOperand());
|
|
|
|
return SE.getZeroExtendExpr(Operand, Expr->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
|
|
|
|
const SCEV *Operand = visit(Expr->getOperand());
|
|
|
|
return SE.getSignExtendExpr(Operand, Expr->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 2> Operands;
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
|
|
|
|
Operands.push_back(visit(Expr->getOperand(i)));
|
|
|
|
return SE.getAddExpr(Operands);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 2> Operands;
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
|
|
|
|
Operands.push_back(visit(Expr->getOperand(i)));
|
|
|
|
return SE.getMulExpr(Operands);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitUDivExpr(const SCEVUDivExpr *Expr) {
|
|
|
|
return SE.getUDivExpr(visit(Expr->getLHS()), visit(Expr->getRHS()));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 2> Operands;
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
|
|
|
|
Operands.push_back(visit(Expr->getOperand(i)));
|
|
|
|
return SE.getAddRecExpr(Operands, Expr->getLoop(),
|
|
|
|
Expr->getNoWrapFlags());
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitSMaxExpr(const SCEVSMaxExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 2> Operands;
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
|
|
|
|
Operands.push_back(visit(Expr->getOperand(i)));
|
|
|
|
return SE.getSMaxExpr(Operands);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitUMaxExpr(const SCEVUMaxExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 2> Operands;
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
|
|
|
|
Operands.push_back(visit(Expr->getOperand(i)));
|
|
|
|
return SE.getUMaxExpr(Operands);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitUnknown(const SCEVUnknown *Expr) {
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ScalarEvolution &SE;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef DenseMap<const Value*, Value*> ValueToValueMap;
|
|
|
|
|
2013-02-15 21:26:36 +00:00
|
|
|
/// The SCEVParameterRewriter takes a scalar evolution expression and updates
|
2013-02-15 20:55:33 +00:00
|
|
|
/// the SCEVUnknown components following the Map (Value -> Value).
|
2013-02-15 21:26:36 +00:00
|
|
|
struct SCEVParameterRewriter: public SCEVRewriter {
|
2013-02-15 20:55:33 +00:00
|
|
|
public:
|
|
|
|
static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
|
|
|
|
ValueToValueMap &Map) {
|
2013-02-15 21:26:36 +00:00
|
|
|
SCEVParameterRewriter Rewriter(SE, Map);
|
2013-02-15 20:55:33 +00:00
|
|
|
return Rewriter.visit(Scev);
|
|
|
|
}
|
2013-02-15 21:26:36 +00:00
|
|
|
SCEVParameterRewriter(ScalarEvolution &S, ValueToValueMap &M)
|
|
|
|
: SCEVRewriter(S), Map(M) {}
|
2013-02-15 20:55:33 +00:00
|
|
|
|
|
|
|
virtual const SCEV *visitUnknown(const SCEVUnknown *Expr) {
|
|
|
|
Value *V = Expr->getValue();
|
|
|
|
if (Map.count(V))
|
|
|
|
return SE.getUnknown(Map[V]);
|
|
|
|
return Expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ValueToValueMap ⤅
|
|
|
|
};
|
|
|
|
|
2013-02-15 20:55:36 +00:00
|
|
|
typedef DenseMap<const Loop*, const SCEV*> LoopToScevMapT;
|
|
|
|
|
2013-02-15 21:26:36 +00:00
|
|
|
/// The SCEVApplyRewriter takes a scalar evolution expression and applies
|
2013-02-15 20:55:36 +00:00
|
|
|
/// the Map (Loop -> SCEV) to all AddRecExprs.
|
2013-02-15 21:26:36 +00:00
|
|
|
struct SCEVApplyRewriter: public SCEVRewriter {
|
2013-02-15 20:55:36 +00:00
|
|
|
public:
|
|
|
|
static const SCEV *rewrite(const SCEV *Scev, LoopToScevMapT &Map,
|
|
|
|
ScalarEvolution &SE) {
|
2013-02-15 21:26:36 +00:00
|
|
|
SCEVApplyRewriter Rewriter(SE, Map);
|
2013-02-15 20:55:36 +00:00
|
|
|
return Rewriter.visit(Scev);
|
|
|
|
}
|
2013-02-15 21:26:36 +00:00
|
|
|
SCEVApplyRewriter(ScalarEvolution &S, LoopToScevMapT &M)
|
|
|
|
: SCEVRewriter(S), Map(M) {}
|
2013-02-15 20:55:36 +00:00
|
|
|
|
|
|
|
virtual const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
|
|
|
|
SmallVector<const SCEV *, 2> Operands;
|
|
|
|
for (int i = 0, e = Expr->getNumOperands(); i < e; ++i)
|
|
|
|
Operands.push_back(visit(Expr->getOperand(i)));
|
|
|
|
|
|
|
|
const Loop *L = Expr->getLoop();
|
|
|
|
const SCEV *Res = SE.getAddRecExpr(Operands, L, Expr->getNoWrapFlags());
|
|
|
|
|
|
|
|
if (0 == Map.count(L))
|
|
|
|
return Res;
|
|
|
|
|
|
|
|
const SCEVAddRecExpr *Rec = (const SCEVAddRecExpr *) Res;
|
|
|
|
return Rec->evaluateAtIteration(Map[L], SE);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LoopToScevMapT ⤅
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Applies the Map (Loop -> SCEV) to the given Scev.
|
|
|
|
static inline const SCEV *apply(const SCEV *Scev, LoopToScevMapT &Map,
|
|
|
|
ScalarEvolution &SE) {
|
2013-02-15 21:26:36 +00:00
|
|
|
return SCEVApplyRewriter::rewrite(Scev, Map, SE);
|
2013-02-15 20:55:36 +00:00
|
|
|
}
|
|
|
|
|
2004-04-15 15:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|