ScalarEvolutionExpander.h revision 453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9
1//===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the classes used to generate code from scalar expressions. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H 15#define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H 16 17#include "llvm/Instructions.h" 18#include "llvm/Type.h" 19#include "llvm/Analysis/ScalarEvolution.h" 20#include "llvm/Analysis/ScalarEvolutionExpressions.h" 21 22namespace llvm { 23 /// SCEVExpander - This class uses information about analyze scalars to 24 /// rewrite expressions in canonical form. 25 /// 26 /// Clients should create an instance of this class when rewriting is needed, 27 /// and destroy it when finished to allow the release of the associated 28 /// memory. 29 struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> { 30 ScalarEvolution &SE; 31 std::map<SCEVHandle, AssertingVH<Value> > InsertedExpressions; 32 std::set<Value*> InsertedValues; 33 34 BasicBlock::iterator InsertPt; 35 36 friend struct SCEVVisitor<SCEVExpander, Value*>; 37 public: 38 explicit SCEVExpander(ScalarEvolution &se) 39 : SE(se) {} 40 41 /// clear - Erase the contents of the InsertedExpressions map so that users 42 /// trying to expand the same expression into multiple BasicBlocks or 43 /// different places within the same BasicBlock can do so. 44 void clear() { InsertedExpressions.clear(); } 45 46 /// isInsertedInstruction - Return true if the specified instruction was 47 /// inserted by the code rewriter. If so, the client should not modify the 48 /// instruction. 49 bool isInsertedInstruction(Instruction *I) const { 50 return InsertedValues.count(I); 51 } 52 53 /// isInsertedExpression - Return true if the the code rewriter has a 54 /// Value* recorded for the given expression. 55 bool isInsertedExpression(const SCEV *S) const { 56 return InsertedExpressions.count(S); 57 } 58 59 /// getOrInsertCanonicalInductionVariable - This method returns the 60 /// canonical induction variable of the specified type for the specified 61 /// loop (inserting one if there is none). A canonical induction variable 62 /// starts at zero and steps by one on each iteration. 63 Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){ 64 assert(Ty->isInteger() && "Can only insert integer induction variables!"); 65 SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty), 66 SE.getIntegerSCEV(1, Ty), L); 67 return expand(H); 68 } 69 70 /// addInsertedValue - Remember the specified instruction as being the 71 /// canonical form for the specified SCEV. 72 void addInsertedValue(Value *V, const SCEV *S) { 73 InsertedExpressions[S] = V; 74 InsertedValues.insert(V); 75 } 76 77 void setInsertionPoint(BasicBlock::iterator NewIP) { InsertPt = NewIP; } 78 79 BasicBlock::iterator getInsertionPoint() const { return InsertPt; } 80 81 /// expandCodeFor - Insert code to directly compute the specified SCEV 82 /// expression into the program. The inserted code is inserted into the 83 /// SCEVExpander's current insertion point. If a type is specified, the 84 /// result will be expanded to have that type, with a cast if necessary. 85 Value *expandCodeFor(SCEVHandle SH, const Type *Ty = 0); 86 87 /// expandCodeFor - Insert code to directly compute the specified SCEV 88 /// expression into the program. The inserted code is inserted into the 89 /// specified block. 90 Value *expandCodeFor(SCEVHandle SH, const Type *Ty, 91 BasicBlock::iterator IP) { 92 setInsertionPoint(IP); 93 return expandCodeFor(SH, Ty); 94 } 95 96 /// InsertCastOfTo - Insert a cast of V to the specified type, doing what 97 /// we can to share the casts. 98 Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, 99 const Type *Ty); 100 101 /// InsertNoopCastOfTo - Insert a cast of V to the specified type, 102 /// which must be possible with a noop cast. 103 Value *InsertNoopCastOfTo(Value *V, const Type *Ty); 104 105 /// InsertBinop - Insert the specified binary operator, doing a small amount 106 /// of work to avoid inserting an obviously redundant operation. 107 Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, 108 Value *RHS, BasicBlock::iterator InsertPt); 109 110 private: 111 /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP 112 /// instead of using ptrtoint+arithmetic+inttoptr. 113 Value *expandAddToGEP(const SCEVHandle *op_begin, const SCEVHandle *op_end, 114 const PointerType *PTy, const Type *Ty, Value *V); 115 116 Value *expand(const SCEV *S); 117 118 Value *visitConstant(const SCEVConstant *S) { 119 return S->getValue(); 120 } 121 122 Value *visitTruncateExpr(const SCEVTruncateExpr *S); 123 124 Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S); 125 126 Value *visitSignExtendExpr(const SCEVSignExtendExpr *S); 127 128 Value *visitAddExpr(const SCEVAddExpr *S); 129 130 Value *visitMulExpr(const SCEVMulExpr *S); 131 132 Value *visitUDivExpr(const SCEVUDivExpr *S); 133 134 Value *visitAddRecExpr(const SCEVAddRecExpr *S); 135 136 Value *visitSMaxExpr(const SCEVSMaxExpr *S); 137 138 Value *visitUMaxExpr(const SCEVUMaxExpr *S); 139 140 Value *visitUnknown(const SCEVUnknown *S) { 141 return S->getValue(); 142 } 143 }; 144} 145 146#endif 147 148