Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 1 | //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the implementation of the scalar evolution expander, |
| 11 | // which is used to generate the code corresponding to a given scalar evolution |
| 12 | // expression. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopInfo.h" |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetData.h" |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 21 | /// InsertCastOfTo - Insert a cast of V to the specified type, doing what |
| 22 | /// we can to share the casts. |
Reid Spencer | 3ba68b9 | 2006-12-13 08:06:42 +0000 | [diff] [blame] | 23 | Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, |
| 24 | const Type *Ty) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 25 | // Short-circuit unnecessary bitcasts. |
| 26 | if (opcode == Instruction::BitCast && V->getType() == Ty) |
| 27 | return V; |
| 28 | |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 29 | // Short-circuit unnecessary inttoptr<->ptrtoint casts. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 30 | if ((opcode == Instruction::PtrToInt || opcode == Instruction::IntToPtr) && |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 31 | SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 32 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 33 | if ((CI->getOpcode() == Instruction::PtrToInt || |
| 34 | CI->getOpcode() == Instruction::IntToPtr) && |
| 35 | SE.getTypeSizeInBits(CI->getType()) == |
| 36 | SE.getTypeSizeInBits(CI->getOperand(0)->getType())) |
| 37 | return CI->getOperand(0); |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 38 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 39 | if ((CE->getOpcode() == Instruction::PtrToInt || |
| 40 | CE->getOpcode() == Instruction::IntToPtr) && |
| 41 | SE.getTypeSizeInBits(CE->getType()) == |
| 42 | SE.getTypeSizeInBits(CE->getOperand(0)->getType())) |
| 43 | return CE->getOperand(0); |
| 44 | } |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 45 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 46 | // FIXME: keep track of the cast instruction. |
| 47 | if (Constant *C = dyn_cast<Constant>(V)) |
Reid Spencer | d977d86 | 2006-12-12 23:36:14 +0000 | [diff] [blame] | 48 | return ConstantExpr::getCast(opcode, C, Ty); |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 49 | |
| 50 | if (Argument *A = dyn_cast<Argument>(V)) { |
| 51 | // Check to see if there is already a cast! |
| 52 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); |
| 53 | UI != E; ++UI) { |
| 54 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 55 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
| 56 | if (CI->getOpcode() == opcode) { |
| 57 | // If the cast isn't the first instruction of the function, move it. |
| 58 | if (BasicBlock::iterator(CI) != |
| 59 | A->getParent()->getEntryBlock().begin()) { |
Dan Gohman | aabb04f | 2009-04-22 16:11:16 +0000 | [diff] [blame] | 60 | // If the CastInst is the insert point, change the insert point. |
| 61 | if (CI == InsertPt) ++InsertPt; |
| 62 | // Splice the cast at the beginning of the entry block. |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 63 | CI->moveBefore(A->getParent()->getEntryBlock().begin()); |
| 64 | } |
| 65 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 67 | } |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 68 | Instruction *I = CastInst::Create(opcode, V, Ty, V->getName(), |
| 69 | A->getParent()->getEntryBlock().begin()); |
| 70 | InsertedValues.insert(I); |
| 71 | return I; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 72 | } |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 73 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 74 | Instruction *I = cast<Instruction>(V); |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 75 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 76 | // Check to see if there is already a cast. If there is, use it. |
| 77 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 78 | UI != E; ++UI) { |
| 79 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 80 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
| 81 | if (CI->getOpcode() == opcode) { |
| 82 | BasicBlock::iterator It = I; ++It; |
| 83 | if (isa<InvokeInst>(I)) |
| 84 | It = cast<InvokeInst>(I)->getNormalDest()->begin(); |
| 85 | while (isa<PHINode>(It)) ++It; |
| 86 | if (It != BasicBlock::iterator(CI)) { |
Dan Gohman | aabb04f | 2009-04-22 16:11:16 +0000 | [diff] [blame] | 87 | // If the CastInst is the insert point, change the insert point. |
| 88 | if (CI == InsertPt) ++InsertPt; |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 89 | // Splice the cast immediately after the operand in question. |
| 90 | CI->moveBefore(It); |
| 91 | } |
| 92 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 93 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 94 | } |
| 95 | BasicBlock::iterator IP = I; ++IP; |
| 96 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 97 | IP = II->getNormalDest()->begin(); |
| 98 | while (isa<PHINode>(IP)) ++IP; |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 99 | Instruction *CI = CastInst::Create(opcode, V, Ty, V->getName(), IP); |
| 100 | InsertedValues.insert(CI); |
| 101 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 104 | /// InsertNoopCastOfTo - Insert a cast of V to the specified type, |
| 105 | /// which must be possible with a noop cast. |
| 106 | Value *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) { |
| 107 | Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false); |
| 108 | assert((Op == Instruction::BitCast || |
Devang Patel | e2a1746 | 2009-04-22 18:51:05 +0000 | [diff] [blame] | 109 | Op == Instruction::PtrToInt || |
| 110 | Op == Instruction::IntToPtr) && |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 111 | "InsertNoopCastOfTo cannot perform non-noop casts!"); |
| 112 | assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) && |
| 113 | "InsertNoopCastOfTo cannot change sizes!"); |
| 114 | return InsertCastOfTo(Op, V, Ty); |
| 115 | } |
| 116 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 117 | /// InsertBinop - Insert the specified binary operator, doing a small amount |
| 118 | /// of work to avoid inserting an obviously redundant operation. |
| 119 | Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, |
Dan Gohman | 6cdc727 | 2009-04-22 16:05:50 +0000 | [diff] [blame] | 120 | Value *RHS, BasicBlock::iterator InsertPt) { |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 121 | // Fold a binop with constant operands. |
| 122 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 123 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 124 | return ConstantExpr::get(Opcode, CLHS, CRHS); |
| 125 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 126 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 127 | unsigned ScanLimit = 6; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 128 | BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin(); |
| 129 | if (InsertPt != BlockBegin) { |
| 130 | // Scanning starts from the last instruction before InsertPt. |
| 131 | BasicBlock::iterator IP = InsertPt; |
| 132 | --IP; |
| 133 | for (; ScanLimit; --IP, --ScanLimit) { |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 134 | if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS && |
| 135 | IP->getOperand(1) == RHS) |
| 136 | return IP; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 137 | if (IP == BlockBegin) break; |
| 138 | } |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 139 | } |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 140 | |
| 141 | // If we haven't found this binop, insert it. |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 142 | Instruction *BO = BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt); |
| 143 | InsertedValues.insert(BO); |
| 144 | return BO; |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 147 | /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP |
| 148 | /// instead of using ptrtoint+arithmetic+inttoptr. |
| 149 | Value *SCEVExpander::expandAddToGEP(const SCEVAddExpr *S, |
| 150 | const PointerType *PTy, |
| 151 | const Type *Ty, |
| 152 | Value *V) { |
| 153 | const Type *ElTy = PTy->getElementType(); |
| 154 | SmallVector<Value *, 4> GepIndices; |
| 155 | std::vector<SCEVHandle> Ops = S->getOperands(); |
| 156 | bool AnyNonZeroIndices = false; |
| 157 | Ops.pop_back(); |
| 158 | |
| 159 | // Decend down the pointer's type and attempt to convert the other |
| 160 | // operands into GEP indices, at each level. The first index in a GEP |
| 161 | // indexes into the array implied by the pointer operand; the rest of |
| 162 | // the indices index into the element or field type selected by the |
| 163 | // preceding index. |
| 164 | for (;;) { |
| 165 | APInt ElSize = APInt(SE.getTypeSizeInBits(Ty), |
| 166 | ElTy->isSized() ? SE.TD->getTypeAllocSize(ElTy) : 0); |
| 167 | std::vector<SCEVHandle> NewOps; |
| 168 | std::vector<SCEVHandle> ScaledOps; |
| 169 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 170 | if (ElSize != 0) { |
Dan Gohman | 0d56b06 | 2009-05-22 07:14:20 +0000 | [diff] [blame] | 171 | // For a Constant, check for a multiple of the pointer type's |
| 172 | // scale size. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 173 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) |
| 174 | if (!C->getValue()->getValue().srem(ElSize)) { |
| 175 | ConstantInt *CI = |
| 176 | ConstantInt::get(C->getValue()->getValue().sdiv(ElSize)); |
| 177 | SCEVHandle Div = SE.getConstant(CI); |
| 178 | ScaledOps.push_back(Div); |
| 179 | continue; |
| 180 | } |
Dan Gohman | 0d56b06 | 2009-05-22 07:14:20 +0000 | [diff] [blame] | 181 | // In a Mul, check if there is a constant operand which is a multiple |
| 182 | // of the pointer type's scale size. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 183 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) |
| 184 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0))) |
Dan Gohman | 0d56b06 | 2009-05-22 07:14:20 +0000 | [diff] [blame] | 185 | if (!C->getValue()->getValue().srem(ElSize)) { |
| 186 | std::vector<SCEVHandle> NewMulOps(M->getOperands()); |
| 187 | NewMulOps[0] = |
| 188 | SE.getConstant(C->getValue()->getValue().sdiv(ElSize)); |
| 189 | ScaledOps.push_back(SE.getMulExpr(NewMulOps)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 190 | continue; |
| 191 | } |
Dan Gohman | 0d56b06 | 2009-05-22 07:14:20 +0000 | [diff] [blame] | 192 | // In an Unknown, check if the underlying value is a Mul by a constant |
| 193 | // which is equal to the pointer type's scale size. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 194 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Ops[i])) |
| 195 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getValue())) |
| 196 | if (BO->getOpcode() == Instruction::Mul) |
| 197 | if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) |
| 198 | if (CI->getValue() == ElSize) { |
| 199 | ScaledOps.push_back(SE.getUnknown(BO->getOperand(0))); |
| 200 | continue; |
| 201 | } |
Dan Gohman | 0d56b06 | 2009-05-22 07:14:20 +0000 | [diff] [blame] | 202 | // If the pointer type's scale size is 1, no scaling is necessary |
| 203 | // and any value can be used. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 204 | if (ElSize == 1) { |
| 205 | ScaledOps.push_back(Ops[i]); |
| 206 | continue; |
| 207 | } |
| 208 | } |
| 209 | NewOps.push_back(Ops[i]); |
| 210 | } |
| 211 | Ops = NewOps; |
| 212 | AnyNonZeroIndices |= !ScaledOps.empty(); |
| 213 | Value *Scaled = ScaledOps.empty() ? |
| 214 | Constant::getNullValue(Ty) : |
| 215 | expandCodeFor(SE.getAddExpr(ScaledOps), Ty); |
| 216 | GepIndices.push_back(Scaled); |
| 217 | |
| 218 | // Collect struct field index operands. |
| 219 | if (!Ops.empty()) |
| 220 | while (const StructType *STy = dyn_cast<StructType>(ElTy)) { |
| 221 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0])) |
| 222 | if (SE.getTypeSizeInBits(C->getType()) <= 64) { |
| 223 | const StructLayout &SL = *SE.TD->getStructLayout(STy); |
| 224 | uint64_t FullOffset = C->getValue()->getZExtValue(); |
| 225 | if (FullOffset < SL.getSizeInBytes()) { |
| 226 | unsigned ElIdx = SL.getElementContainingOffset(FullOffset); |
| 227 | GepIndices.push_back(ConstantInt::get(Type::Int32Ty, ElIdx)); |
| 228 | ElTy = STy->getTypeAtIndex(ElIdx); |
| 229 | Ops[0] = |
| 230 | SE.getConstant(ConstantInt::get(Ty, |
| 231 | FullOffset - |
| 232 | SL.getElementOffset(ElIdx))); |
| 233 | AnyNonZeroIndices = true; |
| 234 | continue; |
| 235 | } |
| 236 | } |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | if (const ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) { |
| 241 | ElTy = ATy->getElementType(); |
| 242 | continue; |
| 243 | } |
| 244 | break; |
| 245 | } |
| 246 | |
| 247 | // If none of the operands were convertable to proper GEP indices, cast |
| 248 | // the base to i8* and do an ugly getelementptr with that. It's still |
| 249 | // better than ptrtoint+arithmetic+inttoptr at least. |
| 250 | if (!AnyNonZeroIndices) { |
| 251 | V = InsertNoopCastOfTo(V, |
| 252 | Type::Int8Ty->getPointerTo(PTy->getAddressSpace())); |
| 253 | Value *Idx = expand(SE.getAddExpr(Ops)); |
| 254 | Idx = InsertNoopCastOfTo(Idx, Ty); |
| 255 | |
| 256 | // Fold a GEP with constant operands. |
| 257 | if (Constant *CLHS = dyn_cast<Constant>(V)) |
| 258 | if (Constant *CRHS = dyn_cast<Constant>(Idx)) |
Dan Gohman | 278b49a | 2009-05-19 19:18:01 +0000 | [diff] [blame] | 259 | return ConstantExpr::getGetElementPtr(CLHS, &CRHS, 1); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 260 | |
| 261 | // Do a quick scan to see if we have this GEP nearby. If so, reuse it. |
| 262 | unsigned ScanLimit = 6; |
| 263 | BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin(); |
| 264 | if (InsertPt != BlockBegin) { |
| 265 | // Scanning starts from the last instruction before InsertPt. |
| 266 | BasicBlock::iterator IP = InsertPt; |
| 267 | --IP; |
| 268 | for (; ScanLimit; --IP, --ScanLimit) { |
| 269 | if (IP->getOpcode() == Instruction::GetElementPtr && |
| 270 | IP->getOperand(0) == V && IP->getOperand(1) == Idx) |
| 271 | return IP; |
| 272 | if (IP == BlockBegin) break; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | Value *GEP = GetElementPtrInst::Create(V, Idx, "scevgep", InsertPt); |
| 277 | InsertedValues.insert(GEP); |
| 278 | return GEP; |
| 279 | } |
| 280 | |
| 281 | // Insert a pretty getelementptr. |
| 282 | Value *GEP = GetElementPtrInst::Create(V, |
| 283 | GepIndices.begin(), |
| 284 | GepIndices.end(), |
| 285 | "scevgep", InsertPt); |
| 286 | Ops.push_back(SE.getUnknown(GEP)); |
| 287 | InsertedValues.insert(GEP); |
| 288 | return expand(SE.getAddExpr(Ops)); |
| 289 | } |
| 290 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 291 | Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 292 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 293 | Value *V = expand(S->getOperand(S->getNumOperands()-1)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 294 | |
| 295 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. This helps |
| 296 | // BasicAliasAnalysis analyze the result. However, it suffers from the |
| 297 | // underlying bug described in PR2831. Addition in LLVM currently always |
| 298 | // has two's complement wrapping guaranteed. However, the semantics for |
| 299 | // getelementptr overflow are ambiguous. In the common case though, this |
| 300 | // expansion gets used when a GEP in the original code has been converted |
| 301 | // into integer arithmetic, in which case the resulting code will be no |
| 302 | // more undefined than it was originally. |
| 303 | if (SE.TD) |
| 304 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) |
| 305 | return expandAddToGEP(S, PTy, Ty, V); |
| 306 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 307 | V = InsertNoopCastOfTo(V, Ty); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 308 | |
| 309 | // Emit a bunch of add instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 310 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 311 | Value *W = expand(S->getOperand(i)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 312 | W = InsertNoopCastOfTo(W, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 313 | V = InsertBinop(Instruction::Add, V, W, InsertPt); |
| 314 | } |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 315 | return V; |
| 316 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 317 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 318 | Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 319 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 320 | int FirstOp = 0; // Set if we should emit a subtract. |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 321 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 322 | if (SC->getValue()->isAllOnesValue()) |
| 323 | FirstOp = 1; |
| 324 | |
| 325 | int i = S->getNumOperands()-2; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 326 | Value *V = expand(S->getOperand(i+1)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 327 | V = InsertNoopCastOfTo(V, Ty); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 328 | |
| 329 | // Emit a bunch of multiply instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 330 | for (; i >= FirstOp; --i) { |
| 331 | Value *W = expand(S->getOperand(i)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 332 | W = InsertNoopCastOfTo(W, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 333 | V = InsertBinop(Instruction::Mul, V, W, InsertPt); |
| 334 | } |
| 335 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 336 | // -1 * ... ---> 0 - ... |
| 337 | if (FirstOp == 1) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 338 | V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 339 | return V; |
| 340 | } |
| 341 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 342 | Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 343 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 344 | |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 345 | Value *LHS = expand(S->getLHS()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 346 | LHS = InsertNoopCastOfTo(LHS, Ty); |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 347 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 348 | const APInt &RHS = SC->getValue()->getValue(); |
| 349 | if (RHS.isPowerOf2()) |
| 350 | return InsertBinop(Instruction::LShr, LHS, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 351 | ConstantInt::get(Ty, RHS.logBase2()), |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 352 | InsertPt); |
| 353 | } |
| 354 | |
| 355 | Value *RHS = expand(S->getRHS()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 356 | RHS = InsertNoopCastOfTo(RHS, Ty); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 357 | return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt); |
| 358 | } |
| 359 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 360 | Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 361 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 362 | const Loop *L = S->getLoop(); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 363 | |
| 364 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 365 | if (!S->getStart()->isZero()) { |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 366 | std::vector<SCEVHandle> NewOps(S->getOperands()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 367 | NewOps[0] = SE.getIntegerSCEV(0, Ty); |
| 368 | Value *Rest = expand(SE.getAddRecExpr(NewOps, L)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 369 | return expand(SE.getAddExpr(S->getStart(), SE.getUnknown(Rest))); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
Dan Gohman | 17f1972 | 2008-06-22 19:23:09 +0000 | [diff] [blame] | 373 | if (S->isAffine() && |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 374 | S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) { |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 375 | // Create and insert the PHI node for the induction variable in the |
| 376 | // specified loop. |
| 377 | BasicBlock *Header = L->getHeader(); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 378 | PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin()); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 379 | InsertedValues.insert(PN); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 380 | PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader()); |
| 381 | |
| 382 | pred_iterator HPI = pred_begin(Header); |
| 383 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 384 | if (!L->contains(*HPI)) ++HPI; |
| 385 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 386 | "No backedge in loop?"); |
| 387 | |
| 388 | // Insert a unit add instruction right before the terminator corresponding |
| 389 | // to the back-edge. |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 390 | Constant *One = ConstantInt::get(Ty, 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 391 | Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next", |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 392 | (*HPI)->getTerminator()); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 393 | InsertedValues.insert(Add); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 394 | |
| 395 | pred_iterator PI = pred_begin(Header); |
| 396 | if (*PI == L->getLoopPreheader()) |
| 397 | ++PI; |
| 398 | PN->addIncoming(Add, *PI); |
| 399 | return PN; |
| 400 | } |
| 401 | |
| 402 | // Get the canonical induction variable I for this loop. |
| 403 | Value *I = getOrInsertCanonicalInductionVariable(L, Ty); |
| 404 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 405 | // If this is a simple linear addrec, emit it now as a special case. |
Dan Gohman | 17f1972 | 2008-06-22 19:23:09 +0000 | [diff] [blame] | 406 | if (S->isAffine()) { // {0,+,F} --> i*F |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 407 | Value *F = expand(S->getOperand(1)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 408 | F = InsertNoopCastOfTo(F, Ty); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 409 | |
| 410 | // IF the step is by one, just return the inserted IV. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 411 | if (ConstantInt *CI = dyn_cast<ConstantInt>(F)) |
Reid Spencer | 4d050d7 | 2007-03-01 19:45:00 +0000 | [diff] [blame] | 412 | if (CI->getValue() == 1) |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 413 | return I; |
| 414 | |
| 415 | // If the insert point is directly inside of the loop, emit the multiply at |
| 416 | // the insert point. Otherwise, L is a loop that is a parent of the insert |
| 417 | // point loop. If we can, move the multiply to the outer most loop that it |
| 418 | // is safe to be in. |
Dan Gohman | 6cdc727 | 2009-04-22 16:05:50 +0000 | [diff] [blame] | 419 | BasicBlock::iterator MulInsertPt = getInsertionPoint(); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 420 | Loop *InsertPtLoop = SE.LI->getLoopFor(MulInsertPt->getParent()); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 421 | if (InsertPtLoop != L && InsertPtLoop && |
| 422 | L->contains(InsertPtLoop->getHeader())) { |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 423 | do { |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 424 | // If we cannot hoist the multiply out of this loop, don't. |
| 425 | if (!InsertPtLoop->isLoopInvariant(F)) break; |
| 426 | |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 427 | BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader(); |
| 428 | |
| 429 | // If this loop hasn't got a preheader, we aren't able to hoist the |
| 430 | // multiply. |
| 431 | if (!InsertPtLoopPH) |
| 432 | break; |
| 433 | |
| 434 | // Otherwise, move the insert point to the preheader. |
| 435 | MulInsertPt = InsertPtLoopPH->getTerminator(); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 436 | InsertPtLoop = InsertPtLoop->getParentLoop(); |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 437 | } while (InsertPtLoop != L); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 440 | return InsertBinop(Instruction::Mul, I, F, MulInsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 444 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 445 | // simplify the expression without having to build a bunch of special code |
| 446 | // into this folder. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 447 | SCEVHandle IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 448 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 449 | SCEVHandle V = S->evaluateAtIteration(IH, SE); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 450 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 451 | |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 452 | return expand(V); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 453 | } |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 454 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 455 | Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 456 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 457 | Value *V = expand(S->getOperand()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 458 | V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 459 | Instruction *I = new TruncInst(V, Ty, "tmp.", InsertPt); |
| 460 | InsertedValues.insert(I); |
| 461 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 462 | } |
| 463 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 464 | Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 465 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 466 | Value *V = expand(S->getOperand()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 467 | V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 468 | Instruction *I = new ZExtInst(V, Ty, "tmp.", InsertPt); |
| 469 | InsertedValues.insert(I); |
| 470 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 473 | Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 474 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 475 | Value *V = expand(S->getOperand()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 476 | V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 477 | Instruction *I = new SExtInst(V, Ty, "tmp.", InsertPt); |
| 478 | InsertedValues.insert(I); |
| 479 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 480 | } |
| 481 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 482 | Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 483 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 484 | Value *LHS = expand(S->getOperand(0)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 485 | LHS = InsertNoopCastOfTo(LHS, Ty); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 486 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
| 487 | Value *RHS = expand(S->getOperand(i)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 488 | RHS = InsertNoopCastOfTo(RHS, Ty); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 489 | Instruction *ICmp = |
| 490 | new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt); |
| 491 | InsertedValues.insert(ICmp); |
| 492 | Instruction *Sel = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt); |
| 493 | InsertedValues.insert(Sel); |
| 494 | LHS = Sel; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 495 | } |
| 496 | return LHS; |
| 497 | } |
| 498 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 499 | Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 500 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 501 | Value *LHS = expand(S->getOperand(0)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 502 | LHS = InsertNoopCastOfTo(LHS, Ty); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 503 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
| 504 | Value *RHS = expand(S->getOperand(i)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 505 | RHS = InsertNoopCastOfTo(RHS, Ty); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 506 | Instruction *ICmp = |
| 507 | new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt); |
| 508 | InsertedValues.insert(ICmp); |
| 509 | Instruction *Sel = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt); |
| 510 | InsertedValues.insert(Sel); |
| 511 | LHS = Sel; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 512 | } |
| 513 | return LHS; |
| 514 | } |
| 515 | |
Dan Gohman | 752ec7d | 2009-04-23 15:16:49 +0000 | [diff] [blame] | 516 | Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty) { |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 517 | // Expand the code for this SCEV. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 518 | Value *V = expand(SH); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 519 | if (Ty) { |
| 520 | assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && |
| 521 | "non-trivial casts should be done with the SCEVs directly!"); |
| 522 | V = InsertNoopCastOfTo(V, Ty); |
| 523 | } |
| 524 | return V; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 527 | Value *SCEVExpander::expand(const SCEV *S) { |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 528 | // Check to see if we already expanded this. |
Torok Edwin | b679de2 | 2009-05-24 14:23:16 +0000 | [diff] [blame^] | 529 | std::map<SCEVHandle, AssertingVH<Value> >::iterator I = InsertedExpressions.find(S); |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 530 | if (I != InsertedExpressions.end()) |
| 531 | return I->second; |
| 532 | |
| 533 | Value *V = visit(S); |
| 534 | InsertedExpressions[S] = V; |
| 535 | return V; |
| 536 | } |