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"
|
2009-07-13 22:19:41 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2009-08-11 16:02:12 +00:00
|
|
|
#include "llvm/Function.h"
|
2007-03-05 00:00:42 +00:00
|
|
|
#include "llvm/Support/DataTypes.h"
|
2009-05-04 22:30:44 +00:00
|
|
|
#include "llvm/Support/ValueHandle.h"
|
2009-06-27 21:21:31 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2009-07-13 21:35:55 +00:00
|
|
|
#include "llvm/Support/ConstantRange.h"
|
2009-06-27 21:21:31 +00:00
|
|
|
#include "llvm/ADT/FoldingSet.h"
|
2009-06-14 22:55:07 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2008-05-29 19:52:31 +00:00
|
|
|
#include <iosfwd>
|
2009-07-13 22:19:41 +00:00
|
|
|
#include <map>
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2007-10-22 18:31:58 +00:00
|
|
|
class APInt;
|
2009-07-13 22:19:41 +00:00
|
|
|
class Constant;
|
2007-10-22 18:31:58 +00:00
|
|
|
class ConstantInt;
|
2009-07-13 22:19:41 +00:00
|
|
|
class DominatorTree;
|
2004-04-02 20:23:17 +00:00
|
|
|
class Type;
|
2007-10-22 18:31:58 +00:00
|
|
|
class ScalarEvolution;
|
2009-04-21 23:15:49 +00:00
|
|
|
class TargetData;
|
2009-08-11 17:45:13 +00:00
|
|
|
class LLVMContext;
|
2009-07-13 22:19:41 +00:00
|
|
|
class Loop;
|
|
|
|
class LoopInfo;
|
2009-07-17 21:03:54 +00:00
|
|
|
class Operator;
|
2004-04-02 20:23:17 +00:00
|
|
|
|
2009-05-24 23:45:28 +00:00
|
|
|
/// SCEV - This class represents an analyzed expression in the program. These
|
2009-06-24 04:47:54 +00:00
|
|
|
/// are opaque objects that the client is not allowed to do much with
|
|
|
|
/// directly.
|
2004-04-02 20:23:17 +00:00
|
|
|
///
|
2009-07-13 20:50:19 +00:00
|
|
|
class SCEV : public FastFoldingSetNode {
|
2009-07-24 00:59:53 +00:00
|
|
|
// The SCEV baseclass this node corresponds to
|
|
|
|
const unsigned short SCEVType;
|
2004-04-02 20:23:17 +00:00
|
|
|
|
2009-07-24 00:59:53 +00:00
|
|
|
protected:
|
|
|
|
/// SubclassData - This field is initialized to zero and may be used in
|
|
|
|
/// subclasses to store miscelaneous information.
|
|
|
|
unsigned short SubclassData;
|
|
|
|
|
|
|
|
private:
|
2004-04-02 20:23:17 +00:00
|
|
|
SCEV(const SCEV &); // DO NOT IMPLEMENT
|
|
|
|
void operator=(const SCEV &); // DO NOT IMPLEMENT
|
|
|
|
protected:
|
|
|
|
virtual ~SCEV();
|
|
|
|
public:
|
2009-07-13 20:50:19 +00:00
|
|
|
explicit SCEV(const FoldingSetNodeID &ID, unsigned SCEVTy) :
|
2009-07-24 00:59:53 +00:00
|
|
|
FastFoldingSetNode(ID), SCEVType(SCEVTy), SubclassData(0) {}
|
2009-06-27 21:21:31 +00:00
|
|
|
|
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;
|
|
|
|
|
2008-06-18 16:23:07 +00:00
|
|
|
/// isZero - Return true if the expression is a constant zero.
|
|
|
|
///
|
|
|
|
bool isZero() const;
|
|
|
|
|
2009-05-18 15:22:39 +00:00
|
|
|
/// isOne - Return true if the expression is a constant one.
|
|
|
|
///
|
|
|
|
bool isOne() const;
|
|
|
|
|
2009-06-24 00:30:26 +00:00
|
|
|
/// isAllOnesValue - Return true if the expression is a constant
|
|
|
|
/// all-ones value.
|
|
|
|
///
|
|
|
|
bool isAllOnesValue() const;
|
|
|
|
|
2009-07-25 01:13:03 +00:00
|
|
|
/// hasOperand - Test whether this SCEV has Op as a direct or
|
|
|
|
/// indirect operand.
|
|
|
|
virtual bool hasOperand(const SCEV *Op) const = 0;
|
2005-02-13 04:34:51 +00:00
|
|
|
|
2009-02-17 00:13:06 +00:00
|
|
|
/// dominates - Return true if elements that makes up this SCEV dominates
|
|
|
|
/// the specified basic block.
|
|
|
|
virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0;
|
|
|
|
|
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.
|
2009-04-21 00:47:46 +00:00
|
|
|
virtual void print(raw_ostream &OS) const = 0;
|
|
|
|
void print(std::ostream &OS) const;
|
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
|
|
|
|
2009-04-21 00:47:46 +00:00
|
|
|
inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
|
|
|
|
S.print(OS);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
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 {
|
2009-06-22 21:57:23 +00:00
|
|
|
SCEVCouldNotCompute();
|
2004-04-02 20:23:17 +00:00
|
|
|
|
|
|
|
// 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;
|
2009-04-21 00:47:46 +00:00
|
|
|
virtual void print(raw_ostream &OS) const;
|
2009-07-25 01:13:03 +00:00
|
|
|
virtual bool hasOperand(const SCEV *Op) const;
|
2004-04-02 20:23:17 +00:00
|
|
|
|
2009-02-17 00:13:06 +00:00
|
|
|
virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// 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 {
|
2009-05-19 19:22:47 +00:00
|
|
|
/// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be
|
|
|
|
/// notified whenever a Value is deleted.
|
|
|
|
class SCEVCallbackVH : public CallbackVH {
|
|
|
|
ScalarEvolution *SE;
|
|
|
|
virtual void deleted();
|
|
|
|
virtual void allUsesReplacedWith(Value *New);
|
|
|
|
public:
|
|
|
|
SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0);
|
|
|
|
};
|
|
|
|
|
2009-05-04 22:30:44 +00:00
|
|
|
friend class SCEVCallbackVH;
|
2009-07-12 23:50:34 +00:00
|
|
|
friend struct SCEVExpander;
|
2009-05-04 22:30:44 +00:00
|
|
|
|
2009-04-21 23:15:49 +00:00
|
|
|
/// F - The function we are analyzing.
|
|
|
|
///
|
|
|
|
Function *F;
|
|
|
|
|
|
|
|
/// LI - The loop information for the function we are currently analyzing.
|
|
|
|
///
|
|
|
|
LoopInfo *LI;
|
|
|
|
|
|
|
|
/// TD - The target data information for the target we are targetting.
|
|
|
|
///
|
|
|
|
TargetData *TD;
|
|
|
|
|
2009-06-06 14:37:11 +00:00
|
|
|
/// CouldNotCompute - This SCEV is used to represent unknown trip
|
|
|
|
/// counts and things.
|
2009-06-27 21:21:31 +00:00
|
|
|
SCEVCouldNotCompute CouldNotCompute;
|
2009-04-21 23:15:49 +00:00
|
|
|
|
|
|
|
/// Scalars - This is a cache of the scalars we have analyzed so far.
|
|
|
|
///
|
2009-07-07 17:06:11 +00:00
|
|
|
std::map<SCEVCallbackVH, const SCEV *> Scalars;
|
2009-04-21 23:15:49 +00:00
|
|
|
|
2009-04-30 20:47:05 +00:00
|
|
|
/// BackedgeTakenInfo - Information about the backedge-taken count
|
|
|
|
/// of a loop. This currently inclues an exact count and a maximum count.
|
|
|
|
///
|
|
|
|
struct BackedgeTakenInfo {
|
|
|
|
/// Exact - An expression indicating the exact backedge-taken count of
|
|
|
|
/// the loop if it is known, or a SCEVCouldNotCompute otherwise.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *Exact;
|
2009-04-30 20:47:05 +00:00
|
|
|
|
2009-07-09 22:14:25 +00:00
|
|
|
/// Max - An expression indicating the least maximum backedge-taken
|
2009-04-30 20:47:05 +00:00
|
|
|
/// count of the loop that is known, or a SCEVCouldNotCompute.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *Max;
|
2009-04-30 20:47:05 +00:00
|
|
|
|
2009-07-07 17:06:11 +00:00
|
|
|
/*implicit*/ BackedgeTakenInfo(const SCEV *exact) :
|
2009-04-30 20:47:05 +00:00
|
|
|
Exact(exact), Max(exact) {}
|
|
|
|
|
2009-07-07 17:06:11 +00:00
|
|
|
BackedgeTakenInfo(const SCEV *exact, const SCEV *max) :
|
2009-04-30 20:47:05 +00:00
|
|
|
Exact(exact), Max(max) {}
|
|
|
|
|
|
|
|
/// hasAnyInfo - Test whether this BackedgeTakenInfo contains any
|
|
|
|
/// computed information, or whether it's all SCEVCouldNotCompute
|
|
|
|
/// values.
|
|
|
|
bool hasAnyInfo() const {
|
|
|
|
return !isa<SCEVCouldNotCompute>(Exact) ||
|
|
|
|
!isa<SCEVCouldNotCompute>(Max);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-21 23:15:49 +00:00
|
|
|
/// BackedgeTakenCounts - Cache the backedge-taken count of the loops for
|
|
|
|
/// this function as they are computed.
|
2009-04-30 20:47:05 +00:00
|
|
|
std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
|
2009-04-21 23:15:49 +00:00
|
|
|
|
|
|
|
/// ConstantEvolutionLoopExitValue - This map contains entries for all of
|
|
|
|
/// the PHI instructions that we attempt to compute constant evolutions for.
|
|
|
|
/// This allows us to avoid potentially expensive recomputation of these
|
|
|
|
/// properties. An instruction maps to null if we are unable to compute its
|
|
|
|
/// exit value.
|
|
|
|
std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
|
|
|
|
|
2009-05-08 20:47:27 +00:00
|
|
|
/// ValuesAtScopes - This map contains entries for all the instructions
|
|
|
|
/// that we attempt to compute getSCEVAtScope information for without
|
|
|
|
/// using SCEV techniques, which can be expensive.
|
|
|
|
std::map<Instruction *, std::map<const Loop *, Constant *> > ValuesAtScopes;
|
|
|
|
|
2009-04-21 23:15:49 +00:00
|
|
|
/// createSCEV - We know that there is no SCEV for the specified value.
|
|
|
|
/// Analyze the expression.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *createSCEV(Value *V);
|
2009-04-21 23:15:49 +00:00
|
|
|
|
|
|
|
/// createNodeForPHI - Provide the special handling we need to analyze PHI
|
|
|
|
/// SCEVs.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *createNodeForPHI(PHINode *PN);
|
2009-04-21 23:15:49 +00:00
|
|
|
|
2009-05-08 20:26:55 +00:00
|
|
|
/// createNodeForGEP - Provide the special handling we need to analyze GEP
|
|
|
|
/// SCEVs.
|
2009-07-17 21:03:54 +00:00
|
|
|
const SCEV *createNodeForGEP(Operator *GEP);
|
2009-05-08 20:26:55 +00:00
|
|
|
|
2009-07-25 01:13:03 +00:00
|
|
|
/// ForgetSymbolicValue - This looks up computed SCEV values for all
|
|
|
|
/// instructions that depend on the given instruction and removes them from
|
|
|
|
/// the Scalars map if they reference SymName. This is used during PHI
|
|
|
|
/// resolution.
|
|
|
|
void ForgetSymbolicName(Instruction *I, const SCEV *SymName);
|
2009-04-21 23:15:49 +00:00
|
|
|
|
2009-06-21 23:46:38 +00:00
|
|
|
/// getBECount - Subtract the end and start values and divide by the step,
|
|
|
|
/// rounding up, to get the number of times the backedge is executed. Return
|
|
|
|
/// CouldNotCompute if an intermediate computation overflows.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getBECount(const SCEV *Start,
|
2009-07-24 00:55:33 +00:00
|
|
|
const SCEV *End,
|
|
|
|
const SCEV *Step);
|
2009-06-21 23:46:38 +00:00
|
|
|
|
2009-04-30 20:47:05 +00:00
|
|
|
/// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given
|
|
|
|
/// loop, lazily computing new values if the loop hasn't been analyzed
|
|
|
|
/// yet.
|
|
|
|
const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
|
|
|
|
|
2009-04-21 23:15:49 +00:00
|
|
|
/// ComputeBackedgeTakenCount - Compute the number of times the specified
|
|
|
|
/// loop will iterate.
|
2009-04-30 20:47:05 +00:00
|
|
|
BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L);
|
2009-04-21 23:15:49 +00:00
|
|
|
|
2009-06-22 00:31:57 +00:00
|
|
|
/// ComputeBackedgeTakenCountFromExit - Compute the number of times the
|
|
|
|
/// backedge of the specified loop will execute if it exits via the
|
|
|
|
/// specified block.
|
|
|
|
BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L,
|
|
|
|
BasicBlock *ExitingBlock);
|
|
|
|
|
|
|
|
/// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
|
|
|
|
/// backedge of the specified loop will execute if its exit condition
|
|
|
|
/// were a conditional branch of ExitCond, TBB, and FBB.
|
|
|
|
BackedgeTakenInfo
|
|
|
|
ComputeBackedgeTakenCountFromExitCond(const Loop *L,
|
|
|
|
Value *ExitCond,
|
|
|
|
BasicBlock *TBB,
|
|
|
|
BasicBlock *FBB);
|
|
|
|
|
|
|
|
/// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of
|
|
|
|
/// times the backedge of the specified loop will execute if its exit
|
|
|
|
/// condition were a conditional branch of the ICmpInst ExitCond, TBB,
|
|
|
|
/// and FBB.
|
|
|
|
BackedgeTakenInfo
|
|
|
|
ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
|
|
|
|
ICmpInst *ExitCond,
|
|
|
|
BasicBlock *TBB,
|
|
|
|
BasicBlock *FBB);
|
|
|
|
|
2009-04-21 23:15:49 +00:00
|
|
|
/// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
|
2009-07-25 16:18:38 +00:00
|
|
|
/// of 'icmp op load X, cst', try to see if we can compute the
|
|
|
|
/// backedge-taken count.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *
|
2009-04-21 23:15:49 +00:00
|
|
|
ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
|
|
|
|
Constant *RHS,
|
|
|
|
const Loop *L,
|
|
|
|
ICmpInst::Predicate p);
|
|
|
|
|
2009-07-27 16:09:48 +00:00
|
|
|
/// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute
|
2009-04-21 23:15:49 +00:00
|
|
|
/// a constant number of times (the condition evolves only from constants),
|
|
|
|
/// try to evaluate a few iterations of the loop until we get the exit
|
|
|
|
/// condition gets a value of ExitWhen (true or false). If we cannot
|
2009-07-25 16:18:38 +00:00
|
|
|
/// evaluate the backedge-taken count of the loop, return CouldNotCompute.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *ComputeBackedgeTakenCountExhaustively(const Loop *L,
|
2009-06-24 04:47:54 +00:00
|
|
|
Value *Cond,
|
|
|
|
bool ExitWhen);
|
2009-04-21 23:15:49 +00:00
|
|
|
|
|
|
|
/// HowFarToZero - Return the number of times a backedge comparing the
|
|
|
|
/// specified value to zero will execute. If not computable, return
|
2009-06-06 14:37:11 +00:00
|
|
|
/// CouldNotCompute.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *HowFarToZero(const SCEV *V, const Loop *L);
|
2009-04-21 23:15:49 +00:00
|
|
|
|
|
|
|
/// HowFarToNonZero - Return the number of times a backedge checking the
|
|
|
|
/// specified value for nonzero will execute. If not computable, return
|
2009-06-06 14:37:11 +00:00
|
|
|
/// CouldNotCompute.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *HowFarToNonZero(const SCEV *V, const Loop *L);
|
2009-04-21 23:15:49 +00:00
|
|
|
|
|
|
|
/// HowManyLessThans - Return the number of times a backedge containing the
|
|
|
|
/// specified less-than comparison will execute. If not computable, return
|
2009-06-06 14:37:11 +00:00
|
|
|
/// CouldNotCompute. isSigned specifies whether the less-than is signed.
|
2009-05-04 22:30:44 +00:00
|
|
|
BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
|
|
|
|
const Loop *L, bool isSigned);
|
2009-04-21 23:15:49 +00:00
|
|
|
|
2009-05-18 15:36:09 +00:00
|
|
|
/// getLoopPredecessor - If the given loop's header has exactly one unique
|
|
|
|
/// predecessor outside the loop, return it. Otherwise return null.
|
|
|
|
BasicBlock *getLoopPredecessor(const Loop *L);
|
|
|
|
|
2009-04-21 23:15:49 +00:00
|
|
|
/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
|
|
|
|
/// (which may not be an immediate predecessor) which has exactly one
|
|
|
|
/// successor from which BB is reachable, or null if no such block is
|
|
|
|
/// found.
|
|
|
|
BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
|
|
|
|
|
2009-07-21 23:03:19 +00:00
|
|
|
/// isImpliedCond - Test whether the condition described by Pred, LHS,
|
|
|
|
/// and RHS is true whenever the given Cond value evaluates to true.
|
|
|
|
bool isImpliedCond(Value *Cond, ICmpInst::Predicate Pred,
|
|
|
|
const SCEV *LHS, const SCEV *RHS,
|
|
|
|
bool Inverse);
|
|
|
|
|
|
|
|
/// isImpliedCondOperands - Test whether the condition described by Pred,
|
|
|
|
/// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS,
|
|
|
|
/// and FoundRHS is true.
|
|
|
|
bool isImpliedCondOperands(ICmpInst::Predicate Pred,
|
|
|
|
const SCEV *LHS, const SCEV *RHS,
|
|
|
|
const SCEV *FoundLHS, const SCEV *FoundRHS);
|
|
|
|
|
|
|
|
/// isImpliedCondOperandsHelper - Test whether the condition described by
|
|
|
|
/// Pred, LHS, and RHS is true whenever the condition desribed by Pred,
|
|
|
|
/// FoundLHS, and FoundRHS is true.
|
|
|
|
bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
|
|
|
|
const SCEV *LHS, const SCEV *RHS,
|
|
|
|
const SCEV *FoundLHS, const SCEV *FoundRHS);
|
2009-07-13 21:35:55 +00:00
|
|
|
|
2009-04-21 23:15:49 +00:00
|
|
|
/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
|
|
|
|
/// in the header of its containing loop, we know the loop executes a
|
|
|
|
/// constant number of times, and the PHI node is just a recurrence
|
|
|
|
/// involving constants, fold it.
|
|
|
|
Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
|
|
|
|
const Loop *L);
|
|
|
|
|
2004-04-02 20:23:17 +00:00
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2009-04-21 23:15:49 +00:00
|
|
|
ScalarEvolution();
|
2005-04-21 20:19:05 +00:00
|
|
|
|
2009-07-22 00:24:57 +00:00
|
|
|
LLVMContext &getContext() const { return F->getContext(); }
|
2009-07-06 18:42:36 +00:00
|
|
|
|
2009-04-21 01:07:12 +00:00
|
|
|
/// isSCEVable - Test if values of the given type are analyzable within
|
|
|
|
/// the SCEV framework. This primarily includes integer types, and it
|
|
|
|
/// can optionally include pointer types if the ScalarEvolution class
|
|
|
|
/// has access to target-specific information.
|
|
|
|
bool isSCEVable(const Type *Ty) const;
|
|
|
|
|
|
|
|
/// getTypeSizeInBits - Return the size in bits of the specified type,
|
|
|
|
/// for which isSCEVable must return true.
|
|
|
|
uint64_t getTypeSizeInBits(const Type *Ty) const;
|
|
|
|
|
|
|
|
/// getEffectiveSCEVType - Return a type with the same bitwidth as
|
|
|
|
/// the given type and which represents how SCEV will treat the given
|
|
|
|
/// type, for which isSCEVable must return true. For pointer types,
|
|
|
|
/// this is the pointer-sized integer type.
|
|
|
|
const Type *getEffectiveSCEVType(const Type *Ty) const;
|
2009-04-16 03:18:22 +00:00
|
|
|
|
2004-04-02 20:23:17 +00:00
|
|
|
/// getSCEV - Return a SCEV expression handle for the full generality of the
|
|
|
|
/// specified expression.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getSCEV(Value *V);
|
|
|
|
|
|
|
|
const SCEV *getConstant(ConstantInt *V);
|
|
|
|
const SCEV *getConstant(const APInt& Val);
|
|
|
|
const SCEV *getConstant(const Type *Ty, uint64_t V, bool isSigned = false);
|
|
|
|
const SCEV *getTruncateExpr(const SCEV *Op, const Type *Ty);
|
|
|
|
const SCEV *getZeroExtendExpr(const SCEV *Op, const Type *Ty);
|
|
|
|
const SCEV *getSignExtendExpr(const SCEV *Op, const Type *Ty);
|
|
|
|
const SCEV *getAnyExtendExpr(const SCEV *Op, const Type *Ty);
|
|
|
|
const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops);
|
|
|
|
const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS) {
|
|
|
|
SmallVector<const SCEV *, 2> Ops;
|
2007-10-22 18:31:58 +00:00
|
|
|
Ops.push_back(LHS);
|
|
|
|
Ops.push_back(RHS);
|
|
|
|
return getAddExpr(Ops);
|
|
|
|
}
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1,
|
2009-07-24 00:55:33 +00:00
|
|
|
const SCEV *Op2) {
|
2009-07-07 17:06:11 +00:00
|
|
|
SmallVector<const SCEV *, 3> Ops;
|
2007-10-22 18:31:58 +00:00
|
|
|
Ops.push_back(Op0);
|
|
|
|
Ops.push_back(Op1);
|
|
|
|
Ops.push_back(Op2);
|
|
|
|
return getAddExpr(Ops);
|
|
|
|
}
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops);
|
|
|
|
const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS) {
|
|
|
|
SmallVector<const SCEV *, 2> Ops;
|
2007-10-22 18:31:58 +00:00
|
|
|
Ops.push_back(LHS);
|
|
|
|
Ops.push_back(RHS);
|
|
|
|
return getMulExpr(Ops);
|
|
|
|
}
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
|
|
|
|
const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
|
2009-07-24 00:55:33 +00:00
|
|
|
const Loop *L);
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
|
2009-07-24 00:55:33 +00:00
|
|
|
const Loop *L);
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
|
2009-07-24 00:55:33 +00:00
|
|
|
const Loop *L) {
|
2009-07-07 17:06:11 +00:00
|
|
|
SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
|
2007-10-22 18:31:58 +00:00
|
|
|
return getAddRecExpr(NewOp, L);
|
|
|
|
}
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
|
|
|
|
const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
|
|
|
|
const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
|
|
|
|
const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
|
|
|
|
const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
|
|
|
|
const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
|
|
|
|
const SCEV *getUnknown(Value *V);
|
|
|
|
const SCEV *getCouldNotCompute();
|
2007-10-22 18:31:58 +00:00
|
|
|
|
|
|
|
/// getNegativeSCEV - Return the SCEV object corresponding to -V.
|
|
|
|
///
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getNegativeSCEV(const SCEV *V);
|
2007-10-22 18:31:58 +00:00
|
|
|
|
2008-02-20 06:48:22 +00:00
|
|
|
/// getNotSCEV - Return the SCEV object corresponding to ~V.
|
|
|
|
///
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getNotSCEV(const SCEV *V);
|
2008-02-20 06:48:22 +00:00
|
|
|
|
2007-10-22 18:31:58 +00:00
|
|
|
/// getMinusSCEV - Return LHS-RHS.
|
|
|
|
///
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getMinusSCEV(const SCEV *LHS,
|
2009-07-24 00:55:33 +00:00
|
|
|
const SCEV *RHS);
|
2007-10-22 18:31:58 +00:00
|
|
|
|
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.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getTruncateOrZeroExtend(const SCEV *V, const Type *Ty);
|
2008-06-13 04:38:55 +00:00
|
|
|
|
2009-02-18 17:22:41 +00:00
|
|
|
/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
|
|
|
|
/// of the input value to the specified type. If the type must be
|
|
|
|
/// extended, it is sign extended.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getTruncateOrSignExtend(const SCEV *V, const Type *Ty);
|
2009-02-18 17:22:41 +00:00
|
|
|
|
2009-05-13 03:46:30 +00:00
|
|
|
/// getNoopOrZeroExtend - 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. The conversion must not be narrowing.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getNoopOrZeroExtend(const SCEV *V, const Type *Ty);
|
2009-05-13 03:46:30 +00:00
|
|
|
|
|
|
|
/// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of
|
|
|
|
/// the input value to the specified type. If the type must be extended,
|
|
|
|
/// it is sign extended. The conversion must not be narrowing.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getNoopOrSignExtend(const SCEV *V, const Type *Ty);
|
2009-05-13 03:46:30 +00:00
|
|
|
|
2009-06-13 15:56:47 +00:00
|
|
|
/// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
|
|
|
|
/// the input value to the specified type. If the type must be extended,
|
|
|
|
/// it is extended with unspecified bits. The conversion must not be
|
|
|
|
/// narrowing.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getNoopOrAnyExtend(const SCEV *V, const Type *Ty);
|
2009-06-13 15:56:47 +00:00
|
|
|
|
2009-05-13 03:46:30 +00:00
|
|
|
/// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
|
|
|
|
/// input value to the specified type. The conversion must not be
|
|
|
|
/// widening.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getTruncateOrNoop(const SCEV *V, const Type *Ty);
|
2009-05-13 03:46:30 +00:00
|
|
|
|
2009-06-24 00:54:57 +00:00
|
|
|
/// getIntegerSCEV - Given a SCEVable type, create a constant for the
|
2007-10-22 18:31:58 +00:00
|
|
|
/// specified signed integer value and return a SCEV for the constant.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getIntegerSCEV(int Val, const Type *Ty);
|
2007-10-22 18:31:58 +00:00
|
|
|
|
2009-06-22 00:31:57 +00:00
|
|
|
/// getUMaxFromMismatchedTypes - Promote the operands to the wider of
|
|
|
|
/// the types using zero-extension, and then perform a umax operation
|
|
|
|
/// with them.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS,
|
2009-07-24 00:55:33 +00:00
|
|
|
const SCEV *RHS);
|
2009-06-22 00:31:57 +00:00
|
|
|
|
2009-06-22 15:03:27 +00:00
|
|
|
/// getUMinFromMismatchedTypes - Promote the operands to the wider of
|
|
|
|
/// the types using zero-extension, and then perform a umin operation
|
|
|
|
/// with them.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS,
|
|
|
|
const SCEV *RHS);
|
2009-06-22 15:03:27 +00:00
|
|
|
|
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.
|
|
|
|
///
|
2009-05-24 23:25:42 +00:00
|
|
|
/// In the case that a relevant loop exit value cannot be computed, the
|
|
|
|
/// original value V is returned.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
|
2009-05-08 20:38:54 +00:00
|
|
|
|
|
|
|
/// getSCEVAtScope - This is a convenience function which does
|
|
|
|
/// getSCEVAtScope(getSCEV(V), L).
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getSCEVAtScope(Value *V, const Loop *L);
|
2004-04-02 20:23:17 +00:00
|
|
|
|
2009-02-12 22:19:27 +00:00
|
|
|
/// isLoopGuardedByCond - Test whether entry to the loop is protected by
|
2009-04-30 20:48:53 +00:00
|
|
|
/// a conditional between LHS and RHS. This is used to help avoid max
|
2009-07-13 21:35:55 +00:00
|
|
|
/// expressions in loop trip counts, and to eliminate casts.
|
2009-02-12 22:19:27 +00:00
|
|
|
bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
|
2009-05-04 22:30:44 +00:00
|
|
|
const SCEV *LHS, const SCEV *RHS);
|
2009-02-12 22:19:27 +00:00
|
|
|
|
2009-07-13 21:35:55 +00:00
|
|
|
/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
|
|
|
|
/// protected by a conditional between LHS and RHS. This is used to
|
|
|
|
/// to eliminate casts.
|
|
|
|
bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
|
|
|
|
const SCEV *LHS, const SCEV *RHS);
|
|
|
|
|
2009-02-24 18:55:53 +00:00
|
|
|
/// getBackedgeTakenCount - If the specified loop has a predictable
|
|
|
|
/// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
|
|
|
|
/// object. The backedge-taken count is the number of times the loop header
|
|
|
|
/// will be branched to from within the loop. This is one less than the
|
|
|
|
/// trip count of the loop, since it doesn't count the first iteration,
|
|
|
|
/// when the header is branched to from outside the loop.
|
|
|
|
///
|
|
|
|
/// Note that it is not valid to call this method on a loop without a
|
|
|
|
/// loop-invariant backedge-taken count (see
|
|
|
|
/// hasLoopInvariantBackedgeTakenCount).
|
|
|
|
///
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getBackedgeTakenCount(const Loop *L);
|
2004-04-02 20:23:17 +00:00
|
|
|
|
2009-04-30 20:47:05 +00:00
|
|
|
/// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
|
|
|
|
/// return the least SCEV value that is known never to be less than the
|
|
|
|
/// actual backedge taken count.
|
2009-07-07 17:06:11 +00:00
|
|
|
const SCEV *getMaxBackedgeTakenCount(const Loop *L);
|
2009-04-30 20:47:05 +00:00
|
|
|
|
2009-02-24 18:55:53 +00:00
|
|
|
/// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
|
|
|
|
/// has an analyzable loop-invariant backedge-taken count.
|
2009-04-21 23:15:49 +00:00
|
|
|
bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
|
2004-04-02 20:23:17 +00:00
|
|
|
|
2009-02-24 18:55:53 +00:00
|
|
|
/// forgetLoopBackedgeTakenCount - This method should be called by the
|
2009-02-17 20:49:49 +00:00
|
|
|
/// client when it has changed a loop in a way that may effect
|
2009-02-24 18:55:53 +00:00
|
|
|
/// ScalarEvolution's ability to compute a trip count, or if the loop
|
|
|
|
/// is deleted.
|
|
|
|
void forgetLoopBackedgeTakenCount(const Loop *L);
|
2009-02-17 20:49:49 +00:00
|
|
|
|
2009-06-24 04:47:54 +00:00
|
|
|
/// GetMinTrailingZeros - Determine the minimum number of zero bits that S
|
|
|
|
/// is guaranteed to end in (at every loop iteration). It is, at the same
|
|
|
|
/// time, the minimum number of times S is divisible by 2. For example,
|
|
|
|
/// given {4,+,8} it returns 2. If S is guaranteed to be 0, it returns the
|
|
|
|
/// bitwidth of S.
|
2009-07-07 17:06:11 +00:00
|
|
|
uint32_t GetMinTrailingZeros(const SCEV *S);
|
2009-06-19 23:29:04 +00:00
|
|
|
|
2009-07-13 21:35:55 +00:00
|
|
|
/// getUnsignedRange - Determine the unsigned range for a particular SCEV.
|
|
|
|
///
|
|
|
|
ConstantRange getUnsignedRange(const SCEV *S);
|
|
|
|
|
|
|
|
/// getSignedRange - Determine the signed range for a particular SCEV.
|
|
|
|
///
|
|
|
|
ConstantRange getSignedRange(const SCEV *S);
|
|
|
|
|
|
|
|
/// isKnownNegative - Test if the given expression is known to be negative.
|
|
|
|
///
|
|
|
|
bool isKnownNegative(const SCEV *S);
|
|
|
|
|
|
|
|
/// isKnownPositive - Test if the given expression is known to be positive.
|
|
|
|
///
|
|
|
|
bool isKnownPositive(const SCEV *S);
|
|
|
|
|
|
|
|
/// isKnownNonNegative - Test if the given expression is known to be
|
|
|
|
/// non-negative.
|
|
|
|
///
|
|
|
|
bool isKnownNonNegative(const SCEV *S);
|
|
|
|
|
|
|
|
/// isKnownNonPositive - Test if the given expression is known to be
|
|
|
|
/// non-positive.
|
|
|
|
///
|
|
|
|
bool isKnownNonPositive(const SCEV *S);
|
|
|
|
|
|
|
|
/// isKnownNonZero - Test if the given expression is known to be
|
|
|
|
/// non-zero.
|
|
|
|
///
|
|
|
|
bool isKnownNonZero(const SCEV *S);
|
2009-06-19 23:29:04 +00:00
|
|
|
|
2009-07-13 21:35:55 +00:00
|
|
|
/// isKnownNonZero - Test if the given expression is known to satisfy
|
|
|
|
/// the condition described by Pred, LHS, and RHS.
|
|
|
|
///
|
|
|
|
bool isKnownPredicate(ICmpInst::Predicate Pred,
|
|
|
|
const SCEV *LHS, const SCEV *RHS);
|
2009-06-19 23:29:04 +00:00
|
|
|
|
2004-04-02 20:23:17 +00:00
|
|
|
virtual bool runOnFunction(Function &F);
|
|
|
|
virtual void releaseMemory();
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const;
|
2009-04-21 00:47:46 +00:00
|
|
|
void print(raw_ostream &OS, const Module* = 0) 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);
|
|
|
|
}
|
2009-06-27 21:21:31 +00:00
|
|
|
|
2009-06-22 18:25:46 +00:00
|
|
|
private:
|
2009-06-27 21:21:31 +00:00
|
|
|
FoldingSet<SCEV> UniqueSCEVs;
|
|
|
|
BumpPtrAllocator SCEVAllocator;
|
2004-04-02 20:23:17 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|