LLVM API Documentation

ScalarEvolutionExpander.h

Go to the documentation of this file.
00001 //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the classes used to generate code from scalar expressions.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
00015 #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
00016 
00017 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
00018 #include "llvm/Support/IRBuilder.h"
00019 #include "llvm/Support/TargetFolder.h"
00020 #include <set>
00021 
00022 namespace llvm {
00023   /// SCEVExpander - This class uses information about analyze scalars to
00024   /// rewrite expressions in canonical form.
00025   ///
00026   /// Clients should create an instance of this class when rewriting is needed,
00027   /// and destroy it when finished to allow the release of the associated
00028   /// memory.
00029   struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
00030     ScalarEvolution &SE;
00031     std::map<std::pair<const SCEV *, Instruction *>, AssertingVH<Value> >
00032       InsertedExpressions;
00033     std::set<Value*> InsertedValues;
00034 
00035     typedef IRBuilder<true, TargetFolder> BuilderType;
00036     BuilderType Builder;
00037 
00038     friend struct SCEVVisitor<SCEVExpander, Value*>;
00039   public:
00040     explicit SCEVExpander(ScalarEvolution &se)
00041       : SE(se), Builder(se.getContext(),
00042                         TargetFolder(se.TD, se.getContext())) {}
00043 
00044     /// clear - Erase the contents of the InsertedExpressions map so that users
00045     /// trying to expand the same expression into multiple BasicBlocks or
00046     /// different places within the same BasicBlock can do so.
00047     void clear() { InsertedExpressions.clear(); }
00048 
00049     /// getOrInsertCanonicalInductionVariable - This method returns the
00050     /// canonical induction variable of the specified type for the specified
00051     /// loop (inserting one if there is none).  A canonical induction variable
00052     /// starts at zero and steps by one on each iteration.
00053     Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty);
00054 
00055     /// expandCodeFor - Insert code to directly compute the specified SCEV
00056     /// expression into the program.  The inserted code is inserted into the
00057     /// specified block.
00058     Value *expandCodeFor(const SCEV *SH, const Type *Ty, Instruction *IP) {
00059       Builder.SetInsertPoint(IP->getParent(), IP);
00060       return expandCodeFor(SH, Ty);
00061     }
00062 
00063   private:
00064     LLVMContext &getContext() const { return SE.getContext(); }
00065 
00066     /// InsertBinop - Insert the specified binary operator, doing a small amount
00067     /// of work to avoid inserting an obviously redundant operation.
00068     Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS);
00069 
00070     /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
00071     /// which must be possible with a noop cast, doing what we can to
00072     /// share the casts.
00073     Value *InsertNoopCastOfTo(Value *V, const Type *Ty);
00074 
00075     /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
00076     /// instead of using ptrtoint+arithmetic+inttoptr.
00077     Value *expandAddToGEP(const SCEV *const *op_begin,
00078                           const SCEV *const *op_end,
00079                           const PointerType *PTy, const Type *Ty, Value *V);
00080 
00081     Value *expand(const SCEV *S);
00082 
00083     /// expandCodeFor - Insert code to directly compute the specified SCEV
00084     /// expression into the program.  The inserted code is inserted into the
00085     /// SCEVExpander's current insertion point. If a type is specified, the
00086     /// result will be expanded to have that type, with a cast if necessary.
00087     Value *expandCodeFor(const SCEV *SH, const Type *Ty = 0);
00088 
00089     /// isInsertedInstruction - Return true if the specified instruction was
00090     /// inserted by the code rewriter.  If so, the client should not modify the
00091     /// instruction.
00092     bool isInsertedInstruction(Instruction *I) const {
00093       return InsertedValues.count(I);
00094     }
00095 
00096     Value *visitConstant(const SCEVConstant *S) {
00097       return S->getValue();
00098     }
00099 
00100     Value *visitTruncateExpr(const SCEVTruncateExpr *S);
00101 
00102     Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S);
00103 
00104     Value *visitSignExtendExpr(const SCEVSignExtendExpr *S);
00105 
00106     Value *visitAddExpr(const SCEVAddExpr *S);
00107 
00108     Value *visitMulExpr(const SCEVMulExpr *S);
00109 
00110     Value *visitUDivExpr(const SCEVUDivExpr *S);
00111 
00112     Value *visitAddRecExpr(const SCEVAddRecExpr *S);
00113 
00114     Value *visitSMaxExpr(const SCEVSMaxExpr *S);
00115 
00116     Value *visitUMaxExpr(const SCEVUMaxExpr *S);
00117 
00118     Value *visitFieldOffsetExpr(const SCEVFieldOffsetExpr *S);
00119 
00120     Value *visitAllocSizeExpr(const SCEVAllocSizeExpr *S);
00121 
00122     Value *visitUnknown(const SCEVUnknown *S) {
00123       return S->getValue();
00124     }
00125   };
00126 }
00127 
00128 #endif