ScalarEvolutionExpander.h revision 204494149b6f846e8f173f525b129f5508076049
19ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
29ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//
39ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//                     The LLVM Compiler Infrastructure
49ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//
59ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell// This file is distributed under the University of Illinois Open Source
69ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell// License. See LICENSE.TXT for details.
79ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//
89ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//===----------------------------------------------------------------------===//
99ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//
109ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell// This file defines the classes used to generate code from scalar expressions.
119ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//
129ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell//===----------------------------------------------------------------------===//
139ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
149ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
159ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
169ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
179ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#include "llvm/Analysis/ScalarEvolutionExpressions.h"
189ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#include "llvm/Analysis/ScalarEvolutionNormalization.h"
199ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#include "llvm/Support/IRBuilder.h"
209ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#include "llvm/Support/TargetFolder.h"
219ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#include "llvm/Support/ValueHandle.h"
229ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#include <set>
239ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
249ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwellnamespace llvm {
259ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  /// SCEVExpander - This class uses information about analyze scalars to
269ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  /// rewrite expressions in canonical form.
279ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  ///
289ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  /// Clients should create an instance of this class when rewriting is needed,
299ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  /// and destroy it when finished to allow the release of the associated
309ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  /// memory.
319ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
329ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    ScalarEvolution &SE;
339ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
349ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    // New instructions receive a name to identifies them with the current pass.
359ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    const char* IVName;
369ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
379ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    std::map<std::pair<const SCEV *, Instruction *>, AssertingVH<Value> >
389ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      InsertedExpressions;
399ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    std::set<AssertingVH<Value> > InsertedValues;
409ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    std::set<AssertingVH<Value> > InsertedPostIncValues;
419ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
429ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// RelevantLoops - A memoization of the "relevant" loop for a given SCEV.
439ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    DenseMap<const SCEV *, const Loop *> RelevantLoops;
449ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
459ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// PostIncLoops - Addrecs referring to any of the given loops are expanded
469ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode
479ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// returns the add instruction that adds one to the phi for {0,+,1}<L>,
489ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// as opposed to a new phi starting at 1. This is only supported in
499ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// non-canonical mode.
509ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    PostIncLoopSet PostIncLoops;
519ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
529ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// IVIncInsertPos - When this is non-null, addrecs expanded in the
539ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// loop it indicates should be inserted with increments at
549ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// IVIncInsertPos.
559ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    const Loop *IVIncInsertLoop;
569ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
579ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// IVIncInsertPos - When expanding addrecs in the IVIncInsertLoop loop,
589ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// insert the IV increment at this position.
599ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Instruction *IVIncInsertPos;
60997a2547d10890dbc00f2c48191cde58a2ee37c8Chia-I Wu
61576d9af505bd19ccaac04a68c8744f36f887bd5cChia-I Wu    /// CanonicalMode - When true, expressions are expanded in "canonical"
622aaca1df9df6980ec88180c8866c8987b31db91aJosé Fonseca    /// form. In particular, addrecs are expanded as arithmetic based on
639ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// a canonical induction variable. When false, expression are expanded
641675d05f911fbd569efb5248674aa71cb755c75bKeith Whitwell    /// in a more literal form.
659ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    bool CanonicalMode;
6605f8e41b9567695e9b96276d3ac5734ed2b268a8Keith Whitwell
6705f8e41b9567695e9b96276d3ac5734ed2b268a8Keith Whitwell    /// When invoked from LSR, the expander is in "strength reduction" mode. The
6805f8e41b9567695e9b96276d3ac5734ed2b268a8Keith Whitwell    /// only difference is that phi's are only reused if they are already in
699ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// "expanded" form.
706632915e957149c153a3f793c400a532b4995b18Chia-I Wu    bool LSRMode;
719ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
7205f8e41b9567695e9b96276d3ac5734ed2b268a8Keith Whitwell    typedef IRBuilder<true, TargetFolder> BuilderType;
7305f8e41b9567695e9b96276d3ac5734ed2b268a8Keith Whitwell    BuilderType Builder;
749ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
759ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#ifndef NDEBUG
766632915e957149c153a3f793c400a532b4995b18Chia-I Wu    const char *DebugType;
776632915e957149c153a3f793c400a532b4995b18Chia-I Wu#endif
786632915e957149c153a3f793c400a532b4995b18Chia-I Wu
796632915e957149c153a3f793c400a532b4995b18Chia-I Wu    friend struct SCEVVisitor<SCEVExpander, Value*>;
806632915e957149c153a3f793c400a532b4995b18Chia-I Wu
816632915e957149c153a3f793c400a532b4995b18Chia-I Wu  public:
8248bc3cca89f7aecc40d1661e695796113df6e583Chia-I Wu    /// SCEVExpander - Construct a SCEVExpander in "canonical" mode.
8348bc3cca89f7aecc40d1661e695796113df6e583Chia-I Wu    explicit SCEVExpander(ScalarEvolution &se, const char *name)
846632915e957149c153a3f793c400a532b4995b18Chia-I Wu      : SE(se), IVName(name), IVIncInsertLoop(0), IVIncInsertPos(0),
856632915e957149c153a3f793c400a532b4995b18Chia-I Wu        CanonicalMode(true), LSRMode(false),
869ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell        Builder(se.getContext(), TargetFolder(se.TD)) {
879ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#ifndef NDEBUG
889ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      DebugType = "";
899ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#endif
909ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
919ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
929ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#ifndef NDEBUG
939ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void setDebugType(const char* s) { DebugType = s; }
949ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell#endif
959ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
969ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// clear - Erase the contents of the InsertedExpressions map so that users
979ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// trying to expand the same expression into multiple BasicBlocks or
989ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// different places within the same BasicBlock can do so.
999ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void clear() {
1009ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      InsertedExpressions.clear();
1019ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      InsertedValues.clear();
1029ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      InsertedPostIncValues.clear();
1039ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
1049ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1059ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// getOrInsertCanonicalInductionVariable - This method returns the
1069ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// canonical induction variable of the specified type for the specified
1079ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// loop (inserting one if there is none).  A canonical induction variable
1089ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// starts at zero and steps by one on each iteration.
1099ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty);
110153b4d5cdd934812d8c24ef10bb8bbbe852eaf62Keith Whitwell
111153b4d5cdd934812d8c24ef10bb8bbbe852eaf62Keith Whitwell    /// hoistStep - Utility for hoisting an IV increment.
1129ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    static bool hoistStep(Instruction *IncV, Instruction *InsertPos,
1139ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                          const DominatorTree *DT);
1149ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1159ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// replaceCongruentIVs - replace congruent phis with their most canonical
1169ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// representative. Return the number of phis eliminated.
1179ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
1189ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                                 SmallVectorImpl<WeakVH> &DeadInsts);
1199ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1209ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// expandCodeFor - Insert code to directly compute the specified SCEV
1219ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// expression into the program.  The inserted code is inserted into the
1229ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// specified block.
1239ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I);
1249ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1259ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// setIVIncInsertPos - Set the current IV increment loop and position.
1269ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
1279ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      assert(!CanonicalMode &&
1289ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell             "IV increment positions are not supported in CanonicalMode");
1299ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      IVIncInsertLoop = L;
1309ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      IVIncInsertPos = Pos;
1319ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
1329ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1339ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// setPostInc - Enable post-inc expansion for addrecs referring to the
1349ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// given loops. Post-inc expansion is only supported in non-canonical
1359ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// mode.
1369ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void setPostInc(const PostIncLoopSet &L) {
1379ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      assert(!CanonicalMode &&
1389ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell             "Post-inc expansion is not supported in CanonicalMode");
1399ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      PostIncLoops = L;
1409ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
1419ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1429ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// clearPostInc - Disable all post-inc expansion.
1431929d52fd907b4e42e31ad459dd50a1de53df26cBrian Paul    void clearPostInc() {
1441929d52fd907b4e42e31ad459dd50a1de53df26cBrian Paul      PostIncLoops.clear();
1451929d52fd907b4e42e31ad459dd50a1de53df26cBrian Paul
1461929d52fd907b4e42e31ad459dd50a1de53df26cBrian Paul      // When we change the post-inc loop set, cached expansions may no
1479ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      // longer be valid.
1489ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      InsertedPostIncValues.clear();
1499ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
1509ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1519ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// disableCanonicalMode - Disable the behavior of expanding expressions in
1529ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// canonical form rather than in a more literal form. Non-canonical mode
1539ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// is useful for late optimization passes.
1549ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void disableCanonicalMode() { CanonicalMode = false; }
1559ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1569ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void enableLSRMode() { LSRMode = true; }
1579ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1589ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// clearInsertPoint - Clear the current insertion point. This is useful
159153b4d5cdd934812d8c24ef10bb8bbbe852eaf62Keith Whitwell    /// if the instruction that had been serving as the insertion point may
1609ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// have been deleted.
1619ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void clearInsertPoint() {
1629ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      Builder.ClearInsertionPoint();
1639ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
1649ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  private:
1659ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    LLVMContext &getContext() const { return SE.getContext(); }
166153b4d5cdd934812d8c24ef10bb8bbbe852eaf62Keith Whitwell
167153b4d5cdd934812d8c24ef10bb8bbbe852eaf62Keith Whitwell    /// InsertBinop - Insert the specified binary operator, doing a small amount
1689ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// of work to avoid inserting an obviously redundant operation.
1699ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
1709ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1719ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// ReuseOrCreateCast - Arange for there to be a cast of V to Ty at IP,
1729ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// reusing an existing cast if a suitable one exists, moving an existing
1739ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// cast if a suitable one exists but isn't in the right place, or
1749ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// or creating a new one.
1759ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *ReuseOrCreateCast(Value *V, Type *Ty,
1769ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                             Instruction::CastOps Op,
1779ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                             BasicBlock::iterator IP);
1789ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1799ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
1809ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// which must be possible with a noop cast, doing what we can to
181153b4d5cdd934812d8c24ef10bb8bbbe852eaf62Keith Whitwell    /// share the casts.
182153b4d5cdd934812d8c24ef10bb8bbbe852eaf62Keith Whitwell    Value *InsertNoopCastOfTo(Value *V, Type *Ty);
1839ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1849ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
1859ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// instead of using ptrtoint+arithmetic+inttoptr.
1869ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *expandAddToGEP(const SCEV *const *op_begin,
1879ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                          const SCEV *const *op_end,
1889ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                          PointerType *PTy, Type *Ty, Value *V);
1899ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1909ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *expand(const SCEV *S);
1919ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1929ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// expandCodeFor - Insert code to directly compute the specified SCEV
1939ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// expression into the program.  The inserted code is inserted into the
1949ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// SCEVExpander's current insertion point. If a type is specified, the
1959ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// result will be expanded to have that type, with a cast if necessary.
1969ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *expandCodeFor(const SCEV *SH, Type *Ty = 0);
1979ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
1989ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// isInsertedInstruction - Return true if the specified instruction was
1999ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// inserted by the code rewriter.  If so, the client should not modify the
2009ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// instruction.
2019ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    bool isInsertedInstruction(Instruction *I) const {
2029ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      return InsertedValues.count(I) || InsertedPostIncValues.count(I);
2039ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
2049ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2059ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    /// getRelevantLoop - Determine the most "relevant" loop for the given SCEV.
2069ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    const Loop *getRelevantLoop(const SCEV *);
2079ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2089ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitConstant(const SCEVConstant *S) {
2099ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      return S->getValue();
2109ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
2119ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2129ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitTruncateExpr(const SCEVTruncateExpr *S);
2139ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2149ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
2159ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2169ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
2179ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2189ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitAddExpr(const SCEVAddExpr *S);
2199ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2209ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitMulExpr(const SCEVMulExpr *S);
2219ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2229ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitUDivExpr(const SCEVUDivExpr *S);
2239ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2249ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitAddRecExpr(const SCEVAddRecExpr *S);
2259ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2269ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitSMaxExpr(const SCEVSMaxExpr *S);
2279ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2289ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitUMaxExpr(const SCEVUMaxExpr *S);
2299ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2309ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *visitUnknown(const SCEVUnknown *S) {
2319ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell      return S->getValue();
2329ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    }
2339ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2349ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void rememberInstruction(Value *I);
2359ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2369ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    void restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I);
2379ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2389ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
2399ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2409ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L,
2419ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                                 Type *ExpandTy);
2429ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
2439ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
2449ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell    PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
2459ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                                       const Loop *L,
2469ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                                       Type *ExpandTy,
2479ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell                                       Type *IntTy);
2489ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell  };
2499ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell}
2509ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell
251153b4d5cdd934812d8c24ef10bb8bbbe852eaf62Keith Whitwell#endif
2529ed74c61d4c587ef7bc202d876d4a7e02c35fab7Keith Whitwell