ScalarEvolution.h revision 7c0fd8eb724a7228a6cf7e3e5487614c25202a91
153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner//===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===//
29769ab22265b313171d201b5928688524a01bd87Misha Brukman//
353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner//                     The LLVM Compiler Infrastructure
453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
79769ab22265b313171d201b5928688524a01bd87Misha Brukman//
853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner//===----------------------------------------------------------------------===//
953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner//
1053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner// The ScalarEvolution class is an LLVM pass which can be used to analyze and
113f46a3abeedba8d517b4182de34c821d752db058Dan Gohman// categorize scalar expressions in loops.  It specializes in recognizing
1253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner// general induction variables, representing them with the abstract and opaque
1353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner// SCEV class.  Given this analysis, trip counts of loops and other important
1453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner// properties can be obtained.
1553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner//
1653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner// This analysis is primarily useful for induction variable substitution and
1753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner// strength reduction.
189769ab22265b313171d201b5928688524a01bd87Misha Brukman//
1953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner//===----------------------------------------------------------------------===//
2053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
2153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H
2253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner#define LLVM_ANALYSIS_SCALAREVOLUTION_H
2353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
2453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner#include "llvm/Pass.h"
2503ee68a145ab5394c070298049d93f305be93ec3Dan Gohman#include "llvm/Instructions.h"
26ffef8acc3e3398bdd04e947c7949befdd52faf86Dan Gohman#include "llvm/Function.h"
278b67f774e9c38b7718b2b300b628388f966df4e0Chandler Carruth#include "llvm/System/DataTypes.h"
2835738ac150afafe2359268d4b2169498c6c98c5fDan Gohman#include "llvm/Support/ValueHandle.h"
291c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman#include "llvm/Support/Allocator.h"
3085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman#include "llvm/Support/ConstantRange.h"
311c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman#include "llvm/ADT/FoldingSet.h"
32444f49150df8a4280ccea20fc2839cd899fc7558Dan Gohman#include "llvm/ADT/DenseMap.h"
3303ee68a145ab5394c070298049d93f305be93ec3Dan Gohman#include <map>
3453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
3553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattnernamespace llvm {
36246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  class APInt;
3703ee68a145ab5394c070298049d93f305be93ec3Dan Gohman  class Constant;
38246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  class ConstantInt;
3903ee68a145ab5394c070298049d93f305be93ec3Dan Gohman  class DominatorTree;
4053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  class Type;
41246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  class ScalarEvolution;
42f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman  class TargetData;
4312ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramer  class LLVMContext;
4403ee68a145ab5394c070298049d93f305be93ec3Dan Gohman  class Loop;
4503ee68a145ab5394c070298049d93f305be93ec3Dan Gohman  class LoopInfo;
46fc8deb971d2b4dff370ba93948975e33a038605dDan Gohman  class Operator;
47dc7a235363166a61e81986c534fe11ceb44109fcDan Gohman  class SCEVUnknown;
48aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman  class SCEV;
49081ad68e687b24dd102ed890bae1e10d8d284cefDan Gohman  template<> struct FoldingSetTrait<SCEV>;
5053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
516c0866ca8373da318741cc90ad7afd1bda22bb1bDan Gohman  /// SCEV - This class represents an analyzed expression in the program.  These
52650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman  /// are opaque objects that the client is not allowed to do much with
53650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman  /// directly.
5453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  ///
55c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman  class SCEV : public FoldingSetNode {
56081ad68e687b24dd102ed890bae1e10d8d284cefDan Gohman    friend struct FoldingSetTrait<SCEV>;
57aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman
58c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman    /// FastID - A reference to an Interned FoldingSetNodeID for this node.
59c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman    /// The ScalarEvolution's BumpPtrAllocator holds the data.
60c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman    FoldingSetNodeIDRef FastID;
61c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman
622f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman    // The SCEV baseclass this node corresponds to
632f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman    const unsigned short SCEVType;
6453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
652f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman  protected:
662f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman    /// SubclassData - This field is initialized to zero and may be used in
673f46a3abeedba8d517b4182de34c821d752db058Dan Gohman    /// subclasses to store miscellaneous information.
682f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman    unsigned short SubclassData;
692f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman
702f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman  private:
7153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    SCEV(const SCEV &);            // DO NOT IMPLEMENT
7253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    void operator=(const SCEV &);  // DO NOT IMPLEMENT
7353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  protected:
7453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual ~SCEV();
7553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  public:
763bf63768e574a2065de1aebba12f6c2e80f4fb16Dan Gohman    explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) :
773bf63768e574a2065de1aebba12f6c2e80f4fb16Dan Gohman      FastID(ID), SCEVType(SCEVTy), SubclassData(0) {}
781c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman
7953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    unsigned getSCEVType() const { return SCEVType; }
8053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
8153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// isLoopInvariant - Return true if the value of this SCEV is unchanging in
8253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// the specified loop.
8353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual bool isLoopInvariant(const Loop *L) const = 0;
8453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
8553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// hasComputableLoopEvolution - Return true if this SCEV changes value in a
8653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// known way in the specified loop.  This property being true implies that
8753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// the value is variant in the loop AND that we can emit an expression to
8853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// compute the value of the expression at any particular loop iteration.
8953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual bool hasComputableLoopEvolution(const Loop *L) const = 0;
9053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
9153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// getType - Return the LLVM type of this SCEV expression.
9253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    ///
9353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual const Type *getType() const = 0;
9453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
95cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman    /// isZero - Return true if the expression is a constant zero.
96cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman    ///
97cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman    bool isZero() const;
98cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman
9970a1fe704831f9b842be0b2a2af5f7082b0e540cDan Gohman    /// isOne - Return true if the expression is a constant one.
10070a1fe704831f9b842be0b2a2af5f7082b0e540cDan Gohman    ///
10170a1fe704831f9b842be0b2a2af5f7082b0e540cDan Gohman    bool isOne() const;
10270a1fe704831f9b842be0b2a2af5f7082b0e540cDan Gohman
1034d289bf4af88759be173a1a809bf8c092d729764Dan Gohman    /// isAllOnesValue - Return true if the expression is a constant
1044d289bf4af88759be173a1a809bf8c092d729764Dan Gohman    /// all-ones value.
1054d289bf4af88759be173a1a809bf8c092d729764Dan Gohman    ///
1064d289bf4af88759be173a1a809bf8c092d729764Dan Gohman    bool isAllOnesValue() const;
1074d289bf4af88759be173a1a809bf8c092d729764Dan Gohman
108fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman    /// hasOperand - Test whether this SCEV has Op as a direct or
109fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman    /// indirect operand.
110fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman    virtual bool hasOperand(const SCEV *Op) const = 0;
111afc0dc7184cf73ff7ac64e516284fbe0c7023ba4Chris Lattner
1125a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng    /// dominates - Return true if elements that makes up this SCEV dominates
1135a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng    /// the specified basic block.
1145a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng    virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0;
1155a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng
1166e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman    /// properlyDominates - Return true if elements that makes up this SCEV
1176e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman    /// properly dominate the specified basic block.
1186e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman    virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const = 0;
1196e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman
12053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// print - Print out the internal representation of this scalar to the
12153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// specified stream.  This should really only be used for debugging
12253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// purposes.
123b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman    virtual void print(raw_ostream &OS) const = 0;
12453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
12553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// dump - This method is used for debugging.
12653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    ///
12753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    void dump() const;
12853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  };
1299769ab22265b313171d201b5928688524a01bd87Misha Brukman
130aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman  // Specialize FoldingSetTrait for SCEV to avoid needing to compute
131aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman  // temporary FoldingSetNodeID values.
132aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman  template<> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> {
133aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman    static void Profile(const SCEV &X, FoldingSetNodeID& ID) {
134aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman      ID = X.FastID;
135aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman    }
136aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman    static bool Equals(const SCEV &X, const FoldingSetNodeID &ID,
137aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman                       FoldingSetNodeID &TempID) {
138aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman      return ID == X.FastID;
139aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman    }
140aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman    static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) {
141aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman      return X.FastID.ComputeHash();
142aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman    }
143aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman  };
144aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman
145b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman  inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) {
146b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman    S.print(OS);
147b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman    return OS;
148b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman  }
149b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman
15053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  /// SCEVCouldNotCompute - An object of this class is returned by queries that
15153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  /// could not be answered.  For example, if you ask for the number of
15253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  /// iterations of a linked-list traversal loop, you will get one of these.
15353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  /// None of the standard SCEV operations are valid on this class, it is just a
15453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  /// marker.
15553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  struct SCEVCouldNotCompute : public SCEV {
156753ad615f96c3d56d6f17983bdba88012e88677cOwen Anderson    SCEVCouldNotCompute();
15753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
15853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    // None of these methods are valid for this object.
15953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual bool isLoopInvariant(const Loop *L) const;
16053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual const Type *getType() const;
16153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual bool hasComputableLoopEvolution(const Loop *L) const;
162b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman    virtual void print(raw_ostream &OS) const;
163fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman    virtual bool hasOperand(const SCEV *Op) const;
16453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
1655a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng    virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const {
1666e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman      return true;
1676e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman    }
1686e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman
1696e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman    virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
1705a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng      return true;
1715a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng    }
1725a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng
17353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// Methods for support type inquiry through isa, cast, and dyn_cast:
17453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    static inline bool classof(const SCEVCouldNotCompute *S) { return true; }
17553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    static bool classof(const SCEV *S);
17653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  };
17753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
17853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  /// ScalarEvolution - This class is the main scalar evolution driver.  Because
17953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  /// client code (intentionally) can't do much with the SCEV objects directly,
18053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  /// they must ask this class for services.
18153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  ///
18253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  class ScalarEvolution : public FunctionPass {
1831959b7562e57f8394496e761486f23b187ac3f1bDan Gohman    /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be
1841959b7562e57f8394496e761486f23b187ac3f1bDan Gohman    /// notified whenever a Value is deleted.
1851959b7562e57f8394496e761486f23b187ac3f1bDan Gohman    class SCEVCallbackVH : public CallbackVH {
1861959b7562e57f8394496e761486f23b187ac3f1bDan Gohman      ScalarEvolution *SE;
1871959b7562e57f8394496e761486f23b187ac3f1bDan Gohman      virtual void deleted();
1881959b7562e57f8394496e761486f23b187ac3f1bDan Gohman      virtual void allUsesReplacedWith(Value *New);
1891959b7562e57f8394496e761486f23b187ac3f1bDan Gohman    public:
1901959b7562e57f8394496e761486f23b187ac3f1bDan Gohman      SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0);
1911959b7562e57f8394496e761486f23b187ac3f1bDan Gohman    };
1921959b7562e57f8394496e761486f23b187ac3f1bDan Gohman
19335738ac150afafe2359268d4b2169498c6c98c5fDan Gohman    friend class SCEVCallbackVH;
194629bff692ae96dea517dc854e27b57ee6c8729efBenjamin Kramer    friend class SCEVExpander;
195dc7a235363166a61e81986c534fe11ceb44109fcDan Gohman    friend class SCEVUnknown;
19635738ac150afafe2359268d4b2169498c6c98c5fDan Gohman
197f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// F - The function we are analyzing.
198f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    ///
199f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    Function *F;
200f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
201f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// LI - The loop information for the function we are currently analyzing.
202f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    ///
203f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    LoopInfo *LI;
204f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
2053f46a3abeedba8d517b4182de34c821d752db058Dan Gohman    /// TD - The target data information for the target we are targeting.
206f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    ///
207f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    TargetData *TD;
208f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
2091cd9275c8d612bd1c92fc7ba436b60aaead1efbfDan Gohman    /// DT - The dominator tree.
2101cd9275c8d612bd1c92fc7ba436b60aaead1efbfDan Gohman    ///
2111cd9275c8d612bd1c92fc7ba436b60aaead1efbfDan Gohman    DominatorTree *DT;
2121cd9275c8d612bd1c92fc7ba436b60aaead1efbfDan Gohman
21386fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman    /// CouldNotCompute - This SCEV is used to represent unknown trip
21486fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman    /// counts and things.
2151c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman    SCEVCouldNotCompute CouldNotCompute;
216f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
217e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman    /// ValueExprMapType - The typedef for ValueExprMap.
218f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    ///
219e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman    typedef DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *> >
220e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman      ValueExprMapType;
221e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman
222e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman    /// ValueExprMap - This is a cache of the values we have analyzed so far.
223e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman    ///
224e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman    ValueExprMapType ValueExprMap;
225f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
226a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    /// BackedgeTakenInfo - Information about the backedge-taken count
2273f46a3abeedba8d517b4182de34c821d752db058Dan Gohman    /// of a loop. This currently includes an exact count and a maximum count.
228a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    ///
229a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    struct BackedgeTakenInfo {
230a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman      /// Exact - An expression indicating the exact backedge-taken count of
231a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman      /// the loop if it is known, or a SCEVCouldNotCompute otherwise.
2320bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      const SCEV *Exact;
233a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman
2347b9547089f0363b803e55dcbde1d6b99710dfe69Andreas Bolka      /// Max - An expression indicating the least maximum backedge-taken
235a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman      /// count of the loop that is known, or a SCEVCouldNotCompute.
2360bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      const SCEV *Max;
237a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman
2380bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      /*implicit*/ BackedgeTakenInfo(const SCEV *exact) :
239a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman        Exact(exact), Max(exact) {}
240a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman
2410bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      BackedgeTakenInfo(const SCEV *exact, const SCEV *max) :
242a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman        Exact(exact), Max(max) {}
243a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman
244a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman      /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any
245a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman      /// computed information, or whether it's all SCEVCouldNotCompute
246a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman      /// values.
247a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman      bool hasAnyInfo() const {
248a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman        return !isa<SCEVCouldNotCompute>(Exact) ||
249a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman               !isa<SCEVCouldNotCompute>(Max);
250a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman      }
251a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    };
252a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman
253f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for
254f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// this function as they are computed.
255a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts;
256f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
257f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// ConstantEvolutionLoopExitValue - This map contains entries for all of
258f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// the PHI instructions that we attempt to compute constant evolutions for.
259f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// This allows us to avoid potentially expensive recomputation of these
260f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// properties.  An instruction maps to null if we are unable to compute its
261f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// exit value.
262f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
263f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
26442214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman    /// ValuesAtScopes - This map contains entries for all the expressions
26542214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman    /// that we attempt to compute getSCEVAtScope information for, which can
26642214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman    /// be expensive in extreme cases.
26742214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman    std::map<const SCEV *,
26842214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman             std::map<const Loop *, const SCEV *> > ValuesAtScopes;
2696bce643c36e7263aada5058f08cd242b4ce6b87dDan Gohman
2706678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman    /// UnsignedRanges - Memoized results from getUnsignedRange
2716678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman    DenseMap<const SCEV *, ConstantRange> UnsignedRanges;
2726678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman
2736678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman    /// SignedRanges - Memoized results from getSignedRange
2746678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman    DenseMap<const SCEV *, ConstantRange> SignedRanges;
2756678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman
2767c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman    /// setUnsignedRange - Set the memoized unsigned range for the given SCEV.
2777c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman    const ConstantRange &setUnsignedRange(const SCEV *S,
2787c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman                                          const ConstantRange &CR) {
2797c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman      std::pair<DenseMap<const SCEV *, ConstantRange>::iterator, bool> Pair =
2807c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman        UnsignedRanges.insert(std::make_pair(S, CR));
2817c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman      if (!Pair.second)
2827c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman        Pair.first->second = CR;
2837c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman      return Pair.first->second;
2847c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman    }
2857c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman
2867c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman    /// setUnsignedRange - Set the memoized signed range for the given SCEV.
2877c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman    const ConstantRange &setSignedRange(const SCEV *S,
2887c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman                                        const ConstantRange &CR) {
2897c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman      std::pair<DenseMap<const SCEV *, ConstantRange>::iterator, bool> Pair =
2907c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman        SignedRanges.insert(std::make_pair(S, CR));
2917c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman      if (!Pair.second)
2927c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman        Pair.first->second = CR;
2937c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman      return Pair.first->second;
2947c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman    }
2957c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman
296f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// createSCEV - We know that there is no SCEV for the specified value.
297f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// Analyze the expression.
2980bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *createSCEV(Value *V);
299f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
300f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// createNodeForPHI - Provide the special handling we need to analyze PHI
301f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// SCEVs.
3020bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *createNodeForPHI(PHINode *PN);
303f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
30426466c0eb3451c5c953b3cca8940359152c4f8e3Dan Gohman    /// createNodeForGEP - Provide the special handling we need to analyze GEP
30526466c0eb3451c5c953b3cca8940359152c4f8e3Dan Gohman    /// SCEVs.
306d281ed2d03654b9cdb290a2d7c73dfe7b826e554Dan Gohman    const SCEV *createNodeForGEP(GEPOperator *GEP);
30726466c0eb3451c5c953b3cca8940359152c4f8e3Dan Gohman
30842214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman    /// computeSCEVAtScope - Implementation code for getSCEVAtScope; called
30942214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman    /// at most once for each SCEV+Loop pair.
31042214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman    ///
31142214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman    const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L);
31242214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman
313fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman    /// ForgetSymbolicValue - This looks up computed SCEV values for all
314fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman    /// instructions that depend on the given instruction and removes them from
315e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman    /// the ValueExprMap map if they reference SymName. This is used during PHI
316fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman    /// resolution.
317fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman    void ForgetSymbolicName(Instruction *I, const SCEV *SymName);
318f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
31951f53b7f5a0e859ceef995c61667905166b96f1bDan Gohman    /// getBECount - Subtract the end and start values and divide by the step,
32051f53b7f5a0e859ceef995c61667905166b96f1bDan Gohman    /// rounding up, to get the number of times the backedge is executed. Return
32151f53b7f5a0e859ceef995c61667905166b96f1bDan Gohman    /// CouldNotCompute if an intermediate computation overflows.
3220bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getBECount(const SCEV *Start,
323a271d36d7ce96bce52fcb73ca1ef028d9a9d8d0fDan Gohman                           const SCEV *End,
3241f96e67f78110d0ac5b30a32375097a28f869c63Dan Gohman                           const SCEV *Step,
3251f96e67f78110d0ac5b30a32375097a28f869c63Dan Gohman                           bool NoWrap);
32651f53b7f5a0e859ceef995c61667905166b96f1bDan Gohman
327a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given
328a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    /// loop, lazily computing new values if the loop hasn't been analyzed
329a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    /// yet.
330a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L);
331a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman
332f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// ComputeBackedgeTakenCount - Compute the number of times the specified
333f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// loop will iterate.
334a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L);
335f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
336a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// ComputeBackedgeTakenCountFromExit - Compute the number of times the
337a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// backedge of the specified loop will execute if it exits via the
338a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// specified block.
339a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L,
340a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman                                                      BasicBlock *ExitingBlock);
341a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman
342a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
343a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// backedge of the specified loop will execute if its exit condition
344a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// were a conditional branch of ExitCond, TBB, and FBB.
345a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    BackedgeTakenInfo
346a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman      ComputeBackedgeTakenCountFromExitCond(const Loop *L,
347a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman                                            Value *ExitCond,
348a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman                                            BasicBlock *TBB,
349a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman                                            BasicBlock *FBB);
350a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman
351a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of
352a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// times the backedge of the specified loop will execute if its exit
353a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// condition were a conditional branch of the ICmpInst ExitCond, TBB,
354a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// and FBB.
355a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    BackedgeTakenInfo
356a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman      ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
357a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman                                                ICmpInst *ExitCond,
358a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman                                                BasicBlock *TBB,
359a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman                                                BasicBlock *FBB);
360a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman
361f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition
3627e77f7959162a601291fd5400a88908d021033d3Dan Gohman    /// of 'icmp op load X, cst', try to see if we can compute the
3637e77f7959162a601291fd5400a88908d021033d3Dan Gohman    /// backedge-taken count.
364f6d009fb6fc19d9f7ee7cdc528bf8e83a758faccDan Gohman    BackedgeTakenInfo
365f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman      ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI,
366f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman                                                   Constant *RHS,
367f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman                                                   const Loop *L,
368f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman                                                   ICmpInst::Predicate p);
369f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
37007ad19b509530b43e6a9acc5c1285cb560dd7198Dan Gohman    /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute
371f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// a constant number of times (the condition evolves only from constants),
372f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// try to evaluate a few iterations of the loop until we get the exit
373f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// condition gets a value of ExitWhen (true or false).  If we cannot
3747e77f7959162a601291fd5400a88908d021033d3Dan Gohman    /// evaluate the backedge-taken count of the loop, return CouldNotCompute.
3750bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *ComputeBackedgeTakenCountExhaustively(const Loop *L,
376650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman                                                      Value *Cond,
377650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman                                                      bool ExitWhen);
378f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
379f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// HowFarToZero - Return the number of times a backedge comparing the
380f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// specified value to zero will execute.  If not computable, return
38186fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman    /// CouldNotCompute.
382f6d009fb6fc19d9f7ee7cdc528bf8e83a758faccDan Gohman    BackedgeTakenInfo HowFarToZero(const SCEV *V, const Loop *L);
383f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
384f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// HowFarToNonZero - Return the number of times a backedge checking the
385f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// specified value for nonzero will execute.  If not computable, return
38686fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman    /// CouldNotCompute.
387f6d009fb6fc19d9f7ee7cdc528bf8e83a758faccDan Gohman    BackedgeTakenInfo HowFarToNonZero(const SCEV *V, const Loop *L);
388f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
389f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// HowManyLessThans - Return the number of times a backedge containing the
390f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// specified less-than comparison will execute.  If not computable, return
39186fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman    /// CouldNotCompute. isSigned specifies whether the less-than is signed.
39235738ac150afafe2359268d4b2169498c6c98c5fDan Gohman    BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
39335738ac150afafe2359268d4b2169498c6c98c5fDan Gohman                                       const Loop *L, bool isSigned);
394f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
395f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
396f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// (which may not be an immediate predecessor) which has exactly one
397f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// successor from which BB is reachable, or null if no such block is
398f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// found.
399005752bbe72868b548bba93dbc1ea46ffe8e5b2cDan Gohman    std::pair<BasicBlock *, BasicBlock *>
400005752bbe72868b548bba93dbc1ea46ffe8e5b2cDan Gohman    getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB);
401f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
402af08a36bd6b9d32a5ea993849d43094fecbd5bedDan Gohman    /// isImpliedCond - Test whether the condition described by Pred, LHS, and
403af08a36bd6b9d32a5ea993849d43094fecbd5bedDan Gohman    /// RHS is true whenever the given FoundCondValue value evaluates to true.
404af08a36bd6b9d32a5ea993849d43094fecbd5bedDan Gohman    bool isImpliedCond(ICmpInst::Predicate Pred,
4050f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman                       const SCEV *LHS, const SCEV *RHS,
406af08a36bd6b9d32a5ea993849d43094fecbd5bedDan Gohman                       Value *FoundCondValue,
4070f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman                       bool Inverse);
4080f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman
4090f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman    /// isImpliedCondOperands - Test whether the condition described by Pred,
4103f46a3abeedba8d517b4182de34c821d752db058Dan Gohman    /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS,
4110f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman    /// and FoundRHS is true.
4120f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman    bool isImpliedCondOperands(ICmpInst::Predicate Pred,
4130f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman                               const SCEV *LHS, const SCEV *RHS,
4140f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman                               const SCEV *FoundLHS, const SCEV *FoundRHS);
4150f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman
4160f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman    /// isImpliedCondOperandsHelper - Test whether the condition described by
4173f46a3abeedba8d517b4182de34c821d752db058Dan Gohman    /// Pred, LHS, and RHS is true whenever the condition described by Pred,
4180f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman    /// FoundLHS, and FoundRHS is true.
4190f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman    bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
4200f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman                                     const SCEV *LHS, const SCEV *RHS,
4210f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman                                     const SCEV *FoundLHS, const SCEV *FoundRHS);
42285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman
423f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
424f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// in the header of its containing loop, we know the loop executes a
425f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// constant number of times, and the PHI node is just a recurrence
426f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    /// involving constants, fold it.
427f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs,
428f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman                                                const Loop *L);
429f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman
43053c66eacc417c0113fba7159487b90005dc8f91eDan Gohman    /// isKnownPredicateWithRanges - Test if the given expression is known to
43153c66eacc417c0113fba7159487b90005dc8f91eDan Gohman    /// satisfy the condition described by Pred and the known constant ranges
43253c66eacc417c0113fba7159487b90005dc8f91eDan Gohman    /// of LHS and RHS.
43353c66eacc417c0113fba7159487b90005dc8f91eDan Gohman    ///
43453c66eacc417c0113fba7159487b90005dc8f91eDan Gohman    bool isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
43553c66eacc417c0113fba7159487b90005dc8f91eDan Gohman                                    const SCEV *LHS, const SCEV *RHS);
43653c66eacc417c0113fba7159487b90005dc8f91eDan Gohman
43753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  public:
438ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky    static char ID; // Pass identification, replacement for typeid
439f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    ScalarEvolution();
4409769ab22265b313171d201b5928688524a01bd87Misha Brukman
441e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson    LLVMContext &getContext() const { return F->getContext(); }
442508955156a25a9abc470a29e1760aa176d341cf9Owen Anderson
443af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// isSCEVable - Test if values of the given type are analyzable within
444af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// the SCEV framework. This primarily includes integer types, and it
445af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// can optionally include pointer types if the ScalarEvolution class
446af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// has access to target-specific information.
447af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    bool isSCEVable(const Type *Ty) const;
448af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman
449af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// getTypeSizeInBits - Return the size in bits of the specified type,
450af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// for which isSCEVable must return true.
451af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    uint64_t getTypeSizeInBits(const Type *Ty) const;
452af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman
453af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// getEffectiveSCEVType - Return a type with the same bitwidth as
454af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// the given type and which represents how SCEV will treat the given
455af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// type, for which isSCEVable must return true. For pointer types,
456af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// this is the pointer-sized integer type.
457af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    const Type *getEffectiveSCEVType(const Type *Ty) const;
4582d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman
459e7125f4babb536df3a2573b6166954da7753c6b8Dan Gohman    /// getSCEV - Return a SCEV expression for the full generality of the
46053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// specified expression.
4610bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getSCEV(Value *V);
4620bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman
4630bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getConstant(ConstantInt *V);
4640bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getConstant(const APInt& Val);
4650bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getConstant(const Type *Ty, uint64_t V, bool isSigned = false);
4660bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getTruncateExpr(const SCEV *Op, const Type *Ty);
4670bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getZeroExtendExpr(const SCEV *Op, const Type *Ty);
4680bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getSignExtendExpr(const SCEV *Op, const Type *Ty);
4690bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getAnyExtendExpr(const SCEV *Op, const Type *Ty);
4703645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman    const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
4713645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                           bool HasNUW = false, bool HasNSW = false);
4723645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman    const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS,
4733645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                           bool HasNUW = false, bool HasNSW = false) {
4740bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      SmallVector<const SCEV *, 2> Ops;
475246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Ops.push_back(LHS);
476246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Ops.push_back(RHS);
4773645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman      return getAddExpr(Ops, HasNUW, HasNSW);
478246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    }
4790bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1,
4803645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                           const SCEV *Op2,
4813645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                           bool HasNUW = false, bool HasNSW = false) {
4820bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      SmallVector<const SCEV *, 3> Ops;
483246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Ops.push_back(Op0);
484246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Ops.push_back(Op1);
485246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Ops.push_back(Op2);
4863645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman      return getAddExpr(Ops, HasNUW, HasNSW);
487246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    }
4883645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman    const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
4893645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                           bool HasNUW = false, bool HasNSW = false);
4903645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman    const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS,
4913645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                           bool HasNUW = false, bool HasNSW = false) {
4920bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      SmallVector<const SCEV *, 2> Ops;
493246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Ops.push_back(LHS);
494246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Ops.push_back(RHS);
4953645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman      return getMulExpr(Ops, HasNUW, HasNSW);
496246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    }
4970bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS);
4980bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step,
4993645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                              const Loop *L,
5003645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                              bool HasNUW = false, bool HasNSW = false);
5010bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
5023645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                              const Loop *L,
5033645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                              bool HasNUW = false, bool HasNSW = false);
5040bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands,
5053645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                              const Loop *L,
5063645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman                              bool HasNUW = false, bool HasNSW = false) {
5070bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end());
5083645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman      return getAddRecExpr(NewOp, L, HasNUW, HasNSW);
509246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    }
5100bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS);
5110bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
5120bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS);
5130bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands);
5140bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS);
5150bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS);
5160bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getUnknown(Value *V);
5170bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getCouldNotCompute();
518246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman
5194f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    /// getSizeOfExpr - Return an expression for sizeof on the given type.
5204f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    ///
5214f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    const SCEV *getSizeOfExpr(const Type *AllocTy);
5224f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman
523ebdcbc26c77b97483afbcb906f988cc66d43606dDan Gohman    /// getAlignOfExpr - Return an expression for alignof on the given type.
5244f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    ///
5254f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    const SCEV *getAlignOfExpr(const Type *AllocTy);
5264f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman
527ebdcbc26c77b97483afbcb906f988cc66d43606dDan Gohman    /// getOffsetOfExpr - Return an expression for offsetof on the given field.
5284f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    ///
5294f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    const SCEV *getOffsetOfExpr(const StructType *STy, unsigned FieldNo);
5304f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman
531ebdcbc26c77b97483afbcb906f988cc66d43606dDan Gohman    /// getOffsetOfExpr - Return an expression for offsetof on the given field.
5324f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    ///
5334f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman    const SCEV *getOffsetOfExpr(const Type *CTy, Constant *FieldNo);
5344f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman
535246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    /// getNegativeSCEV - Return the SCEV object corresponding to -V.
536246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    ///
5370bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getNegativeSCEV(const SCEV *V);
538246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman
5393e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky    /// getNotSCEV - Return the SCEV object corresponding to ~V.
5403e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky    ///
5410bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getNotSCEV(const SCEV *V);
5423e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky
543246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    /// getMinusSCEV - Return LHS-RHS.
544246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    ///
5450bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getMinusSCEV(const SCEV *LHS,
546a271d36d7ce96bce52fcb73ca1ef028d9a9d8d0fDan Gohman                             const SCEV *RHS);
547246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman
5486f8abf929ac173872cc50aff767bc25f2a07a523Nick Lewycky    /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
5496f8abf929ac173872cc50aff767bc25f2a07a523Nick Lewycky    /// of the input value to the specified type.  If the type must be
5506f8abf929ac173872cc50aff767bc25f2a07a523Nick Lewycky    /// extended, it is zero extended.
5510bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getTruncateOrZeroExtend(const SCEV *V, const Type *Ty);
5526f8abf929ac173872cc50aff767bc25f2a07a523Nick Lewycky
553f5a309e989b8d2199cb542793e9edf48395d9fedDan Gohman    /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
554f5a309e989b8d2199cb542793e9edf48395d9fedDan Gohman    /// of the input value to the specified type.  If the type must be
555f5a309e989b8d2199cb542793e9edf48395d9fedDan Gohman    /// extended, it is sign extended.
5560bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getTruncateOrSignExtend(const SCEV *V, const Type *Ty);
557f5a309e989b8d2199cb542793e9edf48395d9fedDan Gohman
558467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of
559467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// the input value to the specified type.  If the type must be extended,
560467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// it is zero extended.  The conversion must not be narrowing.
5610bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getNoopOrZeroExtend(const SCEV *V, const Type *Ty);
562467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman
563467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of
564467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// the input value to the specified type.  If the type must be extended,
565467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// it is sign extended.  The conversion must not be narrowing.
5660bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getNoopOrSignExtend(const SCEV *V, const Type *Ty);
567467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman
5682ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman    /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
5692ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman    /// the input value to the specified type. If the type must be extended,
5702ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman    /// it is extended with unspecified bits. The conversion must not be
5712ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman    /// narrowing.
5720bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getNoopOrAnyExtend(const SCEV *V, const Type *Ty);
5732ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman
574467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
575467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// input value to the specified type.  The conversion must not be
576467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman    /// widening.
5770bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getTruncateOrNoop(const SCEV *V, const Type *Ty);
578467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman
579a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
580a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// the types using zero-extension, and then perform a umax operation
581a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman    /// with them.
5820bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS,
583a271d36d7ce96bce52fcb73ca1ef028d9a9d8d0fDan Gohman                                           const SCEV *RHS);
584a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman
585c9759e80f45e5690c3ed3b69c2e9ffd5a1bffd9cDan Gohman    /// getUMinFromMismatchedTypes - Promote the operands to the wider of
586c9759e80f45e5690c3ed3b69c2e9ffd5a1bffd9cDan Gohman    /// the types using zero-extension, and then perform a umin operation
587c9759e80f45e5690c3ed3b69c2e9ffd5a1bffd9cDan Gohman    /// with them.
5880bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS,
5890bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman                                           const SCEV *RHS);
590c9759e80f45e5690c3ed3b69c2e9ffd5a1bffd9cDan Gohman
591e7125f4babb536df3a2573b6166954da7753c6b8Dan Gohman    /// getSCEVAtScope - Return a SCEV expression for the specified value
59253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// at the specified scope in the program.  The L value specifies a loop
59353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// nest to evaluate the expression at, where null is the top-level or a
59453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// specified loop is immediately inside of the loop.
59553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    ///
59653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// This method can be used to compute the exit value for a variable defined
59753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    /// in a loop by querying what the value will hold in the parent loop.
59853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    ///
599d594e6f0345b3e1e4b640a7099596ca613da16d6Dan Gohman    /// In the case that a relevant loop exit value cannot be computed, the
600d594e6f0345b3e1e4b640a7099596ca613da16d6Dan Gohman    /// original value V is returned.
6010bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L);
60266a7e857aa5843da3a7d0f52aa09a5935cf565dcDan Gohman
60366a7e857aa5843da3a7d0f52aa09a5935cf565dcDan Gohman    /// getSCEVAtScope - This is a convenience function which does
60466a7e857aa5843da3a7d0f52aa09a5935cf565dcDan Gohman    /// getSCEVAtScope(getSCEV(V), L).
6050bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getSCEVAtScope(Value *V, const Loop *L);
60653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
6073948d0b8b0a71fabf25fceba1858b2b6a60d3d00Dan Gohman    /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected
6083948d0b8b0a71fabf25fceba1858b2b6a60d3d00Dan Gohman    /// by a conditional between LHS and RHS.  This is used to help avoid max
60985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// expressions in loop trip counts, and to eliminate casts.
6103948d0b8b0a71fabf25fceba1858b2b6a60d3d00Dan Gohman    bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
6113948d0b8b0a71fabf25fceba1858b2b6a60d3d00Dan Gohman                                  const SCEV *LHS, const SCEV *RHS);
612c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman
61385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
61485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// protected by a conditional between LHS and RHS.  This is used to
61585b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// to eliminate casts.
61685b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred,
61785b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman                                     const SCEV *LHS, const SCEV *RHS);
61885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman
61946bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// getBackedgeTakenCount - If the specified loop has a predictable
62046bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
62146bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// object. The backedge-taken count is the number of times the loop header
62246bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// will be branched to from within the loop. This is one less than the
62346bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// trip count of the loop, since it doesn't count the first iteration,
62446bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// when the header is branched to from outside the loop.
62546bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    ///
62646bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// Note that it is not valid to call this method on a loop without a
62746bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// loop-invariant backedge-taken count (see
62846bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// hasLoopInvariantBackedgeTakenCount).
62946bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    ///
6300bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getBackedgeTakenCount(const Loop *L);
63153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
632a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
633a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    /// return the least SCEV value that is known never to be less than the
634a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman    /// actual backedge taken count.
6350bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *getMaxBackedgeTakenCount(const Loop *L);
636a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman
63746bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop
63846bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    /// has an analyzable loop-invariant backedge-taken count.
639f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman    bool hasLoopInvariantBackedgeTakenCount(const Loop *L);
64053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
6414c7279ac726e338400626fca5a09b5533426eb6aDan Gohman    /// forgetLoop - This method should be called by the client when it has
6424c7279ac726e338400626fca5a09b5533426eb6aDan Gohman    /// changed a loop in a way that may effect ScalarEvolution's ability to
6434c7279ac726e338400626fca5a09b5533426eb6aDan Gohman    /// compute a trip count, or if the loop is deleted.
6444c7279ac726e338400626fca5a09b5533426eb6aDan Gohman    void forgetLoop(const Loop *L);
64560f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman
64645a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen    /// forgetValue - This method should be called by the client when it has
64745a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen    /// changed a value in a way that may effect its value, or which may
64845a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen    /// disconnect it from a def-use chain linking it to a loop.
64945a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen    void forgetValue(Value *V);
65045a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen
651650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman    /// GetMinTrailingZeros - Determine the minimum number of zero bits that S
652650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman    /// is guaranteed to end in (at every loop iteration).  It is, at the same
653650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman    /// time, the minimum number of times S is divisible by 2.  For example,
654650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman    /// given {4,+,8} it returns 2.  If S is guaranteed to be 0, it returns the
655650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman    /// bitwidth of S.
6560bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    uint32_t GetMinTrailingZeros(const SCEV *S);
6572c364ad4a65737f3bda876f86eba0061ecbd5470Dan Gohman
65885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
65985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ///
66085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ConstantRange getUnsignedRange(const SCEV *S);
66185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman
66285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// getSignedRange - Determine the signed range for a particular SCEV.
66385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ///
66485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ConstantRange getSignedRange(const SCEV *S);
66585b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman
66685b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// isKnownNegative - Test if the given expression is known to be negative.
66785b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ///
66885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    bool isKnownNegative(const SCEV *S);
66985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman
67085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// isKnownPositive - Test if the given expression is known to be positive.
67185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ///
67285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    bool isKnownPositive(const SCEV *S);
67385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman
67485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// isKnownNonNegative - Test if the given expression is known to be
67585b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// non-negative.
67685b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ///
67785b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    bool isKnownNonNegative(const SCEV *S);
67885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman
67985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// isKnownNonPositive - Test if the given expression is known to be
68085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// non-positive.
68185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ///
68285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    bool isKnownNonPositive(const SCEV *S);
68385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman
68485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// isKnownNonZero - Test if the given expression is known to be
68585b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// non-zero.
68685b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ///
68785b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    bool isKnownNonZero(const SCEV *S);
6882c364ad4a65737f3bda876f86eba0061ecbd5470Dan Gohman
689e48172710bf78ae210b2257d6ac3e0e8fb3ba792Dan Gohman    /// isKnownPredicate - Test if the given expression is known to satisfy
69085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    /// the condition described by Pred, LHS, and RHS.
69185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    ///
69285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman    bool isKnownPredicate(ICmpInst::Predicate Pred,
69385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman                          const SCEV *LHS, const SCEV *RHS);
6942c364ad4a65737f3bda876f86eba0061ecbd5470Dan Gohman
695e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman    /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with
6965cc6f9ba4777a460d7036edbbb3e8f01fb0a3d32Dan Gohman    /// predicate Pred. Return true iff any changes were made. If the
6975cc6f9ba4777a460d7036edbbb3e8f01fb0a3d32Dan Gohman    /// operands are provably equal or inequal, LHS and RHS are set to
6985cc6f9ba4777a460d7036edbbb3e8f01fb0a3d32Dan Gohman    /// the same value and Pred is set to either ICMP_EQ or ICMP_NE.
699e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman    ///
700e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman    bool SimplifyICmpOperands(ICmpInst::Predicate &Pred,
701e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman                              const SCEV *&LHS,
702e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman                              const SCEV *&RHS);
703e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman
70453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual bool runOnFunction(Function &F);
70553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual void releaseMemory();
70653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner    virtual void getAnalysisUsage(AnalysisUsage &AU) const;
70745cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner    virtual void print(raw_ostream &OS, const Module* = 0) const;
7081c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman
70908367b61638b4d446ebab69e2aad6431daa77ba7Owen Anderson  private:
7101c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman    FoldingSet<SCEV> UniqueSCEVs;
7111c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman    BumpPtrAllocator SCEVAllocator;
712ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman
713ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman    /// FirstUnknown - The head of a linked list of all SCEVUnknown
714ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman    /// values that have been allocated. This is used by releaseMemory
715ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman    /// to locate them all and call their destructors.
716ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman    SCEVUnknown *FirstUnknown;
71753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner  };
71853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner}
71953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner
72053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner#endif
721