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 | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 147 | /// FactorOutConstant - Test if S is evenly divisible by Factor, using signed |
| 148 | /// division. If so, update S with Factor divided out and return true. |
| 149 | /// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made |
| 150 | /// unnecessary; in its place, just signed-divide Ops[i] by the scale and |
| 151 | /// check to see if the divide was folded. |
| 152 | static bool FactorOutConstant(SCEVHandle &S, |
| 153 | const APInt &Factor, |
| 154 | ScalarEvolution &SE) { |
| 155 | // Everything is divisible by one. |
| 156 | if (Factor == 1) |
| 157 | return true; |
| 158 | |
| 159 | // For a Constant, check for a multiple of the given factor. |
| 160 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) |
| 161 | if (!C->getValue()->getValue().srem(Factor)) { |
| 162 | ConstantInt *CI = |
| 163 | ConstantInt::get(C->getValue()->getValue().sdiv(Factor)); |
| 164 | SCEVHandle Div = SE.getConstant(CI); |
| 165 | S = Div; |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | // In a Mul, check if there is a constant operand which is a multiple |
| 170 | // of the given factor. |
| 171 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) |
| 172 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0))) |
| 173 | if (!C->getValue()->getValue().srem(Factor)) { |
| 174 | std::vector<SCEVHandle> NewMulOps(M->getOperands()); |
| 175 | NewMulOps[0] = |
| 176 | SE.getConstant(C->getValue()->getValue().sdiv(Factor)); |
| 177 | S = SE.getMulExpr(NewMulOps); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | // In an AddRec, check if both start and step are divisible. |
| 182 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
| 183 | SCEVHandle Start = A->getStart(); |
| 184 | if (!FactorOutConstant(Start, Factor, SE)) |
| 185 | return false; |
| 186 | SCEVHandle Step = A->getStepRecurrence(SE); |
| 187 | if (!FactorOutConstant(Step, Factor, SE)) |
| 188 | return false; |
| 189 | S = SE.getAddRecExpr(Start, Step, A->getLoop()); |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 196 | /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 197 | /// instead of using ptrtoint+arithmetic+inttoptr. This helps |
| 198 | /// BasicAliasAnalysis analyze the result. However, it suffers from the |
| 199 | /// underlying bug described in PR2831. Addition in LLVM currently always |
| 200 | /// has two's complement wrapping guaranteed. However, the semantics for |
| 201 | /// getelementptr overflow are ambiguous. In the common case though, this |
| 202 | /// expansion gets used when a GEP in the original code has been converted |
| 203 | /// into integer arithmetic, in which case the resulting code will be no |
| 204 | /// more undefined than it was originally. |
| 205 | /// |
| 206 | /// Design note: It might seem desirable for this function to be more |
| 207 | /// loop-aware. If some of the indices are loop-invariant while others |
| 208 | /// aren't, it might seem desirable to emit multiple GEPs, keeping the |
| 209 | /// loop-invariant portions of the overall computation outside the loop. |
| 210 | /// However, there are a few reasons this is not done here. Hoisting simple |
| 211 | /// arithmetic is a low-level optimization that often isn't very |
| 212 | /// important until late in the optimization process. In fact, passes |
| 213 | /// like InstructionCombining will combine GEPs, even if it means |
| 214 | /// pushing loop-invariant computation down into loops, so even if the |
| 215 | /// GEPs were split here, the work would quickly be undone. The |
| 216 | /// LoopStrengthReduction pass, which is usually run quite late (and |
| 217 | /// after the last InstructionCombining pass), takes care of hoisting |
| 218 | /// loop-invariant portions of expressions, after considering what |
| 219 | /// can be folded using target addressing modes. |
| 220 | /// |
| 221 | Value *SCEVExpander::expandAddToGEP(const SCEVHandle *op_begin, |
| 222 | const SCEVHandle *op_end, |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 223 | const PointerType *PTy, |
| 224 | const Type *Ty, |
| 225 | Value *V) { |
| 226 | const Type *ElTy = PTy->getElementType(); |
| 227 | SmallVector<Value *, 4> GepIndices; |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 228 | std::vector<SCEVHandle> Ops(op_begin, op_end); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 229 | bool AnyNonZeroIndices = false; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 230 | |
| 231 | // Decend down the pointer's type and attempt to convert the other |
| 232 | // operands into GEP indices, at each level. The first index in a GEP |
| 233 | // indexes into the array implied by the pointer operand; the rest of |
| 234 | // the indices index into the element or field type selected by the |
| 235 | // preceding index. |
| 236 | for (;;) { |
| 237 | APInt ElSize = APInt(SE.getTypeSizeInBits(Ty), |
| 238 | ElTy->isSized() ? SE.TD->getTypeAllocSize(ElTy) : 0); |
| 239 | std::vector<SCEVHandle> NewOps; |
| 240 | std::vector<SCEVHandle> ScaledOps; |
| 241 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 242 | // Split AddRecs up into parts as either of the parts may be usable |
| 243 | // without the other. |
| 244 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) |
| 245 | if (!A->getStart()->isZero()) { |
| 246 | SCEVHandle Start = A->getStart(); |
| 247 | Ops.push_back(SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()), |
| 248 | A->getStepRecurrence(SE), |
| 249 | A->getLoop())); |
| 250 | Ops[i] = Start; |
| 251 | ++e; |
| 252 | } |
| 253 | // If the scale size is not 0, attempt to factor out a scale. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 254 | if (ElSize != 0) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 255 | SCEVHandle Op = Ops[i]; |
| 256 | if (FactorOutConstant(Op, ElSize, SE)) { |
| 257 | ScaledOps.push_back(Op); // Op now has ElSize factored out. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 258 | continue; |
| 259 | } |
| 260 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 261 | // If the operand was not divisible, add it to the list of operands |
| 262 | // we'll scan next iteration. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 263 | NewOps.push_back(Ops[i]); |
| 264 | } |
| 265 | Ops = NewOps; |
| 266 | AnyNonZeroIndices |= !ScaledOps.empty(); |
| 267 | Value *Scaled = ScaledOps.empty() ? |
| 268 | Constant::getNullValue(Ty) : |
| 269 | expandCodeFor(SE.getAddExpr(ScaledOps), Ty); |
| 270 | GepIndices.push_back(Scaled); |
| 271 | |
| 272 | // Collect struct field index operands. |
| 273 | if (!Ops.empty()) |
| 274 | while (const StructType *STy = dyn_cast<StructType>(ElTy)) { |
| 275 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0])) |
| 276 | if (SE.getTypeSizeInBits(C->getType()) <= 64) { |
| 277 | const StructLayout &SL = *SE.TD->getStructLayout(STy); |
| 278 | uint64_t FullOffset = C->getValue()->getZExtValue(); |
| 279 | if (FullOffset < SL.getSizeInBytes()) { |
| 280 | unsigned ElIdx = SL.getElementContainingOffset(FullOffset); |
| 281 | GepIndices.push_back(ConstantInt::get(Type::Int32Ty, ElIdx)); |
| 282 | ElTy = STy->getTypeAtIndex(ElIdx); |
| 283 | Ops[0] = |
| 284 | SE.getConstant(ConstantInt::get(Ty, |
| 285 | FullOffset - |
| 286 | SL.getElementOffset(ElIdx))); |
| 287 | AnyNonZeroIndices = true; |
| 288 | continue; |
| 289 | } |
| 290 | } |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | if (const ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) { |
| 295 | ElTy = ATy->getElementType(); |
| 296 | continue; |
| 297 | } |
| 298 | break; |
| 299 | } |
| 300 | |
| 301 | // If none of the operands were convertable to proper GEP indices, cast |
| 302 | // the base to i8* and do an ugly getelementptr with that. It's still |
| 303 | // better than ptrtoint+arithmetic+inttoptr at least. |
| 304 | if (!AnyNonZeroIndices) { |
| 305 | V = InsertNoopCastOfTo(V, |
| 306 | Type::Int8Ty->getPointerTo(PTy->getAddressSpace())); |
| 307 | Value *Idx = expand(SE.getAddExpr(Ops)); |
| 308 | Idx = InsertNoopCastOfTo(Idx, Ty); |
| 309 | |
| 310 | // Fold a GEP with constant operands. |
| 311 | if (Constant *CLHS = dyn_cast<Constant>(V)) |
| 312 | if (Constant *CRHS = dyn_cast<Constant>(Idx)) |
Dan Gohman | 278b49a | 2009-05-19 19:18:01 +0000 | [diff] [blame] | 313 | return ConstantExpr::getGetElementPtr(CLHS, &CRHS, 1); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 314 | |
| 315 | // Do a quick scan to see if we have this GEP nearby. If so, reuse it. |
| 316 | unsigned ScanLimit = 6; |
| 317 | BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin(); |
| 318 | if (InsertPt != BlockBegin) { |
| 319 | // Scanning starts from the last instruction before InsertPt. |
| 320 | BasicBlock::iterator IP = InsertPt; |
| 321 | --IP; |
| 322 | for (; ScanLimit; --IP, --ScanLimit) { |
| 323 | if (IP->getOpcode() == Instruction::GetElementPtr && |
| 324 | IP->getOperand(0) == V && IP->getOperand(1) == Idx) |
| 325 | return IP; |
| 326 | if (IP == BlockBegin) break; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | Value *GEP = GetElementPtrInst::Create(V, Idx, "scevgep", InsertPt); |
| 331 | InsertedValues.insert(GEP); |
| 332 | return GEP; |
| 333 | } |
| 334 | |
| 335 | // Insert a pretty getelementptr. |
| 336 | Value *GEP = GetElementPtrInst::Create(V, |
| 337 | GepIndices.begin(), |
| 338 | GepIndices.end(), |
| 339 | "scevgep", InsertPt); |
| 340 | Ops.push_back(SE.getUnknown(GEP)); |
| 341 | InsertedValues.insert(GEP); |
| 342 | return expand(SE.getAddExpr(Ops)); |
| 343 | } |
| 344 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 345 | Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 346 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 347 | Value *V = expand(S->getOperand(S->getNumOperands()-1)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 348 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 349 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 350 | // comments on expandAddToGEP for details. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 351 | if (SE.TD) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 352 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) { |
| 353 | const std::vector<SCEVHandle> &Ops = S->getOperands(); |
Dan Gohman | fb5a341 | 2009-05-24 19:02:45 +0000 | [diff] [blame] | 354 | return expandAddToGEP(&Ops[0], &Ops[Ops.size() - 1], |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 355 | PTy, Ty, V); |
| 356 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 357 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 358 | V = InsertNoopCastOfTo(V, Ty); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 359 | |
| 360 | // Emit a bunch of add instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 361 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 362 | Value *W = expand(S->getOperand(i)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 363 | W = InsertNoopCastOfTo(W, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 364 | V = InsertBinop(Instruction::Add, V, W, InsertPt); |
| 365 | } |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 366 | return V; |
| 367 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 368 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 369 | Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 370 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 371 | int FirstOp = 0; // Set if we should emit a subtract. |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 372 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 373 | if (SC->getValue()->isAllOnesValue()) |
| 374 | FirstOp = 1; |
| 375 | |
| 376 | int i = S->getNumOperands()-2; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 377 | Value *V = expand(S->getOperand(i+1)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 378 | V = InsertNoopCastOfTo(V, Ty); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 379 | |
| 380 | // Emit a bunch of multiply instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 381 | for (; i >= FirstOp; --i) { |
| 382 | Value *W = expand(S->getOperand(i)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 383 | W = InsertNoopCastOfTo(W, Ty); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 384 | V = InsertBinop(Instruction::Mul, V, W, InsertPt); |
| 385 | } |
| 386 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 387 | // -1 * ... ---> 0 - ... |
| 388 | if (FirstOp == 1) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 389 | V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 390 | return V; |
| 391 | } |
| 392 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 393 | Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 394 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 395 | |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 396 | Value *LHS = expand(S->getLHS()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 397 | LHS = InsertNoopCastOfTo(LHS, Ty); |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 398 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 399 | const APInt &RHS = SC->getValue()->getValue(); |
| 400 | if (RHS.isPowerOf2()) |
| 401 | return InsertBinop(Instruction::LShr, LHS, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 402 | ConstantInt::get(Ty, RHS.logBase2()), |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 403 | InsertPt); |
| 404 | } |
| 405 | |
| 406 | Value *RHS = expand(S->getRHS()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 407 | RHS = InsertNoopCastOfTo(RHS, Ty); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 408 | return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt); |
| 409 | } |
| 410 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 411 | /// Move parts of Base into Rest to leave Base with the minimal |
| 412 | /// expression that provides a pointer operand suitable for a |
| 413 | /// GEP expansion. |
| 414 | static void ExposePointerBase(SCEVHandle &Base, SCEVHandle &Rest, |
| 415 | ScalarEvolution &SE) { |
| 416 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) { |
| 417 | Base = A->getStart(); |
| 418 | Rest = SE.getAddExpr(Rest, |
| 419 | SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()), |
| 420 | A->getStepRecurrence(SE), |
| 421 | A->getLoop())); |
| 422 | } |
| 423 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) { |
| 424 | Base = A->getOperand(A->getNumOperands()-1); |
| 425 | std::vector<SCEVHandle> NewAddOps(A->op_begin(), A->op_end()); |
| 426 | NewAddOps.back() = Rest; |
| 427 | Rest = SE.getAddExpr(NewAddOps); |
| 428 | ExposePointerBase(Base, Rest, SE); |
| 429 | } |
| 430 | } |
| 431 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 432 | Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 433 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 434 | const Loop *L = S->getLoop(); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 435 | |
| 436 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 437 | if (!S->getStart()->isZero()) { |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 438 | std::vector<SCEVHandle> NewOps(S->getOperands()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 439 | NewOps[0] = SE.getIntegerSCEV(0, Ty); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 440 | SCEVHandle Rest = SE.getAddRecExpr(NewOps, L); |
| 441 | |
| 442 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 443 | // comments on expandAddToGEP for details. |
| 444 | if (SE.TD) { |
| 445 | SCEVHandle Base = S->getStart(); |
| 446 | SCEVHandle RestArray[1] = Rest; |
| 447 | // Dig into the expression to find the pointer base for a GEP. |
| 448 | ExposePointerBase(Base, RestArray[0], SE); |
| 449 | // If we found a pointer, expand the AddRec with a GEP. |
| 450 | if (const PointerType *PTy = dyn_cast<PointerType>(Base->getType())) { |
Dan Gohman | f876ad0 | 2009-05-26 17:41:16 +0000 | [diff] [blame^] | 451 | // Make sure the Base isn't something exotic, such as a multiplied |
| 452 | // or divided pointer value. In those cases, the result type isn't |
| 453 | // actually a pointer type. |
| 454 | if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) { |
| 455 | Value *StartV = expand(Base); |
| 456 | assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!"); |
| 457 | return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV); |
| 458 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| 462 | Value *RestV = expand(Rest); |
| 463 | return expand(SE.getAddExpr(S->getStart(), SE.getUnknown(RestV))); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
Dan Gohman | 17f1972 | 2008-06-22 19:23:09 +0000 | [diff] [blame] | 467 | if (S->isAffine() && |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 468 | S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) { |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 469 | // Create and insert the PHI node for the induction variable in the |
| 470 | // specified loop. |
| 471 | BasicBlock *Header = L->getHeader(); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 472 | PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin()); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 473 | InsertedValues.insert(PN); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 474 | PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader()); |
| 475 | |
| 476 | pred_iterator HPI = pred_begin(Header); |
| 477 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 478 | if (!L->contains(*HPI)) ++HPI; |
| 479 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 480 | "No backedge in loop?"); |
| 481 | |
| 482 | // Insert a unit add instruction right before the terminator corresponding |
| 483 | // to the back-edge. |
Reid Spencer | 24d6da5 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 484 | Constant *One = ConstantInt::get(Ty, 1); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 485 | Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next", |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 486 | (*HPI)->getTerminator()); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 487 | InsertedValues.insert(Add); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 488 | |
| 489 | pred_iterator PI = pred_begin(Header); |
| 490 | if (*PI == L->getLoopPreheader()) |
| 491 | ++PI; |
| 492 | PN->addIncoming(Add, *PI); |
| 493 | return PN; |
| 494 | } |
| 495 | |
| 496 | // Get the canonical induction variable I for this loop. |
| 497 | Value *I = getOrInsertCanonicalInductionVariable(L, Ty); |
| 498 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 499 | // 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] | 500 | if (S->isAffine()) { // {0,+,F} --> i*F |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 501 | Value *F = expand(S->getOperand(1)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 502 | F = InsertNoopCastOfTo(F, Ty); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 503 | |
| 504 | // IF the step is by one, just return the inserted IV. |
Zhou Sheng | 6b6b6ef | 2007-01-11 12:24:14 +0000 | [diff] [blame] | 505 | if (ConstantInt *CI = dyn_cast<ConstantInt>(F)) |
Reid Spencer | 4d050d7 | 2007-03-01 19:45:00 +0000 | [diff] [blame] | 506 | if (CI->getValue() == 1) |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 507 | return I; |
| 508 | |
| 509 | // If the insert point is directly inside of the loop, emit the multiply at |
| 510 | // the insert point. Otherwise, L is a loop that is a parent of the insert |
| 511 | // point loop. If we can, move the multiply to the outer most loop that it |
| 512 | // is safe to be in. |
Dan Gohman | 6cdc727 | 2009-04-22 16:05:50 +0000 | [diff] [blame] | 513 | BasicBlock::iterator MulInsertPt = getInsertionPoint(); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 514 | Loop *InsertPtLoop = SE.LI->getLoopFor(MulInsertPt->getParent()); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 515 | if (InsertPtLoop != L && InsertPtLoop && |
| 516 | L->contains(InsertPtLoop->getHeader())) { |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 517 | do { |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 518 | // If we cannot hoist the multiply out of this loop, don't. |
| 519 | if (!InsertPtLoop->isLoopInvariant(F)) break; |
| 520 | |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 521 | BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader(); |
| 522 | |
| 523 | // If this loop hasn't got a preheader, we aren't able to hoist the |
| 524 | // multiply. |
| 525 | if (!InsertPtLoopPH) |
| 526 | break; |
| 527 | |
| 528 | // Otherwise, move the insert point to the preheader. |
| 529 | MulInsertPt = InsertPtLoopPH->getTerminator(); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 530 | InsertPtLoop = InsertPtLoop->getParentLoop(); |
Wojciech Matyjewicz | 5d2bc85 | 2008-06-14 16:48:22 +0000 | [diff] [blame] | 531 | } while (InsertPtLoop != L); |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 534 | return InsertBinop(Instruction::Mul, I, F, MulInsertPt); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 538 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 539 | // simplify the expression without having to build a bunch of special code |
| 540 | // into this folder. |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 541 | SCEVHandle IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 542 | |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 543 | SCEVHandle V = S->evaluateAtIteration(IH, SE); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 544 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 545 | |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 546 | return expand(V); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 547 | } |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 548 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 549 | Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 550 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 551 | Value *V = expand(S->getOperand()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 552 | V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 553 | Instruction *I = new TruncInst(V, Ty, "tmp.", InsertPt); |
| 554 | InsertedValues.insert(I); |
| 555 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 558 | Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 559 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 560 | Value *V = expand(S->getOperand()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 561 | V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 562 | Instruction *I = new ZExtInst(V, Ty, "tmp.", InsertPt); |
| 563 | InsertedValues.insert(I); |
| 564 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 567 | Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 568 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 569 | Value *V = expand(S->getOperand()); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 570 | V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType())); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 571 | Instruction *I = new SExtInst(V, Ty, "tmp.", InsertPt); |
| 572 | InsertedValues.insert(I); |
| 573 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 576 | Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 577 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 578 | Value *LHS = expand(S->getOperand(0)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 579 | LHS = InsertNoopCastOfTo(LHS, Ty); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 580 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
| 581 | Value *RHS = expand(S->getOperand(i)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 582 | RHS = InsertNoopCastOfTo(RHS, Ty); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 583 | Instruction *ICmp = |
| 584 | new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt); |
| 585 | InsertedValues.insert(ICmp); |
| 586 | Instruction *Sel = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt); |
| 587 | InsertedValues.insert(Sel); |
| 588 | LHS = Sel; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 589 | } |
| 590 | return LHS; |
| 591 | } |
| 592 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 593 | Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 594 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 595 | Value *LHS = expand(S->getOperand(0)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 596 | LHS = InsertNoopCastOfTo(LHS, Ty); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 597 | for (unsigned i = 1; i < S->getNumOperands(); ++i) { |
| 598 | Value *RHS = expand(S->getOperand(i)); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 599 | RHS = InsertNoopCastOfTo(RHS, Ty); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 600 | Instruction *ICmp = |
| 601 | new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt); |
| 602 | InsertedValues.insert(ICmp); |
| 603 | Instruction *Sel = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt); |
| 604 | InsertedValues.insert(Sel); |
| 605 | LHS = Sel; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 606 | } |
| 607 | return LHS; |
| 608 | } |
| 609 | |
Dan Gohman | 752ec7d | 2009-04-23 15:16:49 +0000 | [diff] [blame] | 610 | Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty) { |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 611 | // Expand the code for this SCEV. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 612 | Value *V = expand(SH); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 613 | if (Ty) { |
| 614 | assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && |
| 615 | "non-trivial casts should be done with the SCEVs directly!"); |
| 616 | V = InsertNoopCastOfTo(V, Ty); |
| 617 | } |
| 618 | return V; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 621 | Value *SCEVExpander::expand(const SCEV *S) { |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 622 | // Check to see if we already expanded this. |
Torok Edwin | 3790fb0 | 2009-05-24 19:36:09 +0000 | [diff] [blame] | 623 | std::map<SCEVHandle, AssertingVH<Value> >::iterator I = |
| 624 | InsertedExpressions.find(S); |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 625 | if (I != InsertedExpressions.end()) |
| 626 | return I->second; |
| 627 | |
| 628 | Value *V = visit(S); |
| 629 | InsertedExpressions[S] = V; |
| 630 | return V; |
| 631 | } |