ScalarEvolution.h revision 0b8c9a80f20772c3793201ab5b251d3520b9cea3
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 24255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/ADT/DenseSet.h" 25255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/ADT/FoldingSet.h" 260b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h" 270b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Instructions.h" 280b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Operator.h" 29255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/Pass.h" 301c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman#include "llvm/Support/Allocator.h" 3185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman#include "llvm/Support/ConstantRange.h" 32255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/Support/DataTypes.h" 33255f89faee13dc491cb64fbeae3c763e7e2ea4e6Chandler Carruth#include "llvm/Support/ValueHandle.h" 3403ee68a145ab5394c070298049d93f305be93ec3Dan Gohman#include <map> 3553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 3653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattnernamespace llvm { 37246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman class APInt; 3803ee68a145ab5394c070298049d93f305be93ec3Dan Gohman class Constant; 39246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman class ConstantInt; 4003ee68a145ab5394c070298049d93f305be93ec3Dan Gohman class DominatorTree; 4153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner class Type; 42246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman class ScalarEvolution; 433574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow class DataLayout; 44618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier class TargetLibraryInfo; 4512ddd409535b52a7fa5157ded9a4cedd161fedb6Benjamin Kramer class LLVMContext; 4603ee68a145ab5394c070298049d93f305be93ec3Dan Gohman class Loop; 4703ee68a145ab5394c070298049d93f305be93ec3Dan Gohman class LoopInfo; 48fc8deb971d2b4dff370ba93948975e33a038605dDan Gohman class Operator; 49dc7a235363166a61e81986c534fe11ceb44109fcDan Gohman class SCEVUnknown; 50aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman class SCEV; 51081ad68e687b24dd102ed890bae1e10d8d284cefDan Gohman template<> struct FoldingSetTrait<SCEV>; 5253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 536c0866ca8373da318741cc90ad7afd1bda22bb1bDan Gohman /// SCEV - This class represents an analyzed expression in the program. These 54650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman /// are opaque objects that the client is not allowed to do much with 55650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman /// directly. 5653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// 57c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman class SCEV : public FoldingSetNode { 58081ad68e687b24dd102ed890bae1e10d8d284cefDan Gohman friend struct FoldingSetTrait<SCEV>; 59aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman 60c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman /// FastID - A reference to an Interned FoldingSetNodeID for this node. 61c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman /// The ScalarEvolution's BumpPtrAllocator holds the data. 62c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman FoldingSetNodeIDRef FastID; 63c93b4cff89d85a13d4eaf1551af9fab276b88450Dan Gohman 642f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman // The SCEV baseclass this node corresponds to 652f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman const unsigned short SCEVType; 6653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 672f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman protected: 682f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman /// SubclassData - This field is initialized to zero and may be used in 693f46a3abeedba8d517b4182de34c821d752db058Dan Gohman /// subclasses to store miscellaneous information. 702f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman unsigned short SubclassData; 712f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman 722f1b15386f84a607304fca7e26fa6b67b490df4dDan Gohman private: 73de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper SCEV(const SCEV &) LLVM_DELETED_FUNCTION; 74de8091708f2d5ade958507aa6d37907a6277e9f2Craig Topper void operator=(const SCEV &) LLVM_DELETED_FUNCTION; 754ce32db913beb6ae27df2fa0a9db5d68fd4889d2Dan Gohman 7653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner public: 773228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// NoWrapFlags are bitfield indices into SubclassData. 783228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// 793228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// Add and Mul expressions may have no-unsigned-wrap <NUW> or 803228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// no-signed-wrap <NSW> properties, which are derived from the IR 813228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// operator. NSW is a misnomer that we use to mean no signed overflow or 823228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// underflow. 833228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// 843228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// AddRec expression may have a no-self-wraparound <NW> property if the 853228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// result can never reach the start value. This property is independent of 863228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// the actual start value and step direction. Self-wraparound is defined 873228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// purely in terms of the recurrence's loop, step size, and 883228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// bitwidth. Formally, a recurrence with no self-wraparound satisfies: 893228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// abs(step) * max-iteration(loop) <= unsigned-max(bitwidth). 903228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// 913228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// Note that NUW and NSW are also valid properties of a recurrence, and 923228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// either implies NW. For convenience, NW will be set for a recurrence 933228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// whenever either NUW or NSW are set. 943228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick enum NoWrapFlags { FlagAnyWrap = 0, // No guarantee. 953228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick FlagNW = (1 << 0), // No self-wrap. 963228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick FlagNUW = (1 << 1), // No unsigned wrap. 973228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick FlagNSW = (1 << 2), // No signed wrap. 983228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick NoWrapMask = (1 << 3) -1 }; 993228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick 1003bf63768e574a2065de1aebba12f6c2e80f4fb16Dan Gohman explicit SCEV(const FoldingSetNodeIDRef ID, unsigned SCEVTy) : 1013bf63768e574a2065de1aebba12f6c2e80f4fb16Dan Gohman FastID(ID), SCEVType(SCEVTy), SubclassData(0) {} 1021c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman 10353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner unsigned getSCEVType() const { return SCEVType; } 10453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 10553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// getType - Return the LLVM type of this SCEV expression. 10653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// 107db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *getType() const; 10853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 109cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman /// isZero - Return true if the expression is a constant zero. 110cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman /// 111cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman bool isZero() const; 112cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman 11370a1fe704831f9b842be0b2a2af5f7082b0e540cDan Gohman /// isOne - Return true if the expression is a constant one. 11470a1fe704831f9b842be0b2a2af5f7082b0e540cDan Gohman /// 11570a1fe704831f9b842be0b2a2af5f7082b0e540cDan Gohman bool isOne() const; 11670a1fe704831f9b842be0b2a2af5f7082b0e540cDan Gohman 1174d289bf4af88759be173a1a809bf8c092d729764Dan Gohman /// isAllOnesValue - Return true if the expression is a constant 1184d289bf4af88759be173a1a809bf8c092d729764Dan Gohman /// all-ones value. 1194d289bf4af88759be173a1a809bf8c092d729764Dan Gohman /// 1204d289bf4af88759be173a1a809bf8c092d729764Dan Gohman bool isAllOnesValue() const; 1214d289bf4af88759be173a1a809bf8c092d729764Dan Gohman 122f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick /// isNonConstantNegative - Return true if the specified scev is negated, 123f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick /// but not a constant. 124f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick bool isNonConstantNegative() const; 125f8fd841a4bb7a59f81cf4642169e8251e039acfeAndrew Trick 12653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// print - Print out the internal representation of this scalar to the 12753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// specified stream. This should really only be used for debugging 12853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// purposes. 1294ce32db913beb6ae27df2fa0a9db5d68fd4889d2Dan Gohman void print(raw_ostream &OS) const; 13053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 13153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// dump - This method is used for debugging. 13253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// 13353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner void dump() const; 13453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner }; 1359769ab22265b313171d201b5928688524a01bd87Misha Brukman 136aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman // Specialize FoldingSetTrait for SCEV to avoid needing to compute 137aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman // temporary FoldingSetNodeID values. 138aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman template<> struct FoldingSetTrait<SCEV> : DefaultFoldingSetTrait<SCEV> { 139aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman static void Profile(const SCEV &X, FoldingSetNodeID& ID) { 140aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman ID = X.FastID; 141aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman } 142aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman static bool Equals(const SCEV &X, const FoldingSetNodeID &ID, 143f7c3e5f05199e1202c86198e0827cac19c0f48b5Benjamin Kramer unsigned IDHash, FoldingSetNodeID &TempID) { 144aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman return ID == X.FastID; 145aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman } 146aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman static unsigned ComputeHash(const SCEV &X, FoldingSetNodeID &TempID) { 147aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman return X.FastID.ComputeHash(); 148aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman } 149aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman }; 150aadb5f5adbdb650c5cdc5ece939aaa30af91b3bcDan Gohman 151b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) { 152b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman S.print(OS); 153b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman return OS; 154b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman } 155b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman 15653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// SCEVCouldNotCompute - An object of this class is returned by queries that 15753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// could not be answered. For example, if you ask for the number of 15853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// iterations of a linked-list traversal loop, you will get one of these. 15953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// None of the standard SCEV operations are valid on this class, it is just a 16053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// marker. 16153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner struct SCEVCouldNotCompute : public SCEV { 162753ad615f96c3d56d6f17983bdba88012e88677cOwen Anderson SCEVCouldNotCompute(); 16353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 16453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 16553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner static bool classof(const SCEV *S); 16653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner }; 16753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 16853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// ScalarEvolution - This class is the main scalar evolution driver. Because 16953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// client code (intentionally) can't do much with the SCEV objects directly, 17053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// they must ask this class for services. 17153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// 17253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner class ScalarEvolution : public FunctionPass { 173714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman public: 174714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman /// LoopDisposition - An enum describing the relationship between a 175714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman /// SCEV and a loop. 176714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman enum LoopDisposition { 177714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman LoopVariant, ///< The SCEV is loop-variant (unknown). 178714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman LoopInvariant, ///< The SCEV is loop-invariant. 179714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman LoopComputable ///< The SCEV varies predictably with the loop. 180714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman }; 181714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman 1829c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman /// BlockDisposition - An enum describing the relationship between a 1839c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman /// SCEV and a basic block. 1849c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman enum BlockDisposition { 1859c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman DoesNotDominateBlock, ///< The SCEV does not dominate the block. 1869c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman DominatesBlock, ///< The SCEV dominates the block. 1879c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman ProperlyDominatesBlock ///< The SCEV properly dominates the block. 1889c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman }; 1899c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman 1903228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// Convenient NoWrapFlags manipulation that hides enum casts and is 1913228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// visible in the ScalarEvolution name space. 1923228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags, int Mask) { 1933228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick return (SCEV::NoWrapFlags)(Flags & Mask); 1943228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick } 1953228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags, 1963228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SCEV::NoWrapFlags OnFlags) { 1973228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick return (SCEV::NoWrapFlags)(Flags | OnFlags); 1983228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick } 1993228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick static SCEV::NoWrapFlags clearFlags(SCEV::NoWrapFlags Flags, 2003228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SCEV::NoWrapFlags OffFlags) { 2013228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick return (SCEV::NoWrapFlags)(Flags & ~OffFlags); 2023228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick } 2033228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick 204714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman private: 2051959b7562e57f8394496e761486f23b187ac3f1bDan Gohman /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be 2061959b7562e57f8394496e761486f23b187ac3f1bDan Gohman /// notified whenever a Value is deleted. 2071959b7562e57f8394496e761486f23b187ac3f1bDan Gohman class SCEVCallbackVH : public CallbackVH { 2081959b7562e57f8394496e761486f23b187ac3f1bDan Gohman ScalarEvolution *SE; 2091959b7562e57f8394496e761486f23b187ac3f1bDan Gohman virtual void deleted(); 2101959b7562e57f8394496e761486f23b187ac3f1bDan Gohman virtual void allUsesReplacedWith(Value *New); 2111959b7562e57f8394496e761486f23b187ac3f1bDan Gohman public: 2121959b7562e57f8394496e761486f23b187ac3f1bDan Gohman SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0); 2131959b7562e57f8394496e761486f23b187ac3f1bDan Gohman }; 2141959b7562e57f8394496e761486f23b187ac3f1bDan Gohman 21535738ac150afafe2359268d4b2169498c6c98c5fDan Gohman friend class SCEVCallbackVH; 216629bff692ae96dea517dc854e27b57ee6c8729efBenjamin Kramer friend class SCEVExpander; 217dc7a235363166a61e81986c534fe11ceb44109fcDan Gohman friend class SCEVUnknown; 21835738ac150afafe2359268d4b2169498c6c98c5fDan Gohman 219f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// F - The function we are analyzing. 220f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// 221f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman Function *F; 222f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 223f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// LI - The loop information for the function we are currently analyzing. 224f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// 225f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman LoopInfo *LI; 226f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 2273f46a3abeedba8d517b4182de34c821d752db058Dan Gohman /// TD - The target data information for the target we are targeting. 228f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// 2293574eca1b02600bac4e625297f4ecf745f4c4f32Micah Villmow DataLayout *TD; 230f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 231618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier /// TLI - The target library information for the target we are targeting. 232618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier /// 233618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier TargetLibraryInfo *TLI; 234618c1dbd293d15ee19f61b1156ab8086ad28311aChad Rosier 2351cd9275c8d612bd1c92fc7ba436b60aaead1efbfDan Gohman /// DT - The dominator tree. 2361cd9275c8d612bd1c92fc7ba436b60aaead1efbfDan Gohman /// 2371cd9275c8d612bd1c92fc7ba436b60aaead1efbfDan Gohman DominatorTree *DT; 2381cd9275c8d612bd1c92fc7ba436b60aaead1efbfDan Gohman 23986fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman /// CouldNotCompute - This SCEV is used to represent unknown trip 24086fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman /// counts and things. 2411c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman SCEVCouldNotCompute CouldNotCompute; 242f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 243e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman /// ValueExprMapType - The typedef for ValueExprMap. 244f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// 245e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman typedef DenseMap<SCEVCallbackVH, const SCEV *, DenseMapInfo<Value *> > 246e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman ValueExprMapType; 247e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman 248e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman /// ValueExprMap - This is a cache of the values we have analyzed so far. 249e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman /// 250e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman ValueExprMapType ValueExprMap; 251f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 2528aa22019ca5ef29a15058be905d782e7225aa206Andrew Trick /// Mark predicate values currently being processed by isImpliedCond. 2538aa22019ca5ef29a15058be905d782e7225aa206Andrew Trick DenseSet<Value*> PendingLoopPredicates; 2548aa22019ca5ef29a15058be905d782e7225aa206Andrew Trick 2555116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// ExitLimit - Information about the number of loop iterations for 2565116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// which a loop exit's branch condition evaluates to the not-taken path. 2575116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// This is a temporary pair of exact and max expressions that are 2585116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// eventually summarized in ExitNotTakenInfo and BackedgeTakenInfo. 2595116ff671f45d48594d11360e22991a7edb13862Andrew Trick struct ExitLimit { 2605116ff671f45d48594d11360e22991a7edb13862Andrew Trick const SCEV *Exact; 2615116ff671f45d48594d11360e22991a7edb13862Andrew Trick const SCEV *Max; 2625116ff671f45d48594d11360e22991a7edb13862Andrew Trick 2635116ff671f45d48594d11360e22991a7edb13862Andrew Trick /*implicit*/ ExitLimit(const SCEV *E) : Exact(E), Max(E) {} 2645116ff671f45d48594d11360e22991a7edb13862Andrew Trick 2655116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitLimit(const SCEV *E, const SCEV *M) : Exact(E), Max(M) {} 2665116ff671f45d48594d11360e22991a7edb13862Andrew Trick 2675116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// hasAnyInfo - Test whether this ExitLimit contains any computed 2685116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// information, or whether it's all SCEVCouldNotCompute values. 2695116ff671f45d48594d11360e22991a7edb13862Andrew Trick bool hasAnyInfo() const { 2705116ff671f45d48594d11360e22991a7edb13862Andrew Trick return !isa<SCEVCouldNotCompute>(Exact) || 2715116ff671f45d48594d11360e22991a7edb13862Andrew Trick !isa<SCEVCouldNotCompute>(Max); 2725116ff671f45d48594d11360e22991a7edb13862Andrew Trick } 2735116ff671f45d48594d11360e22991a7edb13862Andrew Trick }; 2745116ff671f45d48594d11360e22991a7edb13862Andrew Trick 2755116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// ExitNotTakenInfo - Information about the number of times a particular 2765116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// loop exit may be reached before exiting the loop. 2775116ff671f45d48594d11360e22991a7edb13862Andrew Trick struct ExitNotTakenInfo { 2781009c3299be8c147ecd3fbd2d75ba1bafb2c84b1Andrew Trick AssertingVH<BasicBlock> ExitingBlock; 2795116ff671f45d48594d11360e22991a7edb13862Andrew Trick const SCEV *ExactNotTaken; 2805116ff671f45d48594d11360e22991a7edb13862Andrew Trick PointerIntPair<ExitNotTakenInfo*, 1> NextExit; 2815116ff671f45d48594d11360e22991a7edb13862Andrew Trick 282fcb4356dee96563def584fe38eeafb3eb63c5cd8Andrew Trick ExitNotTakenInfo() : ExitingBlock(0), ExactNotTaken(0) {} 2835116ff671f45d48594d11360e22991a7edb13862Andrew Trick 2845116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// isCompleteList - Return true if all loop exits are computable. 2855116ff671f45d48594d11360e22991a7edb13862Andrew Trick bool isCompleteList() const { 2865116ff671f45d48594d11360e22991a7edb13862Andrew Trick return NextExit.getInt() == 0; 2875116ff671f45d48594d11360e22991a7edb13862Andrew Trick } 2885116ff671f45d48594d11360e22991a7edb13862Andrew Trick 2895116ff671f45d48594d11360e22991a7edb13862Andrew Trick void setIncomplete() { NextExit.setInt(1); } 2905116ff671f45d48594d11360e22991a7edb13862Andrew Trick 2915116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// getNextExit - Return a pointer to the next exit's not-taken info. 2925116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitNotTakenInfo *getNextExit() const { 2935116ff671f45d48594d11360e22991a7edb13862Andrew Trick return NextExit.getPointer(); 2945116ff671f45d48594d11360e22991a7edb13862Andrew Trick } 2955116ff671f45d48594d11360e22991a7edb13862Andrew Trick 2965116ff671f45d48594d11360e22991a7edb13862Andrew Trick void setNextExit(ExitNotTakenInfo *ENT) { NextExit.setPointer(ENT); } 2975116ff671f45d48594d11360e22991a7edb13862Andrew Trick }; 2985116ff671f45d48594d11360e22991a7edb13862Andrew Trick 299a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// BackedgeTakenInfo - Information about the backedge-taken count 3003f46a3abeedba8d517b4182de34c821d752db058Dan Gohman /// of a loop. This currently includes an exact count and a maximum count. 301a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// 3025116ff671f45d48594d11360e22991a7edb13862Andrew Trick class BackedgeTakenInfo { 3035116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// ExitNotTaken - A list of computable exits and their not-taken counts. 3045116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// Loops almost never have more than one computable exit. 3055116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitNotTakenInfo ExitNotTaken; 306a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman 3077b9547089f0363b803e55dcbde1d6b99710dfe69Andreas Bolka /// Max - An expression indicating the least maximum backedge-taken 308a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// count of the loop that is known, or a SCEVCouldNotCompute. 3090bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *Max; 310a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman 3115116ff671f45d48594d11360e22991a7edb13862Andrew Trick public: 3125116ff671f45d48594d11360e22991a7edb13862Andrew Trick BackedgeTakenInfo() : Max(0) {} 313a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman 3145116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// Initialize BackedgeTakenInfo from a list of exact exit counts. 3155116ff671f45d48594d11360e22991a7edb13862Andrew Trick BackedgeTakenInfo( 3165116ff671f45d48594d11360e22991a7edb13862Andrew Trick SmallVectorImpl< std::pair<BasicBlock *, const SCEV *> > &ExitCounts, 3175116ff671f45d48594d11360e22991a7edb13862Andrew Trick bool Complete, const SCEV *MaxCount); 318a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman 319a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any 320a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// computed information, or whether it's all SCEVCouldNotCompute 321a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// values. 322a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman bool hasAnyInfo() const { 323fcb4356dee96563def584fe38eeafb3eb63c5cd8Andrew Trick return ExitNotTaken.ExitingBlock || !isa<SCEVCouldNotCompute>(Max); 324a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman } 3255116ff671f45d48594d11360e22991a7edb13862Andrew Trick 3265116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// getExact - Return an expression indicating the exact backedge-taken 3275116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// count of the loop if it is known, or SCEVCouldNotCompute 3285116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// otherwise. This is the number of times the loop header can be 3295116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// guaranteed to execute, minus one. 3305116ff671f45d48594d11360e22991a7edb13862Andrew Trick const SCEV *getExact(ScalarEvolution *SE) const; 3315116ff671f45d48594d11360e22991a7edb13862Andrew Trick 3325116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// getExact - Return the number of times this loop exit may fall through 333252ef7a61a9455c1e5d7b8a5a5f7ec8b3a75e200Andrew Trick /// to the back edge, or SCEVCouldNotCompute. The loop is guaranteed not 334252ef7a61a9455c1e5d7b8a5a5f7ec8b3a75e200Andrew Trick /// to exit via this block before this number of iterations, but may exit 335252ef7a61a9455c1e5d7b8a5a5f7ec8b3a75e200Andrew Trick /// via another block. 336fcb4356dee96563def584fe38eeafb3eb63c5cd8Andrew Trick const SCEV *getExact(BasicBlock *ExitingBlock, ScalarEvolution *SE) const; 3375116ff671f45d48594d11360e22991a7edb13862Andrew Trick 3385116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// getMax - Get the max backedge taken count for the loop. 3395116ff671f45d48594d11360e22991a7edb13862Andrew Trick const SCEV *getMax(ScalarEvolution *SE) const; 3405116ff671f45d48594d11360e22991a7edb13862Andrew Trick 3415116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// clear - Invalidate this result and free associated memory. 3425116ff671f45d48594d11360e22991a7edb13862Andrew Trick void clear(); 343a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman }; 344a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman 345f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for 346f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// this function as they are computed. 34777a2c4c1e54b7e3c4815b276eb6a2d99a7621460Dan Gohman DenseMap<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts; 348f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 349f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// ConstantEvolutionLoopExitValue - This map contains entries for all of 350f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// the PHI instructions that we attempt to compute constant evolutions for. 351f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// This allows us to avoid potentially expensive recomputation of these 352f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// properties. An instruction maps to null if we are unable to compute its 353f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// exit value. 35477a2c4c1e54b7e3c4815b276eb6a2d99a7621460Dan Gohman DenseMap<PHINode*, Constant*> ConstantEvolutionLoopExitValue; 355f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 35642214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman /// ValuesAtScopes - This map contains entries for all the expressions 35742214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman /// that we attempt to compute getSCEVAtScope information for, which can 35842214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman /// be expensive in extreme cases. 35977a2c4c1e54b7e3c4815b276eb6a2d99a7621460Dan Gohman DenseMap<const SCEV *, 36042214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman std::map<const Loop *, const SCEV *> > ValuesAtScopes; 3616bce643c36e7263aada5058f08cd242b4ce6b87dDan Gohman 362714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman /// LoopDispositions - Memoized computeLoopDisposition results. 36377a2c4c1e54b7e3c4815b276eb6a2d99a7621460Dan Gohman DenseMap<const SCEV *, 364714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman std::map<const Loop *, LoopDisposition> > LoopDispositions; 365714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman 366714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman /// computeLoopDisposition - Compute a LoopDisposition value. 367714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman LoopDisposition computeLoopDisposition(const SCEV *S, const Loop *L); 368714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman 3699c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman /// BlockDispositions - Memoized computeBlockDisposition results. 37077a2c4c1e54b7e3c4815b276eb6a2d99a7621460Dan Gohman DenseMap<const SCEV *, 3719c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman std::map<const BasicBlock *, BlockDisposition> > BlockDispositions; 3729c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman 3739c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman /// computeBlockDisposition - Compute a BlockDisposition value. 3749c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman BlockDisposition computeBlockDisposition(const SCEV *S, const BasicBlock *BB); 3759c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman 3766678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman /// UnsignedRanges - Memoized results from getUnsignedRange 3776678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman DenseMap<const SCEV *, ConstantRange> UnsignedRanges; 3786678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman 3796678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman /// SignedRanges - Memoized results from getSignedRange 3806678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman DenseMap<const SCEV *, ConstantRange> SignedRanges; 3816678e7b6eb534b43b92105076e6d0553e5cf7defDan Gohman 3827c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman /// setUnsignedRange - Set the memoized unsigned range for the given SCEV. 3837c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman const ConstantRange &setUnsignedRange(const SCEV *S, 3847c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman const ConstantRange &CR) { 3857c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman std::pair<DenseMap<const SCEV *, ConstantRange>::iterator, bool> Pair = 3867c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman UnsignedRanges.insert(std::make_pair(S, CR)); 3877c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman if (!Pair.second) 3887c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman Pair.first->second = CR; 3897c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman return Pair.first->second; 3907c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman } 3917c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman 3927c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman /// setUnsignedRange - Set the memoized signed range for the given SCEV. 3937c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman const ConstantRange &setSignedRange(const SCEV *S, 3947c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman const ConstantRange &CR) { 3957c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman std::pair<DenseMap<const SCEV *, ConstantRange>::iterator, bool> Pair = 3967c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman SignedRanges.insert(std::make_pair(S, CR)); 3977c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman if (!Pair.second) 3987c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman Pair.first->second = CR; 3997c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman return Pair.first->second; 4007c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman } 4017c0fd8eb724a7228a6cf7e3e5487614c25202a91Dan Gohman 402f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// createSCEV - We know that there is no SCEV for the specified value. 403f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// Analyze the expression. 4040bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *createSCEV(Value *V); 405f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 406f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// createNodeForPHI - Provide the special handling we need to analyze PHI 407f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// SCEVs. 4080bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *createNodeForPHI(PHINode *PN); 409f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 41026466c0eb3451c5c953b3cca8940359152c4f8e3Dan Gohman /// createNodeForGEP - Provide the special handling we need to analyze GEP 41126466c0eb3451c5c953b3cca8940359152c4f8e3Dan Gohman /// SCEVs. 412d281ed2d03654b9cdb290a2d7c73dfe7b826e554Dan Gohman const SCEV *createNodeForGEP(GEPOperator *GEP); 41326466c0eb3451c5c953b3cca8940359152c4f8e3Dan Gohman 41442214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman /// computeSCEVAtScope - Implementation code for getSCEVAtScope; called 41542214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman /// at most once for each SCEV+Loop pair. 41642214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman /// 41742214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman const SCEV *computeSCEVAtScope(const SCEV *S, const Loop *L); 41842214899082bfb5b6f8c6a09d355fec9ef4a0e82Dan Gohman 419fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman /// ForgetSymbolicValue - This looks up computed SCEV values for all 420fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman /// instructions that depend on the given instruction and removes them from 421e8ac3f3be4b4df619368bf46aa4ed91557b5f76eDan Gohman /// the ValueExprMap map if they reference SymName. This is used during PHI 422fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman /// resolution. 423fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman void ForgetSymbolicName(Instruction *I, const SCEV *SymName); 424f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 42551f53b7f5a0e859ceef995c61667905166b96f1bDan Gohman /// getBECount - Subtract the end and start values and divide by the step, 42651f53b7f5a0e859ceef995c61667905166b96f1bDan Gohman /// rounding up, to get the number of times the backedge is executed. Return 42751f53b7f5a0e859ceef995c61667905166b96f1bDan Gohman /// CouldNotCompute if an intermediate computation overflows. 4280bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getBECount(const SCEV *Start, 429a271d36d7ce96bce52fcb73ca1ef028d9a9d8d0fDan Gohman const SCEV *End, 4301f96e67f78110d0ac5b30a32375097a28f869c63Dan Gohman const SCEV *Step, 4311f96e67f78110d0ac5b30a32375097a28f869c63Dan Gohman bool NoWrap); 43251f53b7f5a0e859ceef995c61667905166b96f1bDan Gohman 433a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given 434a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// loop, lazily computing new values if the loop hasn't been analyzed 435a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// yet. 436a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L); 437a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman 438f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// ComputeBackedgeTakenCount - Compute the number of times the specified 439f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// loop will iterate. 440a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L); 441f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 4425116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// ComputeExitLimit - Compute the number of times the backedge of the 4435116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// specified loop will execute if it exits via the specified block. 4445116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitLimit ComputeExitLimit(const Loop *L, BasicBlock *ExitingBlock); 4455116ff671f45d48594d11360e22991a7edb13862Andrew Trick 4465116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// ComputeExitLimitFromCond - Compute the number of times the backedge of 4475116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// the specified loop will execute if its exit condition were a conditional 4485116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// branch of ExitCond, TBB, and FBB. 4495116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitLimit ComputeExitLimitFromCond(const Loop *L, 4505116ff671f45d48594d11360e22991a7edb13862Andrew Trick Value *ExitCond, 4515116ff671f45d48594d11360e22991a7edb13862Andrew Trick BasicBlock *TBB, 4525116ff671f45d48594d11360e22991a7edb13862Andrew Trick BasicBlock *FBB); 4535116ff671f45d48594d11360e22991a7edb13862Andrew Trick 4545116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// ComputeExitLimitFromICmp - Compute the number of times the backedge of 4555116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// the specified loop will execute if its exit condition were a conditional 4565116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// branch of the ICmpInst ExitCond, TBB, and FBB. 4575116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitLimit ComputeExitLimitFromICmp(const Loop *L, 4585116ff671f45d48594d11360e22991a7edb13862Andrew Trick ICmpInst *ExitCond, 4595116ff671f45d48594d11360e22991a7edb13862Andrew Trick BasicBlock *TBB, 4605116ff671f45d48594d11360e22991a7edb13862Andrew Trick BasicBlock *FBB); 4615116ff671f45d48594d11360e22991a7edb13862Andrew Trick 4625116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// ComputeLoadConstantCompareExitLimit - Given an exit condition 4637e77f7959162a601291fd5400a88908d021033d3Dan Gohman /// of 'icmp op load X, cst', try to see if we can compute the 4647e77f7959162a601291fd5400a88908d021033d3Dan Gohman /// backedge-taken count. 4655116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitLimit ComputeLoadConstantCompareExitLimit(LoadInst *LI, 4665116ff671f45d48594d11360e22991a7edb13862Andrew Trick Constant *RHS, 4675116ff671f45d48594d11360e22991a7edb13862Andrew Trick const Loop *L, 4685116ff671f45d48594d11360e22991a7edb13862Andrew Trick ICmpInst::Predicate p); 4695116ff671f45d48594d11360e22991a7edb13862Andrew Trick 4705116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// ComputeExitCountExhaustively - If the loop is known to execute a 4715116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// constant number of times (the condition evolves only from constants), 472f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// try to evaluate a few iterations of the loop until we get the exit 473f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// condition gets a value of ExitWhen (true or false). If we cannot 4745116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// evaluate the exit count of the loop, return CouldNotCompute. 4755116ff671f45d48594d11360e22991a7edb13862Andrew Trick const SCEV *ComputeExitCountExhaustively(const Loop *L, 4765116ff671f45d48594d11360e22991a7edb13862Andrew Trick Value *Cond, 4775116ff671f45d48594d11360e22991a7edb13862Andrew Trick bool ExitWhen); 478f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 4795116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// HowFarToZero - Return the number of times an exit condition comparing 4805116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// the specified value to zero will execute. If not computable, return 48186fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman /// CouldNotCompute. 4825116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitLimit HowFarToZero(const SCEV *V, const Loop *L); 483f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 4845116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// HowFarToNonZero - Return the number of times an exit condition checking 4855116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// the specified value for nonzero will execute. If not computable, return 48686fbf2fe4cae21febffa4bb2f64cd0c2ee834694Dan Gohman /// CouldNotCompute. 4875116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitLimit HowFarToNonZero(const SCEV *V, const Loop *L); 488f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 4895116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// HowManyLessThans - Return the number of times an exit condition 4905116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// containing the specified less-than comparison will execute. If not 4915116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// computable, return CouldNotCompute. isSigned specifies whether the 4925116ff671f45d48594d11360e22991a7edb13862Andrew Trick /// less-than is signed. 4935116ff671f45d48594d11360e22991a7edb13862Andrew Trick ExitLimit HowManyLessThans(const SCEV *LHS, const SCEV *RHS, 4945116ff671f45d48594d11360e22991a7edb13862Andrew Trick const Loop *L, bool isSigned); 495f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 496f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB 497f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// (which may not be an immediate predecessor) which has exactly one 498f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// successor from which BB is reachable, or null if no such block is 499f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// found. 500005752bbe72868b548bba93dbc1ea46ffe8e5b2cDan Gohman std::pair<BasicBlock *, BasicBlock *> 501005752bbe72868b548bba93dbc1ea46ffe8e5b2cDan Gohman getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB); 502f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 503af08a36bd6b9d32a5ea993849d43094fecbd5bedDan Gohman /// isImpliedCond - Test whether the condition described by Pred, LHS, and 504af08a36bd6b9d32a5ea993849d43094fecbd5bedDan Gohman /// RHS is true whenever the given FoundCondValue value evaluates to true. 505af08a36bd6b9d32a5ea993849d43094fecbd5bedDan Gohman bool isImpliedCond(ICmpInst::Predicate Pred, 5060f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman const SCEV *LHS, const SCEV *RHS, 507af08a36bd6b9d32a5ea993849d43094fecbd5bedDan Gohman Value *FoundCondValue, 5080f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman bool Inverse); 5090f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman 5100f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman /// isImpliedCondOperands - Test whether the condition described by Pred, 5113f46a3abeedba8d517b4182de34c821d752db058Dan Gohman /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS, 5120f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman /// and FoundRHS is true. 5130f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman bool isImpliedCondOperands(ICmpInst::Predicate Pred, 5140f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman const SCEV *LHS, const SCEV *RHS, 5150f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman const SCEV *FoundLHS, const SCEV *FoundRHS); 5160f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman 5170f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman /// isImpliedCondOperandsHelper - Test whether the condition described by 5183f46a3abeedba8d517b4182de34c821d752db058Dan Gohman /// Pred, LHS, and RHS is true whenever the condition described by Pred, 5190f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman /// FoundLHS, and FoundRHS is true. 5200f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman bool isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, 5210f4b285a5b86b1e9c6e27bb46028dfbb77bb5db4Dan Gohman const SCEV *LHS, const SCEV *RHS, 522b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick const SCEV *FoundLHS, 523b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick const SCEV *FoundRHS); 52485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman 525f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is 526f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// in the header of its containing loop, we know the loop executes a 527f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// constant number of times, and the PHI node is just a recurrence 528f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman /// involving constants, fold it. 529f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, 530f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman const Loop *L); 531f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman 53253c66eacc417c0113fba7159487b90005dc8f91eDan Gohman /// isKnownPredicateWithRanges - Test if the given expression is known to 53353c66eacc417c0113fba7159487b90005dc8f91eDan Gohman /// satisfy the condition described by Pred and the known constant ranges 53453c66eacc417c0113fba7159487b90005dc8f91eDan Gohman /// of LHS and RHS. 53553c66eacc417c0113fba7159487b90005dc8f91eDan Gohman /// 53653c66eacc417c0113fba7159487b90005dc8f91eDan Gohman bool isKnownPredicateWithRanges(ICmpInst::Predicate Pred, 53753c66eacc417c0113fba7159487b90005dc8f91eDan Gohman const SCEV *LHS, const SCEV *RHS); 53853c66eacc417c0113fba7159487b90005dc8f91eDan Gohman 53956a756821842678a96f2baa8c6a53bd28fc2b69eDan Gohman /// forgetMemoizedResults - Drop memoized information computed for S. 54056a756821842678a96f2baa8c6a53bd28fc2b69eDan Gohman void forgetMemoizedResults(const SCEV *S); 54156a756821842678a96f2baa8c6a53bd28fc2b69eDan Gohman 54253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner public: 543ecd94c804a563f2a86572dcf1d2e81f397e19daaNick Lewycky static char ID; // Pass identification, replacement for typeid 544f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman ScalarEvolution(); 5459769ab22265b313171d201b5928688524a01bd87Misha Brukman 546e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson LLVMContext &getContext() const { return F->getContext(); } 547508955156a25a9abc470a29e1760aa176d341cf9Owen Anderson 548af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// isSCEVable - Test if values of the given type are analyzable within 549af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// the SCEV framework. This primarily includes integer types, and it 550af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// can optionally include pointer types if the ScalarEvolution class 551af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// has access to target-specific information. 552db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner bool isSCEVable(Type *Ty) const; 553af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman 554af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// getTypeSizeInBits - Return the size in bits of the specified type, 555af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// for which isSCEVable must return true. 556db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner uint64_t getTypeSizeInBits(Type *Ty) const; 557af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman 558af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// getEffectiveSCEVType - Return a type with the same bitwidth as 559af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// the given type and which represents how SCEV will treat the given 560af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// type, for which isSCEVable must return true. For pointer types, 561af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// this is the pointer-sized integer type. 562db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner Type *getEffectiveSCEVType(Type *Ty) const; 5632d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman 564e7125f4babb536df3a2573b6166954da7753c6b8Dan Gohman /// getSCEV - Return a SCEV expression for the full generality of the 56553e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// specified expression. 5660bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getSCEV(Value *V); 5670bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman 5680bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getConstant(ConstantInt *V); 5690bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getConstant(const APInt& Val); 570db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false); 571db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty); 572db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getZeroExtendExpr(const SCEV *Op, Type *Ty); 573db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getSignExtendExpr(const SCEV *Op, Type *Ty); 574db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getAnyExtendExpr(const SCEV *Op, Type *Ty); 5753645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops, 5763228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap); 5773645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS, 5783228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) { 5790bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman SmallVector<const SCEV *, 2> Ops; 580246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman Ops.push_back(LHS); 581246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman Ops.push_back(RHS); 5823228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick return getAddExpr(Ops, Flags); 583246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman } 5843228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2, 5853228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) { 5860bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman SmallVector<const SCEV *, 3> Ops; 587246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman Ops.push_back(Op0); 588246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman Ops.push_back(Op1); 589246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman Ops.push_back(Op2); 5903228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick return getAddExpr(Ops, Flags); 591246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman } 5923645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops, 5933228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap); 5943645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS, 5953228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) 5963228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick { 5970bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman SmallVector<const SCEV *, 2> Ops; 598246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman Ops.push_back(LHS); 599246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman Ops.push_back(RHS); 6003228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick return getMulExpr(Ops, Flags); 601246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman } 602e97728ecf8a0ee69562cc0e7880cfaa65200c624Nick Lewycky const SCEV *getMulExpr(const SCEV *Op0, const SCEV *Op1, const SCEV *Op2, 603e97728ecf8a0ee69562cc0e7880cfaa65200c624Nick Lewycky SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap) { 604e97728ecf8a0ee69562cc0e7880cfaa65200c624Nick Lewycky SmallVector<const SCEV *, 3> Ops; 605e97728ecf8a0ee69562cc0e7880cfaa65200c624Nick Lewycky Ops.push_back(Op0); 606e97728ecf8a0ee69562cc0e7880cfaa65200c624Nick Lewycky Ops.push_back(Op1); 607e97728ecf8a0ee69562cc0e7880cfaa65200c624Nick Lewycky Ops.push_back(Op2); 608e97728ecf8a0ee69562cc0e7880cfaa65200c624Nick Lewycky return getMulExpr(Ops, Flags); 609e97728ecf8a0ee69562cc0e7880cfaa65200c624Nick Lewycky } 6100bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS); 6110bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step, 6123228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick const Loop *L, SCEV::NoWrapFlags Flags); 6130bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, 6143228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick const Loop *L, SCEV::NoWrapFlags Flags); 6150bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands, 6163228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick const Loop *L, SCEV::NoWrapFlags Flags) { 6170bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end()); 6183228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick return getAddRecExpr(NewOp, L, Flags); 619246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman } 6200bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS); 6210bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands); 6220bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS); 6230bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands); 6240bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS); 6250bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS); 6260bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getUnknown(Value *V); 6270bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getCouldNotCompute(); 628246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 6294f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman /// getSizeOfExpr - Return an expression for sizeof on the given type. 6304f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman /// 631ece6c6bb6329748b92403c06ac87f45c43485911Chandler Carruth const SCEV *getSizeOfExpr(Type *AllocTy); 6324f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 633ebdcbc26c77b97483afbcb906f988cc66d43606dDan Gohman /// getAlignOfExpr - Return an expression for alignof on the given type. 6344f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman /// 635db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getAlignOfExpr(Type *AllocTy); 6364f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 637ebdcbc26c77b97483afbcb906f988cc66d43606dDan Gohman /// getOffsetOfExpr - Return an expression for offsetof on the given field. 6384f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman /// 639ece6c6bb6329748b92403c06ac87f45c43485911Chandler Carruth const SCEV *getOffsetOfExpr(StructType *STy, unsigned FieldNo); 6404f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 641ebdcbc26c77b97483afbcb906f988cc66d43606dDan Gohman /// getOffsetOfExpr - Return an expression for offsetof on the given field. 6424f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman /// 643db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getOffsetOfExpr(Type *CTy, Constant *FieldNo); 6444f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman 645246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman /// getNegativeSCEV - Return the SCEV object corresponding to -V. 646246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman /// 6470bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getNegativeSCEV(const SCEV *V); 648246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 6493e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky /// getNotSCEV - Return the SCEV object corresponding to ~V. 6503e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky /// 6510bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getNotSCEV(const SCEV *V); 6523e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky 6533228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick /// getMinusSCEV - Return LHS-RHS. Minus is represented in SCEV as A+B*-1. 654992efb03785f2a368fbb63b09373be1d6a96ce5aChris Lattner const SCEV *getMinusSCEV(const SCEV *LHS, const SCEV *RHS, 6553228cc259b5ca00e46af36da369a451f5736cbf4Andrew Trick SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap); 656246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 6576f8abf929ac173872cc50aff767bc25f2a07a523Nick Lewycky /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion 6586f8abf929ac173872cc50aff767bc25f2a07a523Nick Lewycky /// of the input value to the specified type. If the type must be 6596f8abf929ac173872cc50aff767bc25f2a07a523Nick Lewycky /// extended, it is zero extended. 660db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getTruncateOrZeroExtend(const SCEV *V, Type *Ty); 6616f8abf929ac173872cc50aff767bc25f2a07a523Nick Lewycky 662f5a309e989b8d2199cb542793e9edf48395d9fedDan Gohman /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion 663f5a309e989b8d2199cb542793e9edf48395d9fedDan Gohman /// of the input value to the specified type. If the type must be 664f5a309e989b8d2199cb542793e9edf48395d9fedDan Gohman /// extended, it is sign extended. 665db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty); 666f5a309e989b8d2199cb542793e9edf48395d9fedDan Gohman 667467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of 668467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// the input value to the specified type. If the type must be extended, 669467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// it is zero extended. The conversion must not be narrowing. 670db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getNoopOrZeroExtend(const SCEV *V, Type *Ty); 671467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman 672467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of 673467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// the input value to the specified type. If the type must be extended, 674467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// it is sign extended. The conversion must not be narrowing. 675db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getNoopOrSignExtend(const SCEV *V, Type *Ty); 676467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman 6772ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of 6782ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman /// the input value to the specified type. If the type must be extended, 6792ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman /// it is extended with unspecified bits. The conversion must not be 6802ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman /// narrowing. 681db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getNoopOrAnyExtend(const SCEV *V, Type *Ty); 6822ce84c8d4784dfd24458a63db8d531917d2f8ba5Dan Gohman 683467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the 684467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// input value to the specified type. The conversion must not be 685467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman /// widening. 686db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner const SCEV *getTruncateOrNoop(const SCEV *V, Type *Ty); 687467c430316b7a5b6fa8069531ca8d603b1e1197fDan Gohman 688a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman /// getUMaxFromMismatchedTypes - Promote the operands to the wider of 689a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman /// the types using zero-extension, and then perform a umax operation 690a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman /// with them. 6910bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS, 692a271d36d7ce96bce52fcb73ca1ef028d9a9d8d0fDan Gohman const SCEV *RHS); 693a334aa7a106d5ebb971862f25daaadad48d96235Dan Gohman 694c9759e80f45e5690c3ed3b69c2e9ffd5a1bffd9cDan Gohman /// getUMinFromMismatchedTypes - Promote the operands to the wider of 695c9759e80f45e5690c3ed3b69c2e9ffd5a1bffd9cDan Gohman /// the types using zero-extension, and then perform a umin operation 696c9759e80f45e5690c3ed3b69c2e9ffd5a1bffd9cDan Gohman /// with them. 6970bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS, 6980bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *RHS); 699c9759e80f45e5690c3ed3b69c2e9ffd5a1bffd9cDan Gohman 700b12a754cce0c1d5542af605203a47820edba454dAndrew Trick /// getPointerBase - Transitively follow the chain of pointer-type operands 701b12a754cce0c1d5542af605203a47820edba454dAndrew Trick /// until reaching a SCEV that does not have a single pointer operand. This 702b12a754cce0c1d5542af605203a47820edba454dAndrew Trick /// returns a SCEVUnknown pointer for well-formed pointer-type expressions, 703b12a754cce0c1d5542af605203a47820edba454dAndrew Trick /// but corner cases do exist. 704b12a754cce0c1d5542af605203a47820edba454dAndrew Trick const SCEV *getPointerBase(const SCEV *V); 705b12a754cce0c1d5542af605203a47820edba454dAndrew Trick 706e7125f4babb536df3a2573b6166954da7753c6b8Dan Gohman /// getSCEVAtScope - Return a SCEV expression for the specified value 70753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// at the specified scope in the program. The L value specifies a loop 70853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// nest to evaluate the expression at, where null is the top-level or a 70953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// specified loop is immediately inside of the loop. 71053e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// 71153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// This method can be used to compute the exit value for a variable defined 71253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// in a loop by querying what the value will hold in the parent loop. 71353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner /// 714d594e6f0345b3e1e4b640a7099596ca613da16d6Dan Gohman /// In the case that a relevant loop exit value cannot be computed, the 715d594e6f0345b3e1e4b640a7099596ca613da16d6Dan Gohman /// original value V is returned. 7160bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L); 71766a7e857aa5843da3a7d0f52aa09a5935cf565dcDan Gohman 71866a7e857aa5843da3a7d0f52aa09a5935cf565dcDan Gohman /// getSCEVAtScope - This is a convenience function which does 71966a7e857aa5843da3a7d0f52aa09a5935cf565dcDan Gohman /// getSCEVAtScope(getSCEV(V), L). 7200bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getSCEVAtScope(Value *V, const Loop *L); 72153e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 7223948d0b8b0a71fabf25fceba1858b2b6a60d3d00Dan Gohman /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected 7233948d0b8b0a71fabf25fceba1858b2b6a60d3d00Dan Gohman /// by a conditional between LHS and RHS. This is used to help avoid max 72485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// expressions in loop trip counts, and to eliminate casts. 7253948d0b8b0a71fabf25fceba1858b2b6a60d3d00Dan Gohman bool isLoopEntryGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 7263948d0b8b0a71fabf25fceba1858b2b6a60d3d00Dan Gohman const SCEV *LHS, const SCEV *RHS); 727c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman 72885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is 72985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// protected by a conditional between LHS and RHS. This is used to 73085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// to eliminate casts. 73185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 73285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman const SCEV *LHS, const SCEV *RHS); 73385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman 734b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick /// getSmallConstantTripCount - Returns the maximum trip count of this loop 7353eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick /// as a normal unsigned value. Returns 0 if the trip count is unknown or 7363eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick /// not constant. This "trip count" assumes that control exits via 7373eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick /// ExitingBlock. More precisely, it is the number of times that control may 7383eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick /// reach ExitingBlock before taking the branch. For loops with multiple 7393eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick /// exits, it may not be the number times that the loop header executes if 7403eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick /// the loop exits prematurely via another branch. 7413eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick unsigned getSmallConstantTripCount(Loop *L, BasicBlock *ExitingBlock); 742b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick 743b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick /// getSmallConstantTripMultiple - Returns the largest constant divisor of 744b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick /// the trip count of this loop as a normal unsigned value, if 745b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick /// possible. This means that the actual trip count is always a multiple of 746b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick /// the returned value (don't forget the trip count could very well be zero 7473eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick /// as well!). As explained in the comments for getSmallConstantTripCount, 7483eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick /// this assumes that control exits the loop via ExitingBlock. 7493eada319fc21bca31a2bc7819e4c05f46e6f82e1Andrew Trick unsigned getSmallConstantTripMultiple(Loop *L, BasicBlock *ExitingBlock); 750b1831c66403315a1d84593b7c198ddbd43a574cfAndrew Trick 7515116ff671f45d48594d11360e22991a7edb13862Andrew Trick // getExitCount - Get the expression for the number of loop iterations for 752fcb4356dee96563def584fe38eeafb3eb63c5cd8Andrew Trick // which this loop is guaranteed not to exit via ExitingBlock. Otherwise 753fcb4356dee96563def584fe38eeafb3eb63c5cd8Andrew Trick // return SCEVCouldNotCompute. 754fcb4356dee96563def584fe38eeafb3eb63c5cd8Andrew Trick const SCEV *getExitCount(Loop *L, BasicBlock *ExitingBlock); 7555116ff671f45d48594d11360e22991a7edb13862Andrew Trick 75646bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// getBackedgeTakenCount - If the specified loop has a predictable 75746bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute 75846bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// object. The backedge-taken count is the number of times the loop header 75946bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// will be branched to from within the loop. This is one less than the 76046bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// trip count of the loop, since it doesn't count the first iteration, 76146bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// when the header is branched to from outside the loop. 76246bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// 76346bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// Note that it is not valid to call this method on a loop without a 76446bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// loop-invariant backedge-taken count (see 76546bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// hasLoopInvariantBackedgeTakenCount). 76646bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// 7670bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getBackedgeTakenCount(const Loop *L); 76853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 769a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except 770a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// return the least SCEV value that is known never to be less than the 771a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman /// actual backedge taken count. 7720bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getMaxBackedgeTakenCount(const Loop *L); 773a1af757e0af9c2fb5ade4b06408e1adfa0425c6cDan Gohman 77446bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop 77546bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman /// has an analyzable loop-invariant backedge-taken count. 776f8a8be86e3972608741c1e1ecb89ec3c6f570552Dan Gohman bool hasLoopInvariantBackedgeTakenCount(const Loop *L); 77753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 7784c7279ac726e338400626fca5a09b5533426eb6aDan Gohman /// forgetLoop - This method should be called by the client when it has 7794c7279ac726e338400626fca5a09b5533426eb6aDan Gohman /// changed a loop in a way that may effect ScalarEvolution's ability to 7804c7279ac726e338400626fca5a09b5533426eb6aDan Gohman /// compute a trip count, or if the loop is deleted. 7814c7279ac726e338400626fca5a09b5533426eb6aDan Gohman void forgetLoop(const Loop *L); 78260f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman 78345a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen /// forgetValue - This method should be called by the client when it has 78445a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen /// changed a value in a way that may effect its value, or which may 78545a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen /// disconnect it from a def-use chain linking it to a loop. 78645a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen void forgetValue(Value *V); 78745a2d7d44ae697e28df383d31455145fb754ac58Dale Johannesen 788650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman /// GetMinTrailingZeros - Determine the minimum number of zero bits that S 789650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman /// is guaranteed to end in (at every loop iteration). It is, at the same 790650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman /// time, the minimum number of times S is divisible by 2. For example, 791650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman /// given {4,+,8} it returns 2. If S is guaranteed to be 0, it returns the 792650919e8b0aa28d20b8ff11f42ba81fea8b336ccDan Gohman /// bitwidth of S. 7930bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman uint32_t GetMinTrailingZeros(const SCEV *S); 7942c364ad4a65737f3bda876f86eba0061ecbd5470Dan Gohman 79585b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// getUnsignedRange - Determine the unsigned range for a particular SCEV. 79685b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// 79785b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman ConstantRange getUnsignedRange(const SCEV *S); 79885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman 79985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// getSignedRange - Determine the signed range for a particular SCEV. 80085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// 80185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman ConstantRange getSignedRange(const SCEV *S); 80285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman 80385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// isKnownNegative - Test if the given expression is known to be negative. 80485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// 80585b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman bool isKnownNegative(const SCEV *S); 80685b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman 80785b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// isKnownPositive - Test if the given expression is known to be positive. 80885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// 80985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman bool isKnownPositive(const SCEV *S); 81085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman 81185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// isKnownNonNegative - Test if the given expression is known to be 81285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// non-negative. 81385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// 81485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman bool isKnownNonNegative(const SCEV *S); 81585b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman 81685b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// isKnownNonPositive - Test if the given expression is known to be 81785b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// non-positive. 81885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// 81985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman bool isKnownNonPositive(const SCEV *S); 82085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman 82185b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// isKnownNonZero - Test if the given expression is known to be 82285b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// non-zero. 82385b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// 82485b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman bool isKnownNonZero(const SCEV *S); 8252c364ad4a65737f3bda876f86eba0061ecbd5470Dan Gohman 826e48172710bf78ae210b2257d6ac3e0e8fb3ba792Dan Gohman /// isKnownPredicate - Test if the given expression is known to satisfy 82785b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// the condition described by Pred, LHS, and RHS. 82885b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman /// 82985b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman bool isKnownPredicate(ICmpInst::Predicate Pred, 83085b05a2e60e0e696739167b52cc7cc3e7cf390c0Dan Gohman const SCEV *LHS, const SCEV *RHS); 8312c364ad4a65737f3bda876f86eba0061ecbd5470Dan Gohman 832e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with 83394c22716d60ff5edf6a98a3c67e0faa001be1142Sylvestre Ledru /// predicate Pred. Return true iff any changes were made. If the 8345cc6f9ba4777a460d7036edbbb3e8f01fb0a3d32Dan Gohman /// operands are provably equal or inequal, LHS and RHS are set to 8355cc6f9ba4777a460d7036edbbb3e8f01fb0a3d32Dan Gohman /// the same value and Pred is set to either ICMP_EQ or ICMP_NE. 836e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman /// 837e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman bool SimplifyICmpOperands(ICmpInst::Predicate &Pred, 838e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman const SCEV *&LHS, 8396cf07a80ff5ee8ef7dc336f954aae17c7e8d83d4Benjamin Kramer const SCEV *&RHS, 8406cf07a80ff5ee8ef7dc336f954aae17c7e8d83d4Benjamin Kramer unsigned Depth = 0); 841e979650fa5faf4b1253f623662a8d9c81a4a4fc3Dan Gohman 842714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman /// getLoopDisposition - Return the "disposition" of the given SCEV with 843714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman /// respect to the given loop. 844714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L); 845714b5290b04e08570dae4304c1c92d30c06d3c99Dan Gohman 84617ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman /// isLoopInvariant - Return true if the value of the given SCEV is 84717ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman /// unchanging in the specified loop. 84817ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman bool isLoopInvariant(const SCEV *S, const Loop *L); 84917ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman 85017ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman /// hasComputableLoopEvolution - Return true if the given SCEV changes value 85117ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman /// in a known way in the specified loop. This property being true implies 85217ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman /// that the value is variant in the loop AND that we can emit an expression 85317ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman /// to compute the value of the expression at any particular loop iteration. 85417ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman bool hasComputableLoopEvolution(const SCEV *S, const Loop *L); 85517ead4ff4baceb2c5503f233d0288d363ae44165Dan Gohman 8569c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman /// getLoopDisposition - Return the "disposition" of the given SCEV with 8579c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman /// respect to the given block. 8589c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB); 8599c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman 860dc0e8fb9f9512622f55f73e1a434caa5c0915694Dan Gohman /// dominates - Return true if elements that makes up the given SCEV 861dc0e8fb9f9512622f55f73e1a434caa5c0915694Dan Gohman /// dominate the specified basic block. 8629c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman bool dominates(const SCEV *S, const BasicBlock *BB); 863dc0e8fb9f9512622f55f73e1a434caa5c0915694Dan Gohman 864dc0e8fb9f9512622f55f73e1a434caa5c0915694Dan Gohman /// properlyDominates - Return true if elements that makes up the given SCEV 865dc0e8fb9f9512622f55f73e1a434caa5c0915694Dan Gohman /// properly dominate the specified basic block. 8669c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman bool properlyDominates(const SCEV *S, const BasicBlock *BB); 867dc0e8fb9f9512622f55f73e1a434caa5c0915694Dan Gohman 8684ce32db913beb6ae27df2fa0a9db5d68fd4889d2Dan Gohman /// hasOperand - Test whether the given SCEV has Op as a direct or 8694ce32db913beb6ae27df2fa0a9db5d68fd4889d2Dan Gohman /// indirect operand. 8704ce32db913beb6ae27df2fa0a9db5d68fd4889d2Dan Gohman bool hasOperand(const SCEV *S, const SCEV *Op) const; 8714ce32db913beb6ae27df2fa0a9db5d68fd4889d2Dan Gohman 87253e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner virtual bool runOnFunction(Function &F); 87353e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner virtual void releaseMemory(); 87453e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner virtual void getAnalysisUsage(AnalysisUsage &AU) const; 87545cfe545ec8177262dabc70580ce05feaa1c3880Chris Lattner virtual void print(raw_ostream &OS, const Module* = 0) const; 876ff18310274e872429cd06d679b1c8c8a14166328Benjamin Kramer virtual void verifyAnalysis() const; 8771c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman 87808367b61638b4d446ebab69e2aad6431daa77ba7Owen Anderson private: 8791c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman FoldingSet<SCEV> UniqueSCEVs; 8801c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman BumpPtrAllocator SCEVAllocator; 881ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman 882ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman /// FirstUnknown - The head of a linked list of all SCEVUnknown 883ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman /// values that have been allocated. This is used by releaseMemory 884ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman /// to locate them all and call their destructors. 885ab37f50838350e1104579fbd1f7c8820473485a5Dan Gohman SCEVUnknown *FirstUnknown; 88653e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner }; 88753e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner} 88853e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner 88953e677abadadf71ef33f2f69533a32c1fa3d168fChris Lattner#endif 890