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