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