ScalarEvolutionExpander.cpp revision a7235ea7245028a0723e8ab7fd011386b3900777
136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===//
236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//                     The LLVM Compiler Infrastructure
436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//===----------------------------------------------------------------------===//
936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
1036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman// This file contains the implementation of the scalar evolution expander,
1136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman// which is used to generate the code corresponding to a given scalar evolution
1236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman// expression.
1336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//
1436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman//===----------------------------------------------------------------------===//
1536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
1636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#include "llvm/Analysis/ScalarEvolutionExpander.h"
17e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling#include "llvm/Analysis/LoopInfo.h"
1876f600b205606a055ec35e7d3fd1a99602329d67Owen Anderson#include "llvm/LLVMContext.h"
195be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman#include "llvm/Target/TargetData.h"
204d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman#include "llvm/ADT/STLExtras.h"
2136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begemanusing namespace llvm;
2236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
23267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman/// InsertNoopCastOfTo - Insert a cast of V to the specified type,
24267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman/// which must be possible with a noop cast, doing what we can to share
25267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman/// the casts.
26267a385342f2e7388f178b327dd87c5f29afd51bDan GohmanValue *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) {
27267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false);
28267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  assert((Op == Instruction::BitCast ||
29267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman          Op == Instruction::PtrToInt ||
30267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman          Op == Instruction::IntToPtr) &&
31267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman         "InsertNoopCastOfTo cannot perform non-noop casts!");
32267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
33267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman         "InsertNoopCastOfTo cannot change sizes!");
34267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman
352d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  // Short-circuit unnecessary bitcasts.
36267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  if (Op == Instruction::BitCast && V->getType() == Ty)
372d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman    return V;
382d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman
39f04fa483b83227c570bc58e1684ea096430a6697Dan Gohman  // Short-circuit unnecessary inttoptr<->ptrtoint casts.
40267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) &&
4180dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman      SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) {
42af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    if (CastInst *CI = dyn_cast<CastInst>(V))
43af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman      if ((CI->getOpcode() == Instruction::PtrToInt ||
44af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman           CI->getOpcode() == Instruction::IntToPtr) &&
45af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman          SE.getTypeSizeInBits(CI->getType()) ==
46af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman          SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
47af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman        return CI->getOperand(0);
4880dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
4980dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman      if ((CE->getOpcode() == Instruction::PtrToInt ||
5080dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman           CE->getOpcode() == Instruction::IntToPtr) &&
5180dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman          SE.getTypeSizeInBits(CE->getType()) ==
5280dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman          SE.getTypeSizeInBits(CE->getOperand(0)->getType()))
5380dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman        return CE->getOperand(0);
5480dcdee0f48820ecea86c15768324945bb0d68d1Dan Gohman  }
55f04fa483b83227c570bc58e1684ea096430a6697Dan Gohman
56ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  // FIXME: keep track of the cast instruction.
57ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  if (Constant *C = dyn_cast<Constant>(V))
58baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson    return ConstantExpr::getCast(Op, C, Ty);
59ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner
60ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  if (Argument *A = dyn_cast<Argument>(V)) {
61ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner    // Check to see if there is already a cast!
62ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner    for (Value::use_iterator UI = A->use_begin(), E = A->use_end();
6340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman         UI != E; ++UI)
64ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner      if ((*UI)->getType() == Ty)
653913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz        if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
66267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman          if (CI->getOpcode() == Op) {
673913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz            // If the cast isn't the first instruction of the function, move it.
6840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman            if (BasicBlock::iterator(CI) !=
693913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz                A->getParent()->getEntryBlock().begin()) {
7040a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman              // Recreate the cast at the beginning of the entry block.
7140a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman              // The old cast is left in place in case it is being used
7240a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman              // as an insert point.
7340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman              Instruction *NewCI =
74267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman                CastInst::Create(Op, V, Ty, "",
7540a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman                                 A->getParent()->getEntryBlock().begin());
7640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman              NewCI->takeName(CI);
7740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman              CI->replaceAllUsesWith(NewCI);
7840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman              return NewCI;
793913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz            }
803913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz            return CI;
81ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner          }
8240a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman
83267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Instruction *I = CastInst::Create(Op, V, Ty, V->getName(),
84cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman                                      A->getParent()->getEntryBlock().begin());
85cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    InsertedValues.insert(I);
86cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    return I;
87ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  }
883913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz
89ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  Instruction *I = cast<Instruction>(V);
903913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz
91ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  // Check to see if there is already a cast.  If there is, use it.
92ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
93ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner       UI != E; ++UI) {
94ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner    if ((*UI)->getType() == Ty)
953913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz      if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
96267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman        if (CI->getOpcode() == Op) {
973913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz          BasicBlock::iterator It = I; ++It;
983913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz          if (isa<InvokeInst>(I))
993913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz            It = cast<InvokeInst>(I)->getNormalDest()->begin();
1003913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz          while (isa<PHINode>(It)) ++It;
1013913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz          if (It != BasicBlock::iterator(CI)) {
10240a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman            // Recreate the cast at the beginning of the entry block.
10340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman            // The old cast is left in place in case it is being used
10440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman            // as an insert point.
105267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman            Instruction *NewCI = CastInst::Create(Op, V, Ty, "", It);
10640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman            NewCI->takeName(CI);
10740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman            CI->replaceAllUsesWith(NewCI);
10840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman            return NewCI;
1093913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz          }
1103913187bf6135e6a71260c65eed664095c8f9ce9Wojciech Matyjewicz          return CI;
111ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner        }
112ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  }
113ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  BasicBlock::iterator IP = I; ++IP;
114ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  if (InvokeInst *II = dyn_cast<InvokeInst>(I))
115ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner    IP = II->getNormalDest()->begin();
116ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner  while (isa<PHINode>(IP)) ++IP;
117267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Instruction *CI = CastInst::Create(Op, V, Ty, V->getName(), IP);
118cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  InsertedValues.insert(CI);
119cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  return CI;
120ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner}
121ca1a4bebb3928ba18fb8751e2f0c0f88fad54cfdChris Lattner
1227fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner/// InsertBinop - Insert the specified binary operator, doing a small amount
1237fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner/// of work to avoid inserting an obviously redundant operation.
124267a385342f2e7388f178b327dd87c5f29afd51bDan GohmanValue *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
125267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman                                 Value *LHS, Value *RHS) {
1260f0eb18addc94bf3d8179fbce162c8c4622b9866Dan Gohman  // Fold a binop with constant operands.
1270f0eb18addc94bf3d8179fbce162c8c4622b9866Dan Gohman  if (Constant *CLHS = dyn_cast<Constant>(LHS))
1280f0eb18addc94bf3d8179fbce162c8c4622b9866Dan Gohman    if (Constant *CRHS = dyn_cast<Constant>(RHS))
129baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson      return ConstantExpr::get(Opcode, CLHS, CRHS);
1300f0eb18addc94bf3d8179fbce162c8c4622b9866Dan Gohman
1317fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner  // Do a quick scan to see if we have this binop nearby.  If so, reuse it.
1327fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner  unsigned ScanLimit = 6;
133267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
134267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  // Scanning starts from the last instruction before the insertion point.
135267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  BasicBlock::iterator IP = Builder.GetInsertPoint();
136267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  if (IP != BlockBegin) {
1378a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz    --IP;
1388a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz    for (; ScanLimit; --IP, --ScanLimit) {
1395be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS &&
1405be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman          IP->getOperand(1) == RHS)
1415be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman        return IP;
1428a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz      if (IP == BlockBegin) break;
1438a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz    }
1447fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner  }
145267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman
1468a08769bad43a22fae2845bb0ba0fd1266cd55c8Wojciech Matyjewicz  // If we haven't found this binop, insert it.
147267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Value *BO = Builder.CreateBinOp(Opcode, LHS, RHS, "tmp");
148cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  InsertedValues.insert(BO);
149cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  return BO;
1507fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner}
1517fec90ebf4ebe7aa73a6dd7d275c255587c041adChris Lattner
1524a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman/// FactorOutConstant - Test if S is divisible by Factor, using signed
153453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// division. If so, update S with Factor divided out and return true.
1544a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman/// S need not be evenly divisble if a reasonable remainder can be
1554a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman/// computed.
156453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made
157453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// unnecessary; in its place, just signed-divide Ops[i] by the scale and
158453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// check to see if the divide was folded.
1590bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohmanstatic bool FactorOutConstant(const SCEV *&S,
1600bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman                              const SCEV *&Remainder,
161453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                              const APInt &Factor,
162453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                              ScalarEvolution &SE) {
163453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  // Everything is divisible by one.
164453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  if (Factor == 1)
165453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    return true;
166453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman
167453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  // For a Constant, check for a multiple of the given factor.
1684a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman  if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
1694a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman    ConstantInt *CI =
170eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson      ConstantInt::get(SE.getContext(), C->getValue()->getValue().sdiv(Factor));
1714a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman    // If the quotient is zero and the remainder is non-zero, reject
1724a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman    // the value at this scale. It will be considered for subsequent
1734a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman    // smaller scales.
1744a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman    if (C->isZero() || !CI->isZero()) {
1750bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      const SCEV *Div = SE.getConstant(CI);
176453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      S = Div;
1774a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman      Remainder =
1784a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman        SE.getAddExpr(Remainder,
1794a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman                      SE.getConstant(C->getValue()->getValue().srem(Factor)));
180453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      return true;
181453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    }
1824a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman  }
183453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman
184453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  // In a Mul, check if there is a constant operand which is a multiple
185453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  // of the given factor.
186453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S))
187453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0)))
188453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      if (!C->getValue()->getValue().srem(Factor)) {
1895001c21712f2e52fd6254461b2b2edb9a5823d23Dan Gohman        const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands();
1905001c21712f2e52fd6254461b2b2edb9a5823d23Dan Gohman        SmallVector<const SCEV *, 4> NewMulOps(MOperands.begin(),
1915001c21712f2e52fd6254461b2b2edb9a5823d23Dan Gohman                                               MOperands.end());
192453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman        NewMulOps[0] =
193453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman          SE.getConstant(C->getValue()->getValue().sdiv(Factor));
194453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman        S = SE.getMulExpr(NewMulOps);
195453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman        return true;
196453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      }
197453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman
198453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  // In an AddRec, check if both start and step are divisible.
199453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
2000bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *Step = A->getStepRecurrence(SE);
2010bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *StepRem = SE.getIntegerSCEV(0, Step->getType());
2024a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman    if (!FactorOutConstant(Step, StepRem, Factor, SE))
2034a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman      return false;
2044a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman    if (!StepRem->isZero())
2054a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman      return false;
2060bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *Start = A->getStart();
2074a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman    if (!FactorOutConstant(Start, Remainder, Factor, SE))
208453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      return false;
209453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    S = SE.getAddRecExpr(Start, Step, A->getLoop());
210453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    return true;
211453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  }
212453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman
213453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  return false;
214453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman}
215453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman
2165be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman/// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
217453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// instead of using ptrtoint+arithmetic+inttoptr. This helps
21813c5e35222afe0895f0c5e68aa9f22f134ea437aDan Gohman/// BasicAliasAnalysis analyze the result.
21913c5e35222afe0895f0c5e68aa9f22f134ea437aDan Gohman///
22013c5e35222afe0895f0c5e68aa9f22f134ea437aDan Gohman/// Design note: This depends on ScalarEvolution not recognizing inttoptr
22113c5e35222afe0895f0c5e68aa9f22f134ea437aDan Gohman/// and ptrtoint operators, as they may introduce pointer arithmetic
22213c5e35222afe0895f0c5e68aa9f22f134ea437aDan Gohman/// which may not be safely converted into getelementptr.
223453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman///
224453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// Design note: It might seem desirable for this function to be more
225453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// loop-aware. If some of the indices are loop-invariant while others
226453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// aren't, it might seem desirable to emit multiple GEPs, keeping the
227453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// loop-invariant portions of the overall computation outside the loop.
228453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// However, there are a few reasons this is not done here. Hoisting simple
229453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// arithmetic is a low-level optimization that often isn't very
230453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// important until late in the optimization process. In fact, passes
231453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// like InstructionCombining will combine GEPs, even if it means
232453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// pushing loop-invariant computation down into loops, so even if the
233453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// GEPs were split here, the work would quickly be undone. The
234453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// LoopStrengthReduction pass, which is usually run quite late (and
235453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// after the last InstructionCombining pass), takes care of hoisting
236453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// loop-invariant portions of expressions, after considering what
237453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// can be folded using target addressing modes.
238453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman///
2390bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan GohmanValue *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin,
2400bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman                                    const SCEV *const *op_end,
2415be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman                                    const PointerType *PTy,
2425be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman                                    const Type *Ty,
2435be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman                                    Value *V) {
2445be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  const Type *ElTy = PTy->getElementType();
2455be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  SmallVector<Value *, 4> GepIndices;
2460bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  SmallVector<const SCEV *, 8> Ops(op_begin, op_end);
2475be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  bool AnyNonZeroIndices = false;
2485be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
2495be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  // Decend down the pointer's type and attempt to convert the other
2505be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  // operands into GEP indices, at each level. The first index in a GEP
2515be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  // indexes into the array implied by the pointer operand; the rest of
2525be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  // the indices index into the element or field type selected by the
2535be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  // preceding index.
2545be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  for (;;) {
2555be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    APInt ElSize = APInt(SE.getTypeSizeInBits(Ty),
2565be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman                         ElTy->isSized() ?  SE.TD->getTypeAllocSize(ElTy) : 0);
2570bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    SmallVector<const SCEV *, 8> NewOps;
2580bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    SmallVector<const SCEV *, 8> ScaledOps;
2595be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
260453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      // Split AddRecs up into parts as either of the parts may be usable
261453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      // without the other.
262453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i]))
263453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman        if (!A->getStart()->isZero()) {
2640bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman          const SCEV *Start = A->getStart();
265453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman          Ops.push_back(SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()),
266453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                                         A->getStepRecurrence(SE),
267453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                                         A->getLoop()));
268453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman          Ops[i] = Start;
269453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman          ++e;
270453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman        }
271453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      // If the scale size is not 0, attempt to factor out a scale.
2725be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      if (ElSize != 0) {
2730bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman        const SCEV *Op = Ops[i];
2740bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman        const SCEV *Remainder = SE.getIntegerSCEV(0, Op->getType());
2754a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman        if (FactorOutConstant(Op, Remainder, ElSize, SE)) {
276453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman          ScaledOps.push_back(Op); // Op now has ElSize factored out.
2774a4f767235dac50965fc266fb822d9a2e37d5212Dan Gohman          NewOps.push_back(Remainder);
2785be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman          continue;
2795be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman        }
2805be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      }
281453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      // If the operand was not divisible, add it to the list of operands
282453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      // we'll scan next iteration.
2835be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      NewOps.push_back(Ops[i]);
2845be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    }
2855be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    Ops = NewOps;
2865be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    AnyNonZeroIndices |= !ScaledOps.empty();
2875be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    Value *Scaled = ScaledOps.empty() ?
288a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson                    Constant::getNullValue(Ty) :
2895be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman                    expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
2905be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    GepIndices.push_back(Scaled);
2915be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
2925be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    // Collect struct field index operands.
2935be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    if (!Ops.empty())
2945be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      while (const StructType *STy = dyn_cast<StructType>(ElTy)) {
2955be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman        if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0]))
2965be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman          if (SE.getTypeSizeInBits(C->getType()) <= 64) {
2975be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman            const StructLayout &SL = *SE.TD->getStructLayout(STy);
2985be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman            uint64_t FullOffset = C->getValue()->getZExtValue();
2995be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman            if (FullOffset < SL.getSizeInBytes()) {
3005be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman              unsigned ElIdx = SL.getElementContainingOffset(FullOffset);
301eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson              GepIndices.push_back(ConstantInt::get(Type::Int32Ty, ElIdx));
3025be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman              ElTy = STy->getTypeAtIndex(ElIdx);
3035be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman              Ops[0] =
3046de29f8d960505421d61c80cdb738e16720b6c0eDan Gohman                SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
3055be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman              AnyNonZeroIndices = true;
3065be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman              continue;
3075be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman            }
3085be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman          }
3095be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman        break;
3105be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      }
3115be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
3125be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    if (const ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) {
3135be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      ElTy = ATy->getElementType();
3145be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      continue;
3155be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    }
3165be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    break;
3175be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  }
3185be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
3195be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  // If none of the operands were convertable to proper GEP indices, cast
3205be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  // the base to i8* and do an ugly getelementptr with that. It's still
3215be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  // better than ptrtoint+arithmetic+inttoptr at least.
3225be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  if (!AnyNonZeroIndices) {
3235be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    V = InsertNoopCastOfTo(V,
3245be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman                           Type::Int8Ty->getPointerTo(PTy->getAddressSpace()));
32592fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman    Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty);
3265be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
3275be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    // Fold a GEP with constant operands.
3285be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    if (Constant *CLHS = dyn_cast<Constant>(V))
3295be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      if (Constant *CRHS = dyn_cast<Constant>(Idx))
330baf3c404409d5e47b13984a7f95bfbd6d1f2e79eOwen Anderson        return ConstantExpr::getGetElementPtr(CLHS, &CRHS, 1);
3315be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
3325be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    // Do a quick scan to see if we have this GEP nearby.  If so, reuse it.
3335be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    unsigned ScanLimit = 6;
334267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
335267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    // Scanning starts from the last instruction before the insertion point.
336267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    BasicBlock::iterator IP = Builder.GetInsertPoint();
337267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    if (IP != BlockBegin) {
3385be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      --IP;
3395be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      for (; ScanLimit; --IP, --ScanLimit) {
3405be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman        if (IP->getOpcode() == Instruction::GetElementPtr &&
3415be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman            IP->getOperand(0) == V && IP->getOperand(1) == Idx)
3425be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman          return IP;
3435be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman        if (IP == BlockBegin) break;
3445be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman      }
3455be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    }
3465be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
347267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Value *GEP = Builder.CreateGEP(V, Idx, "scevgep");
3485be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    InsertedValues.insert(GEP);
3495be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    return GEP;
3505be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  }
3515be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
352d6aa02de1076c801ac41295156a2379637976918Dan Gohman  // Insert a pretty getelementptr. Note that this GEP is not marked inbounds,
353d6aa02de1076c801ac41295156a2379637976918Dan Gohman  // because ScalarEvolution may have changed the address arithmetic to
354d6aa02de1076c801ac41295156a2379637976918Dan Gohman  // compute a value which is beyond the end of the allocated object.
355267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Value *GEP = Builder.CreateGEP(V,
356267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman                                 GepIndices.begin(),
357267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman                                 GepIndices.end(),
358267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman                                 "scevgep");
3595be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  Ops.push_back(SE.getUnknown(GEP));
3605be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  InsertedValues.insert(GEP);
3615be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  return expand(SE.getAddExpr(Ops));
3625be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman}
3635be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
364890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
365af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
366e24fa64d52330626553298f56ba5aa702624c282Dan Gohman  Value *V = expand(S->getOperand(S->getNumOperands()-1));
3675be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
368453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
369453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  // comments on expandAddToGEP for details.
3705be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  if (SE.TD)
371453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) {
3720bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      const SmallVectorImpl<const SCEV *> &Ops = S->getOperands();
3735001c21712f2e52fd6254461b2b2edb9a5823d23Dan Gohman      return expandAddToGEP(&Ops[0], &Ops[Ops.size() - 1], PTy, Ty, V);
374453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    }
3755be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
376af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman  V = InsertNoopCastOfTo(V, Ty);
377e24fa64d52330626553298f56ba5aa702624c282Dan Gohman
378e24fa64d52330626553298f56ba5aa702624c282Dan Gohman  // Emit a bunch of add instructions
3792d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  for (int i = S->getNumOperands()-2; i >= 0; --i) {
38092fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman    Value *W = expandCodeFor(S->getOperand(i), Ty);
381267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    V = InsertBinop(Instruction::Add, V, W);
3822d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  }
383e24fa64d52330626553298f56ba5aa702624c282Dan Gohman  return V;
384e24fa64d52330626553298f56ba5aa702624c282Dan Gohman}
3855be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman
386890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
387af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
38836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  int FirstOp = 0;  // Set if we should emit a subtract.
389890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman  if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
39036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    if (SC->getValue()->isAllOnesValue())
39136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman      FirstOp = 1;
39236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
39336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  int i = S->getNumOperands()-2;
39492fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman  Value *V = expandCodeFor(S->getOperand(i+1), Ty);
39536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
39636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // Emit a bunch of multiply instructions
3972d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  for (; i >= FirstOp; --i) {
39892fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman    Value *W = expandCodeFor(S->getOperand(i), Ty);
399267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    V = InsertBinop(Instruction::Mul, V, W);
4002d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  }
4012d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman
40236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // -1 * ...  --->  0 - ...
40336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  if (FirstOp == 1)
404a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson    V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V);
40536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  return V;
40636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman}
40736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
408890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
409af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
4102d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman
41192fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman  Value *LHS = expandCodeFor(S->getLHS(), Ty);
412890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohman  if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
4136177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky    const APInt &RHS = SC->getValue()->getValue();
4146177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky    if (RHS.isPowerOf2())
4156177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky      return InsertBinop(Instruction::LShr, LHS,
416eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson                         ConstantInt::get(Ty, RHS.logBase2()));
4176177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky  }
4186177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky
41992fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman  Value *RHS = expandCodeFor(S->getRHS(), Ty);
420267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  return InsertBinop(Instruction::UDiv, LHS, RHS);
4216177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky}
4226177fd4fcee4d82692c47e33754ffe285c38cc69Nick Lewycky
423453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// Move parts of Base into Rest to leave Base with the minimal
424453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// expression that provides a pointer operand suitable for a
425453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman/// GEP expansion.
4260bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohmanstatic void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest,
427453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                              ScalarEvolution &SE) {
428453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) {
429453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    Base = A->getStart();
430453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    Rest = SE.getAddExpr(Rest,
431453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                         SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()),
432453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                                          A->getStepRecurrence(SE),
433453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman                                          A->getLoop()));
434453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  }
435453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) {
436453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    Base = A->getOperand(A->getNumOperands()-1);
4370bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end());
438453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    NewAddOps.back() = Rest;
439453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    Rest = SE.getAddExpr(NewAddOps);
440453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    ExposePointerBase(Base, Rest, SE);
441453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman  }
442453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman}
443453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman
444890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
445af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
44636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  const Loop *L = S->getLoop();
44736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
4484d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  // First check for an existing canonical IV in a suitable type.
4494d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  PHINode *CanonicalIV = 0;
4504d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  if (PHINode *PN = L->getCanonicalInductionVariable())
4514d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    if (SE.isSCEVable(PN->getType()) &&
4524d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman        isa<IntegerType>(SE.getEffectiveSCEVType(PN->getType())) &&
4534d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman        SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty))
4544d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      CanonicalIV = PN;
4554d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman
4564d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  // Rewrite an AddRec in terms of the canonical induction variable, if
4574d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  // its type is more narrow.
4584d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  if (CanonicalIV &&
4594d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      SE.getTypeSizeInBits(CanonicalIV->getType()) >
4604d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      SE.getTypeSizeInBits(Ty)) {
4615001c21712f2e52fd6254461b2b2edb9a5823d23Dan Gohman    const SCEV *Start = SE.getAnyExtendExpr(S->getStart(),
4625001c21712f2e52fd6254461b2b2edb9a5823d23Dan Gohman                                            CanonicalIV->getType());
4635001c21712f2e52fd6254461b2b2edb9a5823d23Dan Gohman    const SCEV *Step = SE.getAnyExtendExpr(S->getStepRecurrence(SE),
4644d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman                                           CanonicalIV->getType());
4654d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    Value *V = expand(SE.getAddRecExpr(Start, Step, S->getLoop()));
466267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
467267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
4684d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    BasicBlock::iterator NewInsertPt =
4694d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      next(BasicBlock::iterator(cast<Instruction>(V)));
4704d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    while (isa<PHINode>(NewInsertPt)) ++NewInsertPt;
4714d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0,
4724d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman                      NewInsertPt);
473267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
4744d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    return V;
4754d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  }
4764d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman
47736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // {X,+,F} --> X + {0,+,F}
478cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman  if (!S->getStart()->isZero()) {
4790bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SmallVectorImpl<const SCEV *> &SOperands = S->getOperands();
4800bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    SmallVector<const SCEV *, 4> NewOps(SOperands.begin(), SOperands.end());
481246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    NewOps[0] = SE.getIntegerSCEV(0, Ty);
4820bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *Rest = SE.getAddRecExpr(NewOps, L);
483453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman
484453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
485453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    // comments on expandAddToGEP for details.
486453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    if (SE.TD) {
4870bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      const SCEV *Base = S->getStart();
4880bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      const SCEV *RestArray[1] = { Rest };
489453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      // Dig into the expression to find the pointer base for a GEP.
490453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      ExposePointerBase(Base, RestArray[0], SE);
491453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      // If we found a pointer, expand the AddRec with a GEP.
492453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      if (const PointerType *PTy = dyn_cast<PointerType>(Base->getType())) {
493f876ad0a70c5c3bb402de2766237410f041700e6Dan Gohman        // Make sure the Base isn't something exotic, such as a multiplied
494f876ad0a70c5c3bb402de2766237410f041700e6Dan Gohman        // or divided pointer value. In those cases, the result type isn't
495f876ad0a70c5c3bb402de2766237410f041700e6Dan Gohman        // actually a pointer type.
496f876ad0a70c5c3bb402de2766237410f041700e6Dan Gohman        if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) {
497f876ad0a70c5c3bb402de2766237410f041700e6Dan Gohman          Value *StartV = expand(Base);
498f876ad0a70c5c3bb402de2766237410f041700e6Dan Gohman          assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!");
499f876ad0a70c5c3bb402de2766237410f041700e6Dan Gohman          return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV);
500f876ad0a70c5c3bb402de2766237410f041700e6Dan Gohman        }
501453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman      }
502453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman    }
503453aa4fbf1083cc7f646a0ac21e2bcc384a91ae9Dan Gohman
50440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman    // Just do a normal add. Pre-expand the operands to suppress folding.
50540a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman    return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())),
50640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman                                SE.getUnknown(expand(Rest))));
50736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  }
50836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
50936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // {0,+,1} --> Insert a canonical induction variable into the loop!
51017f1972c770dc18f5c7c3c95776b4d62ae9e121dDan Gohman  if (S->isAffine() &&
511246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) {
5124d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // If there's a canonical IV, just use it.
5134d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    if (CanonicalIV) {
5144d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) &&
5154d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman             "IVs with types different from the canonical IV should "
5164d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman             "already have been handled!");
5174d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      return CanonicalIV;
5184d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    }
5194d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman
52036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    // Create and insert the PHI node for the induction variable in the
52136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    // specified loop.
52236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    BasicBlock *Header = L->getHeader();
523267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    BasicBlock *Preheader = L->getLoopPreheader();
524051a950000e21935165db56695e35bade668193bGabor Greif    PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
525cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    InsertedValues.insert(PN);
526a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson    PN->addIncoming(Constant::getNullValue(Ty), Preheader);
52736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
52836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    pred_iterator HPI = pred_begin(Header);
52936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    assert(HPI != pred_end(Header) && "Loop with zero preds???");
53036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    if (!L->contains(*HPI)) ++HPI;
53136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    assert(HPI != pred_end(Header) && L->contains(*HPI) &&
53236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman           "No backedge in loop?");
53336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
53436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    // Insert a unit add instruction right before the terminator corresponding
53536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    // to the back-edge.
536eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson    Constant *One = ConstantInt::get(Ty, 1);
5377cbd8a3e92221437048b484d5ef9c0a22d0f8c58Gabor Greif    Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
53836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman                                                 (*HPI)->getTerminator());
539cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    InsertedValues.insert(Add);
54036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
54136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    pred_iterator PI = pred_begin(Header);
542267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    if (*PI == Preheader)
54336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman      ++PI;
54436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    PN->addIncoming(Add, *PI);
54536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman    return PN;
54636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  }
54736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
5484d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  // {0,+,F} --> {0,+,1} * F
54936f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // Get the canonical induction variable I for this loop.
5504d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  Value *I = CanonicalIV ?
5514d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman             CanonicalIV :
5524d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman             getOrInsertCanonicalInductionVariable(L, Ty);
55336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
554df14a04b5ccbbe6a46c2ccb93e27b12a36ff163eChris Lattner  // If this is a simple linear addrec, emit it now as a special case.
55540a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman  if (S->isAffine())    // {0,+,F} --> i*F
55640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman    return
55740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      expand(SE.getTruncateOrNoop(
55840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman        SE.getMulExpr(SE.getUnknown(I),
55940a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman                      SE.getNoopOrAnyExtend(S->getOperand(1),
56040a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman                                            I->getType())),
56140a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman        Ty));
56236f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
56336f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // If this is a chain of recurrences, turn it into a closed form, using the
56436f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // folders, then expandCodeFor the closed form.  This allows the folders to
56536f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // simplify the expression without having to build a bunch of special code
56636f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman  // into this folder.
5670bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  const SCEV *IH = SE.getUnknown(I);   // Get I as a "symbolic" SCEV.
56836f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
5694d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  // Promote S up to the canonical IV type, if the cast is foldable.
5700bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  const SCEV *NewS = S;
5710bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  const SCEV *Ext = SE.getNoopOrAnyExtend(S, I->getType());
5724d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  if (isa<SCEVAddRecExpr>(Ext))
5734d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    NewS = Ext;
5744d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman
5750bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE);
576e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling  //cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
57736f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman
5784d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman  // Truncate the result down to the original type, if needed.
5790bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  const SCEV *T = SE.getTruncateOrNoop(V, Ty);
580469f3cdc13851914f3a766cbd8f701cf8431cacaDan Gohman  return expand(T);
58136f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman}
58296fea337d27357e9b62abbf3d2d5ce29f1c8e870Anton Korobeynikov
583890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
584af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
58592fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman  Value *V = expandCodeFor(S->getOperand(),
58692fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman                           SE.getEffectiveSCEVType(S->getOperand()->getType()));
587267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Value *I = Builder.CreateTrunc(V, Ty, "tmp");
588cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  InsertedValues.insert(I);
589cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  return I;
59011f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman}
59111f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman
592890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
593af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
59492fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman  Value *V = expandCodeFor(S->getOperand(),
59592fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman                           SE.getEffectiveSCEVType(S->getOperand()->getType()));
596267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Value *I = Builder.CreateZExt(V, Ty, "tmp");
597cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  InsertedValues.insert(I);
598cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  return I;
59911f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman}
60011f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman
601890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
602af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
60392fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman  Value *V = expandCodeFor(S->getOperand(),
60492fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman                           SE.getEffectiveSCEVType(S->getOperand()->getType()));
605267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Value *I = Builder.CreateSExt(V, Ty, "tmp");
606cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  InsertedValues.insert(I);
607cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman  return I;
60811f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman}
60911f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman
610890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
6110196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
6120196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  const Type *Ty = LHS->getType();
6130196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  for (int i = S->getNumOperands()-2; i >= 0; --i) {
6140196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    // In the case of mixed integer and pointer types, do the
6150196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    // rest of the comparisons as integer.
6160196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    if (S->getOperand(i)->getType() != Ty) {
6170196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman      Ty = SE.getEffectiveSCEVType(Ty);
6180196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman      LHS = InsertNoopCastOfTo(LHS, Ty);
6190196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    }
62092fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman    Value *RHS = expandCodeFor(S->getOperand(i), Ty);
621267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Value *ICmp = Builder.CreateICmpSGT(LHS, RHS, "tmp");
622cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    InsertedValues.insert(ICmp);
623267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax");
624cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    InsertedValues.insert(Sel);
625cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    LHS = Sel;
626c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky  }
6270196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  // In the case of mixed integer and pointer types, cast the
6280196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  // final result back to the pointer type.
6290196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  if (LHS->getType() != S->getType())
6300196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    LHS = InsertNoopCastOfTo(LHS, S->getType());
631c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky  return LHS;
632c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky}
633c54c561c9f7270c055dd7ba75a3a003b771a42d9Nick Lewycky
634890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
6350196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
6360196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  const Type *Ty = LHS->getType();
6370196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  for (int i = S->getNumOperands()-2; i >= 0; --i) {
6380196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    // In the case of mixed integer and pointer types, do the
6390196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    // rest of the comparisons as integer.
6400196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    if (S->getOperand(i)->getType() != Ty) {
6410196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman      Ty = SE.getEffectiveSCEVType(Ty);
6420196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman      LHS = InsertNoopCastOfTo(LHS, Ty);
6430196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    }
64492fcdcac543653a62949fe9e5a7bd008500c1380Dan Gohman    Value *RHS = expandCodeFor(S->getOperand(i), Ty);
645267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Value *ICmp = Builder.CreateICmpUGT(LHS, RHS, "tmp");
646cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    InsertedValues.insert(ICmp);
647267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax");
648cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    InsertedValues.insert(Sel);
649cf5ab820227dedd77fb91d0904b6dc3694a7c196Dan Gohman    LHS = Sel;
6503e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky  }
6510196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  // In the case of mixed integer and pointer types, cast the
6520196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  // final result back to the pointer type.
6530196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman  if (LHS->getType() != S->getType())
6540196dc5733bf3c6a71f7de1f631f2c43e5809fd9Dan Gohman    LHS = InsertNoopCastOfTo(LHS, S->getType());
6553e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky  return LHS;
6563e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky}
6573e6307698084e7adfc10b739442ae29742beefd0Nick Lewycky
6580bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan GohmanValue *SCEVExpander::expandCodeFor(const SCEV *SH, const Type *Ty) {
65911f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman  // Expand the code for this SCEV.
6602d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  Value *V = expand(SH);
6615be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  if (Ty) {
6625be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
6635be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman           "non-trivial casts should be done with the SCEVs directly!");
6645be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman    V = InsertNoopCastOfTo(V, Ty);
6655be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  }
6665be18e84766fb495b0bde3c8244c1df459a18683Dan Gohman  return V;
66711f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman}
66811f6d3b478c4fa09d126833c57fbac1d795ead31Dan Gohman
669890f92b744fb074465bc2b7006ee753a181f62a4Dan GohmanValue *SCEVExpander::expand(const SCEV *S) {
67040a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman  // Compute an insertion point for this SCEV object. Hoist the instructions
67140a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman  // as far out in the loop nest as possible.
672267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Instruction *InsertPt = Builder.GetInsertPoint();
673267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  for (Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock()); ;
67440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman       L = L->getParentLoop())
67540a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman    if (S->isLoopInvariant(L)) {
67640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      if (!L) break;
67740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      if (BasicBlock *Preheader = L->getLoopPreheader())
67840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman        InsertPt = Preheader->getTerminator();
67940a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman    } else {
68040a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // If the SCEV is computable at this level, insert it into the header
68140a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // after the PHIs (and after any other instructions that we've inserted
68240a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // there) so that it is guaranteed to dominate any user inside the loop.
68340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      if (L && S->hasComputableLoopEvolution(L))
68440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman        InsertPt = L->getHeader()->getFirstNonPHI();
685267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman      while (isInsertedInstruction(InsertPt))
686267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman        InsertPt = next(BasicBlock::iterator(InsertPt));
68740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      break;
68840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman    }
68940a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman
690667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  // Check to see if we already expanded this here.
691667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  std::map<std::pair<const SCEV *, Instruction *>,
692667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman           AssertingVH<Value> >::iterator I =
693667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    InsertedExpressions.find(std::make_pair(S, InsertPt));
694267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  if (I != InsertedExpressions.end())
695667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    return I->second;
696267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman
697267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
698267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
699267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Builder.SetInsertPoint(InsertPt->getParent(), InsertPt);
700667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman
701667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  // Expand the expression into instructions.
70296fea337d27357e9b62abbf3d2d5ce29f1c8e870Anton Korobeynikov  Value *V = visit(S);
70340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman
704667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  // Remember the expanded value for this SCEV at this location.
705667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  InsertedExpressions[std::make_pair(S, InsertPt)] = V;
706667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman
707267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
70896fea337d27357e9b62abbf3d2d5ce29f1c8e870Anton Korobeynikov  return V;
70996fea337d27357e9b62abbf3d2d5ce29f1c8e870Anton Korobeynikov}
7101d09de3eca23267855e28297fcb40de3632ea47bDan Gohman
7111d09de3eca23267855e28297fcb40de3632ea47bDan Gohman/// getOrInsertCanonicalInductionVariable - This method returns the
7121d09de3eca23267855e28297fcb40de3632ea47bDan Gohman/// canonical induction variable of the specified type for the specified
7131d09de3eca23267855e28297fcb40de3632ea47bDan Gohman/// loop (inserting one if there is none).  A canonical induction variable
7141d09de3eca23267855e28297fcb40de3632ea47bDan Gohman/// starts at zero and steps by one on each iteration.
7151d09de3eca23267855e28297fcb40de3632ea47bDan GohmanValue *
7161d09de3eca23267855e28297fcb40de3632ea47bDan GohmanSCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
7171d09de3eca23267855e28297fcb40de3632ea47bDan Gohman                                                    const Type *Ty) {
7181d09de3eca23267855e28297fcb40de3632ea47bDan Gohman  assert(Ty->isInteger() && "Can only insert integer induction variables!");
7190bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  const SCEV *H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
72040a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman                                   SE.getIntegerSCEV(1, Ty), L);
721267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
722267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
72340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman  Value *V = expandCodeFor(H, 0, L->getHeader()->begin());
724267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman  if (SaveInsertBB)
725267a385342f2e7388f178b327dd87c5f29afd51bDan Gohman    Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
72640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman  return V;
7271d09de3eca23267855e28297fcb40de3632ea47bDan Gohman}
728