ScalarEvolutionExpander.h revision 4c0d5d5db876b0628bdf6a2174263a1c0a9130e2
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. 2936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman struct 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 35267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman typedef IRBuilder<true, TargetFolder> BuilderType; 36267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman BuilderType Builder; 3736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 3836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman friend struct SCEVVisitor<SCEVExpander, Value*>; 3936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman public: 405be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman explicit SCEVExpander(ScalarEvolution &se) 41e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson : SE(se), Builder(se.getContext(), 42e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson TargetFolder(se.TD, se.getContext())) {} 438f9f0d3a34ebbcd6d075fbb1250dc74f36579d50Chris Lattner 4436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman /// clear - Erase the contents of the InsertedExpressions map so that users 45d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen /// trying to expand the same expression into multiple BasicBlocks or 4636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman /// different places within the same BasicBlock can do so. 4736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman void clear() { InsertedExpressions.clear(); } 48d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen 4936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman /// getOrInsertCanonicalInductionVariable - This method returns the 5036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman /// canonical induction variable of the specified type for the specified 5136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman /// loop (inserting one if there is none). A canonical induction variable 5236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman /// starts at zero and steps by one on each iteration. 531d09de3eca23267855e28297fcb40de3632ea47bDan Gohman Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty); 5436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 55752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman /// expandCodeFor - Insert code to directly compute the specified SCEV 56752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman /// expression into the program. The inserted code is inserted into the 5736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman /// specified block. 580bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman Value *expandCodeFor(const SCEV *SH, const Type *Ty, Instruction *IP) { 59267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Builder.SetInsertPoint(IP->getParent(), IP); 60752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman return expandCodeFor(SH, Ty); 61752ec7da506f5d41c08bd37e195750b57550ce68Dan Gohman } 6236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 63267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman private: 64e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson LLVMContext &getContext() const { return SE.getContext(); } 654c0d5d5db876b0628bdf6a2174263a1c0a9130e2Dan Gohman 66267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman /// InsertBinop - Insert the specified binary operator, doing a small amount 67267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman /// of work to avoid inserting an obviously redundant operation. 68267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS); 69af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman 70af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman /// InsertNoopCastOfTo - Insert a cast of V to the specified type, 71267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman /// which must be possible with a noop cast, doing what we can to 72267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman /// share the casts. 73af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman Value *InsertNoopCastOfTo(Value *V, const Type *Ty); 74af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman 755be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP 765be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman /// instead of using ptrtoint+arithmetic+inttoptr. 770bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman Value *expandAddToGEP(const SCEV *const *op_begin, 780bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman const SCEV *const *op_end, 79453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman const PointerType *PTy, const Type *Ty, Value *V); 805be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman 81890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *expand(const SCEV *S); 822d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman 83667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman /// expandCodeFor - Insert code to directly compute the specified SCEV 84667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman /// expression into the program. The inserted code is inserted into the 85667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman /// SCEVExpander's current insertion point. If a type is specified, the 86667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman /// result will be expanded to have that type, with a cast if necessary. 870bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman Value *expandCodeFor(const SCEV *SH, const Type *Ty = 0); 88667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman 89667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman /// isInsertedInstruction - Return true if the specified instruction was 90667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman /// inserted by the code rewriter. If so, the client should not modify the 91667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman /// instruction. 92667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman bool isInsertedInstruction(Instruction *I) const { 93667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman return InsertedValues.count(I); 94667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman } 95667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman 96890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitConstant(const SCEVConstant *S) { 9736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman return S->getValue(); 9836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman } 9936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 100890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitTruncateExpr(const SCEVTruncateExpr *S); 10136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 102890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S); 10336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 104890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitSignExtendExpr(const SCEVSignExtendExpr *S); 105d19534add90a2a894af61523b830887097bb780bDan Gohman 106890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitAddExpr(const SCEVAddExpr *S); 10736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 108890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitMulExpr(const SCEVMulExpr *S); 10936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 110890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitUDivExpr(const SCEVUDivExpr *S); 11136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 112890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitAddRecExpr(const SCEVAddRecExpr *S); 11336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 114890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitSMaxExpr(const SCEVSMaxExpr *S); 115c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky 116890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitUMaxExpr(const SCEVUMaxExpr *S); 1173e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky 118c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Value *visitFieldOffsetExpr(const SCEVFieldOffsetExpr *S); 119c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman 120c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman Value *visitAllocSizeExpr(const SCEVAllocSizeExpr *S); 121c40f17b08774c2dcc5787fd83241e3c64ba82974Dan Gohman 122890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman Value *visitUnknown(const SCEVUnknown *S) { 12336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman return S->getValue(); 12436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman } 12536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman }; 12636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman} 12736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman 12836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#endif 129