blob: e3103fa84b6837a4ce1c6be859e13ed4f24166a9 [file] [log] [blame]
Nate Begeman36f891b2005-07-30 00:12:19 +00001//===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman36f891b2005-07-30 00:12:19 +00007//
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 Begeman36f891b2005-07-30 00:12:19 +000016#include "llvm/Analysis/ScalarEvolutionExpander.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000017#include "llvm/Analysis/LoopInfo.h"
Nate Begeman36f891b2005-07-30 00:12:19 +000018using namespace llvm;
19
Chris Lattnerca1a4be2006-02-04 09:51:53 +000020/// InsertCastOfTo - Insert a cast of V to the specified type, doing what
21/// we can to share the casts.
Reid Spencer3ba68b92006-12-13 08:06:42 +000022Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V,
23 const Type *Ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +000024 // Short-circuit unnecessary bitcasts.
25 if (opcode == Instruction::BitCast && V->getType() == Ty)
26 return V;
27
Dan Gohmanf04fa482009-04-16 15:52:57 +000028 // Short-circuit unnecessary inttoptr<->ptrtoint casts.
Dan Gohmanaf79fb52009-04-21 01:07:12 +000029 if ((opcode == Instruction::PtrToInt || opcode == Instruction::IntToPtr) &&
30 SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType()))
31 if (CastInst *CI = dyn_cast<CastInst>(V))
32 if ((CI->getOpcode() == Instruction::PtrToInt ||
33 CI->getOpcode() == Instruction::IntToPtr) &&
34 SE.getTypeSizeInBits(CI->getType()) ==
35 SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
36 return CI->getOperand(0);
Dan Gohmanf04fa482009-04-16 15:52:57 +000037
Chris Lattnerca1a4be2006-02-04 09:51:53 +000038 // FIXME: keep track of the cast instruction.
39 if (Constant *C = dyn_cast<Constant>(V))
Reid Spencerd977d862006-12-12 23:36:14 +000040 return ConstantExpr::getCast(opcode, C, Ty);
Chris Lattnerca1a4be2006-02-04 09:51:53 +000041
42 if (Argument *A = dyn_cast<Argument>(V)) {
43 // Check to see if there is already a cast!
44 for (Value::use_iterator UI = A->use_begin(), E = A->use_end();
45 UI != E; ++UI) {
46 if ((*UI)->getType() == Ty)
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000047 if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
48 if (CI->getOpcode() == opcode) {
49 // If the cast isn't the first instruction of the function, move it.
50 if (BasicBlock::iterator(CI) !=
51 A->getParent()->getEntryBlock().begin()) {
52 CI->moveBefore(A->getParent()->getEntryBlock().begin());
53 }
54 return CI;
Chris Lattnerca1a4be2006-02-04 09:51:53 +000055 }
Chris Lattnerca1a4be2006-02-04 09:51:53 +000056 }
Gabor Greif7cbd8a32008-05-16 19:29:10 +000057 return CastInst::Create(opcode, V, Ty, V->getName(),
Reid Spencerd977d862006-12-12 23:36:14 +000058 A->getParent()->getEntryBlock().begin());
Chris Lattnerca1a4be2006-02-04 09:51:53 +000059 }
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000060
Chris Lattnerca1a4be2006-02-04 09:51:53 +000061 Instruction *I = cast<Instruction>(V);
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000062
Chris Lattnerca1a4be2006-02-04 09:51:53 +000063 // Check to see if there is already a cast. If there is, use it.
64 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
65 UI != E; ++UI) {
66 if ((*UI)->getType() == Ty)
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000067 if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
68 if (CI->getOpcode() == opcode) {
69 BasicBlock::iterator It = I; ++It;
70 if (isa<InvokeInst>(I))
71 It = cast<InvokeInst>(I)->getNormalDest()->begin();
72 while (isa<PHINode>(It)) ++It;
73 if (It != BasicBlock::iterator(CI)) {
74 // Splice the cast immediately after the operand in question.
75 CI->moveBefore(It);
76 }
77 return CI;
Chris Lattnerca1a4be2006-02-04 09:51:53 +000078 }
Chris Lattnerca1a4be2006-02-04 09:51:53 +000079 }
80 BasicBlock::iterator IP = I; ++IP;
81 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
82 IP = II->getNormalDest()->begin();
83 while (isa<PHINode>(IP)) ++IP;
Gabor Greif7cbd8a32008-05-16 19:29:10 +000084 return CastInst::Create(opcode, V, Ty, V->getName(), IP);
Chris Lattnerca1a4be2006-02-04 09:51:53 +000085}
86
Dan Gohmanaf79fb52009-04-21 01:07:12 +000087/// InsertNoopCastOfTo - Insert a cast of V to the specified type,
88/// which must be possible with a noop cast.
89Value *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) {
90 Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false);
91 assert((Op == Instruction::BitCast ||
92 Op == Instruction::Instruction::PtrToInt ||
93 Op == Instruction::Instruction::IntToPtr) &&
94 "InsertNoopCastOfTo cannot perform non-noop casts!");
95 assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
96 "InsertNoopCastOfTo cannot change sizes!");
97 return InsertCastOfTo(Op, V, Ty);
98}
99
Chris Lattner7fec90e2007-04-13 05:04:18 +0000100/// InsertBinop - Insert the specified binary operator, doing a small amount
101/// of work to avoid inserting an obviously redundant operation.
102Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
Wojciech Matyjewicz8a087692008-06-15 19:07:39 +0000103 Value *RHS, Instruction *InsertPt) {
Dan Gohman0f0eb182007-06-15 19:21:55 +0000104 // Fold a binop with constant operands.
105 if (Constant *CLHS = dyn_cast<Constant>(LHS))
106 if (Constant *CRHS = dyn_cast<Constant>(RHS))
107 return ConstantExpr::get(Opcode, CLHS, CRHS);
108
Chris Lattner7fec90e2007-04-13 05:04:18 +0000109 // Do a quick scan to see if we have this binop nearby. If so, reuse it.
110 unsigned ScanLimit = 6;
Wojciech Matyjewicz8a087692008-06-15 19:07:39 +0000111 BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin();
112 if (InsertPt != BlockBegin) {
113 // Scanning starts from the last instruction before InsertPt.
114 BasicBlock::iterator IP = InsertPt;
115 --IP;
116 for (; ScanLimit; --IP, --ScanLimit) {
117 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(IP))
118 if (BinOp->getOpcode() == Opcode && BinOp->getOperand(0) == LHS &&
119 BinOp->getOperand(1) == RHS)
120 return BinOp;
121 if (IP == BlockBegin) break;
122 }
Chris Lattner7fec90e2007-04-13 05:04:18 +0000123 }
Wojciech Matyjewicz8a087692008-06-15 19:07:39 +0000124
125 // If we haven't found this binop, insert it.
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000126 return BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt);
Chris Lattner7fec90e2007-04-13 05:04:18 +0000127}
128
Dan Gohman890f92b2009-04-18 17:56:28 +0000129Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000130 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohmane24fa642008-06-18 16:37:11 +0000131 Value *V = expand(S->getOperand(S->getNumOperands()-1));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000132 V = InsertNoopCastOfTo(V, Ty);
Dan Gohmane24fa642008-06-18 16:37:11 +0000133
134 // Emit a bunch of add instructions
Dan Gohman2d1be872009-04-16 03:18:22 +0000135 for (int i = S->getNumOperands()-2; i >= 0; --i) {
136 Value *W = expand(S->getOperand(i));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000137 W = InsertNoopCastOfTo(W, Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000138 V = InsertBinop(Instruction::Add, V, W, InsertPt);
139 }
Dan Gohmane24fa642008-06-18 16:37:11 +0000140 return V;
141}
142
Dan Gohman890f92b2009-04-18 17:56:28 +0000143Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000144 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Nate Begeman36f891b2005-07-30 00:12:19 +0000145 int FirstOp = 0; // Set if we should emit a subtract.
Dan Gohman890f92b2009-04-18 17:56:28 +0000146 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
Nate Begeman36f891b2005-07-30 00:12:19 +0000147 if (SC->getValue()->isAllOnesValue())
148 FirstOp = 1;
149
150 int i = S->getNumOperands()-2;
Dan Gohmand19534a2007-06-15 14:38:12 +0000151 Value *V = expand(S->getOperand(i+1));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000152 V = InsertNoopCastOfTo(V, Ty);
Nate Begeman36f891b2005-07-30 00:12:19 +0000153
154 // Emit a bunch of multiply instructions
Dan Gohman2d1be872009-04-16 03:18:22 +0000155 for (; i >= FirstOp; --i) {
156 Value *W = expand(S->getOperand(i));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000157 W = InsertNoopCastOfTo(W, Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000158 V = InsertBinop(Instruction::Mul, V, W, InsertPt);
159 }
160
Nate Begeman36f891b2005-07-30 00:12:19 +0000161 // -1 * ... ---> 0 - ...
162 if (FirstOp == 1)
Dan Gohman2d1be872009-04-16 03:18:22 +0000163 V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt);
Nate Begeman36f891b2005-07-30 00:12:19 +0000164 return V;
165}
166
Dan Gohman890f92b2009-04-18 17:56:28 +0000167Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000168 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman2d1be872009-04-16 03:18:22 +0000169
Nick Lewycky6177fd42008-07-08 05:05:37 +0000170 Value *LHS = expand(S->getLHS());
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000171 LHS = InsertNoopCastOfTo(LHS, Ty);
Dan Gohman890f92b2009-04-18 17:56:28 +0000172 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
Nick Lewycky6177fd42008-07-08 05:05:37 +0000173 const APInt &RHS = SC->getValue()->getValue();
174 if (RHS.isPowerOf2())
175 return InsertBinop(Instruction::LShr, LHS,
Dan Gohman2d1be872009-04-16 03:18:22 +0000176 ConstantInt::get(Ty, RHS.logBase2()),
Nick Lewycky6177fd42008-07-08 05:05:37 +0000177 InsertPt);
178 }
179
180 Value *RHS = expand(S->getRHS());
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000181 RHS = InsertNoopCastOfTo(RHS, Ty);
Nick Lewycky6177fd42008-07-08 05:05:37 +0000182 return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
183}
184
Dan Gohman890f92b2009-04-18 17:56:28 +0000185Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000186 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Nate Begeman36f891b2005-07-30 00:12:19 +0000187 const Loop *L = S->getLoop();
Nate Begeman36f891b2005-07-30 00:12:19 +0000188
189 // {X,+,F} --> X + {0,+,F}
Dan Gohmancfeb6a42008-06-18 16:23:07 +0000190 if (!S->getStart()->isZero()) {
Dan Gohmand19534a2007-06-15 14:38:12 +0000191 Value *Start = expand(S->getStart());
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000192 Start = InsertNoopCastOfTo(Start, Ty);
Nate Begeman36f891b2005-07-30 00:12:19 +0000193 std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end());
Dan Gohman246b2562007-10-22 18:31:58 +0000194 NewOps[0] = SE.getIntegerSCEV(0, Ty);
195 Value *Rest = expand(SE.getAddRecExpr(NewOps, L));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000196 Rest = InsertNoopCastOfTo(Rest, Ty);
Nate Begeman36f891b2005-07-30 00:12:19 +0000197
198 // FIXME: look for an existing add to use.
Chris Lattner7fec90e2007-04-13 05:04:18 +0000199 return InsertBinop(Instruction::Add, Rest, Start, InsertPt);
Nate Begeman36f891b2005-07-30 00:12:19 +0000200 }
201
202 // {0,+,1} --> Insert a canonical induction variable into the loop!
Dan Gohman17f19722008-06-22 19:23:09 +0000203 if (S->isAffine() &&
Dan Gohman246b2562007-10-22 18:31:58 +0000204 S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) {
Nate Begeman36f891b2005-07-30 00:12:19 +0000205 // Create and insert the PHI node for the induction variable in the
206 // specified loop.
207 BasicBlock *Header = L->getHeader();
Gabor Greif051a9502008-04-06 20:25:17 +0000208 PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
Nate Begeman36f891b2005-07-30 00:12:19 +0000209 PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
210
211 pred_iterator HPI = pred_begin(Header);
212 assert(HPI != pred_end(Header) && "Loop with zero preds???");
213 if (!L->contains(*HPI)) ++HPI;
214 assert(HPI != pred_end(Header) && L->contains(*HPI) &&
215 "No backedge in loop?");
216
217 // Insert a unit add instruction right before the terminator corresponding
218 // to the back-edge.
Reid Spencer24d6da52007-01-21 00:29:26 +0000219 Constant *One = ConstantInt::get(Ty, 1);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000220 Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
Nate Begeman36f891b2005-07-30 00:12:19 +0000221 (*HPI)->getTerminator());
222
223 pred_iterator PI = pred_begin(Header);
224 if (*PI == L->getLoopPreheader())
225 ++PI;
226 PN->addIncoming(Add, *PI);
227 return PN;
228 }
229
230 // Get the canonical induction variable I for this loop.
231 Value *I = getOrInsertCanonicalInductionVariable(L, Ty);
232
Chris Lattnerdf14a042005-10-30 06:24:33 +0000233 // If this is a simple linear addrec, emit it now as a special case.
Dan Gohman17f19722008-06-22 19:23:09 +0000234 if (S->isAffine()) { // {0,+,F} --> i*F
Dan Gohmand19534a2007-06-15 14:38:12 +0000235 Value *F = expand(S->getOperand(1));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000236 F = InsertNoopCastOfTo(F, Ty);
Chris Lattnerdf14a042005-10-30 06:24:33 +0000237
238 // IF the step is by one, just return the inserted IV.
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000239 if (ConstantInt *CI = dyn_cast<ConstantInt>(F))
Reid Spencer4d050d72007-03-01 19:45:00 +0000240 if (CI->getValue() == 1)
Chris Lattnerdf14a042005-10-30 06:24:33 +0000241 return I;
242
243 // If the insert point is directly inside of the loop, emit the multiply at
244 // the insert point. Otherwise, L is a loop that is a parent of the insert
245 // point loop. If we can, move the multiply to the outer most loop that it
246 // is safe to be in.
247 Instruction *MulInsertPt = InsertPt;
248 Loop *InsertPtLoop = LI.getLoopFor(MulInsertPt->getParent());
249 if (InsertPtLoop != L && InsertPtLoop &&
250 L->contains(InsertPtLoop->getHeader())) {
Wojciech Matyjewicz5d2bc852008-06-14 16:48:22 +0000251 do {
Chris Lattnerdf14a042005-10-30 06:24:33 +0000252 // If we cannot hoist the multiply out of this loop, don't.
253 if (!InsertPtLoop->isLoopInvariant(F)) break;
254
Wojciech Matyjewicz5d2bc852008-06-14 16:48:22 +0000255 BasicBlock *InsertPtLoopPH = InsertPtLoop->getLoopPreheader();
256
257 // If this loop hasn't got a preheader, we aren't able to hoist the
258 // multiply.
259 if (!InsertPtLoopPH)
260 break;
261
262 // Otherwise, move the insert point to the preheader.
263 MulInsertPt = InsertPtLoopPH->getTerminator();
Chris Lattnerdf14a042005-10-30 06:24:33 +0000264 InsertPtLoop = InsertPtLoop->getParentLoop();
Wojciech Matyjewicz5d2bc852008-06-14 16:48:22 +0000265 } while (InsertPtLoop != L);
Chris Lattnerdf14a042005-10-30 06:24:33 +0000266 }
267
Chris Lattner7fec90e2007-04-13 05:04:18 +0000268 return InsertBinop(Instruction::Mul, I, F, MulInsertPt);
Nate Begeman36f891b2005-07-30 00:12:19 +0000269 }
270
271 // If this is a chain of recurrences, turn it into a closed form, using the
272 // folders, then expandCodeFor the closed form. This allows the folders to
273 // simplify the expression without having to build a bunch of special code
274 // into this folder.
Dan Gohman246b2562007-10-22 18:31:58 +0000275 SCEVHandle IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV.
Nate Begeman36f891b2005-07-30 00:12:19 +0000276
Dan Gohman246b2562007-10-22 18:31:58 +0000277 SCEVHandle V = S->evaluateAtIteration(IH, SE);
Bill Wendlinge8156192006-12-07 01:30:32 +0000278 //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n";
Nate Begeman36f891b2005-07-30 00:12:19 +0000279
Dan Gohmand19534a2007-06-15 14:38:12 +0000280 return expand(V);
Nate Begeman36f891b2005-07-30 00:12:19 +0000281}
Anton Korobeynikov96fea332007-08-20 21:17:26 +0000282
Dan Gohman890f92b2009-04-18 17:56:28 +0000283Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000284 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000285 Value *V = expand(S->getOperand());
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000286 V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType()));
287 return CastInst::CreateTruncOrBitCast(V, Ty, "tmp.", InsertPt);
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000288}
289
Dan Gohman890f92b2009-04-18 17:56:28 +0000290Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000291 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000292 Value *V = expand(S->getOperand());
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000293 V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType()));
Dan Gohman8170a682009-04-16 19:25:55 +0000294 return CastInst::CreateZExtOrBitCast(V, Ty, "tmp.", InsertPt);
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000295}
296
Dan Gohman890f92b2009-04-18 17:56:28 +0000297Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000298 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000299 Value *V = expand(S->getOperand());
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000300 V = InsertNoopCastOfTo(V, SE.getEffectiveSCEVType(V->getType()));
Dan Gohman8170a682009-04-16 19:25:55 +0000301 return CastInst::CreateSExtOrBitCast(V, Ty, "tmp.", InsertPt);
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000302}
303
Dan Gohman890f92b2009-04-18 17:56:28 +0000304Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000305 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Nick Lewyckyc54c5612007-11-25 22:41:31 +0000306 Value *LHS = expand(S->getOperand(0));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000307 LHS = InsertNoopCastOfTo(LHS, Ty);
Nick Lewyckyc54c5612007-11-25 22:41:31 +0000308 for (unsigned i = 1; i < S->getNumOperands(); ++i) {
309 Value *RHS = expand(S->getOperand(i));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000310 RHS = InsertNoopCastOfTo(RHS, Ty);
Nick Lewyckyc54c5612007-11-25 22:41:31 +0000311 Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt);
Gabor Greif051a9502008-04-06 20:25:17 +0000312 LHS = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt);
Nick Lewyckyc54c5612007-11-25 22:41:31 +0000313 }
314 return LHS;
315}
316
Dan Gohman890f92b2009-04-18 17:56:28 +0000317Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000318 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Nick Lewycky3e630762008-02-20 06:48:22 +0000319 Value *LHS = expand(S->getOperand(0));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000320 LHS = InsertNoopCastOfTo(LHS, Ty);
Nick Lewycky3e630762008-02-20 06:48:22 +0000321 for (unsigned i = 1; i < S->getNumOperands(); ++i) {
322 Value *RHS = expand(S->getOperand(i));
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000323 RHS = InsertNoopCastOfTo(RHS, Ty);
Nick Lewycky3e630762008-02-20 06:48:22 +0000324 Value *ICmp = new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt);
Gabor Greif051a9502008-04-06 20:25:17 +0000325 LHS = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt);
Nick Lewycky3e630762008-02-20 06:48:22 +0000326 }
327 return LHS;
328}
329
Dan Gohman2d1be872009-04-16 03:18:22 +0000330Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty,
331 Instruction *IP) {
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000332 // Expand the code for this SCEV.
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000333 assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
Dan Gohman2d1be872009-04-16 03:18:22 +0000334 "non-trivial casts should be done with the SCEVs directly!");
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000335 this->InsertPt = IP;
Dan Gohman2d1be872009-04-16 03:18:22 +0000336 Value *V = expand(SH);
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000337 return InsertNoopCastOfTo(V, Ty);
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000338}
339
Dan Gohman890f92b2009-04-18 17:56:28 +0000340Value *SCEVExpander::expand(const SCEV *S) {
Anton Korobeynikov96fea332007-08-20 21:17:26 +0000341 // Check to see if we already expanded this.
342 std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S);
343 if (I != InsertedExpressions.end())
344 return I->second;
345
346 Value *V = visit(S);
347 InsertedExpressions[S] = V;
348 return V;
349}