ScalarEvolution.h revision 85b05a2e60e0e696739167b52cc7cc3e7cf390c0
1ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown//===- llvm/Analysis/ScalarEvolution.h - Scalar Evolution -------*- C++ -*-===// 2ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 3ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// The LLVM Compiler Infrastructure 4ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 5ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// This file is distributed under the University of Illinois Open Source 6ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// License. See LICENSE.TXT for details. 7ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 8ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown//===----------------------------------------------------------------------===// 9663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng// 10663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng// The ScalarEvolution class is an LLVM pass which can be used to analyze and 11663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng// catagorize scalar expressions in loops. It specializes in recognizing 12ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// general induction variables, representing them with the abstract and opaque 13ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// SCEV class. Given this analysis, trip counts of loops and other important 14ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// properties can be obtained. 15ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 16ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// This analysis is primarily useful for induction variable substitution and 17ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// strength reduction. 18ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown// 19ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown//===----------------------------------------------------------------------===// 20ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 21ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_H 22ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#define LLVM_ANALYSIS_SCALAREVOLUTION_H 23ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 24ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/Pass.h" 25ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/Analysis/LoopInfo.h" 26ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/Support/DataTypes.h" 27436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "llvm/Support/ValueHandle.h" 28ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/Support/Allocator.h" 29ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/Support/ConstantRange.h" 30ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown#include "llvm/ADT/FoldingSet.h" 31436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include "llvm/ADT/DenseMap.h" 32436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov#include <iosfwd> 33ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 34ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brownnamespace llvm { 35ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class APInt; 36ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class ConstantInt; 37ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class Type; 38ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class ScalarEvolution; 39ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class TargetData; 40ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class LLVMContext; 41ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 42ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// SCEV - This class represents an analyzed expression in the program. These 43ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// are opaque objects that the client is not allowed to do much with 44ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// directly. 45ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 46ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class SCEV : public FastFoldingSetNode { 47ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const unsigned SCEVType; // The SCEV baseclass this node corresponds to 48ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 49ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown SCEV(const SCEV &); // DO NOT IMPLEMENT 50ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown void operator=(const SCEV &); // DO NOT IMPLEMENT 51663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng protected: 52663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng virtual ~SCEV(); 53ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown public: 54ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown explicit SCEV(const FoldingSetNodeID &ID, unsigned SCEVTy) : 55ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown FastFoldingSetNode(ID), SCEVType(SCEVTy) {} 56ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 57ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown unsigned getSCEVType() const { return SCEVType; } 58ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 59ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// isLoopInvariant - Return true if the value of this SCEV is unchanging in 60ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// the specified loop. 61663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng virtual bool isLoopInvariant(const Loop *L) const = 0; 62663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng 63663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng /// hasComputableLoopEvolution - Return true if this SCEV changes value in a 64663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng /// known way in the specified loop. This property being true implies that 65663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng /// the value is variant in the loop AND that we can emit an expression to 66436e89c602e787e7a27dd6624b09beed41a0da8aDmitriy Ivanov /// compute the value of the expression at any particular loop iteration. 67663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng virtual bool hasComputableLoopEvolution(const Loop *L) const = 0; 68ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 69ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// getType - Return the LLVM type of this SCEV expression. 70ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 71ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual const Type *getType() const = 0; 72ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 73ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// isZero - Return true if the expression is a constant zero. 74ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 75ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown bool isZero() const; 76ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 77ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// isOne - Return true if the expression is a constant one. 78ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 79ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown bool isOne() const; 80ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 81ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// isAllOnesValue - Return true if the expression is a constant 82ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// all-ones value. 83ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 84ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown bool isAllOnesValue() const; 85ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 86b32f58018498ea2225959b0ba11c18f0c433deefEvgeniy Stepanov /// replaceSymbolicValuesWithConcrete - If this SCEV internally references 87ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// the symbolic value "Sym", construct and return a new SCEV that produces 88ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// the same value, but which uses the concrete value Conc instead of the 89ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// symbolic value. If this SCEV does not use the symbolic value, it 90ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// returns itself. 91ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual const SCEV * 92ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown replaceSymbolicValuesWithConcrete(const SCEV *Sym, 93ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *Conc, 94ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ScalarEvolution &SE) const = 0; 95ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 96ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// dominates - Return true if elements that makes up this SCEV dominates 97ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// the specified basic block. 98ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const = 0; 99ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 100ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// print - Print out the internal representation of this scalar to the 101ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// specified stream. This should really only be used for debugging 102ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// purposes. 103ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual void print(raw_ostream &OS) const = 0; 104ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown void print(std::ostream &OS) const; 105ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown void print(std::ostream *OS) const { if (OS) print(*OS); } 106ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 107ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// dump - This method is used for debugging. 108ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 109ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown void dump() const; 110ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown }; 111ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 112ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown inline raw_ostream &operator<<(raw_ostream &OS, const SCEV &S) { 113ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown S.print(OS); 114ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown return OS; 115ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown } 116ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 117ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown inline std::ostream &operator<<(std::ostream &OS, const SCEV &S) { 118ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown S.print(OS); 119ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown return OS; 120ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown } 121ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 122ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// SCEVCouldNotCompute - An object of this class is returned by queries that 123ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// could not be answered. For example, if you ask for the number of 124ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// iterations of a linked-list traversal loop, you will get one of these. 125ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// None of the standard SCEV operations are valid on this class, it is just a 126ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// marker. 127ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown struct SCEVCouldNotCompute : public SCEV { 128ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown SCEVCouldNotCompute(); 129ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 130ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown // None of these methods are valid for this object. 131ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual bool isLoopInvariant(const Loop *L) const; 132ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual const Type *getType() const; 133ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual bool hasComputableLoopEvolution(const Loop *L) const; 134ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual void print(raw_ostream &OS) const; 135ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual const SCEV * 136ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown replaceSymbolicValuesWithConcrete(const SCEV *Sym, 137ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *Conc, 138ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ScalarEvolution &SE) const; 139ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 140ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const { 141ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown return true; 142ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown } 143ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 144ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// Methods for support type inquiry through isa, cast, and dyn_cast: 145ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown static inline bool classof(const SCEVCouldNotCompute *S) { return true; } 146ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown static bool classof(const SCEV *S); 147ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown }; 148ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 149ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ScalarEvolution - This class is the main scalar evolution driver. Because 150ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// client code (intentionally) can't do much with the SCEV objects directly, 151ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// they must ask this class for services. 152ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 153ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class ScalarEvolution : public FunctionPass { 154ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// SCEVCallbackVH - A CallbackVH to arrange for ScalarEvolution to be 155ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// notified whenever a Value is deleted. 156ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown class SCEVCallbackVH : public CallbackVH { 157ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ScalarEvolution *SE; 158ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual void deleted(); 159ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown virtual void allUsesReplacedWith(Value *New); 160ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown public: 161ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown SCEVCallbackVH(Value *V, ScalarEvolution *SE = 0); 162ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown }; 163ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 164ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown friend class SCEVCallbackVH; 165ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown friend struct SCEVExpander; 166ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 167ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// F - The function we are analyzing. 168ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 169ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown Function *F; 170ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 171ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// LI - The loop information for the function we are currently analyzing. 172ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 173ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown LoopInfo *LI; 174ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 175ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// TD - The target data information for the target we are targetting. 176ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 177ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown TargetData *TD; 178ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 179ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// CouldNotCompute - This SCEV is used to represent unknown trip 180ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// counts and things. 181ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown SCEVCouldNotCompute CouldNotCompute; 182ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 183ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// Scalars - This is a cache of the scalars we have analyzed so far. 184ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 185ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown std::map<SCEVCallbackVH, const SCEV *> Scalars; 186ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 187ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// BackedgeTakenInfo - Information about the backedge-taken count 188ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// of a loop. This currently inclues an exact count and a maximum count. 189ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// 190ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown struct BackedgeTakenInfo { 191ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// Exact - An expression indicating the exact backedge-taken count of 192ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// the loop if it is known, or a SCEVCouldNotCompute otherwise. 193ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *Exact; 194ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 195ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// Max - An expression indicating the least maximum backedge-taken 196ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// count of the loop that is known, or a SCEVCouldNotCompute. 197ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *Max; 198ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 199ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /*implicit*/ BackedgeTakenInfo(const SCEV *exact) : 200ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown Exact(exact), Max(exact) {} 201ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 202ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BackedgeTakenInfo(const SCEV *exact, const SCEV *max) : 203ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown Exact(exact), Max(max) {} 204ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 205ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// hasAnyInfo - Test whether this BackedgeTakenInfo contains any 206ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// computed information, or whether it's all SCEVCouldNotCompute 207ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// values. 208ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown bool hasAnyInfo() const { 209ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown return !isa<SCEVCouldNotCompute>(Exact) || 210ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown !isa<SCEVCouldNotCompute>(Max); 211ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown } 212ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown }; 213ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 214ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// BackedgeTakenCounts - Cache the backedge-taken count of the loops for 215ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// this function as they are computed. 216ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown std::map<const Loop*, BackedgeTakenInfo> BackedgeTakenCounts; 217ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 218ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ConstantEvolutionLoopExitValue - This map contains entries for all of 219ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// the PHI instructions that we attempt to compute constant evolutions for. 220ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// This allows us to avoid potentially expensive recomputation of these 221ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// properties. An instruction maps to null if we are unable to compute its 222ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// exit value. 223ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue; 224ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 225ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ValuesAtScopes - This map contains entries for all the instructions 226ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// that we attempt to compute getSCEVAtScope information for without 227ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// using SCEV techniques, which can be expensive. 228ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown std::map<Instruction *, std::map<const Loop *, Constant *> > ValuesAtScopes; 229ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 230ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// createSCEV - We know that there is no SCEV for the specified value. 231ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// Analyze the expression. 232ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *createSCEV(Value *V); 233ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 234ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// createNodeForPHI - Provide the special handling we need to analyze PHI 235ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// SCEVs. 236ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *createNodeForPHI(PHINode *PN); 237ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 238ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// createNodeForGEP - Provide the special handling we need to analyze GEP 239ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// SCEVs. 240ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *createNodeForGEP(User *GEP); 241ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 242ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value 243ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// for the specified instruction and replaces any references to the 244ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// symbolic value SymName with the specified value. This is used during 245ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// PHI resolution. 246ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown void ReplaceSymbolicValueWithConcrete(Instruction *I, 247ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *SymName, 248ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *NewVal); 249ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 250ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// getBECount - Subtract the end and start values and divide by the step, 251ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// rounding up, to get the number of times the backedge is executed. Return 252ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// CouldNotCompute if an intermediate computation overflows. 253ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *getBECount(const SCEV *Start, 254ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *End, 255ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *Step); 256ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 257ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given 258ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// loop, lazily computing new values if the loop hasn't been analyzed 259ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// yet. 260ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const BackedgeTakenInfo &getBackedgeTakenInfo(const Loop *L); 261ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 262ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ComputeBackedgeTakenCount - Compute the number of times the specified 263ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// loop will iterate. 264ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BackedgeTakenInfo ComputeBackedgeTakenCount(const Loop *L); 265ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 266ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ComputeBackedgeTakenCountFromExit - Compute the number of times the 267ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// backedge of the specified loop will execute if it exits via the 268ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// specified block. 269ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BackedgeTakenInfo ComputeBackedgeTakenCountFromExit(const Loop *L, 270ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BasicBlock *ExitingBlock); 271ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 272ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the 273ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// backedge of the specified loop will execute if its exit condition 274ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// were a conditional branch of ExitCond, TBB, and FBB. 275ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BackedgeTakenInfo 276ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ComputeBackedgeTakenCountFromExitCond(const Loop *L, 277ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown Value *ExitCond, 278ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BasicBlock *TBB, 279ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BasicBlock *FBB); 280ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 281ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of 282ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// times the backedge of the specified loop will execute if its exit 283ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// condition were a conditional branch of the ICmpInst ExitCond, TBB, 284ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// and FBB. 285ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BackedgeTakenInfo 286ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, 287ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ICmpInst *ExitCond, 288ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BasicBlock *TBB, 289ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BasicBlock *FBB); 290ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 291ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition 292ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// of 'icmp op load X, cst', try to see if we can compute the trip count. 293ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV * 294ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ComputeLoadConstantCompareBackedgeTakenCount(LoadInst *LI, 295ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown Constant *RHS, 296ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const Loop *L, 297ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ICmpInst::Predicate p); 298ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 299ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute 300ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// a constant number of times (the condition evolves only from constants), 301ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// try to evaluate a few iterations of the loop until we get the exit 302ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// condition gets a value of ExitWhen (true or false). If we cannot 303ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// evaluate the trip count of the loop, return CouldNotCompute. 304ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *ComputeBackedgeTakenCountExhaustively(const Loop *L, 305ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown Value *Cond, 306ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown bool ExitWhen); 307ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 308ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// HowFarToZero - Return the number of times a backedge comparing the 309ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// specified value to zero will execute. If not computable, return 310ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// CouldNotCompute. 311ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *HowFarToZero(const SCEV *V, const Loop *L); 312ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 313ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// HowFarToNonZero - Return the number of times a backedge checking the 314ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// specified value for nonzero will execute. If not computable, return 315ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// CouldNotCompute. 316ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *HowFarToNonZero(const SCEV *V, const Loop *L); 317ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 318ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// HowManyLessThans - Return the number of times a backedge containing the 319ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// specified less-than comparison will execute. If not computable, return 320ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// CouldNotCompute. isSigned specifies whether the less-than is signed. 321ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BackedgeTakenInfo HowManyLessThans(const SCEV *LHS, const SCEV *RHS, 322ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const Loop *L, bool isSigned); 323ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 324ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// getLoopPredecessor - If the given loop's header has exactly one unique 325ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// predecessor outside the loop, return it. Otherwise return null. 326ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BasicBlock *getLoopPredecessor(const Loop *L); 327ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 328ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB 329ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// (which may not be an immediate predecessor) which has exactly one 330ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// successor from which BB is reachable, or null if no such block is 331ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// found. 332ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown BasicBlock* getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB); 333ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 334ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// isNecessaryCond - Test whether the condition described by Pred, LHS, 335ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// and RHS is a necessary condition for the given Cond value to evaluate 336ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// to true. 337ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown bool isNecessaryCond(Value *Cond, ICmpInst::Predicate Pred, 338ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *LHS, const SCEV *RHS, 339ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown bool Inverse); 340ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 341ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// isNecessaryCondOperands - Test whether the condition described by Pred, 342ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// LHS, and RHS is a necessary condition for the condition described by 343ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// Pred, FoundLHS, and FoundRHS to evaluate to true. 344ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown bool isNecessaryCondOperands(ICmpInst::Predicate Pred, 345ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *LHS, const SCEV *RHS, 346ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const SCEV *FoundLHS, const SCEV *FoundRHS); 347ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 348663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is 349ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// in the header of its containing loop, we know the loop executes a 350ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// constant number of times, and the PHI node is just a recurrence 351ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown /// involving constants, fold it. 352663860b1408516d02ebfcb3a9999a134e6cfb223Ben Cheng Constant *getConstantEvolutionLoopExitValue(PHINode *PN, const APInt& BEs, 353ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown const Loop *L); 354ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown 355ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown public: 356ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown static char ID; // Pass identification, replacement for typeid 357ed07e00d438c74b7a23c01bfffde77e3968305e4Jeff Brown ScalarEvolution(); 358 359 LLVMContext *getContext() const { return Context; } 360 361 /// isSCEVable - Test if values of the given type are analyzable within 362 /// the SCEV framework. This primarily includes integer types, and it 363 /// can optionally include pointer types if the ScalarEvolution class 364 /// has access to target-specific information. 365 bool isSCEVable(const Type *Ty) const; 366 367 /// getTypeSizeInBits - Return the size in bits of the specified type, 368 /// for which isSCEVable must return true. 369 uint64_t getTypeSizeInBits(const Type *Ty) const; 370 371 /// getEffectiveSCEVType - Return a type with the same bitwidth as 372 /// the given type and which represents how SCEV will treat the given 373 /// type, for which isSCEVable must return true. For pointer types, 374 /// this is the pointer-sized integer type. 375 const Type *getEffectiveSCEVType(const Type *Ty) const; 376 377 /// getSCEV - Return a SCEV expression handle for the full generality of the 378 /// specified expression. 379 const SCEV *getSCEV(Value *V); 380 381 const SCEV *getConstant(ConstantInt *V); 382 const SCEV *getConstant(const APInt& Val); 383 const SCEV *getConstant(const Type *Ty, uint64_t V, bool isSigned = false); 384 const SCEV *getTruncateExpr(const SCEV *Op, const Type *Ty); 385 const SCEV *getZeroExtendExpr(const SCEV *Op, const Type *Ty); 386 const SCEV *getSignExtendExpr(const SCEV *Op, const Type *Ty); 387 const SCEV *getAnyExtendExpr(const SCEV *Op, const Type *Ty); 388 const SCEV *getAddExpr(SmallVectorImpl<const SCEV *> &Ops); 389 const SCEV *getAddExpr(const SCEV *LHS, const SCEV *RHS) { 390 SmallVector<const SCEV *, 2> Ops; 391 Ops.push_back(LHS); 392 Ops.push_back(RHS); 393 return getAddExpr(Ops); 394 } 395 const SCEV *getAddExpr(const SCEV *Op0, const SCEV *Op1, 396 const SCEV *Op2) { 397 SmallVector<const SCEV *, 3> Ops; 398 Ops.push_back(Op0); 399 Ops.push_back(Op1); 400 Ops.push_back(Op2); 401 return getAddExpr(Ops); 402 } 403 const SCEV *getMulExpr(SmallVectorImpl<const SCEV *> &Ops); 404 const SCEV *getMulExpr(const SCEV *LHS, const SCEV *RHS) { 405 SmallVector<const SCEV *, 2> Ops; 406 Ops.push_back(LHS); 407 Ops.push_back(RHS); 408 return getMulExpr(Ops); 409 } 410 const SCEV *getUDivExpr(const SCEV *LHS, const SCEV *RHS); 411 const SCEV *getAddRecExpr(const SCEV *Start, const SCEV *Step, 412 const Loop *L); 413 const SCEV *getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, 414 const Loop *L); 415 const SCEV *getAddRecExpr(const SmallVectorImpl<const SCEV *> &Operands, 416 const Loop *L) { 417 SmallVector<const SCEV *, 4> NewOp(Operands.begin(), Operands.end()); 418 return getAddRecExpr(NewOp, L); 419 } 420 const SCEV *getSMaxExpr(const SCEV *LHS, const SCEV *RHS); 421 const SCEV *getSMaxExpr(SmallVectorImpl<const SCEV *> &Operands); 422 const SCEV *getUMaxExpr(const SCEV *LHS, const SCEV *RHS); 423 const SCEV *getUMaxExpr(SmallVectorImpl<const SCEV *> &Operands); 424 const SCEV *getSMinExpr(const SCEV *LHS, const SCEV *RHS); 425 const SCEV *getUMinExpr(const SCEV *LHS, const SCEV *RHS); 426 const SCEV *getUnknown(Value *V); 427 const SCEV *getCouldNotCompute(); 428 429 /// getNegativeSCEV - Return the SCEV object corresponding to -V. 430 /// 431 const SCEV *getNegativeSCEV(const SCEV *V); 432 433 /// getNotSCEV - Return the SCEV object corresponding to ~V. 434 /// 435 const SCEV *getNotSCEV(const SCEV *V); 436 437 /// getMinusSCEV - Return LHS-RHS. 438 /// 439 const SCEV *getMinusSCEV(const SCEV *LHS, 440 const SCEV *RHS); 441 442 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion 443 /// of the input value to the specified type. If the type must be 444 /// extended, it is zero extended. 445 const SCEV *getTruncateOrZeroExtend(const SCEV *V, const Type *Ty); 446 447 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion 448 /// of the input value to the specified type. If the type must be 449 /// extended, it is sign extended. 450 const SCEV *getTruncateOrSignExtend(const SCEV *V, const Type *Ty); 451 452 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of 453 /// the input value to the specified type. If the type must be extended, 454 /// it is zero extended. The conversion must not be narrowing. 455 const SCEV *getNoopOrZeroExtend(const SCEV *V, const Type *Ty); 456 457 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of 458 /// the input value to the specified type. If the type must be extended, 459 /// it is sign extended. The conversion must not be narrowing. 460 const SCEV *getNoopOrSignExtend(const SCEV *V, const Type *Ty); 461 462 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of 463 /// the input value to the specified type. If the type must be extended, 464 /// it is extended with unspecified bits. The conversion must not be 465 /// narrowing. 466 const SCEV *getNoopOrAnyExtend(const SCEV *V, const Type *Ty); 467 468 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the 469 /// input value to the specified type. The conversion must not be 470 /// widening. 471 const SCEV *getTruncateOrNoop(const SCEV *V, const Type *Ty); 472 473 /// getIntegerSCEV - Given a SCEVable type, create a constant for the 474 /// specified signed integer value and return a SCEV for the constant. 475 const SCEV *getIntegerSCEV(int Val, const Type *Ty); 476 477 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of 478 /// the types using zero-extension, and then perform a umax operation 479 /// with them. 480 const SCEV *getUMaxFromMismatchedTypes(const SCEV *LHS, 481 const SCEV *RHS); 482 483 /// getUMinFromMismatchedTypes - Promote the operands to the wider of 484 /// the types using zero-extension, and then perform a umin operation 485 /// with them. 486 const SCEV *getUMinFromMismatchedTypes(const SCEV *LHS, 487 const SCEV *RHS); 488 489 /// getSCEVAtScope - Return a SCEV expression handle for the specified value 490 /// at the specified scope in the program. The L value specifies a loop 491 /// nest to evaluate the expression at, where null is the top-level or a 492 /// specified loop is immediately inside of the loop. 493 /// 494 /// This method can be used to compute the exit value for a variable defined 495 /// in a loop by querying what the value will hold in the parent loop. 496 /// 497 /// In the case that a relevant loop exit value cannot be computed, the 498 /// original value V is returned. 499 const SCEV *getSCEVAtScope(const SCEV *S, const Loop *L); 500 501 /// getSCEVAtScope - This is a convenience function which does 502 /// getSCEVAtScope(getSCEV(V), L). 503 const SCEV *getSCEVAtScope(Value *V, const Loop *L); 504 505 /// isLoopGuardedByCond - Test whether entry to the loop is protected by 506 /// a conditional between LHS and RHS. This is used to help avoid max 507 /// expressions in loop trip counts, and to eliminate casts. 508 bool isLoopGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 509 const SCEV *LHS, const SCEV *RHS); 510 511 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is 512 /// protected by a conditional between LHS and RHS. This is used to 513 /// to eliminate casts. 514 bool isLoopBackedgeGuardedByCond(const Loop *L, ICmpInst::Predicate Pred, 515 const SCEV *LHS, const SCEV *RHS); 516 517 /// getBackedgeTakenCount - If the specified loop has a predictable 518 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute 519 /// object. The backedge-taken count is the number of times the loop header 520 /// will be branched to from within the loop. This is one less than the 521 /// trip count of the loop, since it doesn't count the first iteration, 522 /// when the header is branched to from outside the loop. 523 /// 524 /// Note that it is not valid to call this method on a loop without a 525 /// loop-invariant backedge-taken count (see 526 /// hasLoopInvariantBackedgeTakenCount). 527 /// 528 const SCEV *getBackedgeTakenCount(const Loop *L); 529 530 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except 531 /// return the least SCEV value that is known never to be less than the 532 /// actual backedge taken count. 533 const SCEV *getMaxBackedgeTakenCount(const Loop *L); 534 535 /// hasLoopInvariantBackedgeTakenCount - Return true if the specified loop 536 /// has an analyzable loop-invariant backedge-taken count. 537 bool hasLoopInvariantBackedgeTakenCount(const Loop *L); 538 539 /// forgetLoopBackedgeTakenCount - This method should be called by the 540 /// client when it has changed a loop in a way that may effect 541 /// ScalarEvolution's ability to compute a trip count, or if the loop 542 /// is deleted. 543 void forgetLoopBackedgeTakenCount(const Loop *L); 544 545 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S 546 /// is guaranteed to end in (at every loop iteration). It is, at the same 547 /// time, the minimum number of times S is divisible by 2. For example, 548 /// given {4,+,8} it returns 2. If S is guaranteed to be 0, it returns the 549 /// bitwidth of S. 550 uint32_t GetMinTrailingZeros(const SCEV *S); 551 552 /// getUnsignedRange - Determine the unsigned range for a particular SCEV. 553 /// 554 ConstantRange getUnsignedRange(const SCEV *S); 555 556 /// getSignedRange - Determine the signed range for a particular SCEV. 557 /// 558 ConstantRange getSignedRange(const SCEV *S); 559 560 /// isKnownNegative - Test if the given expression is known to be negative. 561 /// 562 bool isKnownNegative(const SCEV *S); 563 564 /// isKnownPositive - Test if the given expression is known to be positive. 565 /// 566 bool isKnownPositive(const SCEV *S); 567 568 /// isKnownNonNegative - Test if the given expression is known to be 569 /// non-negative. 570 /// 571 bool isKnownNonNegative(const SCEV *S); 572 573 /// isKnownNonPositive - Test if the given expression is known to be 574 /// non-positive. 575 /// 576 bool isKnownNonPositive(const SCEV *S); 577 578 /// isKnownNonZero - Test if the given expression is known to be 579 /// non-zero. 580 /// 581 bool isKnownNonZero(const SCEV *S); 582 583 /// isKnownNonZero - Test if the given expression is known to satisfy 584 /// the condition described by Pred, LHS, and RHS. 585 /// 586 bool isKnownPredicate(ICmpInst::Predicate Pred, 587 const SCEV *LHS, const SCEV *RHS); 588 589 virtual bool runOnFunction(Function &F); 590 virtual void releaseMemory(); 591 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 592 void print(raw_ostream &OS, const Module* = 0) const; 593 virtual void print(std::ostream &OS, const Module* = 0) const; 594 void print(std::ostream *OS, const Module* M = 0) const { 595 if (OS) print(*OS, M); 596 } 597 598 private: 599 FoldingSet<SCEV> UniqueSCEVs; 600 BumpPtrAllocator SCEVAllocator; 601 }; 602} 603 604#endif 605