136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//                     The LLVM Compiler Infrastructure
436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//===----------------------------------------------------------------------===//
936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
1036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman// This file defines the classes used to generate code from scalar expressions.
1136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
1236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//===----------------------------------------------------------------------===//
1336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
14674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
15674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#define LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H
1636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
1736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#include "llvm/Analysis/ScalarEvolutionExpressions.h"
18448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman#include "llvm/Analysis/ScalarEvolutionNormalization.h"
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/IRBuilder.h"
20267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman#include "llvm/Support/TargetFolder.h"
21ab28928fe276d20cf9533ae6b858497f835c7a53Dan Gohman#include "llvm/Support/ValueHandle.h"
2203ee68a145ab5394c070298049d93f305be93ec3Dan Gohman#include <set>
2336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
2436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begemannamespace llvm {
25e4ba75f43e2ab1480d119d2d4eb878256274e0fbChandler Carruth  class TargetTransformInfo;
26ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick
27e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick  /// Return true if the given expression is safe to expand in the sense that
28e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick  /// all materialized values are safe to speculate.
29e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick  bool isSafeToExpand(const SCEV *S);
30e08c32249fca32cd7b122024a4ca252fcb235694Andrew Trick
3136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  /// SCEVExpander - This class uses information about analyze scalars to
3236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  /// rewrite expressions in canonical form.
3336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  ///
3436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  /// Clients should create an instance of this class when rewriting is needed,
354c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman  /// and destroy it when finished to allow the release of the associated
3636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  /// memory.
37a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman  class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
3836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    ScalarEvolution &SE;
395e7645be4c9dd2193add44d30b5fef8036d7a3ceAndrew Trick
405e7645be4c9dd2193add44d30b5fef8036d7a3ceAndrew Trick    // New instructions receive a name to identifies them with the current pass.
416d64a04a4d1c2f4f6f8877cc0811a4d4386e518fAndrew Trick    const char* IVName;
425e7645be4c9dd2193add44d30b5fef8036d7a3ceAndrew Trick
431ba5769676bb14078ddbdb9760523619726800c0Andrew Trick    // InsertedExpressions caches Values for reuse, so must track RAUW.
441ba5769676bb14078ddbdb9760523619726800c0Andrew Trick    std::map<std::pair<const SCEV *, Instruction *>, TrackingVH<Value> >
45667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman      InsertedExpressions;
461ba5769676bb14078ddbdb9760523619726800c0Andrew Trick    // InsertedValues only flags inserted instructions so needs no RAUW.
47ab28928fe276d20cf9533ae6b858497f835c7a53Dan Gohman    std::set<AssertingVH<Value> > InsertedValues;
48ab28928fe276d20cf9533ae6b858497f835c7a53Dan Gohman    std::set<AssertingVH<Value> > InsertedPostIncValues;
4936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
509c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman    /// RelevantLoops - A memoization of the "relevant" loop for a given SCEV.
519c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman    DenseMap<const SCEV *, const Loop *> RelevantLoops;
529c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman
53448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    /// PostIncLoops - Addrecs referring to any of the given loops are expanded
54448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode
55448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    /// returns the add instruction that adds one to the phi for {0,+,1}<L>,
56448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    /// as opposed to a new phi starting at 1. This is only supported in
57448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    /// non-canonical mode.
58448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    PostIncLoopSet PostIncLoops;
59a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
60a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// IVIncInsertPos - When this is non-null, addrecs expanded in the
61a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// loop it indicates should be inserted with increments at
62a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// IVIncInsertPos.
63a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    const Loop *IVIncInsertLoop;
64a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
65a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// IVIncInsertPos - When expanding addrecs in the IVIncInsertLoop loop,
66a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// insert the IV increment at this position.
67a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    Instruction *IVIncInsertPos;
68a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
6964925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick    /// Phis that complete an IV chain. Reuse
7064925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick    std::set<AssertingVH<PHINode> > ChainedPhis;
7164925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick
72a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// CanonicalMode - When true, expressions are expanded in "canonical"
73a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// form. In particular, addrecs are expanded as arithmetic based on
74a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// a canonical induction variable. When false, expression are expanded
75a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// in a more literal form.
76a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    bool CanonicalMode;
77a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
78c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick    /// When invoked from LSR, the expander is in "strength reduction" mode. The
79c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick    /// only difference is that phi's are only reused if they are already in
80c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick    /// "expanded" form.
81c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick    bool LSRMode;
82c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick
83267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    typedef IRBuilder<true, TargetFolder> BuilderType;
84267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    BuilderType Builder;
8536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
86204494149b6f846e8f173f525b129f5508076049Andrew Trick#ifndef NDEBUG
87204494149b6f846e8f173f525b129f5508076049Andrew Trick    const char *DebugType;
88204494149b6f846e8f173f525b129f5508076049Andrew Trick#endif
89204494149b6f846e8f173f525b129f5508076049Andrew Trick
9036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    friend struct SCEVVisitor<SCEVExpander, Value*>;
919269926bfb713e92e328e9578c6d8826c68dcb9dDan Gohman
9236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  public:
93a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// SCEVExpander - Construct a SCEVExpander in "canonical" mode.
946d64a04a4d1c2f4f6f8877cc0811a4d4386e518fAndrew Trick    explicit SCEVExpander(ScalarEvolution &se, const char *name)
95d152d03a476b8d0d4b26577db26e2ba76034b0f3Andrew Trick      : SE(se), IVName(name), IVIncInsertLoop(0), IVIncInsertPos(0),
96c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick        CanonicalMode(true), LSRMode(false),
97204494149b6f846e8f173f525b129f5508076049Andrew Trick        Builder(se.getContext(), TargetFolder(se.TD)) {
98204494149b6f846e8f173f525b129f5508076049Andrew Trick#ifndef NDEBUG
99204494149b6f846e8f173f525b129f5508076049Andrew Trick      DebugType = "";
100204494149b6f846e8f173f525b129f5508076049Andrew Trick#endif
101204494149b6f846e8f173f525b129f5508076049Andrew Trick    }
102204494149b6f846e8f173f525b129f5508076049Andrew Trick
103204494149b6f846e8f173f525b129f5508076049Andrew Trick#ifndef NDEBUG
104204494149b6f846e8f173f525b129f5508076049Andrew Trick    void setDebugType(const char* s) { DebugType = s; }
105204494149b6f846e8f173f525b129f5508076049Andrew Trick#endif
1068f9f0d3a34ebbcd6d075fbb1250dc74f36579d50Chris Lattner
10736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// clear - Erase the contents of the InsertedExpressions map so that users
108d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen    /// trying to expand the same expression into multiple BasicBlocks or
10936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// different places within the same BasicBlock can do so.
110ab28928fe276d20cf9533ae6b858497f835c7a53Dan Gohman    void clear() {
111ab28928fe276d20cf9533ae6b858497f835c7a53Dan Gohman      InsertedExpressions.clear();
112ab28928fe276d20cf9533ae6b858497f835c7a53Dan Gohman      InsertedValues.clear();
113ab28928fe276d20cf9533ae6b858497f835c7a53Dan Gohman      InsertedPostIncValues.clear();
11464925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick      ChainedPhis.clear();
115ab28928fe276d20cf9533ae6b858497f835c7a53Dan Gohman    }
116d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
11736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// getOrInsertCanonicalInductionVariable - This method returns the
11836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// canonical induction variable of the specified type for the specified
11936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// loop (inserting one if there is none).  A canonical induction variable
12036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// starts at zero and steps by one on each iteration.
121c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick    PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty);
12236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
123b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    /// getIVIncOperand - Return the induction variable increment's IV operand.
124b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    Instruction *getIVIncOperand(Instruction *IncV, Instruction *InsertPos,
125b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick                                 bool allowScale);
126b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick
127b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    /// hoistIVInc - Utility for hoisting an IV increment.
128b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    bool hoistIVInc(Instruction *IncV, Instruction *InsertPos);
129204494149b6f846e8f173f525b129f5508076049Andrew Trick
130204494149b6f846e8f173f525b129f5508076049Andrew Trick    /// replaceCongruentIVs - replace congruent phis with their most canonical
131204494149b6f846e8f173f525b129f5508076049Andrew Trick    /// representative. Return the number of phis eliminated.
132204494149b6f846e8f173f525b129f5508076049Andrew Trick    unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
133ee98aa87434d9d49a8e4dab41d873888ac9c4805Andrew Trick                                 SmallVectorImpl<WeakVH> &DeadInsts,
134e4ba75f43e2ab1480d119d2d4eb878256274e0fbChandler Carruth                                 const TargetTransformInfo *TTI = NULL);
135204494149b6f846e8f173f525b129f5508076049Andrew Trick
136752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman    /// expandCodeFor - Insert code to directly compute the specified SCEV
137752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman    /// expression into the program.  The inserted code is inserted into the
13836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// specified block.
139db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I);
14036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
141a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// setIVIncInsertPos - Set the current IV increment loop and position.
142a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
143a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      assert(!CanonicalMode &&
144a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman             "IV increment positions are not supported in CanonicalMode");
145a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      IVIncInsertLoop = L;
146a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      IVIncInsertPos = Pos;
147a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    }
148a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
149448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    /// setPostInc - Enable post-inc expansion for addrecs referring to the
150448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    /// given loops. Post-inc expansion is only supported in non-canonical
151a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// mode.
152448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    void setPostInc(const PostIncLoopSet &L) {
153a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      assert(!CanonicalMode &&
154a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman             "Post-inc expansion is not supported in CanonicalMode");
155448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman      PostIncLoops = L;
156448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    }
157448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman
158448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    /// clearPostInc - Disable all post-inc expansion.
159448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman    void clearPostInc() {
160448db1cdef5872713ef77beffacf502ae3450cd7Dan Gohman      PostIncLoops.clear();
16125fcaff409f5c4c6da08f148ffb9404a71e8e4a7Dan Gohman
16225fcaff409f5c4c6da08f148ffb9404a71e8e4a7Dan Gohman      // When we change the post-inc loop set, cached expansions may no
16325fcaff409f5c4c6da08f148ffb9404a71e8e4a7Dan Gohman      // longer be valid.
16425fcaff409f5c4c6da08f148ffb9404a71e8e4a7Dan Gohman      InsertedPostIncValues.clear();
165a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    }
166a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
167a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// disableCanonicalMode - Disable the behavior of expanding expressions in
168a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// canonical form rather than in a more literal form. Non-canonical mode
169a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// is useful for late optimization passes.
170a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    void disableCanonicalMode() { CanonicalMode = false; }
171a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
172c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick    void enableLSRMode() { LSRMode = true; }
173c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick
174472fdf7090bb00af3a3f9dcbe22263120a527533Dan Gohman    /// clearInsertPoint - Clear the current insertion point. This is useful
175472fdf7090bb00af3a3f9dcbe22263120a527533Dan Gohman    /// if the instruction that had been serving as the insertion point may
176472fdf7090bb00af3a3f9dcbe22263120a527533Dan Gohman    /// have been deleted.
177472fdf7090bb00af3a3f9dcbe22263120a527533Dan Gohman    void clearInsertPoint() {
178472fdf7090bb00af3a3f9dcbe22263120a527533Dan Gohman      Builder.ClearInsertionPoint();
179472fdf7090bb00af3a3f9dcbe22263120a527533Dan Gohman    }
18064925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick
181b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    /// isInsertedInstruction - Return true if the specified instruction was
182b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    /// inserted by the code rewriter.  If so, the client should not modify the
183b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    /// instruction.
184b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    bool isInsertedInstruction(Instruction *I) const {
185b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick      return InsertedValues.count(I) || InsertedPostIncValues.count(I);
186b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick    }
187b5c26ef9da16052597d59a412eaae32098aa1be0Andrew Trick
18864925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick    void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); }
18964925c55c65f9345a69fb67db07aa62cfb723577Andrew Trick
190267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  private:
191e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson    LLVMContext &getContext() const { return SE.getContext(); }
1924c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman
193267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    /// InsertBinop - Insert the specified binary operator, doing a small amount
194267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    /// of work to avoid inserting an obviously redundant operation.
195267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
196af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman
197485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman    /// ReuseOrCreateCast - Arange for there to be a cast of V to Ty at IP,
198485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman    /// reusing an existing cast if a suitable one exists, moving an existing
199485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman    /// cast if a suitable one exists but isn't in the right place, or
200485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman    /// or creating a new one.
201db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Value *ReuseOrCreateCast(Value *V, Type *Ty,
202485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman                             Instruction::CastOps Op,
203485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman                             BasicBlock::iterator IP);
204485c43fc478d5e16c55e14cb2586b56cc1c4c91fDan Gohman
205af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
206267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    /// which must be possible with a noop cast, doing what we can to
207267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    /// share the casts.
208db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Value *InsertNoopCastOfTo(Value *V, Type *Ty);
209af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman
2105be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
2115be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    /// instead of using ptrtoint+arithmetic+inttoptr.
2120bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    Value *expandAddToGEP(const SCEV *const *op_begin,
2130bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman                          const SCEV *const *op_end,
214db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                          PointerType *PTy, Type *Ty, Value *V);
2155be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
216890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *expand(const SCEV *S);
2172d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman
218667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// expandCodeFor - Insert code to directly compute the specified SCEV
219667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// expression into the program.  The inserted code is inserted into the
220667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// SCEVExpander's current insertion point. If a type is specified, the
221667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// result will be expanded to have that type, with a cast if necessary.
222db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner    Value *expandCodeFor(const SCEV *SH, Type *Ty = 0);
223667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman
2249c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman    /// getRelevantLoop - Determine the most "relevant" loop for the given SCEV.
2259c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman    const Loop *getRelevantLoop(const SCEV *);
2269c9fcfc719158a46cb2e41b66d7dc1a63cd48d74Dan Gohman
227890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitConstant(const SCEVConstant *S) {
22836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman      return S->getValue();
22936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    }
23036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
231890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitTruncateExpr(const SCEVTruncateExpr *S);
23236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
233890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
23436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
235890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
236d19534add90a2a894af61523b830887097bb780bDan Gohman
237890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitAddExpr(const SCEVAddExpr *S);
23836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
239890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitMulExpr(const SCEVMulExpr *S);
24036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
241890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitUDivExpr(const SCEVUDivExpr *S);
24236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
243890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitAddRecExpr(const SCEVAddRecExpr *S);
24436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
245890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitSMaxExpr(const SCEVSMaxExpr *S);
246c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky
247890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitUMaxExpr(const SCEVUMaxExpr *S);
2483e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky
249890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitUnknown(const SCEVUnknown *S) {
25036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman      return S->getValue();
25136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    }
252a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
2531d826a76f591afea445489b9a5485c345e66bf87Dan Gohman    void rememberInstruction(Value *I);
254a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
255455985501381777db03534c925a35e261e356395Dan Gohman    void restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I);
256455985501381777db03534c925a35e261e356395Dan Gohman
257c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick    bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
258c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick
259365c9f1ff55bef134c6b9707f7df44d680ddabeaAndrew Trick    bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
260c5701910604cdf65811fabd31d41e38f1d1d4eb1Andrew Trick
261a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
262a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
263a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman                                       const Loop *L,
264db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                       Type *ExpandTy,
265db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner                                       Type *IntTy);
266553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick    Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
267553fe05f236f46fe27b7fcfa822b06367d50183eAndrew Trick                       Type *ExpandTy, Type *IntTy, bool useSubtract);
26836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  };
26936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman}
27036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
27136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#endif
272