ScalarEvolutionExpander.h revision 3e6307698084e7adfc10b739442ae29742beefd0
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/BasicBlock.h" 18#include "llvm/Constants.h" 19#include "llvm/Instructions.h" 20#include "llvm/Type.h" 21#include "llvm/Analysis/ScalarEvolution.h" 22#include "llvm/Analysis/ScalarEvolutionExpressions.h" 23#include "llvm/Support/CFG.h" 24 25namespace llvm { 26 /// SCEVExpander - This class uses information about analyze scalars to 27 /// rewrite expressions in canonical form. 28 /// 29 /// Clients should create an instance of this class when rewriting is needed, 30 /// and destroy it when finished to allow the release of the associated 31 /// memory. 32 struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> { 33 ScalarEvolution &SE; 34 LoopInfo &LI; 35 std::map<SCEVHandle, Value*> InsertedExpressions; 36 std::set<Instruction*> InsertedInstructions; 37 38 Instruction *InsertPt; 39 40 friend struct SCEVVisitor<SCEVExpander, Value*>; 41 public: 42 SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {} 43 44 LoopInfo &getLoopInfo() const { return LI; } 45 46 /// clear - Erase the contents of the InsertedExpressions map so that users 47 /// trying to expand the same expression into multiple BasicBlocks or 48 /// different places within the same BasicBlock can do so. 49 void clear() { InsertedExpressions.clear(); } 50 51 /// isInsertedInstruction - Return true if the specified instruction was 52 /// inserted by the code rewriter. If so, the client should not modify the 53 /// instruction. 54 bool isInsertedInstruction(Instruction *I) const { 55 return InsertedInstructions.count(I); 56 } 57 58 /// getOrInsertCanonicalInductionVariable - This method returns the 59 /// canonical induction variable of the specified type for the specified 60 /// loop (inserting one if there is none). A canonical induction variable 61 /// starts at zero and steps by one on each iteration. 62 Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){ 63 assert(Ty->isInteger() && "Can only insert integer induction variables!"); 64 SCEVHandle H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty), 65 SE.getIntegerSCEV(1, Ty), L); 66 return expand(H); 67 } 68 69 /// addInsertedValue - Remember the specified instruction as being the 70 /// canonical form for the specified SCEV. 71 void addInsertedValue(Instruction *I, SCEV *S) { 72 InsertedExpressions[S] = (Value*)I; 73 InsertedInstructions.insert(I); 74 } 75 76 Instruction *getInsertionPoint() const { return InsertPt; } 77 78 /// expandCodeFor - Insert code to directly compute the specified SCEV 79 /// expression into the program. The inserted code is inserted into the 80 /// specified block. 81 Value *expandCodeFor(SCEVHandle SH, Instruction *IP) { 82 // Expand the code for this SCEV. 83 this->InsertPt = IP; 84 return expand(SH); 85 } 86 87 /// InsertCastOfTo - Insert a cast of V to the specified type, doing what 88 /// we can to share the casts. 89 static Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, 90 const Type *Ty); 91 /// InsertBinop - Insert the specified binary operator, doing a small amount 92 /// of work to avoid inserting an obviously redundant operation. 93 static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, 94 Value *RHS, Instruction *&InsertPt); 95 protected: 96 Value *expand(SCEV *S); 97 98 Value *visitConstant(SCEVConstant *S) { 99 return S->getValue(); 100 } 101 102 Value *visitTruncateExpr(SCEVTruncateExpr *S) { 103 Value *V = expand(S->getOperand()); 104 return CastInst::createTruncOrBitCast(V, S->getType(), "tmp.", InsertPt); 105 } 106 107 Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) { 108 Value *V = expand(S->getOperand()); 109 return CastInst::createZExtOrBitCast(V, S->getType(), "tmp.", InsertPt); 110 } 111 112 Value *visitSignExtendExpr(SCEVSignExtendExpr *S) { 113 Value *V = expand(S->getOperand()); 114 return CastInst::createSExtOrBitCast(V, S->getType(), "tmp.", InsertPt); 115 } 116 117 Value *visitAddExpr(SCEVAddExpr *S) { 118 Value *V = expand(S->getOperand(S->getNumOperands()-1)); 119 120 // Emit a bunch of add instructions 121 for (int i = S->getNumOperands()-2; i >= 0; --i) 122 V = InsertBinop(Instruction::Add, V, expand(S->getOperand(i)), 123 InsertPt); 124 return V; 125 } 126 127 Value *visitMulExpr(SCEVMulExpr *S); 128 129 Value *visitUDivExpr(SCEVUDivExpr *S) { 130 Value *LHS = expand(S->getLHS()); 131 Value *RHS = expand(S->getRHS()); 132 return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt); 133 } 134 135 Value *visitAddRecExpr(SCEVAddRecExpr *S); 136 137 Value *visitSMaxExpr(SCEVSMaxExpr *S); 138 139 Value *visitUMaxExpr(SCEVUMaxExpr *S); 140 141 Value *visitUnknown(SCEVUnknown *S) { 142 return S->getValue(); 143 } 144 }; 145} 146 147#endif 148 149