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/Instruction.h"
00018 #include "llvm/Type.h"
00019 #include "llvm/Analysis/ScalarEvolution.h"
00020 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
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     LoopInfo &LI;
00032     std::map<SCEVHandle, Value*> InsertedExpressions;
00033     std::set<Instruction*> InsertedInstructions;
00034 
00035     Instruction *InsertPt;
00036 
00037     friend struct SCEVVisitor<SCEVExpander, Value*>;
00038   public:
00039     SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {}
00040 
00041     LoopInfo &getLoopInfo() const { return LI; }
00042 
00043     /// clear - Erase the contents of the InsertedExpressions map so that users
00044     /// trying to expand the same expression into multiple BasicBlocks or
00045     /// different places within the same BasicBlock can do so.
00046     void clear() { InsertedExpressions.clear(); }
00047 
00048     /// isInsertedInstruction - Return true if the specified instruction was
00049     /// inserted by the code rewriter.  If so, the client should not modify the
00050     /// instruction.
00051     bool isInsertedInstruction(Instruction *I) const {
00052       return InsertedInstructions.count(I);
00053     }
00054 
00055     /// getOrInsertCanonicalInductionVariable - This method returns the
00056     /// canonical induction variable of the specified type for the specified
00057     /// loop (inserting one if there is none).  A canonical induction variable
00058     /// starts at zero and steps by one on each iteration.
00059     Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){
00060       assert(Ty->isInteger() && "Can only insert integer induction variables!");
00061       SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
00062                                       SE.getIntegerSCEV(1, Ty), L);
00063       return expand(H);
00064     }
00065 
00066     /// addInsertedValue - Remember the specified instruction as being the
00067     /// canonical form for the specified SCEV.
00068     void addInsertedValue(Instruction *I, SCEV *S) {
00069       InsertedExpressions[S] = (Value*)I;
00070       InsertedInstructions.insert(I);
00071     }
00072 
00073     Instruction *getInsertionPoint() const { return InsertPt; }
00074     
00075     /// expandCodeFor - Insert code to directly compute the specified SCEV
00076     /// expression into the program.  The inserted code is inserted into the
00077     /// specified block.
00078     Value *expandCodeFor(SCEVHandle SH, Instruction *IP);
00079 
00080     /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
00081     /// we can to share the casts.
00082     static Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, 
00083                                  const Type *Ty);
00084     /// InsertBinop - Insert the specified binary operator, doing a small amount
00085     /// of work to avoid inserting an obviously redundant operation.
00086     static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
00087                               Value *RHS, Instruction *InsertPt);
00088   protected:
00089     Value *expand(SCEV *S);
00090     
00091     Value *visitConstant(SCEVConstant *S) {
00092       return S->getValue();
00093     }
00094 
00095     Value *visitTruncateExpr(SCEVTruncateExpr *S);
00096 
00097     Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S);
00098 
00099     Value *visitSignExtendExpr(SCEVSignExtendExpr *S);
00100 
00101     Value *visitAddExpr(SCEVAddExpr *S);
00102 
00103     Value *visitMulExpr(SCEVMulExpr *S);
00104 
00105     Value *visitUDivExpr(SCEVUDivExpr *S);
00106 
00107     Value *visitSDivExpr(SCEVSDivExpr *S);
00108 
00109     Value *visitAddRecExpr(SCEVAddRecExpr *S);
00110 
00111     Value *visitSMaxExpr(SCEVSMaxExpr *S);
00112 
00113     Value *visitUMaxExpr(SCEVUMaxExpr *S);
00114 
00115     Value *visitUnknown(SCEVUnknown *S) {
00116       return S->getValue();
00117     }
00118   };
00119 }
00120 
00121 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.