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" |
Owen Anderson | 76f600b | 2009-07-06 22:37:39 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 23 | /// InsertNoopCastOfTo - Insert a cast of V to the specified type, |
| 24 | /// which must be possible with a noop cast, doing what we can to share |
| 25 | /// the casts. |
| 26 | Value *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) { |
| 27 | Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false); |
| 28 | assert((Op == Instruction::BitCast || |
| 29 | Op == Instruction::PtrToInt || |
| 30 | Op == Instruction::IntToPtr) && |
| 31 | "InsertNoopCastOfTo cannot perform non-noop casts!"); |
| 32 | assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) && |
| 33 | "InsertNoopCastOfTo cannot change sizes!"); |
| 34 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 35 | // Short-circuit unnecessary bitcasts. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 36 | if (Op == Instruction::BitCast && V->getType() == Ty) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 37 | return V; |
| 38 | |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 39 | // Short-circuit unnecessary inttoptr<->ptrtoint casts. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 40 | if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) && |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 41 | SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 42 | if (CastInst *CI = dyn_cast<CastInst>(V)) |
| 43 | if ((CI->getOpcode() == Instruction::PtrToInt || |
| 44 | CI->getOpcode() == Instruction::IntToPtr) && |
| 45 | SE.getTypeSizeInBits(CI->getType()) == |
| 46 | SE.getTypeSizeInBits(CI->getOperand(0)->getType())) |
| 47 | return CI->getOperand(0); |
Dan Gohman | 80dcdee | 2009-05-01 17:00:00 +0000 | [diff] [blame] | 48 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) |
| 49 | if ((CE->getOpcode() == Instruction::PtrToInt || |
| 50 | CE->getOpcode() == Instruction::IntToPtr) && |
| 51 | SE.getTypeSizeInBits(CE->getType()) == |
| 52 | SE.getTypeSizeInBits(CE->getOperand(0)->getType())) |
| 53 | return CE->getOperand(0); |
| 54 | } |
Dan Gohman | f04fa48 | 2009-04-16 15:52:57 +0000 | [diff] [blame] | 55 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 56 | if (Constant *C = dyn_cast<Constant>(V)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 57 | return ConstantExpr::getCast(Op, C, Ty); |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 58 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 59 | if (Argument *A = dyn_cast<Argument>(V)) { |
| 60 | // Check to see if there is already a cast! |
| 61 | for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 62 | UI != E; ++UI) |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 63 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 64 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 65 | if (CI->getOpcode() == Op) { |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 66 | // If the cast isn't the first instruction of the function, move it. |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 67 | if (BasicBlock::iterator(CI) != |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 68 | A->getParent()->getEntryBlock().begin()) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 69 | // Recreate the cast at the beginning of the entry block. |
| 70 | // The old cast is left in place in case it is being used |
| 71 | // as an insert point. |
| 72 | Instruction *NewCI = |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 73 | CastInst::Create(Op, V, Ty, "", |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 74 | A->getParent()->getEntryBlock().begin()); |
| 75 | NewCI->takeName(CI); |
| 76 | CI->replaceAllUsesWith(NewCI); |
| 77 | return NewCI; |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 78 | } |
| 79 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 80 | } |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 81 | |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 82 | Instruction *I = CastInst::Create(Op, V, Ty, V->getName(), |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 83 | A->getParent()->getEntryBlock().begin()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 84 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 85 | return I; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 86 | } |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 87 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 88 | Instruction *I = cast<Instruction>(V); |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 89 | |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 90 | // Check to see if there is already a cast. If there is, use it. |
| 91 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 92 | UI != E; ++UI) { |
| 93 | if ((*UI)->getType() == Ty) |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 94 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 95 | if (CI->getOpcode() == Op) { |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 96 | BasicBlock::iterator It = I; ++It; |
| 97 | if (isa<InvokeInst>(I)) |
| 98 | It = cast<InvokeInst>(I)->getNormalDest()->begin(); |
| 99 | while (isa<PHINode>(It)) ++It; |
| 100 | if (It != BasicBlock::iterator(CI)) { |
Dan Gohman | c37e3d5 | 2010-01-21 10:08:42 +0000 | [diff] [blame] | 101 | // Recreate the cast after the user. |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 102 | // The old cast is left in place in case it is being used |
| 103 | // as an insert point. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 104 | Instruction *NewCI = CastInst::Create(Op, V, Ty, "", It); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 105 | NewCI->takeName(CI); |
| 106 | CI->replaceAllUsesWith(NewCI); |
Dan Gohman | c37e3d5 | 2010-01-21 10:08:42 +0000 | [diff] [blame] | 107 | rememberInstruction(NewCI); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 108 | return NewCI; |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 109 | } |
Dan Gohman | c37e3d5 | 2010-01-21 10:08:42 +0000 | [diff] [blame] | 110 | rememberInstruction(CI); |
Wojciech Matyjewicz | 3913187 | 2008-02-09 18:30:13 +0000 | [diff] [blame] | 111 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 113 | } |
| 114 | BasicBlock::iterator IP = I; ++IP; |
| 115 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 116 | IP = II->getNormalDest()->begin(); |
| 117 | while (isa<PHINode>(IP)) ++IP; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 118 | Instruction *CI = CastInst::Create(Op, V, Ty, V->getName(), IP); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 119 | rememberInstruction(CI); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 120 | return CI; |
Chris Lattner | ca1a4be | 2006-02-04 09:51:53 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 123 | /// InsertBinop - Insert the specified binary operator, doing a small amount |
| 124 | /// of work to avoid inserting an obviously redundant operation. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 125 | Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, |
| 126 | Value *LHS, Value *RHS) { |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 127 | // Fold a binop with constant operands. |
| 128 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 129 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 130 | return ConstantExpr::get(Opcode, CLHS, CRHS); |
Dan Gohman | 0f0eb18 | 2007-06-15 19:21:55 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 132 | // Do a quick scan to see if we have this binop nearby. If so, reuse it. |
| 133 | unsigned ScanLimit = 6; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 134 | BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); |
| 135 | // Scanning starts from the last instruction before the insertion point. |
| 136 | BasicBlock::iterator IP = Builder.GetInsertPoint(); |
| 137 | if (IP != BlockBegin) { |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 138 | --IP; |
| 139 | for (; ScanLimit; --IP, --ScanLimit) { |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 140 | if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS && |
| 141 | IP->getOperand(1) == RHS) |
| 142 | return IP; |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 143 | if (IP == BlockBegin) break; |
| 144 | } |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 145 | } |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 146 | |
Wojciech Matyjewicz | 8a08769 | 2008-06-15 19:07:39 +0000 | [diff] [blame] | 147 | // If we haven't found this binop, insert it. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 148 | Value *BO = Builder.CreateBinOp(Opcode, LHS, RHS, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 149 | rememberInstruction(BO); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 150 | return BO; |
Chris Lattner | 7fec90e | 2007-04-13 05:04:18 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 153 | /// FactorOutConstant - Test if S is divisible by Factor, using signed |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 154 | /// division. If so, update S with Factor divided out and return true. |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 155 | /// S need not be evenly divisble if a reasonable remainder can be |
| 156 | /// computed. |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 157 | /// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made |
| 158 | /// unnecessary; in its place, just signed-divide Ops[i] by the scale and |
| 159 | /// check to see if the divide was folded. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 160 | static bool FactorOutConstant(const SCEV *&S, |
| 161 | const SCEV *&Remainder, |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 162 | const SCEV *Factor, |
| 163 | ScalarEvolution &SE, |
| 164 | const TargetData *TD) { |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 165 | // Everything is divisible by one. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 166 | if (Factor->isOne()) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 167 | return true; |
| 168 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 169 | // x/x == 1. |
| 170 | if (S == Factor) { |
| 171 | S = SE.getIntegerSCEV(1, S->getType()); |
| 172 | return true; |
| 173 | } |
| 174 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 175 | // For a Constant, check for a multiple of the given factor. |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 176 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 177 | // 0/x == 0. |
| 178 | if (C->isZero()) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 179 | return true; |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 180 | // Check for divisibility. |
| 181 | if (const SCEVConstant *FC = dyn_cast<SCEVConstant>(Factor)) { |
| 182 | ConstantInt *CI = |
| 183 | ConstantInt::get(SE.getContext(), |
| 184 | C->getValue()->getValue().sdiv( |
| 185 | FC->getValue()->getValue())); |
| 186 | // If the quotient is zero and the remainder is non-zero, reject |
| 187 | // the value at this scale. It will be considered for subsequent |
| 188 | // smaller scales. |
| 189 | if (!CI->isZero()) { |
| 190 | const SCEV *Div = SE.getConstant(CI); |
| 191 | S = Div; |
| 192 | Remainder = |
| 193 | SE.getAddExpr(Remainder, |
| 194 | SE.getConstant(C->getValue()->getValue().srem( |
| 195 | FC->getValue()->getValue()))); |
| 196 | return true; |
| 197 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 198 | } |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 199 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 200 | |
| 201 | // In a Mul, check if there is a constant operand which is a multiple |
| 202 | // of the given factor. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 203 | if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { |
| 204 | if (TD) { |
| 205 | // With TargetData, the size is known. Check if there is a constant |
| 206 | // operand which is a multiple of the given factor. If so, we can |
| 207 | // factor it. |
| 208 | const SCEVConstant *FC = cast<SCEVConstant>(Factor); |
| 209 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0))) |
| 210 | if (!C->getValue()->getValue().srem(FC->getValue()->getValue())) { |
| 211 | const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands(); |
| 212 | SmallVector<const SCEV *, 4> NewMulOps(MOperands.begin(), |
| 213 | MOperands.end()); |
| 214 | NewMulOps[0] = |
| 215 | SE.getConstant(C->getValue()->getValue().sdiv( |
| 216 | FC->getValue()->getValue())); |
| 217 | S = SE.getMulExpr(NewMulOps); |
| 218 | return true; |
| 219 | } |
| 220 | } else { |
| 221 | // Without TargetData, check if Factor can be factored out of any of the |
| 222 | // Mul's operands. If so, we can just remove it. |
| 223 | for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { |
| 224 | const SCEV *SOp = M->getOperand(i); |
| 225 | const SCEV *Remainder = SE.getIntegerSCEV(0, SOp->getType()); |
| 226 | if (FactorOutConstant(SOp, Remainder, Factor, SE, TD) && |
| 227 | Remainder->isZero()) { |
| 228 | const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands(); |
| 229 | SmallVector<const SCEV *, 4> NewMulOps(MOperands.begin(), |
| 230 | MOperands.end()); |
| 231 | NewMulOps[i] = SOp; |
| 232 | S = SE.getMulExpr(NewMulOps); |
| 233 | return true; |
| 234 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 235 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 238 | |
| 239 | // In an AddRec, check if both start and step are divisible. |
| 240 | if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 241 | const SCEV *Step = A->getStepRecurrence(SE); |
| 242 | const SCEV *StepRem = SE.getIntegerSCEV(0, Step->getType()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 243 | if (!FactorOutConstant(Step, StepRem, Factor, SE, TD)) |
Dan Gohman | 4a4f767 | 2009-05-27 02:00:53 +0000 | [diff] [blame] | 244 | return false; |
| 245 | if (!StepRem->isZero()) |
| 246 | return false; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 247 | const SCEV *Start = A->getStart(); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 248 | if (!FactorOutConstant(Start, Remainder, Factor, SE, TD)) |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 249 | return false; |
| 250 | S = SE.getAddRecExpr(Start, Step, A->getLoop()); |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 257 | /// SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs |
| 258 | /// is the number of SCEVAddRecExprs present, which are kept at the end of |
| 259 | /// the list. |
| 260 | /// |
| 261 | static void SimplifyAddOperands(SmallVectorImpl<const SCEV *> &Ops, |
| 262 | const Type *Ty, |
| 263 | ScalarEvolution &SE) { |
| 264 | unsigned NumAddRecs = 0; |
| 265 | for (unsigned i = Ops.size(); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1]); --i) |
| 266 | ++NumAddRecs; |
| 267 | // Group Ops into non-addrecs and addrecs. |
| 268 | SmallVector<const SCEV *, 8> NoAddRecs(Ops.begin(), Ops.end() - NumAddRecs); |
| 269 | SmallVector<const SCEV *, 8> AddRecs(Ops.end() - NumAddRecs, Ops.end()); |
| 270 | // Let ScalarEvolution sort and simplify the non-addrecs list. |
| 271 | const SCEV *Sum = NoAddRecs.empty() ? |
| 272 | SE.getIntegerSCEV(0, Ty) : |
| 273 | SE.getAddExpr(NoAddRecs); |
| 274 | // If it returned an add, use the operands. Otherwise it simplified |
| 275 | // the sum into a single value, so just use that. |
| 276 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum)) |
| 277 | Ops = Add->getOperands(); |
| 278 | else { |
| 279 | Ops.clear(); |
| 280 | if (!Sum->isZero()) |
| 281 | Ops.push_back(Sum); |
| 282 | } |
| 283 | // Then append the addrecs. |
| 284 | Ops.insert(Ops.end(), AddRecs.begin(), AddRecs.end()); |
| 285 | } |
| 286 | |
| 287 | /// SplitAddRecs - Flatten a list of add operands, moving addrec start values |
| 288 | /// out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}. |
| 289 | /// This helps expose more opportunities for folding parts of the expressions |
| 290 | /// into GEP indices. |
| 291 | /// |
| 292 | static void SplitAddRecs(SmallVectorImpl<const SCEV *> &Ops, |
| 293 | const Type *Ty, |
| 294 | ScalarEvolution &SE) { |
| 295 | // Find the addrecs. |
| 296 | SmallVector<const SCEV *, 8> AddRecs; |
| 297 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 298 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) { |
| 299 | const SCEV *Start = A->getStart(); |
| 300 | if (Start->isZero()) break; |
| 301 | const SCEV *Zero = SE.getIntegerSCEV(0, Ty); |
| 302 | AddRecs.push_back(SE.getAddRecExpr(Zero, |
| 303 | A->getStepRecurrence(SE), |
| 304 | A->getLoop())); |
| 305 | if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) { |
| 306 | Ops[i] = Zero; |
| 307 | Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); |
| 308 | e += Add->getNumOperands(); |
| 309 | } else { |
| 310 | Ops[i] = Start; |
| 311 | } |
| 312 | } |
| 313 | if (!AddRecs.empty()) { |
| 314 | // Add the addrecs onto the end of the list. |
| 315 | Ops.insert(Ops.end(), AddRecs.begin(), AddRecs.end()); |
| 316 | // Resort the operand list, moving any constants to the front. |
| 317 | SimplifyAddOperands(Ops, Ty, SE); |
| 318 | } |
| 319 | } |
| 320 | |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 321 | /// expandAddToGEP - Expand an addition expression with a pointer type into |
| 322 | /// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps |
| 323 | /// BasicAliasAnalysis and other passes analyze the result. See the rules |
| 324 | /// for getelementptr vs. inttoptr in |
| 325 | /// http://llvm.org/docs/LangRef.html#pointeraliasing |
| 326 | /// for details. |
Dan Gohman | 13c5e35 | 2009-07-20 17:44:17 +0000 | [diff] [blame] | 327 | /// |
Dan Gohman | 3abf905 | 2010-01-19 22:26:02 +0000 | [diff] [blame] | 328 | /// Design note: The correctness of using getelementptr here depends on |
Dan Gohman | 4c0d5d5 | 2009-08-20 16:42:55 +0000 | [diff] [blame] | 329 | /// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as |
| 330 | /// they may introduce pointer arithmetic which may not be safely converted |
| 331 | /// into getelementptr. |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 332 | /// |
| 333 | /// Design note: It might seem desirable for this function to be more |
| 334 | /// loop-aware. If some of the indices are loop-invariant while others |
| 335 | /// aren't, it might seem desirable to emit multiple GEPs, keeping the |
| 336 | /// loop-invariant portions of the overall computation outside the loop. |
| 337 | /// However, there are a few reasons this is not done here. Hoisting simple |
| 338 | /// arithmetic is a low-level optimization that often isn't very |
| 339 | /// important until late in the optimization process. In fact, passes |
| 340 | /// like InstructionCombining will combine GEPs, even if it means |
| 341 | /// pushing loop-invariant computation down into loops, so even if the |
| 342 | /// GEPs were split here, the work would quickly be undone. The |
| 343 | /// LoopStrengthReduction pass, which is usually run quite late (and |
| 344 | /// after the last InstructionCombining pass), takes care of hoisting |
| 345 | /// loop-invariant portions of expressions, after considering what |
| 346 | /// can be folded using target addressing modes. |
| 347 | /// |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 348 | Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin, |
| 349 | const SCEV *const *op_end, |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 350 | const PointerType *PTy, |
| 351 | const Type *Ty, |
| 352 | Value *V) { |
| 353 | const Type *ElTy = PTy->getElementType(); |
| 354 | SmallVector<Value *, 4> GepIndices; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 355 | SmallVector<const SCEV *, 8> Ops(op_begin, op_end); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 356 | bool AnyNonZeroIndices = false; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 357 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 358 | // Split AddRecs up into parts as either of the parts may be usable |
| 359 | // without the other. |
| 360 | SplitAddRecs(Ops, Ty, SE); |
| 361 | |
Bob Wilson | eb35699 | 2009-12-04 01:33:04 +0000 | [diff] [blame] | 362 | // Descend down the pointer's type and attempt to convert the other |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 363 | // operands into GEP indices, at each level. The first index in a GEP |
| 364 | // indexes into the array implied by the pointer operand; the rest of |
| 365 | // the indices index into the element or field type selected by the |
| 366 | // preceding index. |
| 367 | for (;;) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 368 | // If the scale size is not 0, attempt to factor out a scale for |
| 369 | // array indexing. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 370 | SmallVector<const SCEV *, 8> ScaledOps; |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame^] | 371 | if (ElTy->isSized()) { |
| 372 | const SCEV *ElSize = SE.getAllocSizeExpr(ElTy); |
| 373 | if (!ElSize->isZero()) { |
| 374 | SmallVector<const SCEV *, 8> NewOps; |
| 375 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) { |
| 376 | const SCEV *Op = Ops[i]; |
| 377 | const SCEV *Remainder = SE.getIntegerSCEV(0, Ty); |
| 378 | if (FactorOutConstant(Op, Remainder, ElSize, SE, SE.TD)) { |
| 379 | // Op now has ElSize factored out. |
| 380 | ScaledOps.push_back(Op); |
| 381 | if (!Remainder->isZero()) |
| 382 | NewOps.push_back(Remainder); |
| 383 | AnyNonZeroIndices = true; |
| 384 | } else { |
| 385 | // The operand was not divisible, so add it to the list of operands |
| 386 | // we'll scan next iteration. |
| 387 | NewOps.push_back(Ops[i]); |
| 388 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 389 | } |
Dan Gohman | 150dfa8 | 2010-01-28 06:32:46 +0000 | [diff] [blame^] | 390 | // If we made any changes, update Ops. |
| 391 | if (!ScaledOps.empty()) { |
| 392 | Ops = NewOps; |
| 393 | SimplifyAddOperands(Ops, Ty, SE); |
| 394 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 395 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 396 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 397 | |
| 398 | // Record the scaled array index for this level of the type. If |
| 399 | // we didn't find any operands that could be factored, tentatively |
| 400 | // assume that element zero was selected (since the zero offset |
| 401 | // would obviously be folded away). |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 402 | Value *Scaled = ScaledOps.empty() ? |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 403 | Constant::getNullValue(Ty) : |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 404 | expandCodeFor(SE.getAddExpr(ScaledOps), Ty); |
| 405 | GepIndices.push_back(Scaled); |
| 406 | |
| 407 | // Collect struct field index operands. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 408 | while (const StructType *STy = dyn_cast<StructType>(ElTy)) { |
| 409 | bool FoundFieldNo = false; |
| 410 | // An empty struct has no fields. |
| 411 | if (STy->getNumElements() == 0) break; |
| 412 | if (SE.TD) { |
| 413 | // With TargetData, field offsets are known. See if a constant offset |
| 414 | // falls within any of the struct fields. |
| 415 | if (Ops.empty()) break; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 416 | if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0])) |
| 417 | if (SE.getTypeSizeInBits(C->getType()) <= 64) { |
| 418 | const StructLayout &SL = *SE.TD->getStructLayout(STy); |
| 419 | uint64_t FullOffset = C->getValue()->getZExtValue(); |
| 420 | if (FullOffset < SL.getSizeInBytes()) { |
| 421 | unsigned ElIdx = SL.getElementContainingOffset(FullOffset); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 422 | GepIndices.push_back( |
| 423 | ConstantInt::get(Type::getInt32Ty(Ty->getContext()), ElIdx)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 424 | ElTy = STy->getTypeAtIndex(ElIdx); |
| 425 | Ops[0] = |
Dan Gohman | 6de29f8 | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 426 | SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 427 | AnyNonZeroIndices = true; |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 428 | FoundFieldNo = true; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 431 | } else { |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 432 | // Without TargetData, just check for an offsetof expression of the |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 433 | // appropriate struct type. |
| 434 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 435 | if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Ops[i])) { |
| 436 | const StructType *StructTy; |
| 437 | Constant *FieldNo; |
| 438 | if (U->isOffsetOf(StructTy, FieldNo) && StructTy == STy) { |
| 439 | GepIndices.push_back(FieldNo); |
| 440 | ElTy = |
| 441 | STy->getTypeAtIndex(cast<ConstantInt>(FieldNo)->getZExtValue()); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 442 | Ops[i] = SE.getConstant(Ty, 0); |
| 443 | AnyNonZeroIndices = true; |
| 444 | FoundFieldNo = true; |
| 445 | break; |
| 446 | } |
Dan Gohman | 0f5efe5 | 2010-01-28 02:15:55 +0000 | [diff] [blame] | 447 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 448 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 449 | // If no struct field offsets were found, tentatively assume that |
| 450 | // field zero was selected (since the zero offset would obviously |
| 451 | // be folded away). |
| 452 | if (!FoundFieldNo) { |
| 453 | ElTy = STy->getTypeAtIndex(0u); |
| 454 | GepIndices.push_back( |
| 455 | Constant::getNullValue(Type::getInt32Ty(Ty->getContext()))); |
| 456 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 457 | } |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 458 | |
| 459 | if (const ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) |
| 460 | ElTy = ATy->getElementType(); |
| 461 | else |
| 462 | break; |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | // If none of the operands were convertable to proper GEP indices, cast |
| 466 | // the base to i8* and do an ugly getelementptr with that. It's still |
| 467 | // better than ptrtoint+arithmetic+inttoptr at least. |
| 468 | if (!AnyNonZeroIndices) { |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 469 | // Cast the base to i8*. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 470 | V = InsertNoopCastOfTo(V, |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 471 | Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace())); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 472 | |
| 473 | // Expand the operands for a plain byte offset. |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 474 | Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 475 | |
| 476 | // Fold a GEP with constant operands. |
| 477 | if (Constant *CLHS = dyn_cast<Constant>(V)) |
| 478 | if (Constant *CRHS = dyn_cast<Constant>(Idx)) |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 479 | return ConstantExpr::getGetElementPtr(CLHS, &CRHS, 1); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 480 | |
| 481 | // Do a quick scan to see if we have this GEP nearby. If so, reuse it. |
| 482 | unsigned ScanLimit = 6; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 483 | BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin(); |
| 484 | // Scanning starts from the last instruction before the insertion point. |
| 485 | BasicBlock::iterator IP = Builder.GetInsertPoint(); |
| 486 | if (IP != BlockBegin) { |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 487 | --IP; |
| 488 | for (; ScanLimit; --IP, --ScanLimit) { |
| 489 | if (IP->getOpcode() == Instruction::GetElementPtr && |
| 490 | IP->getOperand(0) == V && IP->getOperand(1) == Idx) |
| 491 | return IP; |
| 492 | if (IP == BlockBegin) break; |
| 493 | } |
| 494 | } |
| 495 | |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 496 | // Emit a GEP. |
| 497 | Value *GEP = Builder.CreateGEP(V, Idx, "uglygep"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 498 | rememberInstruction(GEP); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 499 | return GEP; |
| 500 | } |
| 501 | |
Dan Gohman | d6aa02d | 2009-07-28 01:40:03 +0000 | [diff] [blame] | 502 | // Insert a pretty getelementptr. Note that this GEP is not marked inbounds, |
| 503 | // because ScalarEvolution may have changed the address arithmetic to |
| 504 | // compute a value which is beyond the end of the allocated object. |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 505 | Value *Casted = V; |
| 506 | if (V->getType() != PTy) |
| 507 | Casted = InsertNoopCastOfTo(Casted, PTy); |
| 508 | Value *GEP = Builder.CreateGEP(Casted, |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 509 | GepIndices.begin(), |
| 510 | GepIndices.end(), |
| 511 | "scevgep"); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 512 | Ops.push_back(SE.getUnknown(GEP)); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 513 | rememberInstruction(GEP); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 514 | return expand(SE.getAddExpr(Ops)); |
| 515 | } |
| 516 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 517 | /// isNonConstantNegative - Return true if the specified scev is negated, but |
| 518 | /// not a constant. |
| 519 | static bool isNonConstantNegative(const SCEV *F) { |
| 520 | const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(F); |
| 521 | if (!Mul) return false; |
| 522 | |
| 523 | // If there is a constant factor, it will be first. |
| 524 | const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0)); |
| 525 | if (!SC) return false; |
| 526 | |
| 527 | // Return true if the value is negative, this matches things like (-42 * V). |
| 528 | return SC->getValue()->getValue().isNegative(); |
| 529 | } |
| 530 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 531 | Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) { |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 532 | int NumOperands = S->getNumOperands(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 533 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 534 | |
| 535 | // Find the index of an operand to start with. Choose the operand with |
| 536 | // pointer type, if there is one, or the last operand otherwise. |
| 537 | int PIdx = 0; |
| 538 | for (; PIdx != NumOperands - 1; ++PIdx) |
| 539 | if (isa<PointerType>(S->getOperand(PIdx)->getType())) break; |
| 540 | |
| 541 | // Expand code for the operand that we chose. |
| 542 | Value *V = expand(S->getOperand(PIdx)); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 543 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 544 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 545 | // comments on expandAddToGEP for details. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 546 | if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) { |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 547 | // Take the operand at PIdx out of the list. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 548 | const SmallVectorImpl<const SCEV *> &Ops = S->getOperands(); |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 549 | SmallVector<const SCEV *, 8> NewOps; |
| 550 | NewOps.insert(NewOps.end(), Ops.begin(), Ops.begin() + PIdx); |
| 551 | NewOps.insert(NewOps.end(), Ops.begin() + PIdx + 1, Ops.end()); |
| 552 | // Make a GEP. |
| 553 | return expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, V); |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 554 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 555 | |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 556 | // Otherwise, we'll expand the rest of the SCEVAddExpr as plain integer |
| 557 | // arithmetic. |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 558 | V = InsertNoopCastOfTo(V, Ty); |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 559 | |
| 560 | // Emit a bunch of add instructions |
Dan Gohman | c70c377 | 2009-09-26 16:11:57 +0000 | [diff] [blame] | 561 | for (int i = NumOperands-1; i >= 0; --i) { |
| 562 | if (i == PIdx) continue; |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 563 | const SCEV *Op = S->getOperand(i); |
| 564 | if (isNonConstantNegative(Op)) { |
| 565 | Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty); |
| 566 | V = InsertBinop(Instruction::Sub, V, W); |
| 567 | } else { |
| 568 | Value *W = expandCodeFor(Op, Ty); |
| 569 | V = InsertBinop(Instruction::Add, V, W); |
| 570 | } |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 571 | } |
Dan Gohman | e24fa64 | 2008-06-18 16:37:11 +0000 | [diff] [blame] | 572 | return V; |
| 573 | } |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 574 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 575 | Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 576 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 577 | int FirstOp = 0; // Set if we should emit a subtract. |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 578 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 579 | if (SC->getValue()->isAllOnesValue()) |
| 580 | FirstOp = 1; |
| 581 | |
| 582 | int i = S->getNumOperands()-2; |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 583 | Value *V = expandCodeFor(S->getOperand(i+1), Ty); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 584 | |
| 585 | // Emit a bunch of multiply instructions |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 586 | for (; i >= FirstOp; --i) { |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 587 | Value *W = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 588 | V = InsertBinop(Instruction::Mul, V, W); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 591 | // -1 * ... ---> 0 - ... |
| 592 | if (FirstOp == 1) |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 593 | V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 594 | return V; |
| 595 | } |
| 596 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 597 | Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 598 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 599 | |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 600 | Value *LHS = expandCodeFor(S->getLHS(), Ty); |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 601 | if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) { |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 602 | const APInt &RHS = SC->getValue()->getValue(); |
| 603 | if (RHS.isPowerOf2()) |
| 604 | return InsertBinop(Instruction::LShr, LHS, |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 605 | ConstantInt::get(Ty, RHS.logBase2())); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 608 | Value *RHS = expandCodeFor(S->getRHS(), Ty); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 609 | return InsertBinop(Instruction::UDiv, LHS, RHS); |
Nick Lewycky | 6177fd4 | 2008-07-08 05:05:37 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 612 | /// Move parts of Base into Rest to leave Base with the minimal |
| 613 | /// expression that provides a pointer operand suitable for a |
| 614 | /// GEP expansion. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 615 | static void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest, |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 616 | ScalarEvolution &SE) { |
| 617 | while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) { |
| 618 | Base = A->getStart(); |
| 619 | Rest = SE.getAddExpr(Rest, |
| 620 | SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()), |
| 621 | A->getStepRecurrence(SE), |
| 622 | A->getLoop())); |
| 623 | } |
| 624 | if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) { |
| 625 | Base = A->getOperand(A->getNumOperands()-1); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 626 | SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end()); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 627 | NewAddOps.back() = Rest; |
| 628 | Rest = SE.getAddExpr(NewAddOps); |
| 629 | ExposePointerBase(Base, Rest, SE); |
| 630 | } |
| 631 | } |
| 632 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 633 | /// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand |
| 634 | /// the base addrec, which is the addrec without any non-loop-dominating |
| 635 | /// values, and return the PHI. |
| 636 | PHINode * |
| 637 | SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized, |
| 638 | const Loop *L, |
| 639 | const Type *ExpandTy, |
| 640 | const Type *IntTy) { |
| 641 | // Reuse a previously-inserted PHI, if present. |
| 642 | for (BasicBlock::iterator I = L->getHeader()->begin(); |
| 643 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 644 | if (isInsertedInstruction(PN) && SE.getSCEV(PN) == Normalized) |
| 645 | return PN; |
| 646 | |
| 647 | // Save the original insertion point so we can restore it when we're done. |
| 648 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 649 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
| 650 | |
| 651 | // Expand code for the start value. |
| 652 | Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy, |
| 653 | L->getHeader()->begin()); |
| 654 | |
| 655 | // Expand code for the step value. Insert instructions right before the |
| 656 | // terminator corresponding to the back-edge. Do this before creating the PHI |
| 657 | // so that PHI reuse code doesn't see an incomplete PHI. If the stride is |
| 658 | // negative, insert a sub instead of an add for the increment (unless it's a |
| 659 | // constant, because subtracts of constants are canonicalized to adds). |
| 660 | const SCEV *Step = Normalized->getStepRecurrence(SE); |
| 661 | bool isPointer = isa<PointerType>(ExpandTy); |
| 662 | bool isNegative = !isPointer && isNonConstantNegative(Step); |
| 663 | if (isNegative) |
| 664 | Step = SE.getNegativeSCEV(Step); |
| 665 | Value *StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin()); |
| 666 | |
| 667 | // Create the PHI. |
| 668 | Builder.SetInsertPoint(L->getHeader(), L->getHeader()->begin()); |
| 669 | PHINode *PN = Builder.CreatePHI(ExpandTy, "lsr.iv"); |
| 670 | rememberInstruction(PN); |
| 671 | |
| 672 | // Create the step instructions and populate the PHI. |
| 673 | BasicBlock *Header = L->getHeader(); |
| 674 | for (pred_iterator HPI = pred_begin(Header), HPE = pred_end(Header); |
| 675 | HPI != HPE; ++HPI) { |
| 676 | BasicBlock *Pred = *HPI; |
| 677 | |
| 678 | // Add a start value. |
| 679 | if (!L->contains(Pred)) { |
| 680 | PN->addIncoming(StartV, Pred); |
| 681 | continue; |
| 682 | } |
| 683 | |
| 684 | // Create a step value and add it to the PHI. If IVIncInsertLoop is |
| 685 | // non-null and equal to the addrec's loop, insert the instructions |
| 686 | // at IVIncInsertPos. |
| 687 | Instruction *InsertPos = L == IVIncInsertLoop ? |
| 688 | IVIncInsertPos : Pred->getTerminator(); |
| 689 | Builder.SetInsertPoint(InsertPos->getParent(), InsertPos); |
| 690 | Value *IncV; |
| 691 | // If the PHI is a pointer, use a GEP, otherwise use an add or sub. |
| 692 | if (isPointer) { |
| 693 | const PointerType *GEPPtrTy = cast<PointerType>(ExpandTy); |
| 694 | // If the step isn't constant, don't use an implicitly scaled GEP, because |
| 695 | // that would require a multiply inside the loop. |
| 696 | if (!isa<ConstantInt>(StepV)) |
| 697 | GEPPtrTy = PointerType::get(Type::getInt1Ty(SE.getContext()), |
| 698 | GEPPtrTy->getAddressSpace()); |
| 699 | const SCEV *const StepArray[1] = { SE.getSCEV(StepV) }; |
| 700 | IncV = expandAddToGEP(StepArray, StepArray+1, GEPPtrTy, IntTy, PN); |
| 701 | if (IncV->getType() != PN->getType()) { |
| 702 | IncV = Builder.CreateBitCast(IncV, PN->getType(), "tmp"); |
| 703 | rememberInstruction(IncV); |
| 704 | } |
| 705 | } else { |
| 706 | IncV = isNegative ? |
| 707 | Builder.CreateSub(PN, StepV, "lsr.iv.next") : |
| 708 | Builder.CreateAdd(PN, StepV, "lsr.iv.next"); |
| 709 | rememberInstruction(IncV); |
| 710 | } |
| 711 | PN->addIncoming(IncV, Pred); |
| 712 | } |
| 713 | |
| 714 | // Restore the original insert point. |
| 715 | if (SaveInsertBB) |
| 716 | Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt); |
| 717 | |
| 718 | // Remember this PHI, even in post-inc mode. |
| 719 | InsertedValues.insert(PN); |
| 720 | |
| 721 | return PN; |
| 722 | } |
| 723 | |
| 724 | Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) { |
| 725 | const Type *STy = S->getType(); |
| 726 | const Type *IntTy = SE.getEffectiveSCEVType(STy); |
| 727 | const Loop *L = S->getLoop(); |
| 728 | |
| 729 | // Determine a normalized form of this expression, which is the expression |
| 730 | // before any post-inc adjustment is made. |
| 731 | const SCEVAddRecExpr *Normalized = S; |
| 732 | if (L == PostIncLoop) { |
| 733 | const SCEV *Step = S->getStepRecurrence(SE); |
| 734 | Normalized = cast<SCEVAddRecExpr>(SE.getMinusSCEV(S, Step)); |
| 735 | } |
| 736 | |
| 737 | // Strip off any non-loop-dominating component from the addrec start. |
| 738 | const SCEV *Start = Normalized->getStart(); |
| 739 | const SCEV *PostLoopOffset = 0; |
| 740 | if (!Start->properlyDominates(L->getHeader(), SE.DT)) { |
| 741 | PostLoopOffset = Start; |
| 742 | Start = SE.getIntegerSCEV(0, Normalized->getType()); |
| 743 | Normalized = |
| 744 | cast<SCEVAddRecExpr>(SE.getAddRecExpr(Start, |
| 745 | Normalized->getStepRecurrence(SE), |
| 746 | Normalized->getLoop())); |
| 747 | } |
| 748 | |
| 749 | // Strip off any non-loop-dominating component from the addrec step. |
| 750 | const SCEV *Step = Normalized->getStepRecurrence(SE); |
| 751 | const SCEV *PostLoopScale = 0; |
| 752 | if (!Step->hasComputableLoopEvolution(L) && |
| 753 | !Step->dominates(L->getHeader(), SE.DT)) { |
| 754 | PostLoopScale = Step; |
| 755 | Step = SE.getIntegerSCEV(1, Normalized->getType()); |
| 756 | Normalized = |
| 757 | cast<SCEVAddRecExpr>(SE.getAddRecExpr(Start, Step, |
| 758 | Normalized->getLoop())); |
| 759 | } |
| 760 | |
| 761 | // Expand the core addrec. If we need post-loop scaling, force it to |
| 762 | // expand to an integer type to avoid the need for additional casting. |
| 763 | const Type *ExpandTy = PostLoopScale ? IntTy : STy; |
| 764 | PHINode *PN = getAddRecExprPHILiterally(Normalized, L, ExpandTy, IntTy); |
| 765 | |
| 766 | // Accomodate post-inc mode, if necessary. |
| 767 | Value *Result; |
| 768 | if (L != PostIncLoop) |
| 769 | Result = PN; |
| 770 | else { |
| 771 | // In PostInc mode, use the post-incremented value. |
| 772 | BasicBlock *LatchBlock = L->getLoopLatch(); |
| 773 | assert(LatchBlock && "PostInc mode requires a unique loop latch!"); |
| 774 | Result = PN->getIncomingValueForBlock(LatchBlock); |
| 775 | } |
| 776 | |
| 777 | // Re-apply any non-loop-dominating scale. |
| 778 | if (PostLoopScale) { |
| 779 | Result = Builder.CreateMul(Result, |
| 780 | expandCodeFor(PostLoopScale, IntTy)); |
| 781 | rememberInstruction(Result); |
| 782 | } |
| 783 | |
| 784 | // Re-apply any non-loop-dominating offset. |
| 785 | if (PostLoopOffset) { |
| 786 | if (const PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) { |
| 787 | const SCEV *const OffsetArray[1] = { PostLoopOffset }; |
| 788 | Result = expandAddToGEP(OffsetArray, OffsetArray+1, PTy, IntTy, Result); |
| 789 | } else { |
| 790 | Result = Builder.CreateAdd(Result, |
| 791 | expandCodeFor(PostLoopOffset, IntTy)); |
| 792 | rememberInstruction(Result); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | return Result; |
| 797 | } |
| 798 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 799 | Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) { |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 800 | if (!CanonicalMode) return expandAddRecExprLiterally(S); |
| 801 | |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 802 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 803 | const Loop *L = S->getLoop(); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 804 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 805 | // First check for an existing canonical IV in a suitable type. |
| 806 | PHINode *CanonicalIV = 0; |
| 807 | if (PHINode *PN = L->getCanonicalInductionVariable()) |
| 808 | if (SE.isSCEVable(PN->getType()) && |
| 809 | isa<IntegerType>(SE.getEffectiveSCEVType(PN->getType())) && |
| 810 | SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty)) |
| 811 | CanonicalIV = PN; |
| 812 | |
| 813 | // Rewrite an AddRec in terms of the canonical induction variable, if |
| 814 | // its type is more narrow. |
| 815 | if (CanonicalIV && |
| 816 | SE.getTypeSizeInBits(CanonicalIV->getType()) > |
| 817 | SE.getTypeSizeInBits(Ty)) { |
Dan Gohman | f3f1be6 | 2009-09-28 21:01:47 +0000 | [diff] [blame] | 818 | const SmallVectorImpl<const SCEV *> &Ops = S->getOperands(); |
| 819 | SmallVector<const SCEV *, 4> NewOps(Ops.size()); |
| 820 | for (unsigned i = 0, e = Ops.size(); i != e; ++i) |
| 821 | NewOps[i] = SE.getAnyExtendExpr(Ops[i], CanonicalIV->getType()); |
| 822 | Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop())); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 823 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 824 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 825 | BasicBlock::iterator NewInsertPt = |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 826 | llvm::next(BasicBlock::iterator(cast<Instruction>(V))); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 827 | while (isa<PHINode>(NewInsertPt)) ++NewInsertPt; |
| 828 | V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0, |
| 829 | NewInsertPt); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 830 | Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 831 | return V; |
| 832 | } |
| 833 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 834 | // {X,+,F} --> X + {0,+,F} |
Dan Gohman | cfeb6a4 | 2008-06-18 16:23:07 +0000 | [diff] [blame] | 835 | if (!S->getStart()->isZero()) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 836 | const SmallVectorImpl<const SCEV *> &SOperands = S->getOperands(); |
| 837 | SmallVector<const SCEV *, 4> NewOps(SOperands.begin(), SOperands.end()); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 838 | NewOps[0] = SE.getIntegerSCEV(0, Ty); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 839 | const SCEV *Rest = SE.getAddRecExpr(NewOps, L); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 840 | |
| 841 | // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the |
| 842 | // comments on expandAddToGEP for details. |
Dan Gohman | c40f17b | 2009-08-18 16:46:41 +0000 | [diff] [blame] | 843 | const SCEV *Base = S->getStart(); |
| 844 | const SCEV *RestArray[1] = { Rest }; |
| 845 | // Dig into the expression to find the pointer base for a GEP. |
| 846 | ExposePointerBase(Base, RestArray[0], SE); |
| 847 | // If we found a pointer, expand the AddRec with a GEP. |
| 848 | if (const PointerType *PTy = dyn_cast<PointerType>(Base->getType())) { |
| 849 | // Make sure the Base isn't something exotic, such as a multiplied |
| 850 | // or divided pointer value. In those cases, the result type isn't |
| 851 | // actually a pointer type. |
| 852 | if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) { |
| 853 | Value *StartV = expand(Base); |
| 854 | assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!"); |
| 855 | return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV); |
Dan Gohman | 453aa4f | 2009-05-24 18:06:31 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 859 | // Just do a normal add. Pre-expand the operands to suppress folding. |
| 860 | return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())), |
| 861 | SE.getUnknown(expand(Rest)))); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 862 | } |
| 863 | |
| 864 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
Dan Gohman | 17f1972 | 2008-06-22 19:23:09 +0000 | [diff] [blame] | 865 | if (S->isAffine() && |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 866 | S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) { |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 867 | // If there's a canonical IV, just use it. |
| 868 | if (CanonicalIV) { |
| 869 | assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) && |
| 870 | "IVs with types different from the canonical IV should " |
| 871 | "already have been handled!"); |
| 872 | return CanonicalIV; |
| 873 | } |
| 874 | |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 875 | // Create and insert the PHI node for the induction variable in the |
| 876 | // specified loop. |
| 877 | BasicBlock *Header = L->getHeader(); |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 878 | PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 879 | rememberInstruction(PN); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 880 | |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 881 | Constant *One = ConstantInt::get(Ty, 1); |
Dan Gohman | 83d5774 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 882 | for (pred_iterator HPI = pred_begin(Header), HPE = pred_end(Header); |
| 883 | HPI != HPE; ++HPI) |
| 884 | if (L->contains(*HPI)) { |
Dan Gohman | 3abf905 | 2010-01-19 22:26:02 +0000 | [diff] [blame] | 885 | // Insert a unit add instruction right before the terminator |
| 886 | // corresponding to the back-edge. |
Dan Gohman | 83d5774 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 887 | Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next", |
| 888 | (*HPI)->getTerminator()); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 889 | rememberInstruction(Add); |
Dan Gohman | 83d5774 | 2009-09-27 17:46:40 +0000 | [diff] [blame] | 890 | PN->addIncoming(Add, *HPI); |
| 891 | } else { |
| 892 | PN->addIncoming(Constant::getNullValue(Ty), *HPI); |
| 893 | } |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 896 | // {0,+,F} --> {0,+,1} * F |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 897 | // Get the canonical induction variable I for this loop. |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 898 | Value *I = CanonicalIV ? |
| 899 | CanonicalIV : |
| 900 | getOrInsertCanonicalInductionVariable(L, Ty); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 901 | |
Chris Lattner | df14a04 | 2005-10-30 06:24:33 +0000 | [diff] [blame] | 902 | // If this is a simple linear addrec, emit it now as a special case. |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 903 | if (S->isAffine()) // {0,+,F} --> i*F |
| 904 | return |
| 905 | expand(SE.getTruncateOrNoop( |
| 906 | SE.getMulExpr(SE.getUnknown(I), |
| 907 | SE.getNoopOrAnyExtend(S->getOperand(1), |
| 908 | I->getType())), |
| 909 | Ty)); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 910 | |
| 911 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 912 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 913 | // simplify the expression without having to build a bunch of special code |
| 914 | // into this folder. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 915 | const SCEV *IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV. |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 916 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 917 | // Promote S up to the canonical IV type, if the cast is foldable. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 918 | const SCEV *NewS = S; |
| 919 | const SCEV *Ext = SE.getNoopOrAnyExtend(S, I->getType()); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 920 | if (isa<SCEVAddRecExpr>(Ext)) |
| 921 | NewS = Ext; |
| 922 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 923 | const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE); |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 924 | //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 925 | |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 926 | // Truncate the result down to the original type, if needed. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 927 | const SCEV *T = SE.getTruncateOrNoop(V, Ty); |
Dan Gohman | 469f3cd | 2009-06-22 22:08:45 +0000 | [diff] [blame] | 928 | return expand(T); |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 929 | } |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 930 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 931 | Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 932 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 933 | Value *V = expandCodeFor(S->getOperand(), |
| 934 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 935 | Value *I = Builder.CreateTrunc(V, Ty, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 936 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 937 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 940 | Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 941 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 942 | Value *V = expandCodeFor(S->getOperand(), |
| 943 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 944 | Value *I = Builder.CreateZExt(V, Ty, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 945 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 946 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 949 | Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) { |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 950 | const Type *Ty = SE.getEffectiveSCEVType(S->getType()); |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 951 | Value *V = expandCodeFor(S->getOperand(), |
| 952 | SE.getEffectiveSCEVType(S->getOperand()->getType())); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 953 | Value *I = Builder.CreateSExt(V, Ty, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 954 | rememberInstruction(I); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 955 | return I; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 956 | } |
| 957 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 958 | Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) { |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 959 | Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); |
| 960 | const Type *Ty = LHS->getType(); |
| 961 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 962 | // In the case of mixed integer and pointer types, do the |
| 963 | // rest of the comparisons as integer. |
| 964 | if (S->getOperand(i)->getType() != Ty) { |
| 965 | Ty = SE.getEffectiveSCEVType(Ty); |
| 966 | LHS = InsertNoopCastOfTo(LHS, Ty); |
| 967 | } |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 968 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 969 | Value *ICmp = Builder.CreateICmpSGT(LHS, RHS, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 970 | rememberInstruction(ICmp); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 971 | Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 972 | rememberInstruction(Sel); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 973 | LHS = Sel; |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 974 | } |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 975 | // In the case of mixed integer and pointer types, cast the |
| 976 | // final result back to the pointer type. |
| 977 | if (LHS->getType() != S->getType()) |
| 978 | LHS = InsertNoopCastOfTo(LHS, S->getType()); |
Nick Lewycky | c54c561 | 2007-11-25 22:41:31 +0000 | [diff] [blame] | 979 | return LHS; |
| 980 | } |
| 981 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 982 | Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) { |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 983 | Value *LHS = expand(S->getOperand(S->getNumOperands()-1)); |
| 984 | const Type *Ty = LHS->getType(); |
| 985 | for (int i = S->getNumOperands()-2; i >= 0; --i) { |
| 986 | // In the case of mixed integer and pointer types, do the |
| 987 | // rest of the comparisons as integer. |
| 988 | if (S->getOperand(i)->getType() != Ty) { |
| 989 | Ty = SE.getEffectiveSCEVType(Ty); |
| 990 | LHS = InsertNoopCastOfTo(LHS, Ty); |
| 991 | } |
Dan Gohman | 92fcdca | 2009-06-09 17:18:38 +0000 | [diff] [blame] | 992 | Value *RHS = expandCodeFor(S->getOperand(i), Ty); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 993 | Value *ICmp = Builder.CreateICmpUGT(LHS, RHS, "tmp"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 994 | rememberInstruction(ICmp); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 995 | Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax"); |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 996 | rememberInstruction(Sel); |
Dan Gohman | cf5ab82 | 2009-05-01 17:13:31 +0000 | [diff] [blame] | 997 | LHS = Sel; |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 998 | } |
Dan Gohman | 0196dc5 | 2009-07-14 20:57:04 +0000 | [diff] [blame] | 999 | // In the case of mixed integer and pointer types, cast the |
| 1000 | // final result back to the pointer type. |
| 1001 | if (LHS->getType() != S->getType()) |
| 1002 | LHS = InsertNoopCastOfTo(LHS, S->getType()); |
Nick Lewycky | 3e63076 | 2008-02-20 06:48:22 +0000 | [diff] [blame] | 1003 | return LHS; |
| 1004 | } |
| 1005 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1006 | Value *SCEVExpander::expandCodeFor(const SCEV *SH, const Type *Ty) { |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1007 | // Expand the code for this SCEV. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1008 | Value *V = expand(SH); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 1009 | if (Ty) { |
| 1010 | assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) && |
| 1011 | "non-trivial casts should be done with the SCEVs directly!"); |
| 1012 | V = InsertNoopCastOfTo(V, Ty); |
| 1013 | } |
| 1014 | return V; |
Dan Gohman | 11f6d3b | 2008-06-22 19:09:18 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 1017 | Value *SCEVExpander::expand(const SCEV *S) { |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1018 | // Compute an insertion point for this SCEV object. Hoist the instructions |
| 1019 | // as far out in the loop nest as possible. |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1020 | Instruction *InsertPt = Builder.GetInsertPoint(); |
| 1021 | for (Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock()); ; |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1022 | L = L->getParentLoop()) |
| 1023 | if (S->isLoopInvariant(L)) { |
| 1024 | if (!L) break; |
| 1025 | if (BasicBlock *Preheader = L->getLoopPreheader()) |
| 1026 | InsertPt = Preheader->getTerminator(); |
| 1027 | } else { |
| 1028 | // If the SCEV is computable at this level, insert it into the header |
| 1029 | // after the PHIs (and after any other instructions that we've inserted |
| 1030 | // there) so that it is guaranteed to dominate any user inside the loop. |
| 1031 | if (L && S->hasComputableLoopEvolution(L)) |
| 1032 | InsertPt = L->getHeader()->getFirstNonPHI(); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1033 | while (isInsertedInstruction(InsertPt)) |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1034 | InsertPt = llvm::next(BasicBlock::iterator(InsertPt)); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1035 | break; |
| 1036 | } |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1037 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1038 | // Check to see if we already expanded this here. |
| 1039 | std::map<std::pair<const SCEV *, Instruction *>, |
| 1040 | AssertingVH<Value> >::iterator I = |
| 1041 | InsertedExpressions.find(std::make_pair(S, InsertPt)); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1042 | if (I != InsertedExpressions.end()) |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1043 | return I->second; |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1044 | |
| 1045 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 1046 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
| 1047 | Builder.SetInsertPoint(InsertPt->getParent(), InsertPt); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1048 | |
| 1049 | // Expand the expression into instructions. |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1050 | Value *V = visit(S); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1051 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1052 | // Remember the expanded value for this SCEV at this location. |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 1053 | if (!PostIncLoop) |
| 1054 | InsertedExpressions[std::make_pair(S, InsertPt)] = V; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1055 | |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1056 | Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt); |
Anton Korobeynikov | 96fea33 | 2007-08-20 21:17:26 +0000 | [diff] [blame] | 1057 | return V; |
| 1058 | } |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1059 | |
| 1060 | /// getOrInsertCanonicalInductionVariable - This method returns the |
| 1061 | /// canonical induction variable of the specified type for the specified |
| 1062 | /// loop (inserting one if there is none). A canonical induction variable |
| 1063 | /// starts at zero and steps by one on each iteration. |
| 1064 | Value * |
| 1065 | SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L, |
| 1066 | const Type *Ty) { |
| 1067 | assert(Ty->isInteger() && "Can only insert integer induction variables!"); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1068 | const SCEV *H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty), |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1069 | SE.getIntegerSCEV(1, Ty), L); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1070 | BasicBlock *SaveInsertBB = Builder.GetInsertBlock(); |
| 1071 | BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint(); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1072 | Value *V = expandCodeFor(H, 0, L->getHeader()->begin()); |
Dan Gohman | 267a385 | 2009-06-27 21:18:18 +0000 | [diff] [blame] | 1073 | if (SaveInsertBB) |
| 1074 | Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt); |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 1075 | return V; |
Dan Gohman | 1d09de3 | 2009-06-05 16:35:53 +0000 | [diff] [blame] | 1076 | } |