ScalarEvolutionExpander.h revision 9269926bfb713e92e328e9578c6d8826c68dcb9d
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
1436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
1536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
1636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
1736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#include "llvm/Analysis/ScalarEvolutionExpressions.h"
18267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman#include "llvm/Support/IRBuilder.h"
19267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman#include "llvm/Support/TargetFolder.h"
2003ee68a145ab5394c070298049d93f305be93ec3Dan Gohman#include <set>
2136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
2236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begemannamespace llvm {
2336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  /// SCEVExpander - This class uses information about analyze scalars to
2436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  /// rewrite expressions in canonical form.
2536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  ///
2636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  /// Clients should create an instance of this class when rewriting is needed,
274c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman  /// and destroy it when finished to allow the release of the associated
2836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  /// memory.
29a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman  class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
3036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    ScalarEvolution &SE;
31667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    std::map<std::pair<const SCEV *, Instruction *>, AssertingVH<Value> >
32667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman      InsertedExpressions;
3399a1302ae4c438ab532826685280c0b69687e163Dan Gohman    std::set<Value*> InsertedValues;
3436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
35a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// PostIncLoop - When non-null, expanded addrecs referring to the given
36a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// loop expanded in post-inc mode. For example, expanding {1,+,1}<L> in
37a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// post-inc mode returns the add instruction that adds one to the phi
38a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// for {0,+,1}<L>, as opposed to a new phi starting at 1. This is only
39a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// supported in non-canonical mode.
40a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    const Loop *PostIncLoop;
41a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
42a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// IVIncInsertPos - When this is non-null, addrecs expanded in the
43a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// loop it indicates should be inserted with increments at
44a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// IVIncInsertPos.
45a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    const Loop *IVIncInsertLoop;
46a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
47a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// IVIncInsertPos - When expanding addrecs in the IVIncInsertLoop loop,
48a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// insert the IV increment at this position.
49a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    Instruction *IVIncInsertPos;
50a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
51a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// CanonicalMode - When true, expressions are expanded in "canonical"
52a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// form. In particular, addrecs are expanded as arithmetic based on
53a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// a canonical induction variable. When false, expression are expanded
54a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// in a more literal form.
55a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    bool CanonicalMode;
56a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
57267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    typedef IRBuilder<true, TargetFolder> BuilderType;
58267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    BuilderType Builder;
5936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
6036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    friend struct SCEVVisitor<SCEVExpander, Value*>;
619269926bfb713e92e328e9578c6d8826c68dcb9dDan Gohman
6236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  public:
63a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// SCEVExpander - Construct a SCEVExpander in "canonical" mode.
645be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    explicit SCEVExpander(ScalarEvolution &se)
65a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      : SE(se), PostIncLoop(0), IVIncInsertLoop(0), CanonicalMode(true),
66a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman        Builder(se.getContext(), TargetFolder(se.TD)) {}
678f9f0d3a34ebbcd6d075fbb1250dc74f36579d50Chris Lattner
6836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// clear - Erase the contents of the InsertedExpressions map so that users
69d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen    /// trying to expand the same expression into multiple BasicBlocks or
7036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// different places within the same BasicBlock can do so.
7136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    void clear() { InsertedExpressions.clear(); }
72d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
7336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// getOrInsertCanonicalInductionVariable - This method returns the
7436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// canonical induction variable of the specified type for the specified
7536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// loop (inserting one if there is none).  A canonical induction variable
7636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// starts at zero and steps by one on each iteration.
771d09de3eca23267855e28297fcb40de3632ea47bDan Gohman    Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty);
7836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
79752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman    /// expandCodeFor - Insert code to directly compute the specified SCEV
80752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman    /// expression into the program.  The inserted code is inserted into the
8136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    /// specified block.
82a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    Value *expandCodeFor(const SCEV *SH, const Type *Ty, Instruction *I) {
83a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      BasicBlock::iterator IP = I;
84a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      while (isInsertedInstruction(IP)) ++IP;
85267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman      Builder.SetInsertPoint(IP->getParent(), IP);
86752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman      return expandCodeFor(SH, Ty);
87752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman    }
8836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
89a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// setIVIncInsertPos - Set the current IV increment loop and position.
90a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    void setIVIncInsertPos(const Loop *L, Instruction *Pos) {
91a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      assert(!CanonicalMode &&
92a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman             "IV increment positions are not supported in CanonicalMode");
93a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      IVIncInsertLoop = L;
94a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      IVIncInsertPos = Pos;
95a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    }
96a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
97a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// setPostInc - If L is non-null, enable post-inc expansion for addrecs
98a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// referring to the given loop. If L is null, disable post-inc expansion
99a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// completely. Post-inc expansion is only supported in non-canonical
100a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// mode.
101a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    void setPostInc(const Loop *L) {
102a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      assert(!CanonicalMode &&
103a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman             "Post-inc expansion is not supported in CanonicalMode");
104a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      PostIncLoop = L;
105a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    }
106a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
107a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// disableCanonicalMode - Disable the behavior of expanding expressions in
108a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// canonical form rather than in a more literal form. Non-canonical mode
109a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    /// is useful for late optimization passes.
110a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    void disableCanonicalMode() { CanonicalMode = false; }
111a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
112267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  private:
113e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson    LLVMContext &getContext() const { return SE.getContext(); }
1144c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman
115267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    /// InsertBinop - Insert the specified binary operator, doing a small amount
116267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    /// of work to avoid inserting an obviously redundant operation.
117267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
118af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman
119af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
120267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    /// which must be possible with a noop cast, doing what we can to
121267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    /// share the casts.
122af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
123af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman
1245be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
1255be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    /// instead of using ptrtoint+arithmetic+inttoptr.
1260bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    Value *expandAddToGEP(const SCEV *const *op_begin,
1270bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman                          const SCEV *const *op_end,
128453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                          const PointerType *PTy, const Type *Ty, Value *V);
1295be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
130890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *expand(const SCEV *S);
1312d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman
132667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// expandCodeFor - Insert code to directly compute the specified SCEV
133667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// expression into the program.  The inserted code is inserted into the
134667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// SCEVExpander's current insertion point. If a type is specified, the
135667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// result will be expanded to have that type, with a cast if necessary.
1360bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    Value *expandCodeFor(const SCEV *SH, const Type *Ty = 0);
137667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman
138667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// isInsertedInstruction - Return true if the specified instruction was
139667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// inserted by the code rewriter.  If so, the client should not modify the
140667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    /// instruction.
141667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    bool isInsertedInstruction(Instruction *I) const {
142667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman      return InsertedValues.count(I);
143667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    }
144667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman
145890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitConstant(const SCEVConstant *S) {
14636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman      return S->getValue();
14736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    }
14836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
149890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitTruncateExpr(const SCEVTruncateExpr *S);
15036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
151890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
15236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
153890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
154d19534add90a2a894af61523b830887097bb780bDan Gohman
155890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitAddExpr(const SCEVAddExpr *S);
15636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
157890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitMulExpr(const SCEVMulExpr *S);
15836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
159890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitUDivExpr(const SCEVUDivExpr *S);
16036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
161890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitAddRecExpr(const SCEVAddRecExpr *S);
16236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
163890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitSMaxExpr(const SCEVSMaxExpr *S);
164c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky
165890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitUMaxExpr(const SCEVUMaxExpr *S);
1663e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky
167890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman    Value *visitUnknown(const SCEVUnknown *S) {
16836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman      return S->getValue();
16936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    }
170a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
171a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    void rememberInstruction(Value *I) {
172a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      if (!PostIncLoop) InsertedValues.insert(I);
173a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    }
174a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
175a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
176a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
177a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman                                       const Loop *L,
178a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman                                       const Type *ExpandTy,
179a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman                                       const Type *IntTy);
18036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  };
18136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman}
18236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
18336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#endif
184