ScalarEvolutionExpressions.h revision f9e64729afec646fe93b51417e66e552e8e630a4
14950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner//===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===// 29769ab22265b313171d201b5928688524a01bd87Misha Brukman// 34950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner// The LLVM Compiler Infrastructure 44950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner// 57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source 67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details. 79769ab22265b313171d201b5928688524a01bd87Misha Brukman// 84950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner//===----------------------------------------------------------------------===// 94950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner// 104950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner// This file defines the classes used to represent and build scalar expressions. 119769ab22265b313171d201b5928688524a01bd87Misha Brukman// 124950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner//===----------------------------------------------------------------------===// 134950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 144950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPRESSIONS_H 154950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner#define LLVM_ANALYSIS_SCALAREVOLUTION_EXPRESSIONS_H 164950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 174950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner#include "llvm/Analysis/ScalarEvolution.h" 18c25e7581b9b8088910da31702d4ca21c4734c6d7Torok Edwin#include "llvm/Support/ErrorHandling.h" 194950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 204950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattnernamespace llvm { 214950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner class ConstantInt; 224950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner class ConstantRange; 235a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng class DominatorTree; 244950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 254950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner enum SCEVTypes { 264950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner // These should be ordered in terms of increasing complexity to make the 274950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner // folders simpler. 28d19534add90a2a894af61523b830887097bb780bDan Gohman scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr, 29c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr, 300f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman scUnknown, scCouldNotCompute 314950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 324950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 334950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner //===--------------------------------------------------------------------===// 344950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// SCEVConstant - This class represents a constant integer value. 354950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 364950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner class SCEVConstant : public SCEV { 37246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman friend class ScalarEvolution; 38246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 394950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner ConstantInt *V; 40c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVConstant(const FoldingSetNodeID &ID, ConstantInt *v) : 41c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEV(ID, scConstant), V(v) {} 424950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner public: 434950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner ConstantInt *getValue() const { return V; } 444950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 454950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool isLoopInvariant(const Loop *L) const { 464950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return true; 474950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 484950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 494950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool hasComputableLoopEvolution(const Loop *L) const { 504950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return false; // Not loop variant 514950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 524950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 534950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual const Type *getType() const; 544950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 55fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman virtual bool hasOperand(const SCEV *) const { 56fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman return false; 57afc0dc7184cf73ff7ac64e516284fbe0c7023ba4Chris Lattner } 58afc0dc7184cf73ff7ac64e516284fbe0c7023ba4Chris Lattner 595a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng bool dominates(BasicBlock *BB, DominatorTree *DT) const { 605a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng return true; 615a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng } 625a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng 636e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const { 646e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman return true; 656e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman } 666e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman 67b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman virtual void print(raw_ostream &OS) const; 684950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 694950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 704950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEVConstant *S) { return true; } 714950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 724950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return S->getSCEVType() == scConstant; 734950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 744950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 754950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 764950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner //===--------------------------------------------------------------------===// 7784923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman /// SCEVCastExpr - This is the base class for unary cast operator classes. 784950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 7984923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman class SCEVCastExpr : public SCEV { 8084923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman protected: 810bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *Op; 824950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner const Type *Ty; 8384923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman 84c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVCastExpr(const FoldingSetNodeID &ID, 85c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman unsigned SCEVTy, const SCEV *op, const Type *ty); 8684923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman 874950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner public: 880bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getOperand() const { return Op; } 894950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual const Type *getType() const { return Ty; } 909769ab22265b313171d201b5928688524a01bd87Misha Brukman 914950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool isLoopInvariant(const Loop *L) const { 924950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return Op->isLoopInvariant(L); 934950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 944950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 954950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool hasComputableLoopEvolution(const Loop *L) const { 964950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return Op->hasComputableLoopEvolution(L); 974950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 984950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 99fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman virtual bool hasOperand(const SCEV *O) const { 100fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman return Op == O || Op->hasOperand(O); 101fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman } 102fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman 10384923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman virtual bool dominates(BasicBlock *BB, DominatorTree *DT) const; 10484923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman 1056e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman virtual bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 1066e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman 10784923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman /// Methods for support type inquiry through isa, cast, and dyn_cast: 10884923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman static inline bool classof(const SCEVCastExpr *S) { return true; } 10984923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman static inline bool classof(const SCEV *S) { 11084923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman return S->getSCEVType() == scTruncate || 11184923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman S->getSCEVType() == scZeroExtend || 11284923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman S->getSCEVType() == scSignExtend; 11384923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman } 11484923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman }; 11584923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman 11684923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman //===--------------------------------------------------------------------===// 11784923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman /// SCEVTruncateExpr - This class represents a truncation of an integer value 11884923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman /// to a smaller integer value. 11984923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman /// 12084923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman class SCEVTruncateExpr : public SCEVCastExpr { 12184923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman friend class ScalarEvolution; 12284923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman 123c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVTruncateExpr(const FoldingSetNodeID &ID, 124c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman const SCEV *op, const Type *ty); 12584923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman 12684923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman public: 127b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman virtual void print(raw_ostream &OS) const; 1284950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 1294950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 1304950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEVTruncateExpr *S) { return true; } 1314950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 1324950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return S->getSCEVType() == scTruncate; 1334950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 1344950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 1354950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 1364950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner //===--------------------------------------------------------------------===// 1374950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// SCEVZeroExtendExpr - This class represents a zero extension of a small 1384950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// integer value to a larger integer value. 1394950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 14084923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman class SCEVZeroExtendExpr : public SCEVCastExpr { 141246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman friend class ScalarEvolution; 142246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 143c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVZeroExtendExpr(const FoldingSetNodeID &ID, 144c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman const SCEV *op, const Type *ty); 1454950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 14684923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman public: 147b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman virtual void print(raw_ostream &OS) const; 1484950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 1494950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 1504950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEVZeroExtendExpr *S) { return true; } 1514950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 1524950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return S->getSCEVType() == scZeroExtend; 1534950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 1544950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 1554950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 156d19534add90a2a894af61523b830887097bb780bDan Gohman //===--------------------------------------------------------------------===// 157d19534add90a2a894af61523b830887097bb780bDan Gohman /// SCEVSignExtendExpr - This class represents a sign extension of a small 158d19534add90a2a894af61523b830887097bb780bDan Gohman /// integer value to a larger integer value. 159d19534add90a2a894af61523b830887097bb780bDan Gohman /// 16084923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman class SCEVSignExtendExpr : public SCEVCastExpr { 161246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman friend class ScalarEvolution; 162246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 163c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVSignExtendExpr(const FoldingSetNodeID &ID, 164c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman const SCEV *op, const Type *ty); 165d19534add90a2a894af61523b830887097bb780bDan Gohman 16684923602fdc2a81957e5dee178d5737ad8e72f55Dan Gohman public: 167b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman virtual void print(raw_ostream &OS) const; 168d19534add90a2a894af61523b830887097bb780bDan Gohman 169d19534add90a2a894af61523b830887097bb780bDan Gohman /// Methods for support type inquiry through isa, cast, and dyn_cast: 170d19534add90a2a894af61523b830887097bb780bDan Gohman static inline bool classof(const SCEVSignExtendExpr *S) { return true; } 171d19534add90a2a894af61523b830887097bb780bDan Gohman static inline bool classof(const SCEV *S) { 172d19534add90a2a894af61523b830887097bb780bDan Gohman return S->getSCEVType() == scSignExtend; 173d19534add90a2a894af61523b830887097bb780bDan Gohman } 174d19534add90a2a894af61523b830887097bb780bDan Gohman }; 175d19534add90a2a894af61523b830887097bb780bDan Gohman 1764950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 177d9480d0f6875d8aa3a0d91942d24f0ee416b1ff1Dan Gohman //===--------------------------------------------------------------------===// 178d9480d0f6875d8aa3a0d91942d24f0ee416b1ff1Dan Gohman /// SCEVNAryExpr - This node is a base class providing common 179d9480d0f6875d8aa3a0d91942d24f0ee416b1ff1Dan Gohman /// functionality for n'ary operators. 180d9480d0f6875d8aa3a0d91942d24f0ee416b1ff1Dan Gohman /// 181ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman class SCEVNAryExpr : public SCEV { 182ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman protected: 183f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman // Since SCEVs are immutable, ScalarEvolution allocates operand 184f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman // arrays with its SCEVAllocator, so this class just needs a simple 185f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman // pointer rather than a more elaborate vector-like data structure. 186f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman // This also avoids the need for a non-trivial destructor. 187f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman const SCEV *const *Operands; 188f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman size_t NumOperands; 1894950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 190c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVNAryExpr(const FoldingSetNodeID &ID, 191f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman enum SCEVTypes T, const SCEV *const *O, size_t N) 192f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman : SCEV(ID, T), Operands(O), NumOperands(N) {} 1934950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 1944950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner public: 195f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman size_t getNumOperands() const { return NumOperands; } 1960bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getOperand(unsigned i) const { 197f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman assert(i < NumOperands && "Operand index out of range!"); 1984950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return Operands[i]; 1994950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 2004950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 201f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman typedef const SCEV *const *op_iterator; 202f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman op_iterator op_begin() const { return Operands; } 203f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman op_iterator op_end() const { return Operands + NumOperands; } 2044950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 2054950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool isLoopInvariant(const Loop *L) const { 2064950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 2074950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner if (!getOperand(i)->isLoopInvariant(L)) return false; 2084950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return true; 2094950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 2104950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 21139592233304acbbeea8c53bac2b6a822a63a4c4bDan Gohman // hasComputableLoopEvolution - N-ary expressions have computable loop 212803513b364e27d303285d396fd8ee5b32149962aChris Lattner // evolutions iff they have at least one operand that varies with the loop, 213803513b364e27d303285d396fd8ee5b32149962aChris Lattner // but that all varying operands are computable. 2144950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool hasComputableLoopEvolution(const Loop *L) const { 215803513b364e27d303285d396fd8ee5b32149962aChris Lattner bool HasVarying = false; 2164950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 217ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov if (!getOperand(i)->isLoopInvariant(L)) { 218803513b364e27d303285d396fd8ee5b32149962aChris Lattner if (getOperand(i)->hasComputableLoopEvolution(L)) 219803513b364e27d303285d396fd8ee5b32149962aChris Lattner HasVarying = true; 220803513b364e27d303285d396fd8ee5b32149962aChris Lattner else 221803513b364e27d303285d396fd8ee5b32149962aChris Lattner return false; 222ae9f3a3b7c915f725aef5a7250e88eaeddda03c6Anton Korobeynikov } 223803513b364e27d303285d396fd8ee5b32149962aChris Lattner return HasVarying; 2244950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 2254950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 226fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman virtual bool hasOperand(const SCEV *O) const { 227fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 228fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman if (O == getOperand(i) || getOperand(i)->hasOperand(O)) 229fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman return true; 230fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman return false; 231fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman } 232fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman 233ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman bool dominates(BasicBlock *BB, DominatorTree *DT) const; 234ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman 2356e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 2366e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman 237ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman virtual const Type *getType() const { return getOperand(0)->getType(); } 238ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman 2393645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman bool hasNoUnsignedWrap() const { return SubclassData & (1 << 0); } 2403645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman void setHasNoUnsignedWrap(bool B) { 2413645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman SubclassData = (SubclassData & ~(1 << 0)) | (B << 0); 2423645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman } 2433645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman bool hasNoSignedWrap() const { return SubclassData & (1 << 1); } 2443645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman void setHasNoSignedWrap(bool B) { 2453645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman SubclassData = (SubclassData & ~(1 << 1)) | (B << 1); 2463645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman } 2473645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman 248ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman /// Methods for support type inquiry through isa, cast, and dyn_cast: 249ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman static inline bool classof(const SCEVNAryExpr *S) { return true; } 250ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman static inline bool classof(const SCEV *S) { 251ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman return S->getSCEVType() == scAddExpr || 252ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman S->getSCEVType() == scMulExpr || 253ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman S->getSCEVType() == scSMaxExpr || 254ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman S->getSCEVType() == scUMaxExpr || 255ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman S->getSCEVType() == scAddRecExpr; 256ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman } 257ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman }; 258ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman 259ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman //===--------------------------------------------------------------------===// 260ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman /// SCEVCommutativeExpr - This node is the base class for n'ary commutative 261ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman /// operators. 262ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman /// 263ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman class SCEVCommutativeExpr : public SCEVNAryExpr { 264ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman protected: 265c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVCommutativeExpr(const FoldingSetNodeID &ID, 266c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman enum SCEVTypes T, 267f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman const SCEV *const *O, size_t N) 268f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman : SCEVNAryExpr(ID, T, O, N) {} 269ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman 270ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman public: 2714950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual const char *getOperationStr() const = 0; 2724950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 273b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman virtual void print(raw_ostream &OS) const; 2744950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 2754950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 2764950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEVCommutativeExpr *S) { return true; } 2774950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 2784950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return S->getSCEVType() == scAddExpr || 279c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky S->getSCEVType() == scMulExpr || 2803e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky S->getSCEVType() == scSMaxExpr || 2813e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky S->getSCEVType() == scUMaxExpr; 2824950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 2834950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 2844950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 2854950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 2864950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner //===--------------------------------------------------------------------===// 2874950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// SCEVAddExpr - This node represents an addition of some number of SCEVs. 2884950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 2894950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner class SCEVAddExpr : public SCEVCommutativeExpr { 290246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman friend class ScalarEvolution; 291246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 292c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVAddExpr(const FoldingSetNodeID &ID, 293f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman const SCEV *const *O, size_t N) 294f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman : SCEVCommutativeExpr(ID, scAddExpr, O, N) { 2954950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 2964950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 2974950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner public: 2984950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual const char *getOperationStr() const { return " + "; } 2994950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3002b98bd23cb0bfa0dc09e1bcaef83a0e606e6ec1aDan Gohman virtual const Type *getType() const { 3012b98bd23cb0bfa0dc09e1bcaef83a0e606e6ec1aDan Gohman // Use the type of the last operand, which is likely to be a pointer 3022b98bd23cb0bfa0dc09e1bcaef83a0e606e6ec1aDan Gohman // type, if there is one. This doesn't usually matter, but it can help 3032b98bd23cb0bfa0dc09e1bcaef83a0e606e6ec1aDan Gohman // reduce casts when the expressions are expanded. 3042b98bd23cb0bfa0dc09e1bcaef83a0e606e6ec1aDan Gohman return getOperand(getNumOperands() - 1)->getType(); 3052b98bd23cb0bfa0dc09e1bcaef83a0e606e6ec1aDan Gohman } 3062b98bd23cb0bfa0dc09e1bcaef83a0e606e6ec1aDan Gohman 3074950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 3084950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEVAddExpr *S) { return true; } 3094950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 3104950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return S->getSCEVType() == scAddExpr; 3114950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 3124950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 3134950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3144950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner //===--------------------------------------------------------------------===// 3154950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// SCEVMulExpr - This node represents multiplication of some number of SCEVs. 3164950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 3174950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner class SCEVMulExpr : public SCEVCommutativeExpr { 318246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman friend class ScalarEvolution; 319246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 320c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVMulExpr(const FoldingSetNodeID &ID, 321f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman const SCEV *const *O, size_t N) 322f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman : SCEVCommutativeExpr(ID, scMulExpr, O, N) { 3234950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 3244950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3254950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner public: 3264950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual const char *getOperationStr() const { return " * "; } 3274950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3284950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 3294950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEVMulExpr *S) { return true; } 3304950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 3314950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return S->getSCEVType() == scMulExpr; 3324950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 3334950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 3344950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3354950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3364950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner //===--------------------------------------------------------------------===// 337e3320a1bcce3f6653e109cc86ee1011b0a61d808Wojciech Matyjewicz /// SCEVUDivExpr - This class represents a binary unsigned division operation. 3384950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 339e3320a1bcce3f6653e109cc86ee1011b0a61d808Wojciech Matyjewicz class SCEVUDivExpr : public SCEV { 340246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman friend class ScalarEvolution; 341246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 3420bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *LHS; 3430bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *RHS; 344c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVUDivExpr(const FoldingSetNodeID &ID, const SCEV *lhs, const SCEV *rhs) 345c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman : SCEV(ID, scUDivExpr), LHS(lhs), RHS(rhs) {} 3464950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3474950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner public: 3480bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getLHS() const { return LHS; } 3490bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getRHS() const { return RHS; } 3504950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3514950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool isLoopInvariant(const Loop *L) const { 3524950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L); 3534950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 3544950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3554950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool hasComputableLoopEvolution(const Loop *L) const { 3564950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return LHS->hasComputableLoopEvolution(L) && 3574950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner RHS->hasComputableLoopEvolution(L); 3584950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 3594950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 360fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman virtual bool hasOperand(const SCEV *O) const { 361fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman return O == LHS || O == RHS || LHS->hasOperand(O) || RHS->hasOperand(O); 362afc0dc7184cf73ff7ac64e516284fbe0c7023ba4Chris Lattner } 363afc0dc7184cf73ff7ac64e516284fbe0c7023ba4Chris Lattner 3645a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng bool dominates(BasicBlock *BB, DominatorTree *DT) const; 365afc0dc7184cf73ff7ac64e516284fbe0c7023ba4Chris Lattner 3666e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 3676e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman 3684950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual const Type *getType() const; 3694950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 370b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman void print(raw_ostream &OS) const; 3714950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3724950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 373e3320a1bcce3f6653e109cc86ee1011b0a61d808Wojciech Matyjewicz static inline bool classof(const SCEVUDivExpr *S) { return true; } 3744950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 375e3320a1bcce3f6653e109cc86ee1011b0a61d808Wojciech Matyjewicz return S->getSCEVType() == scUDivExpr; 3764950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 3774950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 3784950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3794950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 3804950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner //===--------------------------------------------------------------------===// 3814950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip 3821e59aa1332b67256dc0e76237eab7f6dd21a25b4Dan Gohman /// count of the specified loop. This is the primary focus of the 3831e59aa1332b67256dc0e76237eab7f6dd21a25b4Dan Gohman /// ScalarEvolution framework; all the other SCEV subclasses are mostly just 3841e59aa1332b67256dc0e76237eab7f6dd21a25b4Dan Gohman /// supporting infrastructure to allow SCEVAddRecExpr expressions to be 3851e59aa1332b67256dc0e76237eab7f6dd21a25b4Dan Gohman /// created and analyzed. 3864950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 3874950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// All operands of an AddRec are required to be loop invariant. 3884950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 389ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman class SCEVAddRecExpr : public SCEVNAryExpr { 390246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman friend class ScalarEvolution; 391246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 3924950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner const Loop *L; 3934950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 394c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVAddRecExpr(const FoldingSetNodeID &ID, 395f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman const SCEV *const *O, size_t N, const Loop *l) 396f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman : SCEVNAryExpr(ID, scAddRecExpr, O, N), L(l) { 397f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman for (size_t i = 0, e = NumOperands; i != e; ++i) 3984950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner assert(Operands[i]->isLoopInvariant(l) && 3994950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner "Operands of AddRec must be loop-invariant!"); 4004950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 4014950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 402ecb403a9d3a340009c266d05cfca2bd778c7b156Dan Gohman public: 4030bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getStart() const { return Operands[0]; } 4044950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner const Loop *getLoop() const { return L; } 4054950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 4064950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// getStepRecurrence - This method constructs and returns the recurrence 4074950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// indicating how much this expression steps by. If this is a polynomial 4084950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// of degree N, it returns a chrec of degree N-1. 4090bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getStepRecurrence(ScalarEvolution &SE) const { 41017f1972c770dc18f5c7c3c95776b4d62ae9e121dDan Gohman if (isAffine()) return getOperand(1); 4110bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman return SE.getAddRecExpr(SmallVector<const SCEV *, 3>(op_begin()+1, 4120bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman op_end()), 413246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman getLoop()); 4144950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 4154950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 4164950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool hasComputableLoopEvolution(const Loop *QL) const { 417c056baed8704bd715ee58e5dfe724c255e68abbdDan Gohman return L == QL; 4184950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 4194950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 4204950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool isLoopInvariant(const Loop *QueryLoop) const; 4214950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 42239125d8ef94cbadccd7339d3344e114dedaab12cDan Gohman bool dominates(BasicBlock *BB, DominatorTree *DT) const; 42339125d8ef94cbadccd7339d3344e114dedaab12cDan Gohman 42439125d8ef94cbadccd7339d3344e114dedaab12cDan Gohman bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 42539125d8ef94cbadccd7339d3344e114dedaab12cDan Gohman 4264950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// isAffine - Return true if this is an affine AddRec (i.e., it represents 4274950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// an expressions A+B*x where A and B are loop invariant values. 4284950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner bool isAffine() const { 4294950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner // We know that the start value is invariant. This expression is thus 4304950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner // affine iff the step is also invariant. 4314950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return getNumOperands() == 2; 4324950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 4334950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 4344950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it 4354950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// represents an expressions A+B*x+C*x^2 where A, B and C are loop 4364950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N} 4374950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner bool isQuadratic() const { 4384950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return getNumOperands() == 3; 4394950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 4404950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 4414950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// evaluateAtIteration - Return the value of this chain of recurrences at 4424950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// the specified iteration number. 4430bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const; 4444950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 4454950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// getNumIterationsInRange - Return the number of iterations of this loop 4464950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// that produce values in the specified constant range. Another way of 4474950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// looking at this is that it returns the first iteration number where the 4484950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// value is not in the condition, thus computing the exit count. If the 4494950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// iteration count can't be computed, an instance of SCEVCouldNotCompute is 450eefdebe002ede066bf80859e72aec831cfd1d92dNick Lewycky /// returned. 4510bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *getNumIterationsInRange(ConstantRange Range, 452246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman ScalarEvolution &SE) const; 4534950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 454c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman /// getPostIncExpr - Return an expression representing the value of 455c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman /// this expression one iteration of the loop ahead. 456fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman const SCEVAddRecExpr *getPostIncExpr(ScalarEvolution &SE) const { 457fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman return cast<SCEVAddRecExpr>(SE.getAddExpr(this, getStepRecurrence(SE))); 458c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman } 459c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman 460b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman virtual void print(raw_ostream &OS) const; 4614950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 4624950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 4634950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEVAddRecExpr *S) { return true; } 4644950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 4654950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return S->getSCEVType() == scAddRecExpr; 4664950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 4674950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 4684950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 469c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky 470c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky //===--------------------------------------------------------------------===// 471c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky /// SCEVSMaxExpr - This class represents a signed maximum selection. 472c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky /// 473c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky class SCEVSMaxExpr : public SCEVCommutativeExpr { 474c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky friend class ScalarEvolution; 475c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky 476c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVSMaxExpr(const FoldingSetNodeID &ID, 477f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman const SCEV *const *O, size_t N) 478f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman : SCEVCommutativeExpr(ID, scSMaxExpr, O, N) { 4793645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman // Max never overflows. 4803645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman setHasNoUnsignedWrap(true); 4813645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman setHasNoSignedWrap(true); 482c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky } 483c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky 484c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky public: 485c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky virtual const char *getOperationStr() const { return " smax "; } 486c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky 487c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky /// Methods for support type inquiry through isa, cast, and dyn_cast: 488c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky static inline bool classof(const SCEVSMaxExpr *S) { return true; } 489c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky static inline bool classof(const SCEV *S) { 490c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky return S->getSCEVType() == scSMaxExpr; 491c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky } 492c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky }; 493c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky 494c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky 4954950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner //===--------------------------------------------------------------------===// 4963e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky /// SCEVUMaxExpr - This class represents an unsigned maximum selection. 4973e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky /// 4983e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky class SCEVUMaxExpr : public SCEVCommutativeExpr { 4993e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky friend class ScalarEvolution; 5003e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky 501c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVUMaxExpr(const FoldingSetNodeID &ID, 502f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman const SCEV *const *O, size_t N) 503f9e64729afec646fe93b51417e66e552e8e630a4Dan Gohman : SCEVCommutativeExpr(ID, scUMaxExpr, O, N) { 5043645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman // Max never overflows. 5053645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman setHasNoUnsignedWrap(true); 5063645b01002e7ac244c1f3d163e5e350df21d869dDan Gohman setHasNoSignedWrap(true); 5073e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky } 5083e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky 5093e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky public: 5103e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky virtual const char *getOperationStr() const { return " umax "; } 5113e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky 5123e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky /// Methods for support type inquiry through isa, cast, and dyn_cast: 5133e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky static inline bool classof(const SCEVUMaxExpr *S) { return true; } 5143e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky static inline bool classof(const SCEV *S) { 5153e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky return S->getSCEVType() == scUMaxExpr; 5163e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky } 5173e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky }; 5183e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky 519c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman //===--------------------------------------------------------------------===// 5204950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// SCEVUnknown - This means that we are dealing with an entirely unknown SCEV 52175144f93eb7e4dbf22d308d21581ae255dd520c6Dan Gohman /// value, and only represent it as its LLVM Value. This is the "bottom" 5224950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// value for the analysis. 5234950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// 5244950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner class SCEVUnknown : public SCEV { 525246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman friend class ScalarEvolution; 526246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman 5274950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner Value *V; 528c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEVUnknown(const FoldingSetNodeID &ID, Value *v) : 529c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman SCEV(ID, scUnknown), V(v) {} 5301c34375f79b7786351c27ae45f0412cfdfa004edDan Gohman 531c050fd94c29e31414591e3a18aa20049e6b3a84fDan Gohman public: 5322cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner Value *getValue() const { return V; } 5334950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 5340f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman /// isSizeOf, isAlignOf, isOffsetOf - Test whether this is a special 5350f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman /// constant representing a type size, alignment, or field offset in 5360f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman /// a target-independent manner, and hasn't happened to have been 5370f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman /// folded with other operations into something unrecognizable. This 5380f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman /// is mainly only useful for pretty-printing and other situations 5390f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman /// where it isn't absolutely required for these to succeed. 5400f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman bool isSizeOf(const Type *&AllocTy) const; 5410f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman bool isAlignOf(const Type *&AllocTy) const; 5424f8eea82d8967cffa85b9df6c9255717b059009eDan Gohman bool isOffsetOf(const Type *&STy, Constant *&FieldNo) const; 5430f5efe56258f8cd6ceff4d7955a5d80144cd9cb0Dan Gohman 5444950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool isLoopInvariant(const Loop *L) const; 5454950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual bool hasComputableLoopEvolution(const Loop *QL) const { 5464950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return false; // not computable 5474950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 5484950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 549fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman virtual bool hasOperand(const SCEV *) const { 550fef8bb24de86ff41d09a7787c6c52b058a853e39Dan Gohman return false; 551afc0dc7184cf73ff7ac64e516284fbe0c7023ba4Chris Lattner } 552afc0dc7184cf73ff7ac64e516284fbe0c7023ba4Chris Lattner 5535a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng bool dominates(BasicBlock *BB, DominatorTree *DT) const; 5545a6c1a840ad343c0ed2fa54a0edb50b61f828f0fEvan Cheng 5556e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman bool properlyDominates(BasicBlock *BB, DominatorTree *DT) const; 5566e70e31810464289a9e9b37e0345847e3ca5d5cfDan Gohman 5574950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner virtual const Type *getType() const; 5584950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 559b7ef72963b2215ca23c27fa8ea777bada06994d0Dan Gohman virtual void print(raw_ostream &OS) const; 5604950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 5614950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner /// Methods for support type inquiry through isa, cast, and dyn_cast: 5624950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEVUnknown *S) { return true; } 5634950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner static inline bool classof(const SCEV *S) { 5644950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner return S->getSCEVType() == scUnknown; 5654950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner } 5664950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner }; 5672cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner 5682cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner /// SCEVVisitor - This class defines a simple visitor class that may be used 5692cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner /// for various SCEV analysis purposes. 5702cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner template<typename SC, typename RetVal=void> 5712cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner struct SCEVVisitor { 572890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman RetVal visit(const SCEV *S) { 5732cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner switch (S->getSCEVType()) { 5742cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner case scConstant: 575890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitConstant((const SCEVConstant*)S); 5762cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner case scTruncate: 577890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitTruncateExpr((const SCEVTruncateExpr*)S); 5782cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner case scZeroExtend: 579890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitZeroExtendExpr((const SCEVZeroExtendExpr*)S); 580d19534add90a2a894af61523b830887097bb780bDan Gohman case scSignExtend: 581890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitSignExtendExpr((const SCEVSignExtendExpr*)S); 5822cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner case scAddExpr: 583890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitAddExpr((const SCEVAddExpr*)S); 5842cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner case scMulExpr: 585890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitMulExpr((const SCEVMulExpr*)S); 586e3320a1bcce3f6653e109cc86ee1011b0a61d808Wojciech Matyjewicz case scUDivExpr: 587890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitUDivExpr((const SCEVUDivExpr*)S); 5882cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner case scAddRecExpr: 589890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitAddRecExpr((const SCEVAddRecExpr*)S); 590c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky case scSMaxExpr: 591890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitSMaxExpr((const SCEVSMaxExpr*)S); 5923e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky case scUMaxExpr: 593890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitUMaxExpr((const SCEVUMaxExpr*)S); 5942cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner case scUnknown: 595890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitUnknown((const SCEVUnknown*)S); 5962cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner case scCouldNotCompute: 597890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman return ((SC*)this)->visitCouldNotCompute((const SCEVCouldNotCompute*)S); 5982cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner default: 599c23197a26f34f559ea9797de51e187087c039c42Torok Edwin llvm_unreachable("Unknown SCEV type!"); 6002cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner } 6012cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner } 6022cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner 603890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S) { 604c23197a26f34f559ea9797de51e187087c039c42Torok Edwin llvm_unreachable("Invalid use of SCEVCouldNotCompute!"); 6052cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner return RetVal(); 6062cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner } 6072cdf0a7a32f08674562b0cf6b071b96b36421f87Chris Lattner }; 6084950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner} 6094950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner 6104950e88e0fa7c44c0b54e2f64ddf57415ed83d40Chris Lattner#endif 611