LSR: Fold redundant bitcasts on-the-fly.
[oota-llvm.git] / lib / Analysis / ScalarEvolutionExpander.cpp
1 //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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
16 #include "llvm/Analysis/ScalarEvolutionExpander.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/IntrinsicInst.h"
19 #include "llvm/LLVMContext.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Target/TargetData.h"
22 #include "llvm/ADT/STLExtras.h"
23
24 using namespace llvm;
25
26 /// ReuseOrCreateCast - Arrange for there to be a cast of V to Ty at IP,
27 /// reusing an existing cast if a suitable one exists, moving an existing
28 /// cast if a suitable one exists but isn't in the right place, or
29 /// creating a new one.
30 Value *SCEVExpander::ReuseOrCreateCast(Value *V, Type *Ty,
31                                        Instruction::CastOps Op,
32                                        BasicBlock::iterator IP) {
33   // Check to see if there is already a cast!
34   for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
35        UI != E; ++UI) {
36     User *U = *UI;
37     if (U->getType() == Ty)
38       if (CastInst *CI = dyn_cast<CastInst>(U))
39         if (CI->getOpcode() == Op) {
40           // If the cast isn't where we want it, fix it.
41           if (BasicBlock::iterator(CI) != IP) {
42             // Create a new cast, and leave the old cast in place in case
43             // it is being used as an insert point. Clear its operand
44             // so that it doesn't hold anything live.
45             Instruction *NewCI = CastInst::Create(Op, V, Ty, "", IP);
46             NewCI->takeName(CI);
47             CI->replaceAllUsesWith(NewCI);
48             CI->setOperand(0, UndefValue::get(V->getType()));
49             rememberInstruction(NewCI);
50             return NewCI;
51           }
52           rememberInstruction(CI);
53           return CI;
54         }
55   }
56
57   // Create a new cast.
58   Instruction *I = CastInst::Create(Op, V, Ty, V->getName(), IP);
59   rememberInstruction(I);
60   return I;
61 }
62
63 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
64 /// which must be possible with a noop cast, doing what we can to share
65 /// the casts.
66 Value *SCEVExpander::InsertNoopCastOfTo(Value *V, Type *Ty) {
67   Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false);
68   assert((Op == Instruction::BitCast ||
69           Op == Instruction::PtrToInt ||
70           Op == Instruction::IntToPtr) &&
71          "InsertNoopCastOfTo cannot perform non-noop casts!");
72   assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
73          "InsertNoopCastOfTo cannot change sizes!");
74
75   // Short-circuit unnecessary bitcasts.
76   if (Op == Instruction::BitCast) {
77     if (V->getType() == Ty)
78       return V;
79     if (CastInst *CI = dyn_cast<CastInst>(V)) {
80       if (CI->getOperand(0)->getType() == Ty)
81         return CI->getOperand(0);
82     }
83   }
84   // Short-circuit unnecessary inttoptr<->ptrtoint casts.
85   if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) &&
86       SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) {
87     if (CastInst *CI = dyn_cast<CastInst>(V))
88       if ((CI->getOpcode() == Instruction::PtrToInt ||
89            CI->getOpcode() == Instruction::IntToPtr) &&
90           SE.getTypeSizeInBits(CI->getType()) ==
91           SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
92         return CI->getOperand(0);
93     if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
94       if ((CE->getOpcode() == Instruction::PtrToInt ||
95            CE->getOpcode() == Instruction::IntToPtr) &&
96           SE.getTypeSizeInBits(CE->getType()) ==
97           SE.getTypeSizeInBits(CE->getOperand(0)->getType()))
98         return CE->getOperand(0);
99   }
100
101   // Fold a cast of a constant.
102   if (Constant *C = dyn_cast<Constant>(V))
103     return ConstantExpr::getCast(Op, C, Ty);
104
105   // Cast the argument at the beginning of the entry block, after
106   // any bitcasts of other arguments.
107   if (Argument *A = dyn_cast<Argument>(V)) {
108     BasicBlock::iterator IP = A->getParent()->getEntryBlock().begin();
109     while ((isa<BitCastInst>(IP) &&
110             isa<Argument>(cast<BitCastInst>(IP)->getOperand(0)) &&
111             cast<BitCastInst>(IP)->getOperand(0) != A) ||
112            isa<DbgInfoIntrinsic>(IP) ||
113            isa<LandingPadInst>(IP))
114       ++IP;
115     return ReuseOrCreateCast(A, Ty, Op, IP);
116   }
117
118   // Cast the instruction immediately after the instruction.
119   Instruction *I = cast<Instruction>(V);
120   BasicBlock::iterator IP = I; ++IP;
121   if (InvokeInst *II = dyn_cast<InvokeInst>(I))
122     IP = II->getNormalDest()->begin();
123   while (isa<PHINode>(IP) || isa<DbgInfoIntrinsic>(IP) ||
124          isa<LandingPadInst>(IP))
125     ++IP;
126   return ReuseOrCreateCast(I, Ty, Op, IP);
127 }
128
129 /// InsertBinop - Insert the specified binary operator, doing a small amount
130 /// of work to avoid inserting an obviously redundant operation.
131 Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
132                                  Value *LHS, Value *RHS) {
133   // Fold a binop with constant operands.
134   if (Constant *CLHS = dyn_cast<Constant>(LHS))
135     if (Constant *CRHS = dyn_cast<Constant>(RHS))
136       return ConstantExpr::get(Opcode, CLHS, CRHS);
137
138   // Do a quick scan to see if we have this binop nearby.  If so, reuse it.
139   unsigned ScanLimit = 6;
140   BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
141   // Scanning starts from the last instruction before the insertion point.
142   BasicBlock::iterator IP = Builder.GetInsertPoint();
143   if (IP != BlockBegin) {
144     --IP;
145     for (; ScanLimit; --IP, --ScanLimit) {
146       // Don't count dbg.value against the ScanLimit, to avoid perturbing the
147       // generated code.
148       if (isa<DbgInfoIntrinsic>(IP))
149         ScanLimit++;
150       if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS &&
151           IP->getOperand(1) == RHS)
152         return IP;
153       if (IP == BlockBegin) break;
154     }
155   }
156
157   // Save the original insertion point so we can restore it when we're done.
158   BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
159   BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
160
161   // Move the insertion point out of as many loops as we can.
162   while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) {
163     if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break;
164     BasicBlock *Preheader = L->getLoopPreheader();
165     if (!Preheader) break;
166
167     // Ok, move up a level.
168     Builder.SetInsertPoint(Preheader, Preheader->getTerminator());
169   }
170
171   // If we haven't found this binop, insert it.
172   Instruction *BO = cast<Instruction>(Builder.CreateBinOp(Opcode, LHS, RHS));
173   BO->setDebugLoc(SaveInsertPt->getDebugLoc());
174   rememberInstruction(BO);
175
176   // Restore the original insert point.
177   if (SaveInsertBB)
178     restoreInsertPoint(SaveInsertBB, SaveInsertPt);
179
180   return BO;
181 }
182
183 /// FactorOutConstant - Test if S is divisible by Factor, using signed
184 /// division. If so, update S with Factor divided out and return true.
185 /// S need not be evenly divisible if a reasonable remainder can be
186 /// computed.
187 /// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made
188 /// unnecessary; in its place, just signed-divide Ops[i] by the scale and
189 /// check to see if the divide was folded.
190 static bool FactorOutConstant(const SCEV *&S,
191                               const SCEV *&Remainder,
192                               const SCEV *Factor,
193                               ScalarEvolution &SE,
194                               const TargetData *TD) {
195   // Everything is divisible by one.
196   if (Factor->isOne())
197     return true;
198
199   // x/x == 1.
200   if (S == Factor) {
201     S = SE.getConstant(S->getType(), 1);
202     return true;
203   }
204
205   // For a Constant, check for a multiple of the given factor.
206   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
207     // 0/x == 0.
208     if (C->isZero())
209       return true;
210     // Check for divisibility.
211     if (const SCEVConstant *FC = dyn_cast<SCEVConstant>(Factor)) {
212       ConstantInt *CI =
213         ConstantInt::get(SE.getContext(),
214                          C->getValue()->getValue().sdiv(
215                                                    FC->getValue()->getValue()));
216       // If the quotient is zero and the remainder is non-zero, reject
217       // the value at this scale. It will be considered for subsequent
218       // smaller scales.
219       if (!CI->isZero()) {
220         const SCEV *Div = SE.getConstant(CI);
221         S = Div;
222         Remainder =
223           SE.getAddExpr(Remainder,
224                         SE.getConstant(C->getValue()->getValue().srem(
225                                                   FC->getValue()->getValue())));
226         return true;
227       }
228     }
229   }
230
231   // In a Mul, check if there is a constant operand which is a multiple
232   // of the given factor.
233   if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
234     if (TD) {
235       // With TargetData, the size is known. Check if there is a constant
236       // operand which is a multiple of the given factor. If so, we can
237       // factor it.
238       const SCEVConstant *FC = cast<SCEVConstant>(Factor);
239       if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0)))
240         if (!C->getValue()->getValue().srem(FC->getValue()->getValue())) {
241           SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end());
242           NewMulOps[0] =
243             SE.getConstant(C->getValue()->getValue().sdiv(
244                                                    FC->getValue()->getValue()));
245           S = SE.getMulExpr(NewMulOps);
246           return true;
247         }
248     } else {
249       // Without TargetData, check if Factor can be factored out of any of the
250       // Mul's operands. If so, we can just remove it.
251       for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
252         const SCEV *SOp = M->getOperand(i);
253         const SCEV *Remainder = SE.getConstant(SOp->getType(), 0);
254         if (FactorOutConstant(SOp, Remainder, Factor, SE, TD) &&
255             Remainder->isZero()) {
256           SmallVector<const SCEV *, 4> NewMulOps(M->op_begin(), M->op_end());
257           NewMulOps[i] = SOp;
258           S = SE.getMulExpr(NewMulOps);
259           return true;
260         }
261       }
262     }
263   }
264
265   // In an AddRec, check if both start and step are divisible.
266   if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
267     const SCEV *Step = A->getStepRecurrence(SE);
268     const SCEV *StepRem = SE.getConstant(Step->getType(), 0);
269     if (!FactorOutConstant(Step, StepRem, Factor, SE, TD))
270       return false;
271     if (!StepRem->isZero())
272       return false;
273     const SCEV *Start = A->getStart();
274     if (!FactorOutConstant(Start, Remainder, Factor, SE, TD))
275       return false;
276     // FIXME: can use A->getNoWrapFlags(FlagNW)
277     S = SE.getAddRecExpr(Start, Step, A->getLoop(), SCEV::FlagAnyWrap);
278     return true;
279   }
280
281   return false;
282 }
283
284 /// SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs
285 /// is the number of SCEVAddRecExprs present, which are kept at the end of
286 /// the list.
287 ///
288 static void SimplifyAddOperands(SmallVectorImpl<const SCEV *> &Ops,
289                                 Type *Ty,
290                                 ScalarEvolution &SE) {
291   unsigned NumAddRecs = 0;
292   for (unsigned i = Ops.size(); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1]); --i)
293     ++NumAddRecs;
294   // Group Ops into non-addrecs and addrecs.
295   SmallVector<const SCEV *, 8> NoAddRecs(Ops.begin(), Ops.end() - NumAddRecs);
296   SmallVector<const SCEV *, 8> AddRecs(Ops.end() - NumAddRecs, Ops.end());
297   // Let ScalarEvolution sort and simplify the non-addrecs list.
298   const SCEV *Sum = NoAddRecs.empty() ?
299                     SE.getConstant(Ty, 0) :
300                     SE.getAddExpr(NoAddRecs);
301   // If it returned an add, use the operands. Otherwise it simplified
302   // the sum into a single value, so just use that.
303   Ops.clear();
304   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum))
305     Ops.append(Add->op_begin(), Add->op_end());
306   else if (!Sum->isZero())
307     Ops.push_back(Sum);
308   // Then append the addrecs.
309   Ops.append(AddRecs.begin(), AddRecs.end());
310 }
311
312 /// SplitAddRecs - Flatten a list of add operands, moving addrec start values
313 /// out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}.
314 /// This helps expose more opportunities for folding parts of the expressions
315 /// into GEP indices.
316 ///
317 static void SplitAddRecs(SmallVectorImpl<const SCEV *> &Ops,
318                          Type *Ty,
319                          ScalarEvolution &SE) {
320   // Find the addrecs.
321   SmallVector<const SCEV *, 8> AddRecs;
322   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
323     while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) {
324       const SCEV *Start = A->getStart();
325       if (Start->isZero()) break;
326       const SCEV *Zero = SE.getConstant(Ty, 0);
327       AddRecs.push_back(SE.getAddRecExpr(Zero,
328                                          A->getStepRecurrence(SE),
329                                          A->getLoop(),
330                                          // FIXME: A->getNoWrapFlags(FlagNW)
331                                          SCEV::FlagAnyWrap));
332       if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) {
333         Ops[i] = Zero;
334         Ops.append(Add->op_begin(), Add->op_end());
335         e += Add->getNumOperands();
336       } else {
337         Ops[i] = Start;
338       }
339     }
340   if (!AddRecs.empty()) {
341     // Add the addrecs onto the end of the list.
342     Ops.append(AddRecs.begin(), AddRecs.end());
343     // Resort the operand list, moving any constants to the front.
344     SimplifyAddOperands(Ops, Ty, SE);
345   }
346 }
347
348 /// expandAddToGEP - Expand an addition expression with a pointer type into
349 /// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps
350 /// BasicAliasAnalysis and other passes analyze the result. See the rules
351 /// for getelementptr vs. inttoptr in
352 /// http://llvm.org/docs/LangRef.html#pointeraliasing
353 /// for details.
354 ///
355 /// Design note: The correctness of using getelementptr here depends on
356 /// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as
357 /// they may introduce pointer arithmetic which may not be safely converted
358 /// into getelementptr.
359 ///
360 /// Design note: It might seem desirable for this function to be more
361 /// loop-aware. If some of the indices are loop-invariant while others
362 /// aren't, it might seem desirable to emit multiple GEPs, keeping the
363 /// loop-invariant portions of the overall computation outside the loop.
364 /// However, there are a few reasons this is not done here. Hoisting simple
365 /// arithmetic is a low-level optimization that often isn't very
366 /// important until late in the optimization process. In fact, passes
367 /// like InstructionCombining will combine GEPs, even if it means
368 /// pushing loop-invariant computation down into loops, so even if the
369 /// GEPs were split here, the work would quickly be undone. The
370 /// LoopStrengthReduction pass, which is usually run quite late (and
371 /// after the last InstructionCombining pass), takes care of hoisting
372 /// loop-invariant portions of expressions, after considering what
373 /// can be folded using target addressing modes.
374 ///
375 Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin,
376                                     const SCEV *const *op_end,
377                                     PointerType *PTy,
378                                     Type *Ty,
379                                     Value *V) {
380   Type *ElTy = PTy->getElementType();
381   SmallVector<Value *, 4> GepIndices;
382   SmallVector<const SCEV *, 8> Ops(op_begin, op_end);
383   bool AnyNonZeroIndices = false;
384
385   // Split AddRecs up into parts as either of the parts may be usable
386   // without the other.
387   SplitAddRecs(Ops, Ty, SE);
388
389   // Descend down the pointer's type and attempt to convert the other
390   // operands into GEP indices, at each level. The first index in a GEP
391   // indexes into the array implied by the pointer operand; the rest of
392   // the indices index into the element or field type selected by the
393   // preceding index.
394   for (;;) {
395     // If the scale size is not 0, attempt to factor out a scale for
396     // array indexing.
397     SmallVector<const SCEV *, 8> ScaledOps;
398     if (ElTy->isSized()) {
399       const SCEV *ElSize = SE.getSizeOfExpr(ElTy);
400       if (!ElSize->isZero()) {
401         SmallVector<const SCEV *, 8> NewOps;
402         for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
403           const SCEV *Op = Ops[i];
404           const SCEV *Remainder = SE.getConstant(Ty, 0);
405           if (FactorOutConstant(Op, Remainder, ElSize, SE, SE.TD)) {
406             // Op now has ElSize factored out.
407             ScaledOps.push_back(Op);
408             if (!Remainder->isZero())
409               NewOps.push_back(Remainder);
410             AnyNonZeroIndices = true;
411           } else {
412             // The operand was not divisible, so add it to the list of operands
413             // we'll scan next iteration.
414             NewOps.push_back(Ops[i]);
415           }
416         }
417         // If we made any changes, update Ops.
418         if (!ScaledOps.empty()) {
419           Ops = NewOps;
420           SimplifyAddOperands(Ops, Ty, SE);
421         }
422       }
423     }
424
425     // Record the scaled array index for this level of the type. If
426     // we didn't find any operands that could be factored, tentatively
427     // assume that element zero was selected (since the zero offset
428     // would obviously be folded away).
429     Value *Scaled = ScaledOps.empty() ?
430                     Constant::getNullValue(Ty) :
431                     expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
432     GepIndices.push_back(Scaled);
433
434     // Collect struct field index operands.
435     while (StructType *STy = dyn_cast<StructType>(ElTy)) {
436       bool FoundFieldNo = false;
437       // An empty struct has no fields.
438       if (STy->getNumElements() == 0) break;
439       if (SE.TD) {
440         // With TargetData, field offsets are known. See if a constant offset
441         // falls within any of the struct fields.
442         if (Ops.empty()) break;
443         if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0]))
444           if (SE.getTypeSizeInBits(C->getType()) <= 64) {
445             const StructLayout &SL = *SE.TD->getStructLayout(STy);
446             uint64_t FullOffset = C->getValue()->getZExtValue();
447             if (FullOffset < SL.getSizeInBytes()) {
448               unsigned ElIdx = SL.getElementContainingOffset(FullOffset);
449               GepIndices.push_back(
450                   ConstantInt::get(Type::getInt32Ty(Ty->getContext()), ElIdx));
451               ElTy = STy->getTypeAtIndex(ElIdx);
452               Ops[0] =
453                 SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
454               AnyNonZeroIndices = true;
455               FoundFieldNo = true;
456             }
457           }
458       } else {
459         // Without TargetData, just check for an offsetof expression of the
460         // appropriate struct type.
461         for (unsigned i = 0, e = Ops.size(); i != e; ++i)
462           if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Ops[i])) {
463             Type *CTy;
464             Constant *FieldNo;
465             if (U->isOffsetOf(CTy, FieldNo) && CTy == STy) {
466               GepIndices.push_back(FieldNo);
467               ElTy =
468                 STy->getTypeAtIndex(cast<ConstantInt>(FieldNo)->getZExtValue());
469               Ops[i] = SE.getConstant(Ty, 0);
470               AnyNonZeroIndices = true;
471               FoundFieldNo = true;
472               break;
473             }
474           }
475       }
476       // If no struct field offsets were found, tentatively assume that
477       // field zero was selected (since the zero offset would obviously
478       // be folded away).
479       if (!FoundFieldNo) {
480         ElTy = STy->getTypeAtIndex(0u);
481         GepIndices.push_back(
482           Constant::getNullValue(Type::getInt32Ty(Ty->getContext())));
483       }
484     }
485
486     if (ArrayType *ATy = dyn_cast<ArrayType>(ElTy))
487       ElTy = ATy->getElementType();
488     else
489       break;
490   }
491
492   // If none of the operands were convertible to proper GEP indices, cast
493   // the base to i8* and do an ugly getelementptr with that. It's still
494   // better than ptrtoint+arithmetic+inttoptr at least.
495   if (!AnyNonZeroIndices) {
496     // Cast the base to i8*.
497     V = InsertNoopCastOfTo(V,
498        Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace()));
499
500     // Expand the operands for a plain byte offset.
501     Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty);
502
503     // Fold a GEP with constant operands.
504     if (Constant *CLHS = dyn_cast<Constant>(V))
505       if (Constant *CRHS = dyn_cast<Constant>(Idx))
506         return ConstantExpr::getGetElementPtr(CLHS, CRHS);
507
508     // Do a quick scan to see if we have this GEP nearby.  If so, reuse it.
509     unsigned ScanLimit = 6;
510     BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
511     // Scanning starts from the last instruction before the insertion point.
512     BasicBlock::iterator IP = Builder.GetInsertPoint();
513     if (IP != BlockBegin) {
514       --IP;
515       for (; ScanLimit; --IP, --ScanLimit) {
516         // Don't count dbg.value against the ScanLimit, to avoid perturbing the
517         // generated code.
518         if (isa<DbgInfoIntrinsic>(IP))
519           ScanLimit++;
520         if (IP->getOpcode() == Instruction::GetElementPtr &&
521             IP->getOperand(0) == V && IP->getOperand(1) == Idx)
522           return IP;
523         if (IP == BlockBegin) break;
524       }
525     }
526
527     // Save the original insertion point so we can restore it when we're done.
528     BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
529     BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
530
531     // Move the insertion point out of as many loops as we can.
532     while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) {
533       if (!L->isLoopInvariant(V) || !L->isLoopInvariant(Idx)) break;
534       BasicBlock *Preheader = L->getLoopPreheader();
535       if (!Preheader) break;
536
537       // Ok, move up a level.
538       Builder.SetInsertPoint(Preheader, Preheader->getTerminator());
539     }
540
541     // Emit a GEP.
542     Value *GEP = Builder.CreateGEP(V, Idx, "uglygep");
543     rememberInstruction(GEP);
544
545     // Restore the original insert point.
546     if (SaveInsertBB)
547       restoreInsertPoint(SaveInsertBB, SaveInsertPt);
548
549     return GEP;
550   }
551
552   // Save the original insertion point so we can restore it when we're done.
553   BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
554   BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
555
556   // Move the insertion point out of as many loops as we can.
557   while (const Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock())) {
558     if (!L->isLoopInvariant(V)) break;
559
560     bool AnyIndexNotLoopInvariant = false;
561     for (SmallVectorImpl<Value *>::const_iterator I = GepIndices.begin(),
562          E = GepIndices.end(); I != E; ++I)
563       if (!L->isLoopInvariant(*I)) {
564         AnyIndexNotLoopInvariant = true;
565         break;
566       }
567     if (AnyIndexNotLoopInvariant)
568       break;
569
570     BasicBlock *Preheader = L->getLoopPreheader();
571     if (!Preheader) break;
572
573     // Ok, move up a level.
574     Builder.SetInsertPoint(Preheader, Preheader->getTerminator());
575   }
576
577   // Insert a pretty getelementptr. Note that this GEP is not marked inbounds,
578   // because ScalarEvolution may have changed the address arithmetic to
579   // compute a value which is beyond the end of the allocated object.
580   Value *Casted = V;
581   if (V->getType() != PTy)
582     Casted = InsertNoopCastOfTo(Casted, PTy);
583   Value *GEP = Builder.CreateGEP(Casted,
584                                  GepIndices,
585                                  "scevgep");
586   Ops.push_back(SE.getUnknown(GEP));
587   rememberInstruction(GEP);
588
589   // Restore the original insert point.
590   if (SaveInsertBB)
591     restoreInsertPoint(SaveInsertBB, SaveInsertPt);
592
593   return expand(SE.getAddExpr(Ops));
594 }
595
596 /// isNonConstantNegative - Return true if the specified scev is negated, but
597 /// not a constant.
598 static bool isNonConstantNegative(const SCEV *F) {
599   const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(F);
600   if (!Mul) return false;
601
602   // If there is a constant factor, it will be first.
603   const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
604   if (!SC) return false;
605
606   // Return true if the value is negative, this matches things like (-42 * V).
607   return SC->getValue()->getValue().isNegative();
608 }
609
610 /// PickMostRelevantLoop - Given two loops pick the one that's most relevant for
611 /// SCEV expansion. If they are nested, this is the most nested. If they are
612 /// neighboring, pick the later.
613 static const Loop *PickMostRelevantLoop(const Loop *A, const Loop *B,
614                                         DominatorTree &DT) {
615   if (!A) return B;
616   if (!B) return A;
617   if (A->contains(B)) return B;
618   if (B->contains(A)) return A;
619   if (DT.dominates(A->getHeader(), B->getHeader())) return B;
620   if (DT.dominates(B->getHeader(), A->getHeader())) return A;
621   return A; // Arbitrarily break the tie.
622 }
623
624 /// getRelevantLoop - Get the most relevant loop associated with the given
625 /// expression, according to PickMostRelevantLoop.
626 const Loop *SCEVExpander::getRelevantLoop(const SCEV *S) {
627   // Test whether we've already computed the most relevant loop for this SCEV.
628   std::pair<DenseMap<const SCEV *, const Loop *>::iterator, bool> Pair =
629     RelevantLoops.insert(std::make_pair(S, static_cast<const Loop *>(0)));
630   if (!Pair.second)
631     return Pair.first->second;
632
633   if (isa<SCEVConstant>(S))
634     // A constant has no relevant loops.
635     return 0;
636   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
637     if (const Instruction *I = dyn_cast<Instruction>(U->getValue()))
638       return Pair.first->second = SE.LI->getLoopFor(I->getParent());
639     // A non-instruction has no relevant loops.
640     return 0;
641   }
642   if (const SCEVNAryExpr *N = dyn_cast<SCEVNAryExpr>(S)) {
643     const Loop *L = 0;
644     if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
645       L = AR->getLoop();
646     for (SCEVNAryExpr::op_iterator I = N->op_begin(), E = N->op_end();
647          I != E; ++I)
648       L = PickMostRelevantLoop(L, getRelevantLoop(*I), *SE.DT);
649     return RelevantLoops[N] = L;
650   }
651   if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) {
652     const Loop *Result = getRelevantLoop(C->getOperand());
653     return RelevantLoops[C] = Result;
654   }
655   if (const SCEVUDivExpr *D = dyn_cast<SCEVUDivExpr>(S)) {
656     const Loop *Result =
657       PickMostRelevantLoop(getRelevantLoop(D->getLHS()),
658                            getRelevantLoop(D->getRHS()),
659                            *SE.DT);
660     return RelevantLoops[D] = Result;
661   }
662   llvm_unreachable("Unexpected SCEV type!");
663   return 0;
664 }
665
666 namespace {
667
668 /// LoopCompare - Compare loops by PickMostRelevantLoop.
669 class LoopCompare {
670   DominatorTree &DT;
671 public:
672   explicit LoopCompare(DominatorTree &dt) : DT(dt) {}
673
674   bool operator()(std::pair<const Loop *, const SCEV *> LHS,
675                   std::pair<const Loop *, const SCEV *> RHS) const {
676     // Keep pointer operands sorted at the end.
677     if (LHS.second->getType()->isPointerTy() !=
678         RHS.second->getType()->isPointerTy())
679       return LHS.second->getType()->isPointerTy();
680
681     // Compare loops with PickMostRelevantLoop.
682     if (LHS.first != RHS.first)
683       return PickMostRelevantLoop(LHS.first, RHS.first, DT) != LHS.first;
684
685     // If one operand is a non-constant negative and the other is not,
686     // put the non-constant negative on the right so that a sub can
687     // be used instead of a negate and add.
688     if (isNonConstantNegative(LHS.second)) {
689       if (!isNonConstantNegative(RHS.second))
690         return false;
691     } else if (isNonConstantNegative(RHS.second))
692       return true;
693
694     // Otherwise they are equivalent according to this comparison.
695     return false;
696   }
697 };
698
699 }
700
701 Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
702   Type *Ty = SE.getEffectiveSCEVType(S->getType());
703
704   // Collect all the add operands in a loop, along with their associated loops.
705   // Iterate in reverse so that constants are emitted last, all else equal, and
706   // so that pointer operands are inserted first, which the code below relies on
707   // to form more involved GEPs.
708   SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
709   for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(S->op_end()),
710        E(S->op_begin()); I != E; ++I)
711     OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I));
712
713   // Sort by loop. Use a stable sort so that constants follow non-constants and
714   // pointer operands precede non-pointer operands.
715   std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT));
716
717   // Emit instructions to add all the operands. Hoist as much as possible
718   // out of loops, and form meaningful getelementptrs where possible.
719   Value *Sum = 0;
720   for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator
721        I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) {
722     const Loop *CurLoop = I->first;
723     const SCEV *Op = I->second;
724     if (!Sum) {
725       // This is the first operand. Just expand it.
726       Sum = expand(Op);
727       ++I;
728     } else if (PointerType *PTy = dyn_cast<PointerType>(Sum->getType())) {
729       // The running sum expression is a pointer. Try to form a getelementptr
730       // at this level with that as the base.
731       SmallVector<const SCEV *, 4> NewOps;
732       for (; I != E && I->first == CurLoop; ++I) {
733         // If the operand is SCEVUnknown and not instructions, peek through
734         // it, to enable more of it to be folded into the GEP.
735         const SCEV *X = I->second;
736         if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(X))
737           if (!isa<Instruction>(U->getValue()))
738             X = SE.getSCEV(U->getValue());
739         NewOps.push_back(X);
740       }
741       Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, Sum);
742     } else if (PointerType *PTy = dyn_cast<PointerType>(Op->getType())) {
743       // The running sum is an integer, and there's a pointer at this level.
744       // Try to form a getelementptr. If the running sum is instructions,
745       // use a SCEVUnknown to avoid re-analyzing them.
746       SmallVector<const SCEV *, 4> NewOps;
747       NewOps.push_back(isa<Instruction>(Sum) ? SE.getUnknown(Sum) :
748                                                SE.getSCEV(Sum));
749       for (++I; I != E && I->first == CurLoop; ++I)
750         NewOps.push_back(I->second);
751       Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, expand(Op));
752     } else if (isNonConstantNegative(Op)) {
753       // Instead of doing a negate and add, just do a subtract.
754       Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty);
755       Sum = InsertNoopCastOfTo(Sum, Ty);
756       Sum = InsertBinop(Instruction::Sub, Sum, W);
757       ++I;
758     } else {
759       // A simple add.
760       Value *W = expandCodeFor(Op, Ty);
761       Sum = InsertNoopCastOfTo(Sum, Ty);
762       // Canonicalize a constant to the RHS.
763       if (isa<Constant>(Sum)) std::swap(Sum, W);
764       Sum = InsertBinop(Instruction::Add, Sum, W);
765       ++I;
766     }
767   }
768
769   return Sum;
770 }
771
772 Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
773   Type *Ty = SE.getEffectiveSCEVType(S->getType());
774
775   // Collect all the mul operands in a loop, along with their associated loops.
776   // Iterate in reverse so that constants are emitted last, all else equal.
777   SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
778   for (std::reverse_iterator<SCEVMulExpr::op_iterator> I(S->op_end()),
779        E(S->op_begin()); I != E; ++I)
780     OpsAndLoops.push_back(std::make_pair(getRelevantLoop(*I), *I));
781
782   // Sort by loop. Use a stable sort so that constants follow non-constants.
783   std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT));
784
785   // Emit instructions to mul all the operands. Hoist as much as possible
786   // out of loops.
787   Value *Prod = 0;
788   for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator
789        I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) {
790     const SCEV *Op = I->second;
791     if (!Prod) {
792       // This is the first operand. Just expand it.
793       Prod = expand(Op);
794       ++I;
795     } else if (Op->isAllOnesValue()) {
796       // Instead of doing a multiply by negative one, just do a negate.
797       Prod = InsertNoopCastOfTo(Prod, Ty);
798       Prod = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), Prod);
799       ++I;
800     } else {
801       // A simple mul.
802       Value *W = expandCodeFor(Op, Ty);
803       Prod = InsertNoopCastOfTo(Prod, Ty);
804       // Canonicalize a constant to the RHS.
805       if (isa<Constant>(Prod)) std::swap(Prod, W);
806       Prod = InsertBinop(Instruction::Mul, Prod, W);
807       ++I;
808     }
809   }
810
811   return Prod;
812 }
813
814 Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
815   Type *Ty = SE.getEffectiveSCEVType(S->getType());
816
817   Value *LHS = expandCodeFor(S->getLHS(), Ty);
818   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
819     const APInt &RHS = SC->getValue()->getValue();
820     if (RHS.isPowerOf2())
821       return InsertBinop(Instruction::LShr, LHS,
822                          ConstantInt::get(Ty, RHS.logBase2()));
823   }
824
825   Value *RHS = expandCodeFor(S->getRHS(), Ty);
826   return InsertBinop(Instruction::UDiv, LHS, RHS);
827 }
828
829 /// Move parts of Base into Rest to leave Base with the minimal
830 /// expression that provides a pointer operand suitable for a
831 /// GEP expansion.
832 static void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest,
833                               ScalarEvolution &SE) {
834   while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) {
835     Base = A->getStart();
836     Rest = SE.getAddExpr(Rest,
837                          SE.getAddRecExpr(SE.getConstant(A->getType(), 0),
838                                           A->getStepRecurrence(SE),
839                                           A->getLoop(),
840                                           // FIXME: A->getNoWrapFlags(FlagNW)
841                                           SCEV::FlagAnyWrap));
842   }
843   if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) {
844     Base = A->getOperand(A->getNumOperands()-1);
845     SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end());
846     NewAddOps.back() = Rest;
847     Rest = SE.getAddExpr(NewAddOps);
848     ExposePointerBase(Base, Rest, SE);
849   }
850 }
851
852 /// Determine if this is a well-behaved chain of instructions leading back to
853 /// the PHI. If so, it may be reused by expanded expressions.
854 bool SCEVExpander::isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV,
855                                          const Loop *L) {
856   if (IncV->getNumOperands() == 0 || isa<PHINode>(IncV) ||
857       (isa<CastInst>(IncV) && !isa<BitCastInst>(IncV)))
858     return false;
859   // If any of the operands don't dominate the insert position, bail.
860   // Addrec operands are always loop-invariant, so this can only happen
861   // if there are instructions which haven't been hoisted.
862   if (L == IVIncInsertLoop) {
863     for (User::op_iterator OI = IncV->op_begin()+1,
864            OE = IncV->op_end(); OI != OE; ++OI)
865       if (Instruction *OInst = dyn_cast<Instruction>(OI))
866         if (!SE.DT->dominates(OInst, IVIncInsertPos))
867           return false;
868   }
869   // Advance to the next instruction.
870   IncV = dyn_cast<Instruction>(IncV->getOperand(0));
871   if (!IncV)
872     return false;
873
874   if (IncV->mayHaveSideEffects())
875     return false;
876
877   if (IncV != PN)
878     return true;
879
880   return isNormalAddRecExprPHI(PN, IncV, L);
881 }
882
883 /// Determine if this cyclic phi is in a form that would have been generated by
884 /// LSR. We don't care if the phi was actually expanded in this pass, as long
885 /// as it is in a low-cost form, for example, no implied multiplication. This
886 /// should match any patterns generated by getAddRecExprPHILiterally and
887 /// expandAddtoGEP.
888 bool SCEVExpander::isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV,
889                                            const Loop *L) {
890   switch (IncV->getOpcode()) {
891   // Check for a simple Add/Sub or GEP of a loop invariant step.
892   case Instruction::Add:
893   case Instruction::Sub:
894     return IncV->getOperand(0) == PN
895       && L->isLoopInvariant(IncV->getOperand(1));
896   case Instruction::BitCast:
897     IncV = dyn_cast<GetElementPtrInst>(IncV->getOperand(0));
898     if (!IncV)
899       return false;
900     // fall-thru to GEP handling
901   case Instruction::GetElementPtr: {
902     // This must be a pointer addition of constants (pretty) or some number of
903     // address-size elements (ugly).
904     for (Instruction::op_iterator I = IncV->op_begin()+1, E = IncV->op_end();
905          I != E; ++I) {
906       if (isa<Constant>(*I))
907         continue;
908       // ugly geps have 2 operands.
909       // i1* is used by the expander to represent an address-size element.
910       if (IncV->getNumOperands() != 2)
911         return false;
912       unsigned AS = cast<PointerType>(IncV->getType())->getAddressSpace();
913       if (IncV->getType() != Type::getInt1PtrTy(SE.getContext(), AS)
914           && IncV->getType() != Type::getInt8PtrTy(SE.getContext(), AS))
915         return false;
916       // Ensure the operands dominate the insertion point. I don't know of a
917       // case when this would not be true, so this is somewhat untested.
918       if (L == IVIncInsertLoop) {
919         for (User::op_iterator OI = IncV->op_begin()+1,
920                OE = IncV->op_end(); OI != OE; ++OI)
921           if (Instruction *OInst = dyn_cast<Instruction>(OI))
922             if (!SE.DT->dominates(OInst, IVIncInsertPos))
923               return false;
924       }
925       break;
926     }
927     IncV = dyn_cast<Instruction>(IncV->getOperand(0));
928     if (IncV && IncV->getOpcode() == Instruction::BitCast)
929       IncV = dyn_cast<Instruction>(IncV->getOperand(0));
930     return IncV == PN;
931   }
932   default:
933     return false;
934   }
935 }
936
937 /// expandIVInc - Expand an IV increment at Builder's current InsertPos.
938 /// Typically this is the LatchBlock terminator or IVIncInsertPos, but we may
939 /// need to materialize IV increments elsewhere to handle difficult situations.
940 Value *SCEVExpander::expandIVInc(PHINode *PN, Value *StepV, const Loop *L,
941                                  Type *ExpandTy, Type *IntTy,
942                                  bool useSubtract) {
943   Value *IncV;
944   // If the PHI is a pointer, use a GEP, otherwise use an add or sub.
945   if (ExpandTy->isPointerTy()) {
946     PointerType *GEPPtrTy = cast<PointerType>(ExpandTy);
947     // If the step isn't constant, don't use an implicitly scaled GEP, because
948     // that would require a multiply inside the loop.
949     if (!isa<ConstantInt>(StepV))
950       GEPPtrTy = PointerType::get(Type::getInt1Ty(SE.getContext()),
951                                   GEPPtrTy->getAddressSpace());
952     const SCEV *const StepArray[1] = { SE.getSCEV(StepV) };
953     IncV = expandAddToGEP(StepArray, StepArray+1, GEPPtrTy, IntTy, PN);
954     if (IncV->getType() != PN->getType()) {
955       IncV = Builder.CreateBitCast(IncV, PN->getType());
956       rememberInstruction(IncV);
957     }
958   } else {
959     IncV = useSubtract ?
960       Builder.CreateSub(PN, StepV, Twine(IVName) + ".iv.next") :
961       Builder.CreateAdd(PN, StepV, Twine(IVName) + ".iv.next");
962     rememberInstruction(IncV);
963   }
964   return IncV;
965 }
966
967 /// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand
968 /// the base addrec, which is the addrec without any non-loop-dominating
969 /// values, and return the PHI.
970 PHINode *
971 SCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
972                                         const Loop *L,
973                                         Type *ExpandTy,
974                                         Type *IntTy) {
975   assert((!IVIncInsertLoop||IVIncInsertPos) && "Uninitialized insert position");
976
977   // Reuse a previously-inserted PHI, if present.
978   BasicBlock *LatchBlock = L->getLoopLatch();
979   if (LatchBlock) {
980     for (BasicBlock::iterator I = L->getHeader()->begin();
981          PHINode *PN = dyn_cast<PHINode>(I); ++I) {
982       if (!SE.isSCEVable(PN->getType()) ||
983           (SE.getEffectiveSCEVType(PN->getType()) !=
984            SE.getEffectiveSCEVType(Normalized->getType())) ||
985           SE.getSCEV(PN) != Normalized)
986         continue;
987
988       Instruction *IncV =
989         cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock));
990
991       if (LSRMode) {
992         if (!isExpandedAddRecExprPHI(PN, IncV, L))
993           continue;
994       }
995       else {
996         if (!isNormalAddRecExprPHI(PN, IncV, L))
997           continue;
998       }
999       // Ok, the add recurrence looks usable.
1000       // Remember this PHI, even in post-inc mode.
1001       InsertedValues.insert(PN);
1002       // Remember the increment.
1003       rememberInstruction(IncV);
1004       if (L == IVIncInsertLoop)
1005         do {
1006           if (SE.DT->dominates(IncV, IVIncInsertPos))
1007             break;
1008           // Make sure the increment is where we want it. But don't move it
1009           // down past a potential existing post-inc user.
1010           IncV->moveBefore(IVIncInsertPos);
1011           IVIncInsertPos = IncV;
1012           IncV = cast<Instruction>(IncV->getOperand(0));
1013         } while (IncV != PN);
1014       return PN;
1015     }
1016   }
1017
1018   // Save the original insertion point so we can restore it when we're done.
1019   BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
1020   BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
1021
1022   // Expand code for the start value.
1023   Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy,
1024                                 L->getHeader()->begin());
1025
1026   // StartV must be hoisted into L's preheader to dominate the new phi.
1027   assert(!isa<Instruction>(StartV) ||
1028          SE.DT->properlyDominates(cast<Instruction>(StartV)->getParent(),
1029                                   L->getHeader()));
1030
1031   // Expand code for the step value. Do this before creating the PHI so that PHI
1032   // reuse code doesn't see an incomplete PHI.
1033   const SCEV *Step = Normalized->getStepRecurrence(SE);
1034   // If the stride is negative, insert a sub instead of an add for the increment
1035   // (unless it's a constant, because subtracts of constants are canonicalized
1036   // to adds).
1037   bool useSubtract = !ExpandTy->isPointerTy() && isNonConstantNegative(Step);
1038   if (useSubtract)
1039     Step = SE.getNegativeSCEV(Step);
1040   // Expand the step somewhere that dominates the loop header.
1041   Value *StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin());
1042
1043   // Create the PHI.
1044   BasicBlock *Header = L->getHeader();
1045   Builder.SetInsertPoint(Header, Header->begin());
1046   pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header);
1047   PHINode *PN = Builder.CreatePHI(ExpandTy, std::distance(HPB, HPE),
1048                                   Twine(IVName) + ".iv");
1049   rememberInstruction(PN);
1050
1051   // Create the step instructions and populate the PHI.
1052   for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) {
1053     BasicBlock *Pred = *HPI;
1054
1055     // Add a start value.
1056     if (!L->contains(Pred)) {
1057       PN->addIncoming(StartV, Pred);
1058       continue;
1059     }
1060
1061     // Create a step value and add it to the PHI.
1062     // If IVIncInsertLoop is non-null and equal to the addrec's loop, insert the
1063     // instructions at IVIncInsertPos.
1064     Instruction *InsertPos = L == IVIncInsertLoop ?
1065       IVIncInsertPos : Pred->getTerminator();
1066     Builder.SetInsertPoint(InsertPos);
1067     Value *IncV = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract);
1068
1069     PN->addIncoming(IncV, Pred);
1070   }
1071
1072   // Restore the original insert point.
1073   if (SaveInsertBB)
1074     restoreInsertPoint(SaveInsertBB, SaveInsertPt);
1075
1076   // Remember this PHI, even in post-inc mode.
1077   InsertedValues.insert(PN);
1078
1079   return PN;
1080 }
1081
1082 Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
1083   Type *STy = S->getType();
1084   Type *IntTy = SE.getEffectiveSCEVType(STy);
1085   const Loop *L = S->getLoop();
1086
1087   // Determine a normalized form of this expression, which is the expression
1088   // before any post-inc adjustment is made.
1089   const SCEVAddRecExpr *Normalized = S;
1090   if (PostIncLoops.count(L)) {
1091     PostIncLoopSet Loops;
1092     Loops.insert(L);
1093     Normalized =
1094       cast<SCEVAddRecExpr>(TransformForPostIncUse(Normalize, S, 0, 0,
1095                                                   Loops, SE, *SE.DT));
1096   }
1097
1098   // Strip off any non-loop-dominating component from the addrec start.
1099   const SCEV *Start = Normalized->getStart();
1100   const SCEV *PostLoopOffset = 0;
1101   if (!SE.properlyDominates(Start, L->getHeader())) {
1102     PostLoopOffset = Start;
1103     Start = SE.getConstant(Normalized->getType(), 0);
1104     Normalized = cast<SCEVAddRecExpr>(
1105       SE.getAddRecExpr(Start, Normalized->getStepRecurrence(SE),
1106                        Normalized->getLoop(),
1107                        // FIXME: Normalized->getNoWrapFlags(FlagNW)
1108                        SCEV::FlagAnyWrap));
1109   }
1110
1111   // Strip off any non-loop-dominating component from the addrec step.
1112   const SCEV *Step = Normalized->getStepRecurrence(SE);
1113   const SCEV *PostLoopScale = 0;
1114   if (!SE.dominates(Step, L->getHeader())) {
1115     PostLoopScale = Step;
1116     Step = SE.getConstant(Normalized->getType(), 1);
1117     Normalized =
1118       cast<SCEVAddRecExpr>(SE.getAddRecExpr(Start, Step,
1119                                             Normalized->getLoop(),
1120                                             // FIXME: Normalized
1121                                             // ->getNoWrapFlags(FlagNW)
1122                                             SCEV::FlagAnyWrap));
1123   }
1124
1125   // Expand the core addrec. If we need post-loop scaling, force it to
1126   // expand to an integer type to avoid the need for additional casting.
1127   Type *ExpandTy = PostLoopScale ? IntTy : STy;
1128   PHINode *PN = getAddRecExprPHILiterally(Normalized, L, ExpandTy, IntTy);
1129
1130   // Accommodate post-inc mode, if necessary.
1131   Value *Result;
1132   if (!PostIncLoops.count(L))
1133     Result = PN;
1134   else {
1135     // In PostInc mode, use the post-incremented value.
1136     BasicBlock *LatchBlock = L->getLoopLatch();
1137     assert(LatchBlock && "PostInc mode requires a unique loop latch!");
1138     Result = PN->getIncomingValueForBlock(LatchBlock);
1139
1140     // For an expansion to use the postinc form, the client must call
1141     // expandCodeFor with an InsertPoint that is either outside the PostIncLoop
1142     // or dominated by IVIncInsertPos.
1143     if (isa<Instruction>(Result)
1144         && !SE.DT->dominates(cast<Instruction>(Result),
1145                              Builder.GetInsertPoint())) {
1146       // The induction variable's postinc expansion does not dominate this use.
1147       // IVUsers tries to prevent this case, so it is rare. However, it can
1148       // happen when an IVUser outside the loop is not dominated by the latch
1149       // block. Adjusting IVIncInsertPos before expansion begins cannot handle
1150       // all cases. Consider a phi outide whose operand is replaced during
1151       // expansion with the value of the postinc user. Without fundamentally
1152       // changing the way postinc users are tracked, the only remedy is
1153       // inserting an extra IV increment. StepV might fold into PostLoopOffset,
1154       // but hopefully expandCodeFor handles that.
1155       bool useSubtract =
1156         !ExpandTy->isPointerTy() && isNonConstantNegative(Step);
1157       if (useSubtract)
1158         Step = SE.getNegativeSCEV(Step);
1159       // Expand the step somewhere that dominates the loop header.
1160       BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
1161       BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
1162       Value *StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin());
1163       // Restore the insertion point to the place where the caller has
1164       // determined dominates all uses.
1165       restoreInsertPoint(SaveInsertBB, SaveInsertPt);
1166       Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract);
1167     }
1168   }
1169
1170   // Re-apply any non-loop-dominating scale.
1171   if (PostLoopScale) {
1172     Result = InsertNoopCastOfTo(Result, IntTy);
1173     Result = Builder.CreateMul(Result,
1174                                expandCodeFor(PostLoopScale, IntTy));
1175     rememberInstruction(Result);
1176   }
1177
1178   // Re-apply any non-loop-dominating offset.
1179   if (PostLoopOffset) {
1180     if (PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) {
1181       const SCEV *const OffsetArray[1] = { PostLoopOffset };
1182       Result = expandAddToGEP(OffsetArray, OffsetArray+1, PTy, IntTy, Result);
1183     } else {
1184       Result = InsertNoopCastOfTo(Result, IntTy);
1185       Result = Builder.CreateAdd(Result,
1186                                  expandCodeFor(PostLoopOffset, IntTy));
1187       rememberInstruction(Result);
1188     }
1189   }
1190
1191   return Result;
1192 }
1193
1194 Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
1195   if (!CanonicalMode) return expandAddRecExprLiterally(S);
1196
1197   Type *Ty = SE.getEffectiveSCEVType(S->getType());
1198   const Loop *L = S->getLoop();
1199
1200   // First check for an existing canonical IV in a suitable type.
1201   PHINode *CanonicalIV = 0;
1202   if (PHINode *PN = L->getCanonicalInductionVariable())
1203     if (SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty))
1204       CanonicalIV = PN;
1205
1206   // Rewrite an AddRec in terms of the canonical induction variable, if
1207   // its type is more narrow.
1208   if (CanonicalIV &&
1209       SE.getTypeSizeInBits(CanonicalIV->getType()) >
1210       SE.getTypeSizeInBits(Ty)) {
1211     SmallVector<const SCEV *, 4> NewOps(S->getNumOperands());
1212     for (unsigned i = 0, e = S->getNumOperands(); i != e; ++i)
1213       NewOps[i] = SE.getAnyExtendExpr(S->op_begin()[i], CanonicalIV->getType());
1214     Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop(),
1215                                        // FIXME: S->getNoWrapFlags(FlagNW)
1216                                        SCEV::FlagAnyWrap));
1217     BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
1218     BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
1219     BasicBlock::iterator NewInsertPt =
1220       llvm::next(BasicBlock::iterator(cast<Instruction>(V)));
1221     while (isa<PHINode>(NewInsertPt) || isa<DbgInfoIntrinsic>(NewInsertPt) ||
1222            isa<LandingPadInst>(NewInsertPt))
1223       ++NewInsertPt;
1224     V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0,
1225                       NewInsertPt);
1226     restoreInsertPoint(SaveInsertBB, SaveInsertPt);
1227     return V;
1228   }
1229
1230   // {X,+,F} --> X + {0,+,F}
1231   if (!S->getStart()->isZero()) {
1232     SmallVector<const SCEV *, 4> NewOps(S->op_begin(), S->op_end());
1233     NewOps[0] = SE.getConstant(Ty, 0);
1234     // FIXME: can use S->getNoWrapFlags()
1235     const SCEV *Rest = SE.getAddRecExpr(NewOps, L, SCEV::FlagAnyWrap);
1236
1237     // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
1238     // comments on expandAddToGEP for details.
1239     const SCEV *Base = S->getStart();
1240     const SCEV *RestArray[1] = { Rest };
1241     // Dig into the expression to find the pointer base for a GEP.
1242     ExposePointerBase(Base, RestArray[0], SE);
1243     // If we found a pointer, expand the AddRec with a GEP.
1244     if (PointerType *PTy = dyn_cast<PointerType>(Base->getType())) {
1245       // Make sure the Base isn't something exotic, such as a multiplied
1246       // or divided pointer value. In those cases, the result type isn't
1247       // actually a pointer type.
1248       if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) {
1249         Value *StartV = expand(Base);
1250         assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!");
1251         return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV);
1252       }
1253     }
1254
1255     // Just do a normal add. Pre-expand the operands to suppress folding.
1256     return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())),
1257                                 SE.getUnknown(expand(Rest))));
1258   }
1259
1260   // If we don't yet have a canonical IV, create one.
1261   if (!CanonicalIV) {
1262     // Create and insert the PHI node for the induction variable in the
1263     // specified loop.
1264     BasicBlock *Header = L->getHeader();
1265     pred_iterator HPB = pred_begin(Header), HPE = pred_end(Header);
1266     CanonicalIV = PHINode::Create(Ty, std::distance(HPB, HPE), "indvar",
1267                                   Header->begin());
1268     rememberInstruction(CanonicalIV);
1269
1270     Constant *One = ConstantInt::get(Ty, 1);
1271     for (pred_iterator HPI = HPB; HPI != HPE; ++HPI) {
1272       BasicBlock *HP = *HPI;
1273       if (L->contains(HP)) {
1274         // Insert a unit add instruction right before the terminator
1275         // corresponding to the back-edge.
1276         Instruction *Add = BinaryOperator::CreateAdd(CanonicalIV, One,
1277                                                      "indvar.next",
1278                                                      HP->getTerminator());
1279         Add->setDebugLoc(HP->getTerminator()->getDebugLoc());
1280         rememberInstruction(Add);
1281         CanonicalIV->addIncoming(Add, HP);
1282       } else {
1283         CanonicalIV->addIncoming(Constant::getNullValue(Ty), HP);
1284       }
1285     }
1286   }
1287
1288   // {0,+,1} --> Insert a canonical induction variable into the loop!
1289   if (S->isAffine() && S->getOperand(1)->isOne()) {
1290     assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) &&
1291            "IVs with types different from the canonical IV should "
1292            "already have been handled!");
1293     return CanonicalIV;
1294   }
1295
1296   // {0,+,F} --> {0,+,1} * F
1297
1298   // If this is a simple linear addrec, emit it now as a special case.
1299   if (S->isAffine())    // {0,+,F} --> i*F
1300     return
1301       expand(SE.getTruncateOrNoop(
1302         SE.getMulExpr(SE.getUnknown(CanonicalIV),
1303                       SE.getNoopOrAnyExtend(S->getOperand(1),
1304                                             CanonicalIV->getType())),
1305         Ty));
1306
1307   // If this is a chain of recurrences, turn it into a closed form, using the
1308   // folders, then expandCodeFor the closed form.  This allows the folders to
1309   // simplify the expression without having to build a bunch of special code
1310   // into this folder.
1311   const SCEV *IH = SE.getUnknown(CanonicalIV);   // Get I as a "symbolic" SCEV.
1312
1313   // Promote S up to the canonical IV type, if the cast is foldable.
1314   const SCEV *NewS = S;
1315   const SCEV *Ext = SE.getNoopOrAnyExtend(S, CanonicalIV->getType());
1316   if (isa<SCEVAddRecExpr>(Ext))
1317     NewS = Ext;
1318
1319   const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE);
1320   //cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
1321
1322   // Truncate the result down to the original type, if needed.
1323   const SCEV *T = SE.getTruncateOrNoop(V, Ty);
1324   return expand(T);
1325 }
1326
1327 Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
1328   Type *Ty = SE.getEffectiveSCEVType(S->getType());
1329   Value *V = expandCodeFor(S->getOperand(),
1330                            SE.getEffectiveSCEVType(S->getOperand()->getType()));
1331   Value *I = Builder.CreateTrunc(V, Ty);
1332   rememberInstruction(I);
1333   return I;
1334 }
1335
1336 Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
1337   Type *Ty = SE.getEffectiveSCEVType(S->getType());
1338   Value *V = expandCodeFor(S->getOperand(),
1339                            SE.getEffectiveSCEVType(S->getOperand()->getType()));
1340   Value *I = Builder.CreateZExt(V, Ty);
1341   rememberInstruction(I);
1342   return I;
1343 }
1344
1345 Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
1346   Type *Ty = SE.getEffectiveSCEVType(S->getType());
1347   Value *V = expandCodeFor(S->getOperand(),
1348                            SE.getEffectiveSCEVType(S->getOperand()->getType()));
1349   Value *I = Builder.CreateSExt(V, Ty);
1350   rememberInstruction(I);
1351   return I;
1352 }
1353
1354 Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
1355   Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
1356   Type *Ty = LHS->getType();
1357   for (int i = S->getNumOperands()-2; i >= 0; --i) {
1358     // In the case of mixed integer and pointer types, do the
1359     // rest of the comparisons as integer.
1360     if (S->getOperand(i)->getType() != Ty) {
1361       Ty = SE.getEffectiveSCEVType(Ty);
1362       LHS = InsertNoopCastOfTo(LHS, Ty);
1363     }
1364     Value *RHS = expandCodeFor(S->getOperand(i), Ty);
1365     Value *ICmp = Builder.CreateICmpSGT(LHS, RHS);
1366     rememberInstruction(ICmp);
1367     Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax");
1368     rememberInstruction(Sel);
1369     LHS = Sel;
1370   }
1371   // In the case of mixed integer and pointer types, cast the
1372   // final result back to the pointer type.
1373   if (LHS->getType() != S->getType())
1374     LHS = InsertNoopCastOfTo(LHS, S->getType());
1375   return LHS;
1376 }
1377
1378 Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
1379   Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
1380   Type *Ty = LHS->getType();
1381   for (int i = S->getNumOperands()-2; i >= 0; --i) {
1382     // In the case of mixed integer and pointer types, do the
1383     // rest of the comparisons as integer.
1384     if (S->getOperand(i)->getType() != Ty) {
1385       Ty = SE.getEffectiveSCEVType(Ty);
1386       LHS = InsertNoopCastOfTo(LHS, Ty);
1387     }
1388     Value *RHS = expandCodeFor(S->getOperand(i), Ty);
1389     Value *ICmp = Builder.CreateICmpUGT(LHS, RHS);
1390     rememberInstruction(ICmp);
1391     Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax");
1392     rememberInstruction(Sel);
1393     LHS = Sel;
1394   }
1395   // In the case of mixed integer and pointer types, cast the
1396   // final result back to the pointer type.
1397   if (LHS->getType() != S->getType())
1398     LHS = InsertNoopCastOfTo(LHS, S->getType());
1399   return LHS;
1400 }
1401
1402 Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty,
1403                                    Instruction *I) {
1404   BasicBlock::iterator IP = I;
1405   while (isInsertedInstruction(IP) || isa<DbgInfoIntrinsic>(IP))
1406     ++IP;
1407   Builder.SetInsertPoint(IP->getParent(), IP);
1408   return expandCodeFor(SH, Ty);
1409 }
1410
1411 Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty) {
1412   // Expand the code for this SCEV.
1413   Value *V = expand(SH);
1414   if (Ty) {
1415     assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
1416            "non-trivial casts should be done with the SCEVs directly!");
1417     V = InsertNoopCastOfTo(V, Ty);
1418   }
1419   return V;
1420 }
1421
1422 Value *SCEVExpander::expand(const SCEV *S) {
1423   // Compute an insertion point for this SCEV object. Hoist the instructions
1424   // as far out in the loop nest as possible.
1425   Instruction *InsertPt = Builder.GetInsertPoint();
1426   for (Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock()); ;
1427        L = L->getParentLoop())
1428     if (SE.isLoopInvariant(S, L)) {
1429       if (!L) break;
1430       if (BasicBlock *Preheader = L->getLoopPreheader())
1431         InsertPt = Preheader->getTerminator();
1432     } else {
1433       // If the SCEV is computable at this level, insert it into the header
1434       // after the PHIs (and after any other instructions that we've inserted
1435       // there) so that it is guaranteed to dominate any user inside the loop.
1436       if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L))
1437         InsertPt = L->getHeader()->getFirstInsertionPt();
1438       while (isInsertedInstruction(InsertPt) || isa<DbgInfoIntrinsic>(InsertPt))
1439         InsertPt = llvm::next(BasicBlock::iterator(InsertPt));
1440       break;
1441     }
1442
1443   // Check to see if we already expanded this here.
1444   std::map<std::pair<const SCEV *, Instruction *>,
1445            AssertingVH<Value> >::iterator I =
1446     InsertedExpressions.find(std::make_pair(S, InsertPt));
1447   if (I != InsertedExpressions.end())
1448     return I->second;
1449
1450   BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
1451   BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
1452   Builder.SetInsertPoint(InsertPt->getParent(), InsertPt);
1453
1454   // Expand the expression into instructions.
1455   Value *V = visit(S);
1456
1457   // Remember the expanded value for this SCEV at this location.
1458   //
1459   // This is independent of PostIncLoops. The mapped value simply materializes
1460   // the expression at this insertion point. If the mapped value happened to be
1461   // a postinc expansion, it could be reused by a non postinc user, but only if
1462   // its insertion point was already at the head of the loop.
1463   InsertedExpressions[std::make_pair(S, InsertPt)] = V;
1464
1465   restoreInsertPoint(SaveInsertBB, SaveInsertPt);
1466   return V;
1467 }
1468
1469 void SCEVExpander::rememberInstruction(Value *I) {
1470   if (!PostIncLoops.empty())
1471     InsertedPostIncValues.insert(I);
1472   else
1473     InsertedValues.insert(I);
1474
1475   // If we just claimed an existing instruction and that instruction had
1476   // been the insert point, adjust the insert point forward so that
1477   // subsequently inserted code will be dominated.
1478   if (Builder.GetInsertPoint() == I) {
1479     BasicBlock::iterator It = cast<Instruction>(I);
1480     do { ++It; } while (isInsertedInstruction(It) ||
1481                         isa<DbgInfoIntrinsic>(It));
1482     Builder.SetInsertPoint(Builder.GetInsertBlock(), It);
1483   }
1484 }
1485
1486 void SCEVExpander::restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I) {
1487   // If we acquired more instructions since the old insert point was saved,
1488   // advance past them.
1489   while (isInsertedInstruction(I) || isa<DbgInfoIntrinsic>(I)) ++I;
1490
1491   Builder.SetInsertPoint(BB, I);
1492 }
1493
1494 /// getOrInsertCanonicalInductionVariable - This method returns the
1495 /// canonical induction variable of the specified type for the specified
1496 /// loop (inserting one if there is none).  A canonical induction variable
1497 /// starts at zero and steps by one on each iteration.
1498 PHINode *
1499 SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
1500                                                     Type *Ty) {
1501   assert(Ty->isIntegerTy() && "Can only insert integer induction variables!");
1502
1503   // Build a SCEV for {0,+,1}<L>.
1504   // Conservatively use FlagAnyWrap for now.
1505   const SCEV *H = SE.getAddRecExpr(SE.getConstant(Ty, 0),
1506                                    SE.getConstant(Ty, 1), L, SCEV::FlagAnyWrap);
1507
1508   // Emit code for it.
1509   BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
1510   BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
1511   PHINode *V = cast<PHINode>(expandCodeFor(H, 0, L->getHeader()->begin()));
1512   if (SaveInsertBB)
1513     restoreInsertPoint(SaveInsertBB, SaveInsertPt);
1514
1515   return V;
1516 }
1517
1518 /// hoistStep - Attempt to hoist an IV increment above a potential use.
1519 ///
1520 /// To successfully hoist, two criteria must be met:
1521 /// - IncV operands dominate InsertPos and
1522 /// - InsertPos dominates IncV
1523 ///
1524 /// Meeting the second condition means that we don't need to check all of IncV's
1525 /// existing uses (it's moving up in the domtree).
1526 ///
1527 /// This does not yet recursively hoist the operands, although that would
1528 /// not be difficult.
1529 ///
1530 /// This does not require a SCEVExpander instance and could be replaced by a
1531 /// general code-insertion helper.
1532 bool SCEVExpander::hoistStep(Instruction *IncV, Instruction *InsertPos,
1533                              const DominatorTree *DT) {
1534   if (DT->dominates(IncV, InsertPos))
1535     return true;
1536
1537   if (!DT->dominates(InsertPos->getParent(), IncV->getParent()))
1538     return false;
1539
1540   if (IncV->mayHaveSideEffects())
1541     return false;
1542
1543   // Attempt to hoist IncV
1544   for (User::op_iterator OI = IncV->op_begin(), OE = IncV->op_end();
1545        OI != OE; ++OI) {
1546     Instruction *OInst = dyn_cast<Instruction>(OI);
1547     if (OInst && !DT->dominates(OInst, InsertPos))
1548       return false;
1549   }
1550   IncV->moveBefore(InsertPos);
1551   return true;
1552 }
1553
1554 /// replaceCongruentIVs - Check for congruent phis in this loop header and
1555 /// replace them with their most canonical representative. Return the number of
1556 /// phis eliminated.
1557 ///
1558 /// This does not depend on any SCEVExpander state but should be used in
1559 /// the same context that SCEVExpander is used.
1560 unsigned SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
1561                                            SmallVectorImpl<WeakVH> &DeadInsts) {
1562   unsigned NumElim = 0;
1563   DenseMap<const SCEV *, PHINode *> ExprToIVMap;
1564   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
1565     PHINode *Phi = cast<PHINode>(I);
1566     if (!SE.isSCEVable(Phi->getType()))
1567       continue;
1568
1569     PHINode *&OrigPhiRef = ExprToIVMap[SE.getSCEV(Phi)];
1570     if (!OrigPhiRef) {
1571       OrigPhiRef = Phi;
1572       continue;
1573     }
1574
1575     // If one phi derives from the other via GEPs, types may differ.
1576     // We could consider adding a bitcast here to handle it.
1577     if (OrigPhiRef->getType() != Phi->getType())
1578       continue;
1579
1580     if (BasicBlock *LatchBlock = L->getLoopLatch()) {
1581       Instruction *OrigInc =
1582         cast<Instruction>(OrigPhiRef->getIncomingValueForBlock(LatchBlock));
1583       Instruction *IsomorphicInc =
1584         cast<Instruction>(Phi->getIncomingValueForBlock(LatchBlock));
1585
1586       // If this phi is more canonical, swap it with the original.
1587       if (!isExpandedAddRecExprPHI(OrigPhiRef, OrigInc, L)
1588           && isExpandedAddRecExprPHI(Phi, IsomorphicInc, L)) {
1589         std::swap(OrigPhiRef, Phi);
1590         std::swap(OrigInc, IsomorphicInc);
1591       }
1592       // Replacing the congruent phi is sufficient because acyclic redundancy
1593       // elimination, CSE/GVN, should handle the rest. However, once SCEV proves
1594       // that a phi is congruent, it's often the head of an IV user cycle that
1595       // is isomorphic with the original phi. So it's worth eagerly cleaning up
1596       // the common case of a single IV increment.
1597       if (OrigInc != IsomorphicInc &&
1598           OrigInc->getType() == IsomorphicInc->getType() &&
1599           SE.getSCEV(OrigInc) == SE.getSCEV(IsomorphicInc) &&
1600           hoistStep(OrigInc, IsomorphicInc, DT)) {
1601         DEBUG_WITH_TYPE(DebugType, dbgs()
1602                         << "INDVARS: Eliminated congruent iv.inc: "
1603                         << *IsomorphicInc << '\n');
1604         IsomorphicInc->replaceAllUsesWith(OrigInc);
1605         DeadInsts.push_back(IsomorphicInc);
1606       }
1607     }
1608     DEBUG_WITH_TYPE(DebugType, dbgs()
1609                     << "INDVARS: Eliminated congruent iv: " << *Phi << '\n');
1610     ++NumElim;
1611     Phi->replaceAllUsesWith(OrigPhiRef);
1612     DeadInsts.push_back(Phi);
1613   }
1614   return NumElim;
1615 }